From 1a39ccc3088dd3fbc6da9571a17060cc50f432e4 Mon Sep 17 00:00:00 2001 From: Nathan Vance Date: Wed, 11 Mar 2015 22:31:44 -0400 Subject: Squelched some bugs, more to go! --- ai.c | 4 +-- ai_weights.c | 5 ++- data.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++--------- main.c | 5 +-- player_turn.c | 8 +++-- 5 files changed, 97 insertions(+), 22 deletions(-) diff --git a/ai.c b/ai.c index 5c0901a..7bb9f86 100644 --- a/ai.c +++ b/ai.c @@ -1,6 +1,6 @@ #include "7w.h" -int data_canafford(int p, int *cost); +int data_canafford(int p, int era, int card); int* data_gethand(int p); int data_numcards(int p); int data_getera(); @@ -23,7 +23,7 @@ int* ai_bestcard(int *hand, int player) //return card int card = 0; for(i = 0; hand[i] != -1 && i < 7; i++) { temp = weight_buildcard(data_getera(), hand[i], player); - if(! data_canafford(player, cards_getcost(data_getera(), hand[i]))) + if(! data_canafford(player, data_getera(), hand[i])) temp = 0; if(temp > max) { max = temp; diff --git a/ai_weights.c b/ai_weights.c index b56fe70..bd7cd0c 100644 --- a/ai_weights.c +++ b/ai_weights.c @@ -3,12 +3,14 @@ int data_getdir(int dir, int p); //0 is east, 1 is west, 2 returns player int* cards_getproduction(int era, int card); int* cards_getcost(int era, int card); -int data_canafford(int p, int *cost); +int data_canafford(int p, int era, int card); int* data_getdefinites(int p); int** data_getindefinites(int p); int* data_gettradables(int p); int data_productiontype(int e, int card); int data_getwonderstages(int p); +int data_getwonder(int p); +int data_getnextwonderstage(int p); int* get_special(int era, int card, int player); int get_trade(int player, int type, int direction); int cards_gettype(int era, int card); @@ -123,5 +125,6 @@ int weight_buildcard(int era, int card, int player) int weight_buildwonder(int player) { + if(! data_canafford(player, data_getwonder(player), data_getnextwonderstage(player))) return 0; return (data_getwonderstages(player)+2) * 2; } diff --git a/data.c b/data.c index 67e9f48..ded022a 100644 --- a/data.c +++ b/data.c @@ -1,17 +1,20 @@ #include "7w.h" #include +#include int* getdeck(int era, int numplayers); int* cards_getproduction(int era, int card); int cards_gettype(int era, int card); int* cards_getcost(int era, int card); int* cards_getproduction(int era, int card); +int* cards_getcouponed(int era, int card); int* get_intarray(int size); void shuffle(int *deck, int n); int* trade_buffer(); int* get_special(int era, int card, int player); void war(); int science(int player); +void halt(); #define MISC 3 #define DATAGOLD 0 @@ -40,10 +43,12 @@ void data_sorthands() } } +void data_endgame(); + void data_nextera() { war(); - if(era == 2) return; + if(era == 2) data_endgame(); era++; turn = 0; totturns = 0; @@ -175,6 +180,21 @@ int data_getwonderstages(int p) return player[p][3][2]; } +int data_getnextwonderstage(int p) +{ + int side = data_getwonderside(p); + int stage = data_getwonderstages(p); + int wonder = data_getwonder(p); + if(side == 0) { + if(stage >= 2) return -1; + } else { + if(wonder == 3 && stage >= 1) return -1; + if(wonder != 9 && stage >= 2) return -1; + if(wonder == 9 && stage >= 3) return -1; + } + return side*3+1+stage; +} + int data_getdefeats(int p) { return player[p][3][5]; @@ -247,6 +267,7 @@ void data_buildwonder(int p, int card) //0 is non producing, 1 produces one kind of resource, 2 produces multiple resources int data_productiontype(int e, int card) { + if(e < 0 || card < 0) return 0; int *prod = cards_getproduction(e, card); int i; int type = 0; @@ -292,9 +313,11 @@ void data_removedefinites(int p, int *cost) int** data_getindefinites(int p) { int i, j, k, l, m, *prod; + static int dats[4*INDEF]; static int *ret[INDEF]; for(i = 0; i < INDEF; i++) { - ret[i] = get_intarray(4); + //ret[i] = get_intarray(4); + ret[i] = &dats[4*i]; for(j = 0; j < 4; j++) ret[i][j] = -1; } @@ -318,12 +341,14 @@ int* data_gettradables(int p) { int *ret = get_intarray(GOLD+1); int i, j, k, *prod; - for(i = 0; i < GOLD; i++) ret[i] = 0; + for(i = 0; i < GOLD+1; i++) ret[i] = 0; for(i = 0; i < 3; i++) { for(j = 0; j < 7; j++) { - prod = cards_getproduction(i, player[p][i][j]); - for(k = 0; k < GOLD; k++) { - ret[k] += prod[k]; + if(player[p][i][j] != -1) { + prod = cards_getproduction(i, player[p][i][j]); + for(k = 0; k < GOLD; k++) { + ret[k] += prod[k]; + } } } } @@ -355,15 +380,6 @@ static int recurse(int *cost, int **indef, int c) return 0; } -int data_canafford(int p, int *cost) -{ - if(cost[GOLD] > data_getgold(p)) return 0; - int i, j, k; - data_removedefinites(p, cost); - if(data_iszerocost(cost)) return 1; - return recurse(cost, data_getindefinites(p), 0); -} - int* data_getbuilt(int p) { int *ret = get_intarray(43); @@ -389,6 +405,27 @@ int data_hasbuilt(int p, int era, int card) return 0; } +int data_iscouponed(int p, int era, int card) +{ + int* coupons = cards_getcouponed(era, card); + int i; + for(i = 0; i < 3; i += 2) { + if(coupons[1+1]) + if(data_hasbuilt(p, coupons[i], coupons[i+1])) return 1; + } +} + +int data_canafford(int p, int era, int card) +{ + if(data_iscouponed(p, era, card)) return 1; + int *cost = cards_getcost(era, card); + if(cost[GOLD] > data_getgold(p)) return 0; + int i, j, k; + data_removedefinites(p, cost); + if(data_iszerocost(cost)) return 1; + return recurse(cost, data_getindefinites(p), 0); +} + int data_haswonderstage(int p, int wonder, int stage) { if(data_getwonder(p) != wonder) return 0; @@ -434,3 +471,33 @@ int data_potentialvps(int p, int era, int card) player[i][3][3] -= cards_getproduction(era, card)[GOLD]; return vps; } + +char* cards_getname(int wonder, int stage); +void io_printborder(int x, int y, int width); +int io_printtext(int xorigin, int y, int width, char* text); +int io_getkey(); + +void data_endgame() +{ + int x = 20; + int y = 5; + int width = 100; + char s[width]; + io_printborder(x, y++, width); + int tots[numplayers]; + int i, max; + for(i = 0; i < numplayers; i++) tots[i] = data_gettotvps(i); + max = 0; + while(1) { + for(i = 0; i < numplayers; i++) { + if(tots[i] > tots[max]) max = i; + } + if(tots[max] == -1) break; + sprintf(s, "vps: %-2d %s", tots[max], cards_getname(data_getwonder(max), 0)); + y = io_printtext(x, y, width, s); + tots[max] = -1; + } + io_printborder(x, y++, width); + io_getkey(); + halt(); +} diff --git a/main.c b/main.c index ceff85c..03f7a18 100644 --- a/main.c +++ b/main.c @@ -13,6 +13,7 @@ void wonder_selected(int player); void view_refresh(int focus, int cursor, int player); int data_numplayers(); void data_endturn(); +int data_getera(); void halt() { @@ -20,7 +21,7 @@ void halt() exit(0); } -static int ais[7] = {0, 0, 1, 0, 0, 0, 0}; +static int ais[7] = {0, 1, 1, 1, 1, 1, 1}; main_routine() { @@ -39,7 +40,7 @@ main() { io_init(); cards_init(); - data_init(3); + data_init(5); main_routine(); halt(); } diff --git a/player_turn.c b/player_turn.c index bd9e805..a7d13d2 100644 --- a/player_turn.c +++ b/player_turn.c @@ -10,6 +10,9 @@ void data_discard(int p, int card); void data_buildwonder(int p, int card); void data_addgold(int amnt, int p); int data_numcards(int p); +int data_canafford(int p, int era, int card); +int data_getwonder(int p); +int data_getnextwonderstage(int p); int view_refresh(int focus, int cursor, int player); int wonder_numstages(int player); int* cards_getcost(int era, int card); @@ -28,7 +31,7 @@ int player_build(int focus, int cursor, int player) if(hand[cursor] == -1) return 0; int choice = postoptions(62, 20); if(choice == 0) { - if(data_canafford(player, cards_getcost(data_getera(), hand[cursor]))) { + if(data_canafford(player, data_getera(), hand[cursor])) { data_build(player, hand[cursor]); return 1; } @@ -38,10 +41,11 @@ int player_build(int focus, int cursor, int player) data_addgold(3, player); return 1; } else if(choice == 2) { - if(data_canafford(player, cards_getcost(data_getwonder(player), data_getwonderside(player)*3+1+cursor))) { + if(data_canafford(player, data_getwonder(player), data_getnextwonderstage(player))) { data_buildwonder(player, hand[cursor]); return 1; } + else postmessage("Can't afford this!"); } } return 0; -- cgit