-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathround.go
293 lines (260 loc) · 7.95 KB
/
round.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
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
type Round struct {
Players []Player
Id, State int
LastStateChange time.Time
Bonus Point
Bombs map[Point]bool
FrameBuffer Symbols
sync.Mutex
}
func (round *Round) generateMap() {
// http://www.theasciicode.com.ar
for row := 0; row < mapHeight; row++ {
for column := 0; column < mapWidth; column++ {
var char []byte
if (row == 0 || row == mapHeight-1) && column < mapWidth-2 {
char = []byte("─")
} else if column == 0 || column == mapWidth-3 || column == mapWidth-nameTableWidth {
char = []byte("│")
} else if column == mapWidth-2 {
char = []byte("\r")
} else if column == mapWidth-1 {
char = []byte("\n")
} else {
char = []byte(" ")
}
round.FrameBuffer[row*mapWidth+column] = Symbol{0, char}
}
}
}
func (round *Round) generateBot() Player {
// Get data of player and return the structure
for {
p := Player{Name: fmt.Sprintf("Bot %d", rand.Intn(10)+1), Health: 100, Bot: true, Car: Car{Speed: 1}}
if !p.searchDuplicateName(round) {
return p
}
}
}
func (round *Round) getPlayersExcept(exceptions []Player) []*Player {
var rest []*Player
for i := range round.Players {
exception := false
for k := range exceptions {
if round.Players[i].Name == exceptions[k].Name {
exception = true
}
}
if !exception {
rest = append(rest, &round.Players[i])
}
}
return rest
}
func (round *Round) getRandomAliveNonBotPlayerId() int {
var alivePlayer []int
for i := range round.Players {
if round.Players[i].Health > 0 && !round.Players[i].Bot {
alivePlayer = append(alivePlayer, i)
}
}
if len(alivePlayer) > 0 {
return alivePlayer[rand.Intn(len(alivePlayer))]
} else {
return -1
}
}
func (round *Round) gameLogic() {
for i := range round.Players {
if round.Players[i].Bot {
go round.Players[i].moveBot(round)
} else {
go round.Players[i].readDirection(round)
}
go round.Players[i].checkPosition(round)
go round.Players[i].checkSpeed(round)
go round.Players[i].checkHealth(round)
go round.Players[i].checkBomb(round)
}
}
func (round *Round) checkGameOver(activeFrameBuffer Symbols) {
humans := 0
deadHumans := 0
deadPlayers := 0
winnersName := ""
for _, p := range round.Players {
if p.Health <= 0 {
deadPlayers++
}
if !p.Bot {
humans++
if p.Health <= 0 {
deadHumans++
} else {
winnersName = p.Name
}
}
}
secondsLeft := round.LastStateChange.Unix() + maxRoundRunningTimeSec - time.Now().Unix()
if humans == deadHumans || maxPlayersPerRound-deadPlayers == 1 || secondsLeft <= 0 {
round.State = FINISHED
fmt.Println(round.Id, "Round has changed to the state FINISHED")
if maxPlayersPerRound-deadPlayers == 1 {
winnerStr := "THE WINNER IS " + winnersName + "!!!"
for i, char := range []byte(winnerStr) {
activeFrameBuffer[mapWidth*(mapHeight/2-2)+mapWidth/2-len(winnerStr)/2+i] = Symbol{GREEN, []byte{char}}
}
round.writeToAllPlayers(activeFrameBuffer.symbolsToByte(), false)
time.Sleep(5 * time.Second)
}
}
}
func (round *Round) over() {
round.writeToAllPlayers([]byte("Time is out\n"), false)
for _, player := range round.Players {
if player.Bot {
player.Health = 0
continue
}
player.Conn.Close()
}
}
func (round *Round) writeToAllPlayers(message []byte, clean bool) {
for i := range round.Players {
if round.Players[i].Bot {
continue
}
go round.Players[i].writeToThePlayer(message, clean)
}
}
func (round *Round) applyNames(lineBetweenPlayersInBar int) {
for line, player := range round.Players {
for i, char := range []byte(player.Name) {
round.FrameBuffer[(line*lineBetweenPlayersInBar+1)*mapWidth+(mapWidth-nameTableWidth+1)+i] = Symbol{player.Color, []byte{char}}
}
round.FrameBuffer[(line*lineBetweenPlayersInBar+1)*mapWidth+(mapWidth-nameTableWidth+1)+len(player.Name)] = Symbol{RESET, []byte{':'}}
}
}
func (round *Round) applyBonus(activeFrameBuffer []Symbol) {
if round.State == STARTING {
return
}
if round.Bonus.X == -1 && round.Bonus.Y == -1 {
if rand.Int()%lowFactor == 0 {
round.Bonus = Point{rand.Intn(mapWidth-nameTableWidth-2) + 1, rand.Intn(mapHeight-2) + 1}
}
} else {
activeFrameBuffer[round.Bonus.Y*mapWidth+round.Bonus.X] = Symbol{RED, []byte(bonus)}
}
}
func (round *Round) applyBombs(activeFrameBuffer []Symbol, lineBetweenPlayersInBar int) {
if round.State == STARTING {
return
}
round.Lock()
for b := range round.Bombs {
activeFrameBuffer[b.Y*mapWidth+b.X] = Symbol{BOLD, []byte(bomb)}
}
round.Unlock()
}
func (round *Round) applyUserData(activeFrameBuffer []Symbol, lineBetweenPlayersInBar int) {
for num, player := range round.Players {
// Apply health
health := []byte(fmt.Sprintf("Health: %3d", player.Health))
for i, char := range health {
// +1 because health is next line after the name
activeFrameBuffer[((num*lineBetweenPlayersInBar+1)+1)*mapWidth+(mapWidth-3)-len(health)+i] = Symbol{player.Color, []byte{char}}
}
// Apply the amount of bombs to the bar
bombs := []byte(fmt.Sprintf("Bombs: %4d", round.Players[num].Bombs))
for i, char := range bombs {
// +2 because "bombs" is next line after the name
activeFrameBuffer[((num*lineBetweenPlayersInBar+2)+1)*mapWidth+(mapWidth-3)-len(bombs)+i] = Symbol{player.Color, []byte{char}}
}
}
}
func (round *Round) applyGetReady(activeFrameBuffer []Symbol, getReadyCounter *int) {
if round.State == STARTING {
getReady := "GET READY!"
if *getReadyCounter == 0 {
fmt.Println(round.Id, "Round has changed to the state RUNNING")
round.State = RUNNING
} else if *getReadyCounter <= framesPerSecond*1 {
getReady += " 1"
} else if *getReadyCounter <= framesPerSecond*2 {
getReady += " 2"
} else if *getReadyCounter <= framesPerSecond*3 {
getReady += " 3"
}
for i, char := range []byte(getReady) {
activeFrameBuffer[mapWidth*(mapHeight/2-2)+mapWidth/2-len(getReady)/2+i] = Symbol{GREEN, []byte{char}}
}
*getReadyCounter--
}
}
func (round *Round) applyCars(activeMap []Symbol) {
// Do not care about errors with moving away from slice
defer func() {
recover()
}()
for _, player := range round.Players {
charPosX, charPosY := 0, 0
for i := 0; i < len(cars[player.Car.Direction]); i++ {
var chars []byte
if cars[player.Car.Direction][i] == byte('\n') {
charPosY++
charPosX = 0
continue
} else if cars[player.Car.Direction][i] == 226 {
/*
This means extended ASCII is used. After 226 2 bytes must follow
*/
chars = []byte{cars[player.Car.Direction][i], cars[player.Car.Direction][i+1], cars[player.Car.Direction][i+2]}
i += 2
} else if cars[player.Car.Direction][i] == 194 {
/*
This means extended ASCII is used. After 194 1 bytes must follow
*/
chars = []byte{cars[player.Car.Direction][i], cars[player.Car.Direction][i+1]}
i++
} else if player.Health <= 0 && cars[player.Car.Direction][i] == 'o' {
chars = []byte{'x'}
} else {
chars = []byte{cars[player.Car.Direction][i]}
}
activeMap[(player.Car.Borders.Points[LEFTUP].Y+charPosY)*mapWidth+player.Car.Borders.Points[LEFTUP].X+charPosX] = Symbol{player.Color, chars}
charPosX++
}
}
}
// We start round only if more than 0 player is presented
func (round *Round) start() {
lineBetweenPlayersInBar := mapHeight / len(round.Players)
getReadyCounter := getReadyPause / framesPerSecond
round.gameLogic()
round.generateMap()
round.applyNames(lineBetweenPlayersInBar)
for {
activeFrameBuffer := make(Symbols, len(round.FrameBuffer))
copy(activeFrameBuffer, round.FrameBuffer)
round.applyUserData(activeFrameBuffer, lineBetweenPlayersInBar)
round.applyBonus(activeFrameBuffer)
round.applyBombs(activeFrameBuffer, lineBetweenPlayersInBar)
round.applyCars(activeFrameBuffer)
round.applyGetReady(activeFrameBuffer, &getReadyCounter)
round.writeToAllPlayers(activeFrameBuffer.symbolsToByte(), false)
round.checkGameOver(activeFrameBuffer)
if round.State == FINISHED {
round.over()
return
}
time.Sleep(1 % framesPerSecond * 100 * time.Millisecond)
}
}