-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.go
847 lines (789 loc) · 20.1 KB
/
game.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
package game
import (
"fmt"
"math"
"regexp"
"strconv"
"strings"
"time"
"github.com/kssilveira/idle-game-engine/data"
"github.com/kssilveira/idle-game-engine/ui"
)
type Input chan string
type Output chan *ui.Data
type Now func() time.Time
type Config struct {
NowFn Now
MaxSkipSeconds time.Duration
MaxCreateIter int
}
type Game struct {
Resources []*data.Resource `json:",omitempty"`
Actions []data.Action `json:",omitempty"`
// maps resource name to index in Resources
resourceToIndex map[string]int
// maps action name to index in Actions
actionToIndex map[string]int
config Config
now time.Time
errors []error
showAll bool
hideOverCap bool
hideCustom bool
hideAction map[string]bool
hideResource map[string]bool
}
func NewGame(cfg Config) *Game {
g := &Game{
config: cfg,
now: cfg.NowFn(),
resourceToIndex: map[string]int{},
actionToIndex: map[string]int{},
hideAction: map[string]bool{},
hideResource: map[string]bool{},
}
g.AddResources([]data.Resource{{
Name: "time", Type: "Calendar", Cap: -1,
}, {
Name: "skip", Type: "Calendar", Cap: -1,
}})
return g
}
func (g *Game) AddResources(resources []data.Resource) {
for _, resource := range resources {
g.AddResource(resource)
}
}
func (g *Game) AddResource(resource data.Resource) {
if _, ok := g.resourceToIndex[resource.Name]; ok {
g.errors = append(g.errors, fmt.Errorf("duplicate resource %s", resource.Name))
}
g.resourceToIndex[resource.Name] = len(g.Resources)
resource.Formula = g.getFormula(resource)
g.Resources = append(g.Resources, &resource)
}
func (g *Game) AddActions(actions []data.Action) {
for _, action := range actions {
g.AddAction(action)
}
}
func (g *Game) AddAction(action data.Action) {
if _, ok := g.actionToIndex[action.Name]; ok {
g.errors = append(g.errors, fmt.Errorf("duplicate action %s", action.Name))
}
g.actionToIndex[action.Name] = len(g.Actions)
g.Actions = append(g.Actions, action)
}
func (g *Game) Validate() error {
if len(g.errors) > 0 {
return fmt.Errorf("%v", g.errors)
}
for _, r := range g.Resources {
if err := g.validateResource(r); err != nil {
return err
}
}
for _, a := range g.Actions {
for _, list := range append([][]data.Resource{}, a.Costs, a.Adds) {
for _, r := range list {
if err := g.validateResource(&r); err != nil {
return err
}
}
for _, name := range []string{a.UnlockedBy, a.LockedBy} {
if err := g.validateResourceName(name); err != nil {
return err
}
}
}
if err := g.validateResource(&a.CostExponentBaseResource); err != nil {
return err
}
}
return nil
}
func (g *Game) Run(input Input, output Output) {
var in string
var parsedInput data.ParsedInput
var err error
for {
g.update(g.config.NowFn())
data := &ui.Data{
LastInput: parsedInput,
Error: err,
ShowAll: g.showAll,
HideOverCap: g.hideOverCap,
HideCustom: g.hideCustom,
HideAction: makeCopy(g.hideAction),
HideResource: makeCopy(g.hideResource),
}
g.populateUIResources(data)
g.populateUIActions(data)
output <- data
select {
case in = <-input:
if in == "999" {
close(output)
return
}
parsedInput, err = g.act(in)
case <-time.After(1 * time.Second):
}
}
}
func makeCopy(in map[string]bool) map[string]bool {
res := map[string]bool{}
for k, v := range in {
res[k] = v
}
return res
}
func (g *Game) populateUIResources(data *ui.Data) {
for _, r := range g.Resources {
data.Resources = append(data.Resources, ui.Resource{
Resource: *r,
Rate: g.getRate(r),
DurationToCap: g.getDuration(r, r.Cap),
DurationToEmpty: g.getDuration(r, 0),
})
}
}
func (g *Game) populateUIActions(data *ui.Data) {
for _, a := range g.Actions {
action := ui.Action{
Name: a.Name,
Type: a.Type,
IsHidden: a.IsHidden,
IsLocked: g.isLocked(a),
}
if g.HasResource(a.Name) {
action.Count = g.GetResource(a.Name).Count
} else {
action.MissingResource = true
}
action.Costs = g.populateUICosts(a, &action, false /* isNested */)
for _, r := range a.Adds {
action.Adds = append(action.Adds, ui.Add{
Name: r.Name,
Count: g.getActionAdd(r).Count,
Cap: r.Cap,
})
}
data.Actions = append(data.Actions, action)
}
data.CustomActions = []ui.CustomAction{{
Name: "mX: max action X (skip, create, buy)",
}, {
Name: "sX: time skip, create inputs and buy action X",
}, {
Name: "cX: create inputs and buy action X",
}, {
Name: "uX: undo action X",
}, {
Name: "XA: X times action A",
}, {
Name: "hm: hide maxed actions",
}, {
Name: "hc: hide custom actions",
}, {
Name: "hX: hide action X",
}, {
Name: "hR: hide resource S",
}, {
Name: "S: show all",
}, {
Name: "r: reset",
}}
}
func (g *Game) populateUICosts(a data.Action, aui *ui.Action, isNested bool) []ui.Cost {
res := []ui.Cost{}
for _, c := range a.Costs {
cost := g.getCost(a, c)
r := g.GetResource(c.Name)
one := ui.Cost{
Name: c.Name,
Count: r.Count,
Cap: r.Cap,
Cost: cost,
Duration: g.getDuration(r, cost),
}
if isNested {
one.Cap = -1
}
one.IsOverCap = one.Cost > one.Cap && one.Cap != -1
if one.IsOverCap {
aui.IsOverCap = true
}
if r.ProducerAction != "" {
one.Costs = g.populateUICosts(g.getNestedAction(a, c), &ui.Action{}, true /* isNested */)
}
res = append(res, one)
}
return res
}
func (g *Game) getDuration(r *data.Resource, quantity float64) time.Duration {
return time.Duration(((quantity - r.Count) / g.getRate(r))) * time.Second
}
func (g *Game) update(now time.Time) {
elapsed := now.Sub(g.now)
g.now = now
g.GetResource("time").Count += float64(elapsed / time.Second)
for _, resource := range g.Resources {
if resource.CapResource != "" {
resource.Cap = g.GetResource(resource.CapResource).Count
}
factor := g.getRate(resource)
if resource.ProductionModulus != 0 {
factor = float64(int(factor) % resource.ProductionModulus)
}
if resource.StartCount != 0 || resource.StartCountFromZero {
if resource.ProductionModulus != 0 && resource.ProductionModulusEquals >= 0 {
if int(factor) == resource.ProductionModulusEquals {
resource.Count = resource.StartCount
} else {
resource.Count = 0
}
} else {
resource.Count = resource.StartCount + factor
}
} else {
resource.Add(data.Resource{Count: elapsed.Seconds()}, factor)
}
if factor < 0 && resource.Count == 0 {
g.updateRate(resource)
}
}
}
func (g *Game) getFormula(resource data.Resource) string {
if len(resource.Producers) == 0 {
return ""
}
factor := g.getRateFormula(resource)
if resource.ProductionModulus != 0 {
factor = fmt.Sprintf("%s %% %d", factor, resource.ProductionModulus)
}
count := ""
if resource.StartCount != 0 || resource.StartCountFromZero {
if resource.ProductionModulus != 0 && resource.ProductionModulusEquals >= 0 {
count = fmt.Sprintf("Count = %s if %s == %d", floatFormula(resource.StartCount), factor, resource.ProductionModulusEquals)
} else {
count = fmt.Sprintf("Count = %s", joinFormula("+", floatFormula(resource.StartCount), factor))
}
} else {
count = fmt.Sprintf("Count += %s", joinFormula("*", factor, "seconds"))
}
return count
}
func joinFormula(operator string, parts ...string) string {
filtered := []string{}
for _, part := range parts {
if part == "" {
continue
}
if operator == "*" && (part == "1" || part == "(1)") {
continue
}
if operator == "+" && (part == "0" || part == "(0)") {
continue
}
filtered = append(filtered, part)
}
res := strings.Join(filtered, fmt.Sprintf(" %s ", operator))
if len(filtered) > 1 {
res = fmt.Sprintf("(%s)", res)
}
return res
}
func floatFormula(f float64) string {
res := fmt.Sprintf("%f", f)
res = strings.TrimRight(res, "0")
res = strings.TrimRight(res, ".")
return res
}
func (g *Game) act(in string) (data.ParsedInput, error) {
input, err := g.parseInput(in)
if err != nil {
return input, err
}
for i := 0; i < input.Count; i++ {
if err := g.actImpl(input); err != nil {
return input, err
}
}
return input, nil
}
func (g *Game) actImpl(input data.ParsedInput) error {
if input.Type == data.ParsedInputTypeReset {
g.reset()
return nil
}
if input.Type == data.ParsedInputTypeHide {
if input.Arg == "m" {
g.hideOverCap = !g.hideOverCap
} else if input.Arg == "c" {
g.hideCustom = !g.hideCustom
} else if input.Action.Name != "" {
g.hideAction[input.Action.Name] = !g.hideAction[input.Action.Name]
} else {
g.hideResource[input.Arg] = !g.hideResource[input.Arg]
}
return nil
}
if input.Type == data.ParsedInputTypeShow {
g.showAll = !g.showAll
return nil
}
if g.isLocked(input.Action) {
return fmt.Errorf("action %s is locked", input.Action.Name)
}
if input.Type != data.ParsedInputTypeUndo {
if err := g.checkMax(input.Action); err != nil {
return err
}
}
if input.Type == data.ParsedInputTypeSkip {
if err := g.skip(input); err != nil {
return err
}
}
if input.Type == data.ParsedInputTypeSkip || input.Type == data.ParsedInputTypeCreate {
if err := g.create(input); err != nil {
return err
}
}
if input.Type == data.ParsedInputTypeMax {
if err := g.doMax(input); err != nil {
return err
}
return nil
}
if input.Type != data.ParsedInputTypeUndo {
if err := g.checkCost(input); err != nil {
return err
}
}
factor := 1.0
if input.Type == data.ParsedInputTypeUndo {
factor = -1.0
}
for _, c := range input.Action.Costs {
r := g.GetResource(c.Name)
r.Count -= g.getCost(input.Action, c) * factor
r.Cap -= c.Cap * factor
}
for _, add := range input.Action.Adds {
r := g.GetResource(add.Name)
r.Add(g.getActionAdd(add), factor)
}
return nil
}
func (g *Game) doMax(input data.ParsedInput) error {
for {
before := g.getActionState(input.Action, 1 /* factor */)
g.act(fmt.Sprintf("s%d", input.Index))
after := g.getActionState(input.Action, 1 /* factor */)
if before == after {
break
}
g.update(g.config.NowFn())
}
return nil
}
func (g *Game) getActionState(action data.Action, factor float64) string {
res := []string{}
for _, c := range action.Costs {
r := g.GetResource(c.Name)
cost := g.getCost(action, c) * factor
missing := 0.0
if r.Count < cost {
missing = cost - r.Count
}
res = append(res, fmt.Sprintf("%s %f %f", c.Name, cost, missing))
if r.ProducerAction == "" {
continue
}
factor := g.getNeededNestedAction(action, c)
res = append(res, g.getActionState(g.getNestedAction(action, c), factor))
}
return strings.Join(res, " ")
}
func (g *Game) checkCost(input data.ParsedInput) error {
for _, c := range input.Action.Costs {
if g.GetResource(c.Name).Count < g.getCost(input.Action, c) {
return fmt.Errorf("not enough %s", c.Name)
}
}
return nil
}
func (g *Game) create(input data.ParsedInput) error {
for _, c := range input.Action.Costs {
r := g.GetResource(c.Name)
if r.ProducerAction == "" {
continue
}
nested := fmt.Sprintf("%d", g.actionToIndex[r.ProducerAction])
need := int(g.getNeededNestedAction(input.Action, c))
if g.config.MaxCreateIter > 0 && need > g.config.MaxCreateIter {
return fmt.Errorf("max create iter %d < need %d for action %s", g.config.MaxCreateIter, need, input.Action.Name)
}
for i := 0; i < need; i++ {
if _, err := g.act(input.Type + nested); err != nil {
break
}
}
}
return nil
}
func (g *Game) skip(input data.ParsedInput) error {
skipTime, err := g.getSkipTime(input.Action, false /* isNested */)
if err != nil {
return err
}
for skipTime > 0 {
g.timeSkip(skipTime)
skipTime, err = g.getSkipTime(input.Action, false /* isNested */)
if err != nil {
return err
}
}
return nil
}
func (g *Game) reset() {
for _, resource := range g.Resources {
count := 0.0
if resource.ResetResource != "" {
count = g.GetResource(resource.ResetResource).Count
}
resource.Count = count
if resource.Cap > 1 {
resource.Cap = 0
}
}
}
func (g *Game) getActionAdd(add data.Resource) data.Resource {
add.Count *= g.getBonus(add)
return add
}
func (g *Game) getBonus(resource data.Resource) float64 {
bonus := 1.0
if resource.BonusStartsFromZero {
bonus = 0
}
if resource.BonusIsMultiplicative {
for _, b := range resource.Bonus {
bonus *= g.getOneRate(b)
}
} else {
for _, b := range resource.Bonus {
bonus += g.getOneRate(b)
}
}
return bonus
}
func (g *Game) getBonusFormula(resource data.Resource) string {
start := "1"
if resource.BonusStartsFromZero {
start = "0"
}
bonus := []string{start}
for _, b := range resource.Bonus {
bonus = append(bonus, g.getOneRateFormula(b))
}
operator := "+"
if resource.BonusIsMultiplicative {
operator = "*"
}
return joinFormula(operator, bonus...)
}
var (
inputRegexp = regexp.MustCompile(fmt.Sprintf(`\s*(\d*)\s*([%s]?)\s*(\D*)\s*(\d*)\s*`, strings.Join(data.ParsedInputTypes, "")))
)
func (g *Game) parseInput(in string) (data.ParsedInput, error) {
res := data.ParsedInput{Count: 1}
matches := inputRegexp.FindStringSubmatch(in)
if len(matches) != 5 {
return res, fmt.Errorf("input %s invalid matches %#v", in, matches)
}
if matches[2] != "" {
for _, t := range data.ParsedInputTypes {
if matches[2] == t {
res.Type = t
break
}
}
if res.Type == "" {
return res, fmt.Errorf("input %s invalid command %s", in, matches[2])
}
}
res.Arg = matches[3]
if g.HasAction(res.Arg) {
matches[4] = fmt.Sprintf("%d", g.GetActionIndex(res.Arg))
res.Arg = ""
}
if matches[4] == "" && matches[1] != "" {
matches[4] = matches[1]
matches[1] = ""
}
if matches[1] != "" {
count, err := strconv.Atoi(matches[1])
if err != nil {
return res, err
}
res.Count = count
}
if matches[4] != "" {
index, err := strconv.Atoi(matches[4])
if err != nil {
return res, err
}
res.Index = index
if index < 0 || index >= len(g.Actions) {
return res, fmt.Errorf("invalid index %d", index)
}
res.Action = g.Actions[index]
}
return res, nil
}
func (g *Game) isLocked(a data.Action) bool {
return (a.UnlockedBy != "" && g.GetResource(a.UnlockedBy).Count <= 0) ||
(a.LockedBy != "" && g.GetResource(a.LockedBy).Count > 0)
}
func (g *Game) checkMax(a data.Action) error {
found := false
for _, add := range a.Adds {
r := g.GetResource(add.Name)
if r.Count < r.Cap || r.Cap == -1 || add.Cap > 0 {
found = true
break
}
}
if !found {
return fmt.Errorf("added resources already at max")
}
return nil
}
func (g *Game) getSkipTime(a data.Action, isNested bool) (time.Duration, error) {
var skipTime time.Duration
for _, c := range a.Costs {
r := g.GetResource(c.Name)
cost := g.getCost(a, c)
if r.Count >= cost {
continue
}
if r.Count == r.Cap {
continue
}
if !isNested && r.Cap != -1 && cost > r.Cap {
return 0, fmt.Errorf("not enough cap for %s", c.Name)
}
if g.getRate(r) > 0 {
duration := g.getDuration(r, cost) + time.Second
if duration > skipTime {
skipTime = duration
}
continue
}
if r.ProducerAction != "" {
duration, err := g.getSkipTime(g.getNestedAction(a, c), true /* isNested */)
if err == nil {
if duration > skipTime {
skipTime = duration
}
continue
}
}
return 0, fmt.Errorf("not enough %s", c.Name)
}
if g.config.MaxSkipSeconds > 0 && skipTime > g.config.MaxSkipSeconds {
return 0, fmt.Errorf("max skip %s < skip %s", g.config.MaxSkipSeconds, skipTime)
}
return skipTime, nil
}
func (g *Game) getNestedAction(a data.Action, c data.Resource) data.Action {
r := g.GetResource(c.Name)
if r.ProducerAction == "" {
return data.Action{}
}
need := g.getNeededNestedAction(a, c)
res := data.Action{
Adds: []data.Resource{{}},
}
action := g.GetAction(r.ProducerAction)
if g.isLocked(action) {
return data.Action{}
}
for _, c := range action.Costs {
cost := g.getCost(action, c) * need
res.Costs = append(res.Costs, data.Resource{
Name: c.Name,
Count: cost,
})
}
return res
}
func (g *Game) getNeededNestedAction(a data.Action, c data.Resource) float64 {
r := g.GetResource(c.Name)
if r.ProducerAction == "" {
return 0
}
cost := g.getCost(a, c) - r.Count
if cost < 0 {
return 0
}
action := g.GetAction(r.ProducerAction)
res := math.Ceil(cost / g.getActionAdd(action.Adds[0]).Count)
return res
}
func (g *Game) timeSkip(skip time.Duration) {
g.GetResource("skip").Count += float64(skip / time.Second)
now := g.now
g.now = time.Time(now.Add(-skip))
g.update(now)
}
func (g *Game) validateResource(r *data.Resource) error {
for _, name := range []string{r.Name, r.CapResource, r.ResetResource} {
if err := g.validateResourceName(name); err != nil {
return err
}
}
for _, name := range []string{r.ProducerAction} {
if err := g.validateActionName(name); err != nil {
return err
}
}
for _, list := range append(
[][]data.Resource{}, r.Producers, r.Bonus, r.OnGone) {
for _, r := range list {
if err := g.validateResource(&r); err != nil {
return err
}
}
}
if r.StartCount != 0 || r.StartCountFromZero {
if r.Count != 0 {
return fmt.Errorf("resource %s has StartCount and Count", r.Name)
}
if len(r.Producers) == 0 && len(r.Bonus) == 0 {
return fmt.Errorf("resource %s has StartCount and no Producers or Bonus", r.Name)
}
}
return nil
}
func (g *Game) validateResourceName(name string) error {
if name != "" && !g.HasResource(name) {
return fmt.Errorf("invalid resource name %s", name)
}
return nil
}
func (g *Game) validateActionName(name string) error {
if name != "" && !g.HasAction(name) {
return fmt.Errorf("invalid action name %s", name)
}
return nil
}
func (g *Game) getRate(resource *data.Resource) float64 {
factor := 0.0
for _, p := range resource.Producers {
factor += g.getOneRate(p)
}
return factor * g.getBonus(*resource)
}
func (g *Game) getRateFormula(resource data.Resource) string {
factors := []string{}
for _, p := range resource.Producers {
factors = append(factors, g.getOneRateFormula(p))
}
return joinFormula("*", joinFormula("+", factors...), g.getBonusFormula(resource))
}
func (g *Game) getOneRate(resource data.Resource) float64 {
res := g.getCountForRate(resource) * GetFactor(resource.Factor) * g.getBonus(resource)
if resource.Min != 0 && res < resource.Min {
res = resource.Min
}
return res
}
func (g *Game) getOneRateFormula(resource data.Resource) string {
res := joinFormula("*", g.getCountForRateFormula(resource), floatFormula(GetFactor(resource.Factor)), g.getBonusFormula(resource))
if resource.Min != 0 {
res = fmt.Sprintf("max(%s, %s)", floatFormula(resource.Min), res)
}
return res
}
func GetFactor(factor float64) float64 {
if factor == 0 {
return 1
}
return factor
}
func (g *Game) getCountForRate(p data.Resource) float64 {
quantity := 1.0
if p.Name != "" {
quantity = g.GetResource(p.Name).Count
}
if p.ProductionFloor {
quantity = math.Floor(quantity)
}
if p.ProductionBoolean {
if quantity > 0 {
quantity = 1
} else {
quantity = 0
}
}
return quantity
}
func (g *Game) getCountForRateFormula(p data.Resource) string {
quantity := "1"
if p.Name != "" {
quantity = fmt.Sprintf("c(%s)", p.Name)
}
if p.ProductionFloor {
quantity = fmt.Sprintf("floor(%s)", quantity)
}
if p.ProductionBoolean {
quantity = fmt.Sprintf("(1 if %s gt 0)", quantity)
}
return quantity
}
func (g *Game) updateRate(resource *data.Resource) {
for _, p := range resource.Producers {
if !p.ProductionOnGone {
continue
}
one := g.getOneRate(p)
if one < 0 {
r := g.GetResource(p.Name)
r.Count--
for _, onGone := range r.OnGone {
gone := g.GetResource(onGone.Name)
gone.Count += onGone.Count
gone.Cap += onGone.Cap
}
return
}
}
}
func (g *Game) GetResource(name string) *data.Resource {
return g.Resources[g.resourceToIndex[name]]
}
func (g *Game) GetAction(name string) data.Action {
return g.Actions[g.actionToIndex[name]]
}
func (g *Game) GetActionIndex(name string) int {
return g.actionToIndex[name]
}
func (g *Game) getCost(a data.Action, c data.Resource) float64 {
base := a.CostExponentBase
if base == 0 {
base = g.getOneRate(a.CostExponentBaseResource)
}
return c.Count * math.Pow(base, g.GetResource(a.Adds[0].Name).Count)
}
func (g *Game) HasResource(name string) bool {
_, ok := g.resourceToIndex[name]
return ok
}
func (g *Game) HasAction(name string) bool {
_, ok := g.actionToIndex[name]
return ok
}