2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * lndsub.c: Land unit subroutines
29 * Known contributors to this file:
31 * Steve McClure, 1998-2000
32 * Markus Armbruster, 2004-2014
51 #include "prototypes.h"
55 static int lnd_check_one_mines(struct ulist *, int);
56 static void lnd_stays(natid, char *, struct ulist *);
57 static int lnd_hit_mine(struct lndstr *);
58 static int has_helpful_engineer(coord, coord, natid);
61 attack_val(int combat_mode, struct lndstr *lp)
67 if (lp->lnd_effic < LAND_MINEFF) {
68 putland(lp->lnd_uid, lp);
72 lcp = &lchr[(int)lp->lnd_type];
74 /* Spies always count as 1 during assaults. If they are the only ones
75 in the assault, they get to sneak on anyway. */
77 if (lcp->l_flags & L_SPY && combat_mode == A_ASSAULT)
80 men = lp->lnd_item[I_MILIT];
81 value = men * lnd_att(lp) * lp->lnd_effic / 100.0;
83 switch (combat_mode) {
87 if (!(lcp->l_flags & L_MARINE))
88 return assault_penalty * value;
91 if (!(lcp->l_flags & L_MARINE))
92 return assault_penalty * men;
99 defense_val(struct lndstr *lp)
105 if (lp->lnd_effic < LAND_MINEFF) {
106 putland(lp->lnd_uid, lp);
110 lcp = &lchr[(int)lp->lnd_type];
112 men = lp->lnd_item[I_MILIT];
114 if ((lp->lnd_ship >= 0 || lp->lnd_land >= 0) &&
115 !(lcp->l_flags & L_MARINE))
118 value = men * lnd_def(lp) * lp->lnd_effic / 100.0;
119 value *= ((double)land_mob_max + lp->lnd_harden) / land_mob_max;
121 /* If there are military on the unit, you get at least a 1
122 man defensive unit, except for spies */
123 if (value < 1.0 && men > 0 && !(lcp->l_flags & L_SPY))
130 lnd_reaction_range(struct lndstr *lp)
134 getsect(lp->lnd_x, lp->lnd_y, §);
135 if (sect.sct_type == SCT_HEADQ && sect.sct_effic >= 60)
136 return lchr[lp->lnd_type].l_rad + 1;
137 return lchr[lp->lnd_type].l_rad;
141 lnd_print(natid actor, struct ulist *llp, char *s)
143 if (actor == player->cnum)
144 pr("%s %s\n", prland(&llp->unit.land), s);
146 wu(0, actor, "%s %s\n", prland(&llp->unit.land), s);
150 lnd_take_casualty(int combat_mode, struct ulist *llp, int cas)
151 /* attacking or assaulting or paratrooping? */
152 /* number of casualties to take */
164 int nowhere_to_go = 0;
165 double mobcost, bmcost;
169 taken = llp->unit.land.lnd_item[I_MILIT];
170 /* Spies always die */
171 if (lchr[llp->unit.land.lnd_type].l_flags & L_SPY)
172 llp->unit.land.lnd_effic = 0;
174 eff_eq = ldround(cas * 100.0 /
175 lchr[llp->unit.land.lnd_type].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_print(llp->unit.land.lnd_own, llp, buf);
187 /* Since we killed the unit, we killed all the mil on it */
190 /* Ok, now, how many did we take off? (sould be the diff) */
191 taken = taken - llp->unit.land.lnd_item[I_MILIT];
194 if (llp->unit.land.lnd_effic >= llp->unit.land.lnd_retreat)
197 /* we're being boarded */
198 if (llp->unit.land.lnd_ship >= 0 && combat_mode == A_DEFEND)
201 /* we're being boarded */
202 if (llp->unit.land.lnd_land >= 0 && combat_mode == A_DEFEND)
205 /* Have to make a retreat check */
207 ret_chance = llp->unit.land.lnd_retreat - llp->unit.land.lnd_effic;
208 if (pct_chance(ret_chance)) {
210 lnd_print(llp->unit.land.lnd_own, llp, "fails morale check!");
211 llp->unit.land.lnd_mission = 0;
212 llp->unit.land.lnd_harden = 0;
213 if (llp->unit.land.lnd_ship >= 0 || llp->unit.land.lnd_land >= 0)
215 else if (combat_mode == A_DEFEND) {
217 * defending unit.. find a place to send it
218 * strategy: look for the most-populated
219 * adjacent sector that is owned by the unit
220 * 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, §);
234 civs = sect.sct_item[I_CIVIL];
235 if (civs > biggest) {
245 /* retreat to bx,by */
246 llp->unit.land.lnd_x = bx;
247 llp->unit.land.lnd_y = by;
248 /* FIXME landmines */
249 mob = llp->unit.land.lnd_mobil - (int)bmcost;
252 orig = llp->unit.land.lnd_mobil;
253 llp->unit.land.lnd_mobil = (signed char)mob;
254 if (llp->unit.land.lnd_mobil > orig)
255 llp->unit.land.lnd_mobil = -127;
256 sprintf(buf, "retreats at %d%% efficiency to %s!",
257 llp->unit.land.lnd_effic,
258 xyas(bx, by, llp->unit.land.lnd_own));
259 lnd_print(llp->unit.land.lnd_own, llp, buf);
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;
268 llp->unit.land.lnd_mobil -= (int)llp->mobil;
270 lnd_print(llp->unit.land.lnd_own, llp, buf);
275 /* nowhere to go.. take more casualties */
276 llp->unit.land.lnd_effic -= 10;
277 lnd_submil(&llp->unit.land,
278 lchr[llp->unit.land.lnd_type].l_item[I_MILIT] / 10);
279 if (llp->unit.land.lnd_effic < LAND_MINEFF) {
280 lnd_print(llp->unit.land.lnd_own, llp,
281 "has nowhere to retreat, and dies!");
284 lnd_print(llp->unit.land.lnd_own, llp,
285 "has nowhere to retreat and takes extra losses!");
292 lnd_takemob(struct emp_qelem *list, double loss)
294 struct emp_qelem *qp, *next;
297 int mcost = ldround(combat_mob * loss, 1);
299 for (qp = list->q_forw; qp != list; qp = next) {
301 llp = (struct ulist *)qp;
304 use_supply(&llp->unit.land);
306 new = llp->unit.land.lnd_mobil - mcost;
309 llp->unit.land.lnd_mobil = (signed char)new;
314 lnd_submil(struct lndstr *lp, int num)
316 int new = lp->lnd_item[I_MILIT] - num;
317 lp->lnd_item[I_MILIT] = new < 0 ? 0 : new;
321 lnd_spyval(struct lndstr *lp)
323 if (lchr[(int)lp->lnd_type].l_flags & L_RECON)
324 return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0) + 2;
326 return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0);
330 intelligence_report(int destination, struct lndstr *lp, int spy,
333 int vis = lnd_vis(lp);
334 char buf1[80], buf2[80], buf3[80];
336 if (destination == 0)
339 if (lp->lnd_own == 0)
342 memset(buf1, 0, sizeof(buf1));
343 memset(buf2, 0, sizeof(buf2));
344 memset(buf3, 0, sizeof(buf3));
345 if (chance((spy + vis) / 10.0)) {
346 if (destination == player->cnum)
347 pr("%s %s", mess, prland(lp));
349 sprintf(buf1, "%s %s", mess, prland(lp));
351 if (chance((spy + vis) / 20.0)) {
352 if (destination == player->cnum)
353 pr(" (eff %d, mil %d",
354 roundintby(lp->lnd_effic, 5),
355 roundintby(lp->lnd_item[I_MILIT], 10));
357 sprintf(buf2, " (eff %d, mil %d",
358 roundintby(lp->lnd_effic, 5),
359 roundintby(lp->lnd_item[I_MILIT], 10));
361 if (chance((spy + vis) / 20.0)) {
363 t = lp->lnd_tech - 20 + roll(40);
365 if (destination == player->cnum)
366 pr(", tech %d)\n", t);
368 sprintf(buf3, ", tech %d)\n", t);
370 if (destination == player->cnum)
373 sprintf(buf3, ")\n");
376 if (destination == player->cnum)
383 if (destination != player->cnum) {
384 wu(0, destination, "%s%s%s", buf1, buf2, buf3);
389 lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
393 int mobtype = MOB_MOVE; /* indeterminate */
396 while (nxtitem(ni, &land)) {
398 * It would be nice to let deities march foreign land units,
399 * but much of the code assumes that only the land unit's
400 * owner can march it.
402 if (!land.lnd_own || land.lnd_own != player->cnum)
405 if (ontradingblock(EF_LAND, &land)) {
406 pr("unit #%d inelligible - it's for sale.\n",
412 * The marching code gets confused when trains and non-trains
413 * march together. Disallow for now.
415 this_mot = lnd_mobtype(&land);
416 if (this_mot != mobtype) {
417 if (mobtype == MOB_MOVE)
419 else if (mobtype == MOB_MARCH) {
420 pr("%s is a train and can't march with the leader.\n",
424 pr("%s can't rail-march with the leading train.\n",
430 land.lnd_mission = 0;
433 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
434 putland(land.lnd_uid, &land);
435 lnd_insque(&land, list);
441 * Return the new list link.
444 lnd_insque(struct lndstr *lp, struct emp_qelem *list)
446 struct ulist *mlp = malloc(sizeof(struct ulist));
448 mlp->unit.land = *lp;
449 mlp->mobil = lp->lnd_mobil;
450 emp_insque(&mlp->queue, list);
454 /* This function assumes that the list was created by lnd_sel */
456 lnd_mar(struct emp_qelem *list, double *minmobp, double *maxmobp,
459 struct emp_qelem *qp;
460 struct emp_qelem *next;
462 struct lndstr *lp, *ldr = NULL;
468 for (qp = list->q_back; qp != list; qp = next) {
470 llp = (struct ulist *)qp;
471 lp = &llp->unit.land;
472 getland(lp->lnd_uid, lp);
473 if (lp->lnd_own != actor) {
474 mpr(actor, "%s was disbanded at %s\n",
475 prland(lp), xyas(lp->lnd_x, lp->lnd_y, actor));
476 emp_remque(&llp->queue);
480 if (lp->lnd_ship >= 0) {
481 lnd_stays(actor, "is on a ship", llp);
484 if (lp->lnd_land >= 0) {
485 lnd_stays(actor, "is on a unit", llp);
488 if (!getsect(lp->lnd_x, lp->lnd_y, §)) {
489 lnd_stays(actor, "was sucked into the sky by a strange looking spaceland", llp); /* heh -KHS */
492 if (!(lchr[lp->lnd_type].l_flags & L_SPY) &&
493 !(lchr[lp->lnd_type].l_flags & L_TRAIN) &&
494 lp->lnd_item[I_MILIT] == 0) {
495 lnd_stays(actor, "has no mil on it to guide it", llp);
498 switch (lnd_check_mar(lp, §)) {
501 case LND_STUCK_NO_RAIL:
502 lnd_stays(actor, "is stuck off the rail system", llp);
507 case LND_STUCK_IMPASSABLE:
508 lnd_stays(actor, "is stuck", llp);
511 if (relations_with(sect.sct_own, actor) != ALLIED &&
512 !(lchr[lp->lnd_type].l_flags & L_SPY) &&
514 sprintf(mess, "has been kidnapped by %s", cname(sect.sct_own));
515 lnd_stays(actor, mess, llp);
520 else if (lp->lnd_x != ldr->lnd_x || lp->lnd_y != ldr->lnd_y) {
521 lnd_stays(actor, "is not with the leader", llp);
524 if (lp->lnd_mobil + 1 < (int)llp->mobil) {
525 llp->mobil = lp->lnd_mobil;
527 if (llp->mobil < *minmobp)
528 *minmobp = llp->mobil;
529 if (llp->mobil > *maxmobp)
530 *maxmobp = llp->mobil;
535 lnd_mar_put_one(struct ulist *llp)
537 if (llp->mobil < -127)
539 llp->unit.land.lnd_mobil = llp->mobil;
544 lnd_mar_put(struct emp_qelem *list, natid actor)
546 struct emp_qelem *qp, *next;
550 for (qp = list->q_back; qp != list; qp = next) {
552 llp = (struct ulist *)qp;
553 lp = &llp->unit.land;
554 mpr(actor, "%s stopped at %s\n",
555 prland(lp), xyas(lp->lnd_x, lp->lnd_y, actor));
556 lnd_mar_put_one(llp);
561 lnd_put(struct emp_qelem *list)
563 struct emp_qelem *qp, *next;
565 for (qp = list->q_back; qp != list; qp = next) {
567 lnd_put_one((struct ulist *)qp);
572 lnd_put_one(struct ulist *llp)
574 putland(llp->unit.land.lnd_uid, &llp->unit.land);
575 emp_remque(&llp->queue);
580 * Sweep landmines with engineers in LAND_LIST for ACTOR.
581 * If EXPLICIT is non-zero, this is for an explicit sweep command from
582 * a player. Else it's an automatic "on the move" sweep.
583 * If TAKEMOB is non-zero, require and charge mobility.
586 lnd_sweep(struct emp_qelem *land_list, int explicit, int takemob,
589 struct emp_qelem *qp;
590 struct emp_qelem *next;
593 int mines, m, max, sshells, lshells;
596 for (qp = land_list->q_back; qp != land_list; qp = next) {
598 llp = (struct ulist *)qp;
599 if (!(lchr[llp->unit.land.lnd_type].l_flags & L_ENGINEER)) {
601 mpr(actor, "%s is not an engineer!\n",
602 prland(&llp->unit.land));
605 if (takemob && llp->mobil <= 0.0) {
607 mpr(actor, "%s is out of mobility!\n",
608 prland(&llp->unit.land));
611 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
612 if (!explicit && relations_with(sect.sct_oldown, actor) == ALLIED)
614 if (SCT_MINES_ARE_SEAMINES(§)) {
616 mpr(actor, "%s is in a %s sector. No landmines there!\n",
617 prland(&llp->unit.land), dchr[sect.sct_type].d_name);
621 llp->mobil -= lnd_pathcost(&llp->unit.land, 0.2);
622 llp->unit.land.lnd_mobil = (int)llp->mobil;
623 llp->unit.land.lnd_harden = 0;
625 putland(llp->unit.land.lnd_uid, &llp->unit.land);
626 if (!(mines = sect.sct_mines))
628 max = lchr[llp->unit.land.lnd_type].l_item[I_SHELL];
629 lshells = llp->unit.land.lnd_item[I_SHELL];
630 sshells = sect.sct_item[I_SHELL];
631 for (m = 0; mines > 0 && m < max * 2; m++) {
632 if (chance(0.5 * lchr[llp->unit.land.lnd_type].l_att)) {
633 mpr(actor, "Sweep...\n");
637 else if (sshells < ITEM_MAX)
641 sect.sct_mines = mines;
642 llp->unit.land.lnd_item[I_SHELL] = lshells;
643 sect.sct_item[I_SHELL] = sshells;
644 putland(llp->unit.land.lnd_uid, &llp->unit.land);
646 if (lnd_check_one_mines(llp, 1)) {
656 contains_engineer(struct emp_qelem *list)
658 struct emp_qelem *qp;
659 struct emp_qelem *next;
662 for (qp = list->q_back; qp != list; qp = next) {
664 llp = (struct ulist *)qp;
665 if (lchr[llp->unit.land.lnd_type].l_flags & L_ENGINEER)
672 lnd_check_one_mines(struct ulist *llp, int with_eng)
676 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
677 if (SCT_LANDMINES(§) == 0)
679 if (relations_with(sect.sct_oldown, llp->unit.land.lnd_own) == ALLIED)
681 if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
682 lnd_hit_mine(&llp->unit.land);
685 putland(llp->unit.land.lnd_uid, &llp->unit.land);
686 if (!llp->unit.land.lnd_own)
693 lnd_check_mines(struct emp_qelem *land_list)
695 struct emp_qelem *qp;
696 struct emp_qelem *next;
699 int with_eng = contains_engineer(land_list);
701 for (qp = land_list->q_back; qp != land_list; qp = next) {
703 llp = (struct ulist *)qp;
704 if (lnd_check_one_mines(llp, with_eng)) {
714 lnd_stays(natid actor, char *str, struct ulist *llp)
716 mpr(actor, "%s %s & stays in %s\n",
717 prland(&llp->unit.land), str,
718 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y, actor));
719 lnd_mar_put_one(llp);
722 /* Return whether and why SP would be stuck in SECTP. */
724 lnd_check_mar(struct lndstr *lp, struct sctstr *sectp)
726 if (dchr[sectp->sct_type].d_mob0 < 0)
727 return LND_STUCK_IMPASSABLE;
728 if (lnd_mobtype(lp) == MOB_RAIL && !SCT_HAS_RAIL(sectp))
729 return LND_STUCK_NO_RAIL;
730 return LND_STUCK_NOT;
734 lnd_damage(struct emp_qelem *list, int totdam)
736 struct emp_qelem *qp;
737 struct emp_qelem *next;
742 if (!totdam || !(count = emp_quelen(list)))
744 dam = ldround((double)totdam / count, 1);
745 for (qp = list->q_back; qp != list; qp = next) {
747 llp = (struct ulist *)qp;
748 /* land unit might have changed (launched SAMs, collateral dmg) */
749 getland(llp->unit.land.lnd_uid, &llp->unit.land);
750 landdamage(&llp->unit.land, dam);
751 putland(llp->unit.land.lnd_uid, &llp->unit.land);
752 if (!llp->unit.land.lnd_own) {
761 lnd_easiest_target(struct emp_qelem *list)
763 struct emp_qelem *qp;
764 struct emp_qelem *next;
767 int easiest = 9876; /* things start great for victim */
770 for (qp = list->q_back; qp != list; qp = next) {
772 llp = (struct ulist *)qp;
773 hard = lnd_hardtarget(&llp->unit.land);
775 easiest = hard; /* things get worse for victim */
778 return easiest - count;
782 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
785 int mindam = emp_quelen(list) * 20;
786 int hardtarget = lnd_easiest_target(list);
787 int dam, newdam, sublaunch;
790 struct emp_qelem msl_list, *qp, *newqp;
792 msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
795 for (qp = msl_list.q_back; qp != &msl_list; qp = newqp) {
797 plp = (struct plist *)qp;
799 if (dam < mindam && mission_pln_equip(plp, NULL, 'p') >= 0) {
800 if (msl_launch(&plp->plane, EF_LAND, "troops",
801 newx, newy, victim, &sublaunch) < 0)
804 if (msl_hit(&plp->plane, hardtarget, EF_LAND,
805 N_LND_MISS, N_LND_SMISS, sublaunch, victim)) {
806 newdam = pln_damage(&plp->plane, 'p', 1);
809 newdam = pln_damage(&plp->plane, 'p', 0);
810 collateral_damage(newx, newy, newdam);
813 plp->plane.pln_effic = 0;
814 putplane(plp->plane.pln_uid, &plp->plane);
821 mpr(victim, "missile interdiction mission does %d damage!\n", dam);
822 collateral_damage(newx, newy, dam);
823 lnd_damage(list, dam);
829 /* Steve M. - commented out for now until abuse is decided upon */
830 /* risner: allow forts to interdict land units. */
832 lnd_fort_interdiction(struct emp_qelem *list,
833 coord newx, coord newy, natid victim)
842 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
843 while (nxtsct(&ns, &fsect)) {
844 if (fsect.sct_own == 0)
846 if (relations_with(fsect.sct_own, victim) >= NEUTRAL)
848 range = roundrange(fortrange(&fsect));
849 trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
852 dam = fort_fire(&fsect);
858 mpr(victim, "Incoming fire does %d damage!\n", dam);
860 "%s fires at %s land units in %s for %d!\n",
861 xyas(fsect.sct_x, fsect.sct_y,
863 cname(victim), xyas(newx, newy, fsect.sct_own), dam);
864 nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
867 lnd_damage(list, totdam);
873 lnd_mission_interdiction(struct emp_qelem *list, coord x, coord y,
878 dam = unit_interdict(x, y, victim, "land units",
879 lnd_easiest_target(list),
882 lnd_damage(list, dam);
887 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
892 if (!opt_NO_FORT_FIRE)
893 /* Steve M. - commented out for now until abuse is decided upon */
894 stopping |= lnd_fort_interdiction(list, newx, newy, victim);
897 stopping |= lnd_mission_interdiction(list, newx, newy, victim);
898 stopping |= lnd_missile_interdiction(list, newx, newy, victim);
902 /* high value of hardtarget is harder to hit */
904 lnd_hardtarget(struct lndstr *lp)
908 getsect(lp->lnd_x, lp->lnd_y, §);
909 return (int)((lp->lnd_effic / 100.0) *
910 (10 + dchr[sect.sct_type].d_dstr * 2 + lnd_spd(lp) / 2.0
915 lnd_hit_mine(struct lndstr *lp)
919 mpr(lp->lnd_own, "Blammo! Landmines detected in %s!\n",
920 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
922 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
925 if (lchr[lp->lnd_type].l_flags & L_ENGINEER)
933 lnd_pathcost(struct lndstr *lp, double pathcost)
937 effspd = lnd_spd(lp);
938 if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
939 effspd *= lp->lnd_effic * 0.01;
942 * The return value must be PATHCOST times a factor that depends
943 * only on the land unit. Anything else breaks path finding. In
944 * particular, you can't add or enforce a minimum cost here. Do
945 * it in sector_mcost().
947 return pathcost * 5.0 * speed_factor(effspd, lp->lnd_tech);
951 lnd_mobtype(struct lndstr *lp)
953 return (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
954 ? MOB_RAIL : MOB_MARCH;
958 lnd_mobcost(struct lndstr *lp, struct sctstr *sp)
960 return lnd_pathcost(lp, sector_mcost(sp, lnd_mobtype(lp)));
964 * Ask user to confirm sector abandonment, if any.
965 * All land units in LIST must be in the same sector.
966 * If removing the land units in LIST would abandon their sector, ask
967 * the user to confirm.
968 * Return zero when abandonment was declined, else non-zero.
970 int lnd_abandon_askyn(struct emp_qelem *list)
974 struct emp_qelem *qp;
978 llp = (struct ulist *)list->q_back;
979 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
980 if (!abandon_askyn(§, I_CIVIL, 0, llp))
982 if (!check_sect_ok(§))
984 for (qp = list->q_back; qp != list; qp = qp->q_back) {
985 if (!check_land_ok(&((struct ulist *)qp)->unit.land))
992 lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor)
994 struct sctstr sect, osect;
995 struct emp_qelem *qp;
996 struct emp_qelem *next;
1009 if (CANT_HAPPEN(QEMPTY(list)))
1012 if (dir <= DIR_STOP || dir >= DIR_VIEW) {
1013 lnd_mar_put(list, actor);
1016 dx = diroff[dir][0];
1017 dy = diroff[dir][1];
1019 llp = (struct ulist *)list->q_back;
1020 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &osect);
1021 oldown = osect.sct_own;
1022 newx = xnorm(llp->unit.land.lnd_x + dx);
1023 newy = ynorm(llp->unit.land.lnd_y + dy);
1024 getsect(newx, newy, §);
1025 rel = sect.sct_own ? relations_with(sect.sct_own, actor) : ALLIED;
1028 for (qp = list->q_back; qp != list; qp = next) {
1030 llp = (struct ulist *)qp;
1031 switch (lnd_check_mar(&llp->unit.land, §)) {
1034 || (lchr[llp->unit.land.lnd_type].l_flags & L_SPY))
1037 case LND_STUCK_NO_RAIL:
1039 mpr(actor, "no rail system in %s\n",
1040 xyas(newx, newy, actor));
1042 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1047 case LND_STUCK_IMPASSABLE:
1048 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1053 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1057 for (qp = list->q_back; qp != list; qp = next) {
1059 llp = (struct ulist *)qp;
1061 && !(lchr[llp->unit.land.lnd_type].l_flags & L_SPY)) {
1062 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
1063 lnd_stays(actor, dp, llp);
1066 if (llp->mobil <= 0.0) {
1067 lnd_stays(actor, "is out of mobility", llp);
1070 llp->unit.land.lnd_x = newx;
1071 llp->unit.land.lnd_y = newy;
1072 llp->mobil -= lnd_mobcost(&llp->unit.land, §);
1073 llp->unit.land.lnd_mobil = (int)llp->mobil;
1074 llp->unit.land.lnd_harden = 0;
1075 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1077 getsect(osect.sct_x, osect.sct_y, &osect);
1078 if (osect.sct_own != oldown && oldown == actor) {
1079 /* It was your sector, now it's not. Simple :) */
1080 mpr(actor, "You no longer own %s\n",
1081 xyas(osect.sct_x, osect.sct_y, actor));
1083 if (rel != ALLIED) {
1085 /* Always a 10% chance of getting caught. */
1086 if (chance(LND_SPY_DETECT_CHANCE(llp->unit.land.lnd_effic))) {
1087 if (rel == NEUTRAL || rel == FRIENDLY) {
1089 "%s unit spotted in %s\n", cname(actor),
1090 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1091 setrel(sect.sct_own, llp->unit.land.lnd_own, HOSTILE);
1092 } else if (rel <= HOSTILE) {
1094 "%s spy shot in %s\n", cname(actor),
1095 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1096 mpr(actor, "%s was shot and killed.\n",
1097 prland(&llp->unit.land));
1098 llp->unit.land.lnd_effic = 0;
1099 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1107 stopping |= lnd_sweep(list, 0, 1, actor);
1110 stopping |= lnd_check_mines(list);
1115 for (qp = list->q_back; qp != list; qp = next) {
1117 llp = (struct ulist *)qp;
1118 if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY))
1122 stopping |= lnd_interdict(list, newx, newy, actor);
1128 * Fire land unit support against VICTIM for ATTACKER, at X,Y.
1129 * If DEFENDING, this is defensive support, else offensive support.
1130 * Return total damage.
1133 lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
1135 struct nstr_item ni;
1142 snxtitem_all(&ni, EF_LAND);
1143 while (nxtitem(&ni, &land)) {
1144 if ((land.lnd_x == x) && (land.lnd_y == y))
1146 if (!feels_like_helping(land.lnd_own, attacker, victim))
1149 /* are we in range? */
1150 dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1152 range = roundrange(lnd_fire_range(&land));
1156 dam2 = lnd_fire(&land);
1157 putland(land.lnd_uid, &land);
1162 nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
1164 nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1165 if (pct_chance(lnd_acc(&land) - 1))
1168 if (land.lnd_own != attacker)
1170 "%s supported %s at %s\n",
1171 prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1177 lnd_can_attack(struct lndstr *lp)
1179 struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1181 if (lcp->l_flags & L_SUPPLY)
1188 * Increase fortification value of LP.
1189 * Fortification costs mobility. Use up to MOB mobility.
1190 * Return actual fortification increase.
1193 lnd_fortify(struct lndstr *lp, int mob)
1196 double mob_used, mult;
1198 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
1201 mob_used = MIN(lp->lnd_mobil, mob);
1205 mult = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own)
1208 hard_amt = (int)(mob_used * mult);
1209 if (lp->lnd_harden + hard_amt > land_mob_max) {
1210 hard_amt = land_mob_max - lp->lnd_harden;
1211 mob_used = ceil(hard_amt / mult);
1214 lp->lnd_mobil -= (int)mob_used;
1215 lp->lnd_harden += hard_amt;
1216 lp->lnd_harden = MIN(lp->lnd_harden, land_mob_max);
1222 * Is there a engineer unit at X,Y that can help nation CN?
1225 has_helpful_engineer(coord x, coord y, natid cn)
1227 struct nstr_item ni;
1230 snxtitem_xy(&ni, EF_LAND, x, y);
1231 while (nxtitem(&ni, &land)) {
1232 if (relations_with(land.lnd_own, cn) != ALLIED)
1234 if (lchr[(int)land.lnd_type].l_flags & L_ENGINEER)
1242 * Set LP's tech to TLEV along with everything else that depends on it.
1245 lnd_set_tech(struct lndstr *lp, int tlev)
1247 struct lchrstr *lcp = lchr + lp->lnd_type;
1249 if (CANT_HAPPEN(tlev < lcp->l_tech))
1252 lp->lnd_tech = tlev;