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

Revision 873, 3.6 KB checked in by dave, 10 years ago (diff)

larger visible world, hacked plant types, lazy stack problem possibly fixed

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(comment (def myworld
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 "WiltedVine"
38             "AppleTree"))))
39         )
40
41;(def my-game-world (ref (game-world-load state-filename)))
42(def my-game-world (ref (make-game-world 5000 4)))
43
44(append-spit log-filename (str (str (Date.)) " server started\n"))
45
46(defn parse [s]
47  (try (Integer/parseInt (.trim s))
48       (catch NumberFormatException e nil)))
49
50(defroutes main-routes
51  (GET "/get-tile/:tilex/:tiley" [tilex tiley]
52       (let [tile (game-world-get-tile (deref my-game-world)
53                                       (make-vec2 (parse tilex)
54                                                  (parse tiley)))]
55         (if tile
56           (json/encode-to-str tile)
57           (json/encode-to-str '()))))
58  (GET "/make-plant/:tilex/:tiley/:posx/:posy/:type/:owner/:size"
59       [tilex tiley posx posy type owner size]
60       (println "making plant...")
61       (append-spit
62        log-filename
63        (str
64         (str (Date.)) " " owner " has created a " type " at tile "
65         tilex "," tiley " position " posx "," posy "\n"))
66       (dosync
67        (ref-set my-game-world
68                 (game-world-add-entity
69                  (deref my-game-world)
70                  (make-vec2 (parse tilex) (parse tiley))
71                  (make-plant (make-vec2 (parse posx) (parse posy)) type owner size))))
72       ;(game-world-save (deref my-game-world) state-filename)
73       ;(println (deref my-game-world))
74       (json/encode-to-str '("ok")))
75  (GET "/spirit-sprites" []
76       (println (read-islands "./public/islands"))
77       (read-islands "./public/islands"))
78
79  (comment
80  (GET "/agent-info" []
81       (json/encode-to-str (map
82                            (fn [a]
83                              {:name (remote-agent-name a)
84                               :emotions (remote-agent-emotions a)})
85                            (world-agents (deref myworld)))
86                           :indent 2))
87  (GET "/perceive" []
88       (world-perceive-all (deref myworld)))
89 
90  (GET "/add-object/:obj" [obj]
91       (println (str "adding " obj))
92       (dosync (ref-set myworld (world-add-object (deref myworld)
93                                                  (load-object obj))))))
94 
95  (route/not-found "<h1>Page not found</h1>"))
96
97(defn tick []
98  (Thread/sleep 1000)
99  ;(game-world-print (deref my-game-world))
100  (let [time (.getTime (java.util.Date.))]
101    ;(dosync
102     ;(ref-set myworld
103     ;         (world-run (deref myworld))))
104     (dosync (ref-set my-game-world
105              (game-world-update (deref my-game-world)
106                                 time 1))))
107  (println "tick...")
108  (recur))
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.