/
ck_keen.c
2021 lines (1815 loc) · 51.2 KB
/
ck_keen.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
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Omnispeak: A Commander Keen Reimplementation
Copyright (C) 2012 David Gow <david@ingeniumdigital.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "ck_act.h"
#include "ck_def.h"
#include "ck_phys.h"
#include "ck_play.h"
// TODO: Handle multiple episodes in some way
#include "ck4_ep.h"
#include "ck5_ep.h"
#include "id_ca.h"
#include "id_heads.h"
#include "id_in.h"
#include "id_rf.h"
#include "id_sd.h"
#include "id_ti.h"
#include "ck_cross.h"
// For all the shitty debug stuff I have.
#include <stdio.h>
#include "id_vl.h"
void CK_SpawnKeen(int tileX, int tileY, int direction);
extern CK_object *ck_keenObj;
CK_keenState ck_keenState;
void CK_BasicDrawFunc1(CK_object *obj);
soundnames *ck_itemSounds;
uint16_t ck_itemPoints[] = {0, 0, 0, 0, 100, 200, 500, 1000, 2000, 5000, 0, 0, 0};
uint16_t *ck_itemShadows;
// Episode dependent stuff
int16_t ck_keenMoon;
extern void CK6_ToggleBigSwitch(CK_object *obj, bool p);
void CK_KeenColFunc(CK_object *a, CK_object *b)
{
if (b->type == CT_CLASS(Item))
{
if ((ck_currentEpisode->ep == EP_CK4 && b->user1 > 11) ||
(ck_currentEpisode->ep == EP_CK5 && b->user1 > 12))
return;
SD_PlaySound(ck_itemSounds[b->user1]);
b->type = 1;
b->zLayer = 3;
b->gfxChunk = ck_itemShadows[b->user1];
CK_IncreaseScore(ck_itemPoints[b->user1]);
//b->yDirection = -1;
if (b->user1 < 4)
{
ck_gameState.keyGems[b->user1]++;
}
else if (b->user1 == 10)
{
ck_gameState.numLives++;
}
else if (b->user1 == 11)
{
ck_gameState.numShots += (ck_gameState.difficulty == D_Easy) ? 8 : 5;
}
else if ((ck_currentEpisode->ep == EP_CK5) && (b->user1 == 12))
{
ck_gameState.ep.ck5.securityCard = 1;
}
CK_SetAction2(b, CK_GetActionByName("CK_ACT_itemNotify"));
}
else if (b->type == CT_CLASS(Platform)) //Platform
{
impossibleBullet:
if (!ck_keenState.platform)
CK_PhysPushY(a, b);
}
else if (ck_currentEpisode->ep == EP_CK4)
{
if (b->type == CT4_Foot)
{
ck_gameState.levelState = LS_Foot;
}
else if (b->type == CT4_StunnedCreature && b->user4 == CT4_Bounder)
{
if (!ck_keenState.platform)
CK_PhysPushY(a, b);
}
else if (b->type == CT4_Lindsey)
{
CK4_ShowPrincessMessage();
CK_RemoveObj(b);
// RF_ForceRefresh();
}
else if (b->type == CT4_CouncilMember)
{
if (a->topTI)
{
if (ca_mapOn == 14) // Janitor level
{
CK4_ShowJanitorMessage();
//RF_ForceRefresh();
CK_RemoveObj(b);
}
else
{
SD_PlaySound(SOUND_COUNCILSAVE);
ck_gameState.levelState = LS_CouncilRescued;
}
}
}
else if (b->type == CT4_Bounder)
{
CK_PhysPushXY(a, b, false);
}
}
else if (ck_currentEpisode->ep == EP_CK5)
{
}
else if (ck_currentEpisode->ep == EP_CK6)
{
if (b->type == CT_Stunner)
{
// Reflected off of flect
if (b->user4)
{
CK_ShotHit(b);
CK_SetAction2(a, CK_GetActionByName("CK6_ACT_keenStunned0"));
}
if (CFG_GetConfigBool("impossibleBullet", true))
goto impossibleBullet;
// The legendary bug! For some reason, the control flows to check if
// keen is riding a platform he contacts his own stunner bullet.
}
}
}
int ck_KeenRunXVels[8] = {0, 0, 4, 4, 8, -4, -4, -8};
int ck_KeenPoleOffs[3] = {-8, 0, 8};
void CK_IncreaseScore(int score)
{
ck_gameState.keenScore += score;
if (IN_DemoGetMode() != IN_Demo_Off)
return;
if (ck_gameState.keenScore > ck_gameState.nextKeenAt)
{
SD_PlaySound(SOUND_GOTEXTRALIFE);
ck_gameState.numLives++;
ck_gameState.nextKeenAt *= 2;
}
}
void CK_SpawnKeen(int tileX, int tileY, int direction)
{
ck_keenObj->type = CT_Player; //TODO: obj_keen
ck_keenObj->active = OBJ_ALWAYS_ACTIVE;
ck_keenObj->visible = true;
ck_keenObj->zLayer = 1;
ck_keenObj->clipped = CLIP_normal;
ck_keenObj->posX = RF_TileToUnit(tileX);
ck_keenObj->posY = RF_TileToUnit(tileY) - 241;
ck_keenObj->xDirection = direction;
ck_keenObj->yDirection = 1;
CK_SetAction(ck_keenObj, CK_GetActionByName("CK_ACT_keenStanding"));
}
static uint16_t emptyTile = 0;
void CK_KeenGetTileItem(int tileX, int tileY, int itemNumber)
{
RF_ReplaceTiles(&emptyTile, 1, tileX, tileY, 1, 1);
SD_PlaySound(ck_itemSounds[itemNumber]);
CK_IncreaseScore(ck_itemPoints[itemNumber]);
// TODO: Handle more kinds of pick-ups
if (itemNumber == 11)
{
ck_gameState.numShots += (ck_gameState.difficulty == D_Easy) ? 8 : 5;
}
CK_object *notify = CK_GetNewObj(true);
notify->type = 1;
notify->zLayer = 3;
notify->posX = RF_TileToUnit(tileX);
notify->posY = RF_TileToUnit(tileY);
notify->yDirection = -1;
notify->user2 = ck_itemShadows[itemNumber];
notify->gfxChunk = notify->user2;
CK_SetAction(notify, CK_GetActionByName("CK_ACT_itemNotify"));
notify->clipped = CLIP_not;
}
void CK_KeenGetTileCentilife(int tileX, int tileY)
{
RF_ReplaceTiles(&emptyTile, 1, tileX, tileY, 1, 1);
SD_PlaySound(SOUND_GOTCENTILIFE);
CK_SpawnCentilifeNotify(tileX, tileY);
if (++ck_gameState.numCentilife == 100)
{
ck_gameState.numCentilife = 0;
SD_PlaySound(SOUND_GOTEXTRALIFE);
ck_gameState.numLives++;
// Spawn a 1up shadow
CK_object *obj = CK_GetNewObj(true);
obj->type = CT_Friendly;
obj->zLayer = PRIORITIES - 1;
obj->posX = RF_TileToUnit(tileX);
obj->posY = RF_TileToUnit(tileY);
obj->yDirection = IN_motion_Up;
obj->user2 = obj->gfxChunk = SPR_CENTILIFE1UPSHADOW;
CK_SetAction(obj, CK_GetActionByName("CK_ACT_itemNotify"));
obj->clipped = CLIP_not;
}
}
void CK_KeenCheckSpecialTileInfo(CK_object *obj)
{
if (ck_keenMoon == 1)
ck_keenMoon = 0; // Yes, also set in Keen 5
for (int y = obj->clipRects.tileY1; y <= obj->clipRects.tileY2; ++y)
{
for (int x = obj->clipRects.tileX1; x <= obj->clipRects.tileX2; ++x)
{
int specialTileInfo = (TI_ForeMisc(CA_TileAtPos(x, y, 1)) & 0x7F);
switch (specialTileInfo)
{
case 0:
break;
case MISCFLAG_DEADLY:
CK_KillKeen();
break;
case 4:
CK_KeenGetTileCentilife(x, y);
break;
case MISCFLAG_GEMHOLDER0:
case MISCFLAG_GEMHOLDER1:
case MISCFLAG_GEMHOLDER2:
case MISCFLAG_GEMHOLDER3:
if (y == obj->clipRects.tileY2 && obj->topTI && obj->currentAction != CK_GetActionByName("CK_ACT_keenPlaceGem") && ck_gameState.keyGems[specialTileInfo - MISCFLAG_GEMHOLDER0])
{
int targetXUnit = RF_TileToUnit(x) - 64;
if (obj->posX == targetXUnit)
{
ck_gameState.keyGems[specialTileInfo - MISCFLAG_GEMHOLDER0]--;
CK_SetAction2(obj, CK_GetActionByName("CK_ACT_keenPlaceGem"));
}
else
{
obj->user1 = targetXUnit;
obj->currentAction = CK_GetActionByName("CK_ACT_keenSlide");
}
}
return;
case 16:
if (ck_keenMoon == 0)
ck_keenMoon = 1; // Yes, also set in Keen 5
break;
case 21:
case 22:
case 23:
case 24:
case 25:
case 26:
case 27:
case 28:
case 29:
CK_KeenGetTileItem(x, y, specialTileInfo - 17);
/*CK_KeenGetTileItem(x,y,specialTileInfo - 21 + 4);*/
break;
}
}
}
}
void CK_KeenPressSwitchThink(CK_object *obj)
{
uint16_t switchTarget = CA_TileAtPos(obj->clipRects.tileXmid, obj->clipRects.tileY1, 2);
int switchTargetX = (switchTarget >> 8);
int switchTargetY = (switchTarget & 0xFF);
uint16_t switchTile = CA_TileAtPos(obj->clipRects.tileXmid, obj->clipRects.tileY1, 1);
uint8_t switchMisc = TI_ForeMisc(switchTile);
// Toggle the switch.
uint16_t switchNextTile = switchTile + TI_ForeAnimTile(switchTile);
RF_ReplaceTiles(&switchNextTile, 1, obj->clipRects.tileXmid, obj->clipRects.tileY1, 1, 1);
SD_PlaySound(SOUND_KEENOUTOFAMMO);
if (switchMisc == MISCFLAG_SWITCHBRIDGE)
{
for (int tileY = switchTargetY; tileY < switchTargetY + 2; ++tileY)
{
for (int tileX = switchTargetX - ((tileY == switchTargetY) ? 0 : 1); tileX < CA_GetMapWidth(); ++tileX)
{
uint16_t currentTile = CA_TileAtPos(tileX, tileY, 1);
if (!TI_ForeAnimTile(currentTile))
break;
uint16_t newTile = currentTile + TI_ForeAnimTile(currentTile);
RF_ReplaceTiles(&newTile, 1, tileX, tileY, 1, 1);
}
}
}
else
{
int infoPlaneInverses[8] = {2, 3, 0, 1, 6, 7, 4, 5};
uint16_t infoPlaneValue = CA_TileAtPos(switchTargetX, switchTargetY, 2);
if (infoPlaneValue >= 91 && infoPlaneValue < 99)
{
// Invert the direction of the goplat arrow.
infoPlaneValue = infoPlaneInverses[infoPlaneValue - 91] + 91;
}
else
{
// Insert or remove a [B] block.
infoPlaneValue ^= 0x1F;
}
CA_mapPlanes[2][switchTargetY * CA_GetMapWidth() + switchTargetX] = infoPlaneValue;
}
}
bool CK_KeenPressUp(CK_object *obj)
{
uint8_t tileMiscFlag = TI_ForeMisc(CA_TileAtPos(obj->clipRects.tileXmid, obj->clipRects.tileY1, 1));
// Are we pressing a switch?
if (tileMiscFlag == MISCFLAG_SWITCHPLATON || tileMiscFlag == MISCFLAG_SWITCHPLATOFF || tileMiscFlag == MISCFLAG_SWITCHBRIDGE)
{
int16_t destXunit = RF_TileToUnit(obj->clipRects.tileXmid) - 64;
if (obj->posX == destXunit)
{
// Flip that switch!
obj->currentAction = CK_GetActionByName("CK_ACT_keenPressSwitch1");
}
else
{
obj->user1 = destXunit;
obj->currentAction = CK_GetActionByName("CK_ACT_keenSlide");
}
ck_keenState.keenSliding = true;
return true;
}
// Are we enterting a door?
if (tileMiscFlag == MISCFLAG_DOOR || tileMiscFlag == MISCFLAG_SECURITYDOOR)
{
uint16_t destUnitX = RF_TileToUnit(obj->clipRects.tileXmid) + 96;
// If the door is two tiles wide, we want to be in the centre.
uint8_t miscFlagLeft = TI_ForeMisc(CA_TileAtPos(obj->clipRects.tileXmid - 1, obj->clipRects.tileY1, 1));
if (miscFlagLeft == MISCFLAG_DOOR || miscFlagLeft == MISCFLAG_SECURITYDOOR)
{
destUnitX -= 256;
}
if (obj->posX == destUnitX)
{
//We're at the door.
// Is it a security door?
if (tileMiscFlag == MISCFLAG_SECURITYDOOR)
{
if (ck_gameState.ep.ck5.securityCard)
{
ck_gameState.ep.ck5.securityCard = 0;
SD_PlaySound(SOUND_OPENSECURITYDOOR);
CK_object *newObj = CK_GetNewObj(false);
newObj->posX = obj->clipRects.tileXmid - 2;
newObj->posY = obj->clipRects.tileY2 - 4;
newObj->active = OBJ_ALWAYS_ACTIVE;
newObj->type = 1;
CK_SetAction(newObj, CK_GetActionByName("CK_ACT_SecurityDoorOpen"));
obj->currentAction = CK_GetActionByName("CK_ACT_keenEnterSecurityDoor");
obj->zLayer = 0;
ck_keenState.keenSliding = true;
return true;
}
else
{
SD_PlaySound(SOUND_NEEDKEYCARD);
obj->currentAction = CK_GetActionByName("CK_ACT_keenStanding");
ck_keenState.keenSliding = true;
return false;
}
}
else
{
ck_invincibilityTimer = 110;
obj->currentAction = CK_GetActionByName("CK_ACT_keenEnterDoor0");
obj->zLayer = 0;
if ((ck_currentEpisode->ep == EP_CK5) && !CA_TileAtPos(obj->clipRects.tileXmid, obj->clipRects.tileY1, 2))
{
CK5_SpawnLightning();
}
}
}
else
{
obj->user1 = destUnitX;
obj->currentAction = CK_GetActionByName("CK_ACT_keenSlide");
}
ck_keenState.keenSliding = true;
return true;
}
// No? Return to our caller, who will handle poles/looking up.
return false;
}
// Think function for keen "sliding" towards a switch, keygem or door.
void CK_KeenSlide(CK_object *obj)
{
int16_t deltaX = obj->user1 - obj->posX;
if (deltaX < 0)
{
// Move left one px per tick.
ck_nextX -= SD_GetSpriteSync() * 16;
// If we're not at our target yet, return.
if (ck_nextX > deltaX)
return;
}
else if (deltaX > 0)
{
// Move right one px per tick.
ck_nextX += SD_GetSpriteSync() * 16;
// If we're not at our target yet, return.
if (ck_nextX < deltaX)
return;
}
// We're at our target.
ck_nextX = deltaX;
obj->user1 = 0;
if (!CK_KeenPressUp(obj))
{
obj->currentAction = CK_GetActionByName("CK_ACT_keenStanding");
}
}
void CK_KeenEnterDoor0(CK_object *obj)
{
SD_PlaySound(SOUND_KEENWALK0);
}
void CK_KeenEnterDoor1(CK_object *obj)
{
SD_PlaySound(SOUND_KEENWALK1);
}
// Think function for entering a door.
void CK_KeenEnterDoor(CK_object *obj)
{
uint16_t destination = CA_TileAtPos(obj->clipRects.tileX1, obj->clipRects.tileY2, 2);
if (ck_currentEpisode->ep == EP_CK5)
{
if (destination == 0x0000)
{
ck_gameState.levelState = LS_TeleportToKorath;
obj->currentAction = CK_GetActionByName("CK_ACT_keenEnteredDoor");
return;
}
if (destination == 0xB1B1)
{
ck_gameState.levelState = LS_LevelComplete;
obj->currentAction = CK_GetActionByName("CK_ACT_keenEnteredDoor");
return;
}
}
obj->posY = RF_TileToUnit((destination & 0xFF)) - 256 + 15;
obj->posX = RF_TileToUnit((destination >> 8));
obj->zLayer = 1;
obj->clipped = CLIP_not;
CK_SetAction2(obj, obj->currentAction->next);
obj->clipped = CLIP_normal;
CK_CentreCamera(obj);
}
void CK_KeenPlaceGem(CK_object *obj)
{
uint16_t oldFGTile = CA_TileAtPos(obj->clipRects.tileXmid, obj->clipRects.tileY2, 1);
uint16_t newFGTile = oldFGTile + 18;
uint16_t target = CA_TileAtPos(obj->clipRects.tileXmid, obj->clipRects.tileY2, 2);
int targetX = target >> 8;
int targetY = target & 0xFF;
RF_ReplaceTiles(&newFGTile, 1, obj->clipRects.tileXmid, obj->clipRects.tileY2, 1, 1);
SD_PlaySound(SOUND_OPENGEMDOOR);
CK_object *newObj = CK_GetNewObj(false);
newObj->posX = targetX;
newObj->posY = targetY;
if (targetX > CA_GetMapWidth() || targetX < 2 || targetY > CA_GetMapHeight() || targetY < 2)
{
Quit("Keyholder points to a bad spot!");
}
// Calculate the height of the door.
int doorHeight = 0;
int doorY = targetY;
uint16_t doorTile = CA_TileAtPos(targetX, doorY, 1);
do
{
doorHeight++;
doorY++;
} while (CA_TileAtPos(targetX, doorY, 1) == doorTile);
newObj->user1 = doorHeight;
newObj->clipped = CLIP_not;
newObj->type = CT_Friendly;
newObj->active = OBJ_ALWAYS_ACTIVE;
CK_SetAction(newObj, CK_GetActionByName("CK_ACT_DoorOpen1"));
}
void CK_KeenRidePlatform(CK_object *obj)
{
// Save the platform pointer, we might be wiping it.
CK_object *plat = ck_keenState.platform;
if (obj->clipRects.unitX2 < plat->clipRects.unitX1 || obj->clipRects.unitX1 > plat->clipRects.unitX2)
{
// We've fallen off the platform horizontally.
ck_keenState.platform = 0;
return;
}
if (obj->deltaPosY < 0)
{
// If we've jumped off the platform.
ck_keenState.platform = 0;
if (plat->deltaPosY < 0)
{
ck_nextX = 0;
ck_nextY = plat->deltaPosY;
CK_PhysUpdateSimpleObj(obj);
return;
}
}
else
{
//Ride the platform
ck_nextX = plat->deltaPosX;
ck_nextY = plat->clipRects.unitY1 - obj->clipRects.unitY2 - 16;
CK_PhysUpdateSimpleObj(obj);
if (vl_noPan)
{
// Keen has a "/NOPAN" parameter which disables use of the EGA panning
// register. In order to make platforms less ugly when this is on
// (though not much less ugly, IMHO), clamp Keen's movement to the nearest
// 8px, so that scrolling timing is aligned with the platform.
// This causes a bug where you can't always walk off bounders, see:
// https://pckf.com/viewtopic.php?p=102264
// https://www.youtube.com/watch?v=cScZlYPhFnU
obj->posX &= ~0x7F;
obj->posX |= plat->posX & 0x7F;
}
else
{
// Make Keen's x-position move in tandem with the platform, so that they appear to move as a unit,
// and (more importantly) that the platform moves in time with the camera scrolling (which is based
// on Keen's position).
// Technically, this probably should clear out the low bits like the /NOPAN case, so that their subpixel
// values are identical.
obj->posX |= plat->posX & 0x1F;
}
// We've hit the ceiling?
if (obj->bottomTI)
{
ck_keenState.platform = 0;
return;
}
// We're standing on something, don't fall down!
obj->topTI = 0x19;
}
}
bool CK_KeenTryClimbPole(CK_object *obj)
{
if (SD_GetLastTimeCount() < ck_keenState.poleGrabTime)
ck_keenState.poleGrabTime = 0;
else if (SD_GetLastTimeCount() - ck_keenState.poleGrabTime < 19)
return false;
uint16_t candidateTile = CA_TileAtPos(obj->clipRects.tileXmid,
((ck_inputFrame.yDirection == -1) ? (RF_UnitToTile(obj->clipRects.unitY1 + 96)) : (obj->clipRects.tileY2 + 1)), 1);
if ((TI_ForeMisc(candidateTile) & 0x7F) == 1)
{
obj->posX = 128 + RF_TileToUnit(obj->clipRects.tileXmid - 1);
ck_nextX = 0;
ck_nextY = (ck_inputFrame.yDirection << 5);
obj->clipped = CLIP_not;
obj->currentAction = CK_GetActionByName("CK_ACT_keenPoleSit");
return true;
}
return false;
}
void CK_HandleInputOnGround(CK_object *obj);
void CK_KeenRunningThink(CK_object *obj)
{
if (!ck_inputFrame.xDirection)
{
obj->currentAction = CK_GetActionByName("CK_ACT_keenStanding");
CK_HandleInputOnGround(obj);
return;
}
obj->xDirection = ck_inputFrame.xDirection;
if (ck_inputFrame.yDirection == -1)
{
if (CK_KeenTryClimbPole(obj))
return;
if (ck_keenState.keenSliding || CK_KeenPressUp(obj))
return;
}
else if (ck_inputFrame.yDirection == 1)
{
if (CK_KeenTryClimbPole(obj))
return;
}
if (ck_keenState.shootIsPressed && !ck_keenState.shootWasPressed)
{
ck_keenState.shootWasPressed = true;
if (ck_inputFrame.yDirection == -1)
{
obj->currentAction = CK_GetActionByName("CK_ACT_keenShootUp1");
}
else
{
obj->currentAction = CK_GetActionByName("CK_ACT_keenShoot1");
}
return;
}
if (ck_keenState.jumpIsPressed && !ck_keenState.jumpWasPressed)
{
ck_keenState.jumpWasPressed = true;
SD_PlaySound(SOUND_KEENJUMP);
obj->velX = obj->xDirection * 16;
obj->velY = -40;
ck_nextX = ck_nextY = 0;
ck_keenState.jumpTimer = 18;
obj->currentAction = CK_GetActionByName("CK_ACT_keenJump1");
// Is this the mystical 'impossible pogo'?
// k5disasm has a 'return' here, probably a mistake?
}
if (ck_keenState.pogoIsPressed && !ck_keenState.pogoWasPressed)
{
ck_keenState.pogoWasPressed = true;
obj->currentAction = CK_GetActionByName("CK_ACT_keenPogo1");
SD_PlaySound(SOUND_KEENJUMP);
obj->velX = obj->xDirection * 16;
obj->velY = -48;
// Should this be nextY? Andy seems to think so, but lemm thinks that X is right...
ck_nextX = 0;
ck_keenState.jumpTimer = 24;
return;
}
// Andy seems to think this is Y as well. Need to do some more disasm.
// If this is an X vel, then surely we'd want to multiply it by the direction?
ck_nextX += ck_KeenRunXVels[obj->topTI & 7] * SD_GetSpriteSync();
if ((obj->currentAction->chunkLeft == CK_GetActionByName("CK_ACT_keenRun1")->chunkLeft) && !(obj->user3))
{
SD_PlaySound(SOUND_KEENWALK0);
obj->user3 = 1;
return;
}
if ((obj->currentAction->chunkLeft == CK_GetActionByName("CK_ACT_keenRun3")->chunkLeft) && !(obj->user3))
{
SD_PlaySound(SOUND_KEENWALK1);
obj->user3 = 1;
return;
}
if ((obj->currentAction->chunkLeft == CK_GetActionByName("CK_ACT_keenRun2")->chunkLeft) || (obj->currentAction->chunkLeft == CK_GetActionByName("CK_ACT_keenRun4")->chunkLeft))
{
obj->user3 = 0;
}
}
void CK_HandleInputOnGround(CK_object *obj)
{
// If we're riding a platform, do it surfin' style!
if (obj->topTI == 0x19)
{
// But only if such an action exists in this episode. :/
if (CK_GetActionByName("CK_ACT_keenRidePlatform"))
{
obj->currentAction = CK_GetActionByName("CK_ACT_keenRidePlatform");
}
}
if (ck_inputFrame.xDirection)
{
obj->currentAction = CK_GetActionByName("CK_ACT_keenRun1");
CK_KeenRunningThink(obj);
ck_nextX = (obj->xDirection * obj->currentAction->velX * SD_GetSpriteSync()) / 4;
return;
}
if (ck_keenState.shootIsPressed && !ck_keenState.shootWasPressed)
{
ck_keenState.shootWasPressed = true;
if (ck_inputFrame.yDirection == -1)
{
obj->currentAction = CK_GetActionByName("CK_ACT_keenShootUp1");
}
else
{
obj->currentAction = CK_GetActionByName("CK_ACT_keenShoot1");
}
return;
}
if (ck_keenState.jumpIsPressed && !ck_keenState.jumpWasPressed)
{
ck_keenState.jumpWasPressed = true;
SD_PlaySound(SOUND_KEENJUMP);
obj->velX = 0;
obj->velY = -40;
ck_nextY = 0;
ck_keenState.jumpTimer = 18;
obj->currentAction = CK_GetActionByName("CK_ACT_keenJump1");
return;
}
if (ck_keenState.pogoIsPressed && !ck_keenState.pogoWasPressed)
{
ck_keenState.pogoWasPressed = true;
SD_PlaySound(SOUND_KEENJUMP);
obj->currentAction = CK_GetActionByName("CK_ACT_keenPogo1");
obj->velX = 0;
obj->velY = -48;
ck_nextY = 0;
ck_keenState.jumpTimer = 24;
return;
}
if (ck_inputFrame.yDirection == -1)
{
if (CK_KeenTryClimbPole(obj))
return;
if (!ck_keenState.keenSliding && CK_KeenPressUp(obj))
return;
obj->currentAction = CK_GetActionByName("CK_ACT_keenLookUp1");
}
else if (ck_inputFrame.yDirection == 1)
{
// Try poles.
if (CK_KeenTryClimbPole(obj))
return;
// Keen looks down.
obj->currentAction = CK_GetActionByName("CK_ACT_keenLookDown1");
return;
}
}
void CK_KeenStandingThink(CK_object *obj)
{
// If we're riding a platform, do it surfin' style!
if (obj->topTI == 0x19)
{
// But only if such an action exists in this episode. :/
if (CK_GetActionByName("CK_ACT_keenRidePlatform"))
{
obj->currentAction = CK_GetActionByName("CK_ACT_keenRidePlatform");
}
}
if (ck_inputFrame.xDirection || ck_inputFrame.yDirection || ck_keenState.jumpIsPressed || ck_keenState.pogoIsPressed || ck_keenState.shootIsPressed)
{
obj->user1 = obj->user2 = 0; //Idle Time + Idle State
obj->currentAction = CK_GetActionByName("CK_ACT_keenStanding");
CK_HandleInputOnGround(obj);
return;
}
//If not on platform
if ((obj->topTI & ~7) != 0x18)
obj->user1 += SD_GetSpriteSync();
if (obj->user2 == 0 && obj->user1 > 200)
{
obj->user2++;
obj->currentAction = CK_GetActionByName("CK_ACT_keenIdle");
obj->user1 = 0;
return;
}
if (obj->user2 == 1 && obj->user1 > 300)
{
obj->user2++;
if ((ck_currentEpisode->ep == EP_CK4) && (ck_keenMoon == 1))
{
ck_keenMoon = 2;
obj->currentAction = CK_GetActionByName("CK_ACT_keenMoon1");
}
else
{
obj->currentAction = CK_GetActionByName("CK_ACT_keenBored1");
}
obj->user1 = 0;
return;
}
if (obj->user2 == 2 && obj->user1 > 700)
{
obj->user2++;
obj->currentAction = CK_GetActionByName("CK_ACT_keenOpenBook1");
obj->user1 = 0;
}
}
void CK_KeenLookUpThink(CK_object *obj)
{
if (ck_inputFrame.yDirection != -1 ||
ck_inputFrame.xDirection != 0 ||
(ck_keenState.jumpIsPressed && !ck_keenState.jumpWasPressed) ||
(ck_keenState.pogoIsPressed && !ck_keenState.pogoWasPressed) ||
(ck_keenState.shootIsPressed))
{
obj->currentAction = CK_GetActionByName("CK_ACT_keenStanding");
CK_HandleInputOnGround(obj);
}
}
void CK_KeenLookDownThink(CK_object *obj)
{
//Try to jump down
if (ck_keenState.jumpIsPressed && !ck_keenState.jumpWasPressed && (obj->topTI & 7) == 1)
{
ck_keenState.jumpWasPressed = true;
//If the tiles below the player are blocking on any side but the top, they cannot be jumped through
int tile1 = CA_TileAtPos(obj->clipRects.tileXmid, obj->clipRects.tileY2, 1);
int tile2 = CA_TileAtPos(obj->clipRects.tileXmid, obj->clipRects.tileY2 + 1, 1);
if (TI_ForeLeft(tile1) || TI_ForeBottom(tile1) || TI_ForeRight(tile1))
return;
if (TI_ForeLeft(tile2) || TI_ForeBottom(tile2) || TI_ForeRight(tile2))
return;
uint16_t deltay = CK_Cross_max(SD_GetSpriteSync(), 4) << 4;
//Moving platforms
if (ck_keenState.platform)
deltay += ck_keenState.platform->deltaPosY;
ck_keenState.platform = 0;
obj->clipRects.unitY2 += deltay;
obj->posY += deltay;
ck_nextX = 0;
ck_nextY = 0;
obj->currentAction = CK_GetActionByName("CK_ACT_keenFall1");
obj->velX = obj->velY = 0;
SD_PlaySound(SOUND_KEENFALL);
return;
}
if (ck_inputFrame.yDirection != 1 || ck_inputFrame.xDirection != 0 || (ck_keenState.jumpIsPressed && !ck_keenState.jumpWasPressed) || (ck_keenState.pogoIsPressed && !ck_keenState.pogoWasPressed))
{
obj->currentAction = CK_GetActionByName("CK_ACT_keenLookDown4");
return;
}
}
// TODO: More to modify here
void CK_KeenDrawFunc(CK_object *obj)
{
if (!obj->topTI)
{
SD_PlaySound(SOUND_KEENFALL);
obj->velX = obj->xDirection * 8;
obj->velY = 0;
CK_SetAction2(obj, CK_GetActionByName("CK_ACT_keenFall1"));
ck_keenState.jumpTimer = 0;
}
else if ((obj->topTI & 0xFFF8) == 8)
{
CK_KillKeen();
}
else if (obj->topTI == 0x29)
{
// Keen6 conveyor belt right
ck_nextX = SD_GetSpriteSync() * 8;
ck_nextY = 0;
obj->user1 = 0;
CK_PhysUpdateNormalObj(obj);
}
else if (obj->topTI == 0x31)
{
// Keen6 conveyor belt left
ck_nextX = SD_GetSpriteSync() * -8;
ck_nextY = 0;
obj->user1 = 0;
CK_PhysUpdateNormalObj(obj);
}
RF_AddSpriteDraw(&obj->sde, obj->posX, obj->posY, obj->gfxChunk, 0, obj->zLayer);
}
// TODO: More to modify here
void CK_KeenRunDrawFunc(CK_object *obj)
{
if (!obj->topTI)
{
SD_PlaySound(SOUND_KEENFALL);
obj->velX = obj->xDirection * 8;
obj->velY = 0;
CK_SetAction2(obj, CK_GetActionByName("CK_ACT_keenFall1"));
ck_keenState.jumpTimer = 0;
}
else if ((obj->topTI & 0xFFF8) == 8)
{
CK_KillKeen();
}
else if (obj->topTI == 0x29)
{
// Keen6 conveyor belt right
ck_nextX = SD_GetSpriteSync() * 8;
ck_nextY = 0;
obj->user1 = 0;
CK_PhysUpdateNormalObj(obj);
}
else if (obj->topTI == 0x31)
{
// Keen6 conveyor belt left
ck_nextX = SD_GetSpriteSync() * -8;
ck_nextY = 0;
obj->user1 = 0;
CK_PhysUpdateNormalObj(obj);
}
// If we're running into a wall, we should stand still.
if ((obj->rightTI && obj->xDirection == -1) || (obj->leftTI && obj->xDirection == 1))
{
obj->actionTimer = 0;
obj->currentAction = CK_GetActionByName("CK_ACT_keenStanding");
obj->gfxChunk = (obj->xDirection == -1) ? obj->currentAction->chunkLeft : obj->currentAction->chunkRight;
}
RF_AddSpriteDraw(&obj->sde, obj->posX, obj->posY, obj->gfxChunk, 0, obj->zLayer);
}
void CK_KeenReadThink(CK_object *obj)
{
if (ck_inputFrame.xDirection != 0 || ck_keenState.jumpIsPressed || ck_keenState.pogoIsPressed)
{
obj->currentAction = CK_GetActionByName("CK_ACT_keenStowBook1");