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

Revision 987, 3.7 KB checked in by dave, 10 years ago (diff)

new plants and butterflies

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") "cover"
52   (= type "plant-002") "canopy"
53   (= type "plant-003") "vertical"
54   (= type "apple") "canopy"
55   (= type "cherry") "canopy"))
56 
57(defn make-plant [pos type owner size]
58  (plant. (generate-id) pos type (plant-type->layer type)
59          'grow-a '() owner size 0 (+ 30 (Math/floor (rand 10))) start-health))
60
61(defn make-random-plant []
62  (make-plant
63   (make-vec2 (Math/floor (rand 15)) (Math/floor (rand 15)))
64   (rand-nth (list "plant-001" "plant-002" "plant-003" "apple" "cherry"))
65   "the garden"
66   (Math/round (+ 50 (rand 100)))))
67
68; the plant state machine, advance state, based on health
69(defn adv-state [state health]
70  (cond
71   (= state 'grow-a) (cond (> health min-health) 'grow-b :else (rand-nth (list 'grow-a 'grow-b)))
72   (= state 'grow-b) (cond (> health min-health) 'grow-c :else (rand-nth (list 'grow-b 'grow-c)))
73   (= state 'grow-c) (cond (> health min-health) 'grown :else (rand-nth (list 'grow-c 'grown)))
74   (= 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)
81   (= state 'fruit-a) 'fruit-b
82   (= state 'fruit-b) 'fruit-c
83   (= state 'fruit-c) 'grown
84   (= state 'ill-a) (cond (< health min-health) 'ill-b
85                (> 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
92                :else 'ill-c)
93   (= state 'decayed) 'decayed))
94
95(defn plant-update [plant time delta neighbours]
96  (modify
97   :health
98   (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))))
104   (modify
105    :timer
106    (fn [timer]
107      (+ timer delta))
108    (if (> (:timer plant) (:tick plant))
109      (modify
110       :state
111       (fn [state] (adv-state state (:health plant)))
112       (modify
113        :timer (fn [t] 0) plant))
114      plant))))
115
Note: See TracBrowser for help on using the repository browser.