-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
749 lines (662 loc) · 19.2 KB
/
game.c
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include "server.h"
#include "game.h"
#include "sort.h"
#include "gameconstants.h"
#include "tokens.h"
typedef struct gamer_instance *p_gamer_instance;
/* contains pointers to active gamers */
enum BUFFERSIZE_T{
BUFFERSIZE = 512
};
static struct global_t{
p_gamer_instance * gamers;
int total_players;
int active_players;
int finished_players;
int trade_players;
int month;
int current_market_state;
char message_buffer[BUFFERSIZE];
char message_buffer_m[BUFFERSIZE];
int gc_counter;
} global;
enum auction_type {
RESOURCE,
PRODUCTION
};
/* In-game interface */
void game_request_get_playerinfo(int from_id, int about_id);
void game_request_get_marketinfo(int from_id);
void game_request_get_generalinfo(int from_id);
void game_request_build_factory(int id, int quantity);
void game_request_convert_resource(int id, int quantity);
void game_request_auction_bid(int id, enum auction_type type, int value, int price);
void game_request_finish_turn(int id);
void game_request_help(int id);
void game_player_pay(int id, int money);
int game_player_total_month_spent(int id);
void game_auction_totals();
void game_month_totals();
void reset_request(int id);
p_gamer_instance new_gamer_instance(int id)
{
p_gamer_instance p;
int i;
p = (p_gamer_instance) malloc (sizeof(*p));
p->id = id;
p->res = default_gamer.res;
p->prod = default_gamer.prod;
p->factory[0] = default_gamer.factory[0];
p->cash = default_gamer.cash;
p->finished_turn = 0;
p->request.res2prod = 0;
p->request.res_value = 0;
p->request.res_price = 0;
p->request.res_approved = 0;
p->request.prod_value = 0;
p->request.prod_price = 0;
p->request.prod_approved = 0;
for(i = 1; i < FACTORY_BUILD_TIME; i++){
p->factory[i] = 0;
}
return p;
}
void game_new_month()
{
int id, i, r;
int acc;
global.month++;
global.finished_players = 0;
global.trade_players = 0;
for(id = 0; id < global.total_players; id++){
if(global.gamers[id] != NULL){
/* shift the factories to the month back */
int * factories = (int *)(global.gamers[id]->factory);
int k, curr_factories = factories[0];
for(k = 0; k < FACTORY_BUILD_TIME-1; k++){
factories[k] = factories[k+1];
}
factories[0] += curr_factories;
factories[FACTORY_BUILD_TIME-1] = 0;
sprintf(global.message_buffer,
"Congrutilations! You have %d new factories\n",
curr_factories);
/* reset monthly activity */
reset_request(id);
global.gamers[id]->finished_turn = 0;
}
}
/* Change market state according to the Markov process */
r = 1 + (int)(12.0*rand()/(RAND_MAX+1.0)); /* r in [1,...,12] */
i = acc = 0;
while(acc < r){
acc += Markov_matrix[global.current_market_state][i];
i++;
}
/* i refer to the_next_state */
global.current_market_state = i - 1;
sprintf(global.message_buffer,
"Happy new %d month! The market state is %d\n",
global.month, global.current_market_state+1);
send_broadcast(global.message_buffer, S_HUMAN);
sprintf(global.message_buffer_m,
"new_month %d %d\n",
global.month, global.current_market_state+1);
send_broadcast(global.message_buffer_m, S_BROADCAST);
}
void game_start(int n)
{
int id;
srand(clock());
global.gamers = (p_gamer_instance *) malloc (n * sizeof(*global.gamers));
global.active_players = global.total_players = n;
for(id = 0; id < global.total_players; id++){
global.gamers[id] = new_gamer_instance(id);
}
global.month = 1;
global.finished_players = 0;
global.trade_players = 0;
global.current_market_state = default_market_state - 1;
global.gc_counter = 0;
}
void game_finish()
{
int i;
for(i = 0; i < global.total_players; i++){
free(global.gamers[i]);
}
free(global.gamers);
global.gamers = 0;
}
void game_kick(int id)
{
if(global.gamers != NULL && id >= 0 && id < global.total_players){
if(global.gamers[id] != NULL){
if(global.gamers[id]->finished_turn){
global.finished_players--;
}
free(global.gamers[id]);
global.gamers[id] = NULL;
global.active_players--;
}
}
}
void reset_request(int id){
p_gamer_instance g = global.gamers[id];
if(g != NULL){
g->request.res2prod = 0;
g->request.res_value = 0;
g->request.res_price = 0;
g->request.res_approved = 0;
g->request.prod_value = 0;
g->request.prod_price = 0;
g->request.prod_approved = 0;
}
}
void game_request_get_playerinfo(int from_id, int about_id)
{
p_gamer_instance g;
int id = about_id;
if(!(id >= 0 && id < global.total_players)){
sprintf(global.message_buffer,
"Wrong player id[%d]. "
"Try another value between 0 and %d\n",
id, global.total_players);
sprintf(global.message_buffer_m, "w id=%d\n", id);
} else
if((g = global.gamers[id]) == NULL){
sprintf(global.message_buffer,
"The player[%d] went into a bankrupt\n", id);
sprintf(global.message_buffer_m,
"g b id=%d\n", id); // feedback bankrupt
} else {
int i, all_factories = 0;
for(i = 0; i < FACTORY_BUILD_TIME; i++){
all_factories += g->factory[i];
}
sprintf(global.message_buffer,
"Player[%d] info:\n"
"\tmoney\t= %ld\n"
"\tfactories\t(active) = %d\t(in progress) = %d\n"
"\tproduction\t= %d\n"
"\tresource\t= %d\n",
id, g->cash,
g->factory[0], all_factories - g->factory[0],
g->prod, g->res);
sprintf(global.message_buffer_m,
"g %ld %d %d %d %d id=%d\n",
g->cash, g->factory[0], all_factories - g->factory[0],
g->prod, g->res, id);
}
send_dirrect(from_id, global.message_buffer, S_HUMAN);
send_dirrect(from_id, global.message_buffer_m, S_MACHINE);
}
void game_request_get_generalinfo(int from_id)
{
sprintf(global.message_buffer,
"General info (game constants):\n"
"\tFactiory build time\t= %d\n"
"\tFactory price\t= %d\n"
"\tConvertation (resource->production) price\t= %d\n"
"\tMonthy expenditures (for each object):\n"
"\t\tfactory\t= %d\n"
"\t\tresource\t= %d\n"
"\t\tproduction\t= %d\n"
,FACTORY_BUILD_TIME,factory_price,
convertation_price,
monthy.factory, monthy.res, monthy.prod
);
send_dirrect(from_id, global.message_buffer, S_HUMAN);
}
void game_request_get_marketinfo(int from_id)
{
struct market_state_t * ms =
(struct market_state_t *)(market_state + global.current_market_state);
sprintf(global.message_buffer,
"Market info:\n"
"\tMarket state#\t= %d\n"
"\tMinimal resource price\t= %d\n"
"\tMaximal production price\t= %d\n"
"\tResource quantity\t= %d\n"
"\tProduction needs\t= %d\n"
"\tActive players\t= %d\n",
global.current_market_state+1,
ms->res_price, ms->prod_price,
(int)(ms->res_coeff * global.active_players),
(int)(ms->prod_coeff * global.active_players),
global.active_players
);
sprintf(global.message_buffer_m, "g %d\n", global.current_market_state+1);
send_dirrect(from_id, global.message_buffer, S_HUMAN);
send_dirrect(from_id, global.message_buffer_m, S_MACHINE);
}
void game_request_help(int id)
{
if(global.gamers[id] != NULL){
send_dirrect(id, help_info, S_HUMAN);
} else {
send_dirrect(id, help_info_for_bankrupts, S_HUMAN);
}
}
void game_player_pay(int id, int money)
{
if(global.gamers[id] != NULL){
global.gamers[id]->cash -= money;
if(global.gamers[id]->cash < 0){
sprintf(global.message_buffer,
"Player [%d] goes bankrupt\n", id);
sprintf(global.message_buffer_m,
"info b %d\n", id);
send_broadcast(global.message_buffer, S_HUMAN);
send_broadcast(global.message_buffer_m, S_BROADCAST);
game_kick(id);
}
}
}
int game_player_total_month_spent(int id)
{
p_gamer_instance g = global.gamers[id];
struct request_t * r;
if(g == NULL){
return 0;
}
r = &(g->request);
return /* factory building */
/* the first factory building is payed immediately
* not in the end of the month */
g->factory[1] * factory_price/2 +
/* market trades */
(- r->prod_approved * r->prod_price) +
r->res_approved * r->res_price +
/* monthly expenditures */
g->factory[0] * monthy.factory +
g->prod * monthy.prod +
g->res * monthy.res;
}
int cmp_res(int id1, int id2){
return global.gamers[id1]->request.res_price >
global.gamers[id2]->request.res_price;
}
int cmp_prod(int id1, int id2){
return global.gamers[id1]->request.prod_price <
global.gamers[id2]->request.prod_price;
}
void game_auction_totals()
{
int * buffer = (int *) malloc (global.trade_players * sizeof(int));
int * sorted = (int *) malloc (global.trade_players * sizeof(int));
int val_left, val_transfered, traders;
int i, lo, hi, tmp, r;
p_gamer_instance * g = global.gamers;
/* resource trade */
traders = 0;
for(i = 0; i < global.total_players; i++){
if(g[i] != NULL){
if(g[i]->request.res_value != 0){
sorted[traders++] = i;
}
}
}
sort(sorted, traders, cmp_res, buffer);
/* take gifts */
val_left = (int) (market_state[global.current_market_state].res_coeff *
global.active_players);
lo = hi = 0;
while(val_left > 0 && lo < traders){
if(lo > hi){
hi = lo;
}
while(hi < traders && g[sorted[lo]]->request.res_price ==
g[sorted[hi]]->request.res_price){
hi++;
}
hi--;
while(lo <= hi && val_left > 0){
if(lo < hi){
/* pick one of them by random */
r = lo + (int)(((float)(hi-lo+1))*rand()/(RAND_MAX+1.0));
/* swap sorted[r] and sorted[lo] */
tmp = sorted[r];
sorted[r] = sorted[lo];
sorted[lo] = tmp;
}
if(g[sorted[lo]]->request.res_value < val_left){
val_transfered = g[sorted[lo]]->request.res_value;
} else {
val_transfered = val_left;
}
g[sorted[lo]]->request.res_approved = val_transfered;
val_left -= val_transfered;
lo++;
}
}
/* production trade */
traders = 0;
for(i = 0; i < global.total_players; i++){
if(g[i] != NULL){
if(g[i]->request.prod_value != 0){
sorted[traders++] = i;
}
}
}
sort(sorted, traders, cmp_prod, buffer);
/* take gifts */
val_left = (int) (market_state[global.current_market_state].prod_coeff *
global.active_players);
lo = hi = 0;
while(val_left > 0 && lo < traders){
if(lo > hi){
hi = lo;
}
while(hi < traders && g[sorted[lo]]->request.prod_price ==
g[sorted[hi]]->request.prod_price )
{
hi++;
}
hi--;
while(lo <= hi && val_left > 0){
if(lo < hi){
/* pick one of them by random */
r = lo + (int)(((float)(hi-lo+1))*rand()/(RAND_MAX+1.0));
/* swap sorted[r] and sorted[lo] */
tmp = sorted[r];
sorted[r] = sorted[lo];
sorted[lo] = tmp;
}
if(g[sorted[lo]]->request.prod_value < val_left){
val_transfered = g[sorted[lo]]->request.prod_value;
} else {
val_transfered = val_left;
}
g[sorted[lo]]->request.prod_approved = val_transfered;
val_left -= val_transfered;
lo++;
}
}
free(buffer);
free(sorted);
}
void game_month_totals()
{
p_gamer_instance g;
int id;
for(id = 0; id < global.total_players; id++){
if((g = global.gamers[id]) != NULL){
if(g->request.res2prod != 0){
g->prod += g->request.res2prod;
sprintf(global.message_buffer,
"You have produced %d production\n",
g->request.res2prod);
sprintf(global.message_buffer_m,
"info p %d\n",
g->request.res2prod);
send_dirrect(id, global.message_buffer, S_HUMAN);
send_dirrect(id, global.message_buffer_m, S_MACHINE);
}
if(g->request.prod_approved != 0){
g->prod -= g->request.prod_approved;
sprintf(global.message_buffer,
"Congrutilations! You have sold %d products\n",
g->request.prod_approved);
sprintf(global.message_buffer_m,
"info s %d\n",
g->request.prod_approved);
send_dirrect(id, global.message_buffer, S_HUMAN);
send_dirrect(id, global.message_buffer_m, S_MACHINE);
}
if(g->request.res_approved != 0){
g->res += g->request.res_approved;
sprintf(global.message_buffer,
"Congrutilations! You have bought %d resources\n",
g->request.res_approved);
sprintf(global.message_buffer_m,
"info b %d\n",
g->request.res_approved);
send_dirrect(id, global.message_buffer, S_HUMAN);
send_dirrect(id, global.message_buffer_m, S_MACHINE);
}
game_player_pay(id, game_player_total_month_spent(id));
}
}
}
/*============================= requests ================================*/
void game_request_auction_bid(int id, enum auction_type type, int value, int price)
{
struct market_state_t * ms =
(struct market_state_t * )(market_state + global.current_market_state);
struct request_t * req = NULL;
int was_requester = global.gamers[id]->request.prod_value &&
global.gamers[id]->request.res_value;
int still_requester;
if(global.gamers[id] != NULL){
req = &(global.gamers[id]->request);
}
if(type == RESOURCE){
if(ms->res_price > price){
sprintf(global.message_buffer,
"You can't take such small bid. "
"The minimal price for resource is %d\n",
ms->res_price);
send_dirrect(id, global.message_buffer, S_HUMAN);
send_dirrect(id, "w\n", S_MACHINE);
} else {
req->res_value = value;
req->res_price = price;
sprintf(global.message_buffer,
"Your current bid is %d resources for $%d per one\n"
"You can change your request if you want\n",
value, price);
send_dirrect(id, global.message_buffer, S_HUMAN);
send_dirrect(id, "g\n", S_MACHINE);
}
} else { /* type == PRODUCTION */
if(ms->prod_price < price){
sprintf(global.message_buffer,
"You can't take such big bid. "
"The maximal price for production is %d\n",
ms->prod_price);
send_dirrect(id, global.message_buffer, S_HUMAN);
send_dirrect(id, "w\n", S_MACHINE);
} else
if(global.gamers[id]->prod < value){
sprintf(global.message_buffer,
"You can't sell production that you don't have. "
"Your avaible production quantity is %d.\n",
global.gamers[id]->prod);
send_dirrect(id, global.message_buffer, S_HUMAN);
send_dirrect(id, "w\n", S_MACHINE);
} else {
req->prod_value = value;
req->prod_price = price;
sprintf(global.message_buffer,
"Your current bid is %d production for $%d per one\n"
"You can change your request if you want\n",
value, price);
send_dirrect(id, global.message_buffer, S_HUMAN);
send_dirrect(id, "g\n", S_MACHINE);
}
}
still_requester = global.gamers[id]->request.prod_value &&
global.gamers[id]->request.res_value;
if(was_requester && !still_requester){
global.trade_players--;
} else
if(!was_requester && still_requester){
global.trade_players++;
}
}
void game_request_build_factory(int id, int quantity)
{
if(global.gamers[id] != NULL){
global.gamers[id]->factory[FACTORY_BUILD_TIME-1] += quantity;
sprintf(global.message_buffer,
"You requested to build %d factories. "
"It will be avaible in %d months.\n",
quantity, FACTORY_BUILD_TIME);
send_dirrect(id, global.message_buffer, S_HUMAN);
send_dirrect(id, "g\n", S_MACHINE);
game_player_pay(id, factory_price * quantity);
}
}
void game_request_convert_resource(int id, int quantity)
{
p_gamer_instance p = global.gamers[id];
if(p != NULL){
if(p->request.res2prod + quantity <= p->factory[0] && p->res >= quantity){
p->request.res2prod += quantity;
p->res -= quantity;
sprintf(global.message_buffer,
"You requested to produce %d production. "
"It will be avaible on the next month.\n",
quantity);
send_dirrect(id, global.message_buffer, S_HUMAN);
send_dirrect(id, "g\n", S_MACHINE);
game_player_pay(id, convertation_price * quantity);
} else
if(p->res < quantity){
sprintf(global.message_buffer,
"Can't produce to much production. "
"Not enough of resource (%d left)\n",
p->res);
send_dirrect(id, global.message_buffer, S_HUMAN);
send_dirrect(id, "w\n", S_MACHINE);
} else {
sprintf(global.message_buffer,
"Can't produce to much production. "
"Not enough of active non-working factories (%d left)\n",
p->factory[0] - p->request.res2prod);
send_dirrect(id, global.message_buffer, S_HUMAN);
send_dirrect(id, "w\n", S_MACHINE);
}
}
}
void game_request_finish_turn(int id)
{
if(global.gamers[id] != NULL){
if(global.gamers[id]->finished_turn == 0){
global.gamers[id]->finished_turn = 1;
global.finished_players++;
sprintf(global.message_buffer,
"The player[%d] has finished the turn\n", id);
sprintf(global.message_buffer_m,
"info turn %d\n", id);
send_broadcast(global.message_buffer, S_HUMAN);
send_broadcast(global.message_buffer_m, S_BROADCAST);
} else {
sprintf(global.message_buffer,
"Heh. You can't finish the turn twice.\n");
send_dirrect(id, global.message_buffer, S_HUMAN);
}
}
if(global.finished_players >= global.active_players){
game_state_update();
}
}
/*============================= game process ================================*/
int game_state_update()
{
int id;
if(global.active_players <= 1){
for(id = 0; id < global.total_players; id++){
if(global.gamers[id] != NULL){
send_dirrect(id, "You WIN!\n", S_HUMAN);
send_dirrect(id, "info win\n", S_MACHINE);
}
}
return -1;
}
if(global.active_players <= global.finished_players){
send_broadcast( "All the turns finished. It's time to look at the stats.\n", S_HUMAN);
send_broadcast( "info turns\n", S_BROADCAST);
game_auction_totals();
game_month_totals();
game_new_month();
}
return 0;
}
/* returns -1 in case of empty command */
int game_command(int id, char * command)
{
p_tokens tokens = split(command);
char * word;
int val = 0, price = 0;
int cnt = tokens->cnt;
switch(tokens->cnt){
case 3:
price = atoi(tokens->token->next->next->word);
case 2:
val = atoi(tokens->token->next->word);
case 1:
word = tokens->token->word;
break;
default:
send_dirrect(id, "Unknown command\n", S_HUMAN);
send_dirrect(id, "w\n", S_MACHINE);
game_request_help(id);
case 0:
destroy_tokens(tokens);
return -1;
};
/* common tread both for gamers and zombies */
if(!strcmp(word, "gcinc")){
global.gc_counter++;
sprintf(global.message_buffer,
"Global counter increased. Current value is %d\n", global.gc_counter);
send_broadcast(global.message_buffer, S_HUMAN);
} else
if(!strcmp(word, "gcdec")){
global.gc_counter--;
sprintf(global.message_buffer,
"Global counter increased. Current value is %d\n", global.gc_counter);
send_broadcast(global.message_buffer, S_HUMAN);
} else
if(command[0] == '/'){
sprintf(global.message_buffer, "Player[%d]>> %s", id, command+1);
send_broadcast(global.message_buffer, U_BROADCAST);
}else
if(!strcmp(word, "market")){
game_request_get_marketinfo(id);
} else
if(!strcmp(word, "player") && cnt == 2){
game_request_get_playerinfo(id, val);
} else
if(!strcmp(word, "stat")){
game_request_get_playerinfo(id, id);
} else
if(!strcmp(word, "help")){
game_request_help(id);
} else
if(!strcmp(word, "generalinfo")){
game_request_get_generalinfo(id);
} else
if(global.gamers[id] != NULL){ /* if user is can play */
if(!strcmp(word, "turn")){
game_request_finish_turn(id);
} else
if(!strcmp(word, "build") && cnt == 2){
game_request_build_factory(id, val);
} else
if(!strcmp(word, "prod") && cnt == 2){
game_request_convert_resource(id, val);
} else
if(!strcmp(word, "buy") && cnt == 3){
game_request_auction_bid(id, RESOURCE, val, price);
} else
if(!strcmp(word, "sell") && cnt == 3){
game_request_auction_bid(id, PRODUCTION, val, price);
} else {
send_dirrect(id, "Unknown command\n", S_HUMAN);
game_request_help(id);
}
} else { /* if user can't play */
send_dirrect(id, "Unknown command\n", S_HUMAN);
game_request_help(id);
}
//send_dirrect(id, ">> ");
destroy_tokens(tokens);
return 0;
}