This repository has been archived by the owner on Aug 30, 2018. It is now read-only.
/
schedule.go
162 lines (135 loc) · 3.48 KB
/
schedule.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package world
import (
"fmt"
)
type Schedule interface {
ObjectLike
// Act returns the number of ticks the object should wait before
// calling Act again. 0 means to call Act on the next tick.
// Returning false for the second value cancels the schedule.
Act(Living) (uint, bool)
// ShouldSave returns false if this schedule is impossible to save (for
// example if it requires a pointer to another object).
ShouldSave() bool
// Target returns the target of this schedule, or nil if this schedule
// does not target an object.
Target() ObjectLike
}
type cancelSchedule struct {
Object
}
func init() {
Register("nosched", Schedule((*cancelSchedule)(nil)))
}
func (s *cancelSchedule) Act(o Living) (uint, bool) { return 0, false }
func (s *cancelSchedule) ShouldSave() bool { return true }
func (s *cancelSchedule) Target() ObjectLike { return nil }
type walkSchedule struct {
Object
ex, ey uint8
path [][2]uint8
stopEarly bool
delay uint
}
func init() {
Register("walksched", Schedule((*walkSchedule)(nil)))
}
func NewWalkSchedule(ex, ey uint8, stopEarly bool, delay uint) Schedule {
return InitObject(&walkSchedule{
ex: ex,
ey: ey,
stopEarly: stopEarly,
delay: delay,
}).(Schedule)
}
func (s *walkSchedule) Act(o Living) (uint, bool) {
t := o.Position()
if t == nil {
return 0, false
}
if s.path == nil {
s.path = t.Zone().Path(t, t.Zone().Tile(s.ex, s.ey), s.stopEarly)
}
if len(s.path) <= 1 {
return 0, false
}
x, y := s.path[0][0], s.path[0][1]
s.path = s.path[1:]
if tx, ty := t.Position(); tx != x && ty != y {
return 0, false
}
x, y = s.path[0][0], s.path[0][1]
if t.Zone().Tile(x, y).Blocked() {
return 0, false
}
t.Move(o, t.Zone().Tile(x, y))
return 2 + s.delay, true
}
func (s *walkSchedule) ShouldSave() bool { return true }
func (s *walkSchedule) Target() ObjectLike { return nil }
func (s *walkSchedule) Save() (uint, interface{}, []ObjectLike) {
return 0, map[string]interface{}{
"ex": s.ex,
"ey": s.ey,
"se": s.stopEarly,
"d": s.delay,
}, nil
}
func (s *walkSchedule) Load(version uint, data interface{}, attached []ObjectLike) {
switch version {
case 0:
dataMap := data.(map[string]interface{})
s.ex = dataMap["ex"].(uint8)
s.ey = dataMap["ey"].(uint8)
s.stopEarly, _ = dataMap["se"].(bool)
s.delay, _ = dataMap["d"].(uint)
default:
panic(fmt.Sprintf("version %d unknown", version))
}
}
type ScheduleSchedule struct {
Object
Schedules []Schedule
}
func (s *ScheduleSchedule) Act(o Living) (uint, bool) {
if len(s.Schedules) > 0 {
delay, keep := s.Schedules[0].Act(o)
if !keep {
s.Schedules = s.Schedules[1:]
}
return delay, true
}
return 0, false
}
func (s *ScheduleSchedule) ShouldSave() bool { return false }
func (s *ScheduleSchedule) Target() ObjectLike {
if len(s.Schedules) > 0 {
return s.Schedules[0].Target()
}
return nil
}
type DelaySchedule struct {
Object
Delay uint
}
func (s *DelaySchedule) Act(o Living) (uint, bool) {
delay := s.Delay
if delay == 0 {
return 0, false
}
s.Delay = 0
return delay, true
}
func (s *DelaySchedule) ShouldSave() bool { return false }
func (s *DelaySchedule) Target() ObjectLike { return nil }
type ActionSchedule struct {
Object
Action string
Target_ Visible
}
func (s *ActionSchedule) Act(o Living) (uint, bool) {
s.Target_.Interact(o.(PlayerLike), s.Action)
return 0, false
}
func (s *ActionSchedule) ShouldSave() bool { return false }
func (s *ActionSchedule) Target() ObjectLike { return s.Target_ }