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

Revision 905, 3.6 KB checked in by dave, 10 years ago (diff)

fatima integration

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 min-health 10)
23(def max-health 90)
24(def start-health 20)
25(def min-neighbours 2)
26(def max-neighbours 5)
27
28(defrecord plant
29  [id
30   pos
31   type
32   layer
33   state
34   picked-by
35   owner
36   size
37   timer
38   tick
39   health])
40 
41(defn plant-pos [plant] (:pos plant))
42(defn plant-type [plant] (:type plant))
43(defn plant-layer [plant] (:layer plant))
44(defn plant-state [plant] (:state plant))
45(defn plant-picked-by [plant] (:picked-by plant))
46(defn plant-owner [plant] (:owner plant))
47(defn plant-size [plant] (:size plant))
48
49(defn plant-type->layer [type]
50  (cond
51   (= type "plant-001") "canopy"
52   (= type "plant-002") "vertical"
53   (= type "plant-003") "cover"))
54 
55(defn make-plant [pos type owner size]
56  (plant. (generate-id) pos type (plant-type->layer type)
57          'grow-a '() owner size 0 (+ 30 (Math/floor (rand 10))) start-health))
58
59(defn make-random-plant []
60  (make-plant
61   (make-vec2 (Math/floor (rand 15)) (Math/floor (rand 15)))
62   (rand-nth (list "plant-001" "plant-002" "plant-003"))
63   "the garden"
64   (Math/round (+ 50 (rand 100)))))
65
66; the plant state machine, advance state, based on health
67(defn adv-state [state health]
68  (cond
69   (= state 'grow-a) (cond (> health min-health) 'grow-b :else (rand-nth (list 'grow-a 'grow-b)))
70   (= state 'grow-b) (cond (> health min-health) 'grow-c :else (rand-nth (list 'grow-b 'grow-c)))
71   (= state 'grow-c) (cond (> health min-health) 'grown :else (rand-nth (list 'grow-c 'grown)))
72   (= state 'grown) (cond
73           (> health max-health) (rand-nth (list 'grown 'fruit-a))
74           (< health min-health) (rand-nth (list 'grown 'ill-a))
75           :else 'grown)
76   (= state 'decay-a) (cond (> health max-health) 'decay-a :else 'decay-b)
77   (= state 'decay-b) (cond (> health max-health) 'decay-b :else 'decay-c)
78   (= state 'decay-c) (cond (> health max-health) 'decay-c :else 'decayed)
79   (= state 'fruit-a) 'fruit-b
80   (= state 'fruit-b) 'fruit-c
81   (= state 'fruit-c) 'grown
82   (= state 'ill-a) (cond (< health min-health) 'ill-b
83                (> health max-health) 'grown
84                :else 'ill-a)
85   (= state 'ill-b) (cond (< health min-health) 'ill-c
86                (> health max-health) 'ill-a
87                :else 'ill-b)
88   (= state 'ill-c) (cond (< health min-health) 'decayed
89                (> health max-health) 'ill-b
90                :else 'ill-c)
91   (= state 'decayed) 'decayed))
92
93(defn plant-update [plant time delta neighbours]
94  (modify
95   :health
96   (fn [health]
97     (cond
98      (< (count neighbours) min-neighbours) (max 0 (- health 1))
99      (> (count neighbours) max-neighbours) (max 0 (- health 1))
100      (= (:state plant) 'fruit-c) (max 0 (- health 10))
101      :else (min 100 (+ health 1))))
102   (modify
103    :timer
104    (fn [timer]
105      (+ timer delta))
106    (if (> (:timer plant) (:tick plant))
107      (modify
108       :state
109       (fn [state] (adv-state state (:health plant)))
110       (modify
111        :timer (fn [t] 0) plant))
112      plant))))
113
Note: See TracBrowser for help on using the repository browser.