Wing Commander
Reconstructed Win32 source (1996)
Loading...
Searching...
No Matches
mathfp.c
Go to the documentation of this file.
1/**
2 * @file
3 * @ingroup data
4 * @brief Random numbers, fixed-point arithmetic and trigonometry computed in
5 * floating point, and text-cursor and shape-frame helpers.
6 *
7 * Address range 434cd0-43540f. The preceding smart unit ends at chase_speed
8 * (434c70).
9 */
10#include "wc1.h"
11
12/* Linker commons defined here; LINK orders them by identifier hash. */
13short nPacketDecompressPending; /* 0x0059ab36 */
14unsigned short wPacketDecompressInputSize; /* 0x0059ab38 */
16volatile int bFrameTickPendingFlag; /* 0x0059ab3c */
17int bMouseCursorDamagePending; /* 0x0059ab40 */
18int nMouseCursorDamageRight; /* 0x0059ab44 */
19int nEventMousePointerDamageBottom; /* 0x0059ab48 */
20int nSavedDosInterruptVector; /* 0x0059ab4c */
21short nViewportOriginPositionY; /* 0x0059ab50 */
22short nViewportOriginPositionX; /* 0x0059ab52 */
23int nTickCount60Hz; /* 0x0059ab54 */
24unsigned char bEscapePressedFlag; /* 0x0059ab58 */
25int nMouseCursorDamageLeft; /* 0x0059ab5c */
26int nMousePointerDamageTopValue; /* 0x0059ab60 */
27
28/* Function start: 0x434CD0 */
29short RandomBelow(short n)
30{
31 return (short)(rand() % (int)n);
32}
33
34/* Function start: 0x434CF0 */
35/* `ret 4` shows one stack argument; the clock seeds the generator and the
36 * argument is never read. */
37void __stdcall SeedRandomFromClock(int seed)
38{
39 srand((unsigned int)time(0));
40}
41
42/* Function start: 0x434D10 */
43unsigned short RandomWord(void)
44{
45 return (unsigned short)rand();
46}
47
48/* Function start: 0x434D20 */
49short __stdcall RandomInRange(short lo, short hi)
50{
51 short span = hi - lo;
52
53 if (span == 0)
54 span = 1;
55 return lo + rand() % (span + 1);
56}
57
58/* Function start: 0x434D50 */
59short RandomBelowOrEqual(short n)
60{
61 if (n != -1 && n != 0)
62 return (short)(rand() % (n + 1));
63 return 0;
64}
65
66/* Function start: 0x434D80 */
67long MultiplyFixed(int left, int right)
68{
69 double leftValue = (double)left * (1.0 / 256.0);
70 double rightValue = (double)right * (1.0 / 256.0);
71
72 return (long)(leftValue * rightValue * 256.0);
73}
74
75/* Function start: 0x434DB0 */
76long DivideFixed(int numerator, int denominator)
77{
78 float numeratorValue = (float)((double)numerator * (1.0 / 256.0));
79 float denominatorValue;
80
81 if (denominator != 0)
82 denominatorValue = (float)((double)denominator * (1.0 / 256.0));
83 else
84 denominatorValue = 1.0f;
85 return (long)(numeratorValue / denominatorValue * 256.0);
86}
87
88/* Function start: 0x434E00 */
89long SinFixed(short degrees)
90{
91 return (long)(sin((double)degrees * DEGREES_TO_RADIANS) * 256.0);
92}
93
94/* Function start: 0x434E30 */
95long CosFixed(short degrees)
96{
97 return (long)(cos((double)degrees * DEGREES_TO_RADIANS) * 256.0);
98}
99
100/* Function start: 0x434E60 */
101long ArcSin(int value)
102{
103 return (long)(asin(value * 0.00390625f) *
104 57.295779513082323);
105}
106
107/* Function start: 0x434E90 */
108long ArcCos(int value)
109{
110 return (long)(acos(value * 0.00390625f) *
111 57.295779513082323);
112}
113
114/* Function start: 0x434EC0 */
115long Magnitude(int value)
116{
117 return (long)(sqrt(value * 0.00390625f) * 256.0);
118}
119
120/* Function start: 0x434EE0 */
121long PlanarMagnitude(int x, int y)
122{
123 double scaledX = (double)x * (1.0 / 256.0);
124 double scaledY = (double)y * (1.0 / 256.0);
125
126 scaledX *= scaledX;
127 scaledY *= scaledY;
128 return (long)(sqrt(scaledX + scaledY) * 256.0);
129}
130
131/* Function start: 0x434F20 */
132long Vector_magnitude(const FixedVector *vector)
133{
134 double x = (double)vector->x * (1.0 / 256.0);
135 double y = (double)vector->y * (1.0 / 256.0);
136 double z = (double)vector->z * (1.0 / 256.0);
137
138 x *= x;
139 y *= y;
140 z *= z;
141 return (long)(sqrt(x + y + z) * 256.0);
142}
143
144/* Function start: 0x434F70 */
145void __stdcall SetTextCursor(unsigned short a, unsigned short b)
146{
147 pCurTextContext->cursorX = (short)a;
148 pCurTextContext->cursorY = (short)b;
149}
150
151/* Function start: 0x434FA0 */
152void __stdcall SetTextContext(TextContext *context)
153{
154 pCurTextContext = context;
155}
156
157/* Function start: 0x434FB0 */
162
163/* Function start: 0x434FC0 */
164void *__stdcall IdentityHandle(void *v)
165{
166 return v;
167}
168
169/* Function start: 0x434FD0 */
170/* __stdcall forwarder that passes its argument through; `ret 4` is callee
171 * cleanup. The Ghidra label claimed a no-argument call, which was wrong. */
172void __stdcall SetWholePaletteFromTriplets(unsigned char *palette)
173{
175}
176
177/* Function start: 0x434FE0 */
178unsigned short __stdcall FontHeight(const void *p)
179{
180 return *(const unsigned short *)p;
181}
182
183/* Function start: 0x434FF0 */
184unsigned short __stdcall GetFontCharWidth(char i)
185{
186 return pCurTextContext->font[4 + (int)i];
187}
188
189/* Function start: 0x435010 */
191{
192}
193
194/* Function start: 0x435020 */
195short __stdcall GetShapeFrameBounds(short *bounds, short x, short y,
196 struct Shape *shape, short frame)
197{
198 short frameTableOffset;
199 struct ShapeFrame *frameData;
200
201 frameTableOffset = (short)(frame * 4);
202 if ((int)frameTableOffset < (int)(unsigned short)shape->frameOffsets[0]) {
203 frameTableOffset = (short)(frameTableOffset + 4);
204 shape = (struct Shape *)((unsigned char *)shape +
205 *(unsigned short *)((unsigned char *)shape + frameTableOffset));
206 frameData = (struct ShapeFrame *)shape;
207 bounds[2] = (short)(frameData->right + x);
208 bounds[0] = (short)(x - frameData->left);
209 bounds[1] = (short)(y - frameData->top);
210 bounds[3] = (short)(frameData->bottom + y);
211 return -1;
212 }
213 return 0;
214}
215
216/* Function start: 0x435090 */
217short __stdcall IsPointInRect(short x, short y, const short *rect)
218{
219 if (rect[0] <= x && x <= rect[2] &&
220 rect[1] <= y && y <= rect[3])
221 return 1;
222 return 0;
223}
224
225/* Function start: 0x4350D0 */
226void __stdcall SplitPackedPoint(ShortPoint point, short *p)
227{
228 IsPointInRect(point.x, point.y, p);
229}
230
231/* Function start: 0x4350F0 */
232void __stdcall DrawTextString(const char *text)
233{
234 const char *cursor;
235 const char *lineStart;
236 char value;
237 int right;
238 int lineWidth;
239 int wrapped;
240 int savedX;
241 int finished;
242
243 finished = 0;
244 wrapped = 0;
245 cursor = text;
246 do {
247 lineWidth = pCurTextContext->cursorX;
248 while (*cursor == ' ')
249 cursor++;
250 lineStart = cursor;
252 while (lineWidth < right) {
253 value = *cursor;
254 cursor++;
255 if (value == '\n' || value == '\r')
256 break;
257 if (value == 0) {
258 finished = 1;
259 break;
260 }
261 lineWidth +=
262 pCurTextContext->font[4 + value];
263 if (lineWidth >= right) {
264 /* Back up to the last space so the word wraps whole. */
265 lineWidth -=
266 pCurTextContext->font[4 + value];
267 cursor--;
268 wrapped = 1;
269 if (*cursor != ' ') {
270 while (*cursor != ' ' && cursor > text) {
271 lineWidth -=
273 ->font[4 + *cursor];
274 cursor--;
275 }
276 if (cursor <= text) {
278 "FATAL : INVALID STRING '%s'n", text);
281 exit(0);
282 }
283 }
284 break;
285 }
286 }
287
288 if (pCurTextContext->alignment == 2) {
289 savedX = pCurTextContext->cursorX;
290 pCurTextContext->cursorX = (short)(
294 lineWidth + pCurTextContext->cursorX + 1) / 2);
295 }
296 while (lineStart < cursor) {
297 DrawTextCharacter(*lineStart);
298 lineStart++;
299 }
300 if (pCurTextContext->alignment == 2)
301 pCurTextContext->cursorX = (short)savedX;
302 if (wrapped != 0) {
306 *(short *)pCurTextContext->font;
307 wrapped = 0;
308 }
309 } while (finished == 0);
310}
311
312/* Function start: 0x435290 */
313void __stdcall DrawTextCharacter(char character)
314{
315 TextContext *context;
316 unsigned char *font;
317 int fontHeight;
318 unsigned int glyphWidth;
319 int cursorY;
320
321 if (character == '\n') {
325 (short)(pCurTextContext->cursorY +
326 *(short *)pCurTextContext->font);
327 } else if (character == '\r') {
330 } else if (character != 0) {
331 font = pCurTextContext->font;
332 fontHeight = *(short *)font;
333 glyphWidth = font[4 + (int)(signed char)character];
334 context = pCurTextContext;
335 cursorY = context->cursorY;
336 DrawFontGlyph(character, context, fontHeight, glyphWidth, cursorY);
337 }
338}
339
340/* Function start: 0x435310 */
341void __stdcall AppendTextCharacter(char character)
342{
343 *pCurTextContext->textCursor = character;
346}
347
348/* Function start: 0x435340 */
349int __stdcall MeasureShapeFrameStorage(struct Shape *shape, short frame)
350{
351 unsigned char *run;
352 unsigned short rowLength;
353 unsigned char command;
354 int frameTableOffset;
355 int size;
356
357 size = 0;
358 if (shape != 0 && frame >= 0) {
359 frameTableOffset = frame * 4 + 4;
360 if (frameTableOffset < (unsigned short)shape->frameOffsets[0]) {
361 run = (unsigned char *)shape +
362 *(int *)((unsigned char *)shape + frameTableOffset) +
363 sizeof(struct ShapeFrame);
364 rowLength = *(unsigned short *)run;
365 run += 2;
366 while (rowLength != 0) {
367 run += 4;
368 if ((rowLength & 1) != 0) {
369 rowLength >>= 1;
370 while (rowLength != 0) {
371 command = *run;
372 run++;
373 if ((command & 1) != 0) {
374 command >>= 1;
375 run++;
376 rowLength =
377 (unsigned short)(rowLength - command);
378 size += command;
379 } else {
380 command >>= 1;
381 rowLength =
382 (unsigned short)(rowLength - command);
383 size += command;
384 run += command;
385 }
386 }
387 } else {
388 rowLength >>= 1;
389 size += rowLength;
390 run += rowLength;
391 }
392 rowLength = *(unsigned short *)run;
393 run += 2;
394 }
395 }
396 }
397 return size;
398}
399
400/* Function start: 0x4353F0 */
402{
405}
unsigned char bEscapePressedFlag
Definition mathfp.c:24
short nViewportOriginPositionX
Definition mathfp.c:22
int nMouseCursorDamageRight
Definition mathfp.c:18
int nEventMousePointerDamageBottom
Definition mathfp.c:19
int bMouseCursorDamagePending
Definition mathfp.c:17
TextContext * pCurTextContext
Definition data.c:244
short nPacketDecompressPending
Definition mathfp.c:13
short nViewportOriginPositionY
Definition mathfp.c:21
int nMousePointerDamageTopValue
Definition mathfp.c:26
short nCurrentPacketDecompressScratchSegment
Definition mathfp.c:15
unsigned short wPacketDecompressInputSize
Definition mathfp.c:14
int nTickCount60Hz
Definition mathfp.c:23
int nMouseCursorDamageLeft
Definition mathfp.c:25
volatile int bFrameTickPendingFlag
Definition mathfp.c:16
int nSavedDosInterruptVector
Definition mathfp.c:20
short bottom
Definition wc1base.h:180
short top
Definition wc1base.h:179
short left
Definition wc1base.h:178
short right
Definition wc1base.h:177
int frameOffsets[1]
Definition wc1base.h:92
short y
Definition wcdata.h:416
short x
Definition wcdata.h:415
short cursorY
Definition wc1base.h:238
unsigned char * font
Definition wc1base.h:239
char * textCursor
Definition wc1base.h:243
short cursorX
Definition wc1base.h:237
Viewport * viewport
Definition wc1base.h:236
unsigned char alignment
Definition wc1base.h:244
short right
Definition wc1base.h:78
short left
Definition wc1base.h:76
Umbrella header for the game core.
#define DEGREES_TO_RADIANS
Definition wc1base.h:31
void SystemDebugPrintf(const char *fmt,...)
Definition pilot.cpp:182
void __stdcall DrawTextCharacter(char character)
Definition mathfp.c:313
short __stdcall IsPointInRect(short x, short y, const short *rect)
Definition mathfp.c:217
unsigned short __stdcall GetFontCharWidth(char i)
Definition mathfp.c:184
long MultiplyFixed(int left, int right)
Definition mathfp.c:67
char PumpMessagesDuringWait(void)
Definition pilot.cpp:188
long ArcSin(int value)
Definition mathfp.c:101
void __stdcall SetWholePaletteFromTriplets(unsigned char *palette)
Definition mathfp.c:172
void __stdcall SplitPackedPoint(ShortPoint point, short *p)
Definition mathfp.c:226
long Vector_magnitude(const FixedVector *vector)
Definition mathfp.c:132
void __stdcall SetTextCursor(unsigned short a, unsigned short b)
Definition mathfp.c:145
void *__stdcall IdentityHandle(void *v)
Definition mathfp.c:164
void __stdcall SeedRandomFromClock(int seed)
Definition mathfp.c:37
long SinFixed(short degrees)
Definition mathfp.c:89
void __stdcall SetTextContext(TextContext *context)
Definition mathfp.c:152
long ArcCos(int value)
Definition mathfp.c:108
long PlanarMagnitude(int x, int y)
Definition mathfp.c:121
void ResetTextCursor(void)
Definition mathfp.c:401
void ReleaseVideoResourcesHook(void)
Definition mathfp.c:190
void WaitForVerticalBlankThunk(void)
Definition mathfp.c:158
void ClearDebugPauseFlags(void)
Definition pilot.cpp:229
long CosFixed(short degrees)
Definition mathfp.c:95
void __stdcall DrawTextString(const char *text)
Definition mathfp.c:232
long DivideFixed(int numerator, int denominator)
Definition mathfp.c:76
short RandomBelow(short n)
Definition mathfp.c:29
unsigned short __stdcall FontHeight(const void *p)
Definition mathfp.c:178
long Magnitude(int value)
Definition mathfp.c:115
void DIBwholePaletteFromTriplets(unsigned char *palette)
Definition dib.c:841
short __stdcall GetShapeFrameBounds(short *bounds, short x, short y, struct Shape *shape, short frame)
Definition mathfp.c:195
void DrawFontGlyph(char character, TextContext *context, int height, int width, int y)
Definition gr.c:533
void DIBwaitForVerticalBlank(void)
Definition dib.c:917
void __stdcall AppendTextCharacter(char character)
Definition mathfp.c:341
int __stdcall MeasureShapeFrameStorage(struct Shape *shape, short frame)
Definition mathfp.c:349
short __stdcall RandomInRange(short lo, short hi)
Definition mathfp.c:49
unsigned short RandomWord(void)
Definition mathfp.c:43
short RandomBelowOrEqual(short n)
Definition mathfp.c:59