forked from Henne/Bright-Eyes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seg032.cpp
1294 lines (1002 loc) · 38.7 KB
/
seg032.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
/*
* Rewrite of DSA1 v3.02_de functions of seg032 (fight)
* Functions rewritten: 12/12 (complete)
*
* Borlandified and identical
* Compiler: Borland C++ 3.1
* Call: BCC.EXE -mlarge -O- -c -1 -Yo seg032.cpp
*
*/
#include <stdlib.h>
#include <string.h>
#include "v302de.h"
#include "seg000.h"
#include "seg002.h"
#include "seg004.h"
#include "seg005.h"
#include "seg006.h"
#include "seg007.h"
#include "seg008.h"
#include "seg026.h"
#include "seg027.h"
#include "seg028.h"
#include "seg033.h"
#include "seg034.h"
#include "seg035.h"
#include "seg037.h"
#include "seg038.h"
#include "seg039.h"
#include "seg040.h"
#include "seg042.h"
#include "seg043.h"
#include "seg049.h"
#include "seg097.h"
#include "seg105.h"
#include "seg113.h"
#include "seg120.h"
#if !defined(__BORLANDC__)
namespace M302de {
#endif
/**
* \brief sets an object to a chessboard position
*
* \param y the Y-Coordinate on the chessboard
* \param x the X-Coordinate on the chessboard
* \param object number of the object
* 0=free, 1-6=heroes, >10=enemies,
* >50walls, trees, etc
* -3 = flee
*
* This function is called multiple times when an entrant in a fight
* is moving. The initial placements of static objects are also done
* with this function.
*/
/* The functions in this file need object to be signed short.
* All other callers use signed short from the header.
*/
#if !defined(__BORLANDC__)
void FIG_set_cb_field(signed short y, signed short x, signed short object)
#else
void FIG_set_cb_field(signed short y, signed short x, signed char object)
#endif
{
/* check that the object is in the borders */
if (y < 0 || y > 24 || x < 0 || x > 24) {
return;
}
set_cb_val(x, y, object);
}
void draw_fight_screen_pal(signed short mode)
{
FIG_draw_pic();
/* check for palette update */
if (ds_readb(PP20_INDEX) != 0xff) {
update_mouse_cursor();
/* clear framebuffer */
#if !defined(__BORLANDC__)
PhysPt p = Real2Phys((RealPt)ds_readd(FRAMEBUF_PTR));
for (int i = 0; i < 64000; i+=4) {
mem_writed(p + i, 0);
}
#else
memset((RealPt)ds_readd(FRAMEBUF_PTR), 0, 64000);
#endif
/* set palettes */
set_palette(p_datseg + PALETTE_FIGHT1, 0x00, 0x20);
set_palette(p_datseg + PALETTE_FIGHT2, 0x80, 0x14);
set_palette(Real2Host((RealPt)ds_readd(BUFFER8_PTR) + 0xfa02), 0x60, 0x20);
ds_writeb(PP20_INDEX, 0xff);
refresh_screen_size();
}
draw_fight_screen(mode);
}
/**
* \brief chooses the next hero on turn
*
* This is simply done randomly.
*/
signed short FIG_choose_next_hero(void)
{
#if !defined(__BORLANDC__)
signed short loop_cnt = 0;
long tries[7] = {0, 0, 0, 0, 0, 0, 0};
#endif
signed short retval;
do {
retval = random_schick(7) - 1;
#if !defined(__BORLANDC__)
tries[retval]++;
if (++loop_cnt > 200) {
D1_ERR("Possible infinite loop in %s()\n", __func__);
D1_ERR("I'll try to get a possible hero\n");
retval = -1;
/*
* print random statistic
*/
for (int i = 0; i < 7; i++)
D1_ERR("tries[%d] = %ld\n", i, tries[i]);
/*
* search by hand for a hero and dump the
* interesting bits
*/
Bit8u *hero = get_hero(0);
for (int i = 0; i < 7; i++, hero += SIZEOF_HERO) {
D1_ERR("Hero %d typus = %x group=%x current_group=%x actions=%x\n",
i, host_readb(hero + HERO_TYPE),
host_readb(hero + HERO_GROUP_NO),
ds_readb(CURRENT_GROUP),
host_readb(hero + HERO_ACTIONS));
if (host_readb(hero + HERO_TYPE) &&
host_readb(hero + HERO_GROUP_NO) == ds_readb(CURRENT_GROUP) &&
host_readb(hero + HERO_ACTIONS))
retval = i;
}
/* exit if we didn't find a hero */
if (retval == -1) {
D1_ERR("Sorry, this is an infinite loop.\n");
D1_ERR("I'll exit\n");
exit(1);
}
return retval;
}
#endif
/* search for a hero who has a class, is in the current group and
something still unknown */
} while (host_readb(get_hero(retval) + HERO_TYPE) == HERO_TYPE_NONE ||
host_readb(get_hero(retval) + HERO_GROUP_NO) != ds_readb(CURRENT_GROUP) ||
host_readb(get_hero(retval) + HERO_ACTIONS) == 0);
return retval;
}
/**
* \brief chooses the next enemy on turn
*
* This is simply done randomly.
*
* Orig_BUG: I had this loop running infinitely.
*/
signed short FIG_choose_next_enemy(void)
{
signed short retval;
#if !defined(__BORLANDC__)
Bit8u *enemy;
unsigned short i;
unsigned short loop_cnt = 0;
long tries[20] = { 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0};
#endif
do {
retval = random_schick(ds_readw(NR_OF_ENEMIES)) - 1;
#if !defined(__BORLANDC__)
tries[retval]++;
if (++loop_cnt > 200) {
D1_ERR("Possible infinite loop in %s()\n", __func__);
D1_ERR("I'll try to get a possible enemy\n");
retval = -1;
/*
* print random statistic
*/
for (i = 0; i < 20; i++)
D1_ERR("tries[%d] = %ld\n", i, tries[i]);
/*
* search by hand for an enemy and dump the
* interesting bits
*/
enemy = p_datseg + ENEMY_SHEETS;
for (i = 0; i < ds_readw(NR_OF_ENEMIES); i++, enemy += SIZEOF_ENEMY_SHEET) {
D1_ERR("Enemy %02d %x %x\n",
i, host_readb(enemy),
host_readb(enemy + ENEMY_SHEET_ATTACKS_LEFT));
if (host_readb(enemy) &&
host_readb(enemy + ENEMY_SHEET_ATTACKS_LEFT))
retval = i;
}
/* exit if we didn't find an enemy */
if (retval == -1) {
D1_ERR("Sorry, this is an infinite loop.\n");
D1_ERR("I'll exit\n");
exit(1);
}
return retval;
}
enemy = p_datseg + ENEMY_SHEETS + retval * SIZEOF_ENEMY_SHEET;
#endif
} while (ds_readbs(ENEMY_SHEETS + retval * SIZEOF_ENEMY_SHEET + ENEMY_SHEET_MON_ID) == 0
|| ds_readbs(ENEMY_SHEETS + ENEMY_SHEET_ATTACKS_LEFT + retval * SIZEOF_ENEMY_SHEET) == 0);
return retval;
}
/**
* \brief return the number of active enemies
*/
signed short FIG_count_active_enemies(void)
{
Bit8u *enemy;
signed short i, retval = 0;
for (i = 0; i < 20; i++) {
enemy = p_datseg + ENEMY_SHEETS + i * SIZEOF_ENEMY_SHEET;
if ((host_readb(enemy + ENEMY_SHEET_MON_ID) != 0) &&
!enemy_dead(enemy) &&
!enemy_petrified(enemy) &&
!enemy_tied(enemy) &&
!enemy_mushroom(enemy) &&
!enemy_busy(enemy) &&
!host_readbs(enemy + ENEMY_SHEET_ROUND_APPEAR))
{
retval++;
}
}
return retval;
}
/**
* \brief checks if an enemy is active
*
* \param enemy pointer to the enemy sheet
* \return 1 if enemy can act or 0 if not.
*/
//static
signed short FIG_is_enemy_active(Bit8u *enemy)
{
if (enemy_asleep(enemy) ||
enemy_dead(enemy) ||
enemy_petrified(enemy) ||
enemy_dancing(enemy) ||
enemy_mushroom(enemy) ||
enemy_busy(enemy) ||
enemy_tame(enemy) ||
(host_readbs(enemy + ENEMY_SHEET_ROUND_APPEAR) > 0))
{
return 0;
}
return 1;
}
/**
* \brief returns the first active hero
*
* \return the index of the firsta active hero.
*/
signed short FIG_get_first_active_hero(void)
{
Bit8u *hero_i;
signed short i;
hero_i = get_hero(0);
for (i = 0; i <= 6; i++, hero_i += SIZEOF_HERO) {
if ((host_readb(hero_i + HERO_TYPE) != HERO_TYPE_NONE) &&
(host_readb(hero_i + HERO_GROUP_NO) == ds_readb(CURRENT_GROUP)) &&
!hero_dead(hero_i) &&
!hero_petrified(hero_i) &&
!hero_renegade(hero_i) &&
!hero_scared(hero_i) &&
!hero_unconscious(hero_i))
{
return i;
}
}
return -1;
}
/**
* \brief 1 if all (active) heroes in group escaped from the fight
*
* \return 1 if FIG_get_first_active_hero() returns -1
* and at least one hero in the group is not dead and has
* something at offset HERO_ACTION_ID set (maybe sleeping).
*/
//static
unsigned short FIG_all_heroes_escaped(void)
{
Bit8u *hero_i;
signed short i;
if (FIG_get_first_active_hero() == -1) {
hero_i = get_hero(0);
for (i = 0; i <= 6; i++, hero_i += SIZEOF_HERO) {
if ((host_readb(hero_i + HERO_TYPE) != HERO_TYPE_NONE) &&
(host_readb(hero_i + HERO_GROUP_NO) == ds_readb(CURRENT_GROUP)) &&
!hero_dead(hero_i) &&
(host_readb(hero_i + HERO_ACTION_ID) == FIG_ACTION_FLEE))
{
return 1;
}
}
return 0;
} else {
return 0;
}
}
//static
unsigned short FIG_fight_continues(void)
{
if (FIG_all_heroes_escaped()) {
ds_writew(FIG_ALL_HEROES_ESCAPED, 1);
return 0;
}
if (FIG_get_first_active_hero() == -1) {
ds_writew(GAME_STATE, GAME_STATE_DEAD);
return 0;
}
if (FIG_count_active_enemies() == 0)
return 0;
return 1;
}
void FIG_do_round(void)
{
signed short i;
signed short nr_hero_action_phases_left_in_round; /* total number over all heroes */
signed short nr_enemy_action_phases_left_in_round; /* total number over all enemies */
signed short actor_id;
signed short x_coord;
signed short y_coord;
signed short nr_action_phases_left_in_turn; /* number of action phases left in the turn of an actor */
signed char is_enemies_turn; /* 0: enemies' turn; 1: heroes' turn */
RealPt hero;
RealPt enemy;
signed short x;
signed short y;
Bit8u *fighter_ptr;
/* A round is the phase of a fight where all heroes and enemies get their number of BP (Bewegungspunkte; depending on load, enemy type etc.) and use them to perform a series of actions.
* Actions are performed in action phases, An action phase consists of one or more actions by the active actor, where the actions 'melee attack', 'ranged attack',
* 'cast spell', 'use item', 'guard' (and others?) terminate the phase and 'move', 'change weapon', 'change item' etc. do not.
* The total number of action phases per round of an actor is usually 1, but can be 2 for certain types of enmies or with active Axxeleratus or Boeser Blick spells.
*
* A round is divided into turns, which alternate between heroes' and enemies' turns.
* In a turn, a number of action phases is performed by actors on the active side.
* This number of action phases depends on the relation between the number of remaining action phases of the heroes and the enemies.
* Phase by phase the actor on the active side is picked randomly among the actors which have action phases left. */
if (!FIG_fight_continues()) {
/* this fight is over */
ds_writew(IN_FIGHT, 0);
}
#if !defined(__BORLANDC__)
D1_INFO("Kampfrunde %d beginnt\n", ds_readws(FIGHT_ROUND));
#endif
nr_hero_action_phases_left_in_round = 0;
/* initialize heroes' #action phases and BP */
for (i = 0; i <= 6; ds_writeb(HERO_IS_TARGET + i, 0), i++) {
hero = (RealPt)ds_readd(HEROES) + SIZEOF_HERO * i;
if ((host_readbs(Real2Host(hero) + HERO_TYPE) != HERO_TYPE_NONE) &&
(host_readbs(Real2Host(hero) + HERO_GROUP_NO) == ds_readbs(CURRENT_GROUP)) &&
(host_readbs(Real2Host(hero) + HERO_ACTION_ID) != FIG_ACTION_FLEE))
{
/* set #action phases to 1 */
host_writeb(Real2Host(hero) + HERO_ACTIONS, 1);
/* give this hero 8 BP */
host_writeb(Real2Host(hero) + HERO_BP_LEFT, 8);
if (host_readbs(Real2Host(hero) + (HERO_ATTRIB + 3 * ATTRIB_KK)) * 50 <= host_readws(Real2Host(hero) + HERO_LOAD)) {
/* load at least 50% of carrying capacity: give BP malus -1 */
dec_ptr_bs(Real2Host(hero) + HERO_BP_LEFT);
}
if (host_readbs(Real2Host(hero) + (HERO_ATTRIB + 3 * ATTRIB_KK)) * 75 <= host_readws(Real2Host(hero) + HERO_LOAD)) {
/* load at least 75% of carrying capacity: give additional BP malus -2 */
sub_ptr_bs(Real2Host(hero) + HERO_BP_LEFT, 2);
}
if (host_readbs(Real2Host(hero) + (HERO_ATTRIB + 3 * ATTRIB_KK)) * 100 <= host_readws(Real2Host(hero) + HERO_LOAD)) {
/* load at least 100% of carrying capacity: give additional give BP malus -2 */
sub_ptr_bs(Real2Host(hero) + HERO_BP_LEFT, 2);
}
host_writew(Real2Host(hero) + HERO_ESCAPE_POSITION, 0);
nr_hero_action_phases_left_in_round++;
if (host_readbs(Real2Host(hero) + HERO_AXXELERATUS) != 0) {
/* Axxeleratus => BP + 4 ... */
add_ptr_bs(Real2Host(hero) + HERO_BP_LEFT, 4);
/* ... and one extra action phase */
inc_ptr_bs(Real2Host(hero) + HERO_ACTIONS);
nr_hero_action_phases_left_in_round++;
}
if (host_readbs(Real2Host(hero) + (HERO_ATTRIB + 3 * ATTRIB_KK)) * 110 <= host_readws(Real2Host(hero) + HERO_LOAD)) {
/* load at least 110% of carrying capacity: set BP to 1 */
host_writeb(Real2Host(hero) + HERO_BP_LEFT, 1);
}
}
}
nr_enemy_action_phases_left_in_round = 0;
for (i = 0; i < ds_readws(NR_OF_ENEMIES); i++) {
/* set #phases */
ds_writeb((ENEMY_SHEETS + ENEMY_SHEET_ATTACKS_LEFT) + SIZEOF_ENEMY_SHEET * i, ds_readbs((ENEMY_SHEETS + ENEMY_SHEET_ATTACKS) + SIZEOF_ENEMY_SHEET * i));
nr_enemy_action_phases_left_in_round += ds_readbs((ENEMY_SHEETS + ENEMY_SHEET_ATTACKS) + SIZEOF_ENEMY_SHEET * i);
/* set BP */
ds_writeb((ENEMY_SHEETS + ENEMY_SHEET_BP) + SIZEOF_ENEMY_SHEET * i, ds_readbs((ENEMY_SHEETS + ENEMY_SHEET_BP_ORIG) + SIZEOF_ENEMY_SHEET * i));
ds_writeb((FIG_ACTORS_UNKN + 10) + i, 0);
}
nr_action_phases_left_in_turn = 0;
is_enemies_turn = (ds_readbs(FIG_INITIATIVE) == 2 ? 1 : (ds_readbs(FIG_INITIATIVE) == 1 ? 0 : random_interval(0, 1))); /* the variable is set up 'the wrong way round', as it will be flipped in the first run */
while ((ds_readws(IN_FIGHT) != 0) && (nr_hero_action_phases_left_in_round + nr_enemy_action_phases_left_in_round > 0)) {
if (ds_readws(AUTOFIGHT) == 2) {
ds_writew(AUTOFIGHT, 0);
}
/* decide if heroes or enemies are next */
if (nr_action_phases_left_in_turn == 0) {
/* flip turn */
is_enemies_turn ^= 1;
if (!is_enemies_turn) {
/* heroes' turn */
/* this might be an Original-Bug:
* The code block here is similar, but not equivalent to to the corresponding block for the enemies' turn below.
* I'd expect first to check nr_hero_action_phases_left_in_round == 0 -> switch turn to enemies
* and then check nr_hero_action_phases_left_in_round <= nr_enemy_action_phases_left_in_round
* as below in the corresponding lines for the enemies. */
if (nr_hero_action_phases_left_in_round <= nr_enemy_action_phases_left_in_round) {
nr_action_phases_left_in_turn = 1;
} else if (nr_hero_action_phases_left_in_round == 0) {
is_enemies_turn = 1;
} else if (nr_enemy_action_phases_left_in_round != 0) {
nr_action_phases_left_in_turn = nr_hero_action_phases_left_in_round / nr_enemy_action_phases_left_in_round;
} else {
nr_action_phases_left_in_turn = nr_hero_action_phases_left_in_round;
}
}
if (is_enemies_turn == 1) {
/* enemies' turn */
if (nr_enemy_action_phases_left_in_round == 0) {
is_enemies_turn = 0;
nr_action_phases_left_in_turn = 1;
} else if (nr_enemy_action_phases_left_in_round <= nr_hero_action_phases_left_in_round) {
nr_action_phases_left_in_turn = 1;
} else {
nr_action_phases_left_in_turn = (nr_hero_action_phases_left_in_round ? nr_enemy_action_phases_left_in_round / nr_hero_action_phases_left_in_round : nr_enemy_action_phases_left_in_round);
}
}
}
if (is_enemies_turn == 0) {
/* heroes on turn */
actor_id = FIG_choose_next_hero();
hero = (RealPt)ds_readd(HEROES) + SIZEOF_HERO * actor_id;
dec_ptr_bs(Real2Host(hero) + HERO_ACTIONS);
if (hero_asleep(Real2Host(hero)) && !hero_dead(Real2Host(hero))) {
/* hero asleep and is not dead: 74% chance of waking up */
if (random_schick(100) < 75) {
/* awake him (or her) */
and_ptr_bs(Real2Host(hero) + HERO_STATUS1, 0xfd); /* unset 'sleep' status bit */
fighter_ptr = Real2Host(FIG_get_ptr(host_readbs(Real2Host(hero) + HERO_FIGHTER_ID)));
host_writeb(fighter_ptr + FIGHTER_NVF_NO, host_readbs(Real2Host(hero) + HERO_VIEWDIR));
host_writeb(fighter_ptr + FIGHTER_RELOAD, -1);
host_writeb(fighter_ptr + FIGHTER_OFFSETX, 0);
host_writeb(fighter_ptr + FIGHTER_OFFSETY, 0);
}
}
if (FIG_search_obj_on_cb(actor_id + 1, &x_coord, &y_coord) &&
check_hero(Real2Host(hero)))
{
#if !defined(__BORLANDC__)
/* BE-fix */
x_coord = host_readws((Bit8u*)&x_coord);
y_coord = host_readws((Bit8u*)&y_coord);
#endif
if (host_readbs(Real2Host(hero) + HERO_BLIND) != 0) {
dec_ptr_bs(Real2Host(hero) + HERO_BLIND);
} else {
if (host_readbs(Real2Host(hero) + HERO_ECLIPTIFACTUS) != 0) {
dec_ptr_bs(Real2Host(hero) + HERO_ECLIPTIFACTUS);
}
/* save the fighter_id of this hero */
ds_writew(FIG_CHAR_PIC, actor_id + 1);
/* select a fight action */
FIG_menu(Real2Host(hero), actor_id, x_coord, y_coord);
if ((host_readbs(Real2Host(hero) + HERO_ACTION_ID) == FIG_ACTION_ATTACK) ||
(host_readbs(Real2Host(hero) + HERO_ACTION_ID) == FIG_ACTION_SPELL) ||
(host_readbs(Real2Host(hero) + HERO_ACTION_ID) == FIG_ACTION_USE_ITEM) ||
(host_readbs(Real2Host(hero) + HERO_ACTION_ID) == FIG_ACTION_RANGE_ATTACK))
{
FIG_do_hero_action(hero, actor_id);
if (host_readbs(Real2Host(hero) + HERO_ENEMY_ID) >= 10) {
/* hero did attack some enemy (by weapon/spell/item etc.) */
/* if the tail of a two-squares enemy has been attacked,
* replace HERO_ENEMY_ID by the main id of that enemy */
if (host_readbs(Real2Host(hero) + HERO_ENEMY_ID) >= 30) {
sub_ptr_bs(Real2Host(hero) + HERO_ENEMY_ID, 20);
}
if (test_bit0(p_datseg + ((ENEMY_SHEETS - 10*SIZEOF_ENEMY_SHEET) + ENEMY_SHEET_STATUS1) + SIZEOF_ENEMY_SHEET * host_readbs(Real2Host(hero) + HERO_ENEMY_ID))) /* check 'dead' status bit */
{
/* attacked enemy is dead */
if (is_in_byte_array(host_readbs(p_datseg + ((ENEMY_SHEETS - 10*SIZEOF_ENEMY_SHEET) + ENEMY_SHEET_GFX_ID) + SIZEOF_ENEMY_SHEET * host_readbs(Real2Host(hero) + HERO_ENEMY_ID)), p_datseg + TWO_FIELDED_SPRITE_ID))
{
/* attacked dead enemy is two-squares */
/* goal: remove tail part */
FIG_search_obj_on_cb(host_readbs(Real2Host(hero) + HERO_ENEMY_ID) + 20, &x, &y);
/* (x,y) are the coordinates of the tail of the enemy. redundant as fighter_ptr + FIGHTER_CBX, fighter_ptr + FIGHTER_CBY could have been used later. */
#if !defined(__BORLANDC__)
/* BE-fix */
x = host_readws((Bit8u*)&x);
y = host_readws((Bit8u*)&y);
#endif
fighter_ptr = Real2Host(FIG_get_ptr(host_readbs(p_datseg + ((ENEMY_SHEETS - 10*SIZEOF_ENEMY_SHEET) + ENEMY_SHEET_FIGHTER_ID) + SIZEOF_ENEMY_SHEET * host_readbs(Real2Host(hero) + HERO_ENEMY_ID))));
/* intermediate: fighter_ptr points to the FIGHTER entry of the enemy */
fighter_ptr = Real2Host(FIG_get_ptr(ds_readbs(FIG_TWOFIELDED_TABLE + host_readbs(fighter_ptr + FIGHTER_TWOFIELDED))));
/* fighter_ptr now points the FIGHTER entry of the tail part of the enemy */
/* should be true: (host_readbs(fighter_ptr + FIGHTER_CBX) == x) and (host_readbs(fighter_ptr + FIGHTER_CBY) == y) */
/* Probably, the following if-then-else-condition is not necessary as the condition is always true. */
if (host_readbs(fighter_ptr + FIGHTER_OBJ_ID) >= 0) {
/* if the id of a cb_entry has been saved in FIGHTER_OBJ_ID (meaning that the tail part is standing on it),
* restore that to the cb */
FIG_set_cb_field(y, x, host_readbs(fighter_ptr + FIGHTER_OBJ_ID));
} else {
/* otherwise, set the square in the cb to 0 (free) */
FIG_set_cb_field(host_readbs(fighter_ptr + FIGHTER_CBY), host_readbs(fighter_ptr + FIGHTER_CBX), 0);
}
}
}
}
herokeeping();
}
/* set fighter_id of the hero to 0 */
ds_writew(FIG_CHAR_PIC, 0);
}
}
if (!FIG_fight_continues()) {
/* this fight is over */
ds_writew(IN_FIGHT, 0);
}
nr_hero_action_phases_left_in_round--;
} else {
/* enemies on turn */
actor_id = FIG_choose_next_enemy();
enemy = (RealPt)RealMake(datseg, ENEMY_SHEETS + SIZEOF_ENEMY_SHEET * actor_id);
dec_ptr_bs(Real2Host(enemy) + ENEMY_SHEET_ATTACKS_LEFT);
if (FIG_search_obj_on_cb(actor_id + 10, &x_coord, &y_coord) &&
FIG_is_enemy_active(Real2Host(enemy)))
{
#if !defined(__BORLANDC__)
/* BE-fix */
x_coord = host_readws((Bit8u*)&x_coord);
y_coord = host_readws((Bit8u*)&y_coord);
#endif
if (host_readbs(Real2Host(enemy) + ENEMY_SHEET_BLIND) != 0) {
dec_ptr_bs(Real2Host(enemy) + ENEMY_SHEET_BLIND);
} else {
ds_writew(FIG_ENEMY_PIC, actor_id + 10);
host_writebs(Real2Host(enemy) + ENEMY_SHEET_ACTION_ID, 1);
enemy_turn(Real2Host(enemy), actor_id, x_coord, y_coord);
if ((host_readbs(Real2Host(enemy) + ENEMY_SHEET_ACTION_ID) == FIG_ACTION_ATTACK) ||
(host_readbs(Real2Host(enemy) + ENEMY_SHEET_ACTION_ID) == FIG_ACTION_SPELL) ||
(host_readbs(Real2Host(enemy) + ENEMY_SHEET_ACTION_ID) == FIG_ACTION_USE_ITEM) ||
(host_readbs(Real2Host(enemy) + ENEMY_SHEET_ACTION_ID) == FIG_ACTION_RANGE_ATTACK))
{
FIG_do_enemy_action(enemy, actor_id);
if (host_readbs(Real2Host(enemy) + ENEMY_SHEET_ENEMY_ID) >= 10) {
/* enemy did attack some enemy (by weapon/spell etc.) */
/* if the tail of a two-squares enemy has been attacked,
* replace ENEMY_SHEET_ENEMY_ID by the main id of that enemy */
if (host_readbs(Real2Host(enemy) + ENEMY_SHEET_ENEMY_ID) >= 30) {
sub_ptr_bs(Real2Host(enemy) + ENEMY_SHEET_ENEMY_ID, 20);
}
if (test_bit0(p_datseg + ((ENEMY_SHEETS - 10*SIZEOF_ENEMY_SHEET) + ENEMY_SHEET_STATUS1) + SIZEOF_ENEMY_SHEET * host_readbs(Real2Host(enemy) + ENEMY_SHEET_ENEMY_ID))) /* check 'dead' status bit */
{
/* attacked enemy is dead */
if (is_in_byte_array(host_readbs(p_datseg + ((ENEMY_SHEETS - 10*SIZEOF_ENEMY_SHEET) + ENEMY_SHEET_GFX_ID) + SIZEOF_ENEMY_SHEET * host_readbs(Real2Host(enemy) + ENEMY_SHEET_ENEMY_ID)), p_datseg + TWO_FIELDED_SPRITE_ID))
{
/* attacked dead enemy is two-squares */
/* goal: remove tail part */
FIG_search_obj_on_cb(host_readbs(Real2Host(enemy) + ENEMY_SHEET_ENEMY_ID) + 20, &x, &y);
/* (x,y) are the coordinates of the tail of the enemy. redundant as fighter_ptr + FIGHTER_CBX, fighter_ptr + FIGHTER_CBY could have been used later. */
#if !defined(__BORLANDC__)
/* BE-fix */
x = host_readws((Bit8u*)&x);
y = host_readws((Bit8u*)&y);
#endif
fighter_ptr = Real2Host(FIG_get_ptr(host_readbs(p_datseg + ((ENEMY_SHEETS - 10*SIZEOF_ENEMY_SHEET) + ENEMY_SHEET_FIGHTER_ID) + SIZEOF_ENEMY_SHEET * host_readbs(Real2Host(enemy) + ENEMY_SHEET_ENEMY_ID))));
/* intermediate: fighter_ptr points to the FIGHTER entry of the killed enemy */
fighter_ptr = Real2Host(FIG_get_ptr(ds_readbs(FIG_TWOFIELDED_TABLE + host_readbs(fighter_ptr + FIGHTER_TWOFIELDED))));
/* fighter_ptr now points the FIGHTER entry of the tail part of the killed enemy */
/* should be true: (host_readbs(fighter_ptr + FIGHTER_CBX) == x) and (host_readbs(fighter_ptr + FIGHTER_CBY) == y) */
/* Probably, the following if-then-else-condition is not necessary as the condition is always true. */
if (host_readbs(fighter_ptr + FIGHTER_OBJ_ID) >= 0) {
/* if the id of a cb_entry has been saved in FIGHTER_OBJ_ID (meaning that the tail part is standing on it),
* restore that to the cb */
FIG_set_cb_field(y, x, host_readbs(fighter_ptr + FIGHTER_OBJ_ID));
} else {
/* otherwise, set the square in the cb to 0 (free) */
FIG_set_cb_field(host_readbs(fighter_ptr + FIGHTER_CBY), host_readbs(fighter_ptr + FIGHTER_CBX), 0);
}
}
}
}
#ifdef M302de_ORIGINAL_BUGFIX
/* Original-Bug 3:
* Tails of dead/disappeared two-squared enemies must be removed separately.
* Otherwise, those tails are an obstacle for movement (by another bug) or can be used
* as a target of the 'Skelettarius' spell, which may result in weired situations, see
* https://www.crystals-dsa-foren.de/showthread.php?tid=5191&pid=166089#pid166089
*
* The tails are removed correctly (by some code above) if the enemies have been
* killed "regularly" by a hero (melee/ranged attack/spell/item) or by a (renegade) enemy.
* However, it has been forgotten for escaped enemies, as well as enemies which killed
* themselves by a critical melee attack failure. (Hopefully, this list is complete.)
*
* The following adds the missing code for self-killed enemies.
* The case of escaped enemies is dealt with in seg005.cpp
* It cannot be treated here as the FIGHTER entry of the tail is
* removed in seg005.cpp, which is needed to restore the object under the tail. */
if (enemy_dead(Real2Host(enemy))) { /* check 'dead' status bit */
/* attacking enemy is dead because of critical attack failure */
if (is_in_byte_array(host_readbs(Real2Host(enemy) + ENEMY_SHEET_GFX_ID), p_datseg + TWO_FIELDED_SPRITE_ID)) {
/* attacking dead enemy is two-squares */
/* goal: remove tail part */
fighter_ptr = Real2Host(FIG_get_ptr(host_readbs(Real2Host(enemy) + ENEMY_SHEET_FIGHTER_ID)));
/* intermediate: fighter_ptr points to the FIGHTER entry of the enemy */
fighter_ptr = Real2Host(FIG_get_ptr(ds_readbs(FIG_TWOFIELDED_TABLE + host_readbs(fighter_ptr + FIGHTER_TWOFIELDED))));
/* fighter_ptr now points the FIGHTER entry of the tail part of the enemy */
/* should be true: (host_readbs(fighter_ptr + FIGHTER_CBX) == x) and (host_readbs(fighter_ptr + FIGHTER_CBY) == y) */
/* restore the cb_entry stored at FIGHTER_OBJ_ID (meaning that the tail part is standing on it). */
FIG_set_cb_field(host_readbs(fighter_ptr + FIGHTER_CBY), host_readbs(fighter_ptr + FIGHTER_CBX), host_readbs(fighter_ptr + FIGHTER_OBJ_ID));
}
}
#endif
herokeeping();
}
ds_writew(FIG_ENEMY_PIC, 0);
}
}
if (!FIG_fight_continues()) {
ds_writew(IN_FIGHT, 0);
}
nr_enemy_action_phases_left_in_round--;
}
nr_action_phases_left_in_turn--;
if (ds_readbs(FIG_CB_MAKRER_ID) != -1) {
FIG_remove_from_list(ds_readbs(FIG_CB_MAKRER_ID), 0);
ds_writeb(FIG_CB_MAKRER_ID, -1);
}
if (ds_readbs(FIG_CB_SELECTOR_ID) != -1) {
FIG_remove_from_list(ds_readbs(FIG_CB_SELECTOR_ID), 0);
ds_writeb(FIG_CB_SELECTOR_ID, -1);
}
}
#if !defined(__BORLANDC__)
D1_INFO("Kampfrunde %d endet\n", ds_readws(FIGHT_ROUND));
#endif
}
/**
* \brief loads some special textures for the ghost ship
*/
void FIG_load_ship_sprites(void)
{
signed short l_si;
signed short l_di;
signed short i;
signed short width;
signed short height;
signed short const1 = 10;
signed short const2 = 118;
signed short l3;
signed short l4;
Bit8u *ptr;
struct nvf_desc nvf;
for (i = 0; i < 24; i++) {
for (l_di = 0; l_di < 24; l_di++) {
l_si = host_readbs(Real2Host(ds_readd(SCENARIO_BUF)) + 0x15 + 25 * i + l_di);
if ((l_si >= 108) && (l_si <= 111)) {
l_si -= 50;
if (NOT_NULL(Real2Host(host_readd(Real2Host(ds_readd(FIGOBJ_GFXBUF_TABLE)) + 4 * l_si)))) {
/* this sprite has already been buffered */
ptr = Real2Host(host_readd(Real2Host(ds_readd(FIGOBJ_GFXBUF_TABLE)) + 4 * l_si));
} else {
/* this sprite has not been used yet */
ptr = Real2Host(ds_readfp(FIGHTOBJ_BUF_SEEK_PTR));
nvf.dst = ptr;
nvf.src = Real2Host(ds_readd(FIGHTOBJ_BUF));
nvf.no = l_si;
nvf.type = 0;
nvf.width = (Bit8u*)&width;
nvf.height = (Bit8u*)&height;
process_nvf(&nvf);
#if !defined(__BORLANDC__)
/* BE-fix */
width = host_readws((Bit8u*)&width);
height = host_readws((Bit8u*)&height);
#endif
/* buffer this picture */
host_writed(Real2Host(ds_readd(FIGOBJ_GFXBUF_TABLE)) + 4 * l_si, (Bit32u)ds_readfp(FIGHTOBJ_BUF_SEEK_PTR));
host_writew(Real2Host(ds_readd(FIGOBJ_GFXWIDTH_TABLE)) + 2 * l_si, width);
host_writew(Real2Host(ds_readd(FIGOBJ_GFXHEIGHT_TABLE)) + 2 * l_si, height);
/* adjust the pointer */
add_ds_fp(FIGHTOBJ_BUF_SEEK_PTR, width * height + 8);
/* adjust the counter */
sub_ds_ds(FIGHTOBJ_BUF_FREESPACE, width * height + 8L);
/* check for error */
if (ds_readds(FIGHTOBJ_BUF_FREESPACE) < 0L) {
GUI_input(p_datseg + STR_ERROR_ON_OBJ_MALLOC, 0);
}
}
/* calculate screen coordinates */
l3 = const1 - host_readws(Real2Host(ds_readd(FIGOBJ_GFXWIDTH_TABLE)) + 2 * l_si) / 2 + 10 * (l_di + i);
l4 = const2 - host_readws(Real2Host(ds_readd(FIGOBJ_GFXHEIGHT_TABLE)) + 2 * l_si) + 5 * (l_di - i);
l3 += ds_readws(GFXTAB_OBJ_OFFSET_X + 2 * l_si);
l4 += ds_readws(GFXTAB_OBJ_OFFSET_Y + 2 * l_si);
/* set screen coordinates */
ds_writew(PIC_COPY_X1, l3);
ds_writew(PIC_COPY_Y1, l4);
ds_writew(PIC_COPY_X2, l3 + host_readws(Real2Host(ds_readd(FIGOBJ_GFXWIDTH_TABLE)) + 2 * l_si) - 1);
ds_writew(PIC_COPY_Y2, l4 + host_readws(Real2Host(ds_readd(FIGOBJ_GFXHEIGHT_TABLE)) + 2 * l_si) - 1);
ds_writed(PIC_COPY_SRC, host_readd(Real2Host(ds_readd(FIGOBJ_GFXBUF_TABLE)) + 4 * l_si));
ds_writed(PIC_COPY_DST, ds_readd(BUFFER8_PTR));
do_pic_copy(2);
ds_writed(PIC_COPY_DST, ds_readd(FRAMEBUF_PTR));
}
}
}
}
/**
* \brief the heroes encounter a fight
*
* \param fight_id id of the fight
* \return 0 = heroes are in Hygellik's ruin and cursed -> no fight (?); 1 = no enemies -> no fight; 2 = ?; 3 = sneaked around -> no fight;
*/
signed short do_fight(signed short fight_id)
{
signed short i;
signed short fd;
signed short j;
signed short new_escape_position_found;
signed short group_nr;
signed short group_size;
signed short retval = 0;
Bit8u *hero;
Bit8u *ptr;
signed short nr_escape_positions;
signed short x_target_bak;
signed short y_target_bak;
signed short dungeon_level_bak;
signed short direction_bak;
signed short textbox_width_bak;
signed short escape_positions[6];
if ((ds_readbs(GROUP_MEMBER_COUNTS + ds_readbs(CURRENT_GROUP)) == 1)
&& (host_readbs(get_hero(0) + HERO_INVISIBLE) != 0))
{
/* only one hero in the group with spell_visibili active */
return 3;
}
if (ds_readb(DNG15_TOOK_CURSED_MONEY) != 0) {
return 0;
}
if (!count_fight_enemies(fight_id)) {
return 1;
}
ds_writew(TIMERS_DISABLED, 1);
ds_writew(CURRENT_FIG_NO, fight_id);
textbox_width_bak = ds_readws(TEXTBOX_WIDTH);
ds_writew(TEXTBOX_WIDTH, 3);
/* set some pointers */
ds_writed(SCENARIO_BUF, (Bit32u)F_PADD(ds_readd(BUFFER8_PTR), 64100));
ds_writed(MONSTER_DAT_BUF, (Bit32u)F_PADD(ds_readd(SCENARIO_BUF), 621));
ds_writed(CURRENT_FIGHT, (Bit32u)F_PADD(ds_readd(MONSTER_DAT_BUF), 3476));
read_fight_lst(fight_id);
load_scenario(host_readws(Real2Host(ds_readd(CURRENT_FIGHT)) + FIGHT_SCENARIO));
if (!host_readbs(Real2Host(ds_readd(CURRENT_FIGHT)) + FIGHT_INTRO_SEEN)) {
GUI_print_fight_intro_msg(fight_id);
host_writeb(Real2Host(ds_readd(CURRENT_FIGHT)) + FIGHT_INTRO_SEEN, 1);
}
if (ds_readws(MAX_ENEMIES) > 0) {
/* reduce number of enemies to MAX_ENEMIES */
memset(Real2Host(ds_readd(CURRENT_FIGHT)) + SIZEOF_FIGHT_MONSTER * ds_readws(MAX_ENEMIES) + FIGHT_MONSTERS_ID, 0, SIZEOF_FIGHT_MONSTER * (20 - ds_readws(MAX_ENEMIES)));
ds_writew(MAX_ENEMIES, 0);
}
/* state that we are in a fight */
ds_writew(IN_FIGHT, 1);
/* set some vars to 0 */
ds_writew(AUTOFIGHT, ds_writew(FIGHT_ROUND, ds_writew(FIG_ALL_HEROES_ESCAPED, 0)));
/* set some vars to -1 */
ds_writew(FIG_FIGURE1, ds_writew(FIG_FIGURE2, -1));
ds_writew(FIGHT_FIGS_INDEX, -1);
ds_writew(REQUEST_REFRESH, 1);
ds_writed(ACTION_TABLE_PRIMARY, (Bit32u)RealMake(datseg, ACTION_TABLE_MENU));
ds_writew(WALLCLOCK_UPDATE, 0);
/* open MONSTER.DAT */
fd = load_archive_file(ARCHIVE_FILE_MONSTER_DAT);
read_archive_file(fd, Real2Host(ds_readd(MONSTER_DAT_BUF)), 3476);
bc_close(fd);
ds_writew(FIG_DROPPED_COUNTER, 0);
for (i = 0; i < 30; i++) {
ds_writew(FIG_DROPPED_WEAPONS + 2 * i, 0);
}
load_tx(ARCHIVE_FILE_FIGHTTXT_LTX);
/* open OBJECTS.NVF */
fd = load_archive_file(ARCHIVE_FILE_OBJECTS_NVF);