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