forked from zond/godip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgodip.go
394 lines (319 loc) · 8.84 KB
/
godip.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package godip
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"strings"
"time"
)
const (
Sea Flag = "Sea"
Land Flag = "Land"
Convoyable Flag = "Convoyable"
Army UnitType = "Army"
Fleet UnitType = "Fleet"
England Nation = "England"
France Nation = "France"
Germany Nation = "Germany"
Russia Nation = "Russia"
Austria Nation = "Austria"
Italy Nation = "Italy"
Turkey Nation = "Turkey"
Neutral Nation = "Neutral"
Spring Season = "Spring"
Fall Season = "Fall"
Movement PhaseType = "Movement"
Retreat PhaseType = "Retreat"
Adjustment PhaseType = "Adjustment"
Build OrderType = "Build"
Move OrderType = "Move"
MoveViaConvoy OrderType = "MoveViaConvoy"
Hold OrderType = "Hold"
Convoy OrderType = "Convoy"
Support OrderType = "Support"
Disband OrderType = "Disband"
ViaConvoy Flag = "C"
Anywhere Flag = "A"
AnyHomeCenter Flag = "H"
)
var Coast = []Flag{Sea, Land}
var Archipelago = []Flag{Sea, Land, Convoyable}
// Invalid is not understood
// Illegal is understood but not allowed
var ErrInvalidSource = fmt.Errorf("ErrInvalidSource")
var ErrInvalidDestination = fmt.Errorf("ErrInvalidDestination")
var ErrInvalidTarget = fmt.Errorf("ErrInvalidTarget")
var ErrInvalidPhase = fmt.Errorf("ErrInvalidPhase")
var ErrMissingUnit = fmt.Errorf("ErrMissingUnit")
var ErrIllegalDestination = fmt.Errorf("ErrIllegalDestination")
var ErrMissingConvoyPath = fmt.Errorf("ErrMissingConvoyPath")
var ErrIllegalMove = fmt.Errorf("ErrIllegalMove")
var ErrConvoyParadox = fmt.Errorf("ErrConvoyParadox")
var ErrIllegalSupportPosition = fmt.Errorf("ErrIllegalSupportPosition")
var ErrIllegalSupportDestination = fmt.Errorf("ErrIllegalSupportDestination")
var ErrIllegalSupportDestinationNation = fmt.Errorf("ErrIllegalSupportDestinationNation")
var ErrMissingSupportUnit = fmt.Errorf("ErrMissingSupportUnit")
var ErrIllegalSupportMove = fmt.Errorf("ErrIllegalSupportMove")
var ErrIllegalConvoyUnit = fmt.Errorf("ErrIllegalConvoyUnit")
var ErrIllegalConvoyPath = fmt.Errorf("ErrIllegalConvoyPath")
var ErrIllegalConvoyMove = fmt.Errorf("ErrIllegalConvoyMove")
var ErrMissingConvoyee = fmt.Errorf("ErrMissingConvoyee")
var ErrIllegalConvoyer = fmt.Errorf("ErrIllegalConvoyer")
var ErrIllegalConvoyee = fmt.Errorf("ErrIllegalConvoyee")
var ErrIllegalBuild = fmt.Errorf("ErrIllegalBuild")
var ErrIllegalDisband = fmt.Errorf("ErrIllegalDisband")
var ErrOccupiedSupplyCenter = fmt.Errorf("ErrOccupiedSupplyCenter")
var ErrMissingSupplyCenter = fmt.Errorf("ErrMissingSupplyCenter")
var ErrMissingSurplus = fmt.Errorf("ErrMissingSurplus")
var ErrIllegalUnitType = fmt.Errorf("ErrIllegalUnitType")
var ErrMissingDeficit = fmt.Errorf("ErrMissingDeficit")
var ErrOccupiedDestination = fmt.Errorf("ErrOccupiedDestination")
var ErrIllegalRetreat = fmt.Errorf("ErrIllegalRetreat")
var ErrForcedDisband = fmt.Errorf("ErrForcedDisband")
var ErrHostileSupplyCenter = fmt.Errorf("ErrHostileSupplyCenter")
type ErrDoubleBuild struct {
Provinces []Province
}
func (self ErrDoubleBuild) Error() string {
return fmt.Sprintf("ErrDoubleBuild:%v", self.Provinces)
}
type ErrConvoyDislodged struct {
Province Province
}
func (self ErrConvoyDislodged) Error() string {
return fmt.Sprintf("ErrConvoyDislodged:%v", self.Province)
}
type ErrSupportBroken struct {
Province Province
}
func (self ErrSupportBroken) Error() string {
return fmt.Sprintf("ErrSupportBroken:%v", self.Province)
}
type ErrBounce struct {
Province Province
}
func (self ErrBounce) Error() string {
return fmt.Sprintf("ErrBounce:%v", self.Province)
}
var Debug = false
var LogIndent = []string{}
var logBuffer = new(bytes.Buffer)
func Indent(s string) {
if Debug {
LogIndent = append(LogIndent, s)
}
}
func DeIndent() {
if Debug {
LogIndent = LogIndent[:len(LogIndent)-1]
}
}
func Logf(s string, o ...interface{}) {
if Debug {
fmt.Fprintf(logBuffer, fmt.Sprintf("%v%v\n", strings.Join(LogIndent, ""), s), o...)
}
}
func ClearLog() {
if Debug {
logBuffer = new(bytes.Buffer)
}
}
func DumpLog() {
if Debug {
fmt.Print(string(logBuffer.Bytes()))
ClearLog()
}
}
func Max(is ...int) (result int) {
for index, i := range is {
if index == 0 || i > result {
result = i
}
}
return
}
func Min(is ...int) (result int) {
for index, i := range is {
if index == 0 || i < result {
result = i
}
}
return
}
type UnitType string
type Nation string
func (n *Nation) String() string {
if n == nil {
return ""
}
return string(*n)
}
type Nations []Nation
func (n Nations) Len() int {
return len(n)
}
func (n Nations) Less(i, j int) bool {
return bytes.Compare([]byte(n[i]), []byte(n[j])) < 0
}
func (n Nations) Swap(i, j int) {
n[i], n[j] = n[j], n[i]
}
type OrderType string
type PhaseType string
type Province string
func (p *Province) String() string {
if p == nil {
return ""
}
return string(*p)
}
type Season string
func (self Province) Split() (sup Province, sub Province) {
split := strings.Split(string(self), "/")
if len(split) > 0 {
sup = Province(split[0])
}
if len(split) > 1 {
sub = Province(split[1])
}
return
}
func (self Province) Join(n Province) (result Province) {
if n != "" {
result = Province(fmt.Sprintf("%v/%v", self, n))
} else {
result = self
}
return
}
func (self Province) Super() (result Province) {
result, _ = self.Split()
return
}
func (self Province) Sub() (result Province) {
_, result = self.Split()
return
}
func (self Province) Contains(p Province) bool {
return self == p || (self.Super() == self && self == p.Super())
}
type Unit struct {
Type UnitType
Nation Nation
}
func (self Unit) Equal(o Unit) bool {
return self.Type == o.Type && self.Nation == o.Nation
}
func (self *Unit) String() string {
return fmt.Sprint(*self)
}
type Phase interface {
Year() int
Season() Season
Type() PhaseType
Next() Phase
PreProcess(State) error
PostProcess(State) error
DefaultOrder(Province) Adjudicator
Options(Validator, Nation) (result Options)
}
type PathFilter func(n Province, edgeFlags, provFlags map[Flag]bool, sc *Nation, trace []Province) bool
type Flag string
type Graph interface {
Has(Province) bool
Flags(Province) map[Flag]bool
AllFlags(Province) map[Flag]bool
SC(Province) *Nation
Path(src, dst Province, reverse bool, filter PathFilter) []Province
Coasts(Province) []Province
Edges(src Province, reverse bool) map[Province]map[Flag]bool
SCs(Nation) []Province
AllSCs() []Province
Provinces() []Province
Nations() []Nation
}
type Orders []Order
func (self Orders) Less(a, b int) bool {
return self[a].At().Before(self[b].At())
}
func (self Orders) Swap(a, b int) {
self[a], self[b] = self[b], self[a]
}
func (self Orders) Len() int {
return len(self)
}
type SrcProvince Province
type OptionValue interface{}
/*
Options defines a tree of valid orders for a given situation
*/
type Options map[OptionValue]Options
func (self Options) MarshalJSON() ([]byte, error) {
repl := map[string]interface{}{}
for k, v := range self {
repl[fmt.Sprint(k)] = map[string]interface{}{
"Type": reflect.ValueOf(k).Type().Name(),
"Next": v,
}
}
return json.Marshal(repl)
}
// Order is a basic order, but unable to adjudicate itself.
type Order interface {
Type() OrderType
DisplayType() OrderType
Targets() []Province
Parse([]string) (Adjudicator, error)
Validate(Validator) (Nation, error)
Options(Validator, Nation, Province) Options
At() time.Time
Flags() map[Flag]bool
}
// Adjudicator is what orders turn into when adjudication has started.
type Adjudicator interface {
Order
Adjudicate(Resolver) error
Execute(State)
}
type BackupRule func(State, []Province) error
type StateFilter func(n Province, o Order, u *Unit) bool
// Validator is a game state able to validate orders, but not adjudicate them.
type Validator interface {
Order(Province) (Order, Province, bool)
Unit(Province) (Unit, Province, bool)
Dislodged(Province) (Unit, Province, bool)
SupplyCenter(Province) (Nation, Province, bool)
Bounce(src, dst Province) bool
Orders() map[Province]Adjudicator
Units() map[Province]Unit
Dislodgeds() map[Province]Unit
SupplyCenters() map[Province]Nation
Graph() Graph
Phase() Phase
Find(StateFilter) (provinces []Province, orders []Order, units []*Unit)
Options([]Order, Nation) (result Options)
Profile(string, time.Time)
GetProfile() (map[string]time.Duration, map[string]int)
MemoizeProvSlice(string, func() []Province) []Province
}
// Resolver is what validators turn into when adjudication has started.
type Resolver interface {
Validator
AddBounce(src, dst Province)
Resolve(Province) error
}
// State is the super-user access to the entire game state.
type State interface {
Resolver
Move(src, dst Province, preventRetreat bool)
Retreat(src, dst Province) error
RemoveDislodged(Province)
RemoveUnit(Province)
SetResolution(Province, error)
SetSC(Province, Nation)
SetUnit(Province, Unit) error
SetOrders(map[Province]Adjudicator)
ClearDislodgers()
ClearBounces()
}