/
character.cpp
4136 lines (3429 loc) · 147 KB
/
character.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
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
//=============================================================================
//
// Adventure Game Studio (AGS)
//
// Copyright (C) 1999-2011 Chris Jones and 2011-2024 various contributors
// The full list of copyright holders can be found in the Copyright.txt
// file, which is part of this source code distribution.
//
// The AGS source code is provided under the Artistic License 2.0.
// A copy of this license can be found in the file License.txt and at
// https://opensource.org/license/artistic-2-0/
//
//=============================================================================
//
// AGS Character functions
//
//=============================================================================
#include <cstdio>
#include "ac/character.h"
#include "ac/common.h"
#include "ac/gamesetupstruct.h"
#include "ac/display.h"
#include "ac/draw.h"
#include "ac/event.h"
#include "ac/game.h"
#include "ac/global_audio.h"
#include "ac/global_character.h"
#include "ac/global_game.h"
#include "ac/global_object.h"
#include "ac/global_region.h"
#include "ac/global_room.h"
#include "ac/global_translation.h"
#include "ac/gui.h"
#include "ac/lipsync.h"
#include "ac/mouse.h"
#include "ac/movelist.h"
#include "ac/object.h"
#include "ac/overlay.h"
#include "ac/properties.h"
#include "ac/room.h"
#include "ac/screenoverlay.h"
#include "ac/string.h"
#include "ac/system.h"
#include "ac/view.h"
#include "ac/viewframe.h"
#include "ac/walkablearea.h"
#include "gui/guimain.h"
#include "ac/route_finder.h"
#include "ac/gamestate.h"
#include "debug/debug_log.h"
#include "main/game_run.h"
#include "main/update.h"
#include "ac/spritecache.h"
#include "util/string_compat.h"
#include <math.h>
#include "gfx/graphicsdriver.h"
#include "script/runtimescriptvalue.h"
#include "ac/dynobj/cc_character.h"
#include "ac/dynobj/cc_inventory.h"
#include "ac/dynobj/dynobj_manager.h"
#include "script/script_runtime.h"
#include "gfx/gfx_def.h"
#include "media/audio/audio_system.h"
using namespace AGS::Common;
using namespace AGS::Engine;
extern GameSetupStruct game;
extern int displayed_room,starting_room;
extern RoomStruct thisroom;
extern std::vector<ViewStruct> views;
extern RoomObject*objs;
extern ScriptInvItem scrInv[MAX_INV];
extern SpriteCache spriteset;
extern Bitmap *walkable_areas_temp;
extern IGraphicsDriver *gfxDriver;
extern int said_speech_line;
extern int said_text;
extern CCCharacter ccDynamicCharacter;
extern CCInventory ccDynamicInv;
//--------------------------------
CharacterInfo*playerchar;
int32_t _sc_PlayerCharPtr = 0;
int char_lowest_yp;
// Sierra-style speech settings
int face_talking=-1,facetalkview=0,facetalkwait=0,facetalkframe=0;
int facetalkloop=0, facetalkrepeat = 0, facetalkAllowBlink = 1;
int facetalkBlinkLoop = 0;
CharacterInfo *facetalkchar = nullptr;
// Do override default portrait position during QFG4-style speech overlay update
bool facetalk_qfg4_override_placement_x = false;
bool facetalk_qfg4_override_placement_y = false;
// lip-sync speech settings
int loops_per_character, text_lips_offset, char_speaking = -1;
int char_thinking = -1;
const char *text_lips_text = nullptr;
std::vector<SpeechLipSyncLine> splipsync;
int numLipLines = 0, curLipLine = -1, curLipLinePhoneme = 0;
// **** CHARACTER: FUNCTIONS ****
bool is_valid_character(int char_id)
{
return ((char_id >= 0) && (char_id < game.numcharacters));
}
bool AssertCharacter(const char *apiname, int char_id)
{
if ((char_id >= 0) && (char_id < game.numcharacters))
return true;
debug_script_warn("%s: invalid character id %d (range is 0..%d)", apiname, char_id, game.numcharacters - 1);
return false;
}
void Character_AddInventory(CharacterInfo *chaa, ScriptInvItem *invi, int addIndex) {
int ee;
if (invi == nullptr)
quit("!AddInventoryToCharacter: invalid inventory number");
int inum = invi->id;
if (chaa->inv[inum] >= 32000)
quit("!AddInventory: cannot carry more than 32000 of one inventory item");
chaa->inv[inum]++;
int charid = chaa->index_id;
if (game.options[OPT_DUPLICATEINV] == 0) {
// Ensure it is only in the list once
for (ee = 0; ee < charextra[charid].invorder_count; ee++) {
if (charextra[charid].invorder[ee] == inum) {
// They already have the item, so don't add it to the list
if (chaa == playerchar)
run_on_event (GE_ADD_INV, RuntimeScriptValue().SetInt32(inum));
return;
}
}
}
if (charextra[charid].invorder_count >= MAX_INVORDER)
quit("!Too many inventory items added, max 500 display at one time");
if ((addIndex == SCR_NO_VALUE) ||
(addIndex >= charextra[charid].invorder_count) ||
(addIndex < 0)) {
// add new item at end of list
charextra[charid].invorder[charextra[charid].invorder_count] = inum;
}
else {
// insert new item at index
for (ee = charextra[charid].invorder_count - 1; ee >= addIndex; ee--)
charextra[charid].invorder[ee + 1] = charextra[charid].invorder[ee];
charextra[charid].invorder[addIndex] = inum;
}
charextra[charid].invorder_count++;
GUIE::MarkInventoryForUpdate(charid, charid == game.playercharacter);
if (chaa == playerchar)
run_on_event (GE_ADD_INV, RuntimeScriptValue().SetInt32(inum));
}
void Character_AddWaypoint(CharacterInfo *chaa, int x, int y) {
if (chaa->room != displayed_room)
quit("!MoveCharacterPath: specified character not in current room");
// not already walking, so just do a normal move
if (chaa->walking <= 0) {
Character_Walk(chaa, x, y, IN_BACKGROUND, ANYWHERE);
return;
}
MoveList *cmls = &mls[chaa->walking % TURNING_AROUND];
if (cmls->numstage >= MAXNEEDSTAGES)
{
debug_script_warn("Character::AddWaypoint: move is too complex, cannot add any further paths");
return;
}
// They're already walking there anyway
const Point &last_pos = cmls->GetLastPos();
if (last_pos == Point(x, y))
return;
int move_speed_x, move_speed_y;
chaa->get_effective_walkspeeds(move_speed_x, move_speed_y);
if ((move_speed_x == 0) && (move_speed_y == 0))
{
debug_script_warn("Character::AddWaypoint: called for '%s' with walk speed 0", chaa->scrname);
}
// There's an issue: the existing movelist is converted to room resolution,
// so we do this trick: convert last step to mask resolution, before calling
// a pathfinder api, and then we'll convert old and new last step back.
// TODO: figure out a better way of processing this!
const int last_stage = cmls->numstage - 1;
cmls->pos[last_stage] = { room_to_mask_coord(last_pos.X), room_to_mask_coord(last_pos.Y) };
const int dst_x = room_to_mask_coord(x);
const int dst_y = room_to_mask_coord(y);
if (add_waypoint_direct(cmls, dst_x, dst_y, move_speed_x, move_speed_y))
{
convert_move_path_to_room_resolution(cmls, last_stage, last_stage + 1);
}
}
void Character_Animate(CharacterInfo *chaa, int loop, int delay, int repeat,
int blocking, int direction, int sframe, int volume)
{
ValidateViewAnimVLF("Character.Animate", chaa->view, loop, sframe);
ValidateViewAnimParams("Character.Animate", repeat, blocking, direction);
animate_character(chaa, loop, delay, repeat, 0, direction, sframe, volume);
if (blocking != 0)
GameLoopUntilValueIsZero(&chaa->animating);
}
void Character_Animate5(CharacterInfo *chaa, int loop, int delay, int repeat, int blocking, int direction) {
Character_Animate(chaa, loop, delay, repeat, blocking, direction, 0 /* first frame */, 100 /* full volume */);
}
void Character_Animate6(CharacterInfo *chaa, int loop, int delay, int repeat, int blocking, int direction, int sframe) {
Character_Animate(chaa, loop, delay, repeat, blocking, direction, sframe, 100 /* full volume */);
}
void Character_ChangeRoomAutoPosition(CharacterInfo *chaa, int room, int newPos)
{
if (chaa->index_id != game.playercharacter)
{
quit("!Character.ChangeRoomAutoPosition can only be used with the player character.");
}
new_room_pos = newPos;
if (new_room_pos == 0) {
// auto place on other side of screen
if (chaa->x <= thisroom.Edges.Left + 10)
new_room_pos = 2000;
else if (chaa->x >= thisroom.Edges.Right - 10)
new_room_pos = 1000;
else if (chaa->y <= thisroom.Edges.Top + 10)
new_room_pos = 3000;
else if (chaa->y >= thisroom.Edges.Bottom - 10)
new_room_pos = 4000;
if (new_room_pos < 3000)
new_room_pos += chaa->y;
else
new_room_pos += chaa->x;
}
NewRoom(room);
}
void Character_ChangeRoom(CharacterInfo *chaa, int room, int x, int y) {
Character_ChangeRoomSetLoop(chaa, room, x, y, SCR_NO_VALUE);
}
void Character_ChangeRoomSetLoop(CharacterInfo *chaa, int room, int x, int y, int direction) {
if (chaa->index_id != game.playercharacter) {
// NewRoomNPC
if ((x != SCR_NO_VALUE) && (y != SCR_NO_VALUE)) {
chaa->x = x;
chaa->y = y;
if (direction != SCR_NO_VALUE && direction>=0) chaa->loop = direction;
}
chaa->prevroom = chaa->room;
chaa->room = room;
debug_script_log("%s moved to room %d, location %d,%d, loop %d",
chaa->scrname, room, chaa->x, chaa->y, chaa->loop);
return;
}
if ((x != SCR_NO_VALUE) && (y != SCR_NO_VALUE)) {
// We cannot set character position right away,
// because room switch will occur only after the script end,
// and character position may be still changing meanwhile.
new_room_pos = 0;
// don't check X or Y bounds, so that they can do a
// walk-in animation if they want
new_room_x = x;
new_room_y = y;
if (direction != SCR_NO_VALUE) new_room_loop = direction;
}
NewRoom(room);
}
void Character_ChangeView(CharacterInfo *chap, int vii) {
vii--;
if ((vii < 0) || (vii >= game.numviews))
quit("!ChangeCharacterView: invalid view number specified");
// if animating, but not idle view, give warning message
if ((chap->flags & CHF_FIXVIEW) && (chap->idleleft >= 0))
debug_script_warn("Warning: ChangeCharacterView was used while the view was fixed - call ReleaseCharView first");
// if the idle animation is playing we should release the view
if ( chap->idleleft < 0) {
Character_UnlockView(chap);
chap->idleleft = chap->idletime;
}
debug_script_log("%s: Change view to %d", chap->scrname, vii+1);
chap->defview = vii;
chap->view = vii;
stop_character_anim(chap);
chap->frame = 0;
chap->wait = 0;
chap->walkwait = 0;
charextra[chap->index_id].animwait = 0;
FindReasonableLoopForCharacter(chap);
}
enum DirectionalLoop
{
kDirLoop_Down = 0,
kDirLoop_Left = 1,
kDirLoop_Right = 2,
kDirLoop_Up = 3,
kDirLoop_DownRight = 4,
kDirLoop_UpRight = 5,
kDirLoop_DownLeft = 6,
kDirLoop_UpLeft = 7,
kDirLoop_Default = kDirLoop_Down,
kDirLoop_LastOrthogonal = kDirLoop_Up,
kDirLoop_Last = kDirLoop_UpLeft,
};
// Internal direction-facing functions
DirectionalLoop GetDirectionalLoop(CharacterInfo *chinfo, int x_diff, int y_diff)
{
DirectionalLoop next_loop = kDirLoop_Left; // NOTE: default loop was Left for some reason
const ViewStruct &chview = views[chinfo->view];
const bool new_version = loaded_game_file_version > kGameVersion_272;
const bool has_down_loop = ((chview.numLoops > kDirLoop_Down) && (chview.loops[kDirLoop_Down].numFrames > 0));
const bool has_up_loop = ((chview.numLoops > kDirLoop_Up) && (chview.loops[kDirLoop_Up].numFrames > 0));
// NOTE: 3.+ games required left & right loops to be present at all times
const bool has_left_loop = new_version ||
((chview.numLoops > kDirLoop_Left) && (chview.loops[kDirLoop_Left].numFrames > 0));
const bool has_right_loop = new_version ||
((chview.numLoops > kDirLoop_Right) && (chview.loops[kDirLoop_Right].numFrames > 0));
const bool has_diagonal_loops = useDiagonal(chinfo) == 0; // NOTE: useDiagonal returns 0 for "true"
const bool want_horizontal = (abs(y_diff) < abs(x_diff)) ||
(new_version && (!has_down_loop || !has_up_loop) )||
// NOTE: <= 2.72 games switch to horizontal loops only if both vertical ones are missing
(!new_version && (!has_down_loop && !has_up_loop));
if (want_horizontal)
{
const bool want_diagonal = has_diagonal_loops && (abs(y_diff) > abs(x_diff) / 2);
if (!has_left_loop && !has_right_loop)
{
next_loop = kDirLoop_Down;
}
else if (has_right_loop && (x_diff > 0))
{
next_loop = want_diagonal ? (y_diff < 0 ? kDirLoop_UpRight : kDirLoop_DownRight) :
kDirLoop_Right;
}
else if (has_left_loop && (x_diff <= 0))
{
next_loop = want_diagonal ? (y_diff < 0 ? kDirLoop_UpLeft : kDirLoop_DownLeft) :
kDirLoop_Left;
}
}
else
{
const bool want_diagonal = has_diagonal_loops && (abs(x_diff) > abs(y_diff) / 2);
if (y_diff > 0 || !has_up_loop)
{
next_loop = want_diagonal ? (x_diff < 0 ? kDirLoop_DownLeft : kDirLoop_DownRight) :
kDirLoop_Down;
}
else
{
next_loop = want_diagonal ? (x_diff < 0 ? kDirLoop_UpLeft : kDirLoop_UpRight) :
kDirLoop_Up;
}
}
return next_loop;
}
void FaceDirectionalLoop(CharacterInfo *char1, int direction, int blockingStyle)
{
// Change facing only if the desired direction is different
if (direction != char1->loop)
{
if ((game.options[OPT_TURNTOFACELOC] != 0) &&
(in_enters_screen == 0))
{
const int no_diagonal = useDiagonal (char1);
const int highestLoopForTurning = no_diagonal != 1 ? kDirLoop_Last : kDirLoop_LastOrthogonal;
if ((char1->loop <= highestLoopForTurning))
{
// Turn to face new direction
Character_StopMoving(char1);
if (char1->on == 1)
{
// only do the turning if the character is not hidden
// (otherwise GameLoopUntilNotMoving will never return)
start_character_turning (char1, direction, no_diagonal);
if ((blockingStyle == BLOCKING) || (blockingStyle == 1))
GameLoopUntilNotMoving(&char1->walking);
}
else
char1->loop = direction;
}
else
char1->loop = direction;
}
else
char1->loop = direction;
}
char1->frame = 0;
}
void FaceLocationXY(CharacterInfo *char1, int xx, int yy, int blockingStyle)
{
debug_script_log("%s: Face location %d,%d", char1->scrname, xx, yy);
const int diffrx = xx - char1->x;
const int diffry = yy - char1->y;
if ((diffrx == 0) && (diffry == 0)) {
// FaceLocation called on their current position - do nothing
return;
}
FaceDirectionalLoop(char1, GetDirectionalLoop(char1, diffrx, diffry), blockingStyle);
}
// External direction-facing functions with validation
void Character_FaceDirection(CharacterInfo *char1, int direction, int blockingStyle)
{
if (char1 == nullptr)
quit("!FaceDirection: invalid character specified");
if (direction != SCR_NO_VALUE)
{
if (direction < 0 || direction > kDirLoop_Last)
quit("!FaceDirection: invalid direction specified");
FaceDirectionalLoop(char1, direction, blockingStyle);
}
}
void Character_FaceLocation(CharacterInfo *char1, int xx, int yy, int blockingStyle)
{
if (char1 == nullptr)
quit("!FaceLocation: invalid character specified");
FaceLocationXY(char1, xx, yy, blockingStyle);
}
void Character_FaceObject(CharacterInfo *char1, ScriptObject *obj, int blockingStyle) {
if (obj == nullptr)
quit("!FaceObject: invalid object specified");
FaceLocationXY(char1, objs[obj->id].x, objs[obj->id].y, blockingStyle);
}
void Character_FaceCharacter(CharacterInfo *char1, CharacterInfo *char2, int blockingStyle) {
if (char2 == nullptr)
quit("!FaceCharacter: invalid character specified");
if (char1->room != char2->room)
quit("!FaceCharacter: characters are in different rooms");
FaceLocationXY(char1, char2->x, char2->y, blockingStyle);
}
void Character_FollowCharacter(CharacterInfo *chaa, CharacterInfo *tofollow, int distaway, int eagerness) {
if ((eagerness < 0) || (eagerness > 250))
quit("!FollowCharacterEx: invalid eagerness: must be 0-250");
if ((chaa->index_id == game.playercharacter) && (tofollow != nullptr) &&
(tofollow->room != chaa->room))
quit("!FollowCharacterEx: you cannot tell the player character to follow a character in another room");
if (tofollow != nullptr) {
debug_script_log("%s: Start following %s (dist %d, eager %d)", chaa->scrname, tofollow->scrname, distaway, eagerness);
}
else {
debug_script_log("%s: Stop following other character", chaa->scrname);
}
if ((chaa->following >= 0) &&
(chaa->followinfo == FOLLOW_ALWAYSONTOP)) {
// if this character was following always-on-top, its baseline will
// have been changed, so release it.
chaa->baseline = -1;
}
if (tofollow == nullptr)
chaa->following = -1;
else
chaa->following = tofollow->index_id;
chaa->followinfo=(distaway << 8) | eagerness;
chaa->flags &= ~CHF_BEHINDSHEPHERD;
// special case for Always On Other Character
if (distaway == FOLLOW_ALWAYSONTOP) {
chaa->followinfo = FOLLOW_ALWAYSONTOP;
if (eagerness == 1)
chaa->flags |= CHF_BEHINDSHEPHERD;
}
if (chaa->animating & CHANIM_REPEAT)
debug_script_warn("Warning: FollowCharacter called but the sheep is currently animating looped. It may never start to follow.");
}
int Character_IsCollidingWithChar(CharacterInfo *char1, CharacterInfo *char2) {
if (char2 == nullptr)
quit("!AreCharactersColliding: invalid char2");
if (char1->room != char2->room) return 0; // not colliding
if ((char1->y > char2->y - 5) && (char1->y < char2->y + 5)) ;
else return 0;
int w1 = game_to_data_coord(GetCharacterWidth(char1->index_id));
int w2 = game_to_data_coord(GetCharacterWidth(char2->index_id));
int xps1=char1->x - w1/2;
int xps2=char2->x - w2/2;
if ((xps1 >= xps2 - w1) & (xps1 <= xps2 + w2)) return 1;
return 0;
}
int Character_IsCollidingWithObject(CharacterInfo *chin, ScriptObject *objid) {
if (objid == nullptr)
quit("!AreCharObjColliding: invalid object number");
if (chin->room != displayed_room)
return 0;
if (objs[objid->id].on != 1)
return 0;
Bitmap *checkblk = GetObjectImage(objid->id);
int objWidth = checkblk->GetWidth();
int objHeight = checkblk->GetHeight();
int o1x = objs[objid->id].x;
int o1y = objs[objid->id].y - game_to_data_coord(objHeight);
Bitmap *charpic = GetCharacterImage(chin->index_id);
int charWidth = charpic->GetWidth();
int charHeight = charpic->GetHeight();
int o2x = chin->x - game_to_data_coord(charWidth) / 2;
int o2y = charextra[chin->index_id].GetEffectiveY(chin) - 5; // only check feet
if ((o2x >= o1x - game_to_data_coord(charWidth)) &&
(o2x <= o1x + game_to_data_coord(objWidth)) &&
(o2y >= o1y - 8) &&
(o2y <= o1y + game_to_data_coord(objHeight))) {
// the character's feet are on the object
if (game.options[OPT_PIXPERFECT] == 0)
return 1;
// check if they're on a transparent bit of the object
int stxp = data_to_game_coord(o2x - o1x);
int styp = data_to_game_coord(o2y - o1y);
int maskcol = checkblk->GetMaskColor ();
int maskcolc = charpic->GetMaskColor ();
int thispix, thispixc;
// check each pixel of the object along the char's feet
for (int i = 0; i < charWidth; i += get_fixed_pixel_size(1)) {
for (int j = 0; j < get_fixed_pixel_size(6); j += get_fixed_pixel_size(1)) {
thispix = my_getpixel(checkblk, i + stxp, j + styp);
thispixc = my_getpixel(charpic, i, j + (charHeight - get_fixed_pixel_size(5)));
if ((thispix != -1) && (thispix != maskcol) &&
(thispixc != -1) && (thispixc != maskcolc))
return 1;
}
}
}
return 0;
}
bool Character_IsInteractionAvailable(CharacterInfo *cchar, int mood) {
play.check_interaction_only = 1;
RunCharacterInteraction(cchar->index_id, mood);
int ciwas = play.check_interaction_only;
play.check_interaction_only = 0;
return (ciwas == 2);
}
void Character_LockView(CharacterInfo *chap, int vii) {
Character_LockViewEx(chap, vii, STOP_MOVING);
}
void Character_LockViewEx(CharacterInfo *chap, int vii, int stopMoving) {
vii--; // convert to 0-based
AssertView("SetCharacterView", vii);
debug_script_log("%s: View locked to %d", chap->scrname, vii+1);
if (chap->idleleft < 0) {
Character_UnlockView(chap);
chap->idleleft = chap->idletime;
}
if (stopMoving != KEEP_MOVING)
{
Character_StopMoving(chap);
}
chap->view=vii;
stop_character_anim(chap);
FindReasonableLoopForCharacter(chap);
chap->frame=0;
chap->wait=0;
chap->flags|=CHF_FIXVIEW;
chap->pic_xoffs = 0;
chap->pic_yoffs = 0;
}
void Character_LockViewAligned_Old(CharacterInfo *chap, int vii, int loop, int align) {
Character_LockViewAlignedEx(chap, vii, loop, ConvertLegacyScriptAlignment((LegacyScriptAlignment)align), STOP_MOVING);
}
void Character_LockViewAlignedEx_Old(CharacterInfo *chap, int vii, int loop, int align, int stopMoving) {
Character_LockViewAlignedEx(chap, vii, loop, ConvertLegacyScriptAlignment((LegacyScriptAlignment)align), stopMoving);
}
void Character_LockViewAligned(CharacterInfo *chap, int vii, int loop, int align) {
Character_LockViewAlignedEx(chap, vii, loop, align, STOP_MOVING);
}
void Character_LockViewAlignedEx(CharacterInfo *chap, int vii, int loop, int align, int stopMoving) {
if (chap->view < 0)
quit("!SetCharacterLoop: character has invalid old view number");
int sppic = views[chap->view].loops[chap->loop].frames[chap->frame].pic;
int leftSide = data_to_game_coord(chap->x) - game.SpriteInfos[sppic].Width / 2;
Character_LockViewEx(chap, vii, stopMoving);
AssertLoop("SetCharacterViewEx", chap->view, loop);
chap->loop = loop;
chap->frame = 0;
int newpic = views[chap->view].loops[chap->loop].frames[chap->frame].pic;
int newLeft = data_to_game_coord(chap->x) - game.SpriteInfos[newpic].Width / 2;
int xdiff = 0;
if (align & kMAlignLeft)
xdiff = leftSide - newLeft;
else if (align & kMAlignHCenter)
xdiff = 0;
else if (align & kMAlignRight)
xdiff = (leftSide + game.SpriteInfos[sppic].Width) - (newLeft + game.SpriteInfos[newpic].Width);
else
quit("!SetCharacterViewEx: invalid alignment type specified");
chap->pic_xoffs = xdiff;
chap->pic_yoffs = 0;
}
void Character_LockViewFrame(CharacterInfo *chaa, int view, int loop, int frame) {
Character_LockViewFrameEx(chaa, view, loop, frame, STOP_MOVING);
}
void Character_LockViewFrameEx(CharacterInfo *chaa, int view, int loop, int frame, int stopMoving) {
Character_LockViewEx(chaa, view, stopMoving);
AssertFrame("SetCharacterFrame", view - 1, loop, frame);
chaa->loop = loop;
chaa->frame = frame;
}
void Character_LockViewOffset(CharacterInfo *chap, int vii, int xoffs, int yoffs) {
Character_LockViewOffsetEx(chap, vii, xoffs, yoffs, STOP_MOVING);
}
void Character_LockViewOffsetEx(CharacterInfo *chap, int vii, int xoffs, int yoffs, int stopMoving) {
Character_LockViewEx(chap, vii, stopMoving);
// This function takes offsets in real game coordinates as opposed to script coordinates
defgame_to_finalgame_coords(xoffs, yoffs);
chap->pic_xoffs = xoffs;
chap->pic_yoffs = yoffs;
}
void Character_LoseInventory(CharacterInfo *chap, ScriptInvItem *invi) {
if (invi == nullptr)
quit("!LoseInventoryFromCharacter: invalid inventory number");
int inum = invi->id;
if (chap->inv[inum] > 0)
chap->inv[inum]--;
if ((chap->activeinv == inum) & (chap->inv[inum] < 1)) {
chap->activeinv = -1;
if ((chap == playerchar) && (GetCursorMode() == MODE_USE))
set_cursor_mode(0);
}
int charid = chap->index_id;
if ((chap->inv[inum] == 0) || (game.options[OPT_DUPLICATEINV] > 0)) {
int xx,tt;
for (xx = 0; xx < charextra[charid].invorder_count; xx++) {
if (charextra[charid].invorder[xx] == inum) {
charextra[charid].invorder_count--;
for (tt = xx; tt < charextra[charid].invorder_count; tt++)
charextra[charid].invorder[tt] = charextra[charid].invorder[tt+1];
break;
}
}
}
GUIE::MarkInventoryForUpdate(charid, charid == game.playercharacter);
if (chap == playerchar)
run_on_event (GE_LOSE_INV, RuntimeScriptValue().SetInt32(inum));
}
void Character_PlaceOnWalkableArea(CharacterInfo *chap)
{
if (displayed_room < 0)
quit("!Character.PlaceOnWalkableArea: no room is currently loaded");
find_nearest_walkable_area(&chap->x, &chap->y);
}
void Character_RemoveTint(CharacterInfo *chaa) {
if (chaa->flags & (CHF_HASTINT | CHF_HASLIGHT)) {
debug_script_log("Un-tint %s", chaa->scrname);
chaa->flags &= ~(CHF_HASTINT | CHF_HASLIGHT);
}
else {
debug_script_warn("Character.RemoveTint called but character was not tinted");
}
}
int Character_GetHasExplicitTint_Old(CharacterInfo *ch)
{
return ch->has_explicit_tint() || ch->has_explicit_light();
}
int Character_GetHasExplicitTint(CharacterInfo *ch)
{
return ch->has_explicit_tint();
}
void Character_Say(CharacterInfo *chaa, const char *text) {
_DisplaySpeechCore(chaa->index_id, text);
}
void Character_SayAt(CharacterInfo *chaa, int x, int y, int width, const char *texx) {
DisplaySpeechAt(x, y, width, chaa->index_id, texx);
}
ScriptOverlay* Character_SayBackground(CharacterInfo *chaa, const char *texx) {
int ovltype = DisplaySpeechBackground(chaa->index_id, texx);
auto *over = get_overlay(ovltype);
if (!over)
quit("!SayBackground internal error: no overlay");
// Create script object with an internal ref, keep at least until internal timeout
return create_scriptoverlay(*over, true);
}
void Character_SetAsPlayer(CharacterInfo *chaa) {
// Set to same character, so ignore.
// But only on versions > 2.61. The relevant entry in the 2.62 changelog is:
// - Fixed SetPlayerCharacter to do nothing at all if you pass the current
// player character to it (previously it was resetting the inventory layout)
if ((loaded_game_file_version > kGameVersion_261) && (game.playercharacter == chaa->index_id))
return;
setup_player_character(chaa->index_id);
//update_invorder();
debug_script_log("%s is new player character", playerchar->scrname);
// Within game_start, return now
if (displayed_room < 0)
return;
// Ignore invalid room numbers for the character and just place him in
// the current room for 2.x. Following script calls to NewRoom() will
// make sure this still works as intended.
if ((loaded_game_file_version <= kGameVersion_272) && (playerchar->room < 0))
playerchar->room = displayed_room;
if (displayed_room != playerchar->room)
NewRoom(playerchar->room);
else // make sure it doesn't run the region interactions
play.player_on_region = GetRegionIDAtRoom(playerchar->x, playerchar->y);
if ((playerchar->activeinv >= 0) && (playerchar->inv[playerchar->activeinv] < 1))
playerchar->activeinv = -1;
// They had inv selected, so change the cursor
if (cur_mode == MODE_USE) {
if (playerchar->activeinv < 0)
SetNextCursor ();
else
SetActiveInventory (playerchar->activeinv);
}
}
void Character_SetIdleView(CharacterInfo *chaa, int iview, int itime) {
if (iview == 1)
quit("!SetCharacterIdle: view 1 cannot be used as an idle view, sorry.");
// if an idle anim is currently playing, release it
if (chaa->idleleft < 0)
Character_UnlockView(chaa);
chaa->idleview = iview - 1;
// make sure they don't appear idle while idle anim is disabled
if (iview < 1)
itime = 10;
chaa->idletime = itime;
chaa->idleleft = itime;
// if not currently animating, reset the wait counter
if ((chaa->animating == 0) && (chaa->walking == 0))
chaa->wait = 0;
if (iview >= 1) {
debug_script_log("Set %s idle view to %d (time %d)", chaa->scrname, iview, itime);
}
else {
debug_script_log("%s idle view disabled", chaa->scrname);
}
if (chaa->flags & CHF_FIXVIEW) {
debug_script_warn("SetCharacterIdle called while character view locked with SetCharacterView; idle ignored");
debug_script_log("View locked, idle will not kick in until Released");
}
// if they switch to a swimming animation, kick it off immediately
if (itime == 0)
charextra[chaa->index_id].process_idle_this_time = 1;
}
bool Character_GetHasExplicitLight(CharacterInfo *ch)
{
return ch->has_explicit_light();
}
int Character_GetLightLevel(CharacterInfo *ch)
{
return ch->has_explicit_light() ? charextra[ch->index_id].tint_light : 0;
}
void Character_SetLightLevel(CharacterInfo *chaa, int light_level)
{
light_level = Math::Clamp(light_level, -100, 100);
charextra[chaa->index_id].tint_light = light_level;
chaa->flags &= ~CHF_HASTINT;
chaa->flags |= CHF_HASLIGHT;
}
int Character_GetTintRed(CharacterInfo *ch)
{
return ch->has_explicit_tint() ? charextra[ch->index_id].tint_r : 0;
}
int Character_GetTintGreen(CharacterInfo *ch)
{
return ch->has_explicit_tint() ? charextra[ch->index_id].tint_g : 0;
}
int Character_GetTintBlue(CharacterInfo *ch)
{
return ch->has_explicit_tint() ? charextra[ch->index_id].tint_b : 0;
}
int Character_GetTintSaturation(CharacterInfo *ch)
{
return ch->has_explicit_tint() ? charextra[ch->index_id].tint_level : 0;
}
int Character_GetTintLuminance(CharacterInfo *ch)
{
return ch->has_explicit_tint() ? ((charextra[ch->index_id].tint_light * 10) / 25) : 0;
}
void Character_SetOption(CharacterInfo *chaa, int flag, int yesorno) {
if ((yesorno < 0) || (yesorno > 1))
quit("!SetCharacterProperty: last parameter must be 0 or 1");
if (flag & CHF_MANUALSCALING) {
// backwards compatibility fix
Character_SetIgnoreScaling(chaa, yesorno);
}
else {
chaa->flags &= ~flag;
if (yesorno)
chaa->flags |= flag;
}
}
void Character_SetSpeed(CharacterInfo *chaa, int xspeed, int yspeed) {
if ((xspeed == 0) || (yspeed == 0))
quit("!SetCharacterSpeedEx: invalid speed value");
if ((chaa->walking > 0) && (loaded_game_file_version < kGameVersion_361))
{
debug_script_warn("Character_SetSpeed: cannot change speed while walking");
return;
}
xspeed = Math::Clamp(xspeed, (int)INT16_MIN, (int)INT16_MAX);
yspeed = Math::Clamp(yspeed, (int)INT16_MIN, (int)INT16_MAX);
uint16_t old_speedx = chaa->walkspeed;
uint16_t old_speedy = ((chaa->walkspeed_y == UNIFORM_WALK_SPEED) ? chaa->walkspeed : chaa->walkspeed_y);
chaa->walkspeed = xspeed;
if (yspeed == xspeed)
chaa->walkspeed_y = UNIFORM_WALK_SPEED;
else
chaa->walkspeed_y = yspeed;
if (chaa->walking > 0)
{
recalculate_move_speeds(&mls[chaa->walking % TURNING_AROUND], old_speedx, old_speedy, xspeed, yspeed);
}
}
void Character_StopMoving(CharacterInfo *charp) {
int chaa = charp->index_id;
if (chaa == play.skip_until_char_stops)
EndSkippingUntilCharStops();
if (charextra[chaa].xwas != INVALID_X) {
charp->x = charextra[chaa].xwas;
charp->y = charextra[chaa].ywas;
charextra[chaa].xwas = INVALID_X;
}
if ((charp->walking > 0) && (charp->walking < TURNING_AROUND)) {
// if it's not a MoveCharDirect, make sure they end up on a walkable area
if ((mls[charp->walking].direct == 0) && (charp->room == displayed_room))
Character_PlaceOnWalkableArea(charp);
debug_script_log("%s: stop moving", charp->scrname);
charp->idleleft = charp->idletime;
// restart the idle animation straight away
charextra[chaa].process_idle_this_time = 1;
}
if (charp->walking) {
// If the character is currently moving, stop them and reset their frame
charp->walking = 0;
if ((charp->flags & CHF_MOVENOTWALK) == 0)
charp->frame = 0;
}
}
void Character_Tint(CharacterInfo *chaa, int red, int green, int blue, int opacity, int luminance) {
if ((red < 0) || (green < 0) || (blue < 0) ||
(red > 255) || (green > 255) || (blue > 255) ||
(opacity < 0) || (opacity > 100) ||
(luminance < 0) || (luminance > 100))
quit("!Character.Tint: invalid parameter. R,G,B must be 0-255, opacity & luminance 0-100");
debug_script_log("Set %s tint RGB(%d,%d,%d) %d%%", chaa->scrname, red, green, blue, opacity);
charextra[chaa->index_id].tint_r = red;
charextra[chaa->index_id].tint_g = green;
charextra[chaa->index_id].tint_b = blue;
charextra[chaa->index_id].tint_level = opacity;