Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add starvation.

  • Loading branch information...
commit 02d91ae5f794bb8fcc6aaf4c38580438c6cb9db5 1 parent c6887c8
Steve Losh authored August 04, 2012
3  src/ruin/core.clj
@@ -70,7 +70,8 @@
70 70
 
71 71
 (defn create-initial-population []
72 72
   (let [population (into {}
73  
-                         (gen-things 10 make-person))]
  73
+                         (gen-things 10 #(make-person [(rand-int 50)
  74
+                                                       (rand-int 40)])))]
74 75
     (dorun (map start-person (vals population)))
75 76
     population))
76 77
 
36  src/ruin/entities.clj
@@ -8,10 +8,10 @@
8 8
 (defn get-entity-id []
9 9
   (swap! eid inc))
10 10
 
11  
-(defn make-person []
12  
-  (agent {:nutrition 100
  11
+(defn make-person [location]
  12
+  (agent {:nutrition (rand-int 5)
13 13
           :id (get-entity-id)
14  
-          :location [(rand-int 40) (rand-int 40)]
  14
+          :location location
15 15
           :type :person
16 16
           :glyph "@"
17 17
           :happiness 50}))
@@ -23,14 +23,34 @@
23 23
         new-location (map + offset (:location person))]
24 24
     (assoc person :location new-location)))
25 25
 
  26
+(defn try-walking [person]
  27
+  (if (< (rand) 0.5)
  28
+    (walk-random person)
  29
+    person))
  30
+
  31
+(defn try-eating [person tick]
  32
+  (if (zero? (mod tick 2))
  33
+    (update-in person [:nutrition] dec)
  34
+    person))
  35
+
  36
+(defn check-starved [person]
  37
+  (if-not (pos? (:nutrition person))
  38
+    (do
  39
+      (alter (:entities @game) dissoc (:id person))
  40
+      (assoc person :dead true))
  41
+    person))
  42
+
  43
+
26 44
 (defn tick-person [person tick]
  45
+  (Thread/sleep 1000)
27 46
   (dosync
28  
-    (when (:state @game)
  47
+    (when (and (:state @game)
  48
+               (not (:dead person)))
29 49
       (send-off *agent* #'tick-person (inc tick)))
30  
-    (Thread/sleep 1000)
31  
-    (if (< (rand) 0.5)
32  
-      (walk-random person)
33  
-      person)))
  50
+    (-> person
  51
+      try-walking
  52
+      (try-eating tick)
  53
+      check-starved)))
34 54
 
35 55
 
36 56
 (defn start-person [p]

0 notes on commit 02d91ae

Please sign in to comment.
Something went wrong with that request. Please try again.