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