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
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.
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.
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
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.
28 * lndsub.c: Land unit subroutines
30 * Known contributors to this file:
32 * Steve McClure, 1998-2000
33 * Markus Armbruster, 2004-2006
49 #include "prototypes.h"
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);
59 attack_val(int combat_mode, struct lndstr *lp)
65 if (lp->lnd_effic < LAND_MINEFF) {
66 putland(lp->lnd_uid, lp);
70 lcp = &lchr[(int)lp->lnd_type];
72 /* Spies always count as 1 during assaults. If they are the only ones
73 in the assault, they get to sneak on anyway. */
75 if (lcp->l_flags & L_SPY && combat_mode == A_ASSAULT)
78 men = lp->lnd_item[I_MILIT];
79 value = men * lnd_att(lp) * lp->lnd_effic / 100.0;
81 switch (combat_mode) {
85 if (!(lcp->l_flags & L_MARINE))
86 return assault_penalty * value;
89 if (!(lcp->l_flags & L_MARINE))
90 return assault_penalty * men;
97 defense_val(struct lndstr *lp)
103 if (lp->lnd_effic < LAND_MINEFF) {
104 putland(lp->lnd_uid, lp);
108 lcp = &lchr[(int)lp->lnd_type];
110 men = lp->lnd_item[I_MILIT];
112 if ((lp->lnd_ship >= 0 || lp->lnd_land >= 0) &&
113 !(lcp->l_flags & L_MARINE))
116 value = men * lnd_def(lp) * lp->lnd_effic / 100.0;
117 value *= ((double)land_mob_max + lp->lnd_harden) / land_mob_max;
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))
128 lnd_print(struct ulist *llp, char *s)
130 if (llp->unit.land.lnd_own == player->cnum)
131 pr("%s %s\n", prland(&llp->unit.land), s);
133 wu(0, llp->unit.land.lnd_own, "%s %s\n", prland(&llp->unit.land), s);
137 lnd_delete(struct ulist *llp, char *s)
141 putland(llp->unit.land.lnd_uid, &llp->unit.land);
142 emp_remque((struct emp_qelem *)llp);
147 lnd_take_casualty(int combat_mode, struct ulist *llp, int cas)
148 /* attacking or assaulting or paratrooping? */
149 /* number of casualties to take */
162 int nowhere_to_go = 0;
164 double mobcost, bmcost;
168 taken = llp->unit.land.lnd_item[I_MILIT];
169 /* Spies always die */
170 if (((struct lchrstr *)llp->chrp)->l_flags & L_SPY) {
172 llp->unit.land.lnd_effic = 0;
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);
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 */
189 /* Ok, now, how many did we take off? (sould be the diff) */
190 taken = taken - llp->unit.land.lnd_item[I_MILIT];
193 if (llp->unit.land.lnd_effic >= llp->unit.land.lnd_retreat)
196 /* we're being boarded */
197 if (llp->unit.land.lnd_ship >= 0 && combat_mode == A_DEFEND)
200 /* we're being boarded */
201 if (llp->unit.land.lnd_land >= 0 && combat_mode == A_DEFEND)
204 /* Have to make a retreat check */
206 ret_chance = llp->unit.land.lnd_retreat - llp->unit.land.lnd_effic;
207 if (roll(100) < ret_chance) {
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)
214 else if (combat_mode == A_DEFEND) {
216 * defending unit.. find a place to send it
217 * strategy: look for the most-populated
218 * adjacent sector that is owned by the unit
219 * player->owner. Charge mob..
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, §);
227 if (sect.sct_own != llp->unit.land.lnd_own)
229 if (sect.sct_type == SCT_MOUNT)
231 mobcost = lnd_mobcost(&llp->unit.land, &rsect);
235 civs = sect.sct_item[I_CIVIL];
236 if (civs > biggest) {
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;
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);
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;
269 llp->unit.land.lnd_mobil -= (int)llp->mobil;
271 lnd_delete(llp, buf);
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!");
283 "has nowhere to retreat and takes extra losses!");
290 lnd_takemob(struct emp_qelem *list, double loss)
292 struct emp_qelem *qp, *next;
295 int mcost = ldround(combat_mob * loss, 1);
297 for (qp = list->q_forw; qp != list; qp = next) {
299 llp = (struct ulist *)qp;
302 use_supply(&llp->unit.land);
303 if (llp->unit.land.lnd_mission == MI_RESERVE)
304 new = llp->unit.land.lnd_mobil - mcost/2;
307 new = llp->unit.land.lnd_mobil - mcost;
310 llp->unit.land.lnd_mobil = (signed char)new;
315 lnd_submil(struct lndstr *lp, int num)
317 int new = lp->lnd_item[I_MILIT] - num;
318 lp->lnd_item[I_MILIT] = new < 0 ? 0 : new;
322 lnd_spyval(struct lndstr *lp)
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;
327 return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0);
331 intelligence_report(int destination, struct lndstr *lp, int spy,
334 int vis = lnd_vis(lp);
335 char buf1[80], buf2[80], buf3[80];
337 if (destination == 0)
340 if (lp->lnd_own == 0)
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));
350 sprintf(buf1, "%s %s", mess, prland(lp));
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));
358 sprintf(buf2, " (eff %d, mil %d",
359 roundintby(lp->lnd_effic, 5),
360 roundintby(lp->lnd_item[I_MILIT], 10));
362 if (chance((spy + vis) / 20.0)) {
364 t = lp->lnd_tech - 20 + roll(40);
366 if (destination == player->cnum)
367 pr(", tech %d)\n", t);
369 sprintf(buf3, ", tech %d)\n", t);
371 if (destination == player->cnum)
374 sprintf(buf3, ")\n");
377 if (destination == player->cnum)
384 if (destination != player->cnum) {
385 wu(0, destination, "%s%s%s", buf1, buf2, buf3);
389 /* Used by the spy command to count land units in a sector. If used
390 for anything else, you may want to reconsider, because this doesn't
391 always count spies. :) */
393 count_sect_units(struct sctstr *sp)
399 snxtitem_all(&ni, EF_LAND);
400 while (nxtitem(&ni, &land)) {
403 if (land.lnd_x != sp->sct_x || land.lnd_y != sp->sct_y)
405 /* Don't always see spies */
406 if (lchr[(int)land.lnd_type].l_flags & L_SPY) {
407 if (!(chance(LND_SPY_DETECT_CHANCE(land.lnd_effic))))
410 /* Got here, report it */
418 count_units(struct shpstr *sp)
424 if (sp->shp_effic < SHIP_MINEFF)
427 snxtitem_xy(&ni, EF_LAND, sp->shp_x, sp->shp_y);
428 while (nxtitem(&ni, &land)) {
429 if (land.lnd_own == 0)
431 if (land.lnd_ship == sp->shp_uid)
435 if (sp->shp_nland != nland) {
436 sp->shp_nland = nland;
437 putship(sp->shp_uid, sp);
442 lnd_count_units(struct lndstr *lp)
448 if (lp->lnd_effic < LAND_MINEFF)
451 snxtitem_xy(&ni, EF_LAND, lp->lnd_x, lp->lnd_y);
452 while (nxtitem(&ni, &land)) {
453 if (land.lnd_own == 0)
455 if (land.lnd_land == lp->lnd_uid)
459 if (lp->lnd_nland != nland) {
460 lp->lnd_nland = nland;
461 putland(lp->lnd_uid, lp);
466 lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
472 int mobtype = MOB_MOVE; /* indeterminate */
475 while (nxtitem(ni, &land)) {
479 if (ontradingblock(EF_LAND, &land)) {
480 pr("unit #%d inelligible - it's for sale.\n",
486 * The marching code gets confused when trains and non-trains
487 * march together. Disallow for now.
489 this_mot = lnd_mobtype(&land);
490 if (this_mot != mobtype) {
491 if (mobtype == MOB_MOVE)
493 else if (mobtype == MOB_MARCH) {
494 pr("%s is a train and can't march with the leader.\n",
498 pr("%s can't rail-march with the leading train.\n",
504 lcp = &lchr[(int)land.lnd_type];
505 land.lnd_mission = 0;
508 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
509 putland(land.lnd_uid, &land);
510 llp = malloc(sizeof(struct ulist));
511 llp->chrp = (struct empobj_chr *)lcp;
512 llp->unit.land = land;
513 llp->mobil = land.lnd_mobil;
514 emp_insque(&llp->queue, list);
518 /* This function assumes that the list was created by lnd_sel */
520 lnd_mar(struct emp_qelem *list, double *minmobp, double *maxmobp,
521 int *togetherp, natid actor)
523 struct emp_qelem *qp;
524 struct emp_qelem *next;
537 for (qp = list->q_back; qp != list; qp = next) {
539 llp = (struct ulist *)qp;
540 getland(llp->unit.land.lnd_uid, &land);
541 if (land.lnd_own != actor) {
542 mpr(actor, "%s was disbanded at %s\n",
543 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
544 emp_remque((struct emp_qelem *)llp);
548 if (land.lnd_ship >= 0) {
549 lnd_mess("is on a ship", llp);
552 if (land.lnd_land >= 0) {
553 lnd_mess("is on a unit", llp);
556 if (!getsect(land.lnd_x, land.lnd_y, §)) {
557 lnd_mess("was sucked into the sky by a strange looking spaceland", llp); /* heh -KHS */
560 if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY) &&
561 !(lchr[(int)llp->unit.land.lnd_type].l_flags & L_TRAIN) &&
562 llp->unit.land.lnd_item[I_MILIT] == 0) {
563 lnd_mess("has no mil on it to guide it", llp);
566 rel = getrel(getnatp(sect.sct_own), player->cnum);
567 if (sect.sct_own != land.lnd_own && rel != ALLIED &&
568 !(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY) &&
570 sprintf(mess, "has been kidnapped by %s", cname(sect.sct_own));
579 if (land.lnd_x != allx || land.lnd_y != ally)
581 if (land.lnd_mobil + 1 < (int)llp->mobil) {
582 llp->mobil = land.lnd_mobil;
584 if (llp->mobil < *minmobp)
585 *minmobp = llp->mobil;
586 if (llp->mobil > *maxmobp)
587 *maxmobp = llp->mobil;
588 llp->unit.land = land;
593 lnd_sweep(struct emp_qelem *land_list, int verbose, int takemob,
596 struct emp_qelem *qp;
597 struct emp_qelem *next;
600 int mines, m, max, sshells, lshells;
602 for (qp = land_list->q_back; qp != land_list; qp = next) {
604 llp = (struct ulist *)qp;
605 if (!(((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)) {
607 mpr(actor, "%s is not an engineer!\n",
608 prland(&llp->unit.land));
611 if (takemob && llp->mobil < 0.0) {
613 lnd_mess("is out of mobility", llp);
616 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
617 if (sect.sct_oldown == llp->unit.land.lnd_own) {
620 "%s is in a sector completely owned by you. Don't bother digging up mines there!\n",
621 prland(&llp->unit.land));
624 if (sect.sct_type == SCT_BSPAN) {
626 mpr(actor, "%s is on a bridge. No mines there!\n",
627 prland(&llp->unit.land));
631 llp->mobil -= lnd_pathcost(&llp->unit.land, 0.2);
632 llp->unit.land.lnd_mobil = (int)llp->mobil;
633 llp->unit.land.lnd_harden = 0;
635 putland(llp->unit.land.lnd_uid, &llp->unit.land);
636 if (!(mines = sect.sct_mines))
638 max = ((struct lchrstr *)llp->chrp)->l_item[I_SHELL];
639 lshells = llp->unit.land.lnd_item[I_SHELL];
640 sshells = sect.sct_item[I_SHELL];
641 for (m = 0; mines > 0 && m < max * 2; m++) {
642 if (chance(0.5 * ((struct lchrstr *)llp->chrp)->l_att)) {
643 mpr(actor, "Sweep...\n");
647 else if (sshells < ITEM_MAX)
651 sect.sct_mines = mines;
652 llp->unit.land.lnd_item[I_SHELL] = lshells;
653 sect.sct_item[I_SHELL] = sshells;
654 putland(llp->unit.land.lnd_uid, &llp->unit.land);
660 contains_engineer(struct emp_qelem *list)
662 struct emp_qelem *qp;
663 struct emp_qelem *next;
666 for (qp = list->q_back; qp != list; qp = next) {
668 llp = (struct ulist *)qp;
669 if (((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)
676 lnd_check_mines(struct emp_qelem *land_list)
678 struct emp_qelem *qp;
679 struct emp_qelem *next;
683 int with_eng = contains_engineer(land_list);
685 for (qp = land_list->q_back; qp != land_list; qp = next) {
687 llp = (struct ulist *)qp;
688 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
689 if (sect.sct_oldown == llp->unit.land.lnd_own)
691 if (sect.sct_type == SCT_BSPAN)
695 if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
696 lnd_hit_mine(&llp->unit.land, ((struct lchrstr *)llp->chrp));
699 putland(llp->unit.land.lnd_uid, &llp->unit.land);
700 if (!llp->unit.land.lnd_own) {
711 lnd_mess(char *str, struct ulist *llp)
713 mpr(llp->unit.land.lnd_own, "%s %s & stays in %s\n",
714 prland(&llp->unit.land),
715 str, xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
716 llp->unit.land.lnd_own));
717 if (llp->mobil < -127)
719 llp->unit.land.lnd_mobil = llp->mobil;
720 putland(llp->unit.land.lnd_uid, &llp->unit.land);
721 emp_remque((struct emp_qelem *)llp);
726 lnd_count(struct emp_qelem *list)
728 struct emp_qelem *qp;
729 struct emp_qelem *next;
732 for (qp = list->q_back; qp != list; qp = next) {
740 lnd_damage(struct emp_qelem *list, int totdam)
742 struct emp_qelem *qp;
743 struct emp_qelem *next;
748 if (!totdam || !(count = lnd_count(list)))
750 dam = ldround((double)totdam / count, 1);
751 for (qp = list->q_back; qp != list; qp = next) {
753 llp = (struct ulist *)qp;
754 /* have to get it again because of collateral damage */
755 getland(llp->unit.land.lnd_uid, &llp->unit.land);
756 landdamage(&llp->unit.land, dam);
757 putland(llp->unit.land.lnd_uid, &llp->unit.land);
758 if (!llp->unit.land.lnd_own) {
767 lnd_easiest_target(struct emp_qelem *list)
769 struct emp_qelem *qp;
770 struct emp_qelem *next;
773 int easiest = 9876; /* things start great for victim */
776 for (qp = list->q_back; qp != list; qp = next) {
778 llp = (struct ulist *)qp;
779 hard = lnd_hardtarget(&llp->unit.land);
781 easiest = hard; /* things get worse for victim */
784 return easiest - count;
788 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
792 struct emp_qelem msl_list, *qp, *newqp;
794 msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
796 dam = msl_launch_mindam(&msl_list, newx, newy,
797 lnd_easiest_target(list), EF_LAND,
798 lnd_count(list) * 20, "troops", victim,
801 mpr(victim, "missile interdiction mission does %d damage!\n", dam);
802 collateral_damage(newx, newy, dam, 0);
804 qp = msl_list.q_forw;
805 while (qp != msl_list.q_forw) {
815 /* Steve M. - commented out for now until abuse is decided upon */
816 /* risner: allow forts to interdict land units. */
818 lnd_fort_interdiction(struct emp_qelem *list,
819 coord newx, coord newy, natid victim)
830 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
831 while (nxtsct(&ns, &fsect)) {
832 if (fsect.sct_own == 0)
834 if (fsect.sct_own == victim)
836 if (getrel(getnatp(fsect.sct_own), victim) >= NEUTRAL)
838 range = roundrange(fortrange(&fsect));
839 trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
842 dam = fort_fire(&fsect);
847 mpr(victim, "Incoming fire does %d damage!\n", dam);
849 "%s fires at %s land units in %s for %d!\n",
850 xyas(fsect.sct_x, fsect.sct_y,
852 cname(victim), xyas(newx, newy, fsect.sct_own), dam);
853 nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
856 return lnd_damage(list, totdam);
862 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
867 if (!opt_NO_FORT_FIRE)
868 /* Steve M. - commented out for now until abuse is decided upon */
869 stopping |= lnd_fort_interdiction(list, newx, newy, victim);
874 unit_interdict(newx, newy, victim, "land units",
875 lnd_easiest_target(list), MI_INTERDICT));
879 lnd_missile_interdiction(list, newx, newy, victim));
883 /* high value of hardtarget is harder to hit */
885 lnd_hardtarget(struct lndstr *lp)
889 getsect(lp->lnd_x, lp->lnd_y, §);
890 return (int)((lp->lnd_effic / 100.0) *
891 (10 + dchr[sect.sct_type].d_dstr * 2 + lnd_spd(lp) / 2.0
896 lnd_hit_mine(struct lndstr *lp, struct lchrstr *lcp)
900 mpr(lp->lnd_own, "Blammo! Landmines detected in %s! ",
901 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
903 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
906 if (lcp->l_flags & L_ENGINEER)
914 lnd_pathcost(struct lndstr *lp, double pathcost)
918 effspd = lnd_spd(lp);
919 if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
920 effspd *= lp->lnd_effic * 0.01;
923 * The return value must be PATHCOST times a factor that depends
924 * only on the land unit. Anything else breaks path finding. In
925 * particular, you can't add or enforce a minimum cost here. Do
926 * it in sector_mcost().
928 return pathcost * 5.0 * speed_factor(effspd, lp->lnd_tech);
932 lnd_mobtype(struct lndstr *lp)
934 return (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
935 ? MOB_RAIL : MOB_MARCH;
939 lnd_mobcost(struct lndstr *lp, struct sctstr *sp)
941 return lnd_pathcost(lp, sector_mcost(sp, lnd_mobtype(lp)));
945 lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor,
948 struct sctstr sect, osect;
949 struct emp_qelem *qp;
950 struct emp_qelem *qp2;
951 struct emp_qelem *next;
953 struct emp_qelem cur, done;
965 if (dir <= DIR_STOP || dir >= DIR_VIEW) {
966 unit_put(list, actor);
971 for (qp = list->q_back; qp != list; qp = next) {
973 llp = (struct ulist *)qp;
974 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &osect);
975 oldown = osect.sct_own;
976 newx = xnorm(llp->unit.land.lnd_x + dx);
977 newy = ynorm(llp->unit.land.lnd_y + dy);
978 getsect(newx, newy, §);
979 rel = getrel(getnatp(sect.sct_own), player->cnum);
980 if ((sect.sct_own != actor && rel != ALLIED &&
981 !(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY) &&
982 sect.sct_own) || (sect.sct_type == SCT_WATER ||
983 sect.sct_type == SCT_SANCT ||
984 sect.sct_type == SCT_WASTE)) {
986 pr("can't go to %s\n", xyas(newx, newy, actor));
989 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
994 if ((!intrchr[INT_RAIL].in_enable || sect.sct_rail == 0)
995 && lnd_mobtype(&llp->unit.land) == MOB_RAIL) {
997 pr("no rail system in %s\n", xyas(newx, newy, actor));
1000 sprintf(dp, "has no rail system in %s",
1001 xyas(newx, newy, actor));
1006 /* Note we check would_abandon first because we don't want
1007 to always have to do these checks */
1008 if (would_abandon(&osect, I_CIVIL, 0, &llp->unit.land)) {
1010 if (!want_to_abandon(&osect, I_CIVIL, 0, &llp->unit.land)) {
1013 /* now check stuff */
1014 if (!check_sect_ok(§))
1016 if (!check_sect_ok(&osect))
1018 for (qp2 = list->q_back; qp2 != list; qp2 = qp2->q_back) {
1019 if (!check_land_ok(&((struct ulist *)qp2)->unit.land))
1023 lnd_mess("stops", llp);
1027 if (llp->mobil <= 0.0) {
1028 lnd_mess("is out of mobility", llp);
1031 llp->unit.land.lnd_x = newx;
1032 llp->unit.land.lnd_y = newy;
1033 llp->mobil -= lnd_mobcost(&llp->unit.land, §);
1034 llp->unit.land.lnd_mobil = (int)llp->mobil;
1035 llp->unit.land.lnd_harden = 0;
1036 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1038 getsect(osect.sct_x, osect.sct_y, &osect);
1039 if (osect.sct_own != oldown && oldown == player->cnum) {
1040 /* It was your sector, now it's not. Simple :) */
1041 pr("You no longer own %s\n",
1042 xyas(osect.sct_x, osect.sct_y, player->cnum));
1044 if (rel != ALLIED && sect.sct_own != actor && sect.sct_own) { /* must be a spy */
1045 /* Always a 10% chance of getting caught. */
1046 if (chance(LND_SPY_DETECT_CHANCE(llp->unit.land.lnd_effic))) {
1047 if (rel == NEUTRAL || rel == FRIENDLY) {
1049 "%s unit spotted in %s\n", cname(player->cnum),
1050 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1051 setrel(sect.sct_own, llp->unit.land.lnd_own, HOSTILE);
1052 } else if (rel == HOSTILE || rel == AT_WAR ||
1053 rel == SITZKRIEG || rel == MOBILIZATION) {
1055 "%s spy shot in %s\n", cname(player->cnum),
1056 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1057 pr("%s was shot and killed.\n", prland(&llp->unit.land));
1058 llp->unit.land.lnd_effic = 0;
1059 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1067 lnd_sweep(list, 0, 1, actor);
1068 stopping |= lnd_check_mines(list);
1072 /* interdict land units sector by sector */
1075 while (!QEMPTY(list)) {
1076 llp = (struct ulist *)list->q_back;
1077 newx = llp->unit.land.lnd_x;
1078 newy = llp->unit.land.lnd_y;
1079 /* move units in NEWX,NEWY to cur */
1081 for (qp = list->q_back; qp != list; qp = next) {
1083 llp = (struct ulist *)qp;
1084 if (llp->unit.land.lnd_x == newx && llp->unit.land.lnd_y == newy) {
1086 emp_insque(qp, &cur);
1087 if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY))
1091 /* interdict them */
1093 stopping |= lnd_interdict(&cur, newx, newy, actor);
1094 /* move survivors to done */
1095 for (qp = cur.q_back; qp != &cur; qp = next) {
1097 llp = (struct ulist *)qp;
1099 emp_insque(qp, &done);
1102 /* assign surviving land units back to list */
1103 emp_insque(list, &done);
1110 * find all artillery units belonging
1111 * to the attacker or defender that can fire.
1112 * Each arty unit adds +1%/damage point
1115 lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
1117 struct nstr_item ni;
1125 snxtitem_all(&ni, EF_LAND);
1126 while (nxtitem(&ni, &land)) {
1127 if ((land.lnd_x == x) && (land.lnd_y == y))
1129 rel = getrel(getnatp(land.lnd_own), attacker);
1130 rel2 = getrel(getnatp(land.lnd_own), victim);
1131 if ((land.lnd_own != attacker) &&
1132 ((rel != ALLIED) || (rel2 != AT_WAR)))
1135 /* are we in range? */
1136 dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1138 range = roundrange(lnd_fire_range(&land));
1142 dam2 = lnd_fire(&land);
1143 putland(land.lnd_uid, &land);
1148 nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
1150 nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1151 if (roll(100) < lnd_acc(&land))
1154 if (land.lnd_own != attacker)
1156 "%s supported %s at %s\n",
1157 prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1163 lnd_can_attack(struct lndstr *lp)
1165 struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1167 if (lcp->l_flags & L_SUPPLY)
1174 * Increase fortification value of LP.
1175 * Fortification costs mobility. Use up to MOB mobility.
1176 * Return actual fortification increase.
1179 lnd_fortify(struct lndstr *lp, int mob)
1182 double mob_used, mult;
1184 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
1187 mob_used = MIN(lp->lnd_mobil, mob);
1191 mult = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own)
1194 hard_amt = (int)(mob_used * mult);
1195 if (lp->lnd_harden + hard_amt > land_mob_max) {
1196 hard_amt = land_mob_max - lp->lnd_harden;
1197 mob_used = ceil(hard_amt / mult);
1200 lp->lnd_mobil -= (int)mob_used;
1201 lp->lnd_harden += hard_amt;
1202 lp->lnd_harden = MIN(lp->lnd_harden, land_mob_max);
1208 * Is there a engineer unit at X,Y that can help nation CN?
1211 has_helpful_engineer(coord x, coord y, natid cn)
1213 struct nstr_item ni;
1216 snxtitem_xy(&ni, EF_LAND, x, y);
1217 while (nxtitem(&ni, &land)) {
1218 if (land.lnd_own != cn && getrel(getnatp(land.lnd_own), cn) != ALLIED)
1220 if (lchr[(int)land.lnd_type].l_flags & L_ENGINEER)
1228 * Set LP's tech to TLEV along with everything else that depends on it.
1231 lnd_set_tech(struct lndstr *lp, int tlev)
1233 struct lchrstr *lcp = lchr + lp->lnd_type;
1235 if (CANT_HAPPEN(tlev < lcp->l_tech))
1238 lp->lnd_tech = tlev;