-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoog.go
182 lines (136 loc) · 3.87 KB
/
goog.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
// Copyright 2015 Donato Cassel Laynes Gonzales
//
// This file is part of GoOgame - Battle Simulator
package main
import (
"github.com/dlaynes/goog-sim/simulator"
"github.com/dlaynes/goog-sim/tools"
"encoding/json"
"fmt"
"io/ioutil"
"strconv"
)
func main() {
var profiler = &tools.Profiler{}
profiler.Init(40)
profiler.StartTask("full_simulation")
profiler.StartTask("load_resources")
var attackerGroup = &simulator.FleetGroup{}
var defenderGroup = &simulator.FleetGroup{}
var resources = map[string]*simulator.Resource{}
ParseJson("./config/resources.json", &resources)
var rapidfire = map[string]map[string]float64{}
ParseJson("./config/rapidfire.json", &rapidfire)
var attackers = []*simulator.Player{}
ParseJson("./data/attackers.json", &attackers)
var defenders = []*simulator.Player{}
ParseJson("./data/defenders.json", &defenders)
profiler.EndTask("load_resources")
//fmt.Printf("%#v\n", resources)
//fmt.Printf("%#v\n", rapidfire)
//fmt.Printf("%#v\n", attackers)
//fmt.Printf("%#v\n", defenders)
profiler.StartTask("init_groups")
for _, res := range resources {
res.Init(rapidfire)
}
/*
profiler.StartTask("calc_fleet_size")
aSize := 0
dSize := 0
for _, pl := range attackers {
for _, amount := range pl.OriginalFleet {
aSize += amount
}
}
fmt.Println("Size attackers" + strconv.Itoa(aSize))
for _, pl2 := range defenders {
for _, amount2 := range pl2.OriginalFleet {
dSize += amount2
}
}
fmt.Println("Size defenders" + strconv.Itoa(dSize))
profiler.EndTask("calc_fleet_size")
*/
profiler.StartTask("init_attackers")
attackerGroup.Init()
for _, player := range attackers {
player.Expand(attackerGroup, resources)
}
profiler.EndTask("init_attackers")
profiler.StartTask("init_defenders")
defenderGroup.Init()
for _, player2 := range defenders {
player2.Expand(defenderGroup, resources)
}
profiler.EndTask("init_defenders")
attackerGroup.CalcStatistics(0)
defenderGroup.CalcStatistics(0)
profiler.EndTask("init_groups")
/* Battle */
simulator.SeedRand()
profiler.StartTask("full_battle")
loops := 6
idx := ""
//battleStatus := 0
exitBattle := false
//successAt := true
//successDf := true
for i := 0; i < loops; i++ {
idx = strconv.Itoa(i + 1)
if len(attackerGroup.Ships) < 1 {
fmt.Println("Attacker group has no remaining ships in battle")
exitBattle = true
}
if len(defenderGroup.Ships) < 1 {
fmt.Println("Defender group has no remaining ships in battle")
exitBattle = true
}
if exitBattle {
break
}
profiler.StartTask("full_round_" + idx)
profiler.StartTask("battle_round_att_" + idx)
_ = attackerGroup.Attack(defenderGroup)
profiler.EndTask("battle_round_att_" + idx)
profiler.StartTask("battle_round_df_" + idx)
_ = defenderGroup.Attack(attackerGroup)
profiler.EndTask("battle_round_df_" + idx)
profiler.StartTask("battle_clean_att_" + idx)
attackerGroup.Clean()
profiler.EndTask("battle_clean_att_" + idx)
profiler.StartTask("battle_clean_df_" + idx)
defenderGroup.Clean()
profiler.EndTask("battle_clean_df_" + idx)
//fmt.Println("Round " + idx + " has ended")
profiler.EndTask("full_round_" + idx)
attackerGroup.CalcStatistics(i + 1)
defenderGroup.CalcStatistics(i + 1)
}
profiler.EndTask("full_battle")
//fmt.Printf("%#v\n", attackerGroup.Ships)
//fmt.Printf("%#v\n", defenderGroup.Ships)
profiler.EndTask("full_simulation")
/* Results */
tasks := profiler.GetTasks()
for n := 0; n < len(tasks); n++ {
theTask := tasks[n]
fmt.Printf("Task "+theTask.Label+" took %v \n", theTask.EndTime.Sub(theTask.StartTime))
}
fmt.Printf("Ok!.\n")
}
func ParseJson(file string, targetPtr interface{}) {
err := json.Unmarshal(ReadFile(file), &targetPtr)
check(err)
}
func ReadFile(path string) []byte {
dat, err := ioutil.ReadFile(path)
check(err)
//fmt.Println(dat)
return dat
}
func check(e error) {
if e != nil {
panic(e)
}
}