aboutsummaryrefslogtreecommitdiff
path: root/trade.c
blob: 8e474bd02ff4b753f17b49dff188cb4a14e05b3f (plain)
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
#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);
void io_printcolor(int x, int y, int color, char *s);
char* getname(int res);
int view_refresh(int focus, int cursor, int player);
char* itoa(int i);

static int tradebuffer[3][GOLD+1];

//0 is east, 1 is west
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);
  if(tradebuffer[1][i])
   io_printcolor(x+10, y-1, 31, itoa(west[i]));
  if(tradebuffer[0][i])
   io_printcolor(x+23, y-1, 31, itoa(east[i]));
 }
 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;
 }
}
bgstack15