aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--data.c66
-rw-r--r--io.c12
-rw-r--r--player_turn.c30
-rw-r--r--trade.c150
-rw-r--r--view.c18
5 files changed, 248 insertions, 28 deletions
diff --git a/data.c b/data.c
index 8c5adb3..8959ab2 100644
--- a/data.c
+++ b/data.c
@@ -8,6 +8,7 @@ int* cards_getcost(int era, int card);
int* cards_getproduction(int era, int card);
int* get_intarray(int size);
void shuffle(int *deck, int n);
+int* trade_buffer();
#define MISC 3
#define DATAGOLD 0
@@ -20,6 +21,7 @@ static int numplayers;
static int era;
static int turn;
static int totturns;
+static int turngoldbuffer[7];
void data_sorthands()
{
@@ -106,11 +108,30 @@ void data_endturn()
if(totturns == 6) data_nextera();
}
+int data_geteast(int p)
+{
+ int ret = p-1;
+ if(ret < 0) ret = numplayers-1;
+}
+
+int data_getwest(int p)
+{
+ return (p+1)%numplayers;
+}
+
int* data_gethand(int p)
{
return hands[(p+turn)%numplayers];
}
+int data_numcards(int p)
+{
+ int *hand = data_gethand(p);
+ int numcards;
+ for(numcards = 0; numcards < 7 && hand[numcards] > -1; numcards++);
+ return numcards;
+}
+
int data_getera()
{
return era;
@@ -133,12 +154,13 @@ int data_getwonderstages(int p)
int data_getgold(int p)
{
- return player[p][3][3];
+ return player[p][3][3] + turngoldbuffer[p];
}
void data_addgold(int amnt, int p)
{
buffer[p][1] += amnt;
+ turngoldbuffer[p] += amnt;
}
int data_numplayers()
@@ -153,22 +175,23 @@ void data_discard(int p, int card)
for(i = 0; hand[i] != card; i++);
for(; i < 6; i++) hand[i] = hand[i+1];
hand[6] = -1;
+ for(i = 0; i < 7; i++) turngoldbuffer[i] = 0; //end of turn
}
void data_build(int p, int card)
{
- data_discard(p, card);
buffer[p][0] = card; //will be added to player array at end of turn
data_addgold(cards_getcost(era, card)[GOLD] * -1, p);
data_addgold(cards_getproduction(era, card)[GOLD], p);
+ data_discard(p, card);
}
void data_buildwonder(int p, int card)
{
- data_discard(p, card);
buffer[p][0] = -2;
data_addgold(cards_getcost(data_getwonder(p), data_getwonderside(p)*3+1+data_getwonderstages(p))[GOLD] * -1, p);
data_addgold(cards_getproduction(data_getwonder(p), data_getwonderside(p)*3+1+data_getwonderstages(p))[GOLD], p);
+ data_discard(p, card);
}
//0 is non producing, 1 produces one kind of resource, 2 produces multiple resources
@@ -189,8 +212,9 @@ int data_productiontype(int e, int card)
int* data_getdefinites(int p)
{
int *ret = get_intarray(GOLD);
+ int *trade = trade_buffer();
int i, j, k, *prod;
- for(i = 0; i < GOLD; i++) ret[i] = 0;
+ for(i = 0; i < GOLD; i++) ret[i] = trade[i];
for(i = 0; i < 3; i++) {
for(j = 0; j < 7; j++) {
if(data_productiontype(i, player[p][i][j]) == 1) {
@@ -240,6 +264,23 @@ int** data_getindefinites(int p)
return ret;
}
+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 < 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];
+ }
+ }
+ }
+ ret[GOLD] = data_getgold(p);
+ return ret;
+}
+
int data_iszerocost(int *cost)
{
int i;
@@ -289,6 +330,23 @@ int* data_getbuilt(int p)
return ret;
}
+int data_hasbuilt(int p, int era, int card)
+{
+ int *built = data_getbuilt(p);
+ int i;
+ for(i = 0; built[i] != -1; i += 2)
+ if(built[i] == era && built[i+1] == card) return 1;
+ return 0;
+}
+
+int data_haswonderstage(int p, int wonder, int stage)
+{
+ if(data_getwonder(p) != wonder) return 0;
+ if(data_getwonderside(p)*3+1 > stage) return 0;
+ if(data_getwonderstages(p)+data_getwonderside(p)*3+1 > stage) return 1;
+ return 0;
+}
+
int data_numbuilt(int p)
{
int num = 0;
diff --git a/io.c b/io.c
index 2658203..0515896 100644
--- a/io.c
+++ b/io.c
@@ -179,7 +179,7 @@ int io_printcard(int x, int y, int era, int card)
return y;
}
-void io_printhand(int x, int y, int player, int cursor)
+int io_printhand(int x, int y, int player, int cursor)
{
int *hand = data_gethand(player);
int i;
@@ -193,8 +193,14 @@ void io_printhand(int x, int y, int player, int cursor)
cursed = 1;
}
}
- if(cursed) io_printcard(x, y, data_getera(), hand[cursor]);
- else io_printborder(x, y, 28);
+ y = io_printtext(x, y, 28, " Trade...");
+ if(i == cursor) {
+ mvprintw(y-1, x+25, "*");
+ //trade_print(x, y, player, -1, -1);
+ }
+ else if(cursed) io_printcard(x, y, data_getera(), hand[cursor]);
+ else io_printborder(x, y++, 28);
+ return y;
}
void io_printplain(int x, int y, char *s)
diff --git a/player_turn.c b/player_turn.c
index d3dff67..3f84465 100644
--- a/player_turn.c
+++ b/player_turn.c
@@ -9,7 +9,8 @@ void data_build(int p, int card);
void data_discard(int p, int card);
void data_buildwonder(int p, int card);
void data_addgold(int amnt, int p);
-void view_refresh(int focus, int cursor, int player);
+int data_numcards(int p);
+int view_refresh(int focus, int cursor, int player);
int wonder_numstages(int player);
int* cards_getcost(int era, int card);
int data_numbuilt(int p);
@@ -17,6 +18,8 @@ void postmessage(char* message);
void posthelp();
void clearmessage();
int postoptions(int x, int y);
+int trade_routine(int x, int y, int player);
+void trade_commit(int player);
int player_build(int focus, int cursor, int player)
{
@@ -46,13 +49,12 @@ int player_build(int focus, int cursor, int player)
void player_turn(int player)
{
- int numcards, *hand;
+ int numcards, tradey;
int cursor = 0;
int focus = data_numplayers();
while(1) {
- hand = data_gethand(player);
- for(numcards = 0; numcards < 7 && hand[numcards] > -1; numcards++);
- view_refresh(focus, cursor, player);
+ numcards = data_numcards(player)+1; //include trade option in hand
+ tradey = view_refresh(focus, cursor, player);
switch(io_getkey()) {
case UP: cursor--;
break;
@@ -64,11 +66,12 @@ void player_turn(int player)
break;
case ENTER:
if(player_build(focus, cursor, player)) {
- player++;
- cursor = 0;
- focus = data_numplayers();
- clearmessage();
- }
+ trade_commit(player);
+ player++;
+ cursor = 0;
+ focus = data_numplayers();
+ clearmessage();
+ }
break;
case '\t': focus = (focus+1)%(data_numplayers()+1);
break;
@@ -83,8 +86,11 @@ void player_turn(int player)
if(focus < 0) focus = data_numplayers();
focus = focus%(data_numplayers()+1);
if(focus == data_numplayers()) {
- if(cursor < 0) cursor = numcards-1;
- if(cursor >= numcards) cursor = 0;
+ if(cursor < 0) cursor = numcards-2;
+ if(cursor >= numcards-1) {
+ if(trade_routine(61, tradey, player)) cursor = 0;
+ else cursor = numcards-2;
+ }
}
else {
if(cursor < 0) cursor = wonder_numstages((player+focus)%data_numplayers())+data_numbuilt(player+focus%data_numplayers())-1;
diff --git a/trade.c b/trade.c
index 1434a05..be3d339 100644
--- a/trade.c
+++ b/trade.c
@@ -1,6 +1,154 @@
#include "7w.h"
+#include <stdio.h>
+
+int data_hasbuilt(int p, int era, int card);
+int data_haswonderstage(int p, int wonder, int stage);
+int* data_gettradables(int p);
+void data_addgold(int amnt, int p);
+int data_getgold(int p);
+int data_geteast(int p);
+int data_getwest(int p);
+int data_numplayers();
+int data_numcards(int p);
+int io_getkey();
+void io_printborder(int x, int y, int width);
+int io_printtext(int xorigin, int y, int width, char* text);
+void io_printplain(int x, int y, char *s);
+char* getname(int res);
+int view_refresh(int focus, int cursor, int player);
+
+static int tradebuffer[3][GOLD+1];
//0 is east, 1 is west
-int get_trade(int direction) {
+int get_trade(int player, int type, int direction) {
+ if(type <= ORE) { //resource
+ if(data_haswonderstage(player, 7, 4)) return 1;
+ if(direction == 0) { //east
+ if(data_hasbuilt(player, 0, 18)) return 1;
+ } else { //west
+ if(data_hasbuilt(player, 0, 19)) return 1;
+ }
+ } else { //industrial product
+ if(data_hasbuilt(player, 0, 20)) return 1;
+ }
+ return 2;
+}
+
+int* trade_buffer()
+{
+ return tradebuffer[2];
+}
+
+void trade_clear(int player)
+{
+ data_addgold(tradebuffer[0][GOLD]*-1, data_geteast(player));
+ data_addgold(tradebuffer[1][GOLD]*-1, data_getwest(player));
+ data_addgold(tradebuffer[2][GOLD]*-1, player);
+ int i, j;
+ for(i = 0; i < 3; i++)
+ for(j = 0; j < GOLD+1; j++)
+ tradebuffer[i][j] = 0;
+}
+
+//gold amounts relative to player
+void trade_addgold(int player, int amnt, int direction)
+{
+ int p;
+ if(direction == 0) p = data_geteast(player);
+ if(direction == 1) p = data_getwest(player);
+ data_addgold(amnt, player);
+ tradebuffer[2][GOLD] += amnt;
+ data_addgold(amnt*-1, p);
+ tradebuffer[direction][GOLD] -= amnt;
+}
+
+void trade_commit(int player)
+{
+ int i;
+ for(i = 0; i < 3; i++)
+ tradebuffer[i][GOLD] = 0;
+ trade_clear(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));
+ for(i = 0; i < GOLD; i++)
+ ret[i] += tradebuffer[direction][i];
+ return ret;
+}
+
+void trade_print(int x, int y, int player, int cursorx, int cursory)
+{
+ view_refresh(data_numplayers(), data_numcards(player), player);
+ char s[40];
+ int *east = trade_gettradables(player, 0);
+ int *west = trade_gettradables(player, 1);
+ int width = 28;
+ io_printborder(x, y++, width);
+ y = io_printtext(x, y, width, "West: | East:");
+ sprintf(s, "rate: %d | rate: %d", get_trade(player, cursory, 1), get_trade(player, cursory, 0));
+ y = io_printtext(x, y, width, s);
+ int i;
+ for(i = 0; i < GOLD+1; i++) {
+ sprintf(s, "%-7s %d %c| %-7s %d %c", getname(i), west[i], (cursorx == 1 && cursory == i)? '*' : ' ', getname(i), east[i], (cursorx == 0 && cursory == i)? '*' : ' ');
+ y = io_printtext(x, y, width, s);
+ }
+ sprintf(s, "Your gold: %d", data_getgold(player));
+ y = io_printtext(x, y, width, s);
+ io_printborder(x, y++, width);
+}
+
+int trade_change(int cursorx, int cursory, int player, int change)
+{
+ int forced = 0;
+ int p;
+ if(cursorx == 1) p = data_getwest(player);
+ else p = data_geteast(player);
+ int *tradee = trade_gettradables(player, cursorx);
+ if(tradebuffer[2][cursory]-1 < 0) {
+ change = 1;
+ forced = 1;
+ }
+ if(tradee[cursory]-1 < 0 || data_getgold(player) < get_trade(player, cursory, cursorx)) {
+ if(forced) return 1;
+ change = -1;
+ }
+
+ tradebuffer[cursorx][cursory] -= change;
+ tradebuffer[2][cursory] += change;
+ trade_addgold(player, get_trade(player, cursory, cursorx)*change*-1, cursorx);
+
+ if(tradebuffer[2][cursory] == 0 || tradee[cursory] == 0 || data_getgold(player) == 0) return change*-1;
+ return change;
+}
+int trade_routine(int x, int y, int player)
+{
+ int cursorx, cursory, change;
+ cursorx = cursory = 0;
+ change = 1;
+ while(1) {
+ trade_print(x, y, player, cursorx, cursory);
+ switch(io_getkey()) {
+ case LEFT:
+ case RIGHT: cursorx = !cursorx;
+ change = 1;
+ break;
+ case UP: cursory--;
+ change = 1;
+ break;
+ case DOWN: cursory++;
+ change = 1;
+ break;
+ case ENTER: change = trade_change(cursorx, cursory, player, change);
+ break;
+ default: break;
+ }
+ if(cursory < 0) return 0;
+ if(cursory >= GOLD) return 1;
+ }
}
diff --git a/view.c b/view.c
index 3061435..54c8ee8 100644
--- a/view.c
+++ b/view.c
@@ -2,12 +2,12 @@
int print_wonder(int x, int y, int player, int cursor);
int print_wondersmall(int x, int y, int player, int selected, int dir);
-void io_printcard(int x, int y, int era, int card);
-void io_printhand(int x, int y, int player, int cursor);
+int io_printhand(int x, int y, int player, int cursor);
void io_clearscreen();
int data_numplayers();
-void io_printhand(int x, int y, int player, int cursor);
void printmessage(int x, int y, int width);
+int data_geteast(int p);
+int data_getwest(int p);
void view_printwonders(int focus, int cursor, int player)
{
@@ -17,8 +17,8 @@ void view_printwonders(int focus, int cursor, int player)
for(i = 0; i < num; i++) {
p = (player+i)%num;
dir = 0;
- if(i == 1) dir = 1;
- if(i == num-1) dir = 2;
+ if(p == data_geteast(player)) dir = 1;
+ if(p == data_getwest(player)) dir = 2;
if(focus == p)
print_wonder(34, 0, p, cursor);
y = print_wondersmall(x, y, p, focus == p, dir);
@@ -26,14 +26,16 @@ void view_printwonders(int focus, int cursor, int player)
printmessage(0, y+2, 35);
}
-void view_refresh(int focus, int cursor, int player)
+int view_refresh(int focus, int cursor, int player)
{
+ int ret;
io_clearscreen();
if(focus == data_numplayers()) {
view_printwonders(player, -1, player);
- io_printhand(61, 0, player, cursor);
+ ret = io_printhand(61, 0, player, cursor);
} else {
view_printwonders((focus+player)%data_numplayers(), cursor, player);
- io_printhand(61, 0, player, -1);
+ ret = io_printhand(61, 0, player, -1);
}
+ return ret;
}
bgstack15