-
Notifications
You must be signed in to change notification settings - Fork 4
/
player.go
580 lines (534 loc) · 15.9 KB
/
player.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
package main
import (
"math/rand"
"net"
"time"
)
type Player struct {
Conn net.Conn
Name string
Health int64
LastCrash int64
Color int
Bot bool
Bombs int
DropBomb bool
Car Car
}
func (p *Player) initPlayer(id int) {
switch id {
case 0:
initX, initY := 1, 1
p.Car.Borders = Rectangle{[4]Point{
{initX, initY},
{initX + horizontalCarWidth - 1, initY},
{initX + horizontalCarWidth - 1, initY + horizontalCarHeight - 1},
{initX, initY + horizontalCarHeight - 1}}}
p.Car.Direction = RIGHT
case 1:
initX, initY := mapWidth-nameTableWidth-verticalCarWidth, 1
p.Car.Borders = Rectangle{[4]Point{
{initX, initY},
{initX + verticalCarWidth - 1, initY},
{initX + verticalCarWidth, initY + verticalCarHeight - 1},
{initX, initY + verticalCarHeight - 1}}}
p.Car.Direction = DOWN
case 2:
initX, initY := mapWidth-nameTableWidth-horizontalCarWidth, mapHeight-horizontalCarHeight-1
p.Car.Borders = Rectangle{[4]Point{
{initX, initY},
{initX + horizontalCarWidth - 1, initY},
{initX + horizontalCarWidth - 1, initY + horizontalCarHeight - 1},
{initX, initY + horizontalCarHeight - 1}}}
p.Car.Direction = LEFT
case 3:
initX, initY := 1, mapHeight-verticalCarHeight-1
p.Car.Borders = Rectangle{[4]Point{
{initX, initY},
{initX + verticalCarWidth - 1, initY},
{initX + verticalCarWidth - 1, initY + verticalCarHeight - 1},
{initX, initY + verticalCarHeight - 1}}}
p.Car.Direction = UP
case 4:
initX, initY := (mapWidth-nameTableWidth)/2, mapHeight/2
p.Car.Borders = Rectangle{[4]Point{
{initX, initY},
{initX + verticalCarWidth - 1, initY},
{initX + verticalCarWidth - 1, initY + verticalCarHeight - 1},
{initX, initY + verticalCarHeight - 1}}}
p.Car.Direction = DOWN
}
p.Bombs = 1
p.LastCrash = time.Now().Add(10 * time.Second).Unix()
// Colors are sequential, so we can use first color RED and set the rest based on IDs
p.Color = RED + id
}
func (p *Player) checkBestRoundForPlayer(compileRoundChannel chan Round) {
foundRoundForUser := false
for i := 0; i < len(compileRoundChannel); i++ {
select {
case r := <-compileRoundChannel:
// If any round is "compiling" now
if len(r.Players) < maxPlayersPerRound && !p.searchDuplicateName(&r) {
p.initPlayer(len(r.Players))
r.Players = append(r.Players, *p)
compileRoundChannel <- r
foundRoundForUser = true
break
} else {
compileRoundChannel <- r
}
default:
}
}
if !foundRoundForUser {
// We need a new round
r := Round{Id: rand.Int(), State: COMPILING, FrameBuffer: make([]Symbol, mapWidth*mapHeight), Bonus: Point{-1, -1}, Bombs: make(map[Point]bool)}
p.initPlayer(len(r.Players))
r.Players = append(r.Players, *p)
compileRoundChannel <- r
}
}
func (player *Player) searchDuplicateName(round *Round) bool {
for _, pl := range round.Players {
if player.Name == pl.Name {
return true
}
}
return false
}
func (player *Player) readDirection(round *Round) {
if initTelnet(player.Conn) != nil {
return
}
for {
if player.Health <= 0 || round.State == FINISHED {
return
}
direction := make([]byte, 1)
// Read all possible bytes and try to find a sequence of:
// ESC [ cursor_key
escpos := 0
for {
_, err := player.Conn.Read(direction)
if err != nil {
player.Health = 0
return
}
// Check if telnet want to negotiate something
if escpos == 0 && direction[0] == 255 {
readTelnet(player.Conn)
} else if escpos == 0 && direction[0] == 3 {
// Ctrl+C
player.Health = 0
return
} else if escpos == 0 && direction[0] == 32 {
// Space
if player.Bombs > 0 {
player.DropBomb = true
}
} else if escpos == 0 && direction[0] == 27 {
escpos = 1
} else if escpos == 1 && direction[0] == 91 {
escpos = 2
} else if escpos == 2 {
break
}
}
switch direction[0] {
case 68:
// Left
if player.Car.Direction != RIGHT {
player.Car.Direction = LEFT
} else if player.Car.Speed > 1 {
player.Car.Speed = 1
}
case 67:
// Right
if player.Car.Direction != LEFT {
player.Car.Direction = RIGHT
} else if player.Car.Speed > 1 {
player.Car.Speed = 1
}
case 65:
// Up
if player.Car.Direction != DOWN {
player.Car.Direction = UP
} else if player.Car.Speed > 1 {
player.Car.Speed = 1
}
case 66:
// Down
if player.Car.Direction != UP {
player.Car.Direction = DOWN
} else if player.Car.Speed > 1 {
player.Car.Speed = 1
}
}
}
}
/*
Checks of the DAMAGE_SIDE is safe
*/
func (player *Player) checkOkSide(players []*Player, side int) bool {
for _, p := range players {
if p.Name == player.Name {
continue
}
// If player nextTo someone from the DAMAGE_SIDE - return false
if side == player.Car.Borders.nextTo(&p.Car.Borders, 3) {
return false
}
}
return true
}
func (player *Player) navigateBot(myCenter, targetCenter *Point, restPlayers []*Player) {
// Chose closet way to the target
if targetCenter.X < myCenter.X {
// Target is left from us
if player.Car.Direction != RIGHT && player.checkOkSide(restPlayers, LEFT) {
player.Car.Direction = LEFT
} else if targetCenter.Y < myCenter.Y {
player.Car.Direction = UP
} else {
player.Car.Direction = DOWN
}
} else if targetCenter.X > myCenter.X {
// Target is right from us
if player.Car.Direction != LEFT && player.checkOkSide(restPlayers, RIGHT) {
player.Car.Direction = RIGHT
} else if targetCenter.Y < myCenter.Y {
player.Car.Direction = UP
} else {
player.Car.Direction = DOWN
}
} else if targetCenter.Y < myCenter.Y && player.checkOkSide(restPlayers, UP) {
// Target is above us
if player.Car.Direction != DOWN {
player.Car.Direction = UP
} else if targetCenter.X > myCenter.X {
player.Car.Direction = RIGHT
} else {
player.Car.Direction = LEFT
}
} else if targetCenter.Y > myCenter.Y && player.checkOkSide(restPlayers, DOWN) {
// Target it below us
if player.Car.Direction != UP {
player.Car.Direction = DOWN
} else if targetCenter.X > myCenter.X {
player.Car.Direction = RIGHT
} else {
player.Car.Direction = LEFT
}
}
}
func (player *Player) moveBot(round *Round) {
for {
if player.Health <= 0 || round.State == FINISHED {
return
}
rid := round.getRandomAliveNonBotPlayerId()
if rid == -1 {
continue
}
targetPlayer := &round.Players[rid]
allPlayersExceptMeAndTarget := round.getPlayersExcept([]Player{*targetPlayer, *player})
allPlayersExceptMe := append(allPlayersExceptMeAndTarget, targetPlayer)
heartRect := &Rectangle{Points: [4]Point{
{round.Bonus.X, round.Bonus.Y},
{round.Bonus.X, round.Bonus.Y},
{round.Bonus.X, round.Bonus.Y},
{round.Bonus.X, round.Bonus.Y}},
}
targetCenter := &Point{}
for i := 0; i < 20; i++ {
if round.State == FINISHED || player.Health <= 0 {
return
} else if round.State == RUNNING {
// Check if BOT is throwing the bomb
if player.Bombs > 0 && rand.Int()%highFactor == 0 {
player.DropBomb = true
}
// Do not hunt dead player
if targetPlayer.Health <= 0 {
break
}
if player.Health <= 0 {
return
}
// Navigate Bot based on centers of cars
myCenter := &Point{
player.Car.Borders.Points[LEFTUP].X + (player.Car.Borders.Points[RIGHTUP].X-player.Car.Borders.Points[LEFTUP].X)/2,
player.Car.Borders.Points[LEFTUP].Y + (player.Car.Borders.Points[LEFTDOWN].Y-player.Car.Borders.Points[LEFTUP].Y)/2,
}
// Check if heart next to Bot
heartOnSide := player.Car.Borders.nextTo(heartRect, 5)
// Bot prefers to grab the heart
if heartOnSide != -1 && round.Bonus.X != -1 && round.Bonus.Y != -1 {
targetCenter = &Point{round.Bonus.X, round.Bonus.Y}
} else {
targetCenter = &Point{
targetPlayer.Car.Borders.Points[LEFTUP].X + (targetPlayer.Car.Borders.Points[RIGHTUP].X-targetPlayer.Car.Borders.Points[LEFTUP].X)/2,
targetPlayer.Car.Borders.Points[LEFTUP].Y + (targetPlayer.Car.Borders.Points[LEFTDOWN].Y-targetPlayer.Car.Borders.Points[LEFTUP].Y)/2,
}
}
player.navigateBot(myCenter, targetCenter, allPlayersExceptMe)
}
time.Sleep(time.Duration(500 * time.Millisecond))
}
}
}
func (player *Player) checkHitWall() bool {
for _, point := range player.Car.Borders.Points {
if point.X < 1 || point.X > mapWidth-nameTableWidth || point.Y < 1 || point.Y > mapHeight-1 {
player.Health -= DAMAGE_FRONT * player.Car.Speed
return true
}
}
return false
}
func (player *Player) checkHitAnotherCar(round *Round) bool {
for _, opponent := range round.Players {
if player.Name == opponent.Name {
continue
}
if player.Car.Borders.intersects(&opponent.Car.Borders) {
switch player.Car.Borders.nextTo(&opponent.Car.Borders, 0) {
case LEFT:
// Player was hit from LEFT
switch player.Car.Direction {
case LEFT:
// DAMAGE_FRONT crash
player.Health -= DAMAGE_FRONT * player.Car.Speed
case RIGHT:
// DAMAGE_BACK crash
player.Health -= DAMAGE_BACK * (maxSpeed - player.Car.Speed)
case UP | DOWN:
// DAMAGE_SIDE crash
player.Health -= DAMAGE_SIDE
}
case RIGHT:
// Player was hit from RIGHT
switch player.Car.Direction {
case RIGHT:
// DAMAGE_FRONT crash
player.Health -= DAMAGE_FRONT * player.Car.Speed
case LEFT:
// DAMAGE_BACK crash
player.Health -= DAMAGE_BACK * (maxSpeed - player.Car.Speed)
case UP | DOWN:
// DAMAGE_SIDE crash
player.Health -= DAMAGE_SIDE
}
case UP:
// Player was hit from UP
switch player.Car.Direction {
case UP:
// DAMAGE_FRONT crash
player.Health -= DAMAGE_FRONT * player.Car.Speed
case DOWN:
// DAMAGE_BACK crash
player.Health -= DAMAGE_BACK * (maxSpeed - player.Car.Speed)
case LEFT | RIGHT:
// DAMAGE_SIDE crash
player.Health -= DAMAGE_SIDE
}
case DOWN:
// Player was hit from DOWN
switch player.Car.Direction {
case DOWN:
// DAMAGE_FRONT crash
player.Health -= DAMAGE_FRONT * player.Car.Speed
case UP:
// DAMAGE_BACK crash
player.Health -= DAMAGE_BACK * (maxSpeed - player.Car.Speed)
case LEFT | RIGHT:
// DAMAGE_SIDE crash
player.Health -= DAMAGE_SIDE
}
}
// Hit in the back
if (player.Car.Direction == RIGHT && opponent.Car.Direction == LEFT) ||
(player.Car.Direction == LEFT && opponent.Car.Direction == RIGHT) ||
(player.Car.Direction == UP && opponent.Car.Direction == DOWN) ||
(player.Car.Direction == DOWN && opponent.Car.Direction == UP) {
// Face to face
player.Health -= DAMAGE_FRONT * player.Car.Speed
} else if (player.Car.Direction == RIGHT && opponent.Car.Direction == UP) ||
(player.Car.Direction == LEFT && opponent.Car.Direction == UP) ||
(player.Car.Direction == RIGHT && opponent.Car.Direction == DOWN) ||
(player.Car.Direction == LEFT && opponent.Car.Direction == DOWN) ||
(player.Car.Direction == UP && opponent.Car.Direction == RIGHT) ||
(player.Car.Direction == UP && opponent.Car.Direction == LEFT) ||
(player.Car.Direction == DOWN && opponent.Car.Direction == RIGHT) ||
(player.Car.Direction == DOWN && opponent.Car.Direction == LEFT) {
// Side hit
player.Health -= DAMAGE_SIDE * player.Car.Speed
} else {
// Back hit
player.Health -= DAMAGE_BACK * (player.Car.Speed - opponent.Car.Speed)
}
return true
}
}
return false
}
func (player *Player) checkHit(round *Round) {
if player.checkHitWall() || player.checkHitAnotherCar(round) {
player.Car.recalculateBorders(true)
player.LastCrash = time.Now().Unix()
// Bounce player to the opposite direction
switch player.Car.Direction {
case RIGHT:
player.Car.Direction = LEFT
case LEFT:
player.Car.Direction = RIGHT
case UP:
player.Car.Direction = DOWN
case DOWN:
player.Car.Direction = UP
}
}
}
func (player *Player) checkHitBonus(round *Round) {
bonusRect := &Rectangle{Points: [4]Point{
{round.Bonus.X, round.Bonus.Y},
{round.Bonus.X, round.Bonus.Y},
{round.Bonus.X, round.Bonus.Y},
{round.Bonus.X, round.Bonus.Y}},
}
if player.Car.Borders.intersects(bonusRect) {
player.Health += bonusPoint
player.Car.Speed = maxSpeed
round.Bonus.X, round.Bonus.Y = -1, -1
}
}
func (player *Player) checkHitBomb(round *Round) {
round.Lock()
for bomb := range round.Bombs {
bombRect := &Rectangle{Points: [4]Point{
{bomb.X, bomb.Y},
{bomb.X, bomb.Y},
{bomb.X, bomb.Y},
{bomb.X, bomb.Y}},
}
if player.Car.Borders.intersects(bombRect) {
player.Health -= bonusPoint
player.LastCrash = time.Now().Unix()
player.Car.Speed = 1
delete(round.Bombs, bomb)
}
}
round.Unlock()
}
func (player *Player) checkPosition(round *Round) {
for {
if player.Health <= 0 || round.State == FINISHED {
return
} else if round.State == RUNNING {
// Move player
player.Car.recalculateBorders(false)
// Check if we catch the bonus
player.checkHitBonus(round)
// Check if we hit the bomb
player.checkHitBomb(round)
// Check if we hit something
player.checkHit(round)
}
/*
Because vertical symbols are 3x bigger, than horizontal, we need to slowdown recalculation of vertical objects
*/
slowerDown := int64(1)
if player.Car.Direction == UP || player.Car.Direction == DOWN {
slowerDown = 3
}
time.Sleep(time.Duration(slowerDown*150/player.Car.Speed) * time.Millisecond)
}
}
func (player *Player) checkSpeed(round *Round) {
for {
if player.Health <= 0 || round.State == FINISHED {
return
}
now := time.Now().Unix()
if now-player.LastCrash > player.Car.Speed*2 && player.Car.Speed < maxSpeed {
player.Car.Speed++
} else if now-player.LastCrash < 2 {
player.Car.Speed = 1
}
time.Sleep(1 % framesPerSecond * 100 * time.Millisecond)
}
}
func (player *Player) checkHealth(round *Round) {
for {
if player.Health <= 0 {
player.Health = 0
return
}
for num, player := range round.Players {
if player.Health > 100 {
round.Players[num].Health = 100
} else if player.Health <= 0 {
round.Players[num].Health = 0
round.Players[num].Color = BOLD
}
}
time.Sleep(1 % framesPerSecond * 100 * time.Millisecond)
}
}
func (player *Player) checkBomb(round *Round) {
for {
if player.Health <= 0 || round.State == FINISHED {
return
}
for num, player := range round.Players {
if player.DropBomb {
bombPosition := Point{}
switch player.Car.Direction {
case LEFT:
bombPosition.X = player.Car.Borders.Points[RIGHTUP].X + 1
bombPosition.Y = player.Car.Borders.Points[RIGHTUP].Y + (player.Car.Borders.Points[RIGHTDOWN].Y-player.Car.Borders.Points[RIGHTUP].Y)/2
case RIGHT:
bombPosition.X = player.Car.Borders.Points[LEFTUP].X - 1
bombPosition.Y = player.Car.Borders.Points[RIGHTUP].Y + (player.Car.Borders.Points[RIGHTDOWN].Y-player.Car.Borders.Points[RIGHTUP].Y)/2
case UP:
bombPosition.X = player.Car.Borders.Points[LEFTUP].X + (player.Car.Borders.Points[RIGHTUP].X-player.Car.Borders.Points[LEFTUP].X)/2
bombPosition.Y = player.Car.Borders.Points[LEFTDOWN].Y + 1
case DOWN:
bombPosition.X = player.Car.Borders.Points[LEFTUP].X + (player.Car.Borders.Points[RIGHTUP].X-player.Car.Borders.Points[LEFTUP].X)/2
bombPosition.Y = player.Car.Borders.Points[LEFTUP].Y - 1
}
if bombPosition.X > 1 && bombPosition.X < mapWidth-nameTableWidth-1 && bombPosition.Y > 1 && bombPosition.Y < mapHeight-1 {
round.Players[num].DropBomb = false
round.Players[num].Bombs--
round.Lock()
round.Bombs[bombPosition] = true
round.Unlock()
}
} else if rand.Int()%(highFactor*lowFactor) == 0 && round.State == RUNNING {
round.Players[num].Bombs++
}
}
time.Sleep(1 % framesPerSecond * 100 * time.Millisecond)
}
}
func (player *Player) writeToThePlayer(message []byte, clean bool) {
if clean {
_, err := player.Conn.Write(clear)
if err != nil {
// Kick user if connection got lost
player.Health = 0
return
}
}
_, err := player.Conn.Write(home)
if err != nil {
player.Health = 0
return
}
_, err = player.Conn.Write(message)
if err != nil {
player.Health = 0
return
}
}