-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmas.cpp
More file actions
470 lines (405 loc) · 16.8 KB
/
mas.cpp
File metadata and controls
470 lines (405 loc) · 16.8 KB
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
#include <vector>
#include <string>
#include "gui.h"
#include "util.h"
#include "auction.h"
#include "agent.h"
using namespace std;
int px = 5; //User x and y coordinates
int py = 5;
int camx = px; //Camera coordinates
int camy = py;
int ch; //Global char input
int turns = 0;
bool none_toggle = false;
bool stop_program = false;
int main(int argc, char *argv[]){
//srand(time(0));
long int seed;
seed = time(0);
//seed = 1651711755; //low death: 1 trap death. good team dispersment
//seed = 1651710710; //moderate death: 1 mugging, 4 traps. some large teams, some small teams. all around good
//seed = 1651710870; //low death: 2 muggings. Well disperesed teams
//seed = 1651711404; //moderate-high death: 5 mugs, 2 traps. little chaotic
//seed = 1651711500; //high death: 7 mugs, 1 trap. lots of chaos
//seed = 1651711692; //high death: 1 survivor
//seed = 1651712717; //everyone dies
srand(seed);
WINDOW* win = stdscr;
bool show_all_map = false;
setlocale(LC_ALL, "");
initscr();
cbreak(); noecho(); keypad(win, TRUE);
clear();
curs_set(0);
start_color();
//Map map;
Map original_map;
genRandom(original_map); //Generate the rooms (do not have walls yet)
genWalls(original_map); //Add walls around the rooms
genColors(original_map, none_toggle); //Add colors and text to all tile types
addDoor(original_map); //Add doors to rooms and add hallways between doors of different rooms
genRiver(original_map);
genWalls(original_map);
none_toggle = true;
genColors(original_map, none_toggle); //Update the new tiles with colors again
//px = map.rooms[0].lowerx-2; //Set user to coordinates of room[0]
//py = map.rooms[0].lowery-2;
while(1)
{
Map map(original_map);
genColors(map, none_toggle);
auto agents = createAgents(AGENT_COUNT, 1,1, map);
// holds current, ongoing auctions
std::list<Auction> auctions;
turns = 0;
while(1){
turns++;
if(turns % AUTO_DECREASE == 0)
{
for(Agent& agent: agents)
{
for(Agent& opponent: agents)
{
//if((agent.m_team != opponent.m_team || agent.m_team == NO_TEAM) && agent.m_id != opponent.m_id)
if(agent.m_id != opponent.m_id && (opponent.m_team == -2 || agent.m_team != opponent.m_team))
{
agent.m_likableness[opponent.m_id] -= 1;
}
}
}
}
for (Agent& agent: agents)
{
agent.step(agents, map);
}
/////////////////////////////////////////////////////////
// TRAPS
/////////////////////////////////////////////////////////
std::vector<int> trap_subset_indices;
int k = 0;
for(Trap& trap: map.traps)
{
if(trap.isSprung)
{
std::vector<int> dead_agents_indices;
int d = 0;
for(int agentId: trap.agentsTrapped)
{
//Bandaid solution to dying after being saved...
if(agents[agentId].m_stuck == true)
agents[agentId].m_health -= TRAP_DAMAGE_PER_TICK;
if(agents[agentId].m_health <= 0)
{
dead_agents_indices.push_back(d);
}
d++;
}
if(dead_agents_indices.size() > 0)
{
removeSubset(trap.agentsTrapped, dead_agents_indices);
}
}
else // trap is not sprung
{
std::vector<int> agentsOnTile = getAgentsOnTile(trap.x, trap.y, agents, map);
if(agentsOnTile.size() > 0)
{
for(int agentId: agentsOnTile)
{
agents[agentId].m_stuck = true;
trap.agentsTrapped.push_back(agentId);
}
trap.isSprung = true;
}
}
if(trap.isSprung && trap.agentsTrapped.size() == 0)
{
trap_subset_indices.push_back(k);
// update map
map.onTop[trap.y][trap.x] = None;
// map.word[trap.y][trap.x] = " ";
updateTileColor(map, none_toggle, trap.y, trap.x);
}
k++;
}
// remove sprung traps
if(trap_subset_indices.size() > 0)
{
removeSubset(map.traps, trap_subset_indices);
}
/////////////////////////////////////////////////////////
// TREASURES
/////////////////////////////////////////////////////////
std::vector<int> treasure_subset_indices;
k = 0;
for(Treasure& treasure: map.treasures)
{
std::vector<int> agentsOnTile = getAgentsOnTile(treasure.x, treasure.y, agents, map);
if(agentsOnTile.size() == 1)
{
int i = agentsOnTile[0];
// if agent collected treasure for full time
if(agents[i].m_collectStep >= TREASURE_COLLECTION_TIME)
{
// remove treasure
treasure_subset_indices.push_back(k);
// give agent treasure
agents[i].m_collectStep = -1;
agents[i].m_treasureCount += treasure.value;
// update map
map.onTop[treasure.y][treasure.x] = None;
// map.word[treasure.y][treasure.x] = " ";
updateTileColor(map, none_toggle, treasure.y, treasure.x);
}
}
// start auction for treasure
else if(agentsOnTile.size() > 1)
{
// start auction with agents
auctions.push_back(Auction(agents, agentsOnTile, treasure));
for(int i: agentsOnTile)
{
agents[i].joinAuction();
}
// remove treasure from treasure list since its now in an auction
treasure_subset_indices.push_back(k);
// update map
map.onTop[treasure.y][treasure.x] = None;
// map.word[treasure.y][treasure.x] = " ";
updateTileColor(map, none_toggle, treasure.y, treasure.x);
}
k++;
}
// remove collected treasure
if(treasure_subset_indices.size() > 0)
{
removeSubset(map.treasures, treasure_subset_indices);
}
map.updateOnTop();
/////////////////////////////////////////////////////////
// AUCTIONS
/////////////////////////////////////////////////////////
std::vector<int> auction_subset_indices;
k = 0;
for(Auction& auction: auctions)
{
auction.step();
if(auction.m_isOver)
{
auction_subset_indices.push_back(k);
}
k++;
}
// remove completed auctions
if(auction_subset_indices.size() > 0)
{
removeSubset(auctions, auction_subset_indices);
}
/////////////////////////////////////////////////////////////////////////
for (int j = 0; j < LINES; j++){
for (int i = 0; i < COLS / 2; i++){
int x = i + camx - COLS / 4;
int y = j + camy - LINES / 2;
if (x < 0 || y < 0 || x >= sizex || y >= sizey) continue;
//Uncover tiles
if (map.discovered[y][x] < 1){
float new_l = calcLighting(map, px, py, x, y, 8);
if (new_l > map.discovered[y][x])
map.discovered[y][x] = new_l;
}
//Refresh Display Tile
float v = 0.4;
bool end = true;
for (Agent& agent: agents)
{
if (agent.m_health > 0){
v = agent.m_map.discovered[y][x]; //Show map of last indexed agent
px = agent.m_x;
py = agent.m_y; //Causes camera to follow this agent
if (v < .8) v = .4;
if (agent.m_gone == false) end = false;
}
}
if (end){
stop_program = true;
}
if (show_all_map)
v = 1; //Uncomment for complete brightness
//v = map.discovered[y][x]; //Show global map
int agentOnTile = isAgentOnTile(x, y, agents, map);
if (x == px && y == py){ //Display Player
// printT(i*2,j, "Al" ,255,255,1, 200,200,1);
}
if(agentOnTile > -1 && agents[agentOnTile].m_gone == false)
{
std::string agent_name = std::to_string(agentOnTile) + " ";
if (agents[agentOnTile].m_health > 0){
//Colors based on teams
int ra, rb, rc;
ra = rb = rc = 155;
if (int(agents[agentOnTile].m_team == 0)){ra = 0; rb = 255; rc = 255; }
if (int(agents[agentOnTile].m_team == 1)){ra = 0; rb = 0; rc = 255; }
if (int(agents[agentOnTile].m_team == 2)){ra = 0; rb = 255; rc = 0; }
if (int(agents[agentOnTile].m_team == 3)){ra = 255; rb = 255; rc = 255; }
if (int(agents[agentOnTile].m_team == 4)){ra = 255; rb = 0; rc = 255; }
if (int(agents[agentOnTile].m_team == 5)){ra = 255; rb = 255; rc = 0; }
if (int(agents[agentOnTile].m_team >= 6)){
ra = int(agents[agentOnTile].m_team)*96%255;
rb = int(agents[agentOnTile].m_team)*165%255;
rc = int(agents[agentOnTile].m_team)*1287%255;
}
if (int(agents[agentOnTile].m_team == NO_TEAM))
{
int scalar = rand()%1000;
ra = scalar*96%255;
rb = scalar*165%255;
rc = scalar*1287%255;
}
ra *= v; rb *= v; rc *= v;
if (agents[agentOnTile].m_hurt <= 0)
{
//Regular alive color
printT(i*2,j, agent_name ,0,0,0, ra,rb,rc);
}
else
{
//alive with red font
agents[agentOnTile].m_hurt--;
printT(i*2,j, agent_name ,255*v,0,0, ra,rb,rc);
}
}else{
//Totally red background and font
printT(i*2,j, agent_name ,255*v,0,0, 150*v,1,1);
}
}
else{ //Display Tile
printT(i*2,j,
map.word[y][x] ,
map.f_r[y][x]*v,
map.f_g[y][x]*v,
map.f_b[y][x]*v,
map.b_r[y][x]*v,
map.b_g[y][x]*v,
map.b_b[y][x]*v);
}
}
}
ch = getch();
int oldx = px;
int oldy = py;
//oldx = agents[0].m_x;
//oldy = agents[0].m_y;
if (ch == 68){ //Left
px--;
}
if (ch == 67){ //Right
px++;
}
if (ch == 65){ //Up
py--;
show_all_map = 1 - show_all_map;
}
if (ch == 66){ //Down
py++;
break;
}
if (ch == KEY_F(1))
break;
if (px < 0 || px >= sizex || py < 0 || py >= sizey){
px = oldx; py = oldy;
}
//Update camera position if moved too far
oldx = camx;
oldy = camy;
if (px-camx > COLS / 8){
camx = px + COLS / 8;
}
if (px-camx < -COLS / 8){
camx = px - COLS / 8;
}
if (py-camy > LINES / 4){
camy = py + LINES / 4;
}
if (py-camy < -LINES / 4){
camy = py - LINES / 4;
}
if (camx != oldx || camy != oldy){
camx = px; camy = py;
}
if (camx < COLS/4) camx = COLS/4;
if (camy < LINES/2) camy = LINES/2;
if (camx > sizex-COLS/4) camx = sizex-COLS/4;
if (camy > sizey-LINES/2) camy = sizey-LINES/2;
if (stop_program) break;
}
if (stop_program){
//Print end output of all agent stats
endwin();
gotoxy(0,0);
system("clear");
int win = -1;
int winner = -1;
printf("%-8s | %-5s | %-10s | %-5s (%d) | %s \n", "Agent ID", "Team", "Treasures", "Health", INITIAL_AGENT_HEALTH_VALUE, "Death(0 Mug, 1 Trap)");
for (Agent& agent: agents)
{
if (agent.m_health < 0) agent.m_health = 0; //Prevent negative health
if (agent.m_health <= 0 && agent.m_stuck == true) agent.m_deathBy = 1; //Last agent MUST have died to trap
printf("%-8d | %-5d | %-10d | %-5d | %d \n", agent.m_id, agent.m_team, agent.m_treasureCount, agent.m_health, agent.m_deathBy);
if (agent.m_treasureCount > win){
win = agent.m_treasureCount;
winner = agent.m_id;
}
}
printf("\nLikableness values of agents:\n");
// table header
int win_like = 0;
int win_team_count = 0;
int win_change_team = 0;
printf("Agent ");
for (Agent& agent: agents)
{
printf("|%-3d ", agent.m_id);
if (agent.m_id == winner){
for (Agent& agentz: agents){
if (agentz.m_team == agent.m_team && agentz.m_health > 0){
win_team_count += 1;
}
}
if (agent.m_team == -2) win_team_count = 1;
}
}
printf("|\n");
for (Agent& agent: agents)
{
printf("%-5d ", agent.m_id);
for(size_t i = 0; i < agent.m_likableness.size(); i++){
printf("|%-3ld ", agent.m_likableness[i]);
if (agent.m_id == winner)
win_like += agent.m_likableness[i];
}
printf("|\n");
}
printf("\nTeam history of agents:\n");
// table header
printf("Agent |\n");
for (Agent& agent: agents)
{
printf("%-6d ", agent.m_id);
for(size_t i = 0; i < agent.m_teamHistory.size(); i++)
printf("|%-3d ", agent.m_teamHistory[i]);
printf("|\n");
if (agent.m_id == winner){
win_change_team = agent.m_teamHistory.size()-1;
}
}
printf("\nTotal time ticks: %d", turns);
printf("\nWinner: %d", winner);
printf("\nWinner Liked: %d", win_like);
printf("\nWin Team Count: %d ", win_team_count);
printf("\nWin Team Changes: %d \n", win_change_team);
printf("\nseed: %ld \n", seed);
break;
}
}
}