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