-
Notifications
You must be signed in to change notification settings - Fork 0
/
scenario.go
292 lines (260 loc) · 7.45 KB
/
scenario.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package main
import (
"encoding/json"
"errors"
"math/rand"
)
const (
SPAWN_REQUEST = iota + 700
)
var (
SpawnReqEvent = Event{
EType: SPAWN_REQUEST,
Object: nil,
Payload: nil,
}
EmptyLocation = Box{
Point{},
Size{},
}
)
type SpawnRequest struct {
Position Point //maybe auto
Location Zone //maybe auto
Team int8
Blueprint string
Tags //tags overriding
Info BlueprintInfo //prefetched value from Blueprint
Count int
}
type ScenarioLimits struct {
AiUnits int64
}
type ScenarioStateInfo struct {
Declare []string
Spawn []*SpawnRequest
Screen *Screen
Location Box
player1Blueprint, player2Blueprint string
limits ScenarioLimits
}
type Scenario struct {
*State
*ObservableObject
declareBlueprint func(blueprint string)
dropBlueprint func(blueprint string)
declarationCleanup []string
player1Blueprint, player2Blueprint string
limits ScenarioLimits
Location Box
}
func (receiver *Scenario) ApplyState(current *StateItem) error { /*
//cleanup
for _, blueprint := range receiver.declarationCleanup {
receiver.dropBlueprint(blueprint)
}
receiver.declarationCleanup = receiver.declarationCleanup[0:0]*/
//move on
scenarioStateInfo := current.StateInfo.(*ScenarioStateInfo)
receiver.Location = scenarioStateInfo.Location
receiver.player1Blueprint = scenarioStateInfo.player1Blueprint
receiver.player2Blueprint = scenarioStateInfo.player2Blueprint
receiver.limits = scenarioStateInfo.limits
for _, blueprint := range scenarioStateInfo.Declare {
receiver.declareBlueprint(blueprint)
receiver.declarationCleanup = append(receiver.declarationCleanup, blueprint)
}
for _, spawnOrder := range scenarioStateInfo.Spawn {
receiver.Trigger(SpawnReqEvent, receiver, spawnOrder)
}
return nil
}
func (receiver *Scenario) DeclareBlueprint(fn func(blueprint string)) {
receiver.declareBlueprint = fn //todo queue
}
func (receiver *Scenario) DropBlueprint(fn func(blueprint string)) {
receiver.declareBlueprint = fn //todo queue
}
func NewScenario() (*Scenario, error) {
instance := new(Scenario)
instance.State, _ = NewState(instance)
root, _ := NewStateItem(nil, nil)
instance.State.root = root
instance.State.Current = root
instance.State.defaultPath = "/"
instance.State.MoveTo("/")
instance.ObservableObject, _ = NewObservableObject(make(EventChanel), instance)
return instance, nil
}
func NewRandomScenario(tankCnt int, wallCnt int, limitAi int) (scenario *Scenario, err error) {
scenario, err = NewScenario()
if err != nil {
return nil, err
}
spawn := make([]*SpawnRequest, 0, tankCnt+wallCnt)
blList := []string{"tank", "tank", "tank", "tank-fast", "tank-fast", "tank-fast", "tank-heavy", "tank-sneaky", "tank-sneaky"}
for i := 0; i < tankCnt; i++ {
spawn = append(spawn, &SpawnRequest{
Position: PosAuto,
Location: ZoneAuto,
Blueprint: blList[rand.Intn(len(blList))],
Team: 1,
})
}
blList = []string{"wall", "wall", "wall", "water", "forest"}
for i := 0; i < wallCnt; i++ {
spawn = append(spawn, &SpawnRequest{
Position: PosAuto,
Location: ZoneAuto,
Blueprint: blList[rand.Intn(len(blList))],
Team: 2,
})
}
start, _ := NewStateItem(scenario.State.root, &ScenarioStateInfo{
player1Blueprint: "player-tank",
player2Blueprint: "player-tank",
Declare: []string{
"player-tank",
"tank",
"tank-fast",
"tank-heavy",
"tank-sneaky",
"effect-onsight",
"effect-offsight",
"opel",
"gun",
"water",
"wall",
"forest",
"player-base",
},
limits: ScenarioLimits{AiUnits: int64(limitMaxAi)},
Spawn: spawn,
Screen: nil,
})
scenario.State.root.items["start"] = start
return scenario, nil
}
func NewFileScenario(filepath string) (*Scenario, error) {
state, err := GetScenarioState(filepath)
if err != nil {
return nil, err
}
instance := new(Scenario)
instance.State = state
instance.State.Owner = instance
instance.ObservableObject, _ = NewObservableObject(make(EventChanel), instance)
if instance.State != nil {
stateItem, _, err := instance.State.find("/start") //todo remove it
if err == nil {
info := stateItem.StateInfo.(*ScenarioStateInfo)
instance.Location = info.Location
instance.player1Blueprint = info.player1Blueprint
instance.player2Blueprint = info.player2Blueprint
}
}
return instance, nil
}
func GetScenario(name string) (*Scenario, error) {
switch name {
default:
return NewFileScenario(name)
}
}
func GetScenarioState(id string) (*State, error) { //todo refactor
//<-time.After(time.Second * 5)
return LoadScenario(id, func(m map[string]interface{}) (interface{}, error) {
var ssi *ScenarioStateInfo = &ScenarioStateInfo{
Declare: make([]string, 0),
Spawn: make([]*SpawnRequest, 0),
Screen: nil,
}
if player1Blueprint, ok := m["player1Blueprint"]; ok {
//todo refactor this shit
ssi.player1Blueprint = player1Blueprint.(string)
}
if player2Blueprint, ok := m["player2Blueprint"]; ok {
//todo refactor this shit
ssi.player2Blueprint = player2Blueprint.(string)
}
if location, ok := m["location"]; ok {
//todo refactor this shit
locationMap := location.(map[string]interface{})
ssi.Location.X = locationMap["x"].(float64)
ssi.Location.Y = locationMap["y"].(float64)
ssi.Location.W = locationMap["w"].(float64)
ssi.Location.H = locationMap["h"].(float64)
}
if limits, ok := m["limits"]; ok {
limitsMap := limits.(map[string]interface{})
ssi.limits.AiUnits = int64(limitsMap["aiUnit"].(float64))
}
if declare, ok := m["declare"]; ok {
//todo refactor this shit
for _, decl := range declare.([]interface{}) {
ssi.Declare = append(ssi.Declare, decl.(string))
}
}
if _, ok := m["screen"]; ok {
}
if spawn, ok := m["spawn"]; ok {
//todo refactor this shit
for _, spawnItem := range spawn.([]interface{}) {
spr := SpawnRequest{}
spr.Position = PosAuto
spr.Location = ZoneAuto
for key, value := range spawnItem.(map[string]interface{}) {
switch key {
case "team":
spr.Team = int8(value.(float64))
case "blueprint":
spr.Blueprint = value.(string)
case "count":
spr.Count = int(value.(float64))
case "position":
loc := value.(map[string]interface{})
spr.Position = Point{}
spr.Position.X = loc["X"].(float64)
spr.Position.Y = loc["Y"].(float64)
case "location":
loc := value.(map[string]interface{})
spr.Location = Zone{}
spr.Location.X = int(loc["X"].(float64))
spr.Location.Y = int(loc["Y"].(float64))
}
}
ssi.Spawn = append(ssi.Spawn, &spr)
}
}
return ssi, nil
})
}
func LoadScenario(id string, builder SateInfoBuilder) (*State, error) { //todo refactor
if state, ok := states[id]; ok {
return state.Copy(), nil
}
buffer, err := loadScenario(id)
if err != nil {
return nil, err
}
stateRead := stateRead{}
err = json.Unmarshal(buffer, &stateRead)
if err != nil {
return nil, err
}
if len(stateRead.Items) == 0 {
return nil, errors.New("load empty state")
}
state, _ := NewState(nil)
root := state.root
recursiveCreateState(root, stateRead.Items, builder)
if stateRead.Default != "" {
err = state.MoveTo(stateRead.Default)
if err != nil {
return nil, err
}
state.defaultPath = stateRead.Default
}
states[id] = state
return state.Copy(), nil
}