source: scenarios/GerminationX/oak/src/oak/plant.clj @ 1002

Revision 1002, 4.5 KB checked in by dave, 10 years ago (diff)

fixed debug

Line 
1;; Copyright (C) 2010 FoAM vzw
2;; This program is free software: you can redistribute it and/or modify
3;; it under the terms of the GNU Affero General Public License as
4;; published by the Free Software Foundation, either version 3 of the
5;; License, or (at your option) any later version.
6;;
7;; This program is distributed in the hope that it will be useful,
8;; but WITHOUT ANY WARRANTY; without even the implied warranty of
9;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10;; GNU Affero General Public License for more details.
11;;
12;; You should have received a copy of the GNU Affero General Public License
13;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
14
15(ns oak.plant
16  (:use
17   oak.vec2
18   oak.forms)
19  (:require
20   clojure.contrib.math))
21
22(def season-length (* 60 10))
23(def min-health 10)
24(def max-health 90)
25(def start-health 20)
26(def min-neighbours 2)
27(def max-neighbours 5)
28
29(defrecord plant
30  [id
31   pos
32   type
33   layer
34   state
35   picked-by
36   owner
37   size
38   timer
39   tick
40   health])
41 
42(defn plant-pos [plant] (:pos plant))
43(defn plant-type [plant] (:type plant))
44(defn plant-layer [plant] (:layer plant))
45(defn plant-state [plant] (:state plant))
46(defn plant-picked-by [plant] (:picked-by plant))
47(defn plant-owner [plant] (:owner plant))
48(defn plant-size [plant] (:size plant))
49
50(defn plant-type->layer [type]
51  (cond
52   (= type "plant-001") "cover"
53   (= type "plant-002") "canopy"
54   (= type "plant-003") "vertical"
55   (= type "apple") "canopy"
56   (= 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))
68 
69(defn make-plant [pos type owner size]
70  (plant. (generate-id) pos type (plant-type->layer type)
71          'grow-a '() owner size 0 (+ (/ season-length 10) (Math/floor (rand 10))) start-health))
72
73(defn make-random-plant []
74  (make-plant
75   (make-vec2 (Math/floor (rand 15)) (Math/floor (rand 15)))
76   (rand-nth (list "plant-001" "plant-002" "plant-003" "apple" "cherry"))
77   "the garden"
78   (Math/round (+ 1 (rand 10)))))
79
80; the plant state machine, advance state, based on health
81(defn adv-state [state health season]
82  (cond
83   (= state 'grow-a) (cond (> health min-health) 'grow-b :else (rand-nth (list 'grow-a 'grow-b)))
84   (= state 'grow-b) (cond (> health min-health) 'grow-c :else (rand-nth (list 'grow-b 'grow-c)))
85   (= state 'grow-c) (cond (> health min-health) 'grown :else (rand-nth (list 'grow-c 'grown)))
86   (= state 'grown) (cond
87                     (and (> health max-health)
88                          (or (= season 'spring)
89                              (= season 'summer)))
90                     'fruit-a
91                     (or (= season 'autumn) (= season 'winter))
92                     'decay-a
93                     :else 'grown)
94   (= state 'fruit-a) 'fruit-b
95   (= state 'fruit-b) 'fruit-c
96   (= state 'fruit-c) (if (or (= season 'autumn) (= season 'winter)) 'decay-a 'fruit-c)
97   (= state 'decay-a) 'decay-b
98   (= state 'decay-b) 'decay-c
99   (= state 'decay-c) (cond (and (or (= season 'spring) (= season 'summer))
100                                 (> health min-health)) 'grown
101                                 :else (if (< health min-health) 'ill-c 'decay-c))
102   (= state 'ill-c) (cond (< health min-health) 'decayed
103                (> health max-health) 'grown
104                :else 'ill-c)
105   (= state 'decayed) 'decayed))
106
107(defn load-companion-rules [filename]
108  (read-string (slurp filename)))
109
110(defn get-relationship [from to rules]
111  (nth (nth rules (plant-type->id from))
112       (plant-type->id to)))
113       
114(defn plant-update [plant time delta neighbours rules season]
115;  (println (str season " " (:state plant) " " (:health plant) " " (:timer plant) " " (:tick plant)))
116  (modify
117   :health
118   (fn [health]
119     health (max 0 (min 100
120                        (+ health
121                           (reduce
122                            (fn [r n]
123                              (+ r (get-relationship
124                                    (:type plant) (:type n) rules)))
125                            (if (empty? neighbours) -1 1)
126                            neighbours)))))
127   (modify
128    :timer
129    (fn [timer]
130      (+ timer delta))
131    (if (> (:timer plant) (:tick plant))
132      (modify
133       :state
134       (fn [state] (adv-state state
135                              (:health plant)
136                              season))
137       (modify
138        :timer (fn [t] 0) plant))
139      plant))))
140
Note: See TracBrowser for help on using the repository browser.