/
tree.go
129 lines (109 loc) · 2.9 KB
/
tree.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import ("fmt"
"mud"
"strconv")
func init() {
mud.Loaders["fruitTree"] = LoadFruitTree
mud.PersistentKeys["fruitTree"] = []string { "id", "fruitName", "nextFlowering" }
mud.PlayerPerceptions["flower"] = DoesPerceiveFlower
}
type FruitTree struct {
mud.PhysicalObject
mud.Persister
mud.TimeListener
universe *mud.Universe
room *mud.Room
fruitName string
nextFlowering int
id int
ping chan int
}
type TreeFlowerStimulus struct {
mud.Stimulus
ft *FruitTree
}
func (s TreeFlowerStimulus) StimType() string { return "flower" }
func (s TreeFlowerStimulus) Description(p mud.Perceiver) string {
return "The " + s.ft.fruitName + " tree has blossomed.\n"
}
func DoesPerceiveFlower(p mud.Player, s mud.Stimulus) bool { return true }
func (f FruitTree) Visible() bool { return true }
func (f FruitTree) Carryable() bool { return false }
func (f FruitTree) TextHandles() []string {
return []string{}
}
func (f FruitTree) Description() string {
return "A " + f.fruitName + " tree";
}
func (f *FruitTree) SetRoom(r *mud.Room) { f.room = r }
func (f *FruitTree) Room() *mud.Room { return f.room }
func (f *FruitTree) Ping() chan int { return f.ping }
func (f *FruitTree) Bloom() {
mud.Log("Bloom in room",f.room)
newFruit := MakeFruit(f.universe, f.fruitName)
f.room.Broadcast(TreeFlowerStimulus{ft: f})
f.room.AddChild(newFruit)
}
func (f *FruitTree) UpdateTimeLoop() {
base := 300000
margin := 1250
for {
now := <- f.ping
if f.nextFlowering == -1 {
f.nextFlowering = now + randRange(base,margin)
}
if now == f.nextFlowering {
f.nextFlowering = now + randRange(base,margin)
f.Bloom()
}
}
}
func (f *FruitTree) PersistentValues() map[string]interface{} {
vals := make(map[string]interface{})
if(f.id > 0) {
vals["id"] = strconv.Itoa(f.id)
}
vals["fruitName"] = f.fruitName
vals["nextFlowering"] = strconv.Itoa(f.nextFlowering)
return vals
}
func (f *FruitTree) Save() string {
outID := f.universe.Store.SaveStructure("fruitTree",f.PersistentValues())
if(f.id == 0) {
f.id, _ = strconv.Atoi(outID)
}
return outID
}
func (f *FruitTree) DBFullName() string {
return fmt.Sprintf("fruitTree:%d",f.id)
}
func TreeCount(r *mud.Room) int {
count := 0
r.WithPhysObjects(func(p *mud.PhysicalObject) {
po := *p
if _, isPlant := po.(*FruitTree); isPlant {
count += 1
}
})
return count
}
func MakeFruitTree(u *mud.Universe, fruitName string) *FruitTree {
ft := new(FruitTree)
ft.universe = u
ft.fruitName = fruitName
ft.nextFlowering = -1
ft.ping = make(chan int)
u.Add(ft)
go ft.UpdateTimeLoop()
return ft
}
func LoadFruitTree(u *mud.Universe, id int) interface{} {
ft := MakeFruitTree(u, "peach")
vals := u.Store.LoadStructure(mud.PersistentKeys["fruitTree"],
mud.FieldJoin(":","fruitTree",strconv.Itoa(id)))
ft.id = id
ft.fruitName = vals["fruitName"].(string)
nextFloweringS := vals["nextFlowering"].(string)
ft.nextFlowering, _ = strconv.Atoi(nextFloweringS)
return ft
}