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

Revision 1004, 4.3 KB checked in by dave, 10 years ago (diff)

lots of playablity stuff for pixelache workshop, FAtiMA actions nowdisplayed

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   oak.forms)
16  (:import
17   java.util.concurrent.Executors
18   java.util.Date)
19  (:require [compojure.route :as route]
20            [org.danlarkin.json :as json]))
21
22(defn game-world-save [game-world fn]
23  (serialise game-world fn))
24
25(defn game-world-load [fn]
26  (deserialise fn))
27
28(def state-filename "state.txt")
29(def log-filename "public/log.txt")
30(def game-world-tick 1)
31
32(def fatima-world
33     (ref
34      (make-world
35       46874
36       "data/characters/minds/language/agent/en/language-set-1"
37       "data/characters/minds/Actions.xml"
38       (list))))
39
40;(def my-game-world (ref (game-world-load state-filename)))
41(def my-game-world (ref (make-game-world 1000 1)))
42
43(append-spit log-filename (str (str (Date.)) " server started\n"))
44
45(defn tick []
46  (Thread/sleep 1000)
47  ;(println ".")
48  ;(game-world-print (deref my-game-world))
49  (let [time (/ (.getTime (java.util.Date.)) 1000.0)]
50    (dosync (ref-set fatima-world
51                     (world-run
52                      (game-world-sync->fatima
53                       (deref fatima-world)
54                       (deref my-game-world)
55                       time) time)))
56    (dosync (ref-set my-game-world
57                     (game-world-update
58                      (game-world-sync<-fatima
59                       (deref my-game-world)
60                       (deref fatima-world))
61                      time 1))))
62  (recur))
63
64;(tick)
65
66(defroutes main-routes
67  (GET "/get-tile/:tilex/:tiley" [tilex tiley]
68       (let [tile (game-world-get-tile (deref my-game-world)
69                                       (make-vec2 (parse-number tilex)
70                                                  (parse-number tiley)))]
71         (if tile
72           (json/encode-to-str tile)
73           (json/encode-to-str '()))))
74
75  (GET "/make-plant/:tilex/:tiley/:posx/:posy/:type/:owner/:size"
76       [tilex tiley posx posy type owner size]
77       (append-spit
78        log-filename
79        (str
80         (str (Date.)) " " owner " has created a " type " at tile "
81         tilex "," tiley " position " posx "," posy "\n"))
82       (dosync
83        (ref-set my-game-world
84                 (game-world-add-entity
85                  (deref my-game-world)
86                  (make-vec2 (parse-number tilex) (parse-number tiley))
87                  (make-plant (make-vec2 (parse-number posx) (parse-number posy)) type owner size))))
88       (game-world-save (deref my-game-world) state-filename)
89       ;(println (deref my-game-world))
90       (json/encode-to-str '("ok")))
91
92  (GET "/pick/:tilex/:tiley/:plant-id" [tilex tiley plant-id]
93       (dosync
94        (ref-set my-game-world
95                 (game-world-modify-tile
96                  (deref my-game-world)
97                  (make-vec2 (parse-number tilex)
98                             (parse-number tiley))
99                  (fn [tile]
100                    (tile-modify-entity
101                     tile
102                     (parse-number plant-id)
103                     (fn [plant]
104                       (modify :fruit (fn [f] false) plant)))))))
105       (json/encode-to-str '("ok")))
106
107  (GET "/hiscores" []
108       (json/encode-to-str
109        (map
110         (fn [s]
111           (list (first s) (second s)))
112         (game-world-hiscores (deref my-game-world)))))
113 
114  (GET "/spirit-sprites/:name" [name]
115       ;(update-islands (str "./" name) (str "./" name))
116       (read-islands (str "./public/" name)))
117
118  (GET "/spirit-info" []
119       (json/encode-to-str (:spirits (deref my-game-world))))
120
121  (GET "/perceive" []
122       (world-perceive-all (deref fatima-world))
123       (json/encode-to-str '("ok")))
124 
125  (comment
126  (GET "/add-object/:obj" [obj]
127       (println (str "adding " obj))
128       (dosync (ref-set myworld (world-add-object (deref myworld)
129                                                  (load-object obj))))))
130 
131  (route/not-found "<h1>Page not found</h1>"))
132 
133(let [pool (Executors/newFixedThreadPool 2)
134      tasks (list (fn []
135                    (run-jetty (wrap-file main-routes "public") {:port 8001}))
136                  (fn []
137                    (tick)))]
138   
139  (doseq [future (.invokeAll pool tasks)]
140    (.get future))
141  (.shutdown pool))
142
Note: See TracBrowser for help on using the repository browser.