-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_logic.c
998 lines (842 loc) · 33 KB
/
game_logic.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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
#define _POSIX_C_SOURCE 200201L
#include <stdio.h>
#include <curses.h>
#include <time.h>
#include "game_logic.h"
struct timespec ts = {
.tv_nsec = 0.001 * 10000000000L
};
void set_village_name(const int height, const int width, char name[]){
attron(COLOR_PAIR(1) | A_BOLD);
mvprintw(5, width / 2 - 25, "Zadaj nazov tvojej dediny (maximalne 25 znakov)");
attroff(COLOR_PAIR(1) | A_BOLD);
refresh();
int input = 0;
int idx = 0;
int last_letter_x = width / 2 - 13;
nodelay(stdscr, FALSE);
while(input != '\n' || idx <= 0){
input = getch();
if( input == KEY_DC || input == 127 ){
if(idx > 0){
idx--;
last_letter_x--;
name[idx] = 0;
mvprintw(8, last_letter_x, " ");
mvprintw(10, width / 2 - 9, " ");
refresh();
}
}
else if(input != '\n' && idx < 25){
mvprintw(12, width / 2 - 9, " ");
name[idx] = input;
attron(A_BOLD);
mvprintw(8, last_letter_x, "%c", input);
attroff(A_BOLD);
refresh();
last_letter_x++;
idx++;
}
else if(input != '\n' && idx >= 25){
attron(COLOR_PAIR(2) | A_BOLD);
mvprintw(10, width / 2 - 9 ,"LIMIT DOSIAHNUTY!");
attroff(COLOR_PAIR(2) | A_BOLD);
refresh();
}
else if( input == '\n' && idx == 0 ){
attron(COLOR_PAIR(2) | A_BOLD);
mvprintw(12, width / 2 - 9, "MUSIS ZADAT NAZOV!");
attroff(COLOR_PAIR(2) | A_BOLD);
refresh();
}
}
name[idx] = '\0';
}
int save_game(char save_file_name[], char village_name[], int gold_count, int wood_count, int army_count, int days, int bank_level, int lumberjack_level, int barracks_level, int trees_count, int army_statues_count, int bank_statues_count, int has_hospital, int guard_towers_count, int bank_buff_days_left, int lumber_buff_days_left, int barracks_buff_days_left){
FILE *save_file;
if((save_file = fopen(save_file_name, "w")) == NULL){
return 1;
}
fprintf(save_file, "Name: %s\n", village_name);
fprintf(save_file, "Gold: %d\n", gold_count);
fprintf(save_file, "Wood: %d\n", wood_count);
fprintf(save_file, "Army: %d\n", army_count );
fprintf(save_file, "Day: %d\n", days);
fprintf(save_file, "Bank_level: %d\n", bank_level );
fprintf(save_file, "Lumberjack_level: %d\n", lumberjack_level);
fprintf(save_file, "Barracks_level: %d\n", barracks_level );
fprintf(save_file, "Trees_count: %d\n", trees_count );
fprintf(save_file, "Army_statues_count: %d\n", army_statues_count);
fprintf(save_file, "Bank_statues_count: %d\n", bank_statues_count);
fprintf(save_file, "Has_hospital: %d\n", has_hospital);
fprintf(save_file, "Guard_towers_count: %d\n", guard_towers_count);
fprintf(save_file, "Bank_buff_days_left: %d\n", bank_buff_days_left);
fprintf(save_file, "Lumberjack_buff_days_left: %d\n", lumber_buff_days_left);
fprintf(save_file, "Barracks_buff_days_left: %d\n", barracks_buff_days_left);
fclose(save_file);
return 0;
}
int set_raid_strength(int day_count){
int base_strength = 50;
if(day_count <= 5){
return base_strength;
}
if(day_count < 100){
return (base_strength * day_count) / 2;
}
if(day_count < 500){
return base_strength * (day_count / 2);
}
else
return base_strength * day_count;
}
int raid_result(int raid_strength, int *gold_count, int *wood_count, int *army_count, int has_hospital, int guard_towers_count){
if(guard_towers_count >= 1){
int guard_tower_nerf = guard_towers_count * 5;
switch(guard_tower_nerf){
case 5:
raid_strength -= ceil((raid_strength / 20));
break;
case 10:
raid_strength -= (raid_strength / 10);
break;
case 15:
raid_strength -= ceil((raid_strength / 6.6));
break;
case 20:
raid_strength -= (raid_strength / 5);
break;
}
}
if(raid_strength <= *army_count){
if(has_hospital == 1){ // loose only 60% of raid strength
int part_of_strength = (raid_strength / 5) * 3 ;
*army_count -= part_of_strength;
}
else
*army_count -= raid_strength;
return 1;
}
else{
if(has_hospital == 1){ // loose only 80% of army
int part_of_army = *army_count / 5;
*army_count = part_of_army;
}
else{
*army_count = 0;
}
*gold_count -= (*gold_count / 100) * 20; // loose 20% of gold
*wood_count -= (*wood_count / 100) * 15; // loose 15% of wood
return 0;
}
}
void main_game(int height, int width, char village_name[], int wood, int gold, int army, int days, struct Building *bank, struct Building *lumberjack, struct Building *barracks, struct Building *hospital, struct Accessories *trees, struct Accessories *bank_statues, struct Accessories *army_statues, struct Accessories *guard_towers, struct Buff *buff_bank, struct Buff *buff_lumberjack, struct Buff *buff_barracks, WINDOW *main_win, char save_file_name[], int texture_pack){
int gold_count = gold; //track gold resource count
int wood_count = wood; //track wood resource count
int army_count = army; //track army resource count
int shop_open = 0; //track whether shop is open
int shop_created = 0;
int day_count = days;
int secCount = 0; //how many time did 10 second passed
int acc_shop_open = 0;
int acc_shop_created = 0;
int trader_shop_open = 0;
int trader_shop_created = 0;
int input = 0;
int raid_strength = 0, raid_happend = 0, raid_res = 0;
clock_t start_t, mid_t, diff_t;
nodelay(stdscr, TRUE);
struct Trade trade;
bool trade_generated = false;
clear();
// box(main_win, 0,0); //add white border
mvwprintw(main_win, COLS - 10, 1, "EXIT");
refresh(); // refresh curses app
wrefresh(main_win); //refresh main window only
WINDOW *shop_win = newwin(0, 0, LINES - 10, 1);
shop_created = 1;
WINDOW *acc_shop_win = newwin(0, 0, LINES - 10, 1);
acc_shop_created = 1;
WINDOW *trader_shop_win = newwin(0, 0, LINES - 10, 1);
trader_shop_created = 1;
print_main_win(gold_count, wood_count, army_count, day_count, village_name, buff_bank, buff_lumberjack, buff_barracks, guard_towers->count, main_win, acc_shop_open, shop_open, trader_shop_open, texture_pack, bank, lumberjack, barracks, hospital, trees);
start_t = clock();
//variable for mouse events
MEVENT event;
mouseinterval(0);
mousemask(ALL_MOUSE_EVENTS, NULL);
keypad(main_win, TRUE);
while(1){
mid_t = clock(); //get preocessor clock
input = getch();
diff_t = (double)(mid_t - start_t) / CLOCKS_PER_SEC; //get difference between cycles
if(shop_open == 0 && shop_created == 0){
shop_win = newwin(0, 0, LINES - 10, 1);
shop_created = 1;
}
if(acc_shop_open == 0 && acc_shop_created == 0){
acc_shop_win = newwin(0, 0, LINES - 10, 1);
acc_shop_created = 1;
}
if(trader_shop_open == 0 && trader_shop_created == 0){
trader_shop_win = newwin(0, 0, LINES - 10, 1);
trader_shop_created = 1;
}
if(diff_t == 5){
wclear(main_win);
secCount++;
start_t = mid_t;
int buffed_gold = 0;
int buffed_wood = 0;
int buffed_army = 0;
if(buff_bank->days_left > 0)
buffed_gold = 15;
if(buff_lumberjack->days_left > 0)
buffed_wood = 10;
if(buff_barracks->days_left > 0)
buffed_army = 10;
if(bank->level == 1){
if(bank_statues->count > 0){
int buffed_income = bank_statues->buff * 0.1;
gold_count += (10 + buffed_income) + buffed_gold;
}
else
gold_count += 10 + buffed_gold;
}
else if(bank->level == 2){
if(bank_statues->count > 0){
int buffed_income = bank_statues->buff * 0.3;
gold_count += (30 + buffed_income) + (buffed_gold * 2);
}
else
gold_count += 30 + (buffed_gold * 2);
}
else{
if(bank_statues->count > 0){
int buffed_income = bank_statues->buff * 0.5;
gold_count += (30 + buffed_income) + (buffed_gold * 3);
}
else
gold_count += 50 + (buffed_gold * 3);
}
if(lumberjack->level == 1){
if(trees->count > 0){
int buffed_income = trees->buff * 0.1;
wood_count += (10 + buffed_income) + buffed_wood;
}
else
wood_count += 10 + buffed_wood;
}
else if(lumberjack->level == 2){
if(trees->count > 0){
int buffed_income = trees->buff * 0.3;
wood_count += (30 + buffed_income) + (buffed_wood * 2);
}
else
wood_count += 30 + (buffed_wood * 2);
}
else{
if(trees->count > 0){
int buffed_income = trees->buff * 0.8;
wood_count += (80 + buffed_income) + (buffed_wood * 3);
}
else
wood_count += 80 + (buffed_wood * 3);
}
if(barracks->level == 1){
if(army_statues->count > 0){
int buffed_income = army_statues->buff * 0.1;
army_count += (10 + buffed_income) + buffed_army;
}
else
army_count += 10 + buffed_army;
}
else if(barracks->level == 2){
if(army_statues->count > 0){
int buffed_income = army_statues->buff * 0.2;
army_count += (20 + buffed_income) + (buffed_army * 2);
}
else
army_count += 20 + (buffed_army * 2);
}
else if(barracks->level == 3){ // needed to add else if bcs barracks begin on level 0
if(army_statues->count > 0){
int buffed_income = army_statues->buff * 0.5;
army_count += (50 + buffed_income) + (buffed_army * 3);
}
else
army_count += 50 + (buffed_army * 3);
}
/*print_resources(gold_count, wood_count, army_count, day_count, village_name, main_win);
print_icon_main_window(main_win);
print_acc_shop_icon(main_win);*/
print_main_win(gold_count, wood_count, army_count, day_count, village_name, buff_bank, buff_lumberjack, buff_barracks, guard_towers->count, main_win, acc_shop_open, shop_open, trader_shop_open, texture_pack, bank, lumberjack, barracks, hospital, trees);
if(shop_open == 1)
print_shop_window(bank, lumberjack, barracks, shop_win, width, hospital, 0, texture_pack);
if(acc_shop_open == 1)
print_accessory_shop(trees, bank_statues, army_statues, guard_towers,acc_shop_win, width, 0, texture_pack);
if(trader_shop_open == 1)
print_trader_shop(trader_shop_win, buff_bank, buff_lumberjack, buff_barracks, width, 0, &trade, texture_pack);
if(secCount == 5){
secCount = 0;
day_count++;
if(raid_happend == 1)
raid_happend = 0;
if(buff_bank->days_left > 0)
buff_bank->days_left -= 1;
if(buff_lumberjack->days_left > 0)
buff_lumberjack->days_left -= 1;
if(buff_barracks->days_left > 0)
buff_barracks->days_left -= 1;
if(trade_generated)
trade_generated = false;
}
}
if((day_count + 2) % 5 == 0){ // show that raid is comming day before
raid_strength = set_raid_strength(day_count);
wattron(main_win, COLOR_PAIR(2) | A_BOLD);
mvwprintw(main_win, 1, COLS - 40 ,"Raid is coming! Stength of raid: %d", raid_strength);
wattroff(main_win, COLOR_PAIR(2) | A_BOLD);
if(!trade_generated){
create_trade(&trade, day_count);
trade_generated = true;
}
}
if((day_count + 1) % 5 == 0){ // day of the raid
if(raid_happend == 0){
raid_happend = 1;
raid_res = raid_result(raid_strength, &gold_count, &wood_count, &army_count, hospital->level, guard_towers->count);
print_main_win(gold_count, wood_count, army_count, day_count, village_name, buff_bank, buff_lumberjack, buff_barracks, guard_towers->count, main_win, acc_shop_open, shop_open, trader_shop_open, texture_pack, bank, lumberjack, barracks, hospital, trees);
}
if(raid_res == 1){
wattron(main_win, COLOR_PAIR(4) | A_BOLD);
mvwprintw(main_win, 1, COLS - 40, "You won! With some casualties");
wattroff(main_win, COLOR_PAIR(4) | A_BOLD);
}
else{
wattron(main_win, COLOR_PAIR(2) | A_BOLD);
mvwprintw(main_win, 1, COLS - 75, "You lost! They took 20%% of gold, 15%% of wood and they killed all soldiers");
wattroff(main_win, COLOR_PAIR(2) | A_BOLD);
}
// print_main_win(gold_count, wood_count, army_count, day_count, village_name, main_win, trees);
}
wrefresh(main_win);
if(input == KEY_MOUSE){
if(getmouse(&event) == OK){
if( event.bstate == BUTTON1_PRESSED){
// click on shop icon
if(event.y <= 11 && event.y >= 7 && event.x >= 1 && event.x <= 9 && shop_open == 0 && acc_shop_open == 0 && trader_shop_open == 0){
open_shop_window(shop_win, width);
//Print shop window
print_shop_window(bank, lumberjack, barracks, shop_win, width, hospital, 0, texture_pack);
//window will be still open after refresh that happens every 5 seconds
shop_open = 1;
}
//close shop window
if(event.y >= LINES - 10 && event.y <= LINES - 9 && event.x >= width - 3 && event.x <= width - 1 && (shop_open == 1 || acc_shop_open == 1 || trader_shop_open == 1))
{
mvwprintw(main_win, height / 2 + 6, width / 2, "CLOSE");
if(shop_open){
close_window(shop_win);
shop_open = 0;
shop_created = 0;
}
else if(acc_shop_open){
close_window(acc_shop_win);
acc_shop_open = 0;
acc_shop_created = 0;
}
else if(trader_shop_open){
close_window(trader_shop_win);
trader_shop_open = 0;
trader_shop_created = 0;
}
print_main_win(gold_count, wood_count, army_count, day_count, village_name, buff_bank, buff_lumberjack, buff_barracks, guard_towers->count, main_win, acc_shop_open, shop_open, trader_shop_open, texture_pack, bank, lumberjack, barracks, hospital, trees);
}
//buy upgrades on first shop position
if((shop_open || acc_shop_open || trader_shop_open) && event.x >= 5 && event.x <= 13 && event.y == 35){
if(shop_open){
int upg = upgrade_building(bank, shop_win, &gold_count, &wood_count, &army_count);
if(upg == 1){
print_main_win(gold_count, wood_count, army_count, day_count, village_name, buff_bank, buff_lumberjack, buff_barracks, guard_towers->count, main_win, acc_shop_open, shop_open, trader_shop_open, texture_pack, bank, lumberjack, barracks, hospital, trees);
print_shop_window(bank, lumberjack, barracks, shop_win, width,hospital, 1, texture_pack);
}
}
else if(acc_shop_open){
int upg = buy_acc(trees, acc_shop_win, &gold_count, &wood_count, &army_count);
if(upg == 1){
print_main_win(gold_count, wood_count, army_count, day_count, village_name, buff_bank, buff_lumberjack, buff_barracks, guard_towers->count,main_win, acc_shop_open, shop_open, trader_shop_open, texture_pack, bank, lumberjack, barracks, hospital, trees);
print_accessory_shop(trees, bank_statues, army_statues, guard_towers, acc_shop_win, width, 1, texture_pack);
}
}
}
//buy upgrade on second position in shop
if((shop_open || acc_shop_open || trader_shop_open) && event.x >= 24 && event.x <= 32 && event.y == 35){
if(shop_open){
int upg = upgrade_building(lumberjack, shop_win, &gold_count, &wood_count, &army_count);
if(upg == 1){
print_main_win(gold_count, wood_count, army_count, day_count, village_name, buff_bank, buff_lumberjack, buff_barracks, guard_towers->count, main_win, acc_shop_open, shop_open, trader_shop_open, texture_pack, bank, lumberjack, barracks, hospital, trees);
print_shop_window(bank, lumberjack, barracks, shop_win, width,hospital, 1, texture_pack);
}
}
else if(acc_shop_open){
int upg = buy_acc(bank_statues, acc_shop_win, &gold_count, &wood_count, &army_count);
if(upg == 1){
print_main_win(gold_count, wood_count, army_count, day_count, village_name, buff_bank, buff_lumberjack, buff_barracks, guard_towers->count, main_win, acc_shop_open, shop_open, trader_shop_open, texture_pack, bank, lumberjack, barracks, hospital, trees);
print_accessory_shop(trees, bank_statues, army_statues, guard_towers, acc_shop_win, width, 1, texture_pack);
}
}
}
//buy upgrade on third position in shop
if((shop_open || acc_shop_open || trader_shop_open) && event.x >= 44 && event.x <= 52 && event.y == 35){
if(shop_open){
int upg = upgrade_building(barracks, shop_win, &gold_count, &wood_count, &army_count);
if(upg == 1){
print_main_win(gold_count, wood_count, army_count, day_count, village_name, buff_bank, buff_lumberjack, buff_barracks, guard_towers->count, main_win, acc_shop_open, shop_open, trader_shop_open, texture_pack, bank, lumberjack, barracks, hospital, trees);
print_shop_window(bank, lumberjack, barracks, shop_win, width,hospital, 1, texture_pack);
}
}
else if(acc_shop_open){
int upg = buy_acc(army_statues, acc_shop_win, &gold_count, &wood_count, &army_count);
if(upg == 1){
print_main_win(gold_count, wood_count, army_count, day_count, village_name, buff_bank, buff_lumberjack, buff_barracks, guard_towers->count, main_win, acc_shop_open, shop_open, trader_shop_open, texture_pack, bank, lumberjack, barracks, hospital, trees);
print_accessory_shop(trees, bank_statues, army_statues, guard_towers, acc_shop_win, width, 1, texture_pack);
}
}
}
//buy hospital
if((shop_open) && event.y == 35 && event.x >= 66 && event.x <= 68){
if(shop_open){
int upg = upgrade_building(hospital, shop_win, &gold_count, &wood_count, &army_count);
if(upg == 1){
print_main_win(gold_count, wood_count, army_count, day_count, village_name, buff_bank, buff_lumberjack, buff_barracks, guard_towers->count, main_win, acc_shop_open, shop_open, trader_shop_open, texture_pack, bank, lumberjack, barracks, hospital, trees);
print_shop_window(bank, lumberjack, barracks, shop_win, width,hospital, 1, texture_pack);
}
}
}
if(acc_shop_open && event.y == 35 && event.x >= 66 && event.x <= 73){
int upg = buy_acc(guard_towers, acc_shop_win, &gold_count, &wood_count, &army_count);
if(upg == 1){
print_main_win(gold_count, wood_count, army_count, day_count, village_name, buff_bank, buff_lumberjack, buff_barracks, guard_towers->count, main_win, acc_shop_open, shop_open, trader_shop_open, texture_pack, bank, lumberjack, barracks, hospital, trees);
print_accessory_shop(trees, bank_statues, army_statues, guard_towers, acc_shop_win, width, 1, texture_pack);
}
}
//open acc shop window
if(event.x >= 2 && event.x <= 8 && event.y >= 13 && event.y <= 18 && acc_shop_open == 0 && shop_open == 0 && trader_shop_open == 0)
{
open_shop_window(acc_shop_win, width);
print_accessory_shop(trees, bank_statues, army_statues, guard_towers, acc_shop_win, width, 0, texture_pack);
acc_shop_open = 1;
}
//open trader shop window
if(event.x >= 1 && event.x <= 10 && event.y <= 25 && event.y >= 20 && trader_shop_open == 0 && (day_count + 2) % 5 == 0 && acc_shop_open == 0 && shop_open == 0)
{
open_shop_window(trader_shop_win, width);
print_trader_shop(trader_shop_win, buff_bank, buff_lumberjack, buff_barracks, width, 0, &trade, texture_pack);
trader_shop_open = 1;
}
//buy bank buff
if(trader_shop_open == 1 && event.y == 35 && event.x >= 8 && event.x <= 10){
int upg = buy_buff(buff_bank, trader_shop_win, &gold_count, &wood_count, &army_count);
if(upg == 1){
print_main_win(gold_count, wood_count, army_count, day_count, village_name, buff_bank, buff_lumberjack, buff_barracks, guard_towers->count, main_win, acc_shop_open, shop_open, trader_shop_open, texture_pack, bank, lumberjack, barracks, hospital, trees);
print_trader_shop(trader_shop_win, buff_bank, buff_lumberjack, buff_barracks, width, 1, &trade, texture_pack);
}
}
//buy lumberjack buff
if(trader_shop_open == 1 && event.y == 35 && event.x >= 27 && event.x <= 29){
int upg = buy_buff(buff_lumberjack, trader_shop_win, &gold_count, &wood_count, &army_count);
if(upg == 1){
print_main_win(gold_count, wood_count, army_count, day_count, village_name, buff_bank, buff_lumberjack, buff_barracks, guard_towers->count, main_win, acc_shop_open, shop_open, trader_shop_open, texture_pack, bank, lumberjack, barracks, hospital, trees);
print_trader_shop(trader_shop_win, buff_bank, buff_lumberjack, buff_barracks, width, 1, &trade, texture_pack);
}
}
//buy barracks buff
if(trader_shop_open == 1 && event.y == 35 && event.x >= 49 && event.x <= 51){
int upg = buy_buff(buff_barracks, trader_shop_win, &gold_count, &wood_count, &army_count);
if(upg == 1){
print_main_win(gold_count, wood_count, army_count, day_count, village_name, buff_bank, buff_lumberjack, buff_barracks, guard_towers->count, main_win, acc_shop_open, shop_open, trader_shop_open, texture_pack, bank, lumberjack, barracks, hospital, trees);
print_trader_shop(trader_shop_win, buff_bank, buff_lumberjack, buff_barracks, width, 1, &trade, texture_pack);
}
}
if(trader_shop_open == 1 && !trade.traded && event.y == 35 && event.x >= 70 && event.x <= 74){
int trade_success = make_trade(trader_shop_win, &trade, &gold_count, &wood_count, &army_count);
if(trade_success == 1){
print_main_win(gold_count, wood_count, army_count, day_count, village_name, buff_bank, buff_lumberjack, buff_barracks, guard_towers->count, main_win, acc_shop_open, shop_open, trader_shop_open, texture_pack, bank, lumberjack, barracks, hospital, trees);
print_trader_shop(trader_shop_win, buff_bank, buff_lumberjack, buff_barracks, width, 1, &trade, texture_pack);
}
}
wrefresh(main_win);
}
}
}
if(input == '\n'){ //temporary EXIT GAME a TUNRS OFF
int save = save_game(save_file_name, village_name, gold_count, wood_count, army_count, day_count, bank->level, lumberjack->level, barracks->level, trees->count, army_statues->count, bank_statues->count, hospital->level, guard_towers->count, buff_bank->days_left, buff_lumberjack->days_left, buff_barracks->days_left);
if(save == 1){
endwin();
printf("COULDNT SAVE YOUR GAME\n");
exit(1);
}
close_window(main_win);
break;
}
}
}
int generate_in_range(int max, int min){
return (rand() % (max - min + 1)) + min;
}
void create_trade(struct Trade *trade, int days_count){
int random_trade_num = generate_in_range(3, 1);
int base_gold_price = 500;
int base_wood_price = 250;
int base_army_price = 250;
if(random_trade_num == 1){
trade->trade_for_gold = true;
trade->trade_for_wood = false;
trade->trade_for_army = false;
//set prices
trade->wood_price = base_wood_price * (generate_in_range(days_count, 1));
trade->army_price = base_army_price * (generate_in_range(days_count, 1));
trade->gold_price = 0;
trade->gold_trade = ceil((trade->wood_price + trade->army_price) / 2);
}
else if(random_trade_num == 2){
trade->trade_for_gold = false;
trade->trade_for_wood = true;
trade->trade_for_army = false;
//set prices
trade->gold_price = base_gold_price * (generate_in_range(days_count, 1));
trade->army_price = base_army_price * (generate_in_range(days_count, 1));
trade->wood_price = 0;
trade->wood_trade = ceil((trade->gold_price + trade->army_price) / 2);
}
else{
trade->trade_for_gold = false;
trade->trade_for_wood = false;
trade->trade_for_army = true;
//set prices
trade->gold_price = base_gold_price * (generate_in_range(days_count, 1));
trade->wood_price = base_wood_price * (generate_in_range(days_count, 1));
trade->army_price = 0;
trade->army_trade = ceil((trade->gold_price + trade->wood_price) / 2);
}
trade->traded = false;
}
void open_shop_window(WINDOW *win, int width){
wattron(win, A_BOLD);
int width_step = width / 25;
for(int i = 1; i < 26; i++){
wclear(win);
wrefresh(win);
wresize(win, 10, width_step * i);
// box(win, 0, 0);
wborder(win, 0, 0, 0, 0, 0, 0, 0, 0);
if(i > 5){
mvwprintw(win, 1, (width_step * i) - 2, "X");
}
wrefresh(win);
nanosleep(&ts, NULL);
}
wattroff(win, A_BOLD);
}
int buy_acc(struct Accessories *accessory, WINDOW *acc_shop_win, int *gold_count, int *wood_count, int *army_count){
//successful upgrade
if(accessory->count < accessory->max_count && accessory->next_gold_price <= *gold_count && accessory->next_wood_price <= *wood_count && accessory->next_army_price <= *army_count)
{
wattron(acc_shop_win, A_BOLD);
mvwprintw(acc_shop_win, 5, COLS - 20, "%s bought!", accessory->singular_name);
accessory->count += 1;
wrefresh(acc_shop_win);
*gold_count -= accessory->next_gold_price;
*wood_count -= accessory->next_wood_price;
*army_count -= accessory->next_army_price;
//update prices base on accessory name
if(strcmp(accessory->singular_name, "Tree") == 0){
accessory->buff += 10;
accessory->next_gold_price += 150;
accessory->next_wood_price += 100;
accessory->next_army_price += 25;
}
else if(strcmp(accessory->singular_name, "Bank statue") == 0){
accessory->buff += 10;
accessory->next_gold_price += 2000;
accessory->next_wood_price += 500;
accessory->next_army_price += 300;
}
else{
accessory->buff += 10;
accessory->next_gold_price += 1500;
accessory->next_wood_price += 700;
accessory->next_army_price += 500;
}
return 1; //successful upgrade
}
else{
if(accessory->count < accessory->max_count){
wattron(acc_shop_win, COLOR_PAIR(2) | A_BOLD);
int not_enough_gold = 0, not_enough_wood = 0, not_enough_army = 0, count = 0;
if(accessory->next_gold_price > *gold_count){
not_enough_gold = 1;
count++;
}
if(accessory->next_wood_price > *wood_count){
not_enough_wood = 1;
count++;
}
if(accessory->next_army_price > *army_count){
not_enough_army = 1;
count++;
}
int main_text_x = COLS - 42;
int secondary_text_x = main_text_x + 22;
mvwprintw(acc_shop_win, 5, main_text_x, "Not enough resources: ");
if(count == 3){
mvwprintw(acc_shop_win, 5, secondary_text_x, "gold, wood, army");
}
else if(count == 2){
if(not_enough_gold == 1 && not_enough_wood == 1){
mvwprintw(acc_shop_win, 5, secondary_text_x, "gold, wood ");
}
else if(not_enough_gold == 1 && not_enough_army == 1){
mvwprintw(acc_shop_win, 5, secondary_text_x, "gold, army ");
}
else if(not_enough_wood == 1 && not_enough_army == 1){
mvwprintw(acc_shop_win, 5, secondary_text_x, "wood, army ");
}
}
else{
if(not_enough_gold == 1){
mvwprintw(acc_shop_win, 5, secondary_text_x, "gold ");
}
else if(not_enough_wood == 1){
mvwprintw(acc_shop_win, 5, secondary_text_x, "wood ");
}
else{
mvwprintw(acc_shop_win, 5, secondary_text_x, "army ");
}
}
wattroff(acc_shop_win, COLOR_PAIR(2) | A_BOLD);
wrefresh(acc_shop_win);
return 0;
}
else{
return -1;
}
}
}
// return 0 or 1 based on whether upgrade was succsefull
int upgrade_building(struct Building *building, WINDOW *shop_win, int* gold_count, int* wood_count, int* army_count){
//successful upgrade
if((building->upgrade_army_price == 0 || building->upgrade_army_price <= *army_count) &&
building->upgrade_gold_price <= *gold_count && building->upgrade_wood_price <= *wood_count && building->level < 3)
{
wattron(shop_win, A_BOLD);
mvwprintw(shop_win, 5, COLS - 20, "%s upgraded!", building->name);
building->level += 1;
// substract price from resource
*gold_count -= building->upgrade_gold_price;
*wood_count -= building->upgrade_wood_price;
*army_count -= building->upgrade_army_price;
//update upgrade prices acording to new level
if(building->level == 1){ // only barracks can be upgraded to level 1 other buildings starts at level 1
building->upgrade_gold_price = 2500;
building->upgrade_wood_price = 1000;
}
else if(building->level == 2){ // upgrade price to level 3
if(strcmp(building->name, "Bank") == 0){
building->upgrade_gold_price = 7500;
building->upgrade_wood_price = 2500;
}
else if(strcmp(building->name, "Lumberjack") == 0){
building->upgrade_gold_price = 3000;
building->upgrade_wood_price = 1500;
building->upgrade_army_price = 600;
}
else{ // barracks
building->upgrade_gold_price = 5000;
building->upgrade_wood_price = 2500;
}
}
return 1;
}
// not enough resources
else{
wattron(shop_win, COLOR_PAIR(2) | A_BOLD);
int not_enough_gold = 0, not_enough_wood = 0, not_enough_army = 0, count = 0;
if(building->upgrade_gold_price > *gold_count){
not_enough_gold = 1;
count++;
}
if(building->upgrade_wood_price > *wood_count){
not_enough_wood = 1;
count++;
}
if(building->upgrade_army_price != 0 && building->upgrade_army_price > *army_count){
not_enough_army = 1;
count++;
}
int main_text_x = COLS - 42;
int secondary_text_x = main_text_x + 22;
mvwprintw(shop_win, 5, main_text_x, "Not enough resources: ");
if(count == 3){
mvwprintw(shop_win, 5, secondary_text_x, "gold, wood, army");
}
else if(count == 2){
if(not_enough_gold == 1 && not_enough_wood == 1){
mvwprintw(shop_win, 5, secondary_text_x, "gold, wood ");
}
else if(not_enough_gold == 1 && not_enough_army == 1){
mvwprintw(shop_win, 5, secondary_text_x, "gold, army ");
}
else if(not_enough_wood == 1 && not_enough_army == 1){
mvwprintw(shop_win, 5, secondary_text_x, "wood, army ");
}
}
else{
if(not_enough_gold == 1){
mvwprintw(shop_win, 5, secondary_text_x, "gold ");
}
else if(not_enough_wood == 1){
mvwprintw(shop_win, 5, secondary_text_x, "wood ");
}
else{
mvwprintw(shop_win, 5, secondary_text_x, "army ");
}
}
wattroff(shop_win, COLOR_PAIR(2) | A_BOLD);
wrefresh(shop_win);
return 0;
}
}
int buy_buff(struct Buff *buff, WINDOW *shop_win, int *gold_count, int *wood_count, int *army_count){
if(buff->gold_price <= *gold_count && buff->wood_price <= *wood_count && buff->army_price <= *army_count){
buff->days_left = 3;
*gold_count -= buff->gold_price;
*wood_count -= buff->wood_price;
*army_count -= buff->army_price;
return 1;
}
else{
wattron(shop_win, COLOR_PAIR(2) | A_BOLD);
int not_enough_gold = 0, not_enough_wood = 0, not_enough_army = 0, count = 0;
if(buff->gold_price > *gold_count){
not_enough_gold = 1;
count++;
}
if(buff->wood_price > *wood_count){
not_enough_wood = 1;
count++;
}
if(buff->army_price > *army_count){
not_enough_army = 1;
count++;
}
int main_text_x = COLS - 42;
int secondary_text_x = main_text_x + 22;
mvwprintw(shop_win, 5, main_text_x, "Not enough resources: ");
if(count == 3){
mvwprintw(shop_win, 5, secondary_text_x, "gold, wood, army");
}
else if(count == 2){
if(not_enough_gold == 1 && not_enough_wood == 1){
mvwprintw(shop_win, 5, secondary_text_x, "gold, wood ");
}
else if(not_enough_gold == 1 && not_enough_army == 1){
mvwprintw(shop_win, 5, secondary_text_x, "gold, army ");
}
else if(not_enough_wood == 1 && not_enough_army == 1){
mvwprintw(shop_win, 5, secondary_text_x, "wood, army ");
}
}
else{
if(not_enough_gold == 1){
mvwprintw(shop_win, 5, secondary_text_x, "gold ");
}
else if(not_enough_wood == 1){
mvwprintw(shop_win, 5, secondary_text_x, "wood ");
}
else{
mvwprintw(shop_win, 5, secondary_text_x, "army ");
}
}
wattroff(shop_win, COLOR_PAIR(2) | A_BOLD);
wrefresh(shop_win);
return 0;
}
}
int make_trade(WINDOW *shop_win, struct Trade *trade, int *gold_count, int *wood_count, int *army_count){
if(trade->trade_for_gold && trade->wood_price <= *wood_count && trade->army_price <= *army_count){
*wood_count -= trade->wood_price;
*army_count -= trade->army_price;
*gold_count += trade->gold_trade;
trade->traded = true;
return 1;
}
else if(trade->trade_for_wood && trade->gold_price <= *gold_count && trade->army_price <= *army_count){
*gold_count -= trade->gold_price;
*army_count -= trade->army_price;
*wood_count += trade->wood_trade;
trade->traded = true;
return 1;
}
else if(trade->trade_for_army && trade->gold_price <= *gold_count && trade->wood_price <= *wood_count){
*gold_count -= trade->gold_price;
*wood_count -= trade->wood_price;
*army_count += trade->army_trade;
trade->traded = true;
return 1;
}
else{
wattron(shop_win, COLOR_PAIR(2) | A_BOLD);
int not_enough_gold = 0, not_enough_wood = 0, not_enough_army = 0, count = 0;
if(trade->gold_price > *gold_count){
not_enough_gold = 1;
count++;
}
if(trade->wood_price > *wood_count){
not_enough_wood = 1;
count++;
}
if(trade->army_price > *army_count){
not_enough_army = 1;
count++;
}
int main_text_x = COLS - 42;
int secondary_text_x = main_text_x + 22;
mvwprintw(shop_win, 5, main_text_x, "Not enough resources: ");
if(count == 3){
mvwprintw(shop_win, 5, secondary_text_x, "gold, wood, army");
}
else if(count == 2){
if(not_enough_gold == 1 && not_enough_wood == 1){
mvwprintw(shop_win, 5, secondary_text_x, "gold, wood ");
}
else if(not_enough_gold == 1 && not_enough_army == 1){
mvwprintw(shop_win, 5, secondary_text_x, "gold, army ");
}
else if(not_enough_wood == 1 && not_enough_army == 1){
mvwprintw(shop_win, 5, secondary_text_x, "wood, army ");
}
}
else{
if(not_enough_gold == 1){
mvwprintw(shop_win, 5, secondary_text_x, "gold ");
}
else if(not_enough_wood == 1){
mvwprintw(shop_win, 5, secondary_text_x, "wood ");
}
else{
mvwprintw(shop_win, 5, secondary_text_x, "army ");
}
}
wattroff(shop_win, COLOR_PAIR(2) | A_BOLD);
wrefresh(shop_win);
return 0;
}
}
void close_window(WINDOW *win){
//wbkgd(win, COLOR_PAIR(1));
wclear(win);
wrefresh(win);
delwin(win);
}