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