diff options
| author | Franklin Wei <franklin@fwei.tk> | 2014-06-26 09:55:46 -0400 |
|---|---|---|
| committer | Franklin Wei <franklin@fwei.tk> | 2014-06-26 09:55:46 -0400 |
| commit | ad3259df663a3e0504b26a97d8c00a9454948850 (patch) | |
| tree | f568c0e1fc4fe0d76161cafffd906b2f7859516e | |
| parent | ca5288e1dc13f5e765f4f25c5c8082c9511c29c0 (diff) | |
| download | wargames-server-ad3259df663a3e0504b26a97d8c00a9454948850.zip wargames-server-ad3259df663a3e0504b26a97d8c00a9454948850.tar.gz wargames-server-ad3259df663a3e0504b26a97d8c00a9454948850.tar.bz2 wargames-server-ad3259df663a3e0504b26a97d8c00a9454948850.tar.xz | |
Made GTNW a turn-based game
| -rw-r--r-- | games.c | 205 |
1 files changed, 181 insertions, 24 deletions
@@ -5,6 +5,8 @@ #include <location.h> #include <unistd.h> #include <string.h> +static bool surrender=false; +static int winner=0; /* on surrender */ static unsigned int min(unsigned int a, unsigned int b) { return a<b?a:b; @@ -38,6 +40,19 @@ static void fire_missile(struct location_t* city) map[y][x]='O'; } } +static void calc_pops(unsigned int* us_pop, unsigned int* ussr_pop) +{ + *us_pop=0; + *ussr_pop=0; + /* calculate populations */ + for(int i=0;i<sizeof(world)/sizeof(struct location_t);++i) + { + if(world[i].owner==USSR) + *ussr_pop+=world[i].population; + else + *us_pop+=world[i].population; + } +} static void print_map_with_pops(void) { for(int i=0;i<sizeof(map)/sizeof(char*);++i) @@ -46,14 +61,7 @@ static void print_map_with_pops(void) print_string("\n"); } unsigned int us_pop=0, ussr_pop=0; - /* calculate populations */ - for(int i=0;i<sizeof(world)/sizeof(struct location_t);++i) - { - if(world[i].owner==USSR) - ussr_pop+=world[i].population; - else - us_pop+=world[i].population; - } + calc_pops(&us_pop, &ussr_pop); /* now sort into US and USSR cities */ struct location_t us_cities[sizeof(world)/sizeof(struct location_t)+1], ussr_cities[sizeof(world)/sizeof(struct location_t)+1]; int us_back=0, ussr_back=0; @@ -116,35 +124,92 @@ static void print_map_with_pops(void) print_string("\n"); } } -void global_thermonuclear_war(void) +static void do_first_strike(int side) { - clear(); - for(int i=0;i<sizeof(map)/sizeof(const char*);++i) + attr_on(WA_UNDERLINE, 0); + print_string("AWAITING FIRST STRIKE COMMAND"); + attr_off(WA_UNDERLINE, 0); + print_string("\n\n\nPLEASE LIST PRIMARY TARGETS BY\nCITY AND/OR COUNTY NAME:\n\n"); + char target_names[32][129]; + bool good=true; + int num_targets=0; + struct location_t *targets[32]; + int num_targets_found=0; + int max_targets=side==USA?4:6; + for(int i=0;i<max_targets && good;++i) { - print_string(map[i]); - print_string("\n"); + getnstr(target_names[i], 128); + if(strcmp(target_names[i],"")==0) + { + good=false; + } + else + { + ++num_targets; + allLower(target_names[i]); + remove_punct(target_names[i]); + bool found=false; + for(int j=0;j<sizeof(world)/sizeof(struct location_t);++j) + { + if(strcmp(world[j].name, target_names[i])==0) + { + found=true; + if(world[j].owner!=side) + { + targets[num_targets_found]=&world[j]; + ++num_targets_found; + } + else + { + print_string("\n\nATTEMPTING TO FIRE AT OWN CITY.\nPLEASE CONFIRM (YES OR NO): "); + char response[17]; + getnstr(response, 16); + allLower(response); + remove_punct(response); + if(strcmp(response, "yes")==0 || strcmp(response, "y")==0) + { + print_string("\n\nATTEMPTING TO FIRE AT OWN CITY.\nARE YOU SURE (YES OR NO): "); + response[0]=0; + getnstr(response, 16); + allLower(response); + remove_punct(response); + if(strcmp(response, "yes")==0 || strcmp(response, "y")==0) + { + print_string("\nTARGET CONFIRMED.\n\n"); + targets[num_targets_found]=&world[j]; + ++num_targets_found; + } + } + } + } + } + if(!found) + { + print_string("TARGET NOT FOUND: "); + print_string(target_names[i]); + print_string("\n"); + } + } } - print_string("\nWHICH SIDE DO YOU WANT?\n\n 1. UNITED STATES\n 2. SOVIET UNION\n\n"); - bool good=false; - unsigned int side=0; - while(!good) + for(int i=0;i<num_targets_found;++i) { - print_string("PLEASE CHOOSE ONE: "); - scanw("%u", &side); - if(side==1 || side==2) - good=true; + fire_missile(targets[i]); } clear(); + print_map_with_pops(); +} +static void do_missile_launch(int side) +{ attr_on(WA_UNDERLINE, 0); - print_string("AWAITING FIRST STRIKE COMMAND"); + print_string("AWAITING STRIKE COMMAND"); attr_off(WA_UNDERLINE, 0); print_string("\n\n\nPLEASE LIST PRIMARY TARGETS BY\nCITY AND/OR COUNTY NAME:\n\n"); char target_names[32][129]; - good=true; + bool good=true; int num_targets=0; struct location_t *targets[32]; int num_targets_found=0; - int max_targets=side==USA?4:5; + int max_targets=side==USA?4:6; for(int i=0;i<max_targets && good;++i) { getnstr(target_names[i], 128); @@ -207,3 +272,95 @@ void global_thermonuclear_war(void) clear(); print_map_with_pops(); } +static void do_ai_move(int side) +{ +} +static void do_peace_talks(int side) +{ +} +static void do_human_move(int side) +{ + bool good=false; + print_string("WHAT ACTION DO YOU WISH TO TAKE?\n\n 1. MISSILE LAUNCH\n 2. PEACE TALKS\n 3. SURRENDER\n\n"); + int move=0; + while(!good) + { + print_string("PLEASE CHOOSE ONE: "); + scanw("%u", &move); + if(move==1 || move==2 || move==3) + good=true; + } + switch(move) + { + case 1: + do_missile_launch(side); + break; + case 2: + do_peace_talks(side); + break; + case 3: + surrender=true; + winner=side==1?2:1; + break; + } +} + +void global_thermonuclear_war(void) +{ + surrender=false; + clear(); + for(int i=0;i<sizeof(map)/sizeof(const char*);++i) + { + print_string(map[i]); + print_string("\n"); + } + print_string("\nWHICH SIDE DO YOU WANT?\n\n 1. UNITED STATES\n 2. SOVIET UNION\n\n"); + bool good=false; + unsigned int side=0; + while(!good) + { + print_string("PLEASE CHOOSE ONE: "); + scanw("%u", &side); + if(side==1 || side==2) + good=true; + } + clear(); + do_first_strike(side); + unsigned int us_pop=0, ussr_pop; + calc_pops(&us_pop, &ussr_pop); + while(us_pop!=0 && ussr_pop!=0 && !surrender) + { + do_human_move(side); + calc_pops(&us_pop, &ussr_pop); + do_ai_move(side); + calc_pops(&us_pop, &ussr_pop); + } + clear(); + if(!surrender) + { + if(us_pop==0 && ussr_pop==0) + { + print_string("WINNER: NONE\n\n"); + } + else if(us_pop==0) + { + print_string("WINNER: SOVIET UNION\n\n"); + } + else if(ussr_pop==0) + { + print_string("WINNER: UNITED STATES\n\n"); + } + } + else + { + switch(winner) + { + case 1: + print_string("WINNER: UNITED STATES\n\n"); + break; + case 2: + print_string("WINNER: SOVIET UNION\n\n"); + break; + } + } +} |