-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrun_raid.c
2060 lines (2039 loc) · 65.8 KB
/
run_raid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
harris - a strategy game
Copyright (C) 2012-2015 Edward Cree
licensed under GPLv3+ - see top of harris.c for details
run_raid: the raid in-progress screen
*/
#include "run_raid.h"
#include <math.h>
#include "ui.h"
#include "globals.h"
#include "date.h"
#include "almanack.h"
#include "history.h"
#include "render.h"
#include "routing.h"
#include "rand.h"
#include "weather.h"
#include "geom.h"
#include "control.h"
atg_element *run_raid_box;
char *RB_time_label;
atg_element *RB_map;
atg_element *RB_kill_box[10];
unsigned int fill_kill_box;
unsigned int totalraids;
int **dij, **nij, **tij, **lij;
unsigned int *heat;
bool *canscore;
double bridge, cidam;
int clear_raids(game *state)
{
if(!state) return(1);
for(unsigned int i=0;i<ntargs;i++)
{
state->raids[i].nbombers=0;
free(state->raids[i].bombers);
state->raids[i].bombers=NULL;
state->raids[i].zerohour=RRT(1,0);
state->raids[i].routed=false;
}
return(0);
}
bool identify_location(int x, int y, int *lr)
{
int mi=-1, md=50;
for(unsigned int i=0;i<nlocs;i++)
{
int d=(locs[i].lon-x)*(locs[i].lon-x)+(locs[i].lat-y)*(locs[i].lat-y);
if(d<md)
{
mi=i;
md=d;
}
}
if(mi<0)
{
if(lr) *lr=region[x][y];
return(false);
}
if(lr) *lr=mi;
return(true);
}
/* The string returned by this function should not be free()d,
* and may be overwritten by subsequent calls */
const char *describe_location(int x, int y)
{
static char cbuf[80];
int lri;
if(!identify_location(x, y, &lri))
{
snprintf(cbuf, 80, "in %s", regions[lri].name);
}
else
{
int dx=x-locs[lri].lon,
dy=y-locs[lri].lat,
th=floor(atan2(dy, dx)*4/M_PI+4.5),
d=hypot(dx, dy)*3;
const char *dir=(const char *[8]){"W", "NW", "N", "NE", "E", "SE", "S", "SW"}[th%8];
if(d==0)
snprintf(cbuf, 80, "at %s", locs[lri].name);
else
snprintf(cbuf, 80, "%umi %s of %s", d, dir, locs[lri].name);
}
return(cbuf);
}
void scroll_kill_box(void)
{
for(unsigned int i=0;i<5;i++)
{
atg_element tmp=*RB_kill_box[i];
*RB_kill_box[i]=*RB_kill_box[i+5];
*RB_kill_box[i+5]=tmp;
atg_ebox_empty(RB_kill_box[i+5]);
RB_kill_box[i+5]->hidden=true;
}
fill_kill_box=5;
}
void describe_cr_helper(atg_colour colour, SDL_Surface *pic, unsigned int sqn, const char *loc, const char *reason, SDL_Surface *kpic)
{
if(fill_kill_box>9)
scroll_kill_box();
atg_element *kb=RB_kill_box[fill_kill_box++];
kb->hidden=false;
atg_element *tpic=atg_create_element_image(pic);
if(!tpic)
{
fprintf(stderr, "atg_create_element_image failed\n");
}
else
{
tpic->w=38;
if(atg_ebox_pack(kb, tpic))
{
perror("atg_ebox_pack");
atg_free_element(tpic);
}
}
atg_element *text_box=atg_create_element_box(ATG_BOX_PACK_VERTICAL, (atg_colour){23, 27, 35, ATG_ALPHA_OPAQUE}), *text_line=NULL;
if(text_box)
{
if(atg_ebox_pack(kb, text_box))
{
perror("atg_ebox_pack");
atg_free_element(text_box);
}
else
{
atg_element *shim=atg_create_element_box(ATG_BOX_PACK_HORIZONTAL, (atg_colour){23, 27, 35, ATG_ALPHA_OPAQUE});
if(shim)
{
shim->h=11;
if(atg_ebox_pack(text_box, shim))
{
perror("atg_ebox_pack");
atg_free_element(shim);
}
else
{
text_line=atg_create_element_box(ATG_BOX_PACK_HORIZONTAL, (atg_colour){23, 27, 35, ATG_ALPHA_OPAQUE});
if(text_line)
{
if(atg_ebox_pack(text_box, text_line))
{
perror("atg_ebox_pack");
atg_free_element(text_line);
text_line=NULL;
}
}
}
}
}
}
if(!text_line)
text_line=kb;
if(sqn) {
char snum[16];
snprintf(snum, sizeof(snum), " of %u Sqn", sqn);
atg_element *crs=atg_create_element_label(snum, 14, colour);
if (!crs)
{
fprintf(stderr, "atg_create_element_label failed\n");
}
else
{
if(atg_ebox_pack(text_line, crs))
{
perror("atg_ebox_pack");
atg_free_element(crs);
}
}
}
atg_element *crl=atg_create_element_label(" crashed ", 14, colour);
if(!crl)
{
fprintf(stderr, "atg_create_element_label failed\n");
}
else
{
if(atg_ebox_pack(text_line, crl))
{
perror("atg_ebox_pack");
atg_free_element(crl);
}
}
atg_element *locl=atg_create_element_label(loc, 14, colour);
if(!locl)
{
fprintf(stderr, "atg_create_element_label failed\n");
}
else
{
if(atg_ebox_pack(text_line, locl))
{
perror("atg_ebox_pack");
atg_free_element(locl);
}
}
if(kpic)
{
atg_element *rl=atg_create_element_label(" - killer: ", 14, colour);
if(!rl)
{
fprintf(stderr, "atg_create_element_label failed\n");
}
else
{
if(atg_ebox_pack(text_line, rl))
{
perror("atg_ebox_pack");
atg_free_element(rl);
}
}
atg_element *ri=atg_create_element_image(kpic);
if(!ri)
{
fprintf(stderr, "atg_create_element_image failed\n");
}
else
{
if(atg_ebox_pack(kb, ri))
{
perror("atg_ebox_pack");
atg_free_element(ri);
}
}
}
else
{
atg_element *rl=atg_create_element_label(reason, 14, colour);
if(!rl)
{
fprintf(stderr, "atg_create_element_label failed\n");
}
else
{
if(atg_ebox_pack(text_line, rl))
{
perror("atg_ebox_pack");
atg_free_element(rl);
}
}
}
}
void describe_crb(const game *state, unsigned int k)
{
ac_bomber b=state->bombers[k];
int x=b.lon, y=b.lat;
char reason[80]="";
SDL_Surface *kpic=NULL;
switch(b.ld.ds)
{
case DS_NONE: /* should never happen */
snprintf(reason, 80, " with no prior damage");
break;
case DS_MECH:
snprintf(reason, 80, " - killer: mechanical failure");
break;
case DS_FLAK:;
flaksite fs=flaks[b.ld.idx];
snprintf(reason, 80, " - killer: flak %s", describe_location(fs.lon, fs.lat));
break;
case DS_TFLK:;
target t=targs[b.ld.idx];
snprintf(reason, 80, " - killer: flak at %s", t.name);
break;
case DS_FIGHTER:;
ac_fighter f=state->fighters[b.ld.idx];
snprintf(reason, 80, " - killer: %s %s", ftypes[f.type].manu, ftypes[f.type].name);
kpic=ftypes[f.type].picture;
break;
default:
snprintf(reason, 80, " - killer: a bug (ds=%d, idx=%u)", b.ld.ds, b.ld.idx);
break;
}
const char *loc=describe_location(x, y);
unsigned int sqn=0;
if(b.squadron>=0)
sqn=state->squads[b.squadron].number;
describe_cr_helper((atg_colour){255, 127, 0, ATG_ALPHA_OPAQUE}, types[b.type].picture, sqn, loc, reason, kpic);
bool marked=types[b.type].markname[0] != NULL;
fprintf(stderr, "%s %s%s%s of %d Sqn crashed %s%s\n", types[b.type].manu, types[b.type].name, marked?" ":"", marked?types[b.type].markname[b.mark]:"", sqn, loc, reason);
}
void describe_crf(const game *state, unsigned int j)
{
ac_fighter f=state->fighters[j];
int x=f.lon, y=f.lat;
char reason[80]="";
SDL_Surface *kpic=NULL;
switch(f.ld.ds)
{
case DS_NONE: /* should never happen */
snprintf(reason, 80, " with no prior damage");
break;
case DS_FUEL:
snprintf(reason, 80, " - killer: fuel exhaustion");
break;
case DS_BOMBER:;
ac_bomber b=state->bombers[f.ld.idx];
snprintf(reason, 80, " - killer: %s %s", types[b.type].manu, types[b.type].name);
kpic=types[b.type].picture;
break;
default:
snprintf(reason, 80, " - killer: a bug (ds=%d, idx=%u)", f.ld.ds, f.ld.idx);
break;
}
const char *loc=describe_location(x, y);
describe_cr_helper((atg_colour){127, 255, 127, ATG_ALPHA_OPAQUE}, ftypes[f.type].picture, 0, loc, reason, kpic);
fprintf(stderr, "%s %s crashed %s%s\n", ftypes[f.type].manu, ftypes[f.type].name, loc, reason);
}
crewman *get_crew(const game *state, unsigned int i, unsigned int j)
{
if(state->bombers[i].crew[j]<0) return(NULL);
return(state->crews+state->bombers[i].crew[j]);
}
double crewman_skill(const crewman *c, unsigned int type)
{
double skill=c->skill;
if(types[type].heavy)
skill*=0.2+c->heavy/80.0;
if(types[type].lfs)
skill*=0.2+c->lanc/80.0;
return skill;
}
double get_skill(const game *state, unsigned int i, unsigned int j)
{
const crewman *c=get_crew(state, i, j);
return crewman_skill(c, state->bombers[i].type);
}
#define practise(_c, _v) do { \
double _new = min((_c).skill * (1 - (_v)/100.0) + (_v) * (_c).lrate / 100.0, 100.0); \
if(floor(_new) > floor((_c).skill)) \
sk_append(&state->hist, state->now, now, (_c).id, (_c).class, _new);\
(_c).skill = _new; \
} while(0);
int wintense(const ac_bomber *b)
{
static const int basew[NWINLVLS] = {
[WL_NONE] = 0,
[WL_NORMAL] = 1,
[WL_EXTRA] = 2,
[WL_FULL] = 4,
};
winlvl w=b->window;
if(w>=NWINLVLS)
return 0;
if(b->routestage==4)
w=min(w+1, WL_FULL);
return basew[w];
}
double wintai(const ac_bomber *b)
{
static const double basew[NWINLVLS] = {
[WL_NONE] = 1.0,
[WL_NORMAL] = 0.8,
[WL_EXTRA] = 0.68,
[WL_FULL] = 0.56,
};
winlvl w=b->window;
if(w>=NWINLVLS)
return 1.0;
if(b->routestage==4)
w=min(w+1, WL_FULL);
return basew[w];
}
int run_raid_create(void)
{
run_raid_box=atg_create_element_box(ATG_BOX_PACK_VERTICAL, GAME_BG_COLOUR);
if(!run_raid_box)
{
fprintf(stderr, "atg_create_element_box failed\n");
return(1);
}
atg_element *RB_hbox=atg_create_element_box(ATG_BOX_PACK_HORIZONTAL, GAME_BG_COLOUR);
if(!RB_hbox)
{
fprintf(stderr, "atg_create_element_box failed\n");
return(1);
}
if(atg_ebox_pack(run_raid_box, RB_hbox))
{
perror("atg_ebox_pack");
return(1);
}
if(!(RB_time_label=malloc(6)))
{
perror("malloc");
return(1);
}
snprintf(RB_time_label, 6, "--:--");
atg_element *RB_time=atg_create_element_label_nocopy(RB_time_label, 12, (atg_colour){175, 199, 255, ATG_ALPHA_OPAQUE});
if(!RB_time)
{
fprintf(stderr, "atg_create_element_label failed\n");
return(1);
}
RB_time->w=239;
if(atg_ebox_pack(RB_hbox, RB_time))
{
perror("atg_ebox_pack");
return(1);
}
RB_map=atg_create_element_image(terrain);
if(!RB_map)
{
fprintf(stderr, "atg_create_element_image failed\n");
return(1);
}
RB_map->h=terrain->h+2;
if(atg_ebox_pack(RB_hbox, RB_map))
{
perror("atg_ebox_pack");
return(1);
}
atg_element *kills_box=atg_create_element_box(ATG_BOX_PACK_VERTICAL, GAME_BG_COLOUR);
if(!kills_box)
{
fprintf(stderr, "atg_create_element_box failed\n");
return(1);
}
kills_box->w=800;
kills_box->h=440;
if(atg_ebox_pack(run_raid_box, kills_box))
{
perror("atg_ebox_pack");
return(1);
}
for(unsigned int i=0;i<10;i++)
{
if(!(RB_kill_box[i]=atg_create_element_box(ATG_BOX_PACK_HORIZONTAL, (atg_colour){23, 27, 35, ATG_ALPHA_OPAQUE})))
{
fprintf(stderr, "atg_create_element_box failed\n");
return(1);
}
RB_kill_box[i]->h=40;
RB_kill_box[i]->w=800;
RB_kill_box[i]->hidden=true;
if(atg_ebox_pack(kills_box, RB_kill_box[i]))
{
perror("atg_ebox_pack");
return(1);
}
atg_element *shim=atg_create_element_box(ATG_BOX_PACK_HORIZONTAL, GAME_BG_COLOUR);
if(!shim)
{
fprintf(stderr, "atg_create_element_box failed\n");
return(1);
}
shim->h=4;
if(atg_ebox_pack(kills_box, shim))
{
perror("atg_ebox_pack");
return(1);
}
}
return(0);
}
screen_id run_raid_screen(atg_canvas *canvas, game *state)
{
state->roe.idtar=datebefore(state->now, event[EVENT_CIV]);
double moonphase=pom(state->now);
double moonillum=foldpom(moonphase);
double flakscale=state->gprod[ICLASS_ARM]/(GET_DC(state,FLAK)*12000.0);
unsigned int rcity=GET_DC(state,RCITY),
rindus=GET_DC(state,RINDUS),
rship=GET_DC(state,RSHIP),
rleaf=GET_DC(state,RLEAF),
rother=GET_DC(state,ROTHER);
double d_fsr=GET_DC(state,FSR)/10.0;
unsigned int fightersleft;
totalraids=0;
fightersleft=state->nfighters;
for(unsigned int i=0;i<ntargs;i++)
{
totalraids+=state->raids[i].nbombers;
targs[i].threat=0;
targs[i].nfighters=0;
targs[i].fires=0;
targs[i].skym=-1;
targs[i].window=0;
}
for(unsigned int i=0;i<nflaks;i++)
{
flaks[i].ftr=-1;
flaks[i].window=0;
}
for(unsigned int i=0;i<state->nfighters;i++)
{
state->fighters[i].targ=-1;
state->fighters[i].k=-1;
state->fighters[i].hflak=-1;
state->fighters[i].damage=0;
state->fighters[i].ld.ds=DS_NONE;
state->fighters[i].landed=true;
state->fighters[i].lat=fbases[state->fighters[i].base].lat;
state->fighters[i].lon=fbases[state->fighters[i].base].lon;
}
if(totalraids)
{
if(RB_time_label) snprintf(RB_time_label, 6, "12:00");
bool stream=!datebefore(state->now, event[EVENT_GEE]),
moonshine=!datebefore(state->now, event[EVENT_MOONSHINE]),
window=!datebefore(state->now, event[EVENT_WINDOW]),
wairad= datewithin(state->now, event[EVENT_WINDOW], event[EVENT_L_SN]),
berlin=!datebefore(state->now, event[EVENT_LONDON]);
unsigned int it=0, startt=9999;
for(unsigned int i=0;i<ntargs;i++)
{
unsigned int halfhalf=0; // count for halfandhalf bombloads
for(unsigned int j=0;j<state->raids[i].nbombers;j++)
{
unsigned int k=state->raids[i].bombers[j], type=state->bombers[k].type;
int s=state->bombers[k].squadron;
if (s<0)
{
fprintf(stderr, "Warning: internal squadron error\n");
s=0;
}
unsigned int b=state->squads[s].base;
signed int blon=base_lon(bases[b]), blat=base_lat(bases[b]);
bool unpaved=types[type].heavy&&!bases[b].paved;
state->bombers[k].targ=i;
state->bombers[k].lat=blat;
state->bombers[k].lon=blon;
state->bombers[k].routestage=0;
if(stream)
for(unsigned int l=0;l<8;l++)
{
state->bombers[k].route[l][0]=targs[i].route[l][0];
state->bombers[k].route[l][1]=targs[i].route[l][1];
}
else
genroute((unsigned int [2]){blat, blon}, i, state->bombers[k].route, state, 100);
double dist=hypot(blat-(signed)state->bombers[k].route[0][0], blon-(signed)state->bombers[k].route[0][1]), outward=dist;
for(unsigned int l=0;l<7;l++)
{
double d=hypot((signed)state->bombers[k].route[l+1][0]-(signed)state->bombers[k].route[l][0], (signed)state->bombers[k].route[l+1][1]-(signed)state->bombers[k].route[l][1]);
dist+=d;
if(l<4) outward+=d;
}
unsigned int cap=bstats(state->bombers[k]).capwt;
if(unpaved)
cap-=cap/4;
state->bombers[k].bombed=false;
state->bombers[k].crashed=false;
state->bombers[k].landed=false;
state->bombers[k].idtar=false;
state->bombers[k].ld.ds=DS_NONE;
state->bombers[k].lat+=rand()*3.0/RAND_MAX-1;
state->bombers[k].lon+=rand()*3.0/RAND_MAX-1;
state->bombers[k].navlat=0;
state->bombers[k].navlon=0;
state->bombers[k].driftlat=0;
state->bombers[k].driftlon=0;
state->bombers[k].flakreport=-1;
double askill=0; // Airmanship
for(unsigned int l=0;l<MAX_CREW;l++)
{
if(bstats(state->bombers[k]).crew[l]==CCLASS_E)
{
askill=get_skill(state, k, l);
break;
}
else if(bstats(state->bombers[k]).crew[l]==CCLASS_P)
{
askill=max(askill, get_skill(state, k, l)*.5);
}
}
state->bombers[k].speed=(bstats(state->bombers[k]).speed+askill/20.0-2.0-state->bombers[k].wear/20.0)/450.0;
bombload load=is_pff(state, k)?state->raids[i].pffloads[type]:state->raids[i].loads[type];
/* Cookies sticking out of the bomb bay slow us down */
if(targs[i].class==TCLASS_CITY&&(load==BL_PLUMDUFF||load==BL_PONLY)&&types[i].smbay)
state->bombers[k].speed*=0.94;
if(stream)
{
// aim for Zero Hour 01:00 plus up to 10 minutes
// PFF should arrive at Zero minus 6, and be finished by Zero minus 2
// Zero Hour is t=840, and a minute is two t-steps
int tt=is_pff(state, k)?(state->raids[i].zerohour-12+irandu(8)):(state->raids[i].zerohour+irandu(20));
int st=tt-(outward/state->bombers[k].speed)-3;
if(is_pff(state, k)) st-=3;
if(st<0)
{
tt-=st;
st=0;
}
state->bombers[k].startt=st;
}
else
state->bombers[k].startt=(state->raids[i].zerohour-480)+irandu(90); // 2100 to 2145
startt=min(startt, state->bombers[k].startt);
ra_append(&state->hist, state->now, maketime(state->bombers[k].startt), state->bombers[k].id, false, state->bombers[k].type, i);
double eff=0.98+askill/1e3; // engineer fuel factor
state->bombers[k].fuelt=state->bombers[k].startt+bstats(state->bombers[k]).range*0.6*eff*(unpaved?0.8:1.0)/(double)state->bombers[k].speed;
unsigned int eta=state->bombers[k].startt+outward*1.1/(double)state->bombers[k].speed+12;
if(!stream) eta+=36;
if(eta>state->bombers[k].fuelt)
{
unsigned int fu=eta-state->bombers[k].fuelt;
state->bombers[k].fuelt+=fu;
cap*=120.0/(120.0+fu);
}
else
state->bombers[k].fuelt=eta;
state->bombers[k].b_hc=0;
state->bombers[k].b_gp=0;
state->bombers[k].b_in=0;
state->bombers[k].b_ti=0;
state->bombers[k].b_le=0;
unsigned int bulk=bstats(state->bombers[k]).capbulk;
bool inext=true;
switch(targs[i].class)
{
case TCLASS_LEAFLET:
state->bombers[k].b_le=min(bulk*3, cap*20);
break;
case TCLASS_CITY:
switch(load)
{
case BL_PLUMDUFF:
if(cap>=4000) // cookie + gp+in mix
{
transfer(4000, cap, state->bombers[k].b_hc);
if(types[type].smbay) // Halifax: 2 cookies + gp
{
if(cap>=4000)
transfer(4000, cap, state->bombers[k].b_hc);
bulk=11000-loadbulk(state->bombers[k]);
transfer(bulk, cap, state->bombers[k].b_gp);
}
else
{
while(cap&&(bulk=bstats(state->bombers[k]).capbulk-loadbulk(state->bombers[k])))
{
if(inext)
transfer(min(bulk/1.5, 800), cap, state->bombers[k].b_in);
else
transfer(min(bulk, 500), cap, state->bombers[k].b_gp);
inext=!inext;
}
}
}
else // can't take a cookie, so just gp+in mix (but more gp than BL_USUAL)
{
while(cap&&(bulk=bstats(state->bombers[k]).capbulk-loadbulk(state->bombers[k])))
{
if(inext)
transfer(min(bulk/1.5, 300), cap, state->bombers[k].b_in);
else
transfer(min(bulk, 1000), cap, state->bombers[k].b_gp);
inext=!inext;
}
}
break;
case BL_USUAL:
if(types[type].load[BL_PLUMDUFF]&&bstats(state->bombers[k]).capwt>=10000&&cap>4000&&!types[type].smbay) // cookie + incendiaries
{
transfer(4000, cap, state->bombers[k].b_hc);
bulk=bstats(state->bombers[k]).capbulk-loadbulk(state->bombers[k]);
state->bombers[k].b_in=min(cap, bulk/1.5);
}
else // gp+in mix
{
while(cap&&(bulk=bstats(state->bombers[k]).capbulk-loadbulk(state->bombers[k])))
{
if(inext)
transfer(min(bulk/1.5, 800), cap, state->bombers[k].b_in);
else
transfer(min(bulk, types[type].inc?250:500), cap, state->bombers[k].b_gp);
inext=!inext;
}
}
break;
case BL_ARSON:
state->bombers[k].b_in=min(cap, bulk/1.5);
break;
case BL_HALFHALF:
if(cap>=4000&&(halfhalf++%2))
{
state->bombers[k].b_hc=(cap/4000)*4000;
break;
}
// else fallthrough
case BL_ILLUM:
while(cap&&(bulk=bstats(state->bombers[k]).capbulk-loadbulk(state->bombers[k])))
{
if(inext)
transfer(min(bulk/2.0, 250), cap, state->bombers[k].b_ti);
else
transfer(min(bulk, 500), cap, state->bombers[k].b_gp);
inext=!inext;
}
break;
case BL_PPLUS: // LanX, up to 12,000lb cookie; LanI, up to 8,000lb cookie
transfer(min(bstats(state->bombers[k]).capwt/5333, cap/4000)*4000, cap, state->bombers[k].b_hc);
state->bombers[k].b_gp=cap;
break;
case BL_PONLY:
if(cap>=4000)
{
state->bombers[k].b_hc=(cap/4000)*4000;
break;
}
// else fallthrough
case BL_ABNORMAL:
default:
state->bombers[k].b_gp=cap;
break;
}
break;
default: // all other targets use all-GP loads
state->bombers[k].b_gp=cap;
break;
}
#define b_roundto(what, n) state->bombers[k].b_##what=(state->bombers[k].b_##what/n)*n
b_roundto(hc, 4000); // should never affect anything
b_roundto(gp, 50);
b_roundto(in, 10);
b_roundto(ti, 50);
b_roundto(le, 1000);
//fprintf(stderr, "%s: %ulb hc + %u lb gp + %ulb in + %ulb ti = %ulb\n", types[type].name, state->bombers[k].b_hc, state->bombers[k].b_gp, state->bombers[k].b_in, state->bombers[k].b_ti, loadweight(state->bombers[k]));
#undef b_roundto
state->bombers[k].window=state->raids[i].window[type];
}
}
oboe.k=-1;
atg_image *map_img=RB_map->elemdata;
SDL_FreeSurface(map_img->data);
SDL_Surface *with_flak_and_target, *with_weather; /* with_weather also has route */
map_img->data=SDL_ConvertSurface(terrain, terrain->format, terrain->flags);
with_flak_and_target=SDL_ConvertSurface(terrain, terrain->format, terrain->flags);
SDL_FreeSurface(flak_overlay);
flak_overlay=render_flak(state->now);
SDL_BlitSurface(flak_overlay, NULL, with_flak_and_target, NULL);
SDL_FreeSurface(target_overlay);
city_overlay=render_cities();
SDL_BlitSurface(city_overlay, NULL, with_flak_and_target, NULL);
target_overlay=render_targets(state->now);
SDL_BlitSurface(target_overlay, NULL, with_flak_and_target, NULL);
with_weather=SDL_ConvertSurface(with_flak_and_target, with_flak_and_target->format, with_flak_and_target->flags);
SDL_FreeSurface(weather_overlay);
weather_overlay=render_weather(&state->weather);
SDL_BlitSurface(weather_overlay, NULL, with_weather, NULL);
SDL_FreeSurface(route_overlay);
route_overlay=render_routes(state);
SDL_BlitSurface(route_overlay, NULL, with_weather, NULL);
SDL_Surface *with_ac=SDL_ConvertSurface(with_weather, with_weather->format, with_weather->flags);
SDL_Surface *ac_overlay=render_ac(state);
SDL_BlitSurface(ac_overlay, NULL, with_ac, NULL);
SDL_BlitSurface(with_ac, NULL, map_img->data, NULL);
for(unsigned int i=0;i<10;i++)
{
atg_ebox_empty(RB_kill_box[i]);
RB_kill_box[i]->hidden=true;
}
fill_kill_box=0;
unsigned int inair=totalraids, t=0;
cidam=0;
bridge=0;
// Tame Boar raid tracking
bool tameboar=!datebefore(state->now, event[EVENT_TAMEBOAR]);
unsigned int boxes[16][16]; // 10x10 boxes starting at (89,40)
int topx=-1, topy=-1; // co-ords of fullest box
unsigned int sumx, sumy; // sum of offsets within box
double velx, vely; // sum of velocity components ditto
while(t<startt)
{
t++;
if((!(t&3))&&(it<720))
{
w_iter(&state->weather, lorw);
it++;
}
}
SDL_FreeSurface(weather_overlay);
weather_overlay=render_weather(&state->weather);
SDL_BlitSurface(with_flak_and_target, NULL, with_weather, NULL);
SDL_BlitSurface(weather_overlay, NULL, with_weather, NULL);
sun_overlay=render_sun(convert_ht(maketime(t)));
while(inair)
{
t++;
harris_time now = maketime(t);
if(RB_time_label) snprintf(RB_time_label, 6, "%02u:%02u", now.hour, now.minute);
if((!(t&3))&&(it<720))
{
w_iter(&state->weather, lorw);
SDL_FreeSurface(weather_overlay);
weather_overlay=render_weather(&state->weather);
SDL_BlitSurface(with_flak_and_target, NULL, with_weather, NULL);
SDL_BlitSurface(weather_overlay, NULL, with_weather, NULL);
SDL_BlitSurface(route_overlay, NULL, with_weather, NULL);
it++;
}
if(!(t&7))
{
SDL_FreeSurface(sun_overlay);
sun_overlay=render_sun(convert_ht(now));
}
if(tameboar)
memset(boxes, 0, sizeof(boxes));
sumx=sumy=0;
velx=vely=0;
for(unsigned int i=0;i<ntargs;i++)
{
targs[i].shots=0;
targs[i].window*=0.98;
}
for(unsigned int i=0;i<nflaks;i++)
{
flaks[i].shots=0;
flaks[i].heat=0;
flaks[i].window*=0.98;
}
for(unsigned int i=0;i<ntargs;i++)
for(unsigned int j=0;j<state->raids[i].nbombers;j++)
{
unsigned int k=state->raids[i].bombers[j], type=state->bombers[k].type;
if(t<state->bombers[k].startt) continue;
if(state->bombers[k].crashed||state->bombers[k].landed) continue;
if(state->bombers[k].damage+(state->bombers[k].wear/2.0)>=100)
{
cr_append(&state->hist, state->now, now, state->bombers[k].id, false, state->bombers[k].type);
state->bombers[k].crashed=true;
describe_crb(state, k);
inair--;
continue;
}
double askill=0; // Airmanship
double sdc=0; // Damage control
double nskill=0; // Navigator
crewman *bac=NULL; // Bomb-aimer
for(unsigned int l=0;l<MAX_CREW;l++)
{
if(bstats(state->bombers[k]).crew[l]==CCLASS_E)
{
sdc+=(askill=get_skill(state, k, l));
}
else if(bstats(state->bombers[k]).crew[l]==CCLASS_W)
{
sdc+=get_skill(state, k, l);
}
else if(bstats(state->bombers[k]).crew[l]==CCLASS_N)
{
nskill=get_skill(state, k, l);
if(!bac)
bac=get_crew(state, k, l);
}
else if(bstats(state->bombers[k]).crew[l]==CCLASS_B)
{
bac=get_crew(state, k, l);
}
else if(bstats(state->bombers[k]).crew[l]==CCLASS_P)
{
// we assume P are always before E in crew order
// thus we get "max of Pskill*.5" if there are no E, otherwise we get Eskill.
askill=max(askill, get_skill(state, k, l)*.5);
}
}
if(brandp(bstats(state->bombers[k]).fail/(50.0*min(240.0, 48.0+t-state->bombers[k].startt))+state->bombers[k].damage/2400.0))
{
// E practise
for(unsigned int l=1;l<MAX_CREW;l++)
if(bstats(state->bombers[k]).crew[l]==CCLASS_E)
practise(*get_crew(state, k, l), 0.05);
if(!brandp(askill/200))
{
if(state->bombers[k].ld.ds==DS_NONE)
state->bombers[k].ld.ds=DS_MECH;
fa_append(&state->hist, state->now, now, state->bombers[k].id, false, state->bombers[k].type, 1);
state->bombers[k].failed=true;
if(brandp((1.0+state->bombers[k].damage/50.0)/(240.0-bstats(state->bombers[k]).fail*5.0)))
{
cr_append(&state->hist, state->now, now, state->bombers[k].id, false, state->bombers[k].type);
state->bombers[k].crashed=true;
describe_crb(state, k);
inair--;
continue;
}
}
}
unsigned int stage=state->bombers[k].routestage;
while((stage<8)&&(t>RRT(7,0)||!(state->bombers[k].route[stage][0]||state->bombers[k].route[stage][1])))
stage=++state->bombers[k].routestage;
bool home=(state->bombers[k].failed&&!state->bombers[k].bombed)||(stage>=8);
double altitude=bstats(state->bombers[k]).alt+(irandu(askill)-10)/10.0-state->bombers[k].wear/20.0;
if((stage==4)&&state->bombers[k].nav[NAV_OBOE]&&xyr(state->bombers[k].lon-oboe.lon, state->bombers[k].lat-oboe.lat, 50+altitude*.3)) // OBOE
{
if(oboe.k==-1)
oboe.k=k;
if(oboe.k==(int)k)
{
state->bombers[k].navlon=state->bombers[k].navlat=0;
state->bombers[k].fix=true;
}
// the rest of these involve taking priority over an existing user and so they grab the k but don't get to use this turn
if(!state->bombers[oboe.k].b_ti) // PFF priority and priority for bigger loads
{
if(state->bombers[k].b_ti||((state->bombers[k].b_hc+state->bombers[k].b_gp)>(state->bombers[oboe.k].b_hc+state->bombers[oboe.k].b_gp)))
oboe.k=k;
}
}
else if(oboe.k==(int)k)
oboe.k=-1;
unsigned int s=max(state->bombers[k].squadron, 0);
unsigned int blon=base_lon(bases[state->squads[s].base]), blat=base_lat(bases[state->squads[s].base]);
int destx=home?blon:state->bombers[k].route[stage][1],
desty=home?blat:state->bombers[k].route[stage][0],
prevx=stage?state->bombers[k].route[stage-1][1]:blon,
prevy=stage?state->bombers[k].route[stage-1][0]:blat;
double thinklon=state->bombers[k].lon+state->bombers[k].navlon,
thinklat=state->bombers[k].lat+state->bombers[k].navlat;
clamp(thinklon, 0, 256);
clamp(thinklat, 0, 256);
double cx=destx-thinklon, cy=desty-thinklat;
int px=destx-prevx, py=desty-prevy;
double d=hypot(cx, cy);
if(home)
{
if(d<0.7)
{
if(state->bombers[k].lon<63)
{
state->bombers[k].landed=true;
inair--;
}
else
{
state->bombers[k].navlon=0;
}
}
else if(t>RRT(9,0))
{
state->bombers[k].navlon=state->bombers[k].navlat=0;
state->bombers[k].lon=min(state->bombers[k].lon, 127);
}
}
else if(stage==4)
{
bool fuel=(t>=state->bombers[k].fuelt);
bool damaged=(state->bombers[k].damage>=8);
bool roeok=state->bombers[k].idtar||(!state->roe.idtar&&brandp(0.2))||brandp(0.005);
bool leaf=state->bombers[k].b_le;
bool pffstop=stream&&(t<state->raids[i].zerohour-(is_pff(state, k)?16:0)); // PFF start bombing at Zero minus 8; Main Force at Zero Hour
double cr=1.2;
if(oboe.k==(int)k) cr=0.3;
unsigned int dm=0; // target crew believes is nearest
double mind=1e6;
for(unsigned int i=0;i<ntargs;i++)
{
double dx=state->bombers[k].lon+state->bombers[k].navlon-targs[i].lon, dy=state->bombers[k].lat+state->bombers[k].navlat-targs[i].lat, dd=dx*dx+dy*dy;
if(dd<mind)
{
mind=dd;
dm=i;
}
}
if(((fabs(cx)<cr)&&(fabs(cy)<cr)&&roeok&&!pffstop&&(dm==state->bombers[k].targ))||((fuel||damaged)&&(roeok||leaf)))
{
double bskill=0;
if(bac)
{
bskill=crewman_skill(bac, state->bombers[k].type);
if(bac->class!=CCLASS_B)
bskill*=0.75;
}
double pre=25.0/(50.0+bskill); // probable radius of error
state->bombers[k].bmblon=state->bombers[k].lon+drandu(pre*2.0)-pre;
state->bombers[k].bmblat=state->bombers[k].lat+drandu(pre*2.0)-pre;
state->bombers[k].navlon=targs[dm].lon-state->bombers[k].lon;
state->bombers[k].navlat=targs[dm].lat-state->bombers[k].lat;
state->bombers[k].bt=t;
state->bombers[k].bombed=true;
if(!leaf)
{
// B practise (if no B, the N doesn't practise even though they're 'bac')
for(unsigned int l=1;l<MAX_CREW;l++)
if(bstats(state->bombers[k]).crew[l]==CCLASS_B)
practise(*get_crew(state, k, l), 1);
for(unsigned int ta=0;ta<ntargs;ta++)