]> git.pond.sub.org Git - empserver/blob - src/lib/subs/lndsub.c
Fix trailing whitespace
[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              * 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 void
390 lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
391 {
392     struct lndstr land;
393     struct lchrstr *lcp;
394     struct ulist *llp;
395     int this_mot;
396     int mobtype = MOB_MOVE;     /* indeterminate */
397
398     emp_initque(list);
399     while (nxtitem(ni, &land)) {
400         if (!player->owner)
401             continue;
402         if (opt_MARKET) {
403             if (ontradingblock(EF_LAND, &land)) {
404                 pr("unit #%d inelligible - it's for sale.\n",
405                    land.lnd_uid);
406                 continue;
407             }
408         }
409         /*
410          * The marching code gets confused when trains and non-trains
411          * march together.  Disallow for now.
412          */
413         this_mot = lnd_mobtype(&land);
414         if (this_mot != mobtype) {
415             if (mobtype == MOB_MOVE)
416                 mobtype = this_mot;
417             else if (mobtype == MOB_MARCH) {
418                 pr("%s is a train and can't march with the leader.\n",
419                    prland(&land));
420                 continue;
421             } else {
422                 pr("%s can't rail-march with the leading train.\n",
423                    prland(&land));
424                 continue;
425             }
426         }
427
428         lcp = &lchr[(int)land.lnd_type];
429         land.lnd_mission = 0;
430         land.lnd_rflags = 0;
431         land.lnd_harden = 0;
432         memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
433         putland(land.lnd_uid, &land);
434         llp = malloc(sizeof(struct ulist));
435         llp->chrp = (struct empobj_chr *)lcp;
436         llp->unit.land = land;
437         llp->mobil = land.lnd_mobil;
438         emp_insque(&llp->queue, list);
439     }
440 }
441
442 /* This function assumes that the list was created by lnd_sel */
443 void
444 lnd_mar(struct emp_qelem *list, double *minmobp, double *maxmobp,
445         int *togetherp, natid actor)
446 {
447     struct emp_qelem *qp;
448     struct emp_qelem *next;
449     struct ulist *llp;
450     struct sctstr sect;
451     struct lndstr land;
452     coord allx;
453     coord ally;
454     int first = 1;
455     char mess[128];
456     int rel;
457
458     *minmobp = 9876.0;
459     *maxmobp = -9876.0;
460     *togetherp = 1;
461     for (qp = list->q_back; qp != list; qp = next) {
462         next = qp->q_back;
463         llp = (struct ulist *)qp;
464         getland(llp->unit.land.lnd_uid, &land);
465         if (land.lnd_own != actor) {
466             mpr(actor, "%s was disbanded at %s\n",
467                 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
468             emp_remque((struct emp_qelem *)llp);
469             free(llp);
470             continue;
471         }
472         if (land.lnd_ship >= 0) {
473             lnd_mess("is on a ship", llp);
474             continue;
475         }
476         if (land.lnd_land >= 0) {
477             lnd_mess("is on a unit", llp);
478             continue;
479         }
480         if (!getsect(land.lnd_x, land.lnd_y, &sect)) {
481             lnd_mess("was sucked into the sky by a strange looking spaceland", llp);    /* heh -KHS */
482             continue;
483         }
484         if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY) &&
485             !(lchr[(int)llp->unit.land.lnd_type].l_flags & L_TRAIN) &&
486             llp->unit.land.lnd_item[I_MILIT] == 0) {
487             lnd_mess("has no mil on it to guide it", llp);
488             continue;
489         }
490         rel = getrel(getnatp(sect.sct_own), player->cnum);
491         if (sect.sct_own != land.lnd_own && rel != ALLIED &&
492             !(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY) &&
493             sect.sct_own) {
494             sprintf(mess, "has been kidnapped by %s", cname(sect.sct_own));
495             lnd_mess(mess, llp);
496             continue;
497         }
498         if (first) {
499             allx = land.lnd_x;
500             ally = land.lnd_y;
501             first = 0;
502         }
503         if (land.lnd_x != allx || land.lnd_y != ally)
504             *togetherp = 0;
505         if (land.lnd_mobil + 1 < (int)llp->mobil) {
506             llp->mobil = land.lnd_mobil;
507         }
508         if (llp->mobil < *minmobp)
509             *minmobp = llp->mobil;
510         if (llp->mobil > *maxmobp)
511             *maxmobp = llp->mobil;
512         llp->unit.land = land;
513     }
514 }
515
516 void
517 lnd_sweep(struct emp_qelem *land_list, int verbose, int takemob,
518           natid actor)
519 {
520     struct emp_qelem *qp;
521     struct emp_qelem *next;
522     struct ulist *llp;
523     struct sctstr sect;
524     int mines, m, max, sshells, lshells;
525
526     for (qp = land_list->q_back; qp != land_list; qp = next) {
527         next = qp->q_back;
528         llp = (struct ulist *)qp;
529         if (!(((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)) {
530             if (verbose)
531                 mpr(actor, "%s is not an engineer!\n",
532                     prland(&llp->unit.land));
533             continue;
534         }
535         if (takemob && llp->mobil < 0.0) {
536             if (verbose)
537                 lnd_mess("is out of mobility", llp);
538             continue;
539         }
540         getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &sect);
541         if (sect.sct_oldown == llp->unit.land.lnd_own) {
542             if (verbose)
543                 mpr(actor,
544                     "%s is in a sector completely owned by you.  Don't bother digging up mines there!\n",
545                     prland(&llp->unit.land));
546             continue;
547         }
548         if (sect.sct_type == SCT_BSPAN) {
549             if (verbose)
550                 mpr(actor, "%s is on a bridge.  No mines there!\n",
551                     prland(&llp->unit.land));
552             continue;
553         }
554         if (takemob) {
555             llp->mobil -= lnd_pathcost(&llp->unit.land, 0.2);
556             llp->unit.land.lnd_mobil = (int)llp->mobil;
557             llp->unit.land.lnd_harden = 0;
558         }
559         putland(llp->unit.land.lnd_uid, &llp->unit.land);
560         if (!(mines = sect.sct_mines))
561             continue;
562         max = ((struct lchrstr *)llp->chrp)->l_item[I_SHELL];
563         lshells = llp->unit.land.lnd_item[I_SHELL];
564         sshells = sect.sct_item[I_SHELL];
565         for (m = 0; mines > 0 && m < max * 2; m++) {
566             if (chance(0.5 * ((struct lchrstr *)llp->chrp)->l_att)) {
567                 mpr(actor, "Sweep...\n");
568                 mines--;
569                 if (lshells < max)
570                     ++lshells;
571                 else if (sshells < ITEM_MAX)
572                     ++sshells;
573             }
574         }
575         sect.sct_mines = mines;
576         llp->unit.land.lnd_item[I_SHELL] = lshells;
577         sect.sct_item[I_SHELL] = sshells;
578         putland(llp->unit.land.lnd_uid, &llp->unit.land);
579         putsect(&sect);
580     }
581 }
582
583 static int
584 contains_engineer(struct emp_qelem *list)
585 {
586     struct emp_qelem *qp;
587     struct emp_qelem *next;
588     struct ulist *llp;
589
590     for (qp = list->q_back; qp != list; qp = next) {
591         next = qp->q_back;
592         llp = (struct ulist *)qp;
593         if (((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)
594             return 1;
595     }
596     return 0;
597 }
598
599 int
600 lnd_check_mines(struct emp_qelem *land_list)
601 {
602     struct emp_qelem *qp;
603     struct emp_qelem *next;
604     struct ulist *llp;
605     struct sctstr sect;
606     int stopping = 0;
607     int with_eng = contains_engineer(land_list);
608
609     for (qp = land_list->q_back; qp != land_list; qp = next) {
610         next = qp->q_back;
611         llp = (struct ulist *)qp;
612         getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &sect);
613         if (sect.sct_oldown == llp->unit.land.lnd_own)
614             continue;
615         if (sect.sct_type == SCT_BSPAN)
616             continue;
617         if (!sect.sct_mines)
618             continue;
619         if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
620             lnd_hit_mine(&llp->unit.land, ((struct lchrstr *)llp->chrp));
621             sect.sct_mines--;
622             putsect(&sect);
623             putland(llp->unit.land.lnd_uid, &llp->unit.land);
624             if (!llp->unit.land.lnd_own) {
625                 stopping = 1;
626                 emp_remque(qp);
627                 free(qp);
628             }
629         }
630     }
631     return stopping;
632 }
633
634 static void
635 lnd_mess(char *str, struct ulist *llp)
636 {
637     mpr(llp->unit.land.lnd_own, "%s %s & stays in %s\n",
638         prland(&llp->unit.land),
639         str, xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
640                   llp->unit.land.lnd_own));
641     if (llp->mobil < -127)
642         llp->mobil = -127;
643     llp->unit.land.lnd_mobil = llp->mobil;
644     putland(llp->unit.land.lnd_uid, &llp->unit.land);
645     emp_remque((struct emp_qelem *)llp);
646     free(llp);
647 }
648
649 static int
650 lnd_count(struct emp_qelem *list)
651 {
652     struct emp_qelem *qp;
653     struct emp_qelem *next;
654     int count = 0;
655
656     for (qp = list->q_back; qp != list; qp = next) {
657         next = qp->q_back;
658         ++count;
659     }
660     return count;
661 }
662
663 static int
664 lnd_damage(struct emp_qelem *list, int totdam)
665 {
666     struct emp_qelem *qp;
667     struct emp_qelem *next;
668     struct ulist *llp;
669     int dam;
670     int count;
671
672     if (!totdam || !(count = lnd_count(list)))
673         return 0;
674     dam = ldround((double)totdam / count, 1);
675     for (qp = list->q_back; qp != list; qp = next) {
676         next = qp->q_back;
677         llp = (struct ulist *)qp;
678         /* have to get it again because of collateral damage */
679         getland(llp->unit.land.lnd_uid, &llp->unit.land);
680         landdamage(&llp->unit.land, dam);
681         putland(llp->unit.land.lnd_uid, &llp->unit.land);
682         if (!llp->unit.land.lnd_own) {
683             emp_remque(qp);
684             free(qp);
685         }
686     }
687     return dam;
688 }
689
690 static int
691 lnd_easiest_target(struct emp_qelem *list)
692 {
693     struct emp_qelem *qp;
694     struct emp_qelem *next;
695     struct ulist *llp;
696     int hard;
697     int easiest = 9876;         /* things start great for victim */
698     int count = 0;
699
700     for (qp = list->q_back; qp != list; qp = next) {
701         next = qp->q_back;
702         llp = (struct ulist *)qp;
703         hard = lnd_hardtarget(&llp->unit.land);
704         if (hard < easiest)
705             easiest = hard;     /* things get worse for victim */
706         ++count;
707     }
708     return easiest - count;
709 }
710
711 static int
712 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
713                          natid victim)
714 {
715     int dam;
716     struct emp_qelem msl_list, *qp, *newqp;
717
718     msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
719
720     dam = msl_launch_mindam(&msl_list, newx, newy,
721                             lnd_easiest_target(list), EF_LAND,
722                             lnd_count(list) * 20, "troops", victim,
723                             MI_INTERDICT);
724     if (dam) {
725         mpr(victim, "missile interdiction mission does %d damage!\n", dam);
726         collateral_damage(newx, newy, dam);
727     }
728     qp = msl_list.q_forw;
729     while (qp != msl_list.q_forw) {
730         newqp = qp->q_forw;
731         emp_remque(qp);
732         free(qp);
733         qp = newqp;
734     }
735     return dam;
736 }
737
738 #if 0
739 /* Steve M. - commented out for now until abuse is decided upon */
740 /* risner: allow forts to interdict land units. */
741 static int
742 lnd_fort_interdiction(struct emp_qelem *list,
743                       coord newx, coord newy, natid victim)
744 {
745     struct nstr_sect ns;
746     struct sctstr fsect;
747     int trange, range;
748     double guneff;
749     int shell, gun;
750     int dam;
751     int totdam = 0;
752     int i;
753
754     snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
755     while (nxtsct(&ns, &fsect)) {
756         if (fsect.sct_own == 0)
757             continue;
758         if (fsect.sct_own == victim)
759             continue;
760         if (getrel(getnatp(fsect.sct_own), victim) >= NEUTRAL)
761             continue;
762         range = roundrange(fortrange(&fsect));
763         trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
764         if (trange > range)
765             continue;
766         dam = fort_fire(&fsect);
767         putsect(&fsect);
768         if (dam < 0)
769             continue;
770         totdam += dam;
771         mpr(victim, "Incoming fire does %d damage!\n", dam);
772         wu(0, fsect.sct_own,
773            "%s fires at %s land units in %s for %d!\n",
774            xyas(fsect.sct_x, fsect.sct_y,
775                 fsect.sct_own),
776            cname(victim), xyas(newx, newy, fsect.sct_own), dam);
777         nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
778     }
779     if (totdam > 0)
780         return lnd_damage(list, totdam);
781     return 0;
782 }
783 #endif
784
785 int
786 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
787 {
788     int stopping = 0;
789
790 #if 0
791     if (!opt_NO_FORT_FIRE)
792 /* Steve M. - commented out for now until abuse is decided upon */
793         stopping |= lnd_fort_interdiction(list, newx, newy, victim);
794 #endif
795
796     stopping |=
797         lnd_damage(list,
798                    unit_interdict(newx, newy, victim, "land units",
799                                   lnd_easiest_target(list), MI_INTERDICT));
800
801     stopping |=
802         lnd_damage(list,
803                    lnd_missile_interdiction(list, newx, newy, victim));
804     return stopping;
805 }
806
807 /* high value of hardtarget is harder to hit */
808 int
809 lnd_hardtarget(struct lndstr *lp)
810 {
811     struct sctstr sect;
812
813     getsect(lp->lnd_x, lp->lnd_y, &sect);
814     return (int)((lp->lnd_effic / 100.0) *
815                  (10 + dchr[sect.sct_type].d_dstr * 2 + lnd_spd(lp) / 2.0
816                   - lnd_vis(lp)));
817 }
818
819 static int
820 lnd_hit_mine(struct lndstr *lp, struct lchrstr *lcp)
821 {
822     int m;
823
824     mpr(lp->lnd_own, "Blammo! Landmines detected in %s! ",
825         xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
826
827     nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
828
829     m = MINE_LDAMAGE();
830     if (lcp->l_flags & L_ENGINEER)
831         m /= 2;
832
833     landdamage(lp, m);
834     return m;
835 }
836
837 double
838 lnd_pathcost(struct lndstr *lp, double pathcost)
839 {
840     double effspd;
841
842     effspd = lnd_spd(lp);
843     if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
844         effspd *= lp->lnd_effic * 0.01;
845
846     /*
847      * The return value must be PATHCOST times a factor that depends
848      * only on the land unit.  Anything else breaks path finding.  In
849      * particular, you can't add or enforce a minimum cost here.  Do
850      * it in sector_mcost().
851      */
852     return pathcost * 5.0 * speed_factor(effspd, lp->lnd_tech);
853 }
854
855 int
856 lnd_mobtype(struct lndstr *lp)
857 {
858     return (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
859         ? MOB_RAIL : MOB_MARCH;
860 }
861
862 double
863 lnd_mobcost(struct lndstr *lp, struct sctstr *sp)
864 {
865     return lnd_pathcost(lp, sector_mcost(sp, lnd_mobtype(lp)));
866 }
867
868 int
869 lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor,
870                    int together)
871 {
872     struct sctstr sect, osect;
873     struct emp_qelem *qp;
874     struct emp_qelem *qp2;
875     struct emp_qelem *next;
876     struct ulist *llp;
877     struct emp_qelem cur, done;
878     coord dx;
879     coord dy;
880     coord newx;
881     coord newy;
882     int stopping = 0;
883     int visible;
884     int stop;
885     char dp[80];
886     int rel;
887     int oldown;
888
889     if (dir <= DIR_STOP || dir >= DIR_VIEW) {
890         unit_put(list, actor);
891         return 1;
892     }
893     dx = diroff[dir][0];
894     dy = diroff[dir][1];
895     for (qp = list->q_back; qp != list; qp = next) {
896         next = qp->q_back;
897         llp = (struct ulist *)qp;
898         getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &osect);
899         oldown = osect.sct_own;
900         newx = xnorm(llp->unit.land.lnd_x + dx);
901         newy = ynorm(llp->unit.land.lnd_y + dy);
902         getsect(newx, newy, &sect);
903         rel = getrel(getnatp(sect.sct_own), player->cnum);
904         if ((sect.sct_own != actor && rel != ALLIED &&
905              !(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY) &&
906              sect.sct_own) || (sect.sct_type == SCT_WATER ||
907                                sect.sct_type == SCT_SANCT ||
908                                sect.sct_type == SCT_WASTE)) {
909             if (together) {
910                 pr("can't go to %s\n", xyas(newx, newy, actor));
911                 return 1;
912             } else {
913                 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
914                 lnd_mess(dp, llp);
915                 continue;
916             }
917         }
918         if ((!intrchr[INT_RAIL].in_enable || sect.sct_rail == 0)
919             && lnd_mobtype(&llp->unit.land) == MOB_RAIL) {
920             if (together) {
921                 pr("no rail system in %s\n", xyas(newx, newy, actor));
922                 return 1;
923             } else {
924                 sprintf(dp, "has no rail system in %s",
925                         xyas(newx, newy, actor));
926                 lnd_mess(dp, llp);
927                 continue;
928             }
929         }
930         /* Note we check would_abandon first because we don't want
931            to always have to do these checks */
932         if (would_abandon(&osect, I_CIVIL, 0, &llp->unit.land)) {
933             stop = 0;
934             if (!want_to_abandon(&osect, I_CIVIL, 0, &llp->unit.land)) {
935                 stop = 1;
936             }
937             /* now check stuff */
938             if (!check_sect_ok(&sect))
939                 return 1;
940             if (!check_sect_ok(&osect))
941                 return 1;
942             for (qp2 = list->q_back; qp2 != list; qp2 = qp2->q_back) {
943                 if (!check_land_ok(&((struct ulist *)qp2)->unit.land))
944                     return 1;
945             }
946             if (stop) {
947                 lnd_mess("stops", llp);
948                 continue;
949             }
950         }
951         if (llp->mobil <= 0.0) {
952             lnd_mess("is out of mobility", llp);
953             continue;
954         }
955         llp->unit.land.lnd_x = newx;
956         llp->unit.land.lnd_y = newy;
957         llp->mobil -= lnd_mobcost(&llp->unit.land, &sect);
958         llp->unit.land.lnd_mobil = (int)llp->mobil;
959         llp->unit.land.lnd_harden = 0;
960         putland(llp->unit.land.lnd_uid, &llp->unit.land);
961         putsect(&osect);
962         getsect(osect.sct_x, osect.sct_y, &osect);
963         if (osect.sct_own != oldown && oldown == player->cnum) {
964             /* It was your sector, now it's not.  Simple :) */
965             pr("You no longer own %s\n",
966                xyas(osect.sct_x, osect.sct_y, player->cnum));
967         }
968         if (rel != ALLIED && sect.sct_own != actor && sect.sct_own) {   /* must be a spy */
969             /* Always a 10% chance of getting caught. */
970             if (chance(LND_SPY_DETECT_CHANCE(llp->unit.land.lnd_effic))) {
971                 if (rel == NEUTRAL || rel == FRIENDLY) {
972                     wu(0, sect.sct_own,
973                        "%s unit spotted in %s\n", cname(player->cnum),
974                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
975                     setrel(sect.sct_own, llp->unit.land.lnd_own, HOSTILE);
976                 } else if (rel == HOSTILE || rel == AT_WAR ||
977                            rel == SITZKRIEG || rel == MOBILIZATION) {
978                     wu(0, sect.sct_own,
979                        "%s spy shot in %s\n", cname(player->cnum),
980                        xyas(sect.sct_x, sect.sct_y, sect.sct_own));
981                     pr("%s was shot and killed.\n", prland(&llp->unit.land));
982                     llp->unit.land.lnd_effic = 0;
983                     putland(llp->unit.land.lnd_uid, &llp->unit.land);
984                     lnd_delete(llp, 0);
985                 }
986             }
987         }
988     }
989     if (QEMPTY(list))
990         return stopping;
991     lnd_sweep(list, 0, 1, actor);
992     stopping |= lnd_check_mines(list);
993     if (QEMPTY(list))
994         return stopping;
995
996     /* interdict land units sector by sector */
997     emp_initque(&cur);
998     emp_initque(&done);
999     while (!QEMPTY(list)) {
1000         llp = (struct ulist *)list->q_back;
1001         newx = llp->unit.land.lnd_x;
1002         newy = llp->unit.land.lnd_y;
1003         /* move units in NEWX,NEWY to cur */
1004         visible = 0;
1005         for (qp = list->q_back; qp != list; qp = next) {
1006             next = qp->q_back;
1007             llp = (struct ulist *)qp;
1008             if (llp->unit.land.lnd_x == newx && llp->unit.land.lnd_y == newy) {
1009                 emp_remque(qp);
1010                 emp_insque(qp, &cur);
1011                 if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY))
1012                     visible = 1;
1013             }
1014         }
1015         /* interdict them */
1016         if (visible)
1017             stopping |= lnd_interdict(&cur, newx, newy, actor);
1018         /* move survivors to done */
1019         for (qp = cur.q_back; qp != &cur; qp = next) {
1020             next = qp->q_back;
1021             llp = (struct ulist *)qp;
1022             emp_remque(qp);
1023             emp_insque(qp, &done);
1024         }
1025     }
1026     /* assign surviving land units back to list */
1027     emp_insque(list, &done);
1028     emp_remque(&done);
1029
1030     return stopping;
1031 }
1032
1033 /*
1034  * find all artillery units belonging
1035  * to the attacker or defender that can fire.
1036  * Each arty unit adds +1%/damage point
1037  */
1038 int
1039 lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
1040 {
1041     struct nstr_item ni;
1042     struct lndstr land;
1043     int rel, rel2;
1044     int dam, dam2;
1045     int dist;
1046     int range;
1047
1048     dam = 0;
1049     snxtitem_all(&ni, EF_LAND);
1050     while (nxtitem(&ni, &land)) {
1051         if ((land.lnd_x == x) && (land.lnd_y == y))
1052             continue;
1053         rel = getrel(getnatp(land.lnd_own), attacker);
1054         rel2 = getrel(getnatp(land.lnd_own), victim);
1055         if ((land.lnd_own != attacker) &&
1056             ((rel != ALLIED) || (rel2 != AT_WAR)))
1057             continue;
1058
1059         /* are we in range? */
1060         dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1061
1062         range = roundrange(lnd_fire_range(&land));
1063         if (dist > range)
1064             continue;
1065
1066         dam2 = lnd_fire(&land);
1067         putland(land.lnd_uid, &land);
1068         if (dam2 < 0)
1069             continue;
1070
1071         if (defending)
1072             nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
1073         else
1074             nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1075         if (roll(100) < lnd_acc(&land))
1076             dam2 /= 2;
1077         dam += dam2;
1078         if (land.lnd_own != attacker)
1079             wu(0, land.lnd_own,
1080                "%s supported %s at %s\n",
1081                prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1082     }
1083     return dam;
1084 }
1085
1086 int
1087 lnd_can_attack(struct lndstr *lp)
1088 {
1089     struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1090
1091     if (lcp->l_flags & L_SUPPLY)
1092         return 0;
1093
1094     return 1;
1095 }
1096
1097 /*
1098  * Increase fortification value of LP.
1099  * Fortification costs mobility.  Use up to MOB mobility.
1100  * Return actual fortification increase.
1101  */
1102 int
1103 lnd_fortify(struct lndstr *lp, int mob)
1104 {
1105     int hard_amt;
1106     double mob_used, mult;
1107
1108     if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
1109         return 0;
1110
1111     mob_used = MIN(lp->lnd_mobil, mob);
1112     if (mob_used < 0)
1113         return 0;
1114
1115     mult = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own)
1116         ? 1.5 : 1.0;
1117
1118     hard_amt = (int)(mob_used * mult);
1119     if (lp->lnd_harden + hard_amt > land_mob_max) {
1120         hard_amt = land_mob_max - lp->lnd_harden;
1121         mob_used = ceil(hard_amt / mult);
1122     }
1123
1124     lp->lnd_mobil -= (int)mob_used;
1125     lp->lnd_harden += hard_amt;
1126     lp->lnd_harden = MIN(lp->lnd_harden, land_mob_max);
1127
1128     return hard_amt;
1129 }
1130
1131 /*
1132  * Is there a engineer unit at X,Y that can help nation CN?
1133  */
1134 static int
1135 has_helpful_engineer(coord x, coord y, natid cn)
1136 {
1137     struct nstr_item ni;
1138     struct lndstr land;
1139
1140     snxtitem_xy(&ni, EF_LAND, x, y);
1141     while (nxtitem(&ni, &land)) {
1142         if (land.lnd_own != cn && getrel(getnatp(land.lnd_own), cn) != ALLIED)
1143             continue;
1144         if (lchr[(int)land.lnd_type].l_flags & L_ENGINEER)
1145             return 1;
1146     }
1147
1148     return 0;
1149 }
1150
1151 /*
1152  * Set LP's tech to TLEV along with everything else that depends on it.
1153  */
1154 void
1155 lnd_set_tech(struct lndstr *lp, int tlev)
1156 {
1157     struct lchrstr *lcp = lchr + lp->lnd_type;
1158
1159     if (CANT_HAPPEN(tlev < lcp->l_tech))
1160         tlev = 0;
1161
1162     lp->lnd_tech = tlev;
1163 }