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

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

fatima integration

Line 
1(ns oak.core
2  (:use
3   compojure.core
4   clojure.contrib.duck-streams
5   ring.adapter.jetty
6   ring.middleware.file
7   oak.world
8   oak.remote-agent
9   oak.io
10   oak.island
11   oak.game-world
12   oak.vec2
13   oak.plant
14   oak.tile)
15  (:import
16   java.util.concurrent.Executors
17   java.util.Date)
18  (:require [compojure.route :as route]
19            [org.danlarkin.json :as json]))
20
21(defn game-world-save [game-world fn]
22  (serialise game-world fn))
23
24(defn game-world-load [fn]
25  (deserialise fn))
26
27(def state-filename "state.txt")
28(def log-filename "public/log.txt")
29(def game-world-tick 1)
30
31(def fatima-world
32     (ref
33      (make-world
34       46874
35       "data/characters/minds/language/agent/en/language-set-1"
36       "data/characters/minds/Actions.xml"
37       (list))))
38
39;(def my-game-world (ref (game-world-load state-filename)))
40(def my-game-world (ref (make-game-world 100 1)))
41
42(append-spit log-filename (str (str (Date.)) " server started\n"))
43
44(defn tick []
45  (Thread/sleep 1000)
46  (println ".")
47  ;(game-world-print (deref my-game-world))
48  (let [time (.getTime (java.util.Date.))]
49    (dosync (ref-set fatima-world
50                     (world-run
51                      (game-world-sync->fatima
52                       (deref fatima-world)
53                       (deref my-game-world)))))
54    (dosync (ref-set my-game-world
55                     (game-world-update
56                      (game-world-sync<-fatima
57                       (deref my-game-world)
58                       (deref fatima-world))
59                      time 1))))
60  (recur))
61
62;(tick)
63
64(defroutes main-routes
65  (GET "/get-tile/:tilex/:tiley" [tilex tiley]
66       (let [tile (game-world-get-tile (deref my-game-world)
67                                       (make-vec2 (parse-number tilex)
68                                                  (parse-number tiley)))]
69         (if tile
70           (json/encode-to-str tile)
71           (json/encode-to-str '()))))
72
73  (GET "/make-plant/:tilex/:tiley/:posx/:posy/:type/:owner/:size"
74       [tilex tiley posx posy type owner size]
75       (append-spit
76        log-filename
77        (str
78         (str (Date.)) " " owner " has created a " type " at tile "
79         tilex "," tiley " position " posx "," posy "\n"))
80       (dosync
81        (ref-set my-game-world
82                 (game-world-add-entity
83                  (deref my-game-world)
84                  (make-vec2 (parse-number tilex) (parse-number tiley))
85                  (make-plant (make-vec2 (parse-number posx) (parse-number posy)) type owner size))))
86       ;(game-world-save (deref my-game-world) state-filename)
87       ;(println (deref my-game-world))
88       (json/encode-to-str '("ok")))
89 
90  (GET "/spirit-sprites/:name" [name]
91       (update-islands (str "./" name) (str "./" name))
92       (read-islands (str "./public/" name)))
93
94  (GET "/spirit-info" []
95       (json/encode-to-str (map
96                            (fn [a]
97                              {:name (remote-agent-name a)
98                               :emotions (remote-agent-emotions a)})
99                            (world-agents (deref fatima-world)))
100                           :indent 2))
101  (GET "/perceive" []
102       (world-perceive-all (deref fatima-world))
103       (json/encode-to-str '("ok")))
104 
105  (comment
106  (GET "/add-object/:obj" [obj]
107       (println (str "adding " obj))
108       (dosync (ref-set myworld (world-add-object (deref myworld)
109                                                  (load-object obj))))))
110 
111  (route/not-found "<h1>Page not found</h1>"))
112 
113(let [pool (Executors/newFixedThreadPool 2)
114      tasks (list (fn []
115                    (run-jetty (wrap-file main-routes "public") {:port 8001}))
116                  (fn []
117                    (tick)))]
118   
119  (doseq [future (.invokeAll pool tasks)]
120    (.get future))
121  (.shutdown pool))
122
Note: See TracBrowser for help on using the repository browser.