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