-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.c
615 lines (511 loc) · 13.8 KB
/
play.c
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
//
// Copyright (c) 2009-2011, Wei Mingzhi <[email protected]>.
// Copyright (c) 2011-2024, SDLPAL development team.
// All rights reserved.
//
// This file is part of SDLPAL.
//
// SDLPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
#include "main.h"
VOID
PAL_GameUpdate(
BOOL fTrigger
)
/*++
Purpose:
The main game logic routine. Update the status of everything.
Parameters:
[IN] fTrigger - whether to process trigger events or not.
Return value:
None.
--*/
{
WORD wEventObjectID, wDir;
int i;
LPEVENTOBJECT p;
WORD wResult;
//
// Check for trigger events
//
if (fTrigger)
{
//
// Check if we are entering a new scene
//
if (gpGlobals->fEnteringScene)
{
//
// Run the script for entering the scene
//
gpGlobals->fEnteringScene = FALSE;
i = gpGlobals->wNumScene - 1;
gpGlobals->g.rgScene[i].wScriptOnEnter = PAL_RunTriggerScript(gpGlobals->g.rgScene[i].wScriptOnEnter, 0xFFFF);
if (gpGlobals->fEnteringScene)
{
//
// Don't go further as we're switching to another scene
//
return;
}
PAL_ClearKeyState();
PAL_MakeScene();
}
//
// Update the vanish time for all event objects
//
for (wEventObjectID = 0; wEventObjectID < gpGlobals->g.nEventObject; wEventObjectID++)
{
p = &gpGlobals->g.lprgEventObject[wEventObjectID];
if (p->sVanishTime != 0)
{
p->sVanishTime += ((p->sVanishTime < 0) ? 1 : -1);
}
}
//
// Loop through all event objects in the current scene
//
for (wEventObjectID = gpGlobals->g.rgScene[gpGlobals->wNumScene - 1].wEventObjectIndex + 1;
wEventObjectID <= gpGlobals->g.rgScene[gpGlobals->wNumScene].wEventObjectIndex;
wEventObjectID++)
{
p = &gpGlobals->g.lprgEventObject[wEventObjectID - 1];
if (p->sVanishTime != 0)
{
continue;
}
if (p->sState < 0)
{
if (p->x < PAL_X(gpGlobals->viewport) ||
p->x > PAL_X(gpGlobals->viewport) + 320 ||
p->y < PAL_Y(gpGlobals->viewport) ||
p->y > PAL_Y(gpGlobals->viewport) + 320)
{
p->sState = abs(p->sState);
p->wCurrentFrameNum = 0;
}
}
else if (p->sState > 0 && p->wTriggerMode >= kTriggerTouchNear)
{
//
// This event object can be triggered without manually exploring
//
if (abs(PAL_X(gpGlobals->viewport) + PAL_X(gpGlobals->partyoffset) - p->x) +
abs(PAL_Y(gpGlobals->viewport) + PAL_Y(gpGlobals->partyoffset) - p->y) * 2 <
(p->wTriggerMode - kTriggerTouchNear) * 32 + 16)
{
//
// Player is in the trigger zone.
//
if (p->nSpriteFrames)
{
//
// The sprite has multiple frames. Try to adjust the direction.
//
int xOffset, yOffset;
p->wCurrentFrameNum = 0;
xOffset = PAL_X(gpGlobals->viewport) + PAL_X(gpGlobals->partyoffset) - p->x;
yOffset = PAL_Y(gpGlobals->viewport) + PAL_Y(gpGlobals->partyoffset) - p->y;
if (xOffset > 0)
{
p->wDirection = ((yOffset > 0) ? kDirEast : kDirNorth);
}
else
{
p->wDirection = ((yOffset > 0) ? kDirSouth : kDirWest);
}
//
// Redraw the scene
//
PAL_UpdatePartyGestures(FALSE);
PAL_MakeScene();
VIDEO_UpdateScreen(NULL);
}
//
// Execute the script.
//
p->wTriggerScript = PAL_RunTriggerScript(p->wTriggerScript, wEventObjectID);
PAL_ClearKeyState();
if (gpGlobals->fEnteringScene)
{
//
// Don't go further on scene switching
//
return;
}
}
}
}
}
//
// Run autoscript for each event objects
//
for (wEventObjectID = gpGlobals->g.rgScene[gpGlobals->wNumScene - 1].wEventObjectIndex + 1;
wEventObjectID <= gpGlobals->g.rgScene[gpGlobals->wNumScene].wEventObjectIndex;
wEventObjectID++)
{
p = &gpGlobals->g.lprgEventObject[wEventObjectID - 1];
if (p->sState > 0 && p->sVanishTime == 0)
{
WORD wScriptEntry = p->wAutoScript;
if (wScriptEntry != 0)
{
p->wAutoScript = PAL_RunAutoScript(wScriptEntry, wEventObjectID);
if (gpGlobals->fEnteringScene)
{
//
// Don't go further on scene switching
//
return;
}
}
}
//
// Check if the player is in the way
//
if (fTrigger && p->sState >= kObjStateBlocker && p->wSpriteNum != 0 &&
abs(p->x - PAL_X(gpGlobals->viewport) - PAL_X(gpGlobals->partyoffset)) +
abs(p->y - PAL_Y(gpGlobals->viewport) - PAL_Y(gpGlobals->partyoffset)) * 2 <= 12)
{
//
// Player is in the way, try to move a step
//
wDir = (p->wDirection + 1) % 4;
for (i = 0; i < 4; i++)
{
int x, y;
PAL_POS pos;
x = PAL_X(gpGlobals->viewport) + PAL_X(gpGlobals->partyoffset);
y = PAL_Y(gpGlobals->viewport) + PAL_Y(gpGlobals->partyoffset);
x += ((wDir == kDirWest || wDir == kDirSouth) ? -16 : 16);
y += ((wDir == kDirWest || wDir == kDirNorth) ? -8 : 8);
pos = PAL_XY(x, y);
if (!PAL_CheckObstacleWithRange(pos, TRUE, 0, TRUE))
{
//
// move here
//
gpGlobals->viewport = PAL_XY(
PAL_X(pos) - PAL_X(gpGlobals->partyoffset),
PAL_Y(pos) - PAL_Y(gpGlobals->partyoffset));
break;
}
wDir = (wDir + 1) % 4;
}
}
}
if (--gpGlobals->wChasespeedChangeCycles == 0)
{
gpGlobals->wChaseRange = 1;
}
gpGlobals->dwFrameNum++;
}
VOID
PAL_GameUseItem(
VOID
)
/*++
Purpose:
Allow player use an item in the game.
Parameters:
None.
Return value:
None.
--*/
{
WORD wObject;
while (TRUE)
{
wObject = PAL_ItemSelectMenu(NULL, kItemFlagUsable);
if (wObject == 0)
{
return;
}
if (!(gpGlobals->g.rgObject[wObject].item.wFlags & kItemFlagApplyToAll))
{
//
// Select the player to use the item on
//
WORD wPlayer = 0;
while (TRUE)
{
wPlayer = PAL_ItemUseMenu(wObject);
if (wPlayer == MENUITEM_VALUE_CANCELLED)
{
break;
}
//
// Run the script
//
gpGlobals->g.rgObject[wObject].item.wScriptOnUse =
PAL_RunTriggerScript(gpGlobals->g.rgObject[wObject].item.wScriptOnUse, wPlayer);
//
// Remove the item if the item is consuming and the script succeeded
//
if ((gpGlobals->g.rgObject[wObject].item.wFlags & kItemFlagConsuming) &&
g_fScriptSuccess)
{
PAL_AddItemToInventory(wObject, -1);
}
}
}
else
{
//
// Run the script
//
gpGlobals->g.rgObject[wObject].item.wScriptOnUse =
PAL_RunTriggerScript(gpGlobals->g.rgObject[wObject].item.wScriptOnUse, 0xFFFF);
//
// Remove the item if the item is consuming and the script succeeded
//
if ((gpGlobals->g.rgObject[wObject].item.wFlags & kItemFlagConsuming) &&
g_fScriptSuccess)
{
PAL_AddItemToInventory(wObject, -1);
}
return;
}
}
}
VOID
PAL_GameEquipItem(
VOID
)
/*++
Purpose:
Allow player equip an item in the game.
Parameters:
None.
Return value:
None.
--*/
{
WORD wObject;
while (TRUE)
{
wObject = PAL_ItemSelectMenu(NULL, kItemFlagEquipable);
if (wObject == 0)
{
return;
}
PAL_EquipItemMenu(wObject);
}
}
VOID
PAL_Search(
VOID
)
/*++
Purpose:
Process searching trigger events.
Parameters:
None.
Return value:
None.
--*/
{
int x, y, xOffset, yOffset, dx, dy, dh, ex, ey, eh, i, k, l;
LPEVENTOBJECT p;
PAL_POS rgPos[13];
//
// Get the party location
//
x = PAL_X(gpGlobals->viewport) + PAL_X(gpGlobals->partyoffset);
y = PAL_Y(gpGlobals->viewport) + PAL_Y(gpGlobals->partyoffset);
if (gpGlobals->wPartyDirection == kDirNorth || gpGlobals->wPartyDirection == kDirEast)
{
xOffset = 16;
}
else
{
xOffset = -16;
}
if (gpGlobals->wPartyDirection == kDirEast || gpGlobals->wPartyDirection == kDirSouth)
{
yOffset = 8;
}
else
{
yOffset = -8;
}
rgPos[0] = PAL_XY(x, y);
for (i = 0; i < 4; i++)
{
rgPos[i * 3 + 1] = PAL_XY(x + xOffset, y + yOffset);
rgPos[i * 3 + 2] = PAL_XY(x, y + yOffset * 2);
rgPos[i * 3 + 3] = PAL_XY(x + xOffset, y);
x += xOffset;
y += yOffset;
}
for (i = 0; i < 13; i++)
{
//
// Convert to map location
//
dh = ((PAL_X(rgPos[i]) % 32) ? 1 : 0);
dx = PAL_X(rgPos[i]) / 32;
dy = PAL_Y(rgPos[i]) / 16;
//
// Loop through all event objects
//
for (k = gpGlobals->g.rgScene[gpGlobals->wNumScene - 1].wEventObjectIndex;
k < gpGlobals->g.rgScene[gpGlobals->wNumScene].wEventObjectIndex; k++)
{
p = &(gpGlobals->g.lprgEventObject[k]);
ex = p->x / 32;
ey = p->y / 16;
eh = ((p->x % 32) ? 1 : 0);
if (p->sState <= 0 || p->wTriggerMode >= kTriggerTouchNear ||
p->wTriggerMode * 6 - 4 < i || dx != ex || dy != ey || dh != eh)
{
continue;
}
//
// Adjust direction/gesture for party members and the event object
//
if (p->nSpriteFrames * 4 > p->wCurrentFrameNum)
{
p->wCurrentFrameNum = 0; // use standing gesture
p->wDirection = (gpGlobals->wPartyDirection + 2) % 4; // face the party
for (l = 0; l <= gpGlobals->wMaxPartyMemberIndex; l++)
{
//
// All party members should face the event object
//
gpGlobals->rgParty[l].wFrame = gpGlobals->wPartyDirection * 3;
}
//
// Redraw everything
//
PAL_MakeScene();
VIDEO_UpdateScreen(NULL);
}
//
// Execute the script
//
p->wTriggerScript = PAL_RunTriggerScript(p->wTriggerScript, k + 1);
//
// Clear inputs and delay for a short time
//
UTIL_Delay(50);
PAL_ClearKeyState();
return; // don't go further
}
}
}
VOID
PAL_StartFrame(
VOID
)
/*++
Purpose:
Starts a video frame. Called once per video frame.
Parameters:
None.
Return value:
None.
--*/
{
//
// Run the game logic of one frame
//
PAL_GameUpdate(TRUE);
if (gpGlobals->fEnteringScene)
{
return;
}
//
// Update the positions and gestures of party members
//
PAL_UpdateParty();
//
// Update the scene
//
PAL_MakeScene();
VIDEO_UpdateScreen(NULL);
if (g_InputState.dwKeyPress & kKeyMenu)
{
//
// Show the in-game menu
//
PAL_InGameMenu();
}
else if (g_InputState.dwKeyPress & kKeyUseItem)
{
//
// Show the use item menu
//
PAL_GameUseItem();
}
else if (g_InputState.dwKeyPress & kKeyThrowItem)
{
//
// Show the equipment menu
//
PAL_GameEquipItem();
}
else if (g_InputState.dwKeyPress & kKeyForce)
{
//
// Show the magic menu
//
PAL_InGameMagicMenu();
}
else if (g_InputState.dwKeyPress & kKeyStatus)
{
//
// Show the player status
//
PAL_PlayerStatus();
}
else if (g_InputState.dwKeyPress & kKeySearch)
{
//
// Process search events
//
PAL_Search();
}
else if (g_InputState.dwKeyPress & kKeyFlee)
{
//
// Quit Game
//
PAL_QuitGame();
}
}
VOID
PAL_WaitForKey(
WORD wTimeOut
)
/*++
Purpose:
Wait for any key.
Parameters:
[IN] wTimeOut - the maximum time of the waiting. 0 = wait forever.
Return value:
None.
--*/
{
DWORD dwTimeOut = SDL_GetTicks() + wTimeOut;
PAL_ClearKeyState();
while (wTimeOut == 0 || !SDL_TICKS_PASSED(SDL_GetTicks(), dwTimeOut))
{
UTIL_Delay(5);
if (g_InputState.dwKeyPress & (kKeySearch | kKeyMenu))
{
break;
}
}
}