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

Revision 999, 3.5 KB checked in by dave, 9 years ago (diff)

remote update of characters, auto polling

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 1000 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.)) 1000.0)]
49    (dosync (ref-set fatima-world
50                     (world-run
51                      (game-world-sync->fatima
52                       (deref fatima-world)
53                       (deref my-game-world)
54                       time) time)))
55    (dosync (ref-set my-game-world
56                     (game-world-update
57                      (game-world-sync<-fatima
58                       (deref my-game-world)
59                       (deref fatima-world))
60                      time 1))))
61  (recur))
62
63;(tick)
64
65(defroutes main-routes
66  (GET "/get-tile/:tilex/:tiley" [tilex tiley]
67       (let [tile (game-world-get-tile (deref my-game-world)
68                                       (make-vec2 (parse-number tilex)
69                                                  (parse-number tiley)))]
70         (if tile
71           (json/encode-to-str tile)
72           (json/encode-to-str '()))))
73
74  (GET "/make-plant/:tilex/:tiley/:posx/:posy/:type/:owner/:size"
75       [tilex tiley posx posy type owner size]
76       (append-spit
77        log-filename
78        (str
79         (str (Date.)) " " owner " has created a " type " at tile "
80         tilex "," tiley " position " posx "," posy "\n"))
81       (dosync
82        (ref-set my-game-world
83                 (game-world-add-entity
84                  (deref my-game-world)
85                  (make-vec2 (parse-number tilex) (parse-number tiley))
86                  (make-plant (make-vec2 (parse-number posx) (parse-number posy)) type owner size))))
87       ;(game-world-save (deref my-game-world) state-filename)
88       ;(println (deref my-game-world))
89       (json/encode-to-str '("ok")))
90 
91  (GET "/spirit-sprites/:name" [name]
92       ;(update-islands (str "./" name) (str "./" name))
93       (read-islands (str "./public/" name)))
94
95  (GET "/spirit-info" []
96       (json/encode-to-str (:spirits (deref my-game-world))))
97
98  (GET "/perceive" []
99       (world-perceive-all (deref fatima-world))
100       (json/encode-to-str '("ok")))
101 
102  (comment
103  (GET "/add-object/:obj" [obj]
104       (println (str "adding " obj))
105       (dosync (ref-set myworld (world-add-object (deref myworld)
106                                                  (load-object obj))))))
107 
108  (route/not-found "<h1>Page not found</h1>"))
109 
110(let [pool (Executors/newFixedThreadPool 2)
111      tasks (list (fn []
112                    (run-jetty (wrap-file main-routes "public") {:port 8001}))
113                  (fn []
114                    (tick)))]
115   
116  (doseq [future (.invokeAll pool tasks)]
117    (.get future))
118  (.shutdown pool))
119
Note: See TracBrowser for help on using the repository browser.