From 61477ba75dd35655f38b3f280cd20cbe43b46d5a Mon Sep 17 00:00:00 2001 From: Nathan Vance Date: Wed, 29 Apr 2015 21:04:29 -0400 Subject: Fixed bug causing array content to be overwritten --- ai.c | 4 +++- ai_trade.c | 5 +++++ ai_weights.c | 6 +++--- ai_weights_special.c | 7 ++++--- data.c | 19 +++++++------------ io.c | 7 ++++--- main.c | 2 +- trade.c | 15 +++++++++------ util.c | 6 ++++++ wonder.c | 4 +++- 10 files changed, 45 insertions(+), 30 deletions(-) diff --git a/ai.c b/ai.c index 2f78a74..0a0936a 100644 --- a/ai.c +++ b/ai.c @@ -20,6 +20,7 @@ int data_hasbuiltname(int p, int era, int card); int* get_intarray(int size); int* ai_trade(int player, int era, int card); void write_trade(int player, int tradel, int trader); +void arraycpy(int *from, int *to, int len); void ai_bestcard(int *hand, int player, int *ret) { @@ -53,7 +54,8 @@ void ai_bestcard(int *hand, int player, int *ret) void ai_turn(int player) { int bestcard[5]; - int *hand = data_gethand(player); + int hand[7]; + arraycpy(data_gethand(player), hand, 7); int *wonder = get_intarray(4); wonder[0] = 0; int i; diff --git a/ai_trade.c b/ai_trade.c index bf6aaf8..5c6116d 100644 --- a/ai_trade.c +++ b/ai_trade.c @@ -8,12 +8,15 @@ This class is designed for the sole purpose of analyzing potential transactions int data_getgold(int p); int get_trade(int player, int type, int direction); int data_canafford(int p, int era, int card); +int* data_gettradables(int p); void trade_set(int player, int trade[3][GOLD]); int* trade_gettradables(int player, int direction); int data_gettotvps(int p); int data_getdir(int dir, int p); +int* cards_getcost(int era, int card); static int bestCost[3]; +static int baseCost[NUMRESOURCES]; void ai_cleartrade(int trade[3][GOLD]) { @@ -68,6 +71,7 @@ void recurse(int ret[], int cost, int trade[3][GOLD], int player, int era, int c return; //if we can afford it, don't bother recursing further. } for(j = 0; j < GOLD; j++) { + if(baseCost[j] - trade[0][j] - trade[1][j] - data_gettradables(player)[j] <= 0) continue; for(k = 0; k < 1; k++) { i = bestNeighbor(player, j); if(k) i = (i-1)*-1; @@ -92,6 +96,7 @@ int* ai_trade(int player, int era, int card) ret[i] = 0; ai_cleartrade(trade); int gold = data_getgold(player); + arraycpy(cards_getcost(era, card), baseCost, NUMRESOURCES); recurse(ret, 0, trade, player, era, card); restoreCost(ret); ai_cleartrade(trade); diff --git a/ai_weights.c b/ai_weights.c index 67fbd8e..23b25e1 100644 --- a/ai_weights.c +++ b/ai_weights.c @@ -4,7 +4,6 @@ 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 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); @@ -75,14 +74,15 @@ int weight_resource(int era, int card, int player) { int weight = 0; int *prod = cards_getproduction(era, card); - int *trade = data_gettradables(player); + int trade[GOLD+1]; + arraycpy(data_gettradables(player), trade, GOLD+1); int i, j; for(i = 0; i < GOLD; i++) { if(prod[i] && !trade[i]) weight += 5; if(prod[i] && (i == STONE || i == ORE) && trade[i] < 2) weight += 2; } for(j = 0; j < 2; j++) { - trade = data_gettradables(data_getdir(j, player)); + arraycpy(data_gettradables(data_getdir(j, player)), trade, GOLD+1); for(i = 0; i < GOLD; i++) { if(prod[i] && !trade[i]) weight++; if(prod[i] && trade[i] && get_trade(player, i, j) == 1) weight--; diff --git a/ai_weights_special.c b/ai_weights_special.c index 32c13dd..1168421 100644 --- a/ai_weights_special.c +++ b/ai_weights_special.c @@ -21,10 +21,11 @@ int weight_marketplace(int player) { int weight = 0; int i, j; - int *prod = data_gettradables(player); - int *trade; + int prod[GOLD+1]; + arraycpy(data_gettradables(player), prod, GOLD+1); + int trade[GOLD+1]; for(i = 0; i < 2; i++) { - trade = data_gettradables(data_getdir(i, player)); + arraycpy(data_gettradables(data_getdir(i, player)), trade, GOLD+1); for(j = CLOTH; j <= PAPER; j++) { if(trade[j] && !prod[j]) weight++; } diff --git a/data.c b/data.c index d8aa83c..aa08fda 100644 --- a/data.c +++ b/data.c @@ -8,7 +8,6 @@ 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); @@ -26,6 +25,7 @@ void write_purchase(int player, int era, int card, int type); void write_trade(int player, int tradel, int trader); void player_turn(int player); void haltError(char *message, int num); +void arraycpy(int *from, int *to, int len); #define MISC 3 #define DATAGOLD 0 @@ -219,12 +219,7 @@ int data_getnext(int p) int* data_gethand(int p) { - //int *ret = get_intarray(7); - static int ret[7]; - int i; - for(i = 0; i < 7; i++) - ret[i] = hands[(p+turn)%numplayers][i]; - return ret; + return hands[(p+turn)%numplayers]; } int data_numcards(int p) @@ -405,7 +400,7 @@ int data_productiontype(int e, int card) int* data_getdefinites(int p) { - int *ret = get_intarray(GOLD); + static int ret[GOLD]; int i, j, k, *prod; for(i = 0; i < GOLD; i++) ret[i] = 0; for(i = 0; i < 3; i++) { @@ -424,7 +419,8 @@ int* data_getdefinites(int p) void data_removedefinites(int p, int *cost) { cost[cards_gettype(data_getwonder(p), 0)]--; - int *prod = data_getdefinites(p); + int prod[GOLD]; + arraycpy(data_getdefinites(p), prod, GOLD); int i; for(i = 0; i < GOLD; i++) { cost[i] -= prod[i]; @@ -448,7 +444,6 @@ int** data_getindefinites(int p) static int dats[4*INDEF]; static int *ret[INDEF]; for(i = 0; i < INDEF; i++) { - //ret[i] = get_intarray(4); ret[i] = &dats[4*i]; for(j = 0; j < 4; j++) ret[i][j] = -1; @@ -487,7 +482,7 @@ int** data_getindefinites(int p) int* data_gettradables(int p) { - int *ret = get_intarray(GOLD+1); + static int ret[GOLD+1]; int i, j, k, *prod; for(i = 0; i < GOLD+1; i++) ret[i] = 0; for(i = 0; i < 3; i++) { @@ -530,7 +525,7 @@ static int recurse(int *cost, int **indef, int c) int* data_getbuilt(int p) { - int *ret = get_intarray(43); + static int ret[43]; int type, e, c; int i = 0; for(type = 0; type <= 7; type++) diff --git a/io.c b/io.c index 17c6166..55c1415 100644 --- a/io.c +++ b/io.c @@ -14,6 +14,7 @@ char* cards_specialmessage(int era, int card); int data_getera(); int* data_gethand(int p); int* get_special(int era, int card, int player); +void arraycpy(int *from, int *to, int len); void io_init() { @@ -194,9 +195,9 @@ int print_cards(int x, int y, int *cards, int cursor); int io_printhand(int x, int y, int player, int cursor, int mode) //mode 0 is normal, 1 is discard search { - int *hand; - if(mode) hand = data_getdiscards(); - else hand = data_gethand(player); + int hand[7]; + if(mode) arraycpy(data_getdiscards(), hand, 7); + else arraycpy(data_gethand(player), hand, 7); int i; io_printborder(x, y++, 28); if(mode) { diff --git a/main.c b/main.c index 2bf5781..174781e 100644 --- a/main.c +++ b/main.c @@ -34,7 +34,7 @@ void haltError(char *message, int num) exit(1); } -static int ais[7] = {1, 1, 1, 1, 1, 1, 1}; +static int ais[7] = {0, 1, 1, 1, 1, 1, 1}; main_routine() { diff --git a/trade.c b/trade.c index 94ecdb5..b220eea 100644 --- a/trade.c +++ b/trade.c @@ -88,9 +88,9 @@ void trade_commit(int player) int* trade_gettradables(int player, int direction) { int i; - int *ret; - if(direction) ret = data_gettradables(data_getwest(player)); - else ret = data_gettradables(data_geteast(player)); + static int ret[GOLD+1]; + if(direction) arraycpy(data_gettradables(data_getwest(player)), ret, GOLD+1); + else arraycpy(data_gettradables(data_geteast(player)), ret, GOLD+1); for(i = 0; i < GOLD; i++) ret[i] += tradebuffer[direction][i]; return ret; @@ -99,8 +99,10 @@ int* trade_gettradables(int player, int direction) void trade_print(int x, int y, int player, int cursorx, int cursory) { char s[40]; - int *east = trade_gettradables(player, 0); - int *west = trade_gettradables(player, 1); + int east[GOLD+1]; + arraycpy(trade_gettradables(player, 0), east, GOLD+1); + int west[GOLD+1]; + arraycpy(trade_gettradables(player, 1), west, GOLD+1); int width = 28; io_printborder(x, y++, width); y = io_printtext(x, y, width, "West: | East:"); @@ -128,7 +130,8 @@ void trade_print(int x, int y, int player, int cursorx, int cursory) int trade_change(int cursorx, int cursory, int player, int change) { int forced = 0; - int *tradee = trade_gettradables(player, cursorx); + int tradee[GOLD+1]; + arraycpy(trade_gettradables(player, cursorx), tradee, GOLD+1); if(tradebuffer[2][cursory]-1 < 0) { change = 1; forced = 1; diff --git a/util.c b/util.c index 20e8290..fcb67d2 100644 --- a/util.c +++ b/util.c @@ -28,6 +28,12 @@ char* get_chararray(int size) return ret; } +void arraycpy(int *from, int *to, int len) +{ + int i; + for(i = 0; i < len; i++) to[i] = from[i]; +} + /* Concatinates a and b and returns a different string. Only use when returned string can be recycled. */ char* cat(char a[], char b[]) { diff --git a/wonder.c b/wonder.c index dc1ba83..16c5647 100644 --- a/wonder.c +++ b/wonder.c @@ -25,6 +25,7 @@ int* data_getdefinites(int p); int** data_getindefinites(int p); int data_gettotvps(int p); int military_might(int player); +void arraycpy(int *from, int *to, int len); int wonder_numstages(int player) { @@ -63,7 +64,8 @@ int* print_wonder(int x, int y, int player, int cursor) io_printborder(x, y++, 28); //Print resource incomes - int *def = data_getdefinites(player); + int def[GOLD]; + arraycpy(data_getdefinites(player), def, GOLD); def[cards_gettype(data_getwonder(player), 0)]++; int **indef = data_getindefinites(player); y = io_printtext(x, y, 28, "Production:"); -- cgit