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