]> git.pond.sub.org Git - empserver/blob - src/lib/subs/lndsub.c
Break inclusion cycle: prototypes.h and commands.h included each
[empserver] / src / lib / subs / lndsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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-2006
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 "land.h"
44 #include "misc.h"
45 #include "mission.h"
46 #include "nat.h"
47 #include "news.h"
48 #include "nsc.h"
49 #include "optlist.h"
50 #include "path.h"
51 #include "plane.h"
52 #include "player.h"
53 #include "prototypes.h"
54 #include "sect.h"
55 #include "ship.h"
56 #include "xy.h"
57
58 static void lnd_mess(char *, struct llist *);
59 static int lnd_hit_mine(struct lndstr *, struct lchrstr *);
60
61 double
62 attack_val(int combat_mode, struct lndstr *lp)
63 {
64     int men;
65     double value;
66     struct lchrstr *lcp;
67
68     if (lp->lnd_effic < LAND_MINEFF) {
69         putland(lp->lnd_uid, lp);
70         return 0;
71     }
72
73     lcp = &lchr[(int)lp->lnd_type];
74
75 /* Spies always count as 1 during assaults.  If they are the only ones
76    in the assault, they get to sneak on anyway. */
77
78     if (lcp->l_flags & L_SPY && combat_mode == A_ASSAULT)
79         return 1;
80
81     men = lp->lnd_item[I_MILIT];
82     value = men * lp->lnd_att * lp->lnd_effic / 100.0;
83
84     switch (combat_mode) {
85     case A_ATTACK:
86         return value;
87     case A_ASSAULT:
88         if (!(lcp->l_flags & L_MARINE))
89             return assault_penalty * value;
90         break;
91     case A_BOARD:
92         if (!(lcp->l_flags & L_MARINE))
93             return assault_penalty * men;
94     }
95
96     return value;
97 }
98
99 double
100 defense_val(struct lndstr *lp)
101 {
102     int men;
103     double value;
104     struct lchrstr *lcp;
105
106     if (lp->lnd_effic < LAND_MINEFF) {
107         putland(lp->lnd_uid, lp);
108         return 0;
109     }
110
111     lcp = &lchr[(int)lp->lnd_type];
112
113     men = lp->lnd_item[I_MILIT];
114
115     if ((lp->lnd_ship >= 0 || lp->lnd_land >= 0) &&
116         !(lcp->l_flags & L_MARINE))
117         return men;
118
119     value = men * lp->lnd_def * lp->lnd_effic / 100.0;
120     value *= ((double)land_mob_max + lp->lnd_harden) / land_mob_max;
121
122     /* If there are military on the unit, you get at least a 1
123        man defensive unit, except for spies */
124     if (value < 1.0 && men > 0 && !(lcp->l_flags & L_SPY))
125         return 1;
126
127     return value;
128 }
129
130 void
131 lnd_print(struct llist *llp, char *s)
132 {
133     if (llp->land.lnd_own == player->cnum)
134         pr("%s %s\n", prland(&llp->land), s);
135     else
136         wu(0, llp->land.lnd_own, "%s %s\n", prland(&llp->land), s);
137 }
138
139 void
140 lnd_delete(struct llist *llp, char *s)
141 {
142     if (s)
143         lnd_print(llp, s);
144     putland(llp->land.lnd_uid, &llp->land);
145     emp_remque((struct emp_qelem *)llp);
146     free(llp);
147 }
148
149 int
150 lnd_take_casualty(int combat_mode, struct llist *llp, int cas)
151                         /* attacking or assaulting or paratrooping? */
152                         /* number of casualties to take */
153 {
154     int eff_eq;
155     int n;
156     int biggest;
157     int civs;
158     int nowned;
159     coord ret_x, ret_y;
160     coord bx, by;
161     struct sctstr sect;
162     int ret_chance;
163     char buf[1024];
164     int taken;
165     int nowhere_to_go = 0;
166     struct sctstr rsect;
167     double mobcost, bmcost;
168     signed char orig;
169     int mob;
170
171     taken = llp->land.lnd_item[I_MILIT];
172     /* Spies always die */
173     if (llp->lcp->l_flags & L_SPY) {
174         eff_eq = 100;
175         llp->land.lnd_effic = 0;
176     } else {
177         eff_eq = ldround(cas * 100.0 / llp->lcp->l_mil, 1);
178         llp->land.lnd_effic -= eff_eq;
179         lnd_submil(&llp->land, cas);
180     }
181
182     if (llp->land.lnd_effic < LAND_MINEFF) {
183         sprintf(buf, "dies %s %s!",
184                 combat_mode ? att_mode[combat_mode] : "defending",
185                 xyas(llp->land.lnd_x, llp->land.lnd_y, llp->land.lnd_own));
186         lnd_delete(llp, buf);
187         /* Since we killed the unit, we killed all the mil on it */
188         return taken;
189     } else {
190         /* Ok, now, how many did we take off? (sould be the diff) */
191         taken = taken - llp->land.lnd_item[I_MILIT];
192     }
193
194     if (llp->land.lnd_effic >= llp->land.lnd_retreat)
195         return taken;
196
197     /* we're being boarded */
198     if (llp->land.lnd_ship >= 0 && combat_mode == A_DEFEND)
199         return taken;
200
201     /* we're being boarded */
202     if (llp->land.lnd_land >= 0 && combat_mode == A_DEFEND)
203         return taken;
204
205     /* Have to make a retreat check */
206
207     ret_chance = llp->land.lnd_retreat - llp->land.lnd_effic;
208     if (roll(100) < ret_chance) {
209         pr("\n");
210         lnd_print(llp, "fails morale check!");
211         llp->land.lnd_mission = 0;
212         llp->land.lnd_harden = 0;
213         if (llp->land.lnd_ship >= 0 || llp->land.lnd_land >= 0)
214             nowhere_to_go = 1;
215         else if (combat_mode == A_DEFEND) {
216             /*
217              * defending unit.. find a place to send it
218              * strategy: look for the most-populated 
219              * adjacent sector that is owned by the unit
220              * player->owner. Charge mob..
221              */
222             biggest = -1;
223             nowned = 0;
224             for (n = 1; n <= 6; ++n) {
225                 ret_x = llp->land.lnd_x + diroff[n][0];
226                 ret_y = llp->land.lnd_y + diroff[n][1];
227                 getsect(ret_x, ret_y, &sect);
228                 if (sect.sct_own != llp->land.lnd_own)
229                     continue;
230                 if (sect.sct_type == SCT_MOUNT)
231                     continue;
232                 mobcost = lnd_mobcost(&llp->land, &rsect);
233                 if (mobcost < 0)
234                     continue;
235                 ++nowned;
236                 civs = sect.sct_item[I_CIVIL];
237                 if (civs > biggest) {
238                     biggest = civs;
239                     bx = sect.sct_x;
240                     by = sect.sct_y;
241                     bmcost = mobcost;
242                 }
243             }
244             if (!nowned)
245                 nowhere_to_go = 1;
246             else {
247                 /* retreat to bx,by */
248                 llp->land.lnd_x = bx;
249                 llp->land.lnd_y = by;
250                 /* FIXME landmines */
251                 getsect(bx, by, &rsect);
252                 mob = llp->land.lnd_mobil - (int)bmcost;
253                 if (mob < -127)
254                     mob = -127;
255                 orig = llp->land.lnd_mobil;
256                 llp->land.lnd_mobil = (signed char)mob;
257                 if (llp->land.lnd_mobil > orig)
258                     llp->land.lnd_mobil = -127;
259                 sprintf(buf, "retreats at %d%% efficiency to %s!",
260                         llp->land.lnd_effic,
261                         xyas(bx, by, llp->land.lnd_own));
262                 lnd_delete(llp, buf);
263             }
264         } else {                /* attacking from a sector */
265             sprintf(buf, "leaves the battlefield at %d%% efficiency",
266                     llp->land.lnd_effic);
267             if ((llp->land.lnd_mobil - (int)llp->mobil) < -127)
268                 llp->land.lnd_mobil = -127;
269             else
270                 llp->land.lnd_mobil -= (int)llp->mobil;
271             llp->mobil = 0.0;
272             lnd_delete(llp, buf);
273         }
274     }
275     if (nowhere_to_go) {
276         /* nowhere to go.. take more casualties */
277         llp->land.lnd_effic -= 10;
278         lnd_submil(&llp->land, llp->lcp->l_mil / 10);
279         if (llp->land.lnd_effic < LAND_MINEFF)
280             lnd_delete(llp, "has nowhere to retreat, and dies!");
281         else
282             lnd_print(llp,
283                       "has nowhere to retreat and takes extra losses!");
284     }
285
286     return taken;
287 }
288
289 void
290 lnd_takemob(struct emp_qelem *list, double loss)
291 {
292     struct emp_qelem *qp, *next;
293     struct llist *llp;
294     int new;
295     int mcost = ldround(combat_mob * loss, 1);
296
297     for (qp = list->q_forw; qp != list; qp = next) {
298         next = qp->q_forw;
299         llp = (struct llist *)qp;
300 /*
301                 if (chance(loss))
302                         use_supply(&llp->land);
303                 if (llp->land.lnd_mission == MI_RESERVE)
304                         new = llp->land.lnd_mobil - mcost/2;
305                 else
306  */
307         new = llp->land.lnd_mobil - mcost;
308         if (new < -127)
309             new = -127;
310         llp->land.lnd_mobil = (signed char)new;
311     }
312 }
313
314 void
315 lnd_submil(struct lndstr *lp, int num)
316 {
317     int new = lp->lnd_item[I_MILIT] - num;
318     lp->lnd_item[I_MILIT] = new < 0 ? 0 : new;
319 }
320
321 int
322 lnd_spyval(struct lndstr *lp)
323 {
324     if (lchr[(int)lp->lnd_type].l_flags & L_RECON)
325         return lp->lnd_spy * (lp->lnd_effic / 100.0) + 2;
326     else
327         return lp->lnd_spy * (lp->lnd_effic / 100.0);
328 }
329
330 double
331 intelligence_report(int destination, struct lndstr *lp, int spy,
332                     char *mess)
333 {
334     struct lchrstr *lcp;
335     char buf1[80], buf2[80], buf3[80];
336     double estimate = 0.0;      /* estimated defense value */
337
338     if (destination == 0)
339         return 0;
340
341     if (lp->lnd_own == 0)
342         return 0;
343
344     lcp = &lchr[(int)lp->lnd_type];
345
346     memset(buf1, 0, sizeof(buf1));
347     memset(buf2, 0, sizeof(buf2));
348     memset(buf3, 0, sizeof(buf3));
349     if (chance((spy + lp->lnd_vis) / 10.0)) {
350         if (destination == player->cnum)
351             pr("%s %s", mess, prland(lp));
352         else
353             sprintf(buf1, "%s %s", mess, prland(lp));
354
355         estimate = lp->lnd_item[I_MILIT];
356
357         if (chance((spy + lp->lnd_vis) / 20.0)) {
358             if (destination == player->cnum)
359                 pr(" (eff %d, mil %d",
360                    roundintby(lp->lnd_effic, 5),
361                    roundintby(lp->lnd_item[I_MILIT], 10));
362             else
363                 sprintf(buf2, " (eff %d, mil %d",
364                         roundintby(lp->lnd_effic, 5),
365                         roundintby(lp->lnd_item[I_MILIT], 10));
366             estimate = lp->lnd_item[I_MILIT] * lp->lnd_effic / 100.0;
367
368             if (chance((spy + lp->lnd_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     if (lp->lnd_ship < 0 || lcp->l_flags & L_MARINE)
395         estimate *= lp->lnd_def;
396
397     return estimate;
398 }
399
400 /* Used by the spy command to count land units in a sector.  If used
401    for anything else, you may want to reconsider, because this doesn't
402    always count spies. :) */
403 int
404 count_sect_units(struct sctstr *sp)
405 {
406     int count = 0;
407     struct nstr_item ni;
408     struct lndstr land;
409
410     snxtitem_all(&ni, EF_LAND);
411     while (nxtitem(&ni, &land)) {
412         if (!land.lnd_own)
413             continue;
414         if (land.lnd_x != sp->sct_x || land.lnd_y != sp->sct_y)
415             continue;
416         /* Don't always see spies */
417         if (lchr[(int)land.lnd_type].l_flags & L_SPY) {
418             if (!(chance(LND_SPY_DETECT_CHANCE(land.lnd_effic))))
419                 continue;
420         }
421         /* Got here, report it */
422         ++count;
423     }
424
425     return count;
426 }
427
428 void
429 count_units(struct shpstr *sp)
430 {
431     struct nstr_item ni;
432     struct lndstr land;
433     int nland = 0;
434
435     if (sp->shp_effic < SHIP_MINEFF)
436         return;
437
438     snxtitem_xy(&ni, EF_LAND, sp->shp_x, sp->shp_y);
439     while (nxtitem(&ni, &land)) {
440         if (land.lnd_own == 0)
441             continue;
442         if (land.lnd_ship == sp->shp_uid)
443             nland++;
444     }
445
446     if (sp->shp_nland != nland) {
447         sp->shp_nland = nland;
448         putship(sp->shp_uid, sp);
449     }
450 }
451
452 void
453 lnd_count_units(struct lndstr *lp)
454 {
455     struct nstr_item ni;
456     struct lndstr land;
457     int nland = 0;
458
459     if (lp->lnd_effic < LAND_MINEFF)
460         return;
461
462     snxtitem_xy(&ni, EF_LAND, lp->lnd_x, lp->lnd_y);
463     while (nxtitem(&ni, &land)) {
464         if (land.lnd_own == 0)
465             continue;
466         if (land.lnd_land == lp->lnd_uid)
467             nland++;
468     }
469
470     if (lp->lnd_nland != nland) {
471         lp->lnd_nland = nland;
472         putland(lp->lnd_uid, lp);
473     }
474 }
475
476 void
477 lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
478 {
479     struct lndstr land;
480     struct lchrstr *lcp;
481     struct llist *llp;
482     int this_mot;
483     int mobtype = MOB_MOVE;     /* indeterminate */
484
485     emp_initque(list);
486     while (nxtitem(ni, &land)) {
487         if (!player->owner)
488             continue;
489         if (opt_MARKET) {
490             if (ontradingblock(EF_LAND, &land)) {
491                 pr("unit #%d inelligible - it's for sale.\n",
492                    land.lnd_uid);
493                 continue;
494             }
495         }
496         /*
497          * The marching code gets confused when trains and non-trains
498          * march together.  Disallow for now.
499          */
500         this_mot = lnd_mobtype(&land);
501         if (this_mot != mobtype) {
502             if (mobtype == MOB_MOVE)
503                 mobtype = this_mot;
504             else if (mobtype == MOB_MARCH) {
505                 pr("%s is a train and can't march with the leader.\n",
506                    prland(&land));
507                 continue;
508             } else {
509                 pr("%s can't rail-march with the leading train.\n",
510                    prland(&land));
511                 continue;
512             }
513         }
514
515         lcp = &lchr[(int)land.lnd_type];
516         land.lnd_mission = 0;
517         land.lnd_rflags = 0;
518         land.lnd_harden = 0;
519         memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
520         putland(land.lnd_uid, &land);
521         llp = malloc(sizeof(struct llist));
522         llp->lcp = lcp;
523         llp->land = land;
524         llp->mobil = land.lnd_mobil;
525         emp_insque(&llp->queue, list);
526     }
527 }
528
529 /* This function assumes that the list was created by lnd_sel */
530 void
531 lnd_mar(struct emp_qelem *list, double *minmobp, double *maxmobp,
532         int *togetherp, natid actor)
533 {
534     struct emp_qelem *qp;
535     struct emp_qelem *next;
536     struct llist *llp;
537     struct sctstr sect;
538     struct lndstr land;
539     coord allx;
540     coord ally;
541     int first = 1;
542     char mess[128];
543     int rel;
544
545     *minmobp = 9876.0;
546     *maxmobp = -9876.0;
547     *togetherp = 1;
548     for (qp = list->q_back; qp != list; qp = next) {
549         next = qp->q_back;
550         llp = (struct llist *)qp;
551         getland(llp->land.lnd_uid, &land);
552         if (land.lnd_own != actor) {
553             mpr(actor, "%s was disbanded at %s\n",
554                 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
555             emp_remque((struct emp_qelem *)llp);
556             free(llp);
557             continue;
558         }
559         if (land.lnd_ship >= 0) {
560             lnd_mess("is on a ship", llp);
561             continue;
562         }
563         if (land.lnd_land >= 0) {
564             lnd_mess("is on a unit", llp);
565             continue;
566         }
567         if (!getsect(land.lnd_x, land.lnd_y, &sect)) {
568             lnd_mess("was sucked into the sky by a strange looking spaceland", llp);    /* heh -KHS */
569             continue;
570         }
571         if (!(lchr[(int)llp->land.lnd_type].l_flags & L_SPY) &&
572             !(lchr[(int)llp->land.lnd_type].l_flags & L_TRAIN) &&
573             llp->land.lnd_item[I_MILIT] == 0) {
574             lnd_mess("has no mil on it to guide it", llp);
575             continue;
576         }
577         rel = getrel(getnatp(sect.sct_own), player->cnum);
578         if (sect.sct_own != land.lnd_own && rel != ALLIED &&
579             !(lchr[(int)llp->land.lnd_type].l_flags & L_SPY) &&
580             sect.sct_own) {
581             sprintf(mess, "has been kidnapped by %s", cname(sect.sct_own));
582             lnd_mess(mess, llp);
583             continue;
584         }
585         if (first) {
586             allx = land.lnd_x;
587             ally = land.lnd_y;
588             first = 0;
589         }
590         if (land.lnd_x != allx || land.lnd_y != ally)
591             *togetherp = 0;
592         if (land.lnd_mobil + 1 < (int)llp->mobil) {
593             llp->mobil = land.lnd_mobil;
594         }
595         if (llp->mobil < *minmobp)
596             *minmobp = llp->mobil;
597         if (llp->mobil > *maxmobp)
598             *maxmobp = llp->mobil;
599         llp->land = land;
600     }
601 }
602
603 void
604 lnd_put(struct emp_qelem *list, natid actor)
605 {
606     struct emp_qelem *qp;
607     struct emp_qelem *newqp;
608     struct llist *llp;
609
610     qp = list->q_back;
611     while (qp != list) {
612         llp = (struct llist *)qp;
613         if (actor) {
614             mpr(actor, "%s stopped at %s\n", prland(&llp->land),
615                 xyas(llp->land.lnd_x, llp->land.lnd_y, llp->land.lnd_own));
616             if (llp->mobil < -127)
617                 llp->mobil = -127;
618             llp->land.lnd_mobil = llp->mobil;
619         }
620         putland(llp->land.lnd_uid, &llp->land);
621         newqp = qp->q_back;
622         emp_remque(qp);
623         free(qp);
624         qp = newqp;
625     }
626 }
627
628 void
629 lnd_sweep(struct emp_qelem *land_list, int verbose, int takemob,
630           natid actor)
631 {
632     struct emp_qelem *qp;
633     struct emp_qelem *next;
634     struct llist *llp;
635     struct sctstr sect;
636     int mines, m, max, sshells, lshells;
637
638     for (qp = land_list->q_back; qp != land_list; qp = next) {
639         next = qp->q_back;
640         llp = (struct llist *)qp;
641         if (!(llp->lcp->l_flags & L_ENGINEER)) {
642             if (verbose)
643                 mpr(actor, "%s is not an engineer!\n", prland(&llp->land));
644             continue;
645         }
646         if (takemob && llp->mobil < 0.0) {
647             if (verbose)
648                 lnd_mess("is out of mobility", llp);
649             continue;
650         }
651         getsect(llp->land.lnd_x, llp->land.lnd_y, &sect);
652         if (sect.sct_oldown == llp->land.lnd_own) {
653             if (verbose)
654                 mpr(actor,
655                     "%s is in a sector completely owned by you.  Don't bother digging up mines there!\n",
656                     prland(&llp->land));
657             continue;
658         }
659         if (sect.sct_type == SCT_BSPAN) {
660             if (verbose)
661                 mpr(actor, "%s is on a bridge.  No mines there!\n",
662                     prland(&llp->land));
663             continue;
664         }
665         if (takemob) {
666             llp->mobil -= lnd_pathcost(&llp->land, 0.2);
667             llp->land.lnd_mobil = (int)llp->mobil;
668             llp->land.lnd_harden = 0;
669         }
670         putland(llp->land.lnd_uid, &llp->land);
671         if (!(mines = sect.sct_mines))
672             continue;
673         max = llp->lcp->l_item[I_SHELL];
674         lshells = llp->land.lnd_item[I_SHELL];
675         sshells = sect.sct_item[I_SHELL];
676         for (m = 0; mines > 0 && m < max * 2; m++) {
677             if (chance(0.5 * llp->lcp->l_att)) {
678                 mpr(actor, "Sweep...\n");
679                 mines--;
680                 if (lshells < max)
681                     ++lshells;
682                 else if (sshells < ITEM_MAX)
683                     ++sshells;
684             }
685         }
686         sect.sct_mines = mines;
687         llp->land.lnd_item[I_SHELL] = lshells;
688         sect.sct_item[I_SHELL] = sshells;
689         putland(llp->land.lnd_uid, &llp->land);
690         putsect(&sect);
691     }
692 }
693
694 static int
695 contains_engineer(struct emp_qelem *list)
696 {
697     struct emp_qelem *qp;
698     struct emp_qelem *next;
699     struct llist *llp;
700
701     for (qp = list->q_back; qp != list; qp = next) {
702         next = qp->q_back;
703         llp = (struct llist *)qp;
704         if (llp->lcp->l_flags & L_ENGINEER)
705             return 1;
706     }
707     return 0;
708 }
709
710 int
711 lnd_check_mines(struct emp_qelem *land_list)
712 {
713     struct emp_qelem *qp;
714     struct emp_qelem *next;
715     struct llist *llp;
716     struct sctstr sect;
717     int stopping = 0;
718     int with_eng = contains_engineer(land_list);
719
720     for (qp = land_list->q_back; qp != land_list; qp = next) {
721         next = qp->q_back;
722         llp = (struct llist *)qp;
723         getsect(llp->land.lnd_x, llp->land.lnd_y, &sect);
724         if (sect.sct_oldown == llp->land.lnd_own)
725             continue;
726         if (sect.sct_type == SCT_BSPAN)
727             continue;
728         if (!sect.sct_mines)
729             continue;
730         if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
731             lnd_hit_mine(&llp->land, llp->lcp);
732             sect.sct_mines--;
733             putsect(&sect);
734             putland(llp->land.lnd_uid, &llp->land);
735             if (!llp->land.lnd_own) {
736                 stopping = 1;
737                 emp_remque(qp);
738                 free(qp);
739             }
740         }
741     }
742     return stopping;
743 }
744
745 void
746 lnd_list(struct emp_qelem *land_list)
747 {
748     struct emp_qelem *qp;
749     struct emp_qelem *next;
750     struct llist *llp;
751     struct lndstr *lnd;
752
753     pr("lnd#     land type       x,y    a  eff  sh gun xl  mu tech retr fuel\n");
754
755     for (qp = land_list->q_back; qp != land_list; qp = next) {
756         next = qp->q_back;
757         llp = (struct llist *)qp;
758         lnd = &llp->land;
759         pr("%4d ", lnd->lnd_uid);
760         pr("%-16.16s ", llp->lcp->l_name);
761         prxy("%4d,%-4d ", lnd->lnd_x, lnd->lnd_y, llp->land.lnd_own);
762         pr("%1.1s", &lnd->lnd_army);
763         pr("%4d%%", lnd->lnd_effic);
764         pr("%4d", lnd->lnd_item[I_SHELL]);
765         pr("%4d", lnd->lnd_item[I_GUN]);
766         count_land_planes(lnd);
767         pr("%3d", lnd->lnd_nxlight);
768         pr("%4d", lnd->lnd_mobil);
769         pr("%4d", lnd->lnd_tech);
770         pr("%4d%%", lnd->lnd_retreat);
771         pr("%5d\n", lnd->lnd_fuel);
772     }
773 }
774
775 static void
776 lnd_mess(char *str, struct llist *llp)
777 {
778     mpr(llp->land.lnd_own, "%s %s & stays in %s\n",
779         prland(&llp->land),
780         str, xyas(llp->land.lnd_x, llp->land.lnd_y, llp->land.lnd_own));
781     if (llp->mobil < -127)
782         llp->mobil = -127;
783     llp->land.lnd_mobil = llp->mobil;
784     putland(llp->land.lnd_uid, &llp->land);
785     emp_remque((struct emp_qelem *)llp);
786     free(llp);
787 }
788
789 static int
790 lnd_count(struct emp_qelem *list)
791 {
792     struct emp_qelem *qp;
793     struct emp_qelem *next;
794     struct llist *llp;
795     int count = 0;
796
797     for (qp = list->q_back; qp != list; qp = next) {
798         next = qp->q_back;
799         llp = (struct llist *)qp;
800         ++count;
801     }
802     return count;
803 }
804
805 static int
806 lnd_damage(struct emp_qelem *list, int totdam)
807 {
808     struct emp_qelem *qp;
809     struct emp_qelem *next;
810     struct llist *llp;
811     int dam;
812     int count;
813
814     if (!totdam || !(count = lnd_count(list)))
815         return 0;
816     dam = ldround((double)totdam / count, 1);
817     for (qp = list->q_back; qp != list; qp = next) {
818         next = qp->q_back;
819         llp = (struct llist *)qp;
820         /* have to get it again because of collateral damage */
821         getland(llp->land.lnd_uid, &llp->land);
822         landdamage(&llp->land, dam);
823         putland(llp->land.lnd_uid, &llp->land);
824         if (!llp->land.lnd_own) {
825             emp_remque(qp);
826             free(qp);
827         }
828     }
829     return dam;
830 }
831
832 static int
833 lnd_easiest_target(struct emp_qelem *list)
834 {
835     struct emp_qelem *qp;
836     struct emp_qelem *next;
837     struct llist *llp;
838     int hard;
839     int easiest = 9876;         /* things start great for victim */
840     int count = 0;
841
842     for (qp = list->q_back; qp != list; qp = next) {
843         next = qp->q_back;
844         llp = (struct llist *)qp;
845         hard = lnd_hardtarget(&llp->land);
846         if (hard < easiest)
847             easiest = hard;     /* things get worse for victim */
848         ++count;
849     }
850     return easiest - count;
851 }
852
853 static int
854 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
855                          natid victim)
856 {
857     int dam;
858     struct emp_qelem msl_list, *qp, *newqp;
859
860     msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
861
862     dam = msl_launch_mindam(&msl_list, newx, newy,
863                             lnd_easiest_target(list), EF_LAND,
864                             lnd_count(list) * 20, "troops", victim,
865                             MI_INTERDICT);
866     if (dam) {
867         mpr(victim, "missile interdiction mission does %d damage!\n", dam);
868         collateral_damage(newx, newy, dam, 0);
869     }
870     qp = msl_list.q_forw;
871     while (qp != msl_list.q_forw) {
872         newqp = qp->q_forw;
873         emp_remque(qp);
874         free(qp);
875         qp = newqp;
876     }
877     return dam;
878 }
879
880 #if 0
881 /* Steve M. - commented out for now until abuse is decided upon */
882 /* risner: allow forts to interdict land units. */
883 static int
884 lnd_fort_interdiction(struct emp_qelem *list,
885                       coord newx, coord newy, natid victim)
886 {
887     struct nstr_sect ns;
888     struct sctstr fsect;
889     int trange, range;
890     double guneff;
891     int shell, gun;
892     int dam;
893     int totdam = 0;
894     int i;
895
896     snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
897     while (nxtsct(&ns, &fsect)) {
898         if (fsect.sct_own == 0)
899             continue;
900         if (fsect.sct_own == victim)
901             continue;
902         if (getrel(getnatp(fsect.sct_own), victim) >= NEUTRAL)
903             continue;
904         gun = fsect.sct_item[I_GUN];
905         if (gun < 1)
906             continue;
907         range = roundrange(fortrange(&fsect));
908         trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
909         if (trange > range)
910             continue;
911         if (fsect.sct_item[I_MILIT] < 5)
912             continue;
913         shell = fsect.sct_item[I_SHELL];
914         if (shell < 1)
915             shell += supply_commod(fsect.sct_own, fsect.sct_x, fsect.sct_y,
916                                    I_SHELL, 1);
917         if (shell < 1)
918             continue;
919         shell--;
920         fsect.sct_item[I_SHELL] = shell;
921         putsect(&fsect);
922         if (gun > 7)
923             gun = 7;
924         guneff = landgun((int)fsect.sct_effic, gun);
925         dam = (int)guneff;
926         totdam += dam;
927         mpr(victim, "Incoming fire does %d damage!\n", dam);
928         wu(0, fsect.sct_own,
929            "%s fires at %s land units in %s for %d!\n",
930            xyas(fsect.sct_x, fsect.sct_y,
931                 fsect.sct_own),
932            cname(victim), xyas(newx, newy, fsect.sct_own), dam);
933         nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
934     }
935     if (totdam > 0)
936         return lnd_damage(list, totdam);
937     return 0;
938 }
939 #endif
940
941 int
942 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
943 {
944     int stopping = 0;
945
946 #if 0
947     if (!opt_NO_FORT_FIRE)
948 /* Steve M. - commented out for now until abuse is decided upon */
949         stopping |= lnd_fort_interdiction(list, newx, newy, victim);
950 #endif
951
952     stopping |=
953         lnd_damage(list,
954                    unit_interdict(newx, newy, victim, "land units",
955                                   lnd_easiest_target(list), MI_INTERDICT));
956
957     stopping |=
958         lnd_damage(list,
959                    lnd_missile_interdiction(list, newx, newy, victim));
960     return stopping;
961 }
962
963 /* high value of hardtarget is harder to hit */
964 int
965 lnd_hardtarget(struct lndstr *lp)
966 {
967     struct sctstr sect;
968
969     getsect(lp->lnd_x, lp->lnd_y, &sect);
970     return (int)((lp->lnd_effic / 100.0) *
971                  (10 + dchr[sect.sct_type].d_dstr * 2 + lp->lnd_spd / 2.0
972                   - lp->lnd_vis));
973 }
974
975 static int
976 lnd_hit_mine(struct lndstr *lp, struct lchrstr *lcp)
977 {
978     int m;
979
980     mpr(lp->lnd_own, "Blammo! Landmines detected in %s! ",
981         xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
982
983     nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
984
985     m = MINE_LDAMAGE();
986     if (lcp->l_flags & L_ENGINEER)
987         m /= 2;
988
989     landdamage(lp, m);
990     return m;
991 }
992
993 double
994 lnd_pathcost(struct lndstr *lp, double pathcost)
995 {
996     double effspd;
997
998     effspd = lp->lnd_spd;
999     if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
1000         effspd *= lp->lnd_effic * 0.01;
1001
1002     /*
1003      * The return value must be PATHCOST times a factor that depends
1004      * only on the land unit.  Anything else breaks path finding.  In
1005      * particular, you can't add or enforce a minimum cost here.  Do
1006      * it in sector_mcost().
1007      */
1008     return pathcost * 5.0 * speed_factor(effspd, lp->lnd_tech);
1009 }
1010
1011 int
1012 lnd_mobtype(struct lndstr *lp)
1013 {
1014     return (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
1015         ? MOB_RAIL : MOB_MARCH;
1016 }
1017
1018 double
1019 lnd_mobcost(struct lndstr *lp, struct sctstr *sp)
1020 {
1021     return lnd_pathcost(lp, sector_mcost(sp, lnd_mobtype(lp)));
1022 }
1023
1024 int
1025 lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor,
1026                    int together)
1027 {
1028     struct sctstr sect, osect;
1029     struct emp_qelem *qp;
1030     struct emp_qelem *qp2;
1031     struct emp_qelem *next;
1032     struct llist *llp;
1033     struct emp_qelem cur, done;
1034     coord dx;
1035     coord dy;
1036     coord newx;
1037     coord newy;
1038     int stopping = 0;
1039     int visible;
1040     int stop;
1041     char dp[80];
1042     int rel;
1043     int oldown;
1044
1045     if (dir <= DIR_STOP || dir >= DIR_VIEW) {
1046         lnd_put(list, actor);
1047         return 1;
1048     }
1049     dx = diroff[dir][0];
1050     dy = diroff[dir][1];
1051     for (qp = list->q_back; qp != list; qp = next) {
1052         next = qp->q_back;
1053         llp = (struct llist *)qp;
1054         getsect(llp->land.lnd_x, llp->land.lnd_y, &osect);
1055         oldown = osect.sct_own;
1056         newx = xnorm(llp->land.lnd_x + dx);
1057         newy = ynorm(llp->land.lnd_y + dy);
1058         getsect(newx, newy, &sect);
1059         rel = getrel(getnatp(sect.sct_own), player->cnum);
1060         if ((sect.sct_own != actor && rel != ALLIED &&
1061              !(lchr[(int)llp->land.lnd_type].l_flags & L_SPY) &&
1062              sect.sct_own) || (sect.sct_type == SCT_WATER ||
1063                                sect.sct_type == SCT_SANCT ||
1064                                sect.sct_type == SCT_WASTE)) {
1065             if (together) {
1066                 pr("can't go to %s\n", xyas(newx, newy, actor));
1067                 return 1;
1068             } else {
1069                 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
1070                 lnd_mess(dp, llp);
1071                 continue;
1072             }
1073         }
1074         if ((!intrchr[INT_RAIL].in_enable || sect.sct_rail == 0)
1075             && lnd_mobtype(&llp->land) == MOB_RAIL) {
1076             if (together) {
1077                 pr("no rail system in %s\n", xyas(newx, newy, actor));
1078                 return 1;
1079             } else {
1080                 sprintf(dp, "has no rail system in %s",
1081                         xyas(newx, newy, actor));
1082                 lnd_mess(dp, llp);
1083                 continue;
1084             }
1085         }
1086         /* Note we check would_abandon first because we don't want
1087            to always have to do these checks */
1088         if (would_abandon(&osect, I_CIVIL, 0, &llp->land)) {
1089             stop = 0;
1090             if (!want_to_abandon(&osect, I_CIVIL, 0, &llp->land)) {
1091                 stop = 1;
1092             }
1093             /* now check stuff */
1094             if (!check_sect_ok(&sect))
1095                 return 1;
1096             if (!check_sect_ok(&osect))
1097                 return 1;
1098             for (qp2 = list->q_back; qp2 != list; qp2 = qp2->q_back) {
1099                 if (!check_land_ok(&((struct llist *)qp2)->land))
1100                     return 1;
1101             }
1102             if (stop) {
1103                 lnd_mess("stops", llp);
1104                 continue;
1105             }
1106         }
1107         if (llp->mobil <= 0.0) {
1108             lnd_mess("is out of mobility", llp);
1109             continue;
1110         }
1111         llp->land.lnd_x = newx;
1112         llp->land.lnd_y = newy;
1113         llp->mobil -= lnd_mobcost(&llp->land, &sect);
1114         llp->land.lnd_mobil = (int)llp->mobil;
1115         llp->land.lnd_harden = 0;
1116         putland(llp->land.lnd_uid, &llp->land);
1117         putsect(&osect);
1118         getsect(osect.sct_x, osect.sct_y, &osect);
1119         if (osect.sct_own != oldown && oldown == player->cnum) {
1120             /* It was your sector, now it's not.  Simple :) */
1121             pr("You no longer own %s\n",
1122                xyas(osect.sct_x, osect.sct_y, player->cnum));
1123         }
1124         if (rel != ALLIED && sect.sct_own != actor && sect.sct_own) {   /* must be a spy */
1125             /* Always a 10% chance of getting caught. */
1126             if (chance(LND_SPY_DETECT_CHANCE(llp->land.lnd_effic))) {
1127                 if (rel == NEUTRAL || rel == FRIENDLY) {
1128                     wu(0, sect.sct_own,
1129                        "%s unit spotted in %s\n", cname(player->cnum),
1130                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1131                     setrel(sect.sct_own, llp->land.lnd_own, HOSTILE);
1132                 } else if (rel == HOSTILE || rel == AT_WAR ||
1133                            rel == SITZKRIEG || rel == MOBILIZATION) {
1134                     wu(0, sect.sct_own,
1135                        "%s spy shot in %s\n", cname(player->cnum),
1136                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1137                     pr("%s was shot and killed.\n", prland(&llp->land));
1138                     llp->land.lnd_effic = 0;
1139                     putland(llp->land.lnd_uid, &llp->land);
1140                     lnd_delete(llp, 0);
1141                 }
1142             }
1143         }
1144     }
1145     if (QEMPTY(list))
1146         return stopping;
1147     lnd_sweep(list, 0, 1, actor);
1148     stopping |= lnd_check_mines(list);
1149     if (QEMPTY(list))
1150         return stopping;
1151
1152     /* interdict land units sector by sector */
1153     emp_initque(&cur);
1154     emp_initque(&done);
1155     while (!QEMPTY(list)) {
1156         llp = (struct llist *)list->q_back;
1157         newx = llp->land.lnd_x;
1158         newy = llp->land.lnd_y;
1159         /* move units in NEWX,NEWY to cur */
1160         visible = 0;
1161         for (qp = list->q_back; qp != list; qp = next) {
1162             next = qp->q_back;
1163             llp = (struct llist *)qp;
1164             if (llp->land.lnd_x == newx && llp->land.lnd_y == newy) {
1165                 emp_remque(qp);
1166                 emp_insque(qp, &cur);
1167                 if (!(lchr[(int)llp->land.lnd_type].l_flags & L_SPY))
1168                     visible = 1;
1169             }
1170         }
1171         /* interdict them */
1172         if (visible)
1173             stopping |= lnd_interdict(&cur, newx, newy, actor);
1174         /* move survivors to done */
1175         for (qp = cur.q_back; qp != &cur; qp = next) {
1176             next = qp->q_back;
1177             llp = (struct llist *)qp;
1178             emp_remque(qp);
1179             emp_insque(qp, &done);
1180         }
1181     }
1182     /* assign surviving land units back to list */
1183     emp_insque(list, &done);
1184     emp_remque(&done);
1185
1186     return stopping;
1187 }
1188
1189 /*
1190  * find all artillery units belonging
1191  * to the attacker or defender that can fire.
1192  * Each arty unit adds +1%/damage point
1193  */
1194 int
1195 lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
1196 {
1197     struct nstr_item ni;
1198     struct lndstr land;
1199     int rel, rel2;
1200     double dam = 0.0;
1201     int dist;
1202     int shell;
1203     int gun;
1204     int range;
1205
1206     snxtitem_all(&ni, EF_LAND);
1207     while (nxtitem(&ni, &land)) {
1208         if (land.lnd_frg == 0)
1209             continue;
1210         if ((land.lnd_x == x) && (land.lnd_y == y))
1211             continue;
1212         if (land.lnd_ship >= 0)
1213             continue;
1214         if (land.lnd_land >= 0)
1215             continue;
1216         if (land.lnd_effic < LAND_MINFIREEFF)
1217             continue;
1218         /* Do we have mil? */
1219         if (land.lnd_item[I_MILIT] <= 0)
1220             continue;
1221         rel = getrel(getnatp(land.lnd_own), attacker);
1222         rel2 = getrel(getnatp(land.lnd_own), victim);
1223         if ((land.lnd_own != attacker) &&
1224             ((rel != ALLIED) || (rel2 != AT_WAR)))
1225             continue;
1226
1227         /* do we have supplies? */
1228         if (!has_supply(&land))
1229             continue;
1230
1231         /* are we in range? */
1232         dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1233
1234         range = roundrange(effrange(land.lnd_frg, land.lnd_tech));
1235         if (dist > range)
1236             continue;
1237
1238         shell = land.lnd_item[I_SHELL];
1239         gun = land.lnd_item[I_GUN];
1240
1241         if (shell == 0 || gun == 0)
1242             continue;
1243
1244         use_supply(&land);
1245         if (defending)
1246             nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
1247         else
1248             nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1249         if (roll(100) < land.lnd_acc) {
1250             dam += landunitgun(land.lnd_effic, land.lnd_dam, gun,
1251                                land.lnd_ammo, shell) / 2;
1252         } else {
1253             dam += landunitgun(land.lnd_effic, land.lnd_dam, gun,
1254                                land.lnd_ammo, shell);
1255         }
1256         if (land.lnd_own != attacker)
1257             wu(0, land.lnd_own,
1258                "%s supported %s at %s\n",
1259                prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1260     }
1261     return (int)dam;
1262 }
1263
1264 char *
1265 lnd_path(int together, struct lndstr *lp, char *buf)
1266 {
1267     coord destx;
1268     coord desty;
1269     struct sctstr d_sect, sect;
1270     char *cp;
1271     double dummy;
1272     int mtype;
1273
1274     if (!sarg_xy(buf, &destx, &desty))
1275         return 0;
1276     if (!together) {
1277         pr("Cannot go to a destination sector if not all starting in the same sector\n");
1278         return 0;
1279     }
1280     if (!getsect(destx, desty, &d_sect)) {
1281         pr("%d,%d is not a sector\n", destx, desty);
1282         return 0;
1283     }
1284     getsect(lp->lnd_x, lp->lnd_y, &sect);
1285     mtype = lnd_mobtype(lp);
1286     cp = BestLandPath(buf, &sect, &d_sect, &dummy, mtype);
1287     if (!cp) {
1288         pr("No owned %s from %s to %s!\n",
1289            mtype == MOB_RAIL ? "railway" : "path",
1290            xyas(lp->lnd_x, lp->lnd_y, player->cnum),
1291            xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
1292         return 0;
1293     }
1294     pr("Using path '%s'\n", cp);
1295     return cp;
1296 }
1297
1298 int
1299 lnd_can_attack(struct lndstr *lp)
1300 {
1301     struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1302
1303     if (lcp->l_flags & L_SUPPLY)
1304         return 0;
1305
1306     return 1;
1307 }
1308
1309 /*
1310  * Increase fortification value of LP.
1311  * Fortification costs mobility.  Use up to MOB mobility.
1312  * Return actual fortification increase.
1313  */
1314 int
1315 lnd_fortify(struct lndstr *lp, int mob)
1316 {
1317     int hard_amt;
1318     double mob_used, mult;
1319
1320     if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
1321         return 0;
1322
1323     mob_used = MIN(lp->lnd_mobil, mob);
1324     if (mob_used < 0)
1325         return 0;
1326
1327     mult = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own)
1328         ? 1.5 : 1.0;
1329
1330     hard_amt = (int)(mob_used * mult);
1331     if (lp->lnd_harden + hard_amt > land_mob_max) {
1332         hard_amt = land_mob_max - lp->lnd_harden;
1333         mob_used = ceil(hard_amt / mult);
1334     }
1335
1336     lp->lnd_mobil -= (int)mob_used;
1337     lp->lnd_harden += hard_amt;
1338     lp->lnd_harden = MIN(lp->lnd_harden, land_mob_max);
1339
1340     return hard_amt;
1341 }
1342
1343 /*
1344  * Set LP's tech to TLEV along with everything else that depends on it.
1345  */
1346 void
1347 lnd_set_tech(struct lndstr *lp, int tlev)
1348 {
1349     struct lchrstr *lcp = lchr + lp->lnd_type;
1350     int tech_diff = tlev - lcp->l_tech;
1351
1352     if (CANT_HAPPEN(tech_diff < 0)) {
1353       tlev -= tech_diff;
1354       tech_diff = 0;
1355     }
1356
1357     lp->lnd_tech = tlev;
1358     lp->lnd_att = (float)LND_ATTDEF(lcp->l_att, tech_diff);
1359     lp->lnd_def = (float)LND_ATTDEF(lcp->l_def, tech_diff);
1360     lp->lnd_vul = (int)LND_VUL(lcp->l_vul, tech_diff);
1361     lp->lnd_spd = (int)LND_SPD(lcp->l_spd, tech_diff);
1362     lp->lnd_vis = (int)LND_VIS(lcp->l_vis, tech_diff);
1363     lp->lnd_spy = (int)LND_SPY(lcp->l_spy, tech_diff);
1364     lp->lnd_rad = (int)LND_RAD(lcp->l_rad, tech_diff);
1365     lp->lnd_frg = (int)LND_FRG(lcp->l_frg, tech_diff);
1366     lp->lnd_acc = (int)LND_ACC(lcp->l_acc, tech_diff);
1367     lp->lnd_dam = (int)LND_DAM(lcp->l_dam, tech_diff);
1368     lp->lnd_ammo = (int)LND_AMM(lcp->l_ammo, tech_diff);
1369     lp->lnd_aaf = (int)LND_AAF(lcp->l_aaf, tech_diff);
1370     lp->lnd_fuelc = (int)LND_FC(lcp->l_fuelc, tech_diff);
1371     lp->lnd_fuelu = (int)LND_FU(lcp->l_fuelu, tech_diff);
1372     lp->lnd_maxlight = (int)LND_XPL(lcp->l_nxlight, tech_diff);
1373     lp->lnd_maxland = (int)LND_MXL(lcp->l_nland, tech_diff);
1374 }