]> git.pond.sub.org Git - empserver/blob - src/lib/subs/lndsub.c
Use relations_with() for US==THEM || getrel(getnatp(US), THEM)
[empserver] / src / lib / subs / lndsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  lndsub.c: Land unit subroutines
29  *
30  *  Known contributors to this file:
31  *     Ken Stevens, 1995
32  *     Steve McClure, 1998-2000
33  *     Markus Armbruster, 2004-2009
34  */
35
36 #include <config.h>
37
38 #include <math.h>
39 #include <stdlib.h>
40 #include "combat.h"
41 #include "damage.h"
42 #include "file.h"
43 #include "misc.h"
44 #include "mission.h"
45 #include "nsc.h"
46 #include "optlist.h"
47 #include "path.h"
48 #include "player.h"
49 #include "prototypes.h"
50 #include "xy.h"
51 #include "empobj.h"
52 #include "unit.h"
53
54 static void lnd_stays(natid, char *, struct ulist *);
55 static int lnd_hit_mine(struct lndstr *);
56 static int has_helpful_engineer(coord, coord, natid);
57
58 double
59 attack_val(int combat_mode, struct lndstr *lp)
60 {
61     int men;
62     double value;
63     struct lchrstr *lcp;
64
65     if (lp->lnd_effic < LAND_MINEFF) {
66         putland(lp->lnd_uid, lp);
67         return 0;
68     }
69
70     lcp = &lchr[(int)lp->lnd_type];
71
72 /* Spies always count as 1 during assaults.  If they are the only ones
73    in the assault, they get to sneak on anyway. */
74
75     if (lcp->l_flags & L_SPY && combat_mode == A_ASSAULT)
76         return 1;
77
78     men = lp->lnd_item[I_MILIT];
79     value = men * lnd_att(lp) * lp->lnd_effic / 100.0;
80
81     switch (combat_mode) {
82     case A_ATTACK:
83         return value;
84     case A_ASSAULT:
85         if (!(lcp->l_flags & L_MARINE))
86             return assault_penalty * value;
87         break;
88     case A_BOARD:
89         if (!(lcp->l_flags & L_MARINE))
90             return assault_penalty * men;
91     }
92
93     return value;
94 }
95
96 double
97 defense_val(struct lndstr *lp)
98 {
99     int men;
100     double value;
101     struct lchrstr *lcp;
102
103     if (lp->lnd_effic < LAND_MINEFF) {
104         putland(lp->lnd_uid, lp);
105         return 0;
106     }
107
108     lcp = &lchr[(int)lp->lnd_type];
109
110     men = lp->lnd_item[I_MILIT];
111
112     if ((lp->lnd_ship >= 0 || lp->lnd_land >= 0) &&
113         !(lcp->l_flags & L_MARINE))
114         return men;
115
116     value = men * lnd_def(lp) * lp->lnd_effic / 100.0;
117     value *= ((double)land_mob_max + lp->lnd_harden) / land_mob_max;
118
119     /* If there are military on the unit, you get at least a 1
120        man defensive unit, except for spies */
121     if (value < 1.0 && men > 0 && !(lcp->l_flags & L_SPY))
122         return 1;
123
124     return value;
125 }
126
127 int
128 lnd_reaction_range(struct lndstr *lp)
129 {
130     struct sctstr sect;
131
132     getsect(lp->lnd_x, lp->lnd_y, &sect);
133     if (sect.sct_type == SCT_HEADQ && sect.sct_effic >= 60)
134         return lchr[lp->lnd_type].l_rad + 1;
135     return lchr[lp->lnd_type].l_rad;
136 }
137
138 void
139 lnd_print(natid actor, struct ulist *llp, char *s)
140 {
141     if (actor == player->cnum)
142         pr("%s %s\n", prland(&llp->unit.land), s);
143     else
144         wu(0, actor, "%s %s\n", prland(&llp->unit.land), s);
145 }
146
147 void
148 lnd_delete(struct ulist *llp)
149 {
150     putland(llp->unit.land.lnd_uid, &llp->unit.land);
151     emp_remque((struct emp_qelem *)llp);
152     free(llp);
153 }
154
155 int
156 lnd_take_casualty(int combat_mode, struct ulist *llp, int cas)
157                         /* attacking or assaulting or paratrooping? */
158                         /* number of casualties to take */
159 {
160     int eff_eq;
161     int n;
162     int biggest;
163     int civs;
164     int nowned;
165     coord ret_x, ret_y;
166     coord bx, by;
167     struct sctstr sect;
168     int ret_chance;
169     char buf[1024];
170     int taken;
171     int nowhere_to_go = 0;
172     struct sctstr rsect;
173     double mobcost, bmcost;
174     signed char orig;
175     int mob;
176
177     taken = llp->unit.land.lnd_item[I_MILIT];
178     /* Spies always die */
179     if (((struct lchrstr *)llp->chrp)->l_flags & L_SPY)
180         llp->unit.land.lnd_effic = 0;
181     else {
182         eff_eq = ldround(cas * 100.0 /
183             ((struct lchrstr *)llp->chrp)->l_item[I_MILIT], 1);
184         llp->unit.land.lnd_effic -= eff_eq;
185         lnd_submil(&llp->unit.land, cas);
186     }
187
188     if (llp->unit.land.lnd_effic < LAND_MINEFF) {
189         sprintf(buf, "dies %s %s!",
190                 combat_mode ? att_mode[combat_mode] : "defending",
191                 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
192                      llp->unit.land.lnd_own));
193         lnd_print(llp->unit.land.lnd_own, llp, buf);
194         lnd_delete(llp);
195         /* Since we killed the unit, we killed all the mil on it */
196         return taken;
197     } else {
198         /* Ok, now, how many did we take off? (sould be the diff) */
199         taken = taken - llp->unit.land.lnd_item[I_MILIT];
200     }
201
202     if (llp->unit.land.lnd_effic >= llp->unit.land.lnd_retreat)
203         return taken;
204
205     /* we're being boarded */
206     if (llp->unit.land.lnd_ship >= 0 && combat_mode == A_DEFEND)
207         return taken;
208
209     /* we're being boarded */
210     if (llp->unit.land.lnd_land >= 0 && combat_mode == A_DEFEND)
211         return taken;
212
213     /* Have to make a retreat check */
214
215     ret_chance = llp->unit.land.lnd_retreat - llp->unit.land.lnd_effic;
216     if (roll(100) < ret_chance) {
217         pr("\n");
218         lnd_print(llp->unit.land.lnd_own, llp, "fails morale check!");
219         llp->unit.land.lnd_mission = 0;
220         llp->unit.land.lnd_harden = 0;
221         if (llp->unit.land.lnd_ship >= 0 || llp->unit.land.lnd_land >= 0)
222             nowhere_to_go = 1;
223         else if (combat_mode == A_DEFEND) {
224             /*
225              * defending unit.. find a place to send it
226              * strategy: look for the most-populated
227              * adjacent sector that is owned by the unit
228              * owner. Charge mob..
229              */
230             biggest = -1;
231             nowned = 0;
232             for (n = 1; n <= 6; ++n) {
233                 ret_x = llp->unit.land.lnd_x + diroff[n][0];
234                 ret_y = llp->unit.land.lnd_y + diroff[n][1];
235                 getsect(ret_x, ret_y, &sect);
236                 if (sect.sct_own != llp->unit.land.lnd_own)
237                     continue;
238                 if (sect.sct_type == SCT_MOUNT)
239                     continue;
240                 mobcost = lnd_mobcost(&llp->unit.land, &rsect);
241                 if (mobcost < 0)
242                     continue;
243                 ++nowned;
244                 civs = sect.sct_item[I_CIVIL];
245                 if (civs > biggest) {
246                     biggest = civs;
247                     bx = sect.sct_x;
248                     by = sect.sct_y;
249                     bmcost = mobcost;
250                 }
251             }
252             if (!nowned)
253                 nowhere_to_go = 1;
254             else {
255                 /* retreat to bx,by */
256                 llp->unit.land.lnd_x = bx;
257                 llp->unit.land.lnd_y = by;
258                 /* FIXME landmines */
259                 getsect(bx, by, &rsect);
260                 mob = llp->unit.land.lnd_mobil - (int)bmcost;
261                 if (mob < -127)
262                     mob = -127;
263                 orig = llp->unit.land.lnd_mobil;
264                 llp->unit.land.lnd_mobil = (signed char)mob;
265                 if (llp->unit.land.lnd_mobil > orig)
266                     llp->unit.land.lnd_mobil = -127;
267                 sprintf(buf, "retreats at %d%% efficiency to %s!",
268                         llp->unit.land.lnd_effic,
269                         xyas(bx, by, llp->unit.land.lnd_own));
270                 lnd_print(llp->unit.land.lnd_own, llp, buf);
271                 lnd_delete(llp);
272             }
273         } else {                /* attacking from a sector */
274             sprintf(buf, "leaves the battlefield at %d%% efficiency",
275                     llp->unit.land.lnd_effic);
276             if ((llp->unit.land.lnd_mobil - (int)llp->mobil) < -127)
277                 llp->unit.land.lnd_mobil = -127;
278             else
279                 llp->unit.land.lnd_mobil -= (int)llp->mobil;
280             llp->mobil = 0.0;
281             lnd_print(llp->unit.land.lnd_own, llp, buf);
282             lnd_delete(llp);
283         }
284     }
285     if (nowhere_to_go) {
286         /* nowhere to go.. take more casualties */
287         llp->unit.land.lnd_effic -= 10;
288         lnd_submil(&llp->unit.land,
289                    ((struct lchrstr *)llp->chrp)->l_item[I_MILIT] / 10);
290         if (llp->unit.land.lnd_effic < LAND_MINEFF) {
291             lnd_print(llp->unit.land.lnd_own, llp,
292                       "has nowhere to retreat, and dies!");
293             lnd_delete(llp);
294         } else
295             lnd_print(llp->unit.land.lnd_own, llp,
296                       "has nowhere to retreat and takes extra losses!");
297     }
298
299     return taken;
300 }
301
302 void
303 lnd_takemob(struct emp_qelem *list, double loss)
304 {
305     struct emp_qelem *qp, *next;
306     struct ulist *llp;
307     int new;
308     int mcost = ldround(combat_mob * loss, 1);
309
310     for (qp = list->q_forw; qp != list; qp = next) {
311         next = qp->q_forw;
312         llp = (struct ulist *)qp;
313 #if 0
314         if (chance(loss))
315             use_supply(&llp->unit.land);
316 #endif
317         new = llp->unit.land.lnd_mobil - mcost;
318         if (new < -127)
319             new = -127;
320         llp->unit.land.lnd_mobil = (signed char)new;
321     }
322 }
323
324 void
325 lnd_submil(struct lndstr *lp, int num)
326 {
327     int new = lp->lnd_item[I_MILIT] - num;
328     lp->lnd_item[I_MILIT] = new < 0 ? 0 : new;
329 }
330
331 int
332 lnd_spyval(struct lndstr *lp)
333 {
334     if (lchr[(int)lp->lnd_type].l_flags & L_RECON)
335         return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0) + 2;
336     else
337         return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0);
338 }
339
340 void
341 intelligence_report(int destination, struct lndstr *lp, int spy,
342                     char *mess)
343 {
344     int vis = lnd_vis(lp);
345     char buf1[80], buf2[80], buf3[80];
346
347     if (destination == 0)
348         return;
349
350     if (lp->lnd_own == 0)
351         return;
352
353     memset(buf1, 0, sizeof(buf1));
354     memset(buf2, 0, sizeof(buf2));
355     memset(buf3, 0, sizeof(buf3));
356     if (chance((spy + vis) / 10.0)) {
357         if (destination == player->cnum)
358             pr("%s %s", mess, prland(lp));
359         else
360             sprintf(buf1, "%s %s", mess, prland(lp));
361
362         if (chance((spy + vis) / 20.0)) {
363             if (destination == player->cnum)
364                 pr(" (eff %d, mil %d",
365                    roundintby(lp->lnd_effic, 5),
366                    roundintby(lp->lnd_item[I_MILIT], 10));
367             else
368                 sprintf(buf2, " (eff %d, mil %d",
369                         roundintby(lp->lnd_effic, 5),
370                         roundintby(lp->lnd_item[I_MILIT], 10));
371
372             if (chance((spy + vis) / 20.0)) {
373                 int t;
374                 t = lp->lnd_tech - 20 + roll(40);
375                 t = MAX(t, 0);
376                 if (destination == player->cnum)
377                     pr(", tech %d)\n", t);
378                 else
379                     sprintf(buf3, ", tech %d)\n", t);
380             } else {
381                 if (destination == player->cnum)
382                     pr(")\n");
383                 else
384                     sprintf(buf3, ")\n");
385             }
386         } else {
387             if (destination == player->cnum)
388                 pr("\n");
389             else
390                 sprintf(buf2, "\n");
391         }
392     }
393
394     if (destination != player->cnum) {
395         wu(0, destination, "%s%s%s", buf1, buf2, buf3);
396     }
397 }
398
399 void
400 lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
401 {
402     struct lndstr land;
403     struct lchrstr *lcp;
404     struct ulist *llp;
405     int this_mot;
406     int mobtype = MOB_MOVE;     /* indeterminate */
407
408     emp_initque(list);
409     while (nxtitem(ni, &land)) {
410         /*
411          * It would be nice to let deities march foreign land units,
412          * but much of the code assumes that only the land unit's
413          * owner can march it.
414          */
415         if (land.lnd_own != player->cnum)
416             continue;
417         if (opt_MARKET) {
418             if (ontradingblock(EF_LAND, &land)) {
419                 pr("unit #%d inelligible - it's for sale.\n",
420                    land.lnd_uid);
421                 continue;
422             }
423         }
424         /*
425          * The marching code gets confused when trains and non-trains
426          * march together.  Disallow for now.
427          */
428         this_mot = lnd_mobtype(&land);
429         if (this_mot != mobtype) {
430             if (mobtype == MOB_MOVE)
431                 mobtype = this_mot;
432             else if (mobtype == MOB_MARCH) {
433                 pr("%s is a train and can't march with the leader.\n",
434                    prland(&land));
435                 continue;
436             } else {
437                 pr("%s can't rail-march with the leading train.\n",
438                    prland(&land));
439                 continue;
440             }
441         }
442
443         lcp = &lchr[(int)land.lnd_type];
444         land.lnd_mission = 0;
445         land.lnd_rflags = 0;
446         land.lnd_harden = 0;
447         memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
448         putland(land.lnd_uid, &land);
449         llp = malloc(sizeof(struct ulist));
450         llp->chrp = (struct empobj_chr *)lcp;
451         llp->unit.land = land;
452         llp->mobil = land.lnd_mobil;
453         emp_insque(&llp->queue, list);
454     }
455 }
456
457 /* This function assumes that the list was created by lnd_sel */
458 void
459 lnd_mar(struct emp_qelem *list, double *minmobp, double *maxmobp,
460         int *togetherp, natid actor)
461 {
462     struct emp_qelem *qp;
463     struct emp_qelem *next;
464     struct ulist *llp;
465     struct sctstr sect;
466     struct lndstr land;
467     coord allx;
468     coord ally;
469     int first = 1;
470     char mess[128];
471
472     *minmobp = 9876.0;
473     *maxmobp = -9876.0;
474     *togetherp = 1;
475     for (qp = list->q_back; qp != list; qp = next) {
476         next = qp->q_back;
477         llp = (struct ulist *)qp;
478         getland(llp->unit.land.lnd_uid, &land);
479         if (land.lnd_own != actor) {
480             mpr(actor, "%s was disbanded at %s\n",
481                 prland(&land), xyas(land.lnd_x, land.lnd_y, actor));
482             emp_remque((struct emp_qelem *)llp);
483             free(llp);
484             continue;
485         }
486         if (land.lnd_ship >= 0) {
487             lnd_stays(actor, "is on a ship", llp);
488             continue;
489         }
490         if (land.lnd_land >= 0) {
491             lnd_stays(actor, "is on a unit", llp);
492             continue;
493         }
494         if (!getsect(land.lnd_x, land.lnd_y, &sect)) {
495             lnd_stays(actor, "was sucked into the sky by a strange looking spaceland", llp);    /* heh -KHS */
496             continue;
497         }
498         if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY) &&
499             !(lchr[(int)llp->unit.land.lnd_type].l_flags & L_TRAIN) &&
500             llp->unit.land.lnd_item[I_MILIT] == 0) {
501             lnd_stays(actor, "has no mil on it to guide it", llp);
502             continue;
503         }
504         if (relations_with(sect.sct_own, actor) != ALLIED &&
505             !(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY) &&
506             sect.sct_own) {
507             sprintf(mess, "has been kidnapped by %s", cname(sect.sct_own));
508             lnd_stays(actor, mess, llp);
509             continue;
510         }
511         if (first) {
512             allx = land.lnd_x;
513             ally = land.lnd_y;
514             first = 0;
515         }
516         if (land.lnd_x != allx || land.lnd_y != ally)
517             *togetherp = 0;
518         if (land.lnd_mobil + 1 < (int)llp->mobil) {
519             llp->mobil = land.lnd_mobil;
520         }
521         if (llp->mobil < *minmobp)
522             *minmobp = llp->mobil;
523         if (llp->mobil > *maxmobp)
524             *maxmobp = llp->mobil;
525         llp->unit.land = land;
526     }
527 }
528
529 void
530 lnd_sweep(struct emp_qelem *land_list, int verbose, int takemob,
531           natid actor)
532 {
533     struct emp_qelem *qp;
534     struct emp_qelem *next;
535     struct ulist *llp;
536     struct sctstr sect;
537     int mines, m, max, sshells, lshells;
538
539     for (qp = land_list->q_back; qp != land_list; qp = next) {
540         next = qp->q_back;
541         llp = (struct ulist *)qp;
542         if (!(((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)) {
543             if (verbose)
544                 mpr(actor, "%s is not an engineer!\n",
545                     prland(&llp->unit.land));
546             continue;
547         }
548         if (takemob && llp->mobil < 0.0) {
549             if (verbose)
550                 lnd_stays(actor, "is out of mobility", llp);
551             continue;
552         }
553         getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &sect);
554         if (sect.sct_oldown == llp->unit.land.lnd_own) {
555             if (verbose)
556                 mpr(actor,
557                     "%s is in a sector completely owned by you.  Don't bother digging up mines there!\n",
558                     prland(&llp->unit.land));
559             continue;
560         }
561         if (SCT_MINES_ARE_SEAMINES(&sect)) {
562             if (verbose)
563                 mpr(actor, "%s is in a %s sector.  No landmines there!\n",
564                     prland(&llp->unit.land), dchr[sect.sct_type].d_name);
565             continue;
566         }
567         if (takemob) {
568             llp->mobil -= lnd_pathcost(&llp->unit.land, 0.2);
569             llp->unit.land.lnd_mobil = (int)llp->mobil;
570             llp->unit.land.lnd_harden = 0;
571         }
572         putland(llp->unit.land.lnd_uid, &llp->unit.land);
573         if (!(mines = sect.sct_mines))
574             continue;
575         max = ((struct lchrstr *)llp->chrp)->l_item[I_SHELL];
576         lshells = llp->unit.land.lnd_item[I_SHELL];
577         sshells = sect.sct_item[I_SHELL];
578         for (m = 0; mines > 0 && m < max * 2; m++) {
579             if (chance(0.5 * ((struct lchrstr *)llp->chrp)->l_att)) {
580                 mpr(actor, "Sweep...\n");
581                 mines--;
582                 if (lshells < max)
583                     ++lshells;
584                 else if (sshells < ITEM_MAX)
585                     ++sshells;
586             }
587         }
588         sect.sct_mines = mines;
589         llp->unit.land.lnd_item[I_SHELL] = lshells;
590         sect.sct_item[I_SHELL] = sshells;
591         putland(llp->unit.land.lnd_uid, &llp->unit.land);
592         putsect(&sect);
593     }
594 }
595
596 static int
597 contains_engineer(struct emp_qelem *list)
598 {
599     struct emp_qelem *qp;
600     struct emp_qelem *next;
601     struct ulist *llp;
602
603     for (qp = list->q_back; qp != list; qp = next) {
604         next = qp->q_back;
605         llp = (struct ulist *)qp;
606         if (((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)
607             return 1;
608     }
609     return 0;
610 }
611
612 int
613 lnd_check_mines(struct emp_qelem *land_list)
614 {
615     struct emp_qelem *qp;
616     struct emp_qelem *next;
617     struct ulist *llp;
618     struct sctstr sect;
619     int stopping = 0;
620     int with_eng = contains_engineer(land_list);
621
622     for (qp = land_list->q_back; qp != land_list; qp = next) {
623         next = qp->q_back;
624         llp = (struct ulist *)qp;
625         getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &sect);
626         if (sect.sct_oldown == llp->unit.land.lnd_own)
627             continue;
628         if (SCT_LANDMINES(&sect) == 0)
629             continue;
630         if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
631             lnd_hit_mine(&llp->unit.land);
632             sect.sct_mines--;
633             putsect(&sect);
634             putland(llp->unit.land.lnd_uid, &llp->unit.land);
635             if (!llp->unit.land.lnd_own) {
636                 stopping = 1;
637                 emp_remque(qp);
638                 free(qp);
639             }
640         }
641     }
642     return stopping;
643 }
644
645 static void
646 lnd_stays(natid actor, char *str, struct ulist *llp)
647 {
648     mpr(actor, "%s %s & stays in %s\n",
649         prland(&llp->unit.land), str,
650         xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y, actor));
651     if (llp->mobil < -127)
652         llp->mobil = -127;
653     llp->unit.land.lnd_mobil = llp->mobil;
654     lnd_delete(llp);
655 }
656
657 static int
658 lnd_count(struct emp_qelem *list)
659 {
660     struct emp_qelem *qp;
661     struct emp_qelem *next;
662     int count = 0;
663
664     for (qp = list->q_back; qp != list; qp = next) {
665         next = qp->q_back;
666         ++count;
667     }
668     return count;
669 }
670
671 static int
672 lnd_damage(struct emp_qelem *list, int totdam)
673 {
674     struct emp_qelem *qp;
675     struct emp_qelem *next;
676     struct ulist *llp;
677     int dam;
678     int count;
679
680     if (!totdam || !(count = lnd_count(list)))
681         return 0;
682     dam = ldround((double)totdam / count, 1);
683     for (qp = list->q_back; qp != list; qp = next) {
684         next = qp->q_back;
685         llp = (struct ulist *)qp;
686         /* land unit might have changed (launched SAMs, collateral dmg) */
687         getland(llp->unit.land.lnd_uid, &llp->unit.land);
688         landdamage(&llp->unit.land, dam);
689         putland(llp->unit.land.lnd_uid, &llp->unit.land);
690         if (!llp->unit.land.lnd_own) {
691             emp_remque(qp);
692             free(qp);
693         }
694     }
695     return dam;
696 }
697
698 static int
699 lnd_easiest_target(struct emp_qelem *list)
700 {
701     struct emp_qelem *qp;
702     struct emp_qelem *next;
703     struct ulist *llp;
704     int hard;
705     int easiest = 9876;         /* things start great for victim */
706     int count = 0;
707
708     for (qp = list->q_back; qp != list; qp = next) {
709         next = qp->q_back;
710         llp = (struct ulist *)qp;
711         hard = lnd_hardtarget(&llp->unit.land);
712         if (hard < easiest)
713             easiest = hard;     /* things get worse for victim */
714         ++count;
715     }
716     return easiest - count;
717 }
718
719 static int
720 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
721                          natid victim)
722 {
723     int mindam = lnd_count(list) * 20;
724     int hardtarget = lnd_easiest_target(list);
725     int dam, newdam, sublaunch;
726     int stopping = 0;
727     struct plist *plp;
728     struct emp_qelem msl_list, *qp, *newqp;
729
730     msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
731
732     dam = 0;
733     for (qp = msl_list.q_back; qp != &msl_list; qp = newqp) {
734         newqp = qp->q_back;
735         plp = (struct plist *)qp;
736
737         if (dam < mindam && mission_pln_equip(plp, NULL, 'p') >= 0) {
738             if (msl_launch(&plp->plane, EF_LAND, "troops",
739                            newx, newy, victim, &sublaunch) < 0)
740                 goto use_up_msl;
741             stopping = 1;
742             if (msl_hit(&plp->plane, hardtarget, EF_LAND,
743                         N_LND_MISS, N_LND_SMISS, sublaunch, victim)) {
744                 newdam = pln_damage(&plp->plane, 'p', 1);
745                 dam += newdam;
746             } else {
747                 newdam = pln_damage(&plp->plane, 'p', 0);
748                 collateral_damage(newx, newy, newdam);
749             }
750         use_up_msl:
751             plp->plane.pln_effic = 0;
752             putplane(plp->plane.pln_uid, &plp->plane);
753         }
754         emp_remque(qp);
755         free(qp);
756     }
757
758     if (dam) {
759         mpr(victim, "missile interdiction mission does %d damage!\n", dam);
760         collateral_damage(newx, newy, dam);
761         lnd_damage(list, dam);
762     }
763     return stopping;
764 }
765
766 #if 0
767 /* Steve M. - commented out for now until abuse is decided upon */
768 /* risner: allow forts to interdict land units. */
769 static int
770 lnd_fort_interdiction(struct emp_qelem *list,
771                       coord newx, coord newy, natid victim)
772 {
773     struct nstr_sect ns;
774     struct sctstr fsect;
775     int trange, range;
776     int dam;
777     int stopping = 0;
778     int totdam = 0;
779
780     snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
781     while (nxtsct(&ns, &fsect)) {
782         if (fsect.sct_own == 0)
783             continue;
784         if (relations_with(fsect.sct_own, victim) >= NEUTRAL)
785             continue;
786         range = roundrange(fortrange(&fsect));
787         trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
788         if (trange > range)
789             continue;
790         dam = fort_fire(&fsect);
791         putsect(&fsect);
792         if (dam < 0)
793             continue;
794         stopping = 1;
795         totdam += dam;
796         mpr(victim, "Incoming fire does %d damage!\n", dam);
797         wu(0, fsect.sct_own,
798            "%s fires at %s land units in %s for %d!\n",
799            xyas(fsect.sct_x, fsect.sct_y,
800                 fsect.sct_own),
801            cname(victim), xyas(newx, newy, fsect.sct_own), dam);
802         nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
803     }
804     if (totdam > 0)
805         lnd_damage(list, totdam);
806     return stopping;
807 }
808 #endif
809
810 static int
811 lnd_mission_interdiction(struct emp_qelem *list, coord x, coord y,
812                          natid victim)
813 {
814     int dam;
815
816     dam = unit_interdict(x, y, victim, "land units",
817                          lnd_easiest_target(list),
818                          MI_INTERDICT);
819     if (dam >= 0)
820         lnd_damage(list, dam);
821     return dam >= 0;
822 }
823
824 int
825 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
826 {
827     int stopping = 0;
828
829 #if 0
830     if (!opt_NO_FORT_FIRE)
831 /* Steve M. - commented out for now until abuse is decided upon */
832         stopping |= lnd_fort_interdiction(list, newx, newy, victim);
833 #endif
834
835     stopping |= lnd_mission_interdiction(list, newx, newy, victim);
836     stopping |= lnd_missile_interdiction(list, newx, newy, victim);
837     return stopping;
838 }
839
840 /* high value of hardtarget is harder to hit */
841 int
842 lnd_hardtarget(struct lndstr *lp)
843 {
844     struct sctstr sect;
845
846     getsect(lp->lnd_x, lp->lnd_y, &sect);
847     return (int)((lp->lnd_effic / 100.0) *
848                  (10 + dchr[sect.sct_type].d_dstr * 2 + lnd_spd(lp) / 2.0
849                   - lnd_vis(lp)));
850 }
851
852 static int
853 lnd_hit_mine(struct lndstr *lp)
854 {
855     int m;
856
857     mpr(lp->lnd_own, "Blammo! Landmines detected in %s!\n",
858         xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
859
860     nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
861
862     m = MINE_LDAMAGE();
863     if (lchr[lp->lnd_type].l_flags & L_ENGINEER)
864         m /= 2;
865
866     landdamage(lp, m);
867     return m;
868 }
869
870 double
871 lnd_pathcost(struct lndstr *lp, double pathcost)
872 {
873     double effspd;
874
875     effspd = lnd_spd(lp);
876     if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
877         effspd *= lp->lnd_effic * 0.01;
878
879     /*
880      * The return value must be PATHCOST times a factor that depends
881      * only on the land unit.  Anything else breaks path finding.  In
882      * particular, you can't add or enforce a minimum cost here.  Do
883      * it in sector_mcost().
884      */
885     return pathcost * 5.0 * speed_factor(effspd, lp->lnd_tech);
886 }
887
888 int
889 lnd_mobtype(struct lndstr *lp)
890 {
891     return (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
892         ? MOB_RAIL : MOB_MARCH;
893 }
894
895 double
896 lnd_mobcost(struct lndstr *lp, struct sctstr *sp)
897 {
898     return lnd_pathcost(lp, sector_mcost(sp, lnd_mobtype(lp)));
899 }
900
901 int
902 lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor,
903                    int together)
904 {
905     struct sctstr sect, osect;
906     struct emp_qelem *qp;
907     struct emp_qelem *qp2;
908     struct emp_qelem *next;
909     struct ulist *llp;
910     struct emp_qelem cur, done;
911     coord dx;
912     coord dy;
913     coord newx;
914     coord newy;
915     int stopping = 0;
916     int visible;
917     int stop;
918     char dp[80];
919     int rel;
920     int oldown;
921
922     if (dir <= DIR_STOP || dir >= DIR_VIEW) {
923         unit_put(list, actor);
924         return 1;
925     }
926     dx = diroff[dir][0];
927     dy = diroff[dir][1];
928     for (qp = list->q_back; qp != list; qp = next) {
929         next = qp->q_back;
930         llp = (struct ulist *)qp;
931         getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &osect);
932         oldown = osect.sct_own;
933         newx = xnorm(llp->unit.land.lnd_x + dx);
934         newy = ynorm(llp->unit.land.lnd_y + dy);
935         getsect(newx, newy, &sect);
936         rel = getrel(getnatp(sect.sct_own), actor);
937         if ((sect.sct_own != actor && rel != ALLIED &&
938              !(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY) &&
939              sect.sct_own) || (sect.sct_type == SCT_WATER ||
940                                sect.sct_type == SCT_SANCT ||
941                                sect.sct_type == SCT_WASTE)) {
942             if (together) {
943                 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
944                 return 1;
945             } else {
946                 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
947                 lnd_stays(actor, dp, llp);
948                 continue;
949             }
950         }
951         if (!SCT_HAS_RAIL(&sect)
952             && lnd_mobtype(&llp->unit.land) == MOB_RAIL) {
953             if (together) {
954                 mpr(actor, "no rail system in %s\n",
955                     xyas(newx, newy, actor));
956                 return 1;
957             } else {
958                 sprintf(dp, "has no rail system in %s",
959                         xyas(newx, newy, actor));
960                 lnd_stays(actor, dp, llp);
961                 continue;
962             }
963         }
964         /* Note we check would_abandon first because we don't want
965            to always have to do these checks */
966         if (would_abandon(&osect, I_CIVIL, 0, &llp->unit.land)) {
967             stop = 0;
968             if (!want_to_abandon(&osect, I_CIVIL, 0, &llp->unit.land)) {
969                 stop = 1;
970             }
971             /* now check stuff */
972             if (!check_sect_ok(&sect))
973                 return 1;
974             if (!check_sect_ok(&osect))
975                 return 1;
976             for (qp2 = list->q_back; qp2 != list; qp2 = qp2->q_back) {
977                 if (!check_land_ok(&((struct ulist *)qp2)->unit.land))
978                     return 1;
979             }
980             if (stop) {
981                 lnd_stays(actor, "stops", llp);
982                 continue;
983             }
984         }
985         if (llp->mobil <= 0.0) {
986             lnd_stays(actor, "is out of mobility", llp);
987             continue;
988         }
989         llp->unit.land.lnd_x = newx;
990         llp->unit.land.lnd_y = newy;
991         llp->mobil -= lnd_mobcost(&llp->unit.land, &sect);
992         llp->unit.land.lnd_mobil = (int)llp->mobil;
993         llp->unit.land.lnd_harden = 0;
994         putland(llp->unit.land.lnd_uid, &llp->unit.land);
995         putsect(&osect);
996         getsect(osect.sct_x, osect.sct_y, &osect);
997         if (osect.sct_own != oldown && oldown == actor) {
998             /* It was your sector, now it's not.  Simple :) */
999             mpr(actor, "You no longer own %s\n",
1000                 xyas(osect.sct_x, osect.sct_y, actor));
1001         }
1002         if (rel != ALLIED && sect.sct_own != actor && sect.sct_own) {   /* must be a spy */
1003             /* Always a 10% chance of getting caught. */
1004             if (chance(LND_SPY_DETECT_CHANCE(llp->unit.land.lnd_effic))) {
1005                 if (rel == NEUTRAL || rel == FRIENDLY) {
1006                     wu(0, sect.sct_own,
1007                        "%s unit spotted in %s\n", cname(actor),
1008                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1009                     setrel(sect.sct_own, llp->unit.land.lnd_own, HOSTILE);
1010                 } else if (rel <= HOSTILE) {
1011                     wu(0, sect.sct_own,
1012                        "%s spy shot in %s\n", cname(actor),
1013                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1014                     mpr(actor, "%s was shot and killed.\n",
1015                         prland(&llp->unit.land));
1016                     llp->unit.land.lnd_effic = 0;
1017                     putland(llp->unit.land.lnd_uid, &llp->unit.land);
1018                     lnd_delete(llp);
1019                 }
1020             }
1021         }
1022     }
1023     if (QEMPTY(list))
1024         return stopping;
1025     lnd_sweep(list, 0, 1, actor);
1026     stopping |= lnd_check_mines(list);
1027     if (QEMPTY(list))
1028         return stopping;
1029
1030     /* interdict land units sector by sector */
1031     emp_initque(&cur);
1032     emp_initque(&done);
1033     while (!QEMPTY(list)) {
1034         llp = (struct ulist *)list->q_back;
1035         newx = llp->unit.land.lnd_x;
1036         newy = llp->unit.land.lnd_y;
1037         /* move units in NEWX,NEWY to cur */
1038         visible = 0;
1039         for (qp = list->q_back; qp != list; qp = next) {
1040             next = qp->q_back;
1041             llp = (struct ulist *)qp;
1042             if (llp->unit.land.lnd_x == newx && llp->unit.land.lnd_y == newy) {
1043                 emp_remque(qp);
1044                 emp_insque(qp, &cur);
1045                 if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY))
1046                     visible = 1;
1047             }
1048         }
1049         /* interdict them */
1050         if (visible)
1051             stopping |= lnd_interdict(&cur, newx, newy, actor);
1052         /* move survivors to done */
1053         for (qp = cur.q_back; qp != &cur; qp = next) {
1054             next = qp->q_back;
1055             emp_remque(qp);
1056             emp_insque(qp, &done);
1057         }
1058     }
1059     /* assign surviving land units back to list */
1060     emp_insque(list, &done);
1061     emp_remque(&done);
1062
1063     return stopping;
1064 }
1065
1066 /*
1067  * Fire land unit support against VICTIM for ATTACKER, at X,Y.
1068  * If DEFENDING, this is defensive support, else offensive support.
1069  * Return total damage.
1070  */
1071 int
1072 lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
1073 {
1074     struct nstr_item ni;
1075     struct lndstr land;
1076     int dam, dam2;
1077     int dist;
1078     int range;
1079
1080     dam = 0;
1081     snxtitem_all(&ni, EF_LAND);
1082     while (nxtitem(&ni, &land)) {
1083         if ((land.lnd_x == x) && (land.lnd_y == y))
1084             continue;
1085         if (!feels_like_helping(land.lnd_own, attacker, victim))
1086             continue;
1087
1088         /* are we in range? */
1089         dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1090
1091         range = roundrange(lnd_fire_range(&land));
1092         if (dist > range)
1093             continue;
1094
1095         dam2 = lnd_fire(&land);
1096         putland(land.lnd_uid, &land);
1097         if (dam2 < 0)
1098             continue;
1099
1100         if (defending)
1101             nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
1102         else
1103             nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1104         if (roll(100) < lnd_acc(&land))
1105             dam2 /= 2;
1106         dam += dam2;
1107         if (land.lnd_own != attacker)
1108             wu(0, land.lnd_own,
1109                "%s supported %s at %s\n",
1110                prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1111     }
1112     return dam;
1113 }
1114
1115 int
1116 lnd_can_attack(struct lndstr *lp)
1117 {
1118     struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1119
1120     if (lcp->l_flags & L_SUPPLY)
1121         return 0;
1122
1123     return 1;
1124 }
1125
1126 /*
1127  * Increase fortification value of LP.
1128  * Fortification costs mobility.  Use up to MOB mobility.
1129  * Return actual fortification increase.
1130  */
1131 int
1132 lnd_fortify(struct lndstr *lp, int mob)
1133 {
1134     int hard_amt;
1135     double mob_used, mult;
1136
1137     if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
1138         return 0;
1139
1140     mob_used = MIN(lp->lnd_mobil, mob);
1141     if (mob_used < 0)
1142         return 0;
1143
1144     mult = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own)
1145         ? 1.5 : 1.0;
1146
1147     hard_amt = (int)(mob_used * mult);
1148     if (lp->lnd_harden + hard_amt > land_mob_max) {
1149         hard_amt = land_mob_max - lp->lnd_harden;
1150         mob_used = ceil(hard_amt / mult);
1151     }
1152
1153     lp->lnd_mobil -= (int)mob_used;
1154     lp->lnd_harden += hard_amt;
1155     lp->lnd_harden = MIN(lp->lnd_harden, land_mob_max);
1156
1157     return hard_amt;
1158 }
1159
1160 /*
1161  * Is there a engineer unit at X,Y that can help nation CN?
1162  */
1163 static int
1164 has_helpful_engineer(coord x, coord y, natid cn)
1165 {
1166     struct nstr_item ni;
1167     struct lndstr land;
1168
1169     snxtitem_xy(&ni, EF_LAND, x, y);
1170     while (nxtitem(&ni, &land)) {
1171         if (relations_with(land.lnd_own, cn) != ALLIED)
1172             continue;
1173         if (lchr[(int)land.lnd_type].l_flags & L_ENGINEER)
1174             return 1;
1175     }
1176
1177     return 0;
1178 }
1179
1180 /*
1181  * Set LP's tech to TLEV along with everything else that depends on it.
1182  */
1183 void
1184 lnd_set_tech(struct lndstr *lp, int tlev)
1185 {
1186     struct lchrstr *lcp = lchr + lp->lnd_type;
1187
1188     if (CANT_HAPPEN(tlev < lcp->l_tech))
1189         tlev = lcp->l_tech;
1190
1191     lp->lnd_tech = tlev;
1192 }