source: scenarios/GerminationX/oak/src/oak/game_world.clj @ 905

Revision 905, 4.4 KB checked in by dave, 10 years ago (diff)

fatima integration

Line 
1;; Copyright (C) 2010 FoAM vzw
2;; This program is free software: you can redistribute it and/or modify
3;; it under the terms of the GNU Affero General Public License as
4;; published by the Free Software Foundation, either version 3 of the
5;; License, or (at your option) any later version.
6;;
7;; This program is distributed in the hope that it will be useful,
8;; but WITHOUT ANY WARRANTY; without even the implied warranty of
9;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10;; GNU Affero General Public License for more details.
11;;
12;; You should have received a copy of the GNU Affero General Public License
13;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
14
15(ns oak.game-world
16  (:use
17   oak.forms
18   oak.vec2
19   oak.plant
20   oak.tile
21   oak.rand
22   oak.remote-agent
23   oak.spirit
24   oak.world)
25  (:require
26   clojure.contrib.math))
27
28(defrecord game-world
29  [players
30   tiles
31   spirits])
32
33(defn game-world-players [game-world] (:players game-world))
34(defn game-world-tiles [game-world] (:tiles game-world))
35(defn game-world-spirits [game-world] (:spirits game-world))
36
37(defn game-world-get-tile [game-world pos]
38  (reduce
39   (fn [r t]
40     (if (and (not r) (vec2-eq? pos (:pos t)))
41       t
42       r))
43   false
44   (:tiles game-world)))
45
46(defn game-world-add-tile [game-world tile]
47  (merge game-world {:tiles (cons tile (game-world-tiles game-world))}))
48
49(defn game-world-modify-tile [game-world pos f]
50  (modify :tiles
51          (fn [tiles]
52            (map
53             (fn [t]
54               (if (vec2-eq? (:pos t) pos) (f t) t))
55             tiles))
56          game-world))
57
58(defn game-world-add-entity [game-world tile-pos entity]
59  (let [tile (game-world-get-tile game-world tile-pos)]
60    (if (not tile)
61      (game-world-add-tile game-world (make-tile tile-pos (list entity)))
62      (game-world-modify-tile
63       game-world
64       tile-pos
65       (fn [tile]
66         (tile-add-entity tile entity))))))
67
68(defn make-game-world [num-plants area]
69  (reduce
70   (fn [world plant]
71     (game-world-add-entity
72      world
73      (make-vec2
74       (Math/round (* (rand-gaussian) area))
75       (Math/round (* (rand-gaussian) area)))
76      plant))
77   (game-world. () {} ())
78   (repeatedly num-plants (fn [] (make-random-plant)))))
79
80(defn game-world-print [game-world]
81  (doseq [tile (game-world-tiles game-world)]
82    (println (format "tile %d %d" (:x (tile-pos tile)) (:y (tile-pos tile))))
83    (doseq [plant (tile-entities tile)]
84      (println (format "plant %d %d state: %s health: %d"
85                       (:x (:pos plant)) (:y (:pos plant))
86                       (:state plant) (:health plant))))))
87
88(defn game-world-update [game-world time delta]
89  (modify :tiles
90          (fn [tiles]
91            (doall (map
92                    (fn [tile]
93                      (tile-update tile time delta))
94                    tiles)))
95          game-world))
96
97(defn game-world-find-spirit [game-world name]
98  (reduce
99   (fn [r spirit]
100     (if (and (not r) (= name (:name spirit)))
101       spirit r))
102   false
103   (:spirits game-world)))
104
105(defn game-world-sync<-fatima [game-world fatima-world]
106  (modify :spirits
107          (fn [spirits]
108            (reduce
109             (fn [spirits agent]
110               (let [spirit (game-world-find-spirit game-world
111                                                    (remote-agent-name agent))]
112                 (if spirit
113                   (cons (spirit-update spirit agent) spirits)
114                   (cons (make-spirit agent) spirits))))
115             '()
116             (world-agents fatima-world)))
117          game-world))
118
119(defn game-world-sync->fatima [fatima-world game-world]
120  (let [tile (game-world-get-tile game-world (make-vec2 0 0))]
121    (reduce
122     (fn [fw entity]
123       (cond
124        (or
125         (= (:state entity) 'grow-a)
126         (= (:state entity) 'fruit-a)
127         (= (:state entity) 'fruit-b)
128         (= (:state entity) 'fruit-c)
129         (= (:state entity) 'ill-a)
130         (= (:state entity) 'ill-b)
131         (= (:state entity) 'ill-c))
132        (do
133          (world-add-object fw
134                            {"name" (str (:layer entity) "-" (:state entity) "-" (str (:id entity)))
135                             "owner" (:layer entity)
136                             "position" (str (:x (:pos entity)) "," (:y (:pos entity)))
137                             "tile" "0,0"
138                             "type" "object"}))
139        :else fw))
140     fatima-world
141     (:entities tile))))
Note: See TracBrowser for help on using the repository browser.