2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2018, 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-2016
50 #include "prototypes.h"
54 static void lnd_mar_put_one(struct ulist *);
55 static int lnd_check_one_mines(struct ulist *, int);
56 static int lnd_hit_mine(struct lndstr *);
57 static int has_helpful_engineer(coord, coord, natid);
60 lnd_find_capable(struct emp_qelem *list, int flags)
65 for (qp = list->q_back; qp != list; qp = qp->q_back) {
66 llp = (struct ulist *)qp;
67 if (lchr[llp->unit.land.lnd_type].l_flags & flags)
74 attack_val(int combat_mode, struct lndstr *lp)
80 if (lp->lnd_effic < LAND_MINEFF) {
81 putland(lp->lnd_uid, lp);
85 lcp = &lchr[(int)lp->lnd_type];
87 men = lp->lnd_item[I_MILIT];
88 value = men * lnd_att(lp) * lp->lnd_effic / 100.0;
90 switch (combat_mode) {
94 if (!(lcp->l_flags & L_MARINE))
95 return assault_penalty * value;
98 if (!(lcp->l_flags & L_MARINE))
99 return assault_penalty * men;
106 defense_val(struct lndstr *lp)
112 if (lp->lnd_effic < LAND_MINEFF) {
113 putland(lp->lnd_uid, lp);
117 lcp = &lchr[(int)lp->lnd_type];
119 men = lp->lnd_item[I_MILIT];
121 if ((lp->lnd_ship >= 0 || lp->lnd_land >= 0) &&
122 !(lcp->l_flags & L_MARINE))
125 value = men * lnd_def(lp) * lp->lnd_effic / 100.0;
126 value *= ((double)land_mob_max + lp->lnd_harden) / land_mob_max;
128 /* If there are military on the unit, you get at least a 1
129 man defensive unit, except for spies */
130 if (value < 1.0 && men > 0 && !(lcp->l_flags & L_SPY))
137 lnd_reaction_range(struct lndstr *lp)
141 getsect(lp->lnd_x, lp->lnd_y, §);
142 if (sect.sct_type == SCT_HEADQ && sect.sct_effic >= 60)
143 return lchr[lp->lnd_type].l_rad + 1;
144 return lchr[lp->lnd_type].l_rad;
148 lnd_print(natid actor, struct ulist *llp, char *s)
150 if (actor == player->cnum)
151 pr("%s %s\n", prland(&llp->unit.land), s);
153 wu(0, actor, "%s %s\n", prland(&llp->unit.land), s);
157 lnd_take_casualty(int combat_mode, struct ulist *llp, int cas)
158 /* attacking or assaulting or paratrooping? */
159 /* number of casualties to take */
171 int nowhere_to_go = 0;
172 double mobcost, bmcost;
176 if (CANT_HAPPEN(lchr[llp->unit.land.lnd_type].l_flags & L_SPY))
179 taken = llp->unit.land.lnd_item[I_MILIT];
180 eff_eq = ldround(cas * 100.0 /
181 lchr[llp->unit.land.lnd_type].l_item[I_MILIT], 1);
182 llp->unit.land.lnd_effic -= eff_eq;
183 lnd_submil(&llp->unit.land, cas);
185 if (llp->unit.land.lnd_effic < LAND_MINEFF) {
186 if (llp->unit.land.lnd_own == player->cnum)
188 sprintf(buf, "dies %sing %s!",
189 att_mode[combat_mode],
190 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
191 llp->unit.land.lnd_own));
192 lnd_print(llp->unit.land.lnd_own, llp, buf);
194 /* Since we killed the unit, we killed all the mil on it */
197 /* Ok, now, how many did we take off? (sould be the diff) */
198 taken = taken - llp->unit.land.lnd_item[I_MILIT];
201 if (llp->unit.land.lnd_effic >= llp->unit.land.lnd_retreat)
204 /* we're being boarded */
205 if (llp->unit.land.lnd_ship >= 0 && combat_mode == A_DEFEND)
208 /* we're being boarded */
209 if (llp->unit.land.lnd_land >= 0 && combat_mode == A_DEFEND)
212 /* Have to make a retreat check */
214 ret_chance = llp->unit.land.lnd_retreat - llp->unit.land.lnd_effic;
215 if (pct_chance(ret_chance)) {
216 if (llp->unit.land.lnd_own == player->cnum)
218 lnd_print(llp->unit.land.lnd_own, llp, "fails morale check!");
219 llp->unit.land.lnd_mission = 0;
220 llp->unit.land.lnd_harden = 0;
221 if (llp->unit.land.lnd_ship >= 0 || llp->unit.land.lnd_land >= 0)
223 else if (combat_mode == A_DEFEND) {
225 * defending unit.. find a place to send it
226 * strategy: look for the most-populated
227 * adjacent sector that is owned by the unit
228 * owner. Charge mob..
231 for (n = 1; n <= 6; ++n) {
232 ret_x = llp->unit.land.lnd_x + diroff[n][0];
233 ret_y = llp->unit.land.lnd_y + diroff[n][1];
234 getsect(ret_x, ret_y, §);
235 if (sect.sct_own != llp->unit.land.lnd_own)
237 if (sect.sct_type == SCT_MOUNT)
239 mobcost = lnd_mobcost(&llp->unit.land, §);
242 civs = sect.sct_item[I_CIVIL];
243 if (civs > biggest) {
253 /* retreat to bx,by */
254 llp->unit.land.lnd_x = bx;
255 llp->unit.land.lnd_y = by;
256 /* FIXME landmines */
257 mob = llp->unit.land.lnd_mobil - (int)bmcost;
260 orig = llp->unit.land.lnd_mobil;
261 llp->unit.land.lnd_mobil = (signed char)mob;
262 if (llp->unit.land.lnd_mobil > orig)
263 llp->unit.land.lnd_mobil = -127;
264 sprintf(buf, "retreats at %d%% efficiency to %s!",
265 llp->unit.land.lnd_effic,
266 xyas(bx, by, llp->unit.land.lnd_own));
267 lnd_print(llp->unit.land.lnd_own, llp, buf);
270 } else { /* attacking from a sector */
271 sprintf(buf, "leaves the battlefield at %d%% efficiency",
272 llp->unit.land.lnd_effic);
273 if ((llp->unit.land.lnd_mobil - (int)llp->mobil) < -127)
274 llp->unit.land.lnd_mobil = -127;
276 llp->unit.land.lnd_mobil -= (int)llp->mobil;
278 lnd_print(llp->unit.land.lnd_own, llp, buf);
283 /* nowhere to go.. take more casualties */
284 llp->unit.land.lnd_effic -= 10;
285 lnd_submil(&llp->unit.land,
286 lchr[llp->unit.land.lnd_type].l_item[I_MILIT] / 10);
287 if (llp->unit.land.lnd_effic < LAND_MINEFF) {
288 lnd_print(llp->unit.land.lnd_own, llp,
289 "has nowhere to retreat, and dies!");
292 lnd_print(llp->unit.land.lnd_own, llp,
293 "has nowhere to retreat and takes extra losses!");
300 lnd_takemob(struct emp_qelem *list, double loss)
302 struct emp_qelem *qp, *next;
305 int mcost = ldround(combat_mob * loss, 1);
307 for (qp = list->q_forw; qp != list; qp = next) {
309 llp = (struct ulist *)qp;
312 use_supply(&llp->unit.land);
314 new = llp->unit.land.lnd_mobil - mcost;
317 llp->unit.land.lnd_mobil = (signed char)new;
322 lnd_submil(struct lndstr *lp, int num)
324 int new = lp->lnd_item[I_MILIT] - num;
325 lp->lnd_item[I_MILIT] = new < 0 ? 0 : new;
329 lnd_spyval(struct lndstr *lp)
331 if (lchr[(int)lp->lnd_type].l_flags & L_RECON)
332 return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0) + 2;
334 return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0);
338 intelligence_report(natid destination, struct lndstr *lp, int spy,
341 int vis = lnd_vis(lp);
342 char buf1[80], buf2[80], buf3[80];
344 if (!destination || !lp->lnd_own)
347 if (chance((spy + vis) / 10.0)) {
348 sprintf(buf1, "%s %s", mess, prland(lp));
350 if (chance((spy + vis) / 20.0)) {
351 sprintf(buf2, " (eff %d, mil %d",
352 roundintby(lp->lnd_effic, 5),
353 roundintby(lp->lnd_item[I_MILIT], 10));
355 if (chance((spy + vis) / 20.0)) {
357 t = lp->lnd_tech - 20 + roll(40);
359 sprintf(buf3, ", tech %d)", t);
364 buf2[0] = buf3[0] = 0;
366 if (destination == player->cnum)
367 pr("%s%s%s\n", buf1, buf2, buf3);
369 wu(0, destination, "%s%s%s\n", buf1, buf2, buf3);
374 lnd_may_mar(struct lndstr *lp, struct lndstr *ldr, char *suffix)
379 if (!lp->lnd_own || !getsect(lp->lnd_x, lp->lnd_y, §)) {
384 if (opt_MARKET && ontradingblock(EF_LAND, lp)) {
385 mpr(lp->lnd_own, "%s is on the trading block%s\n",
390 if (lp->lnd_ship >= 0) {
391 mpr(lp->lnd_own, "%s is on a ship%s\n", prland(lp), suffix);
394 if (lp->lnd_land >= 0) {
395 mpr(lp->lnd_own, "%s is on a unit%s\n", prland(lp), suffix);
399 if (!(lchr[lp->lnd_type].l_flags & L_SPY) &&
400 !(lchr[lp->lnd_type].l_flags & L_TRAIN) &&
401 lp->lnd_item[I_MILIT] == 0) {
402 mpr(lp->lnd_own, "%s has no mil on it to guide it%s\n",
407 switch (lnd_check_mar(lp, §)) {
410 case LND_STUCK_NO_RAIL:
411 mpr(lp->lnd_own, "%s is stuck off the rail system%s\n",
417 case LND_STUCK_IMPASSABLE:
418 mpr(lp->lnd_own, "%s is stuck%s\n", prland(lp), suffix);
422 if (relations_with(sect.sct_own, lp->lnd_own) != ALLIED &&
423 !(lchr[lp->lnd_type].l_flags & L_SPY) &&
425 mpr(lp->lnd_own, "%s has been kidnapped by %s%s\n",
426 prland(lp), cname(sect.sct_own), suffix);
430 if (ldr && (lp->lnd_x != ldr->lnd_x || lp->lnd_y != ldr->lnd_y)) {
431 mpr(lp->lnd_own, "%s is not with the leader%s\n",
437 * The marching code gets confused when trains and non-trains
438 * march together. Disallow for now.
440 mobtype = lnd_mobtype(lp);
441 if (!ldr || mobtype == lnd_mobtype(ldr))
443 else if (mobtype == MOB_RAIL) {
445 "%s is a train and can't march with the leader%s\n",
449 mpr(lp->lnd_own, "%s can't rail-march with the leading train%s\n",
458 lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
460 struct lndstr land, *ldr = NULL;
464 while (nxtitem(ni, &land)) {
466 * It would be nice to let deities march foreign land units,
467 * but much of the code assumes that only the land unit's
468 * owner can march it.
470 if (!land.lnd_own || land.lnd_own != player->cnum)
472 if (!lnd_may_mar(&land, ldr, ""))
475 land.lnd_mission = 0;
477 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
478 putland(land.lnd_uid, &land);
479 llp = lnd_insque(&land, list);
481 ldr = &llp->unit.land;
486 * Append @lp to @list.
487 * Return the new list link.
490 lnd_insque(struct lndstr *lp, struct emp_qelem *list)
492 struct ulist *mlp = malloc(sizeof(struct ulist));
494 mlp->unit.land = *lp;
495 mlp->mobil = lp->lnd_mobil;
496 emp_insque(&mlp->queue, list);
501 lnd_mar_stay_behind(struct emp_qelem *list, natid actor)
503 struct emp_qelem *qp;
504 struct emp_qelem *next;
506 struct lndstr *lp, *ldr = NULL;
509 for (qp = list->q_back; qp != list; qp = next) {
511 llp = (struct ulist *)qp;
512 lp = &llp->unit.land;
513 getland(lp->lnd_uid, lp);
515 if (lp->lnd_own != actor) {
516 mpr(actor, "%s was disbanded at %s\n",
517 prland(lp), xyas(lp->lnd_x, lp->lnd_y, actor));
518 emp_remque(&llp->queue);
523 snprintf(and_stays, sizeof(and_stays), " & stays in %s",
524 xyas(lp->lnd_x, lp->lnd_y, actor));
525 if (!lnd_may_mar(lp, ldr, and_stays)) {
526 lnd_mar_put_one(llp);
532 if (lp->lnd_mobil + 1 < (int)llp->mobil) {
533 llp->mobil = lp->lnd_mobil;
539 lnd_mar_put_one(struct ulist *llp)
541 if (llp->mobil < -127)
543 llp->unit.land.lnd_mobil = llp->mobil;
548 lnd_mar_put(struct emp_qelem *list, natid actor)
550 struct emp_qelem *qp, *next;
554 for (qp = list->q_back; qp != list; qp = next) {
556 llp = (struct ulist *)qp;
557 lp = &llp->unit.land;
558 mpr(actor, "%s stopped at %s\n",
559 prland(lp), xyas(lp->lnd_x, lp->lnd_y, actor));
560 lnd_mar_put_one(llp);
565 lnd_put(struct emp_qelem *list)
567 struct emp_qelem *qp, *next;
569 for (qp = list->q_back; qp != list; qp = next) {
571 lnd_put_one((struct ulist *)qp);
576 lnd_put_one(struct ulist *llp)
578 putland(llp->unit.land.lnd_uid, &llp->unit.land);
579 emp_remque(&llp->queue);
584 * Sweep landmines with engineers in @land_list for @actor.
585 * All land units in @land_list must be in the same sector.
586 * If @explicit is non-zero, this is for an explicit sweep command from
587 * a player. Else it's an automatic "on the move" sweep.
588 * If @takemob is non-zero, require and charge mobility.
589 * Return non-zero when the land units should stop.
592 lnd_sweep(struct emp_qelem *land_list, int explicit, int takemob,
595 struct emp_qelem *qp;
596 struct emp_qelem *next;
599 int mines, m, max, sshells, lshells;
600 int stopping = 0, first = 1;
602 llp = lnd_find_capable(land_list, L_ENGINEER);
605 mpr(actor, "No engineers!\n");
609 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
611 && relations_with(sect.sct_oldown, actor) == ALLIED)
613 if (SCT_MINES_ARE_SEAMINES(§)) {
615 mpr(actor, "%s is a %s. No landmines there!\n",
616 xyas(sect.sct_x, sect.sct_y, actor),
617 dchr[sect.sct_type].d_name);
621 for (qp = land_list->q_back; qp != land_list; qp = next) {
623 llp = (struct ulist *)qp;
624 if (!(lchr[llp->unit.land.lnd_type].l_flags & L_ENGINEER))
627 if (llp->mobil <= 0.0) {
629 mpr(actor, "%s is out of mobility!\n",
630 prland(&llp->unit.land));
633 llp->mobil -= lnd_pathcost(&llp->unit.land, 0.2);
634 llp->unit.land.lnd_mobil = (int)llp->mobil;
635 llp->unit.land.lnd_harden = 0;
637 putland(llp->unit.land.lnd_uid, &llp->unit.land);
638 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
639 if (!(mines = sect.sct_mines))
641 max = lchr[llp->unit.land.lnd_type].l_item[I_SHELL];
642 lshells = llp->unit.land.lnd_item[I_SHELL];
643 sshells = sect.sct_item[I_SHELL];
644 for (m = 0; mines > 0 && m < max * 2; m++) {
645 if (chance(0.5 * lchr[llp->unit.land.lnd_type].l_att)) {
647 mpr(actor, "Approaching minefield at %s...\n",
648 xyas(sect.sct_x, sect.sct_y, actor));
651 mpr(actor, "Sweep...\n");
655 else if (sshells < ITEM_MAX)
659 sect.sct_mines = mines;
660 llp->unit.land.lnd_item[I_SHELL] = lshells;
661 sect.sct_item[I_SHELL] = sshells;
662 putland(llp->unit.land.lnd_uid, &llp->unit.land);
664 stopping |= lnd_check_one_mines(llp, 1);
670 lnd_check_one_mines(struct ulist *llp, int with_eng)
674 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
675 if (SCT_LANDMINES(§) == 0)
677 if (relations_with(sect.sct_oldown, llp->unit.land.lnd_own) == ALLIED)
679 if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
680 lnd_hit_mine(&llp->unit.land);
683 putland(llp->unit.land.lnd_uid, &llp->unit.land);
684 if (!llp->unit.land.lnd_own) {
685 emp_remque(&llp->queue);
694 lnd_check_mines(struct emp_qelem *land_list)
696 struct emp_qelem *qp;
697 struct emp_qelem *next;
699 int with_eng = !!lnd_find_capable(land_list, L_ENGINEER);
701 for (qp = land_list->q_back; qp != land_list; qp = next) {
703 stopping |= lnd_check_one_mines((struct ulist *)qp, with_eng);
708 /* Return whether and why SP would be stuck in SECTP. */
710 lnd_check_mar(struct lndstr *lp, struct sctstr *sectp)
712 if (dchr[sectp->sct_type].d_mob0 < 0)
713 return LND_STUCK_IMPASSABLE;
714 if (lnd_mobtype(lp) == MOB_RAIL && !SCT_HAS_RAIL(sectp))
715 return LND_STUCK_NO_RAIL;
716 return LND_STUCK_NOT;
720 lnd_damage(struct emp_qelem *list, int totdam)
722 struct emp_qelem *qp;
723 struct emp_qelem *next;
728 if (!totdam || !(count = emp_quelen(list)))
730 dam = ldround((double)totdam / count, 1);
731 for (qp = list->q_back; qp != list; qp = next) {
733 llp = (struct ulist *)qp;
734 /* land unit might have changed (launched SAMs, collateral dmg) */
735 getland(llp->unit.land.lnd_uid, &llp->unit.land);
736 landdamage(&llp->unit.land, dam);
737 putland(llp->unit.land.lnd_uid, &llp->unit.land);
738 if (!llp->unit.land.lnd_own) {
747 lnd_easiest_target(struct emp_qelem *list)
749 struct emp_qelem *qp;
750 struct emp_qelem *next;
753 int easiest = 9876; /* things start great for victim */
756 for (qp = list->q_back; qp != list; qp = next) {
758 llp = (struct ulist *)qp;
759 hard = lnd_hardtarget(&llp->unit.land);
761 easiest = hard; /* things get worse for victim */
764 return easiest - count;
768 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
771 int mindam = emp_quelen(list) * 20;
772 int hardtarget = lnd_easiest_target(list);
773 int dam, newdam, sublaunch;
776 struct emp_qelem msl_list, *qp, *newqp;
778 msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
781 for (qp = msl_list.q_back; qp != &msl_list; qp = newqp) {
783 plp = (struct plist *)qp;
785 if (dam < mindam && mission_pln_equip(plp, NULL, 'p') >= 0) {
786 if (msl_launch(&plp->plane, EF_LAND, "troops",
787 newx, newy, victim, &sublaunch) < 0)
790 if (msl_hit(&plp->plane, hardtarget, EF_LAND,
791 N_LND_MISS, N_LND_SMISS, sublaunch, victim)) {
792 newdam = pln_damage(&plp->plane, 'p', "");
795 newdam = pln_damage(&plp->plane, 'p', NULL);
796 collateral_damage(newx, newy, newdam);
799 plp->plane.pln_effic = 0;
800 putplane(plp->plane.pln_uid, &plp->plane);
807 mpr(victim, "missile interdiction mission does %d damage!\n", dam);
808 collateral_damage(newx, newy, dam);
809 lnd_damage(list, dam);
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)
828 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
829 while (nxtsct(&ns, &fsect)) {
830 if (fsect.sct_own == 0)
832 if (relations_with(fsect.sct_own, victim) >= NEUTRAL)
834 range = roundrange(fortrange(&fsect));
835 trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
838 dam = fort_fire(&fsect);
844 mpr(victim, "Incoming fire does %d damage!\n", dam);
846 "%s fires at %s land units in %s for %d!\n",
847 xyas(fsect.sct_x, fsect.sct_y,
849 cname(victim), xyas(newx, newy, fsect.sct_own), dam);
850 nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
853 lnd_damage(list, totdam);
859 lnd_mission_interdiction(struct emp_qelem *list, coord x, coord y,
864 dam = unit_interdict(x, y, victim, "land units",
865 lnd_easiest_target(list),
868 lnd_damage(list, dam);
873 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
878 if (!opt_NO_FORT_FIRE)
879 /* Steve M. - commented out for now until abuse is decided upon */
880 stopping |= lnd_fort_interdiction(list, newx, newy, victim);
883 stopping |= lnd_mission_interdiction(list, newx, newy, victim);
884 stopping |= lnd_missile_interdiction(list, newx, newy, victim);
888 /* high value of hardtarget is harder to hit */
890 lnd_hardtarget(struct lndstr *lp)
894 getsect(lp->lnd_x, lp->lnd_y, §);
895 return (int)((lp->lnd_effic / 100.0) *
896 (10 + dchr[sect.sct_type].d_dstr * 2 + lnd_spd(lp) / 2.0
901 lnd_hit_mine(struct lndstr *lp)
905 mpr(lp->lnd_own, "Blammo! Landmines detected in %s!\n",
906 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
908 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
911 if (lchr[lp->lnd_type].l_flags & L_ENGINEER)
919 lnd_pathcost(struct lndstr *lp, double pathcost)
923 effspd = lnd_spd(lp);
924 if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
925 effspd *= lp->lnd_effic * 0.01;
928 * The return value must be @pathcost times a factor that depends
929 * only on the land unit. Anything else breaks path finding. In
930 * particular, you can't add or enforce a minimum cost here. Do
931 * it in sector_mcost().
933 return pathcost * 5.0 * speed_factor(effspd, lp->lnd_tech);
937 lnd_mobtype(struct lndstr *lp)
939 return (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
940 ? MOB_RAIL : MOB_MARCH;
944 lnd_mobcost(struct lndstr *lp, struct sctstr *sp)
946 return lnd_pathcost(lp, sector_mcost(sp, lnd_mobtype(lp)));
950 * Ask user to confirm sector abandonment, if any.
951 * All land units in @list must be in the same sector, owned by the
952 * player, and not loaded onto anything.
953 * If removing the land units in @list would abandon their sector, ask
954 * the user to confirm.
955 * Return zero when abandonment was declined, else non-zero.
957 int lnd_abandon_askyn(struct emp_qelem *list)
962 struct emp_qelem *qp;
966 llp = (struct ulist *)list->q_back;
967 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
968 abandon = abandon_askyn(§, I_CIVIL, 0, llp);
969 if (!check_sect_ok(§))
971 for (qp = list->q_back; qp != list; qp = qp->q_back) {
972 if (!check_land_ok(&((struct ulist *)qp)->unit.land))
979 lnd_mar_dir(struct emp_qelem *list, int dir, natid actor)
981 struct sctstr sect, osect;
982 struct emp_qelem *qp;
983 struct emp_qelem *next;
993 if (CANT_HAPPEN(QEMPTY(list)))
996 if (dir <= DIR_STOP || dir > DIR_LAST) {
997 CANT_HAPPEN(dir != DIR_STOP);
998 lnd_mar_put(list, actor);
1001 dx = diroff[dir][0];
1002 dy = diroff[dir][1];
1004 llp = (struct ulist *)list->q_back;
1005 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &osect);
1006 oldown = osect.sct_own;
1007 newx = xnorm(llp->unit.land.lnd_x + dx);
1008 newy = ynorm(llp->unit.land.lnd_y + dy);
1009 getsect(newx, newy, §);
1010 rel = sect.sct_own ? relations_with(sect.sct_own, actor) : ALLIED;
1013 for (qp = list->q_back; qp != list; qp = next) {
1015 llp = (struct ulist *)qp;
1016 switch (lnd_check_mar(&llp->unit.land, §)) {
1019 || (lchr[llp->unit.land.lnd_type].l_flags & L_SPY))
1022 case LND_STUCK_NO_RAIL:
1024 mpr(actor, "no rail system in %s\n",
1025 xyas(newx, newy, actor));
1027 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1032 case LND_STUCK_IMPASSABLE:
1033 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1038 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1042 for (qp = list->q_back; qp != list; qp = next) {
1044 llp = (struct ulist *)qp;
1046 && !(lchr[llp->unit.land.lnd_type].l_flags & L_SPY)) {
1047 mpr(actor, "%s can't go to %s & stays in %s\n",
1048 prland(&llp->unit.land), xyas(newx, newy, actor),
1049 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y, actor));
1050 lnd_mar_put_one(llp);
1053 if (llp->mobil <= 0.0) {
1054 mpr(actor, "%s is out of mobility & stays in %s\n",
1055 prland(&llp->unit.land),
1056 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y, actor));
1057 lnd_mar_put_one(llp);
1060 llp->unit.land.lnd_x = newx;
1061 llp->unit.land.lnd_y = newy;
1062 llp->mobil -= lnd_mobcost(&llp->unit.land, §);
1063 llp->unit.land.lnd_mobil = (int)llp->mobil;
1064 llp->unit.land.lnd_harden = 0;
1065 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1067 getsect(osect.sct_x, osect.sct_y, &osect);
1068 if (osect.sct_own != oldown && oldown == actor) {
1069 /* It was your sector, now it's not. Simple :) */
1070 mpr(actor, "You no longer own %s\n",
1071 xyas(osect.sct_x, osect.sct_y, actor));
1073 if (rel != ALLIED) {
1075 /* Always a 10% chance of getting caught. */
1076 if (chance(LND_SPY_DETECT_CHANCE(llp->unit.land.lnd_effic))) {
1077 if (rel == NEUTRAL || rel == FRIENDLY) {
1079 "%s unit spotted in %s\n", cname(actor),
1080 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1081 setrel(sect.sct_own, llp->unit.land.lnd_own, HOSTILE);
1082 } else if (rel <= HOSTILE) {
1084 "%s spy shot in %s\n", cname(actor),
1085 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1086 mpr(actor, "%s was shot and killed.\n",
1087 prland(&llp->unit.land));
1088 llp->unit.land.lnd_effic = 0;
1089 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1096 return QEMPTY(list);
1100 lnd_mar_gauntlet(struct emp_qelem *list, int interdict, natid actor)
1102 struct ulist *mlp = (struct ulist *)list->q_back;
1104 int stopping, visible;
1105 struct emp_qelem *qp, *next;
1108 if (CANT_HAPPEN(QEMPTY(list)))
1110 newx = mlp->unit.land.lnd_x;
1111 newy = mlp->unit.land.lnd_y;
1112 stopping = lnd_sweep(list, 0, 1, actor);
1115 stopping |= lnd_check_mines(list);
1120 for (qp = list->q_back; qp != list; qp = next) {
1122 llp = (struct ulist *)qp;
1123 if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY))
1126 if (visible && interdict)
1127 stopping |= lnd_interdict(list, newx, newy, actor);
1133 * Fire land unit support against @victim for @attacker, at @x,@y.
1134 * If @defending, this is defensive support, else offensive support.
1135 * Return total damage.
1138 lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
1140 struct nstr_item ni;
1147 snxtitem_all(&ni, EF_LAND);
1148 while (nxtitem(&ni, &land)) {
1149 if ((land.lnd_x == x) && (land.lnd_y == y))
1151 if (!feels_like_helping(land.lnd_own, attacker, victim))
1154 /* are we in range? */
1155 dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1157 range = roundrange(lnd_fire_range(&land));
1161 dam2 = lnd_fire(&land);
1162 putland(land.lnd_uid, &land);
1167 nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
1169 nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1170 if (pct_chance(lnd_acc(&land) - 1))
1173 if (land.lnd_own != attacker)
1175 "%s supported %s at %s\n",
1176 prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1182 lnd_can_attack(struct lndstr *lp)
1184 struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1186 if (lcp->l_flags & L_SUPPLY)
1193 * Increase fortification value of @lp.
1194 * Fortification costs mobility. Use up to @mob mobility.
1195 * Return actual fortification increase.
1198 lnd_fortify(struct lndstr *lp, int mob)
1201 double mob_used, mult;
1203 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
1206 mob_used = MIN(lp->lnd_mobil, mob);
1210 mult = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own)
1213 hard_amt = (int)(mob_used * mult);
1214 if (lp->lnd_harden + hard_amt > land_mob_max) {
1215 hard_amt = land_mob_max - lp->lnd_harden;
1216 mob_used = ceil(hard_amt / mult);
1219 lp->lnd_mobil -= (int)mob_used;
1220 lp->lnd_harden += hard_amt;
1221 lp->lnd_harden = MIN(lp->lnd_harden, land_mob_max);
1227 * Is there a engineer unit at @x,@y that can help nation @cn?
1230 has_helpful_engineer(coord x, coord y, natid cn)
1232 struct nstr_item ni;
1235 snxtitem_xy(&ni, EF_LAND, x, y);
1236 while (nxtitem(&ni, &land)) {
1237 if (relations_with(land.lnd_own, cn) != ALLIED)
1239 if (lchr[(int)land.lnd_type].l_flags & L_ENGINEER)
1247 * Set @lp's tech to @tlev along with everything else that depends on it.
1250 lnd_set_tech(struct lndstr *lp, int tlev)
1252 struct lchrstr *lcp = lchr + lp->lnd_type;
1254 if (CANT_HAPPEN(tlev < lcp->l_tech))
1257 lp->lnd_tech = tlev;