1 #include "config.h"
2 #include "copyright.h"
3
4 #include <stdio.h>
5 #include "Wlib.h"
6 #include "defs.h"
7 #include "struct.h"
8 #include "data.h"
9
10 #include "stats.h"
11 #include "util.h"
12
13 #define BX_OFF() ((textWidth + 1) * W_Textwidth + S_IBORDER)
14 #define BY_OFF(line) ((line) * (W_Textheight + S_IBORDER) + S_IBORDER)
15 #define TX_OFF(len) ((textWidth - len) * W_Textwidth + S_IBORDER)
16 #define TY_OFF(line) BY_OFF(line)
17
18 #define STAT_WIDTH 160
19 #define STAT_HEIGHT BY_OFF(NUM_SLIDERS)
20 #define STAT_BORDER 2
21 #define S_IBORDER 5
22 #define STAT_X 422
23 #define STAT_Y 13
24
25 #define SL_WID \
26 (STAT_WIDTH - 2 * S_IBORDER - (textWidth + 1) * W_Textwidth)
27 #define SL_HEI (W_Textheight)
28
29 #define NUM_ELS(a) (sizeof (a) / sizeof (*(a)))
30 #define NUM_SLIDERS NUM_ELS(sliders)
31
32 typedef struct slider
33 {
34 char *label;
35 int min, max;
36 int low_red, high_red;
37 int label_length;
38 int diff;
39 int *var;
40 int lastVal;
41 }
42
43 SLIDER;
44
45 typedef struct record
46 {
47 int *data;
48 int last_value;
49 }
50
51 RECORD;
52
53 static SLIDER sliders[] =
54 {
55 {"Shields", 0, 100, 20, 100},
56 {"Damage", 0, 100, 0, 0},
57 {"Fuel", 0, 10000, 2000, 10000},
58 {"Warp", 0, 9, 0, 9},
59 {"Weapon Temp", 0, 1200, 0, 800},
60 {"Engine Temp", 0, 1200, 0, 800},
61
62 #ifdef ARMY_SLIDER
63 {"Armies", 0, 10, 0, 10},
64 #endif /* ARMY_SLIDER */
65
66 };
67
68 static int textWidth = 0;
69 static int initialized = 0;
70
71 static void box(int filled, int x, int y, int wid, int hei, W_Color color);
72
initStats(void)73 void initStats(void)
74 {
75 int i;
76
77 if (initialized)
78 return;
79 initialized = 1;
80 sliders[0].var = &(me->p_shield);
81 sliders[1].var = &(me->p_damage);
82 sliders[2].var = &(me->p_fuel);
83 sliders[3].var = &(me->p_speed);
84 sliders[4].var = &(me->p_wtemp);
85 sliders[5].var = &(me->p_etemp);
86
87 #ifdef ARMY_SLIDER
88 sliders[6].var = &(me->p_armies); /* note -- changed p_armies
89 * * * to int */
90 #endif /* ARMY_SLIDER */
91
92 for (i = 0; i < NUM_SLIDERS; i++)
93 {
94 sliders[i].label_length = strlen(sliders[i].label);
95 textWidth = MAX(textWidth, sliders[i].label_length);
96 sliders[i].diff = sliders[i].max - sliders[i].min;
97 sliders[i].lastVal = 0;
98 }
99 }
100
redrawStats(void)101 void redrawStats(void)
102 {
103 int i;
104
105 W_ClearWindow(statwin);
106 initStats();
107 for (i = 0; i < NUM_SLIDERS; i++)
108 {
109 sliders[i].lastVal = 0;
110 }
111 for (i = 0; i < NUM_SLIDERS; i++)
112 {
113 W_WriteText(statwin, TX_OFF(sliders[i].label_length), TY_OFF(i),
114 textColor, sliders[i].label, sliders[i].label_length,
115 W_RegularFont);
116 box(0, BX_OFF() - 1, BY_OFF(i) - 1, SL_WID + 2, SL_HEI + 2, borderColor);
117 sliders[i].lastVal = 0;
118 }
119 }
120
updateStats(void)121 void updateStats(void)
122 {
123 int i, value, diff, old_x, new_x;
124 W_Color color;
125 SLIDER *s;
126
127 initStats();
128 for (i = 0; i < NUM_SLIDERS; i++)
129 {
130 s = &sliders[i];
131 value = *(s->var);
132 if (value < s->min)
133 value = s->min;
134 else if (value > s->max)
135 value = s->max;
136 if (value == s->lastVal)
137 continue;
138 diff = value - s->lastVal;
139 if (diff < 0)
140 {
141 old_x = s->lastVal * SL_WID / s->diff;
142 new_x = value * SL_WID / s->diff;
143 box(1, BX_OFF() + new_x, BY_OFF(i), old_x - new_x, SL_HEI, backColor);
144
145 if (s->lastVal >= s->low_red && value < s->low_red)
146 box(1, BX_OFF(), BY_OFF(i), new_x, SL_HEI, warningColor);
147 else if (s->lastVal > s->high_red && value <= s->high_red)
148 box(1, BX_OFF(), BY_OFF(i), new_x, SL_HEI, myColor);
149 }
150 else
151 {
152 if (value < s->low_red)
153 color = warningColor;
154 else if (value > s->high_red)
155 {
156 color = warningColor;
157 if (s->lastVal <= s->high_red)
158 s->lastVal = 0;
159 }
160 else
161 {
162 color = myColor;
163 if (s->lastVal < s->low_red)
164 s->lastVal = 0;
165 }
166 old_x = s->lastVal * SL_WID / s->diff;
167 new_x = value * SL_WID / s->diff;
168 box(1, BX_OFF() + old_x, BY_OFF(i), new_x - old_x, SL_HEI, color);
169 }
170 s->lastVal = value;
171 }
172 }
173
box(int filled,int x,int y,int wid,int hei,W_Color color)174 static void box(int filled, int x, int y, int wid, int hei, W_Color color)
175 {
176 if (wid == 0)
177 return;
178
179 if (filled)
180 {
181 /* XFIX */
182 W_FillArea(statwin, x, y, wid + 1, hei + 1, color);
183 return;
184 }
185
186 W_MakeLine(statwin, x, y, x + wid, y, color);
187 W_MakeLine(statwin, x + wid, y, x + wid, y + hei, color);
188 W_MakeLine(statwin, x + wid, y + hei, x, y + hei, color);
189 W_MakeLine(statwin, x, y + hei, x, y, color);
190 }
191
192
calibrate_stats(void)193 void calibrate_stats(void)
194 {
195 register int i;
196
197 sliders[0].max = me->p_ship.s_maxshield;
198 sliders[0].low_red = .20 * ((double) sliders[0].max);
199 sliders[0].high_red = sliders[0].max;
200
201 sliders[1].max = me->p_ship.s_maxdamage;
202
203 sliders[2].max = me->p_ship.s_maxfuel;
204 sliders[2].low_red = .20 * ((double) sliders[2].max);
205 sliders[2].high_red = sliders[2].max;
206
207 sliders[3].max = me->p_ship.s_maxspeed;
208 sliders[3].high_red = sliders[3].max;
209
210 sliders[4].max = 1.2 * ((double) me->p_ship.s_maxwpntemp);
211 sliders[4].high_red = .667 * ((double) sliders[4].max);
212
213 sliders[5].max = 1.2 * ((double) me->p_ship.s_maxegntemp);
214 sliders[5].high_red = .667 * ((double) sliders[5].max);
215
216 #ifdef ARMY_SLIDER
217 sliders[6].max = troop_capacity();
218 #endif /* ARMY_SLIDER */
219
220 for (i = 0; i < NUM_SLIDERS; i++)
221 sliders[i].diff = sliders[i].max - sliders[i].min;
222 }
223