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