forked from y-salnikov/ironseed_fpc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
combat.pas
1633 lines (1584 loc) · 56.2 KB
/
combat.pas
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
unit combat;
(********************************************************************
This file is part of Ironseed.
Ironseed 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 3 of the License, or
(at your option) any later version.
Ironseed 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 Ironseed. If not, see <http://www.gnu.org/licenses/>.
********************************************************************)
{*********************************************
Battle/Combat unit for IronSeed
Copyright:
1994 Channel 7, Destiny: Virtual
2013 y-salnikov
2020 Matija Nalis <[email protected]>
**********************************************}
{$O+}
{$I-}
{$PACKRECORDS 1}
interface
procedure initiatecombat;
implementation
uses utils_, data, gmouse, utils, utils2, modplay, weird, saveload, usecode, crewtick, math;
const
maxships = 25;
maxformations = 3;
shipclass : array[0..14] of string[14] =
('Shuttle ','Scout ','Fighter ','Assault Scout ',
'Patrol Craft ','Corvette ','Frigate ','Lt. Destroyer ',
'Hv. Destroyer ','Lt. Cruiser ','Hv. Cruiser ','Battle Cruiser',
'Flagship ','Battleship ','Dreadnaught ');
formation : array[0..maxformations-1,0..4,1..3] of integer =
(
((0,0,0),(-3000,0,0),(3000,0,0),(0,-3000,0),(0,3000,0)), { planar plus }
((0,0,0),(-3000,-3000,0),(3000,3000,0),(-3000,3000,0),(3000,-3000,0)), { planar cross}
((0,0,0),(1000,0,1000),(2000,0,2000),(3000,0,3000),(4000,0,4000)) { 3d slash }
);
type
alienshiparray= array[1..maxships] of alienshiptype;
statpictype= array[0..1,0..11] of byte;
alienshipdisplay= array[125..189,6..98] of byte;
shieldpictype= array[0..6,0..3] of byte;
msgtype= array[0..9,0..9] of byte;
msgarray= array[0..3] of msgtype;
{$PACKRECORDS DEFAULT}
var
oldshddmg,i,a,b,nships,targetindex,fireweapon,moveindex,picy: integer;
range: longint;
scanning,autofire,engaging,alienpicmode,dead,done_: boolean;
poweredup: array[1..10] of integer;
userpowerup: array[1..10] of boolean;
ships: ^alienshiparray;
statpic,blank: ^statpictype;
stats: array[1..3] of byte;
part,r: real;
asdisplay: ^alienshipdisplay;
str1: string[10];
shieldpic,shieldpic2: ^shieldpictype;
alienname: string[12];
shipdir,shipdir2: integer;
msgs: ^msgarray;
learnchance:Integer;
{*******************************************************************************}
procedure displaystats;
var i: integer;
begin
if done_ then exit;
mousehide;
part:=102/ship.hullmax*ship.hullintegrity;
if round(part)<>stats[1] then
begin
for i:=0 to 1 do
scrto_move(blank^[i],screen[117-stats[1]+i,269],10);
stats[1]:=round(part);
y:=117-round(part);
for i:=0 to 1 do
scrto_move(statpic^[i],screen[y+i,269],10);
end;
part:=102/32000*ship.battery;
if round(part)<>stats[2] then
begin
for i:=0 to 1 do
scrto_move(blank^[i],screen[117-stats[2]+i,285],10);
stats[2]:=round(part);
y:=117-round(part);
for i:=0 to 1 do
scrto_move(statpic^[i],screen[y+i,285],10);
end;
part:=102/100*ship.shieldlevel;
if round(part)<>stats[3] then
begin
for i:=0 to 1 do
scrto_move(blank^[i],screen[117-stats[3]+i,301],10);
stats[3]:=round(part);
y:=117-round(part);
for i:=0 to 1 do
scrto_move(statpic^[i],screen[y+i,301],10);
end;
mouseshow;
end;
// set shield to n% (or to the maximum the shield subsystem damages allow, if n% is too much) and display levels
procedure displayshieldpic(n: integer);
begin
mousehide;
if ship.shield<=ID_NOSHIELD then { no shield is installed, do not allow moving it }
begin
n := 0;
ship.shieldopt[SHLD_COMBAT_WANT] := 0;
end
else if ship.shield=ID_REFLECTIVEHULL then n:=100-ship.damages[DMG_SHIELD]; { reflective hull always uses max the shield damages allow }
part:=102/100*ship.shieldopt[SHLD_COMBAT_WANT];
for i:=0 to 6 do
scr_fillchar(screen[114-round(part)+i,312],4,0);
if n>100-ship.damages[DMG_SHIELD] then n:=100-ship.damages[DMG_SHIELD];
ship.shieldopt[SHLD_COMBAT_WANT]:=n;
part:=102/100*n;
for i:=0 to 6 do
scrto_move(shieldpic^[i],screen[114-round(part)+i,312],1*4);
mouseshow;
end;
procedure displaytargetinfo2;
var str1 : string[8];
j : Integer;
begin
mousehide;
with ships^[targetindex] do
begin
printxy(4,125,alienname);
b:=ships^[targetindex].maxhull;
if b<1000 then b:=b div 100
else b:=((b-1000) div 1600) + 9;
printxy(4,131,shipclass[b]+' '+chr(64+targetindex));
str(r:8:1,str1);
printxy(4,137,'Range: '+str1+' KM');
printxy(4,143,'Tech Level: '+chr(hi(techlevel)+48)+'.'+chr(lo(techlevel)+48));
str(ships^[targetindex].accelmax,str1);
printxy(4,149,'Accel: '+str1);
if picx>139 then j:=10 else j:=5;
if picy>=179 then a:=19 else a:=20;
for i:=0 to a do
scrto_move(backgr^[i+picy,picx],screen[161+i,14],j*4);
if j=5 then
for i:=0 to 19 do
scr_fillchar(screen[161+i,34],20,0);
for j:=0 to 1 do
begin
a:=random(2);
for i:=0 to 9 do
scrto_move(msgs^[a,i],screen[162+i+j*10,60],10);
end;
for j:=0 to 1 do
begin
a:=random(2);
for i:=0 to 9 do
scrto_move(msgs^[a,i],screen[162+i+j*10,70],10);
end;
for j:=0 to 1 do
begin
a:=random(2)+2;
for i:=0 to 9 do
scrto_move(msgs^[a,i],screen[162+i+j*10,80],10);
end;
end;
mouseshow;
end;
{ n=damage type: 1=Psionic, 2=Particle, 3=Intertial, 4=Energy; 5=SPECIAL damage shield subsystem only; d=amount of damage inflicted }
procedure givedamage(n,d: integer);
var j: integer;
begin
d:=round(d/100*(100-ship.damages[DMG_WEAPONS]));
if d<1 then d:=1;
with ships^[targetindex] do // givedamage() always works on current "targetindex" ship
begin
case n of
DMGTYP_PSIONIC: inc(damages[DMG_LIFESUPPORT],d);
DMGTYP_PARTICLE: dec(hullintegrity,d);
DMGTYP_INERTIAL: dec(hullintegrity,d div 2);
DMGTYP_ENERGY: case random(8) of
0: inc(damages[DMG_POWER],d);
1: inc(damages[DMG_SHIELD],d);
2: inc(damages[DMG_WEAPONS],d);
3: inc(damages[DMG_ENGINES],d);
4: inc(damages[DMG_COMM],d);
5: inc(damages[DMG_CPU],d);
6,7: dec(hullintegrity,d);
end;
DMGTYP_FAKE_SHLD: inc(damages[DMG_SHIELD],d);
end;
if hullintegrity<0 then hullintegrity:=0;
for j:=1 to 7 do if damages[j]>100 then damages[j]:=100;
if shieldlevel<0 then shieldlevel:=0;
if shield=ID_REFLECTIVEHULL then shieldlevel:=100-damages[DMG_SHIELD]; // reflective hull always has same value as shield subsystem integrity (100%-damage%)
assert (shieldlevel >= 0, 'alien shieldlevel is negative');
assert (shieldlevel <= 100, 'alien shieldlevel is too big');
if damages[DMG_LIFESUPPORT]=100 then hullintegrity:=0;
//writeln (' givedamage() type',n,' = ',d,' GJ to alien',targetindex,' result: hullintegrity=', hullintegrity, ' shield', shield, ' level = ', shieldlevel,'%, damages: power=', damages[DMG_POWER], ' shield=', damages[DMG_SHIELD], ' weapons=', damages[DMG_WEAPONS], ' engines=', damages[DMG_ENGINES], ' life=', damages[DMG_LIFESUPPORT], ' comm=', damages[DMG_COMM], ' cpu=', damages[DMG_CPU]);
end;
end;
procedure displaymap; forward;
{ this is counterpart to impact(), but with us firing weapons at aliens }
procedure firingweapon(n: integer);
var j,i,a,b,c,d: integer;
begin
c:=ship.gunnodes[n]; // c = our current weapon index
case weapons[c].dmgtypes[DMGTYP_ENERGY] of
0..23: if weapons[c].dmgtypes[DMGTYP_PARTICLE]>weapons[c].dmgtypes[DMGTYP_INERTIAL] then soundeffect(loc_sound()+'gun4.sam',7000)
else soundeffect(loc_sound()+'gun1.sam',7000);
24..34: soundeffect(loc_sound()+'laser1.sam',7000);
35..45: soundeffect(loc_sound()+'laser2.sam',7000);
46..56: soundeffect(loc_sound()+'laser3.sam',7000);
57..67: soundeffect(loc_sound()+'laser4.sam',7000);
68..78: soundeffect(loc_sound()+'laser5.sam',7000);
79..89: soundeffect(loc_sound()+'laser6.sam',7000);
90..100: soundeffect(loc_sound()+'laser7.sam',7000);
end;
delay(tslice);
{if (skillcheck(4)) or ((scanning) and (random(100)<20)) then}
if SkillTest(True, 4, ships^[targetindex].skill - (ord(scanning) * 20), learnchance) then
begin
b:=ships^[targetindex].shield-ID_SHIELDS_OFFSET; // b = alien's shield index
assert (b>0); // aliens always have *some* shield, see Data_Generators/makedata/alienshp.txt
for j:=1 to 4 do if weapons[c].dmgtypes[j]>0 then // j = our weapon damage types: 1=Psionic, 2=Particle, 3=Intertial, 4=Energy
begin
i:=round(weapons[c].dmgtypes[j]/100*weapons[c].damage*5); // i = maximum damage our weapon can deal (for this dmgtype "j")
//writeln ('successfully firing weapon',c,'(node',n,') with dmgtype',j,' and maxdamage i=',i,' GJ on alien',targetindex, ' with shield', ships^[targetindex].shield, ' at ', ships^[targetindex].shieldlevel ,'%');
if ships^[targetindex].shieldlevel=0 then givedamage(j,i) // alien ship has not shields, let it take full damage "i"
else
begin // alien ship has some shield installed
a:=round(weapons[b].dmgtypes[j]/100*weapons[b].damage*ships^[targetindex].shieldlevel/100); // a = current maximum shield damage absorption
//writeln (' alien',targetindex,' has shield',b, ' with resistance ', weapons[b].dmgtypes[j], '% to dmgtype',j,', and shieldlevel ', ships^[targetindex].shieldlevel, '%. Max current damage absorption = ', a, ' GJ');
if a<i then
begin // alien ship receiving more damage than their shields can handle
givedamage(j,i-a); // let them take residual damage...
ships^[targetindex].shieldlevel:=1; // ... and collapse shields (NB: should be 0, but we are dividing by it!)
if (ships^[targetindex].shield=ID_REFLECTIVEHULL) and (j>1) then ships^[targetindex].damages[DMG_SHIELD]:=100; // psionic damage passes through reflective hull, and thus cannot destroy it
//writeln (' alien shield', ships^[targetindex].shield, ' is collapsing! shield subsystem damages=', ships^[targetindex].damages[DMG_SHIELD]);
end
else
begin // alien ship receiving less damage than their shields can handle
part:=i/ships^[targetindex].shieldlevel; // NB: This calculations looks strange... but is the same as for our ship - see "we've taken LESS damage than our shield can handle" part of the code
part:=part*(1/weapons[b].damage);
part:=part*100;
a:=round(part*100); // a = by how many percent will we reduce shield level
d:=ships^[targetindex].shieldlevel-a; // d = new shield level in percent
//writeln (' alien shield is holding, reducing shieldlevel by ',a,'% to ',d, '%');
if d<0 then
begin // alien shield has collapsed!
givedamage(DMGTYP_FAKE_SHLD,random(4)+1); // this always cases damage to shield subsystem for 1-5
if (ships^[targetindex].shield=ID_REFLECTIVEHULL) and (j>1) then // psionic damage passes through reflective hull, and thus cannot destroy it
ships^[targetindex].damages[DMG_SHIELD]:=100;
ships^[targetindex].shieldlevel:=1; // ... and collapse shields (NB: should be 0, but we are dividing by it!)
end
else
begin // alien shield still holding
ships^[targetindex].shieldlevel:=d;
if (ships^[targetindex].shield=ID_REFLECTIVEHULL) and (j>1) then // psionic damage passes through reflective hull, and thus cannot damage it
ships^[targetindex].damages[DMG_SHIELD]:=100-d;
end;
end;
end;
end;
end;
if ships^[targetindex].hullintegrity=0 then
begin
//writeln (' alien', targetindex, ' has been destroyed');
ships^[targetindex].hullintegrity:=1;
displaymap;
ships^[targetindex].hullintegrity:=0;
targetindex:=1;
while (targetindex<=nships) and (ships^[targetindex].hullintegrity=0) do inc(targetindex);
if targetindex>nships then
begin
done_:=true;
end;
with ships^[targetindex] do
begin
r:=sqr(relx/10);
r:=r+sqr(rely/10);
r:=r+sqr(relz/10);
r:=sqrt(r)*100;
end;
displaymap;
end;
poweredup[n]:=0;
fireweapon:=0;
end;
procedure powerup;
var i,j: integer;
begin
for j:=1 to 10 do
if (poweredup[j]>-1) and (poweredup[j]<100) then
begin
if (userpowerup[j]) and (poweredup[j]=0) and (ship.battery>=weapons[ship.gunnodes[j]].energy) then
begin
dec(ship.battery,weapons[ship.gunnodes[j]].energy);
poweredup[j]:=1;
end
else if poweredup[j]>0 then inc(poweredup[j]);
i:=round(poweredup[j]*0.31);
if i<16 then setcolor(80+i) else setcolor(32+i);
x:=((j-1) mod 5)*23+105;
y:=((j-1) div 5)*31+131;
mousehide;
rectangle(x,y,x+20,y+20);
mouseshow;
end
else if poweredup[j]=100 then
begin
part:=weapons[ship.gunnodes[j]].range;
if part>=r then setcolor(47) else setcolor(63);
x:=((j-1) mod 5)*23+105;
y:=((j-1) div 5)*31+131;
mousehide;
rectangle(x,y,x+20,y+20);
mouseshow;
if (part>=r) and ((autofire) or (fireweapon=j)) then firingweapon(j);
end;
if (ship.battery>0) and (ship.shieldlevel<ship.shieldopt[SHLD_COMBAT_WANT]) then inc(ship.shieldlevel)
else if (ship.battery=0) and (ship.shieldlevel>0) then dec(ship.shieldlevel)
else if (Ship.shieldlevel>ship.shieldopt[SHLD_COMBAT_WANT]) then dec(ship.shieldlevel);
for j:=1 to nships do
with ships^[j] do
begin
if shield>ID_REFLECTIVEHULL then
begin
r:=sqr(relx/10);
r:=r+sqr(rely/10);
r:=r+sqr(relz/10);
r:=sqrt(r)*100;
i:=round(weapons[shield-ID_SHIELDS_OFFSET].energy*shieldlevel/100);
if (battery>0) and (abs(r)<390000) and (shieldlevel<(100-damages[DMG_SHIELD])) then inc(shieldlevel)
else if ((battery=0) or (abs(r)>400000)) and (shieldlevel>0) then dec(shieldlevel)
else if shieldlevel>(100-damages[DMG_SHIELD]) then dec(shieldlevel);
if (abs(r)>230000) and (abs(r)<400000) and (i>round(regen*(100-damages[DMG_POWER])/100))
and (shieldlevel>2) then dec(shieldlevel,3);
end;
for i:=1 to 20 do
if charges[i]<100 then
begin
if (charges[i]=0) and (battery>=weapons[maxweapons].energy) then
begin
dec(battery,weapons[maxweapons].energy);
charges[i]:=1;
end
else if charges[i]>0 then inc(charges[i]);
end;
end;
end;
procedure showweaponicon(x1,y1,weap,node: integer);
var j,i: integer;
begin
if weap=0 then
begin
for i:=0 to 19 do
scr_fillchar(screen[y1+i,x1],20,3);
exit;
end;
readweaicon(weap-1);
node:=4;
case node of
1,2,3,8: for i:=0 to 19 do
for j:=0 to 19 do
screen[y1+j,x1+i]:=tempicon^[i,j];
4,6: for i:=0 to 19 do
scrto_move(tempicon^[i],screen[y1+i,x1],5*4);
5,7: for i:=0 to 19 do
scrto_move(tempicon^[19-i],screen[y1+i,x1],5*4);
9,10: for i:=0 to 19 do
for j:=0 to 19 do
screen[y1+j,x1+20-i]:=tempicon^[i,j];
end;
end;
procedure displayweapons;
var
j : Integer;
begin
mousehide;
for j:=1 to 10 do
begin
x:=((j-1) mod 5)*23+105;
y:=((j-1) div 5)*31+131;
showweaponicon(x,y,ship.gunnodes[j],j);
if ship.gunnodes[j]>0 then
begin
a:=round(poweredup[j]*0.31);
if a<16 then setcolor(80+a) else setcolor(32+a);
rectangle(x,y,x+20,y+20);
end;
end;
mouseshow;
end;
procedure displaydamage;
var a,b,i,j: integer;
begin
if (done_) or (dead) then exit;
mousehide;
for a:=1 to 7 do
begin
b:=round((100-ship.damages[a])/100*49);
if b<=0 then b:=1;
part:=31/b;
for j:=0 to b do
begin
screen[a*9+127,267+j]:=round(j*part);
screen[a*9+128,267+j]:=round(j*part);
end;
if b<51 then
begin
scr_fillchar(screen[a*9+127,268+b],49-b,0);
scr_fillchar(screen[a*9+128,268+b],49-b,0);
end;
end;
if 100-ship.damages[DMG_SHIELD]<ship.shieldopt[SHLD_COMBAT_WANT] then displayshieldpic(100-ship.damages[DMG_SHIELD]);
part:=114-(102/100*(100-ship.damages[DMG_SHIELD]));
if round(part)<>oldshddmg then
begin
for i:=0 to 6 do
scr_fillchar(screen[oldshddmg+i,296],4,0);
for i:=0 to 6 do
scrto_move(shieldpic2^[i],screen[round(part)+i,296],1*4);
oldshddmg:=round(part);
end;
mouseshow;
end;
procedure suckpower;
var
j : Integer;
begin
if ship.shield>ID_REFLECTIVEHULL then
ship.battery:=ship.battery-round(weapons[ship.shield-ID_SHIELDS_OFFSET].energy/100*ship.shieldlevel);
i:=round((100-ship.damages[DMG_POWER])/4);
if i=0 then i:=1;
ship.battery:=ship.battery+i;
if ship.battery<0 then ship.battery:=0
else if ship.battery>32000 then ship.battery:=32000;
for j:=1 to nships do if ships^[j].hullintegrity>0 then
with ships^[j] do
begin
if shield>ID_REFLECTIVEHULL then dec(battery,round(weapons[shield-ID_SHIELDS_OFFSET].energy/100*shieldlevel));
inc(battery,round(regen*(100-damages[DMG_POWER])/100));
if battery<0 then battery:=0
else if battery>32000 then battery:=32000;
if (battery=0) and (shield>ID_REFLECTIVEHULL) and (damages[DMG_SHIELD]<99) then inc(damages[DMG_SHIELD],2);
end;
end;
procedure displaytargetinfo;
var b : integer;
j : Integer;
begin
if done_ then exit;
with ships^[targetindex] do
begin
r:=sqr(relx/10);
r:=r+sqr(rely/10);
r:=r+sqr(relz/10);
r:=sqrt(r)*100;
end;
if alienpicmode then
begin
displaytargetinfo2;
exit;
end;
mousehide;
with ships^[targetindex] do
begin
r:=sqr(relx/10);
r:=r+sqr(rely/10);
r:=r+sqr(relz/10);
r:=sqrt(r)*100;
b:=maxhull;
if b<1000 then b:=b div 100
else b:=((b-1000) div 1600) + 9;
printxy(4,126,shipclass[b]+' '+chr(64+targetindex));
b:=round(hullintegrity/maxhull*49); { hull integrity }
if b<=0 then b:=1;
part:=31/b;
for i:=0 to b do
scr_fillchar(screen[i+138,8],8,round(i*part));
if b<49 then
for i:=b+1 to 49 do
scr_fillchar(screen[i+138,8],8,0);
b:=round((100-damages[DMG_LIFESUPPORT])/100*49); { life support }
if b<=0 then b:=1;
part:=31/b;
for i:=0 to b do
scr_fillchar(screen[i+138,23],8,round(i*part));
if b<49 then
for i:=b+1 to 49 do
scr_fillchar(screen[i+138,23],8,0);
b:=round(battery/32000*49); { power / batt level }
if b<=0 then b:=1;
part:=31/b;
for i:=0 to b do
scr_fillchar(screen[i+138,38],9,round(i*part));
if b<49 then
for i:=b+1 to 49 do
scr_fillchar(screen[i+138,38],9,0);
b:=round(shieldlevel/100*49); { shield level }
if b<=0 then b:=1;
part:=31/b;
for i:=0 to b do
scr_fillchar(screen[i+138,54],8,round(i*part));
if b<49 then
for i:=b+1 to 49 do
scr_fillchar(screen[i+138,54],8,0);
for j:=1 to 7 do { subsystem integrity: 1=power, 2=shield, 3=weapons, 4=engines, 5=life support, 6=comm, 7=cpu }
begin
b:=round((100-damages[j])/100*49);
if b<=0 then b:=1;
part:=31/b;
for i:=0 to b do
screen[i+138,62+5*j]:=round(i*part);
if b<49 then
for i:=b+1 to 49 do
screen[i+138,62+5*j]:=0;
end;
end;
mouseshow;
end;
procedure displaymap;
var
j : Integer;
begin
if dead then exit;
mousehide;
for j:=1 to nships do
begin
y:=round(ships^[j].rely/range*26.66);
x:=round(ships^[j].relx/range*119);
z:=round(ships^[j].relz/range*26);
//writeln ('displaymap ship',j, ' y=',y, ' x=',x, ' z=', z);
if (abs(x)<119) and (abs(y)<26) and (abs(z)<35) then
begin
assert (x+132+2 < 320, 'x too big');
assert (x+132-2 >= 0, 'x too small');
assert (y+62+2 < 200, 'y too big1');
assert (y+62-z+2 < 200, 'y too big2');
assert (y+62-2 >= 0, 'y too small1');
assert (y+62-z-2 >= 0, 'y too small2');
if z<0 then
for i:=y+62 to y+62-z do
screen[i,x+132]:=screen[i,x+132] xor 6
else
for i:=y+62 downto y+62-z do
screen[i,x+132]:=screen[i,x+132] xor 6;
screen[y+62,x+132]:=screen[y+62,x+132] xor 85;
if ships^[j].hullintegrity=0 then i:=12 else i:=31;
screen[y+62-z,x+132]:=screen[y+62-z,x+132] xor i;
if j=targetindex then
begin
screen[y+62-z-2,x+132-2]:=screen[y+62-z-2,x+132-2] xor 60;
screen[y+62-z-2,x+132-1]:=screen[y+62-z-2,x+132-1] xor 60;
screen[y+62-z-1,x+132-2]:=screen[y+62-z-1,x+132-2] xor 60;
screen[y+62-z+2,x+132+1]:=screen[y+62-z+2,x+132+1] xor 60;
screen[y+62-z+2,x+132+2]:=screen[y+62-z+2,x+132+2] xor 60;
screen[y+62-z+1,x+132+2]:=screen[y+62-z+1,x+132+2] xor 60;
end;
end;
end;
mouseshow;
end;
procedure drawdirection(c: integer);
begin
x:=((shipdir-1) mod 3)*12+225;
y:=((shipdir-1) div 3)*10+88;
setcolor(c);
mousehide;
rectangle(x,y,x+12,y+10);
if shipdir2=1 then rectangle(249,68,261,78)
else if shipdir2=2 then rectangle(249,78,261,88);
mouseshow;
end;
{ n=damage type: 1=Psionic, 2=Particle, 3=Intertial, 4=Energy; 5=SPECIAL damage shield subsystem only; d=amount of damage received }
procedure takedamage(n,d: integer);
var j: integer;
begin
//writeln (' takedamage(type=',n,', damage=', d, ')');
if dead then exit;
soundeffect(loc_sound()+'explode'+chr(49+random(2))+'.sam',9000);
delay(tslice div 2);
if d<1 then d:=1;
case n of
DMGTYP_PSIONIC: inc(ship.damages[DMG_LIFESUPPORT],d); { n=1 Psionioc inflicts damage to damages[DMG_LIFESUPPORT] = Lifesupport }
DMGTYP_PARTICLE: dec(ship.hullintegrity,d); { n=2 Particle damage damages hull }
DMGTYP_INERTIAL: dec(ship.hullintegrity,d div 2); { n=3 Intertial damage damages hull more slowly }
DMGTYP_ENERGY: case random(8) of { n=4 Energy damage }
0: inc(ship.damages[DMG_POWER],d); { damages[DMG_POWER] = Power subsystem }
1: begin
if ship.damages[DMG_SHIELD]+d>135 then ship.shield:=ID_NOSHIELD; { uninstalls / permanently destroys shield if shield subsystem > 135% damage }
inc(ship.damages[DMG_SHIELD],d); { damages[DMG_SHIELD] = Shield subsystem }
end;
2: begin
inc(ship.damages[DMG_ENGINES],d); { damages[DMG_ENGINES] = Engines subsystem }
if ship.damages[DMG_ENGINES]>89 then drawdirection(95);
end;
3: begin
if ship.damages[DMG_WEAPONS]+d>120 then { uninstalles / permanently destroys random weapon if weapons subsystems > 120% damage }
begin
j:=random(10)+1;
if ship.gunnodes[j]>0 then
begin
ship.gunnodes[j]:=0;
poweredup[j]:=-1;
displayweapons;
end;
end;
inc(ship.damages[DMG_WEAPONS],d); { damages[DMG_WEAPONS] = Weapons subsystem }
end;
4: inc(ship.damages[DMG_COMM],d); { damages[DMG_COMM] = Communications subsystem }
5: inc(ship.damages[DMG_CPU],d); { damages[DMG_CPU] = CPU subsystem }
6,7: dec(ship.hullintegrity,d);
end;
DMGTYP_FAKE_SHLD: inc(ship.damages[DMG_SHIELD],d); { damages[DMG_SHIELD] = Shield subsystem }
end;
for j:=1 to 7 do if ship.damages[j]>100 then ship.damages[j]:=100;
if ship.hullintegrity<0 then ship.hullintegrity:=0;
displaydamage;
//writeln (' hull=', ship.hullintegrity, ' shieldid=', ship.shield, ' damages: power=', ship.damages[DMG_POWER], ' shield=', ship.damages[DMG_SHIELD], ' weapons=', ship.damages[DMG_WEAPONS], ' engines=', ship.damages[DMG_ENGINES], ' life=', ship.damages[DMG_LIFESUPPORT], ' comm=', ship.damages[DMG_COMM], ' cpu=', ship.damages[DMG_CPU]);
if ship.hullintegrity=0 then { hull breach - we're dead }
begin
if ship.wandering.alienid = 1013 then
begin
stopmod;
blast(63,0,0);
fadestopmod(-FADEFULL_STEP, FADEFULL_DELAY);
end else begin
deathsequence(0);
end;
done_:=true;
dead:=true;
{quit:=true;}
end
else if ship.damages[DMG_LIFESUPPORT]=100 then { damages [5] = Life support - we're dead }
begin
if ship.wandering.alienid = 1013 then
begin
stopmod;
blast(63,0,0);
fadestopmod(-FADEFULL_STEP, FADEFULL_DELAY);
end else begin
deathsequence(1);
end;
done_:=true;
dead:=true;
{quit:=true;}
end;
if ship.shield=ID_REFLECTIVEHULL then
begin // FIXME: shouldn't we do that for all shields if we are losing shield (shieldlevel > ship.damages[DMG_SHIELD])? otherwise we could have shield which has level higher than damaged subsystem allows!
ship.shieldlevel := 100 - ship.damages[DMG_SHIELD]; { damages[DMG_SHIELD] is shield subsystem, drop shield level immedately if reflective hull }
//writeln (' reflective hull damage sets shieldlevel to ', ship.shieldlevel);
end;
end;
{ s=ship attacking us; n=weapontype of ship attacking us }
procedure impact(s,n: integer);
var a,b,c,j,i: integer;
begin
b:=ship.shield-ID_SHIELDS_OFFSET; { weapons[b]=our shield, weapons[n]=attacker's weapon; weapons[] array is generic for some type and readonly }
assert (b>0);
for j:=1 to 4 do if weapons[n].dmgtypes[j]>0 then { j=damage type: 1=Psionic, 2=Particle, 3=Intertial, 4=Energy }
begin
i:=round(weapons[n].dmgtypes[j]/100 * weapons[n].damage * 5); { pct. for this damagetype * total weapon damage in GJ }
{ QUESTION: why *5 ?!
if some weapon has damage potential of 50GJ and 20% is for 'j' damage type, it should be 10GJ damage for this damage type, isn't it so?)
yet putting DIRK as example does: impact: attacking weapon1 for dmgtype4=100% and its damage dealing=5GJ; THEIR CURRENT weapon subsystem damage=0 ; their attack total i=25
ANSWER: Ah, it seems to because real alian weapons are not implemented, and we always do impact(j,maxweapons=72) in moveships().
But aliens ships^[j].gunnodes has 5 positions, so this just simulates as all 5 weapons of type 72 fired all at once with same chances to hit.
We could call it 5 times from there, but then we'd have 5 times as much sound effects in takedamage() unless we accounted for it.
}
i:=round(i/100*(100-ships^[s].damages[DMG_WEAPONS])); { damages[DMG_WEAPONS] is attacker weapons subsystem. If it is not damaged, 'i' remains as above, or is reduced appropriately }
//writeln ('impact: attacker',s,' weapon',n, ' for dmgtype',j, '=', weapons[n].dmgtypes[j],'% and its damage dealing=', weapons[n].damage, 'GJ; THEIR CURRENT weapon subsystem damage=', ships^[s].damages[DMG_WEAPONS], '% ; their attack total i=', i, 'GJ, batt=', ships^[s].battery);
//writeln (' our shield', ship.shield, ' has level=', ship.shieldlevel);
if (ship.shieldlevel=0) or (ship.shield<=ID_NOSHIELD) or (weapons[b].dmgtypes[j]=0) then takedamage(j,i) { if no shield installed, or it is down, or that shield does not protect against that damagetype at all (like psi) - take full damage without affecting the shield}
else
begin { some shield is installed }
a:=round(weapons[b].dmgtypes[j]/100 * weapons[b].damage * ship.shieldlevel/100); { a=how much damage will we resist in GJ = pct. for that dmgtype * total max shield protection * current shield level percentage }
//writeln (' shield', ship.shield, ' resist for dmgtype',j, '=', weapons[b].dmgtypes[j], '% of total shield damage absorption=', weapons[b].damage, 'GJ; OUR CURRENT shield subsystem damage=',ship.damages[DMG_SHIELD],'% current shield level=', ship.shieldlevel,'% (wanted:',ship.shieldopt[SHLD_COMBAT_WANT],') ; our defense total a=', a, 'GJ');
if a<i then
begin { we've taken MORE damage than our shield can handle }
//writeln (' a<i: shield overload; taking residual damage for dmgtype',j,' = ', i-a, 'GJ');
takedamage(j,i-a); {if weapon deals 90GJ of damage, and our shield absorbs 80GJ of damage, there will be 10GJ of pass-through damage }
ship.shieldlevel:=0; { we've taken more damage than shields can handle, so set current level to zero (as it can't be negative). It will automatically slowly recover up to 100% or less if shield subsystem is damaged }
if (ship.shield=ID_REFLECTIVEHULL) and (j>DMGTYP_PSIONIC) then ship.damages[DMG_SHIELD]:=100; { damages[DMG_SHIELD] is as shield subsystem; shield=ID_REFLECTIVEHULL is reflective hull (50GJ, no psionic defence, about 33% for each of the rest; but it does not use energy }
{ psionic damage just passes through reflective hull and inflict damage, but if any of the other damage types physically damages the reflective hull to zero or below, whole reflective hull collapses }
displaydamage;
end
else
begin { we've taken LESS damage than our shield can handle }
a:=round((i/(ship.shieldlevel/100 *weapons[b].damage)*100)); { pct current shield level * total shield protection in GJ }
{ what are we actually calculating here in a?! looks strange, but seems to work in practice...
current_shield_protection = ship.shieldlevel/100 *weapons[b].damage; // pct current shield level * total shield protection in GJ. But it does not take into account damagetype? because it just wants to move our shield slider which is one type only 0-100%?
a := round (attacking_damage_in_GJ / current_shield_protection_in_GJ * 100 )
a is by how much would shield level in PCT be reduced
}
c:=ship.shieldlevel-a; { c will be our new ship.shieldlevel in pct }
//writeln (' shield still holding; shield protection a=', a, 'GJ, new shield level c=', c, '%');
if c<0 then
begin { shield would be reduced below zero }
//writeln (' Shield passing some damage afterall (total shield malfunction if reflective hull)');
takedamage(DMGTYP_FAKE_SHLD,random(3)+1); { shield subsystem takes 1-3 damage }
ship.shieldlevel:=0;
if (ship.shield=ID_REFLECTIVEHULL) and (j>DMGTYP_PSIONIC) then ship.damages[DMG_SHIELD]:=100;
displaydamage;
end
else
begin { shield stays in positive }
ship.shieldlevel:=c;
if (ship.shield=ID_REFLECTIVEHULL) and (j>DMGTYP_PSIONIC) then { hit to reflective hull actually damages shield subsystem, as it is passive defense }
begin
//writeln (' Shield stays above zero; reflective hull damage shield subsystem set to ', 100-c);
ship.damages[DMG_SHIELD]:=100-c;
displaydamage;
end;
end;
end;
end;
end;
displaystats;
end;
procedure moveships;
var r,rt: real;
a,j: integer;
begin
for j:=1 to nships do
with ships^[j] do
begin
if (moveindex=5) and (hullintegrity>0) and (damages[DMG_ENGINES]<90) then
begin
if (relx<5000) and (relx>0) and (dx<-3000) then inc(dx,accelmax)
else if (relx>-5000) and (relx<0) and (dx>3000) then dec(dx,accelmax)
else if (relx>0) and (dx>-1000) then dec(dx,accelmax)
else if (relx<0) and (dx<1000) then inc(dx,accelmax);
if (rely<5000) and (rely>0) and (dy<-3000) then inc(dy,accelmax)
else if (rely>-5000) and (rely<0) and (dy>3000) then dec(dy,accelmax)
else if (rely>0) and (dy>-1000) then dec(dy,accelmax)
else if (rely<0) and (dy<1000) then inc(dy,accelmax);
if (relz<5000) and (relz>0) and (dz<-3000) then inc(dz,accelmax)
else if (relz>-5000) and (relz<0) and (dz>3000) then dec(dz,accelmax)
else if (relz>0) and (dz>-1000) then dec(dz,accelmax)
else if (relz<0) and (dz<1000) then inc(dz,accelmax);
end;
if (moveindex=5) and (hullintegrity>0) and (damages[DMG_ENGINES]>90) then
begin
dx := round(dx * 0.9);
dy := round(dy * 0.9);
dz := round(dz * 0.9);
end;
relx:=relx+round(dx/5);
rely:=rely+round(dy/5);
relz:=relz+round(dz/5);
rt:=(relx/10)*(relx/10);
rt:=rt+(rely/10)*(rely/10);
rt:=rt+(relz/10)*(relz/10);
r:=sqrt(rt)*100;
// writeln('ship ',j,' : ',r);
a:=ship.accelmax;
if ship.damages[DMG_ENGINES]>89 then a:=a div 4;
if shipdir<4 then rely:=rely+a
else if shipdir>6 then rely:=rely-a;
if shipdir mod 3=1 then relx:=relx+a
else if shipdir mod 3=0 then relx:=relx-a;
if shipdir2=1 then relz:=relz-a
else if shipdir2=2 then relz:=relz+a;
part:=ships^[j].range;
if hullintegrity>0 then
for a:=1 to 20 do
if (charges[a]=100) then
begin
if part>=r then
begin
i:=random(120)-15*ship.options[OPT_DIFFICULTY];
{if (i<skill) or ((scanning) and (random(100)<20)) then}
if not SkillTest(True, 4, skill + (ord(scanning) * 20), learnchance) then
begin
displaymap;
if (ships^[j].damages[DMG_WEAPONS] < 100) and { enemy ship can only fire if it's weapons subsystem is not completely destroyed }
(ships^[j].battery > 0) then { and it's battery is not completely deplated }
impact(j,maxweapons); { j=enemyship, second param is weapon: currently always 72 "Alien weapon - debug" }
//FIXME: realistically we should cycle through ships^[j].gunnodes[] -- but that would require tracking their energy, using power, AI for firing etc... and would produce 5 times as much sound effects.
displaymap;
end;
charges[a]:=0;
end;
end;
if (abs(r)>1200000) then
begin
hullintegrity:=0;
end;
if (hullintegrity=0) and (targetindex=j) then
begin
targetindex:=1;
while (targetindex<=nships) and (ships^[targetindex].hullintegrity=0) do inc(targetindex);
if targetindex>nships then
begin
done_:=true;
end;
end;
end;
if moveindex=5 then moveindex:=0 else inc(moveindex);
end;
procedure previoustarget;
begin
displaymap;
dec(targetindex);
while (targetindex>0) and (ships^[targetindex].hullintegrity=0) do dec(targetindex);
if (targetindex=0) then
begin
targetindex:=nships;
while (targetindex>0) and (ships^[targetindex].hullintegrity=0) do dec(targetindex);
end;
displaymap;
end;
procedure nexttarget;
begin
displaymap;
inc(targetindex);
while (targetindex<=nships) and (ships^[targetindex].hullintegrity=0) do inc(targetindex);
if (targetindex>nships) or (ships^[targetindex].hullintegrity=0) then
begin
targetindex:=1;
while (targetindex<nships) and (ships^[targetindex].hullintegrity=0) do inc(targetindex);
end;
displaymap;
end;
procedure switchalienmode;
begin
mousehide;
for i:=125 to 189 do
scr_fillchar(screen[i,6],93,0);
if not alienpicmode then
begin
for i:=156 to 189 do
scrto_move(asdisplay^[i,6],screen[i,6],93);
alienpicmode:=true;
end
else
begin
for i:=125 to 155 do
scrto_move(asdisplay^[i,6],screen[i,6],93);
alienpicmode:=false;
end;
mouseshow;
end;
procedure setdir(d: integer);
begin
if ship.damages[DMG_ENGINES]>89 then exit;
drawdirection(0);
shipdir:=d;
if d=5 then shipdir2:=0;
drawdirection(63);
end;
procedure setdir2(d: integer);
begin
if ship.damages[DMG_ENGINES]>89 then exit;
drawdirection(0);
shipdir2:=d;
drawdirection(63);
end;
procedure findtarget;
var j: integer;
begin
if (mouse.x<6) or (mouse.x>259) or (mouse.y<6) or (mouse.y>117) then exit;
for j:=1 to nships do
begin
z:=round(ships^[j].relz/range*26);
y:=62+round(ships^[j].rely/range*26.66)-z;
x:=132+round(ships^[j].relx/range*119);
if (abs(mouse.x-x)<5) and (abs(mouse.y-y)<5) and (ships^[j].hullintegrity>0) then
begin
displaymap;
targetindex:=j;
displaymap;
displaytargetinfo;
j:=nships;
end;
end;
end;
procedure displaytimedelay;
var
s: string[3];
begin
tcolor:=63;
str(ship.options[OPT_TIMESLICE]:3,s);
mousehide;
printxy(277,2,s);
mouseshow;
tcolor:=95;
end;
procedure findmouse;
begin
if not mouse.getstatus then exit;
case mouse.x of
105..125: case mouse.y of
131..151: fireweapon:=1;
152..156: if (mouse.x>108) and (mouse.x<122) then
begin
if userpowerup[1] then
begin
plainfadearea(109,152,121,154,32);
userpowerup[1]:=false;
end
else
begin
plainfadearea(109,152,121,154,-32);
userpowerup[1]:=true;
end;
end
else findtarget;
157..161: if (mouse.x>108) and (mouse.x<122) then
begin
if userpowerup[6] then
begin
plainfadearea(109,159,121,161,32);
userpowerup[6]:=false;
end
else
begin
plainfadearea(109,159,121,161,-32);
userpowerup[6]:=true;
end;
end
else findtarget;
162..182: fireweapon:=6;
else findtarget;
end;
128..148: case mouse.y of
131..151: fireweapon:=2;
152..156: if (mouse.x>131) and (mouse.x<145) then
begin
if userpowerup[2] then
begin
plainfadearea(132,152,144,154,32);
userpowerup[2]:=false;
end
else
begin
plainfadearea(132,152,144,154,-32);
userpowerup[2]:=true;
end;
end
else findtarget;
157..161: if (mouse.x>131) and (mouse.x<145) then
begin
if userpowerup[7] then
begin
plainfadearea(132,159,144,161,32);
userpowerup[7]:=false;
end
else
begin
plainfadearea(132,159,144,161,-32);
userpowerup[7]:=true;
end;
end
else findtarget;