view org/world.org @ 273:c39b8b29a79e

fixed ambigous in-text function references
author Robert McIntyre <rlm@mit.edu>
date Wed, 15 Feb 2012 06:56:47 -0700
parents 0235c32152af
children 7e7f8d6d9ec5
line wrap: on
line source
1 #+title: A Virtual World for Sensate Creatures
2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu
4 #+description: Creating a Virtual World for AI constructs using clojure and JME3
5 #+keywords: JME3, clojure, virtual world, exception handling
6 #+SETUPFILE: ../../aurellem/org/setup.org
7 #+INCLUDE: ../../aurellem/org/level-0.org
8 #+BABEL: :mkdirp yes :noweb yes :exports both
10 * The World
12 There's no point in having senses if there's nothing to experience. In
13 this section I make some tools with which to build virtual worlds for
14 my characters to inhabit. If you look at the tutorials at [[http://www.jmonkeyengine.org/wiki/doku.php/jme3:beginner][the jme3
15 website]], you will see a pattern in how virtual worlds are normally
16 built. I call this "the Java way" of making worlds.
18 - The Java way:
19 - Create a class that extends =SimpleApplication= or =Application=
20 - Implement setup functions that create all the scene objects using
21 the inherited =assetManager= and call them by overriding the
22 =simpleInitApp= method.
23 - Create =ActionListeners= and add them to the =inputManager=
24 inherited from =Application= to handle key-bindings.
25 - Override =simpleUpdate= to implement game logic.
26 - Running/Testing an Application involves creating a new JVM,
27 running the App, and then closing everything down.
30 - A more Clojureish way:
31 - Use a map from keys->functions to specify key-bindings.
32 - Use functions to create objects separately from any particular
33 application.
34 - Use a REPL -- this means that there's only ever one JVM, and
35 Applications come and go.
37 Since most development work using jMonkeyEngine is done in Java, jme3
38 supports "the Java way" quite well out of the box. To work "the
39 clojure way", it necessary to wrap the JME3 elements that deal with
40 the Application life-cycle with a REPL driven interface.
42 The most important modifications are:
44 - Separation of Object life-cycles with the Application life-cycle.
45 - Functional interface to the underlying =Application= and
46 =SimpleApplication= classes.
48 ** Header
49 #+name: header
50 #+begin_src clojure :results silent
51 (ns cortex.world
52 "World Creation, abstracion over jme3's input system, and REPL
53 driven exception handling"
54 {:author "Robert McIntyre"}
56 (:use (clojure.contrib (def :only (defvar defn-memo))))
57 (:use [clojure.contrib [str-utils :only [re-gsub]]])
59 (:import com.aurellem.capture.IsoTimer)
61 (:import com.jme3.math.Vector3f)
62 (:import com.jme3.scene.Node)
63 (:import com.jme3.system.AppSettings)
64 (:import com.jme3.system.JmeSystem)
65 (:import com.jme3.input.KeyInput)
66 (:import com.jme3.input.controls.KeyTrigger)
67 (:import com.jme3.input.controls.MouseButtonTrigger)
68 (:import com.jme3.input.InputManager)
69 (:import com.jme3.bullet.BulletAppState)
70 (:import com.jme3.shadow.BasicShadowRenderer)
71 (:import com.jme3.app.SimpleApplication)
72 (:import com.jme3.input.controls.ActionListener)
73 (:import com.jme3.renderer.queue.RenderQueue$ShadowMode)
74 (:import org.lwjgl.input.Mouse)
75 (:import com.aurellem.capture.AurellemSystemDelegate))
77 #+end_src
79 ** General Settings
80 #+name: settings
81 #+begin_src clojure
82 (in-ns 'cortex.world)
84 (defvar *app-settings*
85 (doto (AppSettings. true)
86 (.setFullscreen false)
87 (.setTitle "Aurellem.")
88 ;; The "Send" AudioRenderer supports sumulated hearing.
89 (.setAudioRenderer "Send"))
90 "These settings control how the game is displayed on the screen for
91 debugging purposes. Use binding forms to change this if desired.
92 Full-screen mode does not work on some computers.")
94 (defn asset-manager
95 "returns a new, configured assetManager" []
96 (JmeSystem/newAssetManager
97 (.getResource
98 (.getContextClassLoader (Thread/currentThread))
99 "com/jme3/asset/Desktop.cfg")))
100 #+end_src
102 Normally, people just use the =AssetManager= inherited from
103 =Application= whenever they extend that class. However,
104 =AssetManagers= are useful on their own to create objects/ materials,
105 independent from any particular application. =(asset-manager)= makes
106 object creation less tightly bound to a particular Application
107 Instance.
109 ** Exception Protection
110 #+name: exceptions
111 #+begin_src clojure
112 (in-ns 'cortex.world)
114 (defmacro no-exceptions
115 "Sweet relief like I never knew."
116 [& forms]
117 `(try ~@forms (catch Exception e# (.printStackTrace e#))))
119 (defn thread-exception-removal
120 "Exceptions thrown in the graphics rendering thread generally cause
121 the entire REPL to crash! It is good to suppress them while trying
122 things out to shorten the debug loop."
123 []
124 (.setUncaughtExceptionHandler
125 (Thread/currentThread)
126 (proxy [Thread$UncaughtExceptionHandler] []
127 (uncaughtException
128 [thread thrown]
129 (println "uncaught-exception thrown in " thread)
130 (println (.getMessage thrown))))))
132 #+end_src
134 Exceptions thrown in the LWJGL render thread, if not caught, will
135 destroy the entire JVM process including the REPL and slow development
136 to a crawl. It is better to try to continue on in the face of
137 exceptions and keep the REPL alive as long as possible. Normally it
138 is possible to just exit the faulty Application, fix the bug,
139 reevaluate the appropriate forms, and be on your way, without
140 restarting the JVM.
142 ** Input
143 #+name: input
144 #+begin_src clojure
145 (in-ns 'cortex.world)
147 (defn static-integer?
148 "does the field represent a static integer constant?"
149 [#^java.lang.reflect.Field field]
150 (and (java.lang.reflect.Modifier/isStatic (.getModifiers field))
151 (integer? (.get field nil))))
153 (defn integer-constants [class]
154 (filter static-integer? (.getFields class)))
156 (defn-memo constant-map
157 "Takes a class and creates a map of the static constant integer
158 fields with their names. This helps with C wrappers where they have
159 just defined a bunch of integer constants instead of enums"
160 [class]
161 (let [integer-fields (integer-constants class)]
162 (into (sorted-map)
163 (zipmap (map #(.get % nil) integer-fields)
164 (map #(.getName %) integer-fields)))))
166 (defn all-keys
167 "Uses reflection to generate a map of string names to jme3 trigger
168 objects, which govern input from the keyboard and mouse"
169 []
170 (let [inputs (constant-map KeyInput)]
171 (assoc
172 (zipmap (map (fn [field]
173 (.toLowerCase (re-gsub #"_" "-" field))) (vals inputs))
174 (map (fn [val] (KeyTrigger. val)) (keys inputs)))
175 ;;explicitly add mouse controls
176 "mouse-left" (MouseButtonTrigger. 0)
177 "mouse-middle" (MouseButtonTrigger. 2)
178 "mouse-right" (MouseButtonTrigger. 1))))
180 (defn initialize-inputs
181 "Establish key-bindings for a particular virtual world."
182 [game input-manager key-map]
183 (doall
184 (map (fn [[name trigger]]
185 (.addMapping
186 ^InputManager input-manager
187 name (into-array (class trigger)
188 [trigger]))) key-map))
189 (doall
190 (map (fn [name]
191 (.addListener
192 ^InputManager input-manager game
193 (into-array String [name]))) (keys key-map))))
195 #+end_src
197 #+results: input
198 : #'cortex.world/initialize-inputs
200 These functions are for controlling the world through the keyboard and
201 mouse.
203 =constant-map= gets the numerical values for all the keys defined in
204 the =KeyInput= class.
206 #+begin_src clojure :exports both :results verbatim
207 (take 5 (vals (cortex.world/constant-map KeyInput)))
208 #+end_src
210 #+results:
211 : ("KEY_ESCAPE" "KEY_1" "KEY_2" "KEY_3" "KEY_4")
213 =(all-keys)= converts the constant names like =KEY_J= to the more
214 clojure-like =key-j=, and returns a map from these keys to
215 jMonkeyEngine =KeyTrigger= objects, which jMonkeyEngine3 uses as it's
216 abstraction over the physical keys. =all-keys= also adds the three
217 mouse button controls to the map.
219 #+begin_src clojure :exports both :results output
220 (require 'clojure.contrib.pprint)
221 (clojure.contrib.pprint/pprint
222 (take 6 (cortex.world/all-keys)))
223 #+end_src
225 #+results:
226 : (["key-n" #<KeyTrigger com.jme3.input.controls.KeyTrigger@9f9fec0>]
227 : ["key-apps" #<KeyTrigger com.jme3.input.controls.KeyTrigger@28edbe7f>]
228 : ["key-pgup" #<KeyTrigger com.jme3.input.controls.KeyTrigger@647fd33a>]
229 : ["key-f8" #<KeyTrigger com.jme3.input.controls.KeyTrigger@24f97188>]
230 : ["key-o" #<KeyTrigger com.jme3.input.controls.KeyTrigger@685c53ff>]
231 : ["key-at" #<KeyTrigger com.jme3.input.controls.KeyTrigger@4c3e2e5f>])
233 ** World Creation
234 #+name: world
235 #+begin_src clojure :results silent
236 (in-ns 'cortex.world)
238 (defn no-op
239 "Takes any number of arguments and does nothing."
240 [& _])
242 (defn traverse
243 "apply f to every non-node, deeply"
244 [f node]
245 (if (isa? (class node) Node)
246 (dorun (map (partial traverse f) (.getChildren node)))
247 (f node)))
249 (defn world
250 "the =world= function takes care of the details of initializing a
251 SimpleApplication.
253 ***** Arguments:
255 - root-node : a com.jme3.scene.Node object which contains all of
256 the objects that should be in the simulation.
258 - key-map : a map from strings describing keys to functions that
259 should be executed whenever that key is pressed.
260 the functions should take a SimpleApplication object and a
261 boolean value. The SimpleApplication is the current simulation
262 that is running, and the boolean is true if the key is being
263 pressed, and false if it is being released. As an example,
265 {\"key-j\" (fn [game value] (if value (println \"key j pressed\")))}
267 is a valid key-map which will cause the simulation to print a
268 message whenever the 'j' key on the keyboard is pressed.
270 - setup-fn : a function that takes a SimpleApplication object. It
271 is called once when initializing the simulation. Use it to
272 create things like lights, change the gravity, initialize debug
273 nodes, etc.
275 - update-fn : this function takes a SimpleApplication object and a
276 float and is called every frame of the simulation. The float
277 tells how many seconds is has been since the last frame was
278 rendered, according to whatever clock jme is currently
279 using. The default is to use IsoTimer which will result in this
280 value always being the same.
281 "
282 [root-node key-map setup-fn update-fn]
283 (let [physics-manager (BulletAppState.)]
284 (JmeSystem/setSystemDelegate (AurellemSystemDelegate.))
285 (doto
286 (proxy [SimpleApplication ActionListener] []
287 (simpleInitApp
288 []
289 (no-exceptions
290 ;; allow AI entities as much time as they need to think.
291 (.setTimer this (IsoTimer. 60))
292 (.setFrustumFar (.getCamera this) 300)
293 ;; Create default key-map.
294 (initialize-inputs this (.getInputManager this) (all-keys))
295 ;; Don't take control of the mouse
296 (org.lwjgl.input.Mouse/setGrabbed false)
297 ;; add all objects to the world
298 (.attachChild (.getRootNode this) root-node)
299 ;; enable physics
300 ;; add a physics manager
301 (.attach (.getStateManager this) physics-manager)
302 (.setGravity (.getPhysicsSpace physics-manager)
303 (Vector3f. 0 -9.81 0))
304 ;; go through every object and add it to the physics
305 ;; manager if relevant.
306 ;;(traverse (fn [geom]
307 ;; (dorun
308 ;; (for [n (range (.getNumControls geom))]
309 ;; (do
310 ;; (cortex.util/println-repl
311 ;; "adding " (.getControl geom n))
312 ;; (.add (.getPhysicsSpace physics-manager)
313 ;; (.getControl geom n))))))
314 ;; (.getRootNode this))
315 ;; call the supplied setup-fn
316 ;; simpler !
317 (.addAll (.getPhysicsSpace physics-manager) root-node)
318 (if setup-fn
319 (setup-fn this))))
320 (simpleUpdate
321 [tpf]
322 (no-exceptions
323 (update-fn this tpf)))
324 (onAction
325 [binding value tpf]
326 ;; whenever a key is pressed, call the function returned
327 ;; from key-map.
328 (no-exceptions
329 (if-let [react (key-map binding)]
330 (react this value)))))
331 ;; don't show a menu to change options.
332 (.setShowSettings false)
333 ;; continue running simulation even if the window has lost
334 ;; focus.
335 (.setPauseOnLostFocus false)
336 (.setSettings *app-settings*))))
338 #+end_src
341 =(world)= is the most important function here. It presents a more
342 functional interface to the Application life-cycle, and all its
343 arguments except =root-node= are plain immutable clojure data
344 structures. This makes it easier to extend functionally by composing
345 multiple functions together, and to add more keyboard-driven actions
346 by combining clojure maps.
350 * COMMENT code generation
351 #+begin_src clojure :tangle ../src/cortex/world.clj :noweb yes
352 <<header>>
353 <<settings>>
354 <<exceptions>>
355 <<input>>
356 <<world>>
357 #+end_src