-
Notifications
You must be signed in to change notification settings - Fork 0
/
siktacka-game-logic.cc
240 lines (145 loc) · 4.55 KB
/
siktacka-game-logic.cc
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
#include <iostream>
#include <string>
#include <stdint.h>
#include <set>
#include <list>
#include <deque>
#include <math.h>
#include "siktacka-input-server.h"
#include "siktacka-game-logic.h"
#include "siktacka-consts.h"
// specific random generator
uint32_t next_rand(uint32_t rand) {
uint64_t result = (uint64_t) rand * 279470273;
result %= 4294967291;
return (uint32_t) result;
}
// gets value of and updates server's randomizing value
void rand(uint32_t &var, server_params &sp) {
var = sp.random;
sp.random = next_rand(sp.random);
}
// get float from range, add a translation
long double rand_range(uint32_t modulus, long double correction, server_params &sp) {
uint32_t coord;
long double coord_fl;
rand(coord, sp);
coord %= modulus;
coord_fl = (long double)(coord) + correction;
return coord_fl;
}
void place_snake(snake &player, server_params &sp) {
long double x, y;
long double direction;
snake result;
x = rand_range(sp.width, 0.5, sp);
y = rand_range(sp.height, 0.5, sp);
direction = rand_range(360, 0, sp);
player.x = x;
player.y = y;
player.direction = direction;
}
inline spot map_position(snake &player) {
return spot((uint32_t)(player.x), (uint32_t)(player.y));
}
// check if position is free to take by a snake
inline bool is_free(spot position, std::vector<std::vector<uint8_t> > &map,
server_params &sp) {
return (position.x < sp.width && position.y < sp.height
&& map[position.y][position.x] == 0);
}
void make_map(game_state &gs, server_params &sp) {
gs.map.resize(sp.height);
for (uint32_t i = 0; i < sp.height; i++) {
gs.map[i].resize(sp.width, 0);
}
}
void add_snake(std::string &name, uint8_t &player_id, game_state &gs) {
snake player (player_id, name, 0, 0, 0);
gs.snakes.push_back(player);
player_id ++;
}
void add_snakes(std::vector<std::string> &player_names, game_state &gs) {
uint8_t player_id = 0;
for (std::string name : player_names) {
add_snake(name, player_id, gs);
}
}
void init_game(std::vector<std::string> &player_names, game_state &gs,
server_params &sp) {
event ev;
rand(gs.game_id, sp);
make_map(gs, sp);
add_snakes(player_names, gs);
ev = event_new_game(player_names, 0, sp);
gs.all_events.push_back(ev);
}
void add_pixel_map(uint32_t player_id, spot position, game_state &gs,
server_params &sp) {
event ev;
gs.map[position.y][position.x] = 1;
ev = event_pixel(player_id, position.x, position.y,
gs.all_events.size(), sp);
gs.all_events.push_back(ev);
}
void eliminate_player(uint32_t player_id, game_state &gs, server_params &sp) {
event ev;
ev = event_player_eliminated(player_id, gs.all_events.size(), sp);
gs.all_events.push_back(ev);
}
void end_game(game_state &gs, server_params &sp) {
event ev;
ev = event_game_over(gs.all_events.size(), sp);
gs.all_events.push_back(ev);
}
game_state new_game(std::vector<std::string> &player_names, server_params &sp) {
game_state gs;
snake player;
spot player_spot;
init_game(player_names, gs, sp);
for (auto it = gs.snakes.begin(); it != gs.snakes.end(); ) {
place_snake((*it), sp);
player = *it;
player_spot = map_position(player);
if (is_free(player_spot, gs.map, sp)) {
add_pixel_map(player.id, player_spot, gs, sp);
++it;
} else {
eliminate_player(player.id, gs, sp);
it = gs.snakes.erase(it);
if (gs.snakes.size() <= 1) {
end_game(gs, sp);
break;
}
}
}
return gs;
}
// move snake by 1 unit
void move_snake(snake &player) {
player.x += cos(player.direction * PI / 180);
player.y += sin(player.direction * PI / 180);
}
void round(game_state &gs, server_params &sp) {
snake player;
spot before;
spot after;
for (auto it = gs.snakes.begin(); it != gs.snakes.end(); ) {
spot before = map_position(*it);
move_snake(*it);
spot after = map_position(*it);
if (before.x == after.x && before.y == after.y)
continue;
if (is_free(after, gs.map, sp)) {
add_pixel_map(it->id, after, gs, sp);
++it;
} else {
eliminate_player(it->id, gs, sp);
it = gs.snakes.erase(it);
if (gs.snakes.size() <= 1) {
end_game(gs, sp);
break;
}
}
}
}