-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
463 lines (424 loc) · 11.3 KB
/
main.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
package main
import (
"fmt"
"math/rand"
"os"
"strconv"
"time"
"github.com/google/uuid"
)
var r *rand.Rand
func setTileInMap(x int, y int, tile Tile, gameMap *[mapWidth][mapHeight]*Tile) {
newTile := Tile{tile.Terrain, tile.Zombies, tile.Players}
gameMap[x][y] = &newTile
}
func initMap(r rand.Rand, gMap *[mapWidth][mapHeight]*Tile) {
fmt.Println("Initializing game map...")
for a, column := range gMap {
for b := range column {
choice := r.Intn(len(terrainTypes) - 1)
var tile = Tile{terrainTypes[choice], 0, []Player{}}
//gMap[a][b] = &tile
setTileInMap(a, b, tile, gMap)
if gMap[a][b] == nil {
fmt.Println("Map tile assigned nil")
}
}
}
}
func createCityList(gMap *[mapWidth][mapHeight]*Tile) []IntTuple {
var cities []IntTuple
for a, column := range gMap {
for b, tile := range column {
if tile.Terrain == City {
var coordinates = IntTuple{a, b}
cities = append(cities, coordinates)
}
}
}
return cities
}
func getMapTile(x int, y int, gMap *[mapWidth][mapHeight]*Tile) *Tile {
if x < 0 || x >= mapWidth || y < 0 || y >= mapHeight {
fmt.Printf("Prevented tile access at %d/%d", x, y)
return &Tile{Edge, -1, []Player{}}
}
var truncX = x % 100
var truncY = y % 100
return (*gMap)[truncX][truncY]
}
func move(playerMap *map[string]*Player) {
//Set new coordinates per player from move
for _, player := range *playerMap {
if !player.Alive {
continue
}
//Fetch current player state
var player = (*playerMap)[player.ID]
//Perform move
switch player.Direction {
case North:
player.Y += 1
case East:
player.X += 1
case South:
player.Y -= 1
case West:
player.X -= 1
case Stay:
return
}
//Write new coordinates
(*playerMap)[player.ID] = player
//Prevent out-of-map moves
if mapWidth <= player.X {
player.X = mapWidth - 1
}
if player.X < 0 {
player.X = 0
}
if mapHeight <= player.Y {
player.Y = mapHeight - 1
}
if player.Y < 0 {
player.Y = 0
}
//Reset move direction
player.Direction = defaultDirection
//Write new state
(*playerMap)[player.ID] = player
}
}
func getFirstEmptyHandSlot(hand [5]Card) int {
var firstEmpty = -1
for f, card := range hand {
if card == None {
return f
}
}
return firstEmpty
}
func resources(playerMap *map[string]*Player, gMap [mapWidth][mapHeight]*Tile) {
for playerID, _ := range *playerMap {
var player = (*playerMap)[playerID]
if !player.Alive {
continue
}
var firstEmpty = getFirstEmptyHandSlot(player.Cards)
//Add card from tile
if firstEmpty > -1 {
switch gMap[player.X][player.Y].Terrain {
case Forest: //Special case, rewards 2 cards
player.Cards[firstEmpty] = Wood
firstEmpty = getFirstEmptyHandSlot(player.Cards)
if firstEmpty > -1 {
player.Cards[firstEmpty] = Wood
}
case City:
player.Cards[firstEmpty] = Weapon
case Farm:
player.Cards[firstEmpty] = Food
case Laboratory:
player.Cards[firstEmpty] = Research
}
}
(*playerMap)[playerID] = player
}
}
func consume(playerMap *map[string]*Player, gMap *[mapWidth][mapHeight]*Tile) {
for playerID, _ := range *playerMap {
//Fetch current player state
var player = (*playerMap)[playerID]
if !player.Alive {
continue
}
//We don't allow death by indecision
if player.Consume == None {
_, hasCard := playerHasCard(player, Food)
if hasCard {
player.Consume = Food
} else {
player.Consume = Wood
}
}
//Now remove that card or kill the player
cardPos, hasCard := playerHasCard(player, player.Consume)
if hasCard {
if player.Consume == Wood {
var zombiesAttracted = 0
var tileNW = getMapTile(player.X-1, player.Y+1, gMap)
var tileNN = getMapTile(player.X, player.Y+1, gMap)
var tileNE = getMapTile(player.X+1, player.Y+1, gMap)
var tileWW = getMapTile(player.X-1, player.Y, gMap)
var tileEE = getMapTile(player.X+1, player.Y, gMap)
var tileSW = getMapTile(player.X-1, player.Y-1, gMap)
var tileSS = getMapTile(player.X, player.Y-1, gMap)
var tileSE = getMapTile(player.X+1, player.Y-1, gMap)
var tileArray = []*Tile{tileNW, tileNN, tileNE, tileWW, tileEE, tileSW, tileSS, tileSE}
//Remove zombies from surrounding tiles
for _, nextTile := range tileArray {
if nextTile.Zombies > 0 {
zombiesAttracted++
nextTile.Zombies--
}
}
//Add to players tile
getMapTile(player.X, player.Y, gMap).Zombies += zombiesAttracted
}
player.Cards[cardPos] = None //Remove card from hand
(*playerMap)[player.ID] = player
} else {
player.Alive = false //Card not in hand, kill the player
(*playerMap)[player.ID] = player
}
}
}
func getHandSize(player *Player) int {
var count = 0
for _, card := range player.Cards {
if card != None {
count++
}
}
return count
}
func limitCards(playerMap *map[string]*Player) {
for mapKey := range *playerMap {
var player = (*playerMap)[mapKey]
if getHandSize(player) > 4 {
var cardPos, hasCard = playerHasCard(player, player.Discard)
if hasCard && player.Discard != None && cardPos > -1 { //Better safe...
player.Cards[cardPos] = None
} else {
player.Cards[4] = None
}
}
player.Discard = None
(*playerMap)[mapKey] = player
}
}
func handleCombat(gMap *[mapWidth][mapHeight]*Tile, playerMap *map[string]*Player) {
//Create groups from position
var combatGroups = make(map[IntTuple][]*Player)
for mapKey := range *playerMap {
var player = (*playerMap)[mapKey]
var pos = IntTuple{player.X, player.Y}
combatGroups[pos] = append(combatGroups[pos], player)
}
for _, group := range combatGroups {
fight(gMap, group)
}
}
func fight(gMap *[mapWidth][mapHeight]*Tile, group []*Player) {
//Calculate dice + weapon VS Zombies per group
var attackValue = 0
var x = group[0].X
var y = group[0].Y
for a, player := range group {
if player.Play == Weapon {
cardIndex, hasCard := playerHasCard(player, Weapon)
if hasCard {
attackValue += weaponStrength
group[a].Cards[cardIndex] = None
} else {
attackValue += r.Intn(playerMaxAttack-1) + playerMinAttack
}
} else {
attackValue += r.Intn(playerMaxAttack-1) + playerMinAttack
}
group[a].Play = Dice
}
if attackValue < gMap[x][y].Zombies {
for a := range group {
group[a].Alive = false
}
gMap[x][y].Zombies += len(group)
} else {
gMap[x][y].Zombies = 0
}
}
// TODO: citiesList can be value instead of reference
// TODO: decide if spread is 4 or 8 directions
func spread(gMap *[mapWidth][mapHeight]*Tile, cities *[]IntTuple) {
for _, city := range *cities {
if gMap[city.X][city.Y].Zombies < zombieCutoff {
gMap[city.X][city.Y].Zombies++
continue
}
//North
if city.Y < mapHeight-1 && gMap[city.X][city.Y+1].Zombies < zombieCutoff {
gMap[city.X][city.Y+1].Zombies++
}
//East
if city.X < mapWidth-1 && gMap[city.X+1][city.Y].Zombies < zombieCutoff {
gMap[city.X+1][city.Y].Zombies++
}
//South
if city.Y > 0 && gMap[city.X][city.Y-1].Zombies < zombieCutoff {
gMap[city.X][city.Y-1].Zombies++
}
//West
if city.X > 0 && gMap[city.X-1][city.Y].Zombies < zombieCutoff {
gMap[city.X-1][city.Y].Zombies++
}
}
}
// TODO: Look for gameMap[x][y] nil cause here
func updatePlayerPositions(playerMap *map[string]*Player, gMap *[mapWidth][mapHeight]*Tile) {
//Clear all player info in tiles
for _, tileRow := range *gMap {
for _, tile := range tileRow {
(*tile).Players = []Player{}
}
}
//Reposition all players
for _, player := range *playerMap {
var tilePList = gMap[player.X][player.Y].Players
gMap[player.X][player.Y].Players = append(tilePList, *player)
if gMap[player.X][player.Y] == nil {
fmt.Println("Panic - gMap has nil pointer")
}
}
}
// TODO: Unify order of attributes across functions
func tick(gMap *[mapWidth][mapHeight]*Tile, cities *[]IntTuple, playerMap *map[string]*Player) {
fmt.Println("Next tick is happening...")
fmt.Println("Moving players...")
move(playerMap)
fmt.Println("Distributing ressources...")
resources(playerMap, *gMap)
fmt.Println("Combat is upon us...")
handleCombat(gMap, playerMap)
fmt.Println("The infection is spreading...")
spread(gMap, cities)
fmt.Println("Players feeding themselves...")
consume(playerMap, gMap)
fmt.Println("Limiting player inventory")
limitCards(playerMap)
fmt.Println("Updating player info on game map...")
updatePlayerPositions(playerMap, gMap)
}
func playerHasCard(player *Player, card Card) (int, bool) {
for a, c := range player.Cards {
if c == card {
return a, true
}
}
return -1, false
}
func randomizeBots(bots []*Player) {
fmt.Println("Randomizing bot turns...")
for _, bot := range bots {
//Randomize movement
bot.Direction = Directions[r.Intn(len(Directions))]
//Randomize card played
bot.Play = Dice
//Randomize consume
if _, foodFound := playerHasCard(bot, Food); foodFound {
bot.Consume = Food
} else if _, woodFound := playerHasCard(bot, Wood); woodFound {
bot.Consume = Wood
} else {
bot.Consume = None
}
//Randomize discard
_, found := playerHasCard(bot, None)
if !found {
bot.Discard = bot.Cards[0]
}
}
}
// TODO: Somehow remove inactive players
func addPlayer(playerMap *map[string]*Player, playerName string) string {
var rX = r.Intn(mapWidth - 1)
var rY = r.Intn(mapHeight - 1)
playerID, _ := uuid.NewV7()
idString := playerID.String()
var player = Player{
ID: idString,
Name: playerName,
X: rX,
Y: rY,
Direction: defaultDirection,
Play: None,
Consume: None,
Discard: None,
Cards: [5]Card{Food, Wood, Wood, None, None},
Alive: true,
IsBot: false,
}
(*playerMap)[idString] = &player
return idString
}
func addBot(playerMap *map[string]*Player, bots *[]*Player, bID int) {
var rX = r.Intn(mapWidth - 1)
var rY = r.Intn(mapHeight - 1)
var bot = Player{
ID: strconv.Itoa(bID),
X: rX,
Y: rY,
Direction: Stay,
Play: None,
Consume: None,
Discard: None,
Cards: [5]Card{Food, Wood, Wood, None, None},
Alive: true,
IsBot: true,
}
(*playerMap)[strconv.Itoa(bID)] = &bot
*bots = append(*bots, &bot)
}
func restockBots(playerMap *map[string]*Player, bots *[]*Player, bID *int) {
var botDelta = botNumber - len(*bots)
for i := 0; i < botDelta; i++ {
addBot(playerMap, bots, *bID)
*bID++
}
}
func havePlayersWon(playerMap map[string]*Player) bool {
for _, player := range playerMap {
if player.hasWinCondition() {
return true
}
}
return false
}
func main() {
if len(os.Args) == 2 {
idSalt = os.Args[1]
fmt.Println(idSalt)
}
var gameMap [mapWidth][mapHeight]*Tile
var cityList []IntTuple
var playerMap = make(map[string]*Player)
var botList []*Player
var botID = 0
var turnTimer = int8(turnLength)
hasWon = false
r = rand.New(rand.NewSource(time.Now().Unix()))
initMap(*r, &gameMap)
//Data set up, now we can start the API
go setupAPI(&playerMap, &gameMap, &turnTimer, &hasWon)
cityList = createCityList(&gameMap)
var remainingTurns = maxTurns
for ; remainingTurns > 0; remainingTurns-- {
fmt.Println("Remaining turns: ", remainingTurns)
for turnTimer = int8(turnLength); turnTimer >= 0; turnTimer-- {
if turnTimer == 0 {
randomizeBots(botList)
tick(&gameMap, &cityList, &playerMap)
hasWon = havePlayersWon(playerMap)
restockBots(&playerMap, &botList, &botID)
if hasWon {
fmt.Println("Game over due to win")
remainingTurns = 0
turnTimer = -1
break
}
} else {
time.Sleep(time.Second)
}
}
}
time.Sleep(120 * time.Second)
}