Ignore:
Timestamp:
02/25/2011 04:00:29 PM (9 years ago)
Author:
dave
Message:

seasonal variation and companion planting rules, not tested yet

File:
1 edited

Legend:

Unmodified
Added
Removed
  • scenarios/GerminationX/oak/src/oak/plant.clj

    r987 r995  
    2020   clojure.contrib.math)) 
    2121 
     22(def season-length (* 60 2)) 
    2223(def min-health 10) 
    2324(def max-health 90) 
     
    5455   (= type "apple") "canopy" 
    5556   (= type "cherry") "canopy")) 
     57 
     58(defn plant-type->id [type] 
     59  (cond 
     60   (= type "cherry") 0 
     61   (= type "apple") 1 
     62   (= type "plant-002") 1 ; temp apple tree 
     63   (= type "aronia") 2 
     64   (= type "plant-003") 2 ; temp aronia 
     65   (= type "dandelion") 3 
     66   (= type "plant-001") 3 ; temp dandelion 
     67   (= type "clover") 4)) 
    5668   
    5769(defn make-plant [pos type owner size] 
    5870  (plant. (generate-id) pos type (plant-type->layer type) 
    59           'grow-a '() owner size 0 (+ 30 (Math/floor (rand 10))) start-health)) 
     71          'grow-a '() owner size 0 (+ (/ season-length 80) (Math/floor (rand 10))) start-health)) 
    6072 
    6173(defn make-random-plant [] 
     
    6476   (rand-nth (list "plant-001" "plant-002" "plant-003" "apple" "cherry")) 
    6577   "the garden" 
    66    (Math/round (+ 50 (rand 100))))) 
     78   (Math/round (+ 1 (rand 10))))) 
    6779 
    6880; the plant state machine, advance state, based on health 
    69 (defn adv-state [state health] 
     81(defn adv-state [state health season] 
     82  (println "advancing state...") 
    7083  (cond 
    7184   (= state 'grow-a) (cond (> health min-health) 'grow-b :else (rand-nth (list 'grow-a 'grow-b))) 
     
    7386   (= state 'grow-c) (cond (> health min-health) 'grown :else (rand-nth (list 'grow-c 'grown))) 
    7487   (= state 'grown) (cond 
    75            (> health max-health) (rand-nth (list 'grown 'fruit-a)) 
    76            (< health min-health) (rand-nth (list 'grown 'ill-a)) 
    77            :else 'grown) 
    78    (= state 'decay-a) (cond (> health max-health) 'decay-a :else 'decay-b) 
    79    (= state 'decay-b) (cond (> health max-health) 'decay-b :else 'decay-c) 
    80    (= state 'decay-c) (cond (> health max-health) 'decay-c :else 'decayed) 
     88                     (and (> health max-health) 
     89                          (or (= season 'spring) 
     90                              (= season 'summer))) 
     91                     'fruit-a 
     92                     (or (= season 'autumn) (= season 'winter)) 
     93                     'decay-a 
     94                     :else 'grown) 
    8195   (= state 'fruit-a) 'fruit-b 
    8296   (= state 'fruit-b) 'fruit-c 
    83    (= state 'fruit-c) 'grown 
    84    (= state 'ill-a) (cond (< health min-health) 'ill-b 
     97   (= state 'fruit-c) (if (or (= season 'autumn) (= season 'winter)) 'decay-a 'fruit-c) 
     98   (= state 'decay-a) 'decay-b 
     99   (= state 'decay-b) 'decay-c 
     100   (= state 'decay-c) (cond (and (or (= season 'spring) (= season 'summer)) 
     101                                 (> health min-health)) 'grown 
     102                                 :else (if (< health min-health) 'ill-c 'decay-c)) 
     103   (= state 'ill-c) (cond (< health min-health) 'decayed 
    85104                (> health max-health) 'grown 
    86                 :else 'ill-a) 
    87    (= state 'ill-b) (cond (< health min-health) 'ill-c 
    88                 (> health max-health) 'ill-a 
    89                 :else 'ill-b) 
    90    (= state 'ill-c) (cond (< health min-health) 'decayed 
    91                 (> health max-health) 'ill-b 
    92105                :else 'ill-c) 
    93106   (= state 'decayed) 'decayed)) 
    94107 
    95 (defn plant-update [plant time delta neighbours] 
     108(defn load-companion-rules [filename] 
     109  (read-string (slurp filename))) 
     110 
     111(defn get-relationship [from to rules] 
     112  (nth (nth rules (plant-type->id from)) 
     113       (plant-type->id to))) 
     114        
     115(defn plant-update [plant time delta neighbours rules season] 
     116  (println (str season " " (:state plant) " " (:health plant))) 
    96117  (modify 
    97118   :health 
    98119   (fn [health] 
    99      (cond 
    100       (< (count neighbours) min-neighbours) (max 0 (- health 1)) 
    101       (> (count neighbours) max-neighbours) (max 0 (- health 1)) 
    102       (= (:state plant) 'fruit-c) (max 0 (- health 10)) 
    103       :else (min 100 (+ health 1)))) 
     120     health (max 0 (min 100 
     121                        (+ health 
     122                           (reduce 
     123                            (fn [r n] 
     124                              (+ r (get-relationship 
     125                                    (:type plant) (:type n) rules))) 
     126                            (if (empty? neighbours) -1 1) 
     127                            neighbours))))) 
    104128   (modify 
    105129    :timer 
     
    109133      (modify 
    110134       :state 
    111        (fn [state] (adv-state state (:health plant))) 
     135       (fn [state] (adv-state state 
     136                              (:health plant) 
     137                              season)) 
    112138       (modify 
    113139        :timer (fn [t] 0) plant)) 
Note: See TracChangeset for help on using the changeset viewer.