-
Notifications
You must be signed in to change notification settings - Fork 0
/
EdUtils.cpp
executable file
·360 lines (297 loc) · 8.61 KB
/
EdUtils.cpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//============================================================================
//----------------------------------------------------------------------------
// EdUtils.c
//----------------------------------------------------------------------------
//============================================================================
// General utility functions.
#include "stdafx.h"
/*
#include <stdio.h>
#include "string.h"
*/
#include "global.h"
// Global variables
// external global variables
extern town_record_type town;
extern scen_item_data_type scen_data;
// local variables
// function prototype
// void debug_stop(short which_error);
// void RedAlert_c (char *theStr);
// void RedAlert (char *theStr);
// void RedAlert_big_color (char *str1,char *str2,char *str3,char *str4,short num,char *str5,short color);
// void RedAlert_big (char *str1,char *str2,char *str3,char *str4,short num,char *str5);
void display_error_dialog(const char* theStr,Boolean shut_down);
// short rect_dist(RECT *r1,RECT *r2);
// RECT rect_union(RECT *r1,RECT *r2);
Boolean r1_in_r2(macRECT r1,macRECT r2);
// RECT rect_centered_around_point(RECT r, location l);
// Boolean loc_in_rect(location loc,RECT r);
// Boolean loc_touch_rect(location loc,macRECT r);
// short a_v(short x);
// for cross-platform compatibility
void SetRECT( RECT& rect, int left, int top, int right, int bottom )
{
rect.left = left; rect.top = top; rect.right = right; rect.bottom = bottom;
}
void OffsetLocation( location& loc, int xofst, int yofst )
{
loc.x = loc.x + (char)xofst; loc.y = loc.y + (char)yofst;
}
// Beaware, in xxxInRECT, right and bottom is not included in the area
// The RECT is on the mesh, but the POINT is within the mesh.
// The original concept is from Mac QuickDraw
bool LocationInRECT( location& loc, RECT& rect )
{
return ( (rect.left <= (int)loc.x) && ((int)loc.x < rect.right)
&& (rect.top <= (int)loc.y) && ((int)loc.y < rect.bottom) );
}
bool POINTInRECT( POINT p, RECT r )
{
return ( (r.left <= (int)p.x) && ((int)p.x < r.right)
&& (r.top <= (int)p.y) && ((int)p.y < r.bottom) );
}
// DBUG UTILITIES
// Used to stop game when an error happens
/*
void debug_stop(short which_error)
{
// short i = which_error;
}
*/
/*
void RedAlert_c (char *theStr)
{
char new_str[256];
strcpy((char *) new_str,theStr);
c2p(new_str);
display_error_dialog(new_str,TRUE);
}
*/
void ASB (const char* theStr)
{
char new_str[256];
strncpy((char *) new_str,theStr,255);
new_str[255]=0; //strncpy doesn't guarantee null termination
display_error_dialog(new_str,FALSE);
}
void ASB_big (const char* str1,const char* str2,const char* str3,const char* str4,short num,const char* str5)
{
char str[256];
//!!!: this may overflow
if (num >= 0)
sprintf((char *) str,"%s%s%s%s%d%s",str1,str2,str3,str4,(int)num,str5);
else
sprintf((char *) str,"%s%s%s%s%s",str1,str2,str3,str4,str5);
display_error_dialog(str,FALSE);
}
/*
void ASB_big_color (char *str1,char *str2,char *str3,char *str4,short num,char *str5,short dummy)
{
char str[256];
if (num >= 0)
sprintf((char *) str,"%s%s%s%s%d%s",str1,str2,str3,str4,(int)num,str5);
else sprintf((char *) str,"%s%s%s%s%s",str1,str2,str3,str4,str5);
c2p(str);
display_error_dialog(str,FALSE);
}
*/
/*
void RedAlert (char *theStr)
{
display_error_dialog(theStr,TRUE);
}
*/
/*
void RedAlert_big_color (char *str1,char *str2,char *str3,char *str4,short num,char *str5,short color)
{
char str[256];
if (num >= 0)
sprintf((char *) str,"%s%s%s%s%d%s",str1,str2,str3,str4,(int)num,str5);
else sprintf((char *) str,"%s%s%s%s%s",str1,str2,str3,str4,str5);
c2p(str);
display_error_dialog(str,TRUE);
}
*/
/*
void RedAlert_big (char *str1,char *str2,char *str3,char *str4,short num,char *str5)
{
char str[256];
if (num >= 0)
sprintf((char *) str,"%s%s%s%s%d%s",str1,str2,str3,str4,(int)num,str5);
else sprintf((char *) str,"%s%s%s%s%s",str1,str2,str3,str4,str5);
c2p(str);
display_error_dialog(str,TRUE);
}
*/
void display_error_dialog(const char* theStr,Boolean shut_down)
{
mes_box(theStr); // Bring up alert.
if (shut_down == TRUE)
PostQuitMessage(0);
}
void ZeroRectCorner (RECT* theRect)
{
theRect->right -= theRect->left; // Move right edge by amount of left.
theRect->bottom -= theRect->top; // Move bottom edge by amount of top.
theRect->left = 0; // Can now set left to zero.
theRect->top = 0; // Can set top edge to zero as well.
}
// Handy function for returning the absolute width of a rectangle.
short rect_width (RECT* theRect)
{
return (short)(theRect->right - theRect->left);
}
// Handy function for returning the absolute height of a rectangle.
short rect_height (RECT* theRect)
{
return (short)(theRect->bottom - theRect->top);
}
// Other rectangle functions
Boolean rects_touch(RECT* r1,RECT* r2)
{
if (r1->right <= r2->left)
return (FALSE);
if (r1->left >= r2->right)
return (FALSE);
if (r1->top >= r2->bottom)
return (FALSE);
if (r1->bottom <= r2->top)
return (FALSE);
return TRUE;
}
/*
short rect_dist(RECT *r1,RECT *r2)
{
if (rects_touch(r1,r2))
return 0;
short cur_dist = 0;
if ((r2->left >= r1->right) && (r2->left - r1->right > cur_dist))
cur_dist = r2->left - r1->right;
if ((r1->left >= r2->right) && (r1->left - r2->right > cur_dist))
cur_dist = r1->left - r2->right;
if ((r1->top >= r2->bottom) && (r1->top - r2->bottom > cur_dist))
cur_dist = r1->top - r2->bottom;
if ((r2->top >= r1->bottom) && (r2->top - r1->bottom > cur_dist))
cur_dist = r2->top - r1->bottom;
return cur_dist;
}
*/
/*
Boolean rect_empty(RECT *r)
{
if ((r->top >= r->bottom) || (r->right <= r->left))
return TRUE;
return FALSE;
}
*/
/*
RECT rect_union(RECT *r1,RECT *r2)
{
RECT r;
UnionRect(r1,r2,&r);
return r;
}
*/
/*
RECT rect_sect(RECT *r1,RECT *r2)
{
RECT r;
SectRect(r1,r2,&r);
return r;
}
*/
Boolean r1_in_r2(macRECT r1,macRECT r2)
{
if ((r1.left >= r2.left) && (r1.top >= r2.top) &&
(r1.bottom <= r2.bottom) && (r1.right <= r2.right))
return TRUE;
return FALSE;
}
// returns the given rect, but centered around point p
/*
RECT rect_centered_around_point(RECT r, location l)
{
short w = rect_width(&r);
short h = rect_height(&r);
SetRect(&r,l.x - (w / 2),l.y - (h / 2),l.x - (w / 2) + w,l.y - (h / 2) + h);
return r;
}
*/
void CenterRectInRect (RECT* rectA, RECT* rectB)
{
short widthA, tallA;
widthA = rect_width(rectA); // Get width of 1st rect.
tallA = rect_height(rectA); // Get height of 1st rect.
// Do the math (center horizontally).
rectA->left = rectB->left + (rect_width(rectB) - widthA) / 2;
rectA->right = rectA->left + widthA;
// Do the math (center vertically).
rectA->top = rectB->top + (rect_height(rectB) - tallA) / 2;
rectA->bottom = rectA->top + tallA;
}
Boolean same_string(const char* str1,const char* str2)
{
return(!strcmp(str1,str2));
}
// Returns TRUE is this location is in the active region of the zone
Boolean loc_in_active_area(location loc)
{
return loc_touches_rect(loc,town.in_town_rect);
}
/*
Boolean loc_in_rect(location loc,RECT r)
{
if ((loc.x >= r.left) && (loc.x < r.right) && (loc.y >= r.top) && (loc.y < r.bottom))
return TRUE;
return FALSE;
}
*/
// Returns TRUE is this location touches (is not strictly inside of) rectangle
Boolean loc_touches_rect(location loc,macRECT r)
{
if ((loc.x >= r.left) && (loc.x <= r.right) && (loc.y >= r.top) && (loc.y <= r.bottom))
return TRUE;
return FALSE;
}
/*
Boolean loc_touch_rect(location loc,macRECT r)
{
if ((loc.x >= r.left) && (loc.x <= r.right) && (loc.y >= r.top) && (loc.y <= r.bottom))
return TRUE;
return FALSE;
}
*/
/*
short a_v(short x)
{
if (x < 0)
return (-1 * x);
else return x;
}
*/
terrain_type_type get_ter(short which_ter)
{
terrain_type_type ter;
ter = scen_data.scen_ter_types[which_ter];
return ter;
}
void SetMacRect(macRECT* rect,short top, short left, short bottom, short right)
{
rect->top = top;
rect->left = left;
rect->bottom = bottom;
rect->right = right;
}
Boolean loc_touches_rect_border(location loc,macRECT r)
{
if ((loc.y == r.top) && (loc.x >= r.left) && (loc.x <= r.right))
return TRUE;
if ((loc.x == r.left) && (loc.y >= r.top) && (loc.y <= r.bottom))
return TRUE;
if ((loc.y == r.bottom) && (loc.x >= r.left) && (loc.x <= r.right))
return TRUE;
if ((loc.x == r.right) && (loc.y >= r.top) && (loc.y <= r.bottom))
return TRUE;
return FALSE;
}