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

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

connected game world to fatima

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