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

Revision 1000, 4.6 KB checked in by dave, 10 years ago (diff)

debugging the server

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  (println "hello")
83  (cond
84   (= state 'grow-a) (cond (> health min-health) 'grow-b :else (rand-nth (list 'grow-a 'grow-b)))
85   (= state 'grow-b) (cond (> health min-health) 'grow-c :else (rand-nth (list 'grow-b 'grow-c)))
86   (= state 'grow-c) (cond (> health min-health) 'grown :else (rand-nth (list 'grow-c 'grown)))
87   (= state 'grown) (cond
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)
95   (= state 'fruit-a) 'fruit-b
96   (= state 'fruit-b) 'fruit-c
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
104                (> health max-health) 'grown
105                :else 'ill-c)
106   (= state 'decayed) 'decayed))
107
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) " " (:timer plant) " " (:tick plant)))
117  (modify
118   :health
119   (fn [health]
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)))))
128   (modify
129    :timer
130    (fn [timer]
131      (+ timer delta))
132    (if (> (:timer plant) (:tick plant))
133      (modify
134       :state
135       (fn [state] (adv-state state
136                              (:health plant)
137                              season))
138       (modify
139        :timer (fn [t] 0) plant))
140      plant))))
141
Note: See TracBrowser for help on using the repository browser.