2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2015, 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-2015
51 #include "prototypes.h"
55 static void lnd_mar_put_one(struct ulist *);
56 static int lnd_check_one_mines(struct ulist *, int);
57 static void lnd_stays(natid, char *, struct ulist *);
58 static int lnd_hit_mine(struct lndstr *);
59 static int has_helpful_engineer(coord, coord, natid);
62 lnd_find_capable(struct emp_qelem *list, int flags)
67 for (qp = list->q_back; qp != list; qp = qp->q_back) {
68 llp = (struct ulist *)qp;
69 if (lchr[llp->unit.land.lnd_type].l_flags & flags)
76 attack_val(int combat_mode, struct lndstr *lp)
82 if (lp->lnd_effic < LAND_MINEFF) {
83 putland(lp->lnd_uid, lp);
87 lcp = &lchr[(int)lp->lnd_type];
89 /* Spies always count as 1 during assaults. If they are the only ones
90 in the assault, they get to sneak on anyway. */
92 if (lcp->l_flags & L_SPY && combat_mode == A_ASSAULT)
95 men = lp->lnd_item[I_MILIT];
96 value = men * lnd_att(lp) * lp->lnd_effic / 100.0;
98 switch (combat_mode) {
102 if (!(lcp->l_flags & L_MARINE))
103 return assault_penalty * value;
106 if (!(lcp->l_flags & L_MARINE))
107 return assault_penalty * men;
114 defense_val(struct lndstr *lp)
120 if (lp->lnd_effic < LAND_MINEFF) {
121 putland(lp->lnd_uid, lp);
125 lcp = &lchr[(int)lp->lnd_type];
127 men = lp->lnd_item[I_MILIT];
129 if ((lp->lnd_ship >= 0 || lp->lnd_land >= 0) &&
130 !(lcp->l_flags & L_MARINE))
133 value = men * lnd_def(lp) * lp->lnd_effic / 100.0;
134 value *= ((double)land_mob_max + lp->lnd_harden) / land_mob_max;
136 /* If there are military on the unit, you get at least a 1
137 man defensive unit, except for spies */
138 if (value < 1.0 && men > 0 && !(lcp->l_flags & L_SPY))
145 lnd_reaction_range(struct lndstr *lp)
149 getsect(lp->lnd_x, lp->lnd_y, §);
150 if (sect.sct_type == SCT_HEADQ && sect.sct_effic >= 60)
151 return lchr[lp->lnd_type].l_rad + 1;
152 return lchr[lp->lnd_type].l_rad;
156 lnd_print(natid actor, struct ulist *llp, char *s)
158 if (actor == player->cnum)
159 pr("%s %s\n", prland(&llp->unit.land), s);
161 wu(0, actor, "%s %s\n", prland(&llp->unit.land), s);
165 lnd_take_casualty(int combat_mode, struct ulist *llp, int cas)
166 /* attacking or assaulting or paratrooping? */
167 /* number of casualties to take */
179 int nowhere_to_go = 0;
180 double mobcost, bmcost;
184 taken = llp->unit.land.lnd_item[I_MILIT];
185 /* Spies always die */
186 if (lchr[llp->unit.land.lnd_type].l_flags & L_SPY)
187 llp->unit.land.lnd_effic = 0;
189 eff_eq = ldround(cas * 100.0 /
190 lchr[llp->unit.land.lnd_type].l_item[I_MILIT], 1);
191 llp->unit.land.lnd_effic -= eff_eq;
192 lnd_submil(&llp->unit.land, cas);
195 if (llp->unit.land.lnd_effic < LAND_MINEFF) {
196 sprintf(buf, "dies %s %s!",
197 combat_mode ? att_mode[combat_mode] : "defending",
198 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
199 llp->unit.land.lnd_own));
200 lnd_print(llp->unit.land.lnd_own, llp, buf);
202 /* Since we killed the unit, we killed all the mil on it */
205 /* Ok, now, how many did we take off? (sould be the diff) */
206 taken = taken - llp->unit.land.lnd_item[I_MILIT];
209 if (llp->unit.land.lnd_effic >= llp->unit.land.lnd_retreat)
212 /* we're being boarded */
213 if (llp->unit.land.lnd_ship >= 0 && combat_mode == A_DEFEND)
216 /* we're being boarded */
217 if (llp->unit.land.lnd_land >= 0 && combat_mode == A_DEFEND)
220 /* Have to make a retreat check */
222 ret_chance = llp->unit.land.lnd_retreat - llp->unit.land.lnd_effic;
223 if (pct_chance(ret_chance)) {
225 lnd_print(llp->unit.land.lnd_own, llp, "fails morale check!");
226 llp->unit.land.lnd_mission = 0;
227 llp->unit.land.lnd_harden = 0;
228 if (llp->unit.land.lnd_ship >= 0 || llp->unit.land.lnd_land >= 0)
230 else if (combat_mode == A_DEFEND) {
232 * defending unit.. find a place to send it
233 * strategy: look for the most-populated
234 * adjacent sector that is owned by the unit
235 * owner. Charge mob..
238 for (n = 1; n <= 6; ++n) {
239 ret_x = llp->unit.land.lnd_x + diroff[n][0];
240 ret_y = llp->unit.land.lnd_y + diroff[n][1];
241 getsect(ret_x, ret_y, §);
242 if (sect.sct_own != llp->unit.land.lnd_own)
244 if (sect.sct_type == SCT_MOUNT)
246 mobcost = lnd_mobcost(&llp->unit.land, §);
249 civs = sect.sct_item[I_CIVIL];
250 if (civs > biggest) {
260 /* retreat to bx,by */
261 llp->unit.land.lnd_x = bx;
262 llp->unit.land.lnd_y = by;
263 /* FIXME landmines */
264 mob = llp->unit.land.lnd_mobil - (int)bmcost;
267 orig = llp->unit.land.lnd_mobil;
268 llp->unit.land.lnd_mobil = (signed char)mob;
269 if (llp->unit.land.lnd_mobil > orig)
270 llp->unit.land.lnd_mobil = -127;
271 sprintf(buf, "retreats at %d%% efficiency to %s!",
272 llp->unit.land.lnd_effic,
273 xyas(bx, by, llp->unit.land.lnd_own));
274 lnd_print(llp->unit.land.lnd_own, llp, buf);
277 } else { /* attacking from a sector */
278 sprintf(buf, "leaves the battlefield at %d%% efficiency",
279 llp->unit.land.lnd_effic);
280 if ((llp->unit.land.lnd_mobil - (int)llp->mobil) < -127)
281 llp->unit.land.lnd_mobil = -127;
283 llp->unit.land.lnd_mobil -= (int)llp->mobil;
285 lnd_print(llp->unit.land.lnd_own, llp, buf);
290 /* nowhere to go.. take more casualties */
291 llp->unit.land.lnd_effic -= 10;
292 lnd_submil(&llp->unit.land,
293 lchr[llp->unit.land.lnd_type].l_item[I_MILIT] / 10);
294 if (llp->unit.land.lnd_effic < LAND_MINEFF) {
295 lnd_print(llp->unit.land.lnd_own, llp,
296 "has nowhere to retreat, and dies!");
299 lnd_print(llp->unit.land.lnd_own, llp,
300 "has nowhere to retreat and takes extra losses!");
307 lnd_takemob(struct emp_qelem *list, double loss)
309 struct emp_qelem *qp, *next;
312 int mcost = ldround(combat_mob * loss, 1);
314 for (qp = list->q_forw; qp != list; qp = next) {
316 llp = (struct ulist *)qp;
319 use_supply(&llp->unit.land);
321 new = llp->unit.land.lnd_mobil - mcost;
324 llp->unit.land.lnd_mobil = (signed char)new;
329 lnd_submil(struct lndstr *lp, int num)
331 int new = lp->lnd_item[I_MILIT] - num;
332 lp->lnd_item[I_MILIT] = new < 0 ? 0 : new;
336 lnd_spyval(struct lndstr *lp)
338 if (lchr[(int)lp->lnd_type].l_flags & L_RECON)
339 return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0) + 2;
341 return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0);
345 intelligence_report(int destination, struct lndstr *lp, int spy,
348 int vis = lnd_vis(lp);
349 char buf1[80], buf2[80], buf3[80];
351 if (destination == 0)
354 if (lp->lnd_own == 0)
357 memset(buf1, 0, sizeof(buf1));
358 memset(buf2, 0, sizeof(buf2));
359 memset(buf3, 0, sizeof(buf3));
360 if (chance((spy + vis) / 10.0)) {
361 if (destination == player->cnum)
362 pr("%s %s", mess, prland(lp));
364 sprintf(buf1, "%s %s", mess, prland(lp));
366 if (chance((spy + vis) / 20.0)) {
367 if (destination == player->cnum)
368 pr(" (eff %d, mil %d",
369 roundintby(lp->lnd_effic, 5),
370 roundintby(lp->lnd_item[I_MILIT], 10));
372 sprintf(buf2, " (eff %d, mil %d",
373 roundintby(lp->lnd_effic, 5),
374 roundintby(lp->lnd_item[I_MILIT], 10));
376 if (chance((spy + vis) / 20.0)) {
378 t = lp->lnd_tech - 20 + roll(40);
380 if (destination == player->cnum)
381 pr(", tech %d)\n", t);
383 sprintf(buf3, ", tech %d)\n", t);
385 if (destination == player->cnum)
388 sprintf(buf3, ")\n");
391 if (destination == player->cnum)
398 if (destination != player->cnum) {
399 wu(0, destination, "%s%s%s", buf1, buf2, buf3);
404 lnd_may_mar(struct lndstr *lp, struct lndstr *ldr, char *suffix)
408 if (!lp->lnd_own || !getsect(lp->lnd_x, lp->lnd_y, §)) {
413 if (lp->lnd_ship >= 0) {
414 mpr(lp->lnd_own, "%s is on a ship%s\n", prland(lp), suffix);
417 if (lp->lnd_land >= 0) {
418 mpr(lp->lnd_own, "%s is on a unit%s\n", prland(lp), suffix);
422 if (!(lchr[lp->lnd_type].l_flags & L_SPY) &&
423 !(lchr[lp->lnd_type].l_flags & L_TRAIN) &&
424 lp->lnd_item[I_MILIT] == 0) {
425 mpr(lp->lnd_own, "%s has no mil on it to guide it%s\n",
430 switch (lnd_check_mar(lp, §)) {
433 case LND_STUCK_NO_RAIL:
434 mpr(lp->lnd_own, "%s is stuck off the rail system%s\n",
440 case LND_STUCK_IMPASSABLE:
441 mpr(lp->lnd_own, "%s is stuck%s\n", prland(lp), suffix);
445 if (relations_with(sect.sct_own, lp->lnd_own) != ALLIED &&
446 !(lchr[lp->lnd_type].l_flags & L_SPY) &&
448 mpr(lp->lnd_own, "%s has been kidnapped by %s%s\n",
449 prland(lp), cname(sect.sct_own), suffix);
453 if (ldr && (lp->lnd_x != ldr->lnd_x || lp->lnd_y != ldr->lnd_y)) {
454 mpr(lp->lnd_own, "%s is not with the leader%s\n",
462 lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
464 struct lndstr land, *ldr = NULL;
467 int mobtype = MOB_MOVE; /* indeterminate */
470 while (nxtitem(ni, &land)) {
472 * It would be nice to let deities march foreign land units,
473 * but much of the code assumes that only the land unit's
474 * owner can march it.
476 if (!land.lnd_own || land.lnd_own != player->cnum)
479 if (ontradingblock(EF_LAND, &land)) {
480 pr("unit #%d inelligible - it's for sale.\n",
485 if (!lnd_may_mar(&land, ldr, ""))
488 * The marching code gets confused when trains and non-trains
489 * march together. Disallow for now.
491 this_mot = lnd_mobtype(&land);
492 if (this_mot != mobtype) {
493 if (mobtype == MOB_MOVE)
495 else if (mobtype == MOB_MARCH) {
496 pr("%s is a train and can't march with the leader.\n",
500 pr("%s can't rail-march with the leading train.\n",
506 land.lnd_mission = 0;
508 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
509 putland(land.lnd_uid, &land);
510 llp = lnd_insque(&land, list);
512 ldr = &llp->unit.land;
518 * Return the new list link.
521 lnd_insque(struct lndstr *lp, struct emp_qelem *list)
523 struct ulist *mlp = malloc(sizeof(struct ulist));
525 mlp->unit.land = *lp;
526 mlp->mobil = lp->lnd_mobil;
527 emp_insque(&mlp->queue, list);
531 /* This function assumes that the list was created by lnd_sel */
533 lnd_mar(struct emp_qelem *list, double *minmobp, double *maxmobp,
536 struct emp_qelem *qp;
537 struct emp_qelem *next;
539 struct lndstr *lp, *ldr = NULL;
544 for (qp = list->q_back; qp != list; qp = next) {
546 llp = (struct ulist *)qp;
547 lp = &llp->unit.land;
548 getland(lp->lnd_uid, lp);
550 if (lp->lnd_own != actor) {
551 mpr(actor, "%s was disbanded at %s\n",
552 prland(lp), xyas(lp->lnd_x, lp->lnd_y, actor));
553 emp_remque(&llp->queue);
558 snprintf(and_stays, sizeof(and_stays), " & stays in %s",
559 xyas(lp->lnd_x, lp->lnd_y, actor));
560 if (!lnd_may_mar(lp, ldr, and_stays)) {
561 lnd_mar_put_one(llp);
567 if (lp->lnd_mobil + 1 < (int)llp->mobil) {
568 llp->mobil = lp->lnd_mobil;
570 if (llp->mobil < *minmobp)
571 *minmobp = llp->mobil;
572 if (llp->mobil > *maxmobp)
573 *maxmobp = llp->mobil;
578 lnd_mar_put_one(struct ulist *llp)
580 if (llp->mobil < -127)
582 llp->unit.land.lnd_mobil = llp->mobil;
587 lnd_mar_put(struct emp_qelem *list, natid actor)
589 struct emp_qelem *qp, *next;
593 for (qp = list->q_back; qp != list; qp = next) {
595 llp = (struct ulist *)qp;
596 lp = &llp->unit.land;
597 mpr(actor, "%s stopped at %s\n",
598 prland(lp), xyas(lp->lnd_x, lp->lnd_y, actor));
599 lnd_mar_put_one(llp);
604 lnd_put(struct emp_qelem *list)
606 struct emp_qelem *qp, *next;
608 for (qp = list->q_back; qp != list; qp = next) {
610 lnd_put_one((struct ulist *)qp);
615 lnd_put_one(struct ulist *llp)
617 putland(llp->unit.land.lnd_uid, &llp->unit.land);
618 emp_remque(&llp->queue);
623 * Sweep landmines with engineers in LAND_LIST for ACTOR.
624 * All land units in LAND_LIST must be in the same sector.
625 * If EXPLICIT is non-zero, this is for an explicit sweep command from
626 * a player. Else it's an automatic "on the move" sweep.
627 * If TAKEMOB is non-zero, require and charge mobility.
628 * Return non-zero when the land units should stop.
631 lnd_sweep(struct emp_qelem *land_list, int explicit, int takemob,
634 struct emp_qelem *qp;
635 struct emp_qelem *next;
638 int mines, m, max, sshells, lshells;
639 int stopping = 0, first = 1;
641 llp = lnd_find_capable(land_list, L_ENGINEER);
644 mpr(actor, "No engineers!\n");
648 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
650 && relations_with(sect.sct_oldown, actor) == ALLIED)
652 if (SCT_MINES_ARE_SEAMINES(§)) {
654 mpr(actor, "%s is a %s. No landmines there!\n",
655 xyas(sect.sct_x, sect.sct_y, actor),
656 dchr[sect.sct_type].d_name);
660 for (qp = land_list->q_back; qp != land_list; qp = next) {
662 llp = (struct ulist *)qp;
663 if (!(lchr[llp->unit.land.lnd_type].l_flags & L_ENGINEER))
666 if (llp->mobil <= 0.0) {
668 mpr(actor, "%s is out of mobility!\n",
669 prland(&llp->unit.land));
672 llp->mobil -= lnd_pathcost(&llp->unit.land, 0.2);
673 llp->unit.land.lnd_mobil = (int)llp->mobil;
674 llp->unit.land.lnd_harden = 0;
676 putland(llp->unit.land.lnd_uid, &llp->unit.land);
677 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
678 if (!(mines = sect.sct_mines))
680 max = lchr[llp->unit.land.lnd_type].l_item[I_SHELL];
681 lshells = llp->unit.land.lnd_item[I_SHELL];
682 sshells = sect.sct_item[I_SHELL];
683 for (m = 0; mines > 0 && m < max * 2; m++) {
684 if (chance(0.5 * lchr[llp->unit.land.lnd_type].l_att)) {
686 mpr(actor, "Approaching minefield at %s...\n",
687 xyas(sect.sct_x, sect.sct_y, actor));
690 mpr(actor, "Sweep...\n");
694 else if (sshells < ITEM_MAX)
698 sect.sct_mines = mines;
699 llp->unit.land.lnd_item[I_SHELL] = lshells;
700 sect.sct_item[I_SHELL] = sshells;
701 putland(llp->unit.land.lnd_uid, &llp->unit.land);
703 if (lnd_check_one_mines(llp, 1)) {
713 lnd_check_one_mines(struct ulist *llp, int with_eng)
717 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
718 if (SCT_LANDMINES(§) == 0)
720 if (relations_with(sect.sct_oldown, llp->unit.land.lnd_own) == ALLIED)
722 if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
723 lnd_hit_mine(&llp->unit.land);
726 putland(llp->unit.land.lnd_uid, &llp->unit.land);
727 if (!llp->unit.land.lnd_own)
734 lnd_check_mines(struct emp_qelem *land_list)
736 struct emp_qelem *qp;
737 struct emp_qelem *next;
740 int with_eng = !!lnd_find_capable(land_list, L_ENGINEER);
742 for (qp = land_list->q_back; qp != land_list; qp = next) {
744 llp = (struct ulist *)qp;
745 if (lnd_check_one_mines(llp, with_eng)) {
755 lnd_stays(natid actor, char *str, struct ulist *llp)
757 mpr(actor, "%s %s & stays in %s\n",
758 prland(&llp->unit.land), str,
759 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y, actor));
760 lnd_mar_put_one(llp);
763 /* Return whether and why SP would be stuck in SECTP. */
765 lnd_check_mar(struct lndstr *lp, struct sctstr *sectp)
767 if (dchr[sectp->sct_type].d_mob0 < 0)
768 return LND_STUCK_IMPASSABLE;
769 if (lnd_mobtype(lp) == MOB_RAIL && !SCT_HAS_RAIL(sectp))
770 return LND_STUCK_NO_RAIL;
771 return LND_STUCK_NOT;
775 lnd_damage(struct emp_qelem *list, int totdam)
777 struct emp_qelem *qp;
778 struct emp_qelem *next;
783 if (!totdam || !(count = emp_quelen(list)))
785 dam = ldround((double)totdam / count, 1);
786 for (qp = list->q_back; qp != list; qp = next) {
788 llp = (struct ulist *)qp;
789 /* land unit might have changed (launched SAMs, collateral dmg) */
790 getland(llp->unit.land.lnd_uid, &llp->unit.land);
791 landdamage(&llp->unit.land, dam);
792 putland(llp->unit.land.lnd_uid, &llp->unit.land);
793 if (!llp->unit.land.lnd_own) {
802 lnd_easiest_target(struct emp_qelem *list)
804 struct emp_qelem *qp;
805 struct emp_qelem *next;
808 int easiest = 9876; /* things start great for victim */
811 for (qp = list->q_back; qp != list; qp = next) {
813 llp = (struct ulist *)qp;
814 hard = lnd_hardtarget(&llp->unit.land);
816 easiest = hard; /* things get worse for victim */
819 return easiest - count;
823 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
826 int mindam = emp_quelen(list) * 20;
827 int hardtarget = lnd_easiest_target(list);
828 int dam, newdam, sublaunch;
831 struct emp_qelem msl_list, *qp, *newqp;
833 msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
836 for (qp = msl_list.q_back; qp != &msl_list; qp = newqp) {
838 plp = (struct plist *)qp;
840 if (dam < mindam && mission_pln_equip(plp, NULL, 'p') >= 0) {
841 if (msl_launch(&plp->plane, EF_LAND, "troops",
842 newx, newy, victim, &sublaunch) < 0)
845 if (msl_hit(&plp->plane, hardtarget, EF_LAND,
846 N_LND_MISS, N_LND_SMISS, sublaunch, victim)) {
847 newdam = pln_damage(&plp->plane, 'p', 1);
850 newdam = pln_damage(&plp->plane, 'p', 0);
851 collateral_damage(newx, newy, newdam);
854 plp->plane.pln_effic = 0;
855 putplane(plp->plane.pln_uid, &plp->plane);
862 mpr(victim, "missile interdiction mission does %d damage!\n", dam);
863 collateral_damage(newx, newy, dam);
864 lnd_damage(list, dam);
870 /* Steve M. - commented out for now until abuse is decided upon */
871 /* risner: allow forts to interdict land units. */
873 lnd_fort_interdiction(struct emp_qelem *list,
874 coord newx, coord newy, natid victim)
883 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
884 while (nxtsct(&ns, &fsect)) {
885 if (fsect.sct_own == 0)
887 if (relations_with(fsect.sct_own, victim) >= NEUTRAL)
889 range = roundrange(fortrange(&fsect));
890 trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
893 dam = fort_fire(&fsect);
899 mpr(victim, "Incoming fire does %d damage!\n", dam);
901 "%s fires at %s land units in %s for %d!\n",
902 xyas(fsect.sct_x, fsect.sct_y,
904 cname(victim), xyas(newx, newy, fsect.sct_own), dam);
905 nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
908 lnd_damage(list, totdam);
914 lnd_mission_interdiction(struct emp_qelem *list, coord x, coord y,
919 dam = unit_interdict(x, y, victim, "land units",
920 lnd_easiest_target(list),
923 lnd_damage(list, dam);
928 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
933 if (!opt_NO_FORT_FIRE)
934 /* Steve M. - commented out for now until abuse is decided upon */
935 stopping |= lnd_fort_interdiction(list, newx, newy, victim);
938 stopping |= lnd_mission_interdiction(list, newx, newy, victim);
939 stopping |= lnd_missile_interdiction(list, newx, newy, victim);
943 /* high value of hardtarget is harder to hit */
945 lnd_hardtarget(struct lndstr *lp)
949 getsect(lp->lnd_x, lp->lnd_y, §);
950 return (int)((lp->lnd_effic / 100.0) *
951 (10 + dchr[sect.sct_type].d_dstr * 2 + lnd_spd(lp) / 2.0
956 lnd_hit_mine(struct lndstr *lp)
960 mpr(lp->lnd_own, "Blammo! Landmines detected in %s!\n",
961 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
963 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
966 if (lchr[lp->lnd_type].l_flags & L_ENGINEER)
974 lnd_pathcost(struct lndstr *lp, double pathcost)
978 effspd = lnd_spd(lp);
979 if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
980 effspd *= lp->lnd_effic * 0.01;
983 * The return value must be PATHCOST times a factor that depends
984 * only on the land unit. Anything else breaks path finding. In
985 * particular, you can't add or enforce a minimum cost here. Do
986 * it in sector_mcost().
988 return pathcost * 5.0 * speed_factor(effspd, lp->lnd_tech);
992 lnd_mobtype(struct lndstr *lp)
994 return (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
995 ? MOB_RAIL : MOB_MARCH;
999 lnd_mobcost(struct lndstr *lp, struct sctstr *sp)
1001 return lnd_pathcost(lp, sector_mcost(sp, lnd_mobtype(lp)));
1005 * Ask user to confirm sector abandonment, if any.
1006 * All land units in LIST must be in the same sector.
1007 * If removing the land units in LIST would abandon their sector, ask
1008 * the user to confirm.
1009 * Return zero when abandonment was declined, else non-zero.
1011 int lnd_abandon_askyn(struct emp_qelem *list)
1015 struct emp_qelem *qp;
1019 llp = (struct ulist *)list->q_back;
1020 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
1021 if (!abandon_askyn(§, I_CIVIL, 0, llp))
1023 if (!check_sect_ok(§))
1025 for (qp = list->q_back; qp != list; qp = qp->q_back) {
1026 if (!check_land_ok(&((struct ulist *)qp)->unit.land))
1033 lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor)
1035 struct sctstr sect, osect;
1036 struct emp_qelem *qp;
1037 struct emp_qelem *next;
1050 if (CANT_HAPPEN(QEMPTY(list)))
1053 if (dir <= DIR_STOP || dir > DIR_LAST) {
1054 CANT_HAPPEN(dir != DIR_STOP);
1055 lnd_mar_put(list, actor);
1058 dx = diroff[dir][0];
1059 dy = diroff[dir][1];
1061 llp = (struct ulist *)list->q_back;
1062 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &osect);
1063 oldown = osect.sct_own;
1064 newx = xnorm(llp->unit.land.lnd_x + dx);
1065 newy = ynorm(llp->unit.land.lnd_y + dy);
1066 getsect(newx, newy, §);
1067 rel = sect.sct_own ? relations_with(sect.sct_own, actor) : ALLIED;
1070 for (qp = list->q_back; qp != list; qp = next) {
1072 llp = (struct ulist *)qp;
1073 switch (lnd_check_mar(&llp->unit.land, §)) {
1076 || (lchr[llp->unit.land.lnd_type].l_flags & L_SPY))
1079 case LND_STUCK_NO_RAIL:
1081 mpr(actor, "no rail system in %s\n",
1082 xyas(newx, newy, actor));
1084 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1089 case LND_STUCK_IMPASSABLE:
1090 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1095 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1099 for (qp = list->q_back; qp != list; qp = next) {
1101 llp = (struct ulist *)qp;
1103 && !(lchr[llp->unit.land.lnd_type].l_flags & L_SPY)) {
1104 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
1105 lnd_stays(actor, dp, llp);
1108 if (llp->mobil <= 0.0) {
1109 lnd_stays(actor, "is out of mobility", llp);
1112 llp->unit.land.lnd_x = newx;
1113 llp->unit.land.lnd_y = newy;
1114 llp->mobil -= lnd_mobcost(&llp->unit.land, §);
1115 llp->unit.land.lnd_mobil = (int)llp->mobil;
1116 llp->unit.land.lnd_harden = 0;
1117 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1119 getsect(osect.sct_x, osect.sct_y, &osect);
1120 if (osect.sct_own != oldown && oldown == actor) {
1121 /* It was your sector, now it's not. Simple :) */
1122 mpr(actor, "You no longer own %s\n",
1123 xyas(osect.sct_x, osect.sct_y, actor));
1125 if (rel != ALLIED) {
1127 /* Always a 10% chance of getting caught. */
1128 if (chance(LND_SPY_DETECT_CHANCE(llp->unit.land.lnd_effic))) {
1129 if (rel == NEUTRAL || rel == FRIENDLY) {
1131 "%s unit spotted in %s\n", cname(actor),
1132 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1133 setrel(sect.sct_own, llp->unit.land.lnd_own, HOSTILE);
1134 } else if (rel <= HOSTILE) {
1136 "%s spy shot in %s\n", cname(actor),
1137 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1138 mpr(actor, "%s was shot and killed.\n",
1139 prland(&llp->unit.land));
1140 llp->unit.land.lnd_effic = 0;
1141 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1149 stopping |= lnd_sweep(list, 0, 1, actor);
1152 stopping |= lnd_check_mines(list);
1157 for (qp = list->q_back; qp != list; qp = next) {
1159 llp = (struct ulist *)qp;
1160 if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY))
1164 stopping |= lnd_interdict(list, newx, newy, actor);
1170 * Fire land unit support against VICTIM for ATTACKER, at X,Y.
1171 * If DEFENDING, this is defensive support, else offensive support.
1172 * Return total damage.
1175 lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
1177 struct nstr_item ni;
1184 snxtitem_all(&ni, EF_LAND);
1185 while (nxtitem(&ni, &land)) {
1186 if ((land.lnd_x == x) && (land.lnd_y == y))
1188 if (!feels_like_helping(land.lnd_own, attacker, victim))
1191 /* are we in range? */
1192 dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1194 range = roundrange(lnd_fire_range(&land));
1198 dam2 = lnd_fire(&land);
1199 putland(land.lnd_uid, &land);
1204 nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
1206 nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1207 if (pct_chance(lnd_acc(&land) - 1))
1210 if (land.lnd_own != attacker)
1212 "%s supported %s at %s\n",
1213 prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1219 lnd_can_attack(struct lndstr *lp)
1221 struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1223 if (lcp->l_flags & L_SUPPLY)
1230 * Increase fortification value of LP.
1231 * Fortification costs mobility. Use up to MOB mobility.
1232 * Return actual fortification increase.
1235 lnd_fortify(struct lndstr *lp, int mob)
1238 double mob_used, mult;
1240 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
1243 mob_used = MIN(lp->lnd_mobil, mob);
1247 mult = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own)
1250 hard_amt = (int)(mob_used * mult);
1251 if (lp->lnd_harden + hard_amt > land_mob_max) {
1252 hard_amt = land_mob_max - lp->lnd_harden;
1253 mob_used = ceil(hard_amt / mult);
1256 lp->lnd_mobil -= (int)mob_used;
1257 lp->lnd_harden += hard_amt;
1258 lp->lnd_harden = MIN(lp->lnd_harden, land_mob_max);
1264 * Is there a engineer unit at X,Y that can help nation CN?
1267 has_helpful_engineer(coord x, coord y, natid cn)
1269 struct nstr_item ni;
1272 snxtitem_xy(&ni, EF_LAND, x, y);
1273 while (nxtitem(&ni, &land)) {
1274 if (relations_with(land.lnd_own, cn) != ALLIED)
1276 if (lchr[(int)land.lnd_type].l_flags & L_ENGINEER)
1284 * Set LP's tech to TLEV along with everything else that depends on it.
1287 lnd_set_tech(struct lndstr *lp, int tlev)
1289 struct lchrstr *lcp = lchr + lp->lnd_type;
1291 if (CANT_HAPPEN(tlev < lcp->l_tech))
1294 lp->lnd_tech = tlev;