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

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

connected game world to fatima

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  ;(game-world-print (deref my-game-world))
47  (let [time (.getTime (java.util.Date.))]
48    (dosync (ref-set fatima-world
49                     (world-run
50                      (game-world-sync->fatima
51                       (deref fatima-world)
52                       (deref my-game-world)))))
53    (dosync (ref-set my-game-world
54                     (game-world-update
55                      (game-world-sync<-fatima
56                       (deref my-game-world)
57                       (deref fatima-world))
58                      time 1))))
59  (recur))
60
61(tick)
62
63(defroutes main-routes
64  (GET "/get-tile/:tilex/:tiley" [tilex tiley]
65       (let [tile (game-world-get-tile (deref my-game-world)
66                                       (make-vec2 (parse-number tilex)
67                                                  (parse-number tiley)))]
68         (if tile
69           (json/encode-to-str tile)
70           (json/encode-to-str '()))))
71  (GET "/make-plant/:tilex/:tiley/:posx/:posy/:type/:owner/:size"
72       [tilex tiley posx posy type owner size]
73       (println "making plant...")
74       (append-spit
75        log-filename
76        (str
77         (str (Date.)) " " owner " has created a " type " at tile "
78         tilex "," tiley " position " posx "," posy "\n"))
79       (dosync
80        (ref-set my-game-world
81                 (game-world-add-entity
82                  (deref my-game-world)
83                  (make-vec2 (parse-number tilex) (parse-number tiley))
84                  (make-plant (make-vec2 (parse-number posx) (parse-number posy)) type owner size))))
85       ;(game-world-save (deref my-game-world) state-filename)
86       ;(println (deref my-game-world))
87       (json/encode-to-str '("ok")))
88  (GET "/spirit-sprites/:name" [name]
89       (update-islands (str "./" name) (str "./" name))
90       (read-islands "./public/" name))
91
92  (GET "/spirit-info" []
93       (json/encode-to-str (map
94                            (fn [a]
95                              {:name (remote-agent-name a)
96                               :emotions (remote-agent-emotions a)})
97                            (world-agents (deref myworld)))
98                           :indent 2))
99  (GET "/perceive" []
100       (world-perceive-all (deref myworld)))
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) (println "DONE 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.