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

Revision 1009, 5.3 KB checked in by dave, 10 years ago (diff)

fixed character names, click-through plants working, misc fixes

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   fruit])
42 
43(defn plant-pos [plant] (:pos plant))
44(defn plant-type [plant] (:type plant))
45(defn plant-layer [plant] (:layer plant))
46(defn plant-state [plant] (:state plant))
47(defn plant-picked-by [plant] (:picked-by plant))
48(defn plant-owner [plant] (:owner plant))
49(defn plant-size [plant] (:size plant))
50
51(defn plant-type->layer [type]
52  (cond
53   (= type "dandelion") "cover"
54   (= type "clover") "cover"
55   (= type "aronia") "shrub"
56   (= type "apple") "tree"
57   (= type "cherry") "tree"))
58
59(defn plant-type->id [type]
60  (cond
61   (= type "cherry") 0
62   (= type "apple") 1
63   (= type "plant-002") 1 ; temp apple tree
64   (= type "aronia") 2
65   (= type "plant-003") 2 ; temp aronia
66   (= type "dandelion") 3
67   (= type "plant-001") 3 ; temp dandelion
68   (= type "clover") 4))
69
70(defn layer->spirit-name [layer]
71  (cond
72   (= layer "canopy") "CanopySpirit"
73   (= layer "vertical") "VerticalSpirit"
74   (= layer "cover") "CoverSpirit"
75   :else "UnknownSpirit"))
76
77(defn make-plant [pos type owner size]
78  (plant. (generate-id) pos type (plant-type->layer type)
79          'grow-a '() owner size 0 (+ (/ season-length 50) (Math/floor (rand 10))) start-health false))
80
81(defn make-random-plant []
82  (let [type (rand-nth (list "aronia" "dandelion" "apple" "cherry" "clover"))]
83    (make-plant
84     (make-vec2 (Math/floor (rand 15)) (Math/floor (rand 15)))
85     type
86     (layer->spirit-name (plant-type->layer type))
87     (Math/round (+ 1 (rand 10))))))
88
89; the plant state machine, advance state, based on health
90(defn adv-state [state health season annual]
91  (cond
92   (= state 'grow-a) (cond (> health min-health) 'grow-b :else (rand-nth (list 'grow-a 'grow-b)))
93   (= state 'grow-b) (cond (> health min-health) 'grow-c :else (rand-nth (list 'grow-b 'grow-c)))
94   (= state 'grow-c) (cond (> health min-health) 'grown :else (rand-nth (list 'grow-c 'grown)))
95   (= state 'grown) (cond
96                     (and (> health max-health)
97                          (or (= season 'spring)
98                              (= season 'summer)))
99                     'fruit-a
100                     (or (= season 'autumn) (= season 'winter)
101                         (< health min-health))
102                     'decay-a
103                     :else 'grown)
104   (= state 'fruit-a) (if (< health min-health) 'decay-a 'fruit-b)
105   (= state 'fruit-b) (if (< health min-health) 'decay-a'fruit-c)
106   (= state 'fruit-c) (if (or (= season 'autumn) (= season 'winter)
107                              (< health min-health))
108                        'decay-a 'grown)
109   (= state 'decay-a) 'decay-b
110   (= state 'decay-b) 'decay-c
111   (= state 'decay-c) (cond (and (or (= season 'spring) (= season 'summer))
112                                 (> health min-health))
113                            (if annual 'grow-a 'grown)
114                            :else
115                            (if (< health min-health) 'ill-c 'decay-c))
116   (= state 'ill-c) (cond (< health min-health) 'decayed
117                (> health max-health) 'grown
118                :else 'ill-c)
119   (= state 'decayed) 'decayed))
120
121(defn load-companion-rules [filename]
122  (read-string (slurp filename)))
123
124(defn get-relationship [from to rules]
125  (nth (nth rules (plant-type->id from))
126       (plant-type->id to)))
127       
128(defn plant-update [plant time delta neighbours rules season]
129;  (println (str season " " (:state plant) " " (:health plant) " " (:timer plant) " " (:tick plant)))
130  (modify
131   :health
132   (fn [health]
133     health (max 0 (min 100
134                        (+ health
135                           (reduce
136                            (fn [r n]
137                              (+ r (get-relationship
138                                    (:type plant) (:type n) rules)))
139                            (if (empty? neighbours) -1 1)
140                            neighbours)))))
141   (modify
142    :fruit
143    (fn [f]
144      (if (and (not f) (= (:state plant) 'fruit-c))
145        true f))
146    (modify
147     :timer
148     (fn [timer]
149       (+ timer delta))
150     (if (> (:timer plant) (:tick plant))
151       (modify
152        :state
153        (fn [state] (adv-state state
154                               (:health plant)
155                               season
156                               ; for the moment assume cover plants
157                               ; are annuals
158                               (= (:layer plant) "cover")))
159        (modify
160         :timer (fn [t] 0) plant))
161       plant)))))
162
Note: See TracBrowser for help on using the repository browser.