-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth.lisp
60 lines (53 loc) · 2.1 KB
/
health.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
(in-package #:tree-sim)
(defgeneric is-dead(part)
(:documentation "check if this part is dead"))
(defmethod is-dead(part)
t)
(defmethod is-dead((part part))
(not (and part (> (health part) 0))))
(defgeneric health-check(part dna)
(:documentation "checks that the given part has enough supplies to survice, and if not, then reduces this parts health. otherwise heals it if such is needed"))
(defmethod health-check(part dna))
(defmethod health-check((part part) dna)
(unless (or (is-dead part) (not *use-supplies*))
(if (or (less (supplies part) (min-requirements part dna))
(and (max-requirements part dna)
(more (supplies part) (max-requirements part dna))))
(decf (health part) (wilter-rate dna))
(when (< (health part) 1)
(incf (health part) (revive-rate dna)))))
(health part))
(defmethod health-check((bud bud) dna)
(health-check (leaf bud) dna)
(with-slots ((leaf leaf)) bud
(when (and leaf (is-dead leaf) (<= (petiole-strength leaf) 0))
(setf (leaf bud) NIL)))
(call-next-method))
(defmethod health-check ((leaf leaf) dna)
(when (and (is-dead leaf) (> (petiole-strength leaf) 0))
(decf (petiole-strength leaf) (random 0.1)))
(when (and *seasons* (winter-p)
(not (and *use-supplies* *diffuse-abscisic-acid*)))
(decf (health leaf) (wilter-rate dna)))
(call-next-method))
(defmethod health-check((part segment) dna)
(health-check (apex part) dna)
(loop for bud in (buds part) do
(health-check bud dna))
(call-next-method))
(defgeneric get-children(part)
(:documentation "gets the given parts children"))
(defmethod get-children(part))
(defmethod get-children((part bud))
(when (leaf part)
(list (leaf part))))
(defmethod get-children((part segment))
(remove NIL (append (list (apex part)) (buds part))))
(defgeneric count-parts (part)
(:documentation "a helper function to count how many children parts the given part has"))
(defmethod count-parts(part) 0)
(defmethod count-parts((segment segment))
(+ (count-parts (apex segment))
(loop for bud in (buds segment) sum (count-parts bud))))
(defmethod count-parts((bud bud))
(if (leaf bud) 2 1))