2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * mission.c: Mission subroutines for planes/ships/units
30 * Known contributors to this file:
32 * Steve McClure, 1996-2000
57 #include "prototypes.h"
61 struct emp_qelem queue;
66 s_char *mission_name(short int mission);
67 s_char *nameofitem(s_char *buf, struct genitem *gp, int type);
70 * Interdict commodities & transported planes
73 ground_interdict(coord x, coord y, natid victim, s_char *s)
76 int dam=0, newdam, rel;
77 struct genlist mi[MAXNOC];
80 bzero((s_char *)mi,sizeof(mi));
82 emp_initque((struct emp_qelem *)&mi[z]);
84 build_mission_list(mi,x,y,MI_INTERDICT,victim);
86 for(cn=1;cn<MAXNOC;cn++){
87 rel = getrel(getnatp(cn),victim);
91 if (QEMPTY(&mi[cn].queue))
94 newdam = perform_mission(x,y,victim,&mi[cn].queue,
95 MI_INTERDICT,s,SECT_HARDTARGET);
98 mpr(victim, "%s interdiction mission does %d damage!\n",
102 collateral_damage(x,y,dam, 0);
108 collateral_damage(coord x, coord y, int dam, struct emp_qelem *list)
110 extern double collateral_dam;
119 coll = ldround((double)dam * collateral_dam, 1);
122 mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
123 xyas(x,y,sect.sct_own), coll);
124 sectdamage(§, coll, list);
132 only_subs(struct emp_qelem *list)
134 struct emp_qelem *qp;
139 for(qp = list->q_forw; qp != list; qp = qp->q_forw) {
140 glp = (struct genlist *)qp;
141 gp = (struct genitem *)glp->thing;
143 if (glp->type != EF_SHIP)
145 mcp = (struct mchrstr *)glp->cp;
146 if (!(mcp->m_flags & M_SUB))
150 /* They were all subs! */
156 * Interdict ships & land units
160 unit_interdict(coord x, coord y, natid victim, s_char *s, int hardtarget, int mission)
164 struct genlist mi[MAXNOC];
168 bzero((s_char *)mi,sizeof(mi));
169 for(z=1;z<MAXNOC;z++)
170 emp_initque((struct emp_qelem *)&mi[z]);
172 build_mission_list(mi,x,y,mission,victim);
174 for(cn=1;cn<MAXNOC;cn++){
177 if (mission == MI_SINTERDICT) {
178 if (getrel(getnatp(cn),victim) >= FRIENDLY)
180 } else if (getrel(getnatp(cn),victim) > HOSTILE)
183 if (QEMPTY(&mi[cn].queue))
186 osubs = only_subs(&mi[cn].queue);
187 newdam = perform_mission(x,y,victim,&mi[cn].queue,
188 mission,s,hardtarget);
191 /* If only subs responded, then we don't know who's
195 "Enemy interdiction mission does %d damage!\n",
199 "%s interdiction mission does %d damage!\n",
205 collateral_damage(x,y,dam, 0);
211 * Perform a mission against victim, on behalf of actee
214 off_support(coord x, coord y, natid victim, natid actee)
217 struct genlist mi[MAXNOC];
220 bzero((s_char *)mi,sizeof(mi));
221 for(z=1;z<MAXNOC;z++)
222 emp_initque((struct emp_qelem *)&mi[z]);
224 build_mission_list(mi,x,y,MI_SUPPORT,victim);
225 build_mission_list(mi,x,y,MI_OSUPPORT,victim);
227 dam = dosupport(mi,x,y,victim,actee);
232 * Perform a mission against victim, on behalf of actee
235 def_support(coord x, coord y, natid victim, natid actee)
238 struct genlist mi[MAXNOC];
241 bzero((s_char *)mi,sizeof(mi));
242 for(z=1;z<MAXNOC;z++)
243 emp_initque((struct emp_qelem *)&mi[z]);
245 build_mission_list(mi,x,y,MI_SUPPORT,victim);
246 build_mission_list(mi,x,y,MI_DSUPPORT,victim);
248 dam = dosupport(mi,x,y,victim,actee);
253 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
259 for(cn=1;cn<MAXNOC;cn++){
260 rel = getrel(getnatp(cn),actee);
261 if ((cn != actee) && (rel != ALLIED))
263 rel = getrel(getnatp(cn),victim);
264 if ((cn != actee) && (rel != AT_WAR))
267 if (QEMPTY(&mi[cn].queue))
270 dam += perform_mission(x,y,victim,&mi[cn].queue,MI_SUPPORT,
271 "", SECT_HARDTARGET);
277 build_mission_list(struct genlist *mi, coord x, coord y, int mission, natid victim)
279 build_mission_list_type(mi,x,y,mission,EF_LAND,victim);
280 build_mission_list_type(mi,x,y,mission,EF_SHIP,victim);
281 build_mission_list_type(mi,x,y,mission,EF_PLANE,victim);
285 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission, int type, natid victim)
302 size = max(sizeof(struct shpstr),sizeof(struct lndstr));
303 size = max(size,sizeof(struct plnstr));
304 block = (s_char *)malloc(size);
306 size = sizeof(u_block);
307 block = (s_char *)&u_block;
309 snxtitem_all(&ni,type);
310 while(nxtitem(&ni, block)){
311 gp = (struct genitem *)block;
319 if ((gp->mission != mission) && (mission != MI_SINTERDICT))
322 if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
323 (gp->mission != MI_INTERDICT))
326 relat = getrel(getnatp(gp->own), victim);
327 if (mission == MI_SINTERDICT) {
328 if (relat >= FRIENDLY)
330 else if (type != EF_PLANE && relat > HOSTILE)
332 } else if (relat > HOSTILE)
335 dist = mapdist(x, y, gp->opx, gp->opy);
338 if (mission != MI_RESERVE) /* XXX */
339 oprange(gp,type,&radius);
344 /* Ok, it is within the operations range. */
345 /* Now check from where the object actually is */
346 dist = mapdist(x, y, gp->x, gp->y);
348 oprange(gp, type, &radius);
351 /* Ok, the object can get to where the x,y is */
354 if (mission != MI_AIR_DEFENSE) {
355 getsect(x,y,(s_char *)§);
356 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
359 * If the player->owner of the unit isn't at war
360 * with the victim, and doesn't own the
361 * sect being acted upon, and isn't the
362 * old player->owner of that sect, bounce them.
364 if (sect.sct_type != SCT_WATER &&
365 sect.sct_own != gp->own &&
366 sect.sct_oldown != gp->own)
372 glp = (struct genlist *) malloc(sizeof(struct genlist));
373 bzero((s_char *)glp,sizeof(struct genlist));
378 case EF_LAND: glp->cp = (s_char *)&lchr[(int)gp->type];
380 case EF_SHIP: glp->cp = (s_char *)&mchr[(int)gp->type];
382 case EF_PLANE: glp->cp = (s_char *)&plchr[(int)gp->type];
385 glp->thing = (s_char *)malloc(size);
386 bcopy(block,glp->thing,size);
387 emp_insque(&glp->queue, &mi[gp->own].queue);
392 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
399 snxtitem_all(&ni,EF_PLANE);
400 while(nxtitem(&ni, (s_char *)&plane)){
401 if (plane.pln_own != cn)
404 if (plane.pln_mission != MI_ESCORT)
407 dist = mapdist(x,y,plane.pln_x,plane.pln_y);
409 if (dist > ((int)((float)plane.pln_range/2.0)) )
412 plp = (struct plist *) malloc(sizeof(struct plist));
413 bzero((s_char *)plp,sizeof(struct plist));
414 plp->pcp = &plchr[(int)plane.pln_type];
415 bcopy((s_char *)&plane,(s_char *)&plp->plane,sizeof(struct plnstr));
416 emp_insque(&plp->queue, escorts);
421 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list, int mission, s_char *s, int hardtarget)
423 extern int land_max_interdiction_range;
424 extern int ship_max_interdiction_range;
425 s_char *mission_name(short int mission), *nameofitem(s_char *buf, struct genitem *gp, int type);
426 struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
427 struct emp_qelem *newqp;
436 struct plchrstr *pcp;
437 int dam=0,dam2, mission_flags, tech;
438 natid plane_owner = 0;
439 int gun,shell,md, air_dam=0;
440 double range2, prb, range, mobcost, hitchance;
441 extern int torpedo_damage;
445 emp_initque(&missiles);
446 emp_initque(&bombers);
447 emp_initque(&escorts);
450 for(qp = list->q_forw; qp != list; qp = qp->q_forw){
451 glp = (struct genlist *)qp;
452 gp = (struct genitem *)glp->thing;
454 md = mapdist(x,y,gp->x,gp->y);
456 if (glp->type == EF_LAND){
457 lp = (struct lndstr *)glp->thing;
458 lcp = (struct lchrstr *)glp->cp;
460 if (lp->lnd_effic < LAND_MINFIREEFF)
463 if (mission == MI_SINTERDICT)
466 if ((mission == MI_INTERDICT) &&
467 (md > land_max_interdiction_range))
470 if (md > (lp->lnd_frg / 2))
473 if (lnd_getmil(lp) < 1)
476 range = techfact((int)lp->lnd_tech,
477 (double)lp->lnd_frg / 2.0);
478 range2 = (double)roundrange(range);
483 shell = getvar(V_SHELL, (s_char *)lp, EF_LAND);
484 gun = getvar(V_GUN, (s_char *)lp, EF_LAND);
485 if (shell == 0 || gun == 0)
490 putland(lp->lnd_uid, lp);
491 dam2 = ldround(landunitgun(lp->lnd_effic, lp->lnd_dam, gun,
492 lp->lnd_ammo, shell),1);
493 if (sect.sct_type == SCT_WATER){
494 double dam3 = (double)dam2;
495 if (chance(((double)lp->lnd_acc)/100.0))
496 dam2=ldround((dam3/2.0),1);
499 if (sect.sct_type == SCT_WATER)
500 nreport(lp->lnd_own,N_SHP_SHELL,victim,1);
502 nreport(lp->lnd_own,N_SCT_SHELL,victim,1);
504 "%s fires at %s %s at %s\n",
505 prland(lp), cname(victim),
506 s, xyas(x,y,lp->lnd_own));
508 mpr(victim, "%s %s fires at you at %s\n",
509 cname(lp->lnd_own), prland(lp),
512 }else if (glp->type == EF_SHIP){
513 sp = (struct shpstr *)glp->thing;
514 mcp = (struct mchrstr *)glp->cp;
516 if (sp->shp_effic < 60)
518 if (sp->shp_frnge == 0)
520 if (((mission == MI_INTERDICT) ||
521 (mission == MI_SINTERDICT)) &&
522 (md > ship_max_interdiction_range))
524 if (getvar(V_MILIT, (s_char *)sp, EF_SHIP) < 1)
527 if ((mcp->m_flags & M_SUB) &&
528 (sect.sct_type != SCT_WATER))
531 if (mission == MI_SINTERDICT){
532 if (!(mcp->m_flags & M_SONAR))
534 if (!(mcp->m_flags & M_DCH) &&
535 !(mcp->m_flags & M_SUBT))
537 range2 = techfact(sp->shp_tech,
538 (double)mcp->m_vrnge);
539 range2 *= (double)sp->shp_effic / 200.0;
542 /* can't look all the time */
546 if (mcp->m_flags & M_SUB){
547 /* If we aren't shooting at "subs" or "ships" don't fire at all from
551 if (sp->shp_mobil < (s_char)0)
553 gun = getvar(V_GUN,(s_char *)sp,EF_SHIP);
556 shell = getvar(V_SHELL,(s_char *)sp,EF_SHIP);
558 shell += supply_commod(sp->shp_own,
559 sp->shp_x,sp->shp_y,I_SHELL,
564 range = sp->shp_effic* techfact(sp->shp_tech,
565 ((double)sp->shp_frnge)) / 100.0;
567 range2 = (double)roundrange(range);
571 if (!line_of_sight((s_char **)0, x, y,
574 putvar(V_SHELL,shell-3,(s_char *)sp,EF_SHIP);
575 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
576 mobcost = (480.0/(mobcost +
577 techfact(sp->shp_tech, mobcost)));
578 sp->shp_mobil -= mobcost;
579 putship(sp->shp_uid,sp);
580 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
583 "%s locking on %s %s in %s\n",
584 prship(sp),cname(victim),s,
585 xyas(x,y,sp->shp_own));
587 "\tEffective torpedo range is %.1f\n", range);
589 "\tWhooosh... Hitchance = %d%%\n",
590 (int)(hitchance*100));
592 if (hitchance < 1.0 && !chance(hitchance)) {
595 mpr(victim, "Incoming torpedo sighted @ %s missed (whew)!\n",
601 dam2 = TORP_DAMAGE();
604 nreport(victim,N_TORP_SHIP,sp->shp_own,1);
606 "\tTorpedo hit %s %s for %d damage\n",
607 cname(victim),s,dam2);
610 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
611 xyas(x,y,victim), dam2);
613 range = techfact(sp->shp_tech,
614 (double)mcp->m_frnge/2.0);
615 range2 = (double)roundrange(range);
618 gun = getvar(V_GUN, (s_char *)sp, EF_SHIP);
619 gun = min(gun, sp->shp_glim);
620 shell = getvar(V_SHELL, (s_char *)sp, EF_SHIP);
622 shell += supply_commod(sp->shp_own,
623 sp->shp_x,sp->shp_y,I_SHELL,
625 gun = min(gun, shell);
626 gun = min(gun,(int)((float)getvar(V_MILIT,
627 (s_char *)sp,EF_SHIP)/2.0));
631 dam2 = seagun(sp->shp_effic,gun);
635 prb = ((double)md) / range2;
638 dam2 = (int)((float)dam2/2.0);
640 if (sect.sct_type == SCT_WATER)
641 nreport(sp->shp_own,N_SHP_SHELL,victim,1);
643 nreport(sp->shp_own,N_SCT_SHELL,victim,1);
645 "%s fires at %s %s at %s\n",
648 xyas(x,y,sp->shp_own));
650 mpr(victim, "%s fires at you at %s\n",
651 cname(sp->shp_own), prship(sp),
654 putvar(V_SHELL,shell-gun,(s_char *)sp, EF_SHIP);
655 putship(sp->shp_uid,sp);
657 }else if (glp->type == EF_PLANE) {
658 pcp = (struct plchrstr *)glp->cp;
659 if (pcp->pl_flags & P_M)
660 /* units have their own missile interdiction */
661 if (hardtarget != SECT_HARDTARGET ||
662 pcp->pl_flags & P_MAR)
665 /* save planes for later */
666 plp = (struct plist *)malloc(sizeof(struct plist));
668 bzero((s_char *)plp,sizeof(struct plist));
670 bcopy(glp->thing, (s_char *)&plp->plane,
671 sizeof(struct plnstr));
672 if (plp->pcp->pl_flags & P_M)
673 emp_insque(&plp->queue, &missiles);
675 emp_insque(&plp->queue, &bombers);
676 plane_owner = plp->plane.pln_own;
679 if (!QEMPTY(&missiles)) {
680 /* I arbitrarily chose 100 mindam -KHS */
681 dam += msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100, "sector", victim, mission);
683 while(qp != (&missiles)){
689 if (QEMPTY(&bombers)) {
692 glp = (struct genlist *)qp;
701 * If there are planes performing an
702 * interdict or support mission, find
703 * some escorts for them, if possible.
704 * Up to 2 per bomber, if possible.
706 find_escorts(x,y,plane_owner,&escorts);
708 if (mission == MI_SINTERDICT)
709 mission_pln_sel(&bombers,P_T|P_A,0,hardtarget);
711 mission_pln_sel(&bombers,P_T,P_A,SECT_HARDTARGET);
713 mission_pln_sel(&escorts,P_ESC|P_F,0,SECT_HARDTARGET);
715 for(qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw){
716 plp = (struct plist *)qp;
717 if (!find_airport(&airp,plp->plane.pln_x,plp->plane.pln_y))
718 add_airport(&airp,plp->plane.pln_x,
722 for(qp = airp.q_forw; qp != (&airp); qp = qp->q_forw){
726 air = (struct airport *)qp;
727 md = mapdist(x,y,air->x,air->y);
732 /* Split off the bombers at this base into b */
733 divide(&bombers,&b,air->x,air->y);
735 /* Split off the escorts at this base into e */
736 divide(&escorts,&e,air->x,air->y);
740 mission_flags |= P_X; /* stealth (shhh) */
741 mission_flags |= P_H; /* gets turned off if not all choppers */
743 mission_flags = mission_pln_arm(&b,air->x,air->y,2*md,'p',0,
744 0,mission_flags,&tech);
750 mission_flags = mission_pln_arm(&e,air->x,air->y,2*md,'p',0,
751 P_F|P_ESC,mission_flags,&tech);
753 BestAirPath(pp, air->x, air->y, x, y);
754 wu(0, plane_owner, "Flying %s mission from %s\n",
755 mission_name(mission), xyas(air->x, air->y, plane_owner));
756 if (air->own && (air->own != plane_owner)) {
757 wu(0, air->own, "%s is flying %s mission from %s\n",
758 cname(plane_owner), mission_name(mission),
759 xyas(air->x, air->y, air->own));
762 ac_encounter(&b,&e,air->x,air->y,pp,mission_flags,0, 0, 0);
765 air_dam += air_damage(&b,x,y,mission,victim,s,hardtarget);
773 if (sect.sct_type == SCT_WATER)
774 nreport(plane_owner,N_SHP_BOMB,victim,1);
776 nreport(plane_owner,N_SCT_BOMB,victim,1);
779 /* free up all this memory */
782 glp = (struct genlist *)qp;
790 while(qp != (&escorts)){
798 while(qp != (&bombers)){
809 cando(int mission, int type)
813 if (type == EF_PLANE)
817 if (type == EF_PLANE)
821 if ((type == EF_PLANE) || (type == EF_SHIP))
829 if (type == EF_PLANE)
841 s_char *nameofitem(s_char *buf, struct genitem *gp, int type)
845 return prship((struct shpstr *)gp);
848 return prplane((struct plnstr *)gp);
851 return prland((struct lndstr *)gp);
856 s_char *mission_short_name(int mission)
859 case MI_INTERDICT: return "interdict";
860 case MI_SUPPORT: return "support ";
861 case MI_OSUPPORT: return "offensive";
862 case MI_DSUPPORT: return "defensive";
863 case MI_RESERVE: return "reserve ";
864 case MI_ESCORT: return "escort ";
865 case MI_SINTERDICT: return "interdict";
866 case MI_AIR_DEFENSE: return "air def ";
871 s_char *mission_name(short int mission)
874 case MI_INTERDICT: return "an interdiction";
875 case MI_SUPPORT: return "a support";
876 case MI_OSUPPORT: return "a offensive support";
877 case MI_DSUPPORT: return "a defensive support";
878 case MI_RESERVE: return "a reserve";
879 case MI_ESCORT: return "an escort";
880 case MI_SINTERDICT: return "a sub interdiction";
881 case MI_AIR_DEFENSE: return "an air defense";
883 return "a mysterious";
887 show_mission(int type, struct nstr_item *np)
889 int size, first=1, radius;
894 size = max(sizeof(struct lndstr),sizeof(struct plnstr));
895 size = max(size,sizeof(struct shpstr));
896 block = (s_char *)malloc(size);
898 while (nxtitem(np, block)) {
899 gp = (struct genitem *)block;
900 if (!player->owner || gp->own == 0)
904 pr("Thing x,y op-sect rad mission\n");
907 pr("%-25s", nameofitem(buf, gp,type));
908 pr(" %7s", xyas(gp->x,gp->y,player->cnum));
909 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
910 gp->mission == MI_OSUPPORT ||
911 gp->mission == MI_DSUPPORT ||
912 gp->mission == MI_AIR_DEFENSE){
914 oprange(gp,type,&radius);
915 pr(" %7s", xyas(gp->opx,gp->opy,player->cnum));
916 if (radius < gp->radius)
919 pr(" %4d", gp->radius);
920 }else if (gp->mission == MI_RESERVE){
924 getsect(gp->x,gp->y,§);
925 if ((sect.sct_type == SCT_HEADQ) &&
926 (sect.sct_effic >= 60))
929 if (((struct lndstr *)block)->lnd_rad_max == 0)
932 plus += ((struct lndstr *)block)->lnd_rad_max;
933 pr(" %7s", xyas(gp->x,gp->y,player->cnum));
935 }else if (gp->mission == MI_ESCORT){
938 ((float)((struct plnstr *)block)->pln_range/2.0)
943 pr(" is on %s mission\n",
944 mission_name(gp->mission));
946 pr(" has no mission.\n");
951 oprange(struct genitem *gp, int type, int *radius)
953 double techfact(int, double);
961 getship(gp->uid,&ship);
962 range = ldround(techfact(gp->tech,
963 (double)ship.shp_frnge / 2.0),1);
966 getland(gp->uid,&land);
967 range = ldround(techfact((int)land.lnd_tech,
968 (double)land.lnd_frg / 2.0),1);
971 getplane(gp->uid,&plane);
972 /* missiles go one way, so we can use all the range */
973 if (plchr[(int)plane.pln_type].pl_flags & P_M)
974 range = plane.pln_range;
976 range = ldround((double)plane.pln_range/2.0,1);;
980 if ((*radius) > range)
987 * Remove all planes who cannot go on
988 * the mission from the plane list.
991 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags, int hardtarget)
993 struct emp_qelem *qp, *next;
998 struct plchrstr *pcp;
1000 register int y, bad, bad1;
1003 for (qp = list->q_forw; qp != list; qp = next) {
1005 plp = (struct plist *)qp;
1009 if (pp->pln_effic < 40){
1015 if (pp->pln_mobil < 1) {
1022 if (ontradingblock(EF_PLANE, (int *)pp)) {
1032 for(x=0;x<sizeof(wantflags)*8;x++){
1034 if ((wantflags & y) == y)
1035 if ((pcp->pl_flags & y) != y){
1054 if ((pcp->pl_flags & P_ESC) ||
1055 (pcp->pl_flags & P_F))
1059 if ((pcp->pl_flags & P_E) ||
1060 (pcp->pl_flags & P_K) ||
1061 (pcp->pl_flags & P_L))
1073 for(x=0;x<sizeof(nowantflags)*8;x++){
1075 if ((nowantflags & y) == y)
1076 if ((pcp->pl_flags & y) == y)
1085 if (pp->pln_ship >= 0) {
1086 if (!getship(pp->pln_ship, &ship)){
1089 putplane(pp->pln_uid, pp);
1094 if (!can_be_on_ship(pp->pln_uid,ship.shp_uid)) {
1097 if (ship.shp_effic < SHIP_MINEFF){
1100 /* Can't fly off of ships < 50%, or non-owned or non-allied ships */
1101 if ((ship.shp_effic < 50) ||
1102 ((ship.shp_own != pp->pln_own) &&
1103 (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1109 if (pp->pln_land >= 0) {
1110 if (!getland(pp->pln_land, &land)){
1113 putplane(pp->pln_uid, pp);
1118 if (!(pcp->pl_flags & P_E))
1120 if (land.lnd_effic < LAND_MINEFF)
1123 /* Can't fly off of units < 50%, or non-owned or non-allied units */
1124 if ((land.lnd_effic < 50) ||
1125 ((land.lnd_own != pp->pln_own) &&
1126 (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1132 /* Can't fly off units in ships or other units */
1133 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1139 /* Now, check the sector status if not on a plane or unit */
1140 if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1141 /* If we can't get the sector, we can't check it, and can't fly */
1142 if (!getsect(pp->pln_x, pp->pln_y, §)) {
1147 /* First, check allied status */
1148 /* Can't fly from non-owned sectors or non-allied sectors */
1149 if ((sect.sct_own != pp->pln_own) &&
1150 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1155 /* non-vtol plane */
1156 if ((pcp->pl_flags & P_V) == 0) {
1157 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1164 if (pcp->pl_flags & P_A) {
1165 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1172 putplane(pp->pln_uid, pp);
1177 * Arm only the planes at x,y
1181 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist, int mission, struct ichrstr *ip, int flags, int mission_flags, int *tech)
1183 struct emp_qelem *qp;
1184 struct emp_qelem *next;
1189 for (qp = list->q_forw; qp != list; qp = next) {
1191 plp = (struct plist *) qp;
1193 if (plp->plane.pln_x != x)
1195 if (plp->plane.pln_y != y)
1198 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1203 if (flags & (P_S|P_I)) {
1204 if(plp->pcp->pl_flags & P_S)
1205 mission_flags |= P_S;
1206 if(plp->pcp->pl_flags & P_I)
1207 mission_flags |= P_I;
1209 if (*tech > plp->plane.pln_tech)
1210 *tech = plp->plane.pln_tech;
1211 if (!(plp->pcp->pl_flags & P_H))
1212 /* no stealth on this mission */
1213 mission_flags &= ~P_H;
1214 if (!(plp->pcp->pl_flags & P_X))
1215 /* no stealth on this mission */
1216 mission_flags &= ~P_X;
1217 if (!(plp->pcp->pl_flags & P_A)) {
1218 /* no asw on this mission */
1219 mission_flags &= ~P_A;
1221 if (!(plp->pcp->pl_flags & P_MINE)) {
1222 /* no asw on this mission */
1223 mission_flags &= ~P_MINE;
1227 * Mob costs for missions are 1/2 normal
1230 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1231 plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags);
1234 return mission_flags;
1238 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags, s_char mission)
1240 register struct plchrstr *pcp;
1254 if (pp->pln_ship >= 0) {
1255 getship(pp->pln_ship, &ship);
1257 ptr = (s_char *) &ship;
1259 if (pp->pln_land >= 0) {
1260 getland(pp->pln_land, &land);
1262 ptr = (s_char *) &land;
1264 getsect(pp->pln_x, pp->pln_y, §);
1266 ptr = (s_char *) §
1268 getvec(VT_ITEM, vec, ptr, type);
1269 if (pcp->pl_fuel > vec[I_PETROL]) {
1272 vec[I_PETROL] -= pcp->pl_fuel;
1274 if (!(flags & P_F)) {
1280 if (pp->pln_nuketype == -1) {
1282 needed = pp->pln_load;
1286 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1289 needed = (pp->pln_load * 2) / ip->i_lbs;
1292 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1295 needed = (pp->pln_load * 2) / ip->i_lbs;
1298 if ((pcp->pl_flags & (P_V|P_C)) == 0)
1301 needed = pp->pln_load / ip->i_lbs;
1304 if (pp->pln_nuketype == -1)
1307 case 'i': /* missile interception */
1310 needed = pp->pln_load;
1316 if (rval < 0 || (item && needed <= 0)) {
1319 if ((vec[item] < needed) && (item == I_SHELL))
1320 vec[item] += supply_commod(plp->plane.pln_own,
1321 plp->plane.pln_x,plp->plane.pln_y,
1323 if (vec[item] < needed) {
1326 vec[item] -= needed;
1328 if (item == I_SHELL && (mission == 's' || mission == 'p'))
1329 plp->bombs = needed;
1333 putvec(VT_ITEM, vec, ptr, type);
1334 if (type == EF_SHIP)
1335 putship(ship.shp_uid,&ship);
1336 else if (type == EF_LAND)
1337 putland(land.lnd_uid,&land);
1344 * Return 1 if this x,y pair is in the list
1347 find_airport(struct emp_qelem *airp, coord x, coord y)
1349 struct emp_qelem *qp;
1352 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw){
1353 a = (struct airport *)qp;
1354 if ((a->x == x) && (a->y == y))
1361 /* #*# This needs to be changed to include acc's -KHS */
1363 add_airport(struct emp_qelem *airp, coord x, coord y)
1368 a = (struct airport *)malloc(sizeof(struct airport));
1373 a->own = sect.sct_own;
1375 emp_insque((struct emp_qelem *)a,airp);
1379 * Take all the planes in list 1 that
1380 * are at x,y, and put them into list 2.
1383 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1385 struct emp_qelem *qp, *next;
1388 for (qp = l1->q_forw; qp != l1; qp = next){
1390 plp = (struct plist *)qp;
1392 if (plp->plane.pln_x != x)
1394 if (plp->plane.pln_y != y)
1403 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission, natid victim, s_char *s, int hardtarget)
1405 struct emp_qelem *qp;
1412 for(qp = bombers->q_forw; qp != bombers; qp = qp->q_forw){
1413 plp = (struct plist *)qp;
1416 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1423 if (plp->pcp->pl_flags & P_A) {
1424 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1426 "\t%s detects sub movement in %s\n",
1427 prplane(pp), xyas(x,y,pp->pln_own));
1430 if (getrel(getnatp(pp->pln_own),victim) > HOSTILE) {
1432 "\t%s tracks %s %s at %s\n",
1434 cname(victim), s, xyas(x,y,pp->pln_own));
1438 "\t%s depth-charging %s %s in %s\n",
1442 xyas(x,y,pp->pln_own));
1445 "\t%s pinbombing %s %s in %s\n",
1449 xyas(x,y,pp->pln_own));
1451 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1452 if (plp->plane.pln_nuketype != -1)
1454 else if (hardtarget != SECT_HARDTARGET)
1455 wu(0,pp->pln_own,"\t\t%d%% hitchance...", hitchance);
1456 /* Always calculate damage */
1457 if (roll(100) <= hitchance) {
1458 newdam = pln_damage(&plp->plane,x,y,'p',&nukedam, 1);
1460 if (mission == MI_INTERDICT) {
1462 "\t\tnuclear warhead does %d damage to %s %s\n",
1464 nukedam, cname(victim), s);
1469 "\t\thit %s %s for %d damage\n",
1470 cname(victim), s, newdam);
1474 newdam = pln_damage(&plp->plane,x,y,'p',&nukedam, 0);
1475 wu(0,pp->pln_own,"missed\n");
1476 if (mission == MI_SINTERDICT) {
1478 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1480 } else if (*s == 's') {
1482 "SPLASH! Bombs miss your %s in %s\n",
1483 s, xyas(x,y,victim));
1485 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1486 s, xyas(x, y, victim));
1488 /* Now, even though we missed, the bombs
1490 collateral_damage(x, y, newdam, bombers);
1493 /* use up missiles */
1494 if (plp->pcp->pl_flags & P_M) {
1495 makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
1504 * Check to see if anyone hostile to the victim
1505 * is running an air defense mission on this
1506 * sector. If so, do air combat
1509 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
1512 int mission_flags, tech, combat=0, rel, dist, z;
1513 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1515 struct genlist *glp;
1517 struct genlist mi[MAXNOC];
1523 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1525 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1528 bzero((s_char *)mi,sizeof(mi));
1529 for (z = 1; z < MAXNOC; z++)
1530 emp_initque((struct emp_qelem *)&mi[z]);
1532 build_mission_list_type(mi,x,y,MI_AIR_DEFENSE,EF_PLANE,victim);
1534 for (cn = 1; cn < MAXNOC; cn++) {
1535 /* Check our relations */
1536 rel = getrel(getnatp(cn),victim);
1541 if (QEMPTY(&mi[cn].queue))
1544 /* Ok, make a list of all the interceptors. Note that this *copies* the
1545 * list from the mission creation. This list must be deleted later. */
1546 emp_initque(&interceptors);
1547 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1550 glp = (struct genlist *)qp;
1551 gp = (struct genitem *)glp->thing;
1552 plp = (struct plist *)qp;
1554 dist = mapdist(x,y,gp->x,gp->y);
1556 plp = (struct plist *)malloc(sizeof(struct plist));
1557 bzero((s_char *)plp,sizeof(struct plist));
1558 plp->pcp = (struct plchrstr *)glp->cp;
1559 bcopy(glp->thing, (s_char *)&plp->plane,
1560 sizeof(struct plnstr));
1562 /* missiles go one way, so we can use all the range */
1563 if (!(plp->pcp->pl_flags & P_M))
1565 /* If it's out of range, free it and continue on */
1566 if (dist > plp->plane.pln_range) {
1570 emp_insque(&plp->queue, &interceptors);
1573 /* Remove those who cannot go */
1574 mission_pln_sel(&interceptors,P_F,0,SECT_HARDTARGET);
1576 if (QEMPTY(&interceptors))
1579 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1580 * that the higher numbered planes go into battle (they should be the better ones
1581 * at fighting, if all went well.) */
1583 for (qp = interceptors.q_forw; qp != (&interceptors); qp = qp->q_forw)
1585 tcount -= (count * 2);
1586 /* Just in case there are more incoming than we have */
1589 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1593 /* Free it up and continue */
1595 glp = (struct genlist *)qp;
1596 free((s_char *)glp);
1600 /* Now, make a list of all the airports these planes are coming from */
1602 for (qp = interceptors.q_forw ; qp != (&interceptors); qp = qp->q_forw) {
1603 plp = (struct plist *)qp;
1604 if (!find_airport(&airp,plp->plane.pln_x, plp->plane.pln_y))
1605 add_airport(&airp,plp->plane.pln_x, plp->plane.pln_y);
1608 /* Now, fly them out one airport at a time */
1609 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1610 struct airport *air;
1612 air = (struct airport *)qp;
1613 dist = mapdist(x,y,air->x,air->y);
1617 /* Split off the interceptors at this base into i */
1618 divide(&interceptors, &i, air->x, air->y);
1622 mission_flags |= P_X; /* stealth (shhh) */
1623 /* gets turned off if not all choppers */
1624 mission_flags |= P_H;
1625 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1626 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1628 /* Did we run out of interceptors? */
1631 /* Did we run out of bombers? */
1632 if (QEMPTY(bomb_list)) {
1633 /* Yes, so we have to put the rest of the interceptors back, and
1634 then continue, or we leak memory */
1638 mission_flags = mission_pln_arm(&i,air->x,air->y,2*dist,'r',0,
1639 P_F,mission_flags,&tech);
1641 /* Did we run out of interceptors? */
1644 /* Did we run out of bombers? */
1645 if (QEMPTY(bomb_list)) {
1646 /* Yes, so we have to put the rest of the interceptors back, and
1647 then continue, or we leak memory */
1652 BestAirPath(path, air->x, air->y, x, y);
1653 wu(0, cn, "Flying %s mission from %s\n",
1654 mission_name(MI_AIR_DEFENSE),
1655 xyas(air->x, air->y, cn));
1656 if (air->own && (air->own != cn)) {
1657 wu(0, air->own, "%s is flying %s mission from %s\n",
1658 cname(cn), mission_name(MI_AIR_DEFENSE),
1659 xyas(air->x, air->y, air->own));
1662 /* Now, fly the planes to the sector */
1663 emp_initque(&empty);
1664 ac_encounter(&i, &empty, air->x, air->y,
1665 path, mission_flags,1, bomb_list, esc_list);
1667 /* If none made it, continue */
1671 /* Some made it, so now they get to try to fight. */
1672 /* Intercept the escorts first */
1674 if (!QEMPTY(esc_list)){
1675 mpr(victim, "%s air defense planes intercept!\n",
1677 ac_combat_headers(victim, cn);
1678 ac_airtoair(esc_list,&i,air->own);
1681 /* Now intercept the bombers */
1682 if (!QEMPTY(bomb_list)) {
1684 mpr(victim, "%s air defense planes intercept!\n", cname(cn));
1685 ac_combat_headers(victim, cn);
1687 ac_airtoair(bomb_list, &i, air->own);
1696 /* We have to free all of these, if they are still there, otherwise they get
1697 lost and we leak memory all over the place. */
1698 for (cn = 1; cn < MAXNOC; cn++) {
1699 /* free up all this memory if it's still there */
1700 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1702 glp = (struct genlist *)qp;
1704 free((s_char *)glp);