forked from sdlpal/sdlpal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal.c
More file actions
695 lines (604 loc) · 20.9 KB
/
global.c
File metadata and controls
695 lines (604 loc) · 20.9 KB
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
//
// Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
// 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 <http://www.gnu.org/licenses/>.
//
#include "main.h"
#include "palcfg.h"
#include "stddef.h"
SCRIPTENTRY SCRIPT[43557]; // 脚本入口索引表 (用于查找剧情事件或逻辑代码的内存地址)
STORE_T STORE[21]; // 商店物品列表 (0 为灵壶咒获得物品列表)
ENEMY_T ENEMY[154]; // 敌人个体属性表
ENEMYTEAM ENEMY_TEAM[380]; // 敌人队伍配置
MAGIC_T MAGIC[104]; // 技能定义表
BATTLEFIELD_T BATTLEFIELD[58]; // 战场定义
LEVELUPMAGIC_ALL LEVELUP_MAGIC[20]; // 升级习得魔法表
int N_LEVELUP_MAGIC; // 习得规则的数量
ENEMYPOS ENEMY_POS; // 敌人站位坐标表
WORD LEVELUP_EXP[MAX_LEVELS + 1]; // 升级经验表
WORD BATTLE_EFFECT[10][2];
PLAYERROLES gEquipmentEffect[MAX_PLAYER_EQUIPMENTS + 1]; // 装备加成属性
WORD gPlayerStatus[MAX_PLAYER_ROLES][kStatusAll]; // 角色异常状态
DWORD gFrameNum; // 全局帧计数器
BOOL gEnteringScene; // 是否正在进入新场景
BOOL gNeedToFadeIn; // 是否需要淡入画面
BOOL gInBattle; // 是否处于战斗中
BOOL gAutoBattle; // 是否处于自动战斗模式
WORD gLastUnequippedItem; // 记录上一个被卸下的装备
PAL_POS gPartyOffset; // 主角相对于视口的偏移量
WORD gCurPalette; // 当前调色板 ID
BOOL gNightPalette; // 是否黑夜
SHORT gWaveProgression; // 波浪效果的进度计数
WORD gCurrentSaveSlot;
FILE *FP_FBP; // 战斗背景
FILE *FP_MGO; // 场景/地图中的动态对象图像 (Map Graphic Object)
FILE *FP_BALL; // 物品道具位图
FILE *FP_DATA; // 非图像数据
FILE *FP_F; // 角色战斗动作
FILE *FP_FIRE; // 法术特效
FILE *FP_RGM; // 角色头像 (Role Graphic Map)
FILE *FP_SSS; // 脚本数据
SAVEDATA g;
INT PAL_InitGlobals(VOID) {
// init global file handles
FP_FBP = UTIL_OpenRequiredFile("fbp.mkf");
FP_MGO = UTIL_OpenRequiredFile("mgo.mkf");
FP_BALL = UTIL_OpenRequiredFile("ball.mkf");
FP_DATA = UTIL_OpenRequiredFile("data.mkf");
FP_F = UTIL_OpenRequiredFile("f.mkf");
FP_FIRE = UTIL_OpenRequiredFile("fire.mkf");
FP_RGM = UTIL_OpenRequiredFile("rgm.mkf");
FP_SSS = UTIL_OpenRequiredFile("sss.mkf");
// init global static data
int n_events = PAL_MKFGetChunkSize(0, FP_SSS) / sizeof(EVENTOBJECT);
if (n_events != MAX_EVENTS)
TerminateOnError("number of events not matched");
N_LEVELUP_MAGIC = PAL_MKFGetChunkSize(6, FP_DATA) / sizeof(LEVELUPMAGIC_ALL);
PAL_MKFReadChunk((LPBYTE)SCRIPT, sizeof(SCRIPT), 4, FP_SSS);
PAL_MKFReadChunk((LPBYTE)STORE, sizeof(STORE), 0, FP_DATA);
PAL_MKFReadChunk((LPBYTE)ENEMY, sizeof(ENEMY), 1, FP_DATA);
PAL_MKFReadChunk((LPBYTE)ENEMY_TEAM, sizeof(ENEMY_TEAM), 2, FP_DATA);
PAL_MKFReadChunk((LPBYTE)MAGIC, sizeof(MAGIC), 4, FP_DATA);
PAL_MKFReadChunk((LPBYTE)BATTLEFIELD, sizeof(BATTLEFIELD), 5, FP_DATA);
PAL_MKFReadChunk((LPBYTE)LEVELUP_MAGIC, sizeof(LEVELUP_MAGIC), 6, FP_DATA);
PAL_MKFReadChunk((LPBYTE)BATTLE_EFFECT, sizeof(BATTLE_EFFECT), 11, FP_DATA);
PAL_MKFReadChunk((LPBYTE)(&ENEMY_POS), sizeof(ENEMY_POS), 13, FP_DATA);
PAL_MKFReadChunk((LPBYTE)LEVELUP_EXP, sizeof(LEVELUP_EXP), 14, FP_DATA);
return 0;
}
VOID PAL_FreeGlobals(VOID) { PAL_FreeConfig(); }
static VOID PAL_InitGlobalGameData(VOID) {
// init global vars
gEnteringScene = TRUE;
gLastUnequippedItem = 0;
memset(gPlayerStatus, 0, sizeof(gPlayerStatus));
// init global data, may be override by saved data later
memset(&g, 0, sizeof(g));
g.saveSlot = 1;
g.wCurScene = 1;
g.wChaseRange = 1;
PAL_MKFReadChunk((LPBYTE)(&PLAYER), sizeof(PLAYER), 3, FP_DATA);
PAL_MKFReadChunk((LPBYTE)g.scenes, sizeof(g.scenes), 1, FP_SSS);
PAL_MKFReadChunk((LPBYTE)OBJECT, sizeof(OBJECT), 2, FP_SSS);
PAL_MKFReadChunk((LPBYTE)g.events, sizeof(g.events), 0, FP_SSS);
for (int i = 0; i < MAX_PLAYER_ROLES; i++) {
int defaultLevel = PLAYER.rgwLevel[i];
g.Exp.rgPrimaryExp[i].wLevel = defaultLevel;
g.Exp.rgHealthExp[i].wLevel = defaultLevel;
g.Exp.rgMagicExp[i].wLevel = defaultLevel;
g.Exp.rgAttackExp[i].wLevel = defaultLevel;
g.Exp.rgMagicPowerExp[i].wLevel = defaultLevel;
g.Exp.rgDefenseExp[i].wLevel = defaultLevel;
g.Exp.rgDexterityExp[i].wLevel = defaultLevel;
g.Exp.rgFleeExp[i].wLevel = defaultLevel;
}
}
static INT PAL_LoadGame(int iSaveSlot) {
gCurrentSaveSlot = iSaveSlot;
// Try to open the specified file
FILE *fp = UTIL_OpenFile(PAL_va(1, "%d.rpg", iSaveSlot));
if (!fp)
return -1;
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
// Validate file size.
if (size != sizeof(SAVEDATA))
return -1;
// Read all data from the file and close.
fseek(fp, 0, SEEK_SET);
fread((void *)&g, 1, size, fp);
fclose(fp);
// Note: Original code zeroed poison status on load instead of restoring it.
memset(g.rgPoisonStatus, 0, sizeof(g.rgPoisonStatus));
gNightPalette = (g._paletteOffset != 0);
gEnteringScene = FALSE;
PAL_CompressInventory();
return 0;
}
VOID PAL_SaveGame(int iSaveSlot, WORD wSavedTimes) {
gCurrentSaveSlot = iSaveSlot;
g.saveSlot = wSavedTimes;
g._paletteOffset = gNightPalette ? 0x180 : 0;
g.unused = 2; // Hardcoded in original logic
g.unused2[0] = 0;
g.unused2[1] = 0;
g.unused2[2] = 0;
FILE *fp = UTIL_OpenFileForMode(PAL_va(1, "%d.rpg", iSaveSlot), "wb");
if (fp != NULL) {
fwrite(&g, sizeof(SAVEDATA), 1, fp);
fclose(fp);
}
}
// Reload the game IN NEXT TICK, avoid reentrant problems.
VOID PAL_ReloadInNextTick(INT iSaveSlot) {
gCurrentSaveSlot = iSaveSlot; // needed for kLoadGlobalData
PAL_SetLoadFlags(kLoadGlobalData | kLoadScene | kLoadPlayerSprite);
gEnteringScene = TRUE;
gNeedToFadeIn = TRUE;
gFrameNum = 0;
}
VOID PAL_InitGameData(INT iSaveSlot) {
PAL_InitGlobalGameData();
if (iSaveSlot > 0) {
PAL_LoadGame(iSaveSlot);
}
PAL_UpdateEquipmentEffect();
}
INT PAL_CountItem(WORD wObjectID) {
if (wObjectID == 0)
return 0;
int count = 0;
// Search for the specified item in the inventory
for (int index = 0; index < MAX_INVENTORY; index++) {
if (g.rgInventory[index].wItem == wObjectID) {
count = g.rgInventory[index].nAmount;
break;
} else if (g.rgInventory[index].wItem == 0) {
break;
}
}
// search in player's equipments
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++) {
int p = PARTY_PLAYER(i);
for (int j = 0; j < MAX_PLAYER_EQUIPMENTS; j++) {
if (PLAYER.rgwEquipment[j][p] == wObjectID) {
count++;
}
}
}
return count;
}
BOOL PAL_GetItemIndexToInventory(WORD wObjectID, INT *index) {
for (*index = 0; *index < MAX_INVENTORY; (*index)++) {
if (g.rgInventory[*index].wItem == wObjectID) {
return TRUE;
} else if (g.rgInventory[*index].wItem == 0) {
break;
}
}
return FALSE;
}
INT PAL_AddItemToInventory(WORD wObjectID, INT iNum) {
if (wObjectID == 0)
return FALSE;
if (iNum == 0)
iNum = 1;
int index = 0;
bool fFound = PAL_GetItemIndexToInventory(wObjectID, &index);
if (iNum > 0) {
// 增加物品
if (index >= MAX_INVENTORY)
return FALSE;
INVENTORY *pItem = &g.rgInventory[index];
if (!fFound) {
pItem->wItem = wObjectID;
pItem->nAmount = 0;
}
pItem->nAmount += iNum;
if (pItem->nAmount > 99)
pItem->nAmount = 99;
return TRUE;
} else {
// 移除物品
iNum *= -1;
if (!fFound)
return FALSE;
INVENTORY *pItem = &g.rgInventory[index];
if (pItem->nAmount < iNum) {
iNum -= pItem->nAmount;
pItem->nAmount = 0;
} else {
pItem->nAmount -= iNum;
iNum = 0;
}
// 整理物品
PAL_CompressInventory();
// 返回缺少的数量 (负数)
return iNum ? -iNum : TRUE;
}
}
INT PAL_GetItemAmount(WORD wItem) {
for (int i = 0; i < MAX_INVENTORY; i++) {
if (g.rgInventory[i].wItem == 0)
break;
if (g.rgInventory[i].wItem == wItem)
return g.rgInventory[i].nAmount;
}
return 0;
}
// Remove all the items in inventory which has a number of zero.
VOID PAL_CompressInventory(VOID) {
int j = 0;
for (int i = 0; i < MAX_INVENTORY; i++) {
if (g.rgInventory[i].nAmount > 0) {
g.rgInventory[j] = g.rgInventory[i];
j++;
}
}
for (; j < MAX_INVENTORY; j++) {
g.rgInventory[j].wItem = 0;
g.rgInventory[j].nAmount = 0;
g.rgInventory[j].nAmountInUse = 0;
}
}
BOOL PAL_IncreaseHPMP(WORD wPlayerRole, SHORT sHP, SHORT sMP) {
WORD *pHP = &PLAYER.rgwHP[wPlayerRole];
WORD maxHP = PLAYER.rgwMaxHP[wPlayerRole];
WORD *pMP = &PLAYER.rgwMP[wPlayerRole];
WORD maxMP = PLAYER.rgwMaxMP[wPlayerRole];
WORD origHP = *pHP;
WORD origMP = *pMP;
// Only care about alive players
if (*pHP == 0)
return FALSE;
// Change HP
SHORT newHP = (SHORT)(*pHP) + sHP;
if (newHP < 0) {
*pHP = 0;
} else if (newHP > maxHP) {
*pHP = maxHP;
} else {
*pHP = newHP;
}
// Change MP
SHORT newMP = (SHORT)(*pMP) + sMP;
if (newMP < 0) {
*pMP = 0;
} else if (newMP > maxMP) {
*pMP = maxMP;
} else {
*pMP = newMP;
}
// Avoid over treatment
return origHP != *pHP || origMP != *pMP;
}
// Update the effects of all equipped items for all players.
VOID PAL_UpdateEquipmentEffect(VOID) {
memset(&(gEquipmentEffect), 0, sizeof(gEquipmentEffect));
for (int i = 0; i < MAX_PLAYER_ROLES; i++) {
for (int j = 0; j < MAX_PLAYER_EQUIPMENTS; j++) {
WORD w = PLAYER.rgwEquipment[j][i];
if (w != 0) {
OBJECT[w].item.wScriptOnEquip = PAL_RunTriggerScript(OBJECT[w].item.wScriptOnEquip, (WORD)i);
}
}
}
}
VOID PAL_RemoveEquipmentEffect(WORD wPlayerRole, WORD wEquipPart) {
WORD *p = (WORD *)(&gEquipmentEffect[wEquipPart]); // HACK
for (int i = 0; i < sizeof(PLAYERROLES) / sizeof(PLAYERS); i++) {
p[i * MAX_PLAYER_ROLES + wPlayerRole] = 0;
}
// 重置双重攻击
if (wEquipPart == kBodyPartHand) {
gPlayerStatus[wPlayerRole][kStatusDualAttack] = 0;
}
// 重置毒性抵抗
if (wEquipPart == kBodyPartWear) {
// Remove all poisons leveled 99
short index;
for (index = 0; index <= (short)g.wMaxPartyMemberIndex; index++) {
if (PARTY_PLAYER(index) == wPlayerRole)
break;
}
if (index <= (short)g.wMaxPartyMemberIndex) {
for (int i = 0; i < MAX_POISONS; i++) {
WORD w = g.rgPoisonStatus[i][index].wPoisonID;
if (w > 0 && OBJECT[w].poison.wPoisonLevel == 99) {
g.rgPoisonStatus[i][index].wPoisonID = 0;
g.rgPoisonStatus[i][index].wPoisonScript = 0;
}
}
}
}
}
VOID PAL_AddPoisonForPlayer(WORD wPlayerRole, WORD wPoisonID) {
int index;
for (index = 0; index <= g.wMaxPartyMemberIndex; index++) {
if (PARTY_PLAYER(index) == wPlayerRole)
break;
}
if (index > g.wMaxPartyMemberIndex)
return;
int i;
for (i = 0; i < MAX_POISONS; i++) {
WORD w = g.rgPoisonStatus[i][index].wPoisonID;
if (w == 0) // empty slot
break;
if (w == wPoisonID)
return; // already poisoned
}
if (i < MAX_POISONS) {
g.rgPoisonStatus[i][index].wPoisonID = wPoisonID;
g.rgPoisonStatus[i][index].wPoisonScript =
PAL_RunTriggerScript(OBJECT[wPoisonID].poison.wPlayerScript, wPlayerRole);
}
}
VOID PAL_CurePoisonByKind(WORD wPlayerRole, WORD wPoisonID) {
int index;
for (index = 0; index <= g.wMaxPartyMemberIndex; index++) {
if (PARTY_PLAYER(index) == wPlayerRole)
break;
}
if (index > g.wMaxPartyMemberIndex)
return;
for (int i = 0; i < MAX_POISONS; i++) {
if (g.rgPoisonStatus[i][index].wPoisonID == wPoisonID) {
g.rgPoisonStatus[i][index].wPoisonID = 0;
g.rgPoisonStatus[i][index].wPoisonScript = 0;
}
}
}
VOID PAL_CurePoisonByLevel(WORD wPlayerRole, WORD wMaxLevel) {
int index;
for (index = 0; index <= g.wMaxPartyMemberIndex; index++) {
if (PARTY_PLAYER(index) == wPlayerRole)
break;
}
if (index > g.wMaxPartyMemberIndex)
return;
for (int i = 0; i < MAX_POISONS; i++) {
WORD w = g.rgPoisonStatus[i][index].wPoisonID;
if (w > 0 && OBJECT[w].poison.wPoisonLevel <= wMaxLevel) {
g.rgPoisonStatus[i][index].wPoisonID = 0;
g.rgPoisonStatus[i][index].wPoisonScript = 0;
}
}
}
BOOL PAL_IsPlayerPoisonedByLevel(WORD wPlayerRole, WORD wMinLevel) {
int index;
for (index = 0; index <= g.wMaxPartyMemberIndex; index++) {
if (PARTY_PLAYER(index) == wPlayerRole)
break;
}
if (index > g.wMaxPartyMemberIndex)
return FALSE;
for (int i = 0; i < MAX_POISONS; i++) {
WORD w = g.rgPoisonStatus[i][index].wPoisonID;
if (w == 0)
continue;
WORD level = OBJECT[w].poison.wPoisonLevel;
if (level >= wMinLevel && level < 99)
return TRUE;
}
return FALSE;
}
BOOL PAL_IsPlayerPoisonedByKind(WORD wPlayerRole, WORD wPoisonID) {
int index;
for (index = 0; index <= g.wMaxPartyMemberIndex; index++) {
if (PARTY_PLAYER(index) == wPlayerRole)
break;
}
if (index > g.wMaxPartyMemberIndex)
return FALSE;
for (int i = 0; i < MAX_POISONS; i++)
if (g.rgPoisonStatus[i][index].wPoisonID == wPoisonID)
return TRUE;
return FALSE;
}
WORD PAL_GetPlayerAttackStrength(WORD wPlayerRole) {
WORD w = PLAYER.rgwAttackStrength[wPlayerRole];
for (int i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
w += gEquipmentEffect[i].rgwAttackStrength[wPlayerRole];
return w;
}
WORD PAL_GetPlayerMagicStrength(WORD wPlayerRole) {
WORD w = PLAYER.rgwMagicStrength[wPlayerRole];
for (int i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
w += gEquipmentEffect[i].rgwMagicStrength[wPlayerRole];
return w;
}
WORD PAL_GetPlayerDefense(WORD wPlayerRole) {
WORD w = PLAYER.rgwDefense[wPlayerRole];
for (int i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
w += gEquipmentEffect[i].rgwDefense[wPlayerRole];
return w;
}
WORD PAL_GetPlayerDexterity(WORD wPlayerRole) {
WORD w = PLAYER.rgwDexterity[wPlayerRole];
for (int i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
w += gEquipmentEffect[i].rgwDexterity[wPlayerRole];
return w;
}
WORD PAL_GetPlayerFleeRate(WORD wPlayerRole) {
WORD w = PLAYER.rgwFleeRate[wPlayerRole];
for (int i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
w += gEquipmentEffect[i].rgwFleeRate[wPlayerRole];
return w;
}
WORD PAL_GetPlayerPoisonResistance(WORD wPlayerRole) {
WORD w = PLAYER.rgwPoisonResistance[wPlayerRole];
for (int i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
w += gEquipmentEffect[i].rgwPoisonResistance[wPlayerRole];
if (w > 100)
w = 100;
return w;
}
WORD PAL_GetPlayerElementalResistance(WORD wPlayerRole, INT iAttrib) {
WORD w = PLAYER.rgwElementalResistance[iAttrib][wPlayerRole];
for (int i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
w += gEquipmentEffect[i].rgwElementalResistance[iAttrib][wPlayerRole];
if (w > 100)
w = 100;
return w;
}
WORD PAL_GetPlayerBattleSprite(WORD wPlayerRole) {
WORD w = PLAYER.rgwSpriteNumInBattle[wPlayerRole];
for (int i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
if (gEquipmentEffect[i].rgwSpriteNumInBattle[wPlayerRole] != 0)
w = gEquipmentEffect[i].rgwSpriteNumInBattle[wPlayerRole];
return w;
}
WORD PAL_GetPlayerCooperativeMagic(WORD wPlayerRole) {
WORD w = PLAYER.rgwCooperativeMagic[wPlayerRole];
for (int i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
if (gEquipmentEffect[i].rgwCooperativeMagic[wPlayerRole] != 0)
w = gEquipmentEffect[i].rgwCooperativeMagic[wPlayerRole];
return w;
}
BOOL PAL_PlayerCanAttackAll(WORD wPlayerRole) {
for (int i = 0; i <= MAX_PLAYER_EQUIPMENTS; i++)
if (gEquipmentEffect[i].rgwAttackAll[wPlayerRole] != 0)
return TRUE;
return FALSE;
}
BOOL PAL_AddMagic(WORD wPlayerRole, WORD wMagic) {
for (int i = 0; i < MAX_PLAYER_MAGICS; i++)
if (PLAYER.rgwMagic[i][wPlayerRole] == wMagic)
return FALSE;
int i;
for (i = 0; i < MAX_PLAYER_MAGICS; i++)
if (PLAYER.rgwMagic[i][wPlayerRole] == 0)
break;
if (i >= MAX_PLAYER_MAGICS)
return FALSE;
PLAYER.rgwMagic[i][wPlayerRole] = wMagic;
return TRUE;
}
VOID PAL_RemoveMagic(WORD wPlayerRole, WORD wMagic) {
for (int i = 0; i < MAX_PLAYER_MAGICS; i++) {
if (PLAYER.rgwMagic[i][wPlayerRole] == wMagic) {
PLAYER.rgwMagic[i][wPlayerRole] = 0;
break;
}
}
}
BOOL PAL_SetPlayerStatus(WORD wPlayerRole, WORD wStatusID, WORD wNumRound) {
switch (wStatusID) {
case kStatusConfused:
case kStatusSleep:
case kStatusSilence:
case kStatusParalyzed:
// for "bad" statuses, don't set the status when we already have it
if (gPlayerStatus[wPlayerRole][wStatusID] == 0)
gPlayerStatus[wPlayerRole][wStatusID] = wNumRound;
return TRUE;
case kStatusPuppet:
// only allow dead players for "puppet" status
if (PLAYER.rgwHP[wPlayerRole] == 0) {
if (gPlayerStatus[wPlayerRole][wStatusID] < wNumRound) {
gPlayerStatus[wPlayerRole][wStatusID] = wNumRound;
}
return TRUE;
} else {
return FALSE;
}
case kStatusBravery:
case kStatusProtect:
case kStatusDualAttack:
case kStatusHaste:
// for "good" statuses, reset the status if the status to be set lasts longer
if (PLAYER.rgwHP[wPlayerRole] != 0 && gPlayerStatus[wPlayerRole][wStatusID] < wNumRound) {
gPlayerStatus[wPlayerRole][wStatusID] = wNumRound;
}
return TRUE;
default:
assert(FALSE);
return FALSE;
}
}
VOID PAL_RemovePlayerStatus(WORD wPlayerRole, WORD wStatusID) {
// Don't remove effects of equipments (两次攻击)
if (gPlayerStatus[wPlayerRole][wStatusID] <= 999)
gPlayerStatus[wPlayerRole][wStatusID] = 0;
}
VOID PAL_ClearAllPlayerStatus(VOID) {
for (int i = 0; i < MAX_PLAYER_ROLES; i++)
for (int j = 0; j < kStatusAll; j++)
// Don't remove effects of equipments (两次攻击)
if (gPlayerStatus[i][j] <= 999)
gPlayerStatus[i][j] = 0;
}
VOID PAL_PlayerLevelUp(WORD wPlayerRole, WORD wNumLevel) {
// Add the level
PLAYER.rgwLevel[wPlayerRole] += wNumLevel;
if (PLAYER.rgwLevel[wPlayerRole] > MAX_LEVELS)
PLAYER.rgwLevel[wPlayerRole] = MAX_LEVELS;
// Increase player's stats
for (WORD i = 0; i < wNumLevel; i++) {
PLAYER.rgwMaxHP[wPlayerRole] += 10 + RandomLong(0, 7);
PLAYER.rgwMaxMP[wPlayerRole] += 8 + RandomLong(0, 5);
PLAYER.rgwAttackStrength[wPlayerRole] += 4 + RandomLong(0, 1);
PLAYER.rgwMagicStrength[wPlayerRole] += 4 + RandomLong(0, 1);
PLAYER.rgwDefense[wPlayerRole] += 2 + RandomLong(0, 1);
PLAYER.rgwDexterity[wPlayerRole] += 2 + RandomLong(0, 1);
PLAYER.rgwFleeRate[wPlayerRole] += 2;
}
#define STAT_LIMIT(t) \
{ \
if ((t) > 999) \
(t) = 999; \
}
STAT_LIMIT(PLAYER.rgwMaxHP[wPlayerRole]);
STAT_LIMIT(PLAYER.rgwMaxMP[wPlayerRole]);
STAT_LIMIT(PLAYER.rgwAttackStrength[wPlayerRole]);
STAT_LIMIT(PLAYER.rgwMagicStrength[wPlayerRole]);
STAT_LIMIT(PLAYER.rgwDefense[wPlayerRole]);
STAT_LIMIT(PLAYER.rgwDexterity[wPlayerRole]);
STAT_LIMIT(PLAYER.rgwFleeRate[wPlayerRole]);
#undef STAT_LIMIT
// Reset experience points to zero
g.Exp.rgPrimaryExp[wPlayerRole].wExp = 0;
g.Exp.rgPrimaryExp[wPlayerRole].wLevel = PLAYER.rgwLevel[wPlayerRole];
}
BOOL PAL_collectItem() {
if (g.wCollectValue == 0)
return FALSE;
int i = RandomLong(1, min(9, g.wCollectValue));
g.wCollectValue -= i;
WORD wObject = STORE[0].rgwItems[i - 1];
PAL_AddItemToInventory(wObject, 1);
PAL_SetDialogShadow(5);
PAL_StartDialogWithOffset(kDialogCenterWindow, 0, 0, FALSE, 0, -10);
WCHAR s[256];
PAL_swprintf(s, sizeof(s) / sizeof(WCHAR), L"%ls@%ls@", PAL_GetWord(42), PAL_GetWord(wObject));
LPCBITMAPRLE bg = PAL_GetUISprite(SPRITENUM_ITEMBOX);
INT width = PAL_RLEGetWidth(bg), height = PAL_RLEGetHeight(bg);
INT x = (320 - width) / 2, y = (200 - height) / 2;
PAL_POS pos = PAL_XY(x, y);
SDL_Rect rect = {x, y, width, height};
PAL_RLEBlitToSurface(bg, gpScreen, pos);
BYTE bufImage[2048];
if (PAL_MKFReadChunk(bufImage, 2048, OBJECT[wObject].item.wBitmap, FP_BALL) > 0) {
PAL_RLEBlitToSurface(bufImage, gpScreen, PAL_XY(PAL_X(pos) + 8, PAL_Y(pos) + 7));
}
VIDEO_UpdateScreen(&rect);
PAL_ShowDialogText(s);
PAL_SetDialogShadow(0);
return TRUE;
}