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 void lnd_stays(natid, char *, struct ulist *);
56 static int lnd_hit_mine(struct lndstr *);
57 static int has_helpful_engineer(coord, coord, natid);
60 attack_val(int combat_mode, struct lndstr *lp)
66 if (lp->lnd_effic < LAND_MINEFF) {
67 putland(lp->lnd_uid, lp);
71 lcp = &lchr[(int)lp->lnd_type];
73 /* Spies always count as 1 during assaults. If they are the only ones
74 in the assault, they get to sneak on anyway. */
76 if (lcp->l_flags & L_SPY && combat_mode == A_ASSAULT)
79 men = lp->lnd_item[I_MILIT];
80 value = men * lnd_att(lp) * lp->lnd_effic / 100.0;
82 switch (combat_mode) {
86 if (!(lcp->l_flags & L_MARINE))
87 return assault_penalty * value;
90 if (!(lcp->l_flags & L_MARINE))
91 return assault_penalty * men;
98 defense_val(struct lndstr *lp)
104 if (lp->lnd_effic < LAND_MINEFF) {
105 putland(lp->lnd_uid, lp);
109 lcp = &lchr[(int)lp->lnd_type];
111 men = lp->lnd_item[I_MILIT];
113 if ((lp->lnd_ship >= 0 || lp->lnd_land >= 0) &&
114 !(lcp->l_flags & L_MARINE))
117 value = men * lnd_def(lp) * lp->lnd_effic / 100.0;
118 value *= ((double)land_mob_max + lp->lnd_harden) / land_mob_max;
120 /* If there are military on the unit, you get at least a 1
121 man defensive unit, except for spies */
122 if (value < 1.0 && men > 0 && !(lcp->l_flags & L_SPY))
129 lnd_reaction_range(struct lndstr *lp)
133 getsect(lp->lnd_x, lp->lnd_y, §);
134 if (sect.sct_type == SCT_HEADQ && sect.sct_effic >= 60)
135 return lchr[lp->lnd_type].l_rad + 1;
136 return lchr[lp->lnd_type].l_rad;
140 lnd_print(natid actor, struct ulist *llp, char *s)
142 if (actor == player->cnum)
143 pr("%s %s\n", prland(&llp->unit.land), s);
145 wu(0, actor, "%s %s\n", prland(&llp->unit.land), s);
149 lnd_take_casualty(int combat_mode, struct ulist *llp, int cas)
150 /* attacking or assaulting or paratrooping? */
151 /* number of casualties to take */
163 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)
171 llp->unit.land.lnd_effic = 0;
173 eff_eq = ldround(cas * 100.0 /
174 ((struct lchrstr *)llp->chrp)->l_item[I_MILIT], 1);
175 llp->unit.land.lnd_effic -= eff_eq;
176 lnd_submil(&llp->unit.land, cas);
179 if (llp->unit.land.lnd_effic < LAND_MINEFF) {
180 sprintf(buf, "dies %s %s!",
181 combat_mode ? att_mode[combat_mode] : "defending",
182 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
183 llp->unit.land.lnd_own));
184 lnd_print(llp->unit.land.lnd_own, 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 (pct_chance(ret_chance)) {
209 lnd_print(llp->unit.land.lnd_own, 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 * owner. Charge mob..
222 for (n = 1; n <= 6; ++n) {
223 ret_x = llp->unit.land.lnd_x + diroff[n][0];
224 ret_y = llp->unit.land.lnd_y + diroff[n][1];
225 getsect(ret_x, ret_y, §);
226 if (sect.sct_own != llp->unit.land.lnd_own)
228 if (sect.sct_type == SCT_MOUNT)
230 mobcost = lnd_mobcost(&llp->unit.land, §);
233 civs = sect.sct_item[I_CIVIL];
234 if (civs > biggest) {
244 /* retreat to bx,by */
245 llp->unit.land.lnd_x = bx;
246 llp->unit.land.lnd_y = by;
247 /* FIXME landmines */
248 mob = llp->unit.land.lnd_mobil - (int)bmcost;
251 orig = llp->unit.land.lnd_mobil;
252 llp->unit.land.lnd_mobil = (signed char)mob;
253 if (llp->unit.land.lnd_mobil > orig)
254 llp->unit.land.lnd_mobil = -127;
255 sprintf(buf, "retreats at %d%% efficiency to %s!",
256 llp->unit.land.lnd_effic,
257 xyas(bx, by, llp->unit.land.lnd_own));
258 lnd_print(llp->unit.land.lnd_own, llp, buf);
261 } else { /* attacking from a sector */
262 sprintf(buf, "leaves the battlefield at %d%% efficiency",
263 llp->unit.land.lnd_effic);
264 if ((llp->unit.land.lnd_mobil - (int)llp->mobil) < -127)
265 llp->unit.land.lnd_mobil = -127;
267 llp->unit.land.lnd_mobil -= (int)llp->mobil;
269 lnd_print(llp->unit.land.lnd_own, llp, buf);
274 /* nowhere to go.. take more casualties */
275 llp->unit.land.lnd_effic -= 10;
276 lnd_submil(&llp->unit.land,
277 ((struct lchrstr *)llp->chrp)->l_item[I_MILIT] / 10);
278 if (llp->unit.land.lnd_effic < LAND_MINEFF) {
279 lnd_print(llp->unit.land.lnd_own, llp,
280 "has nowhere to retreat, and dies!");
283 lnd_print(llp->unit.land.lnd_own, llp,
284 "has nowhere to retreat and takes extra losses!");
291 lnd_takemob(struct emp_qelem *list, double loss)
293 struct emp_qelem *qp, *next;
296 int mcost = ldround(combat_mob * loss, 1);
298 for (qp = list->q_forw; qp != list; qp = next) {
300 llp = (struct ulist *)qp;
303 use_supply(&llp->unit.land);
305 new = llp->unit.land.lnd_mobil - mcost;
308 llp->unit.land.lnd_mobil = (signed char)new;
313 lnd_submil(struct lndstr *lp, int num)
315 int new = lp->lnd_item[I_MILIT] - num;
316 lp->lnd_item[I_MILIT] = new < 0 ? 0 : new;
320 lnd_spyval(struct lndstr *lp)
322 if (lchr[(int)lp->lnd_type].l_flags & L_RECON)
323 return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0) + 2;
325 return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0);
329 intelligence_report(int destination, struct lndstr *lp, int spy,
332 int vis = lnd_vis(lp);
333 char buf1[80], buf2[80], buf3[80];
335 if (destination == 0)
338 if (lp->lnd_own == 0)
341 memset(buf1, 0, sizeof(buf1));
342 memset(buf2, 0, sizeof(buf2));
343 memset(buf3, 0, sizeof(buf3));
344 if (chance((spy + vis) / 10.0)) {
345 if (destination == player->cnum)
346 pr("%s %s", mess, prland(lp));
348 sprintf(buf1, "%s %s", mess, prland(lp));
350 if (chance((spy + vis) / 20.0)) {
351 if (destination == player->cnum)
352 pr(" (eff %d, mil %d",
353 roundintby(lp->lnd_effic, 5),
354 roundintby(lp->lnd_item[I_MILIT], 10));
356 sprintf(buf2, " (eff %d, mil %d",
357 roundintby(lp->lnd_effic, 5),
358 roundintby(lp->lnd_item[I_MILIT], 10));
360 if (chance((spy + vis) / 20.0)) {
362 t = lp->lnd_tech - 20 + roll(40);
364 if (destination == player->cnum)
365 pr(", tech %d)\n", t);
367 sprintf(buf3, ", tech %d)\n", t);
369 if (destination == player->cnum)
372 sprintf(buf3, ")\n");
375 if (destination == player->cnum)
382 if (destination != player->cnum) {
383 wu(0, destination, "%s%s%s", buf1, buf2, buf3);
388 lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
392 int mobtype = MOB_MOVE; /* indeterminate */
395 while (nxtitem(ni, &land)) {
397 * It would be nice to let deities march foreign land units,
398 * but much of the code assumes that only the land unit's
399 * owner can march it.
401 if (!land.lnd_own || land.lnd_own != player->cnum)
404 if (ontradingblock(EF_LAND, &land)) {
405 pr("unit #%d inelligible - it's for sale.\n",
411 * The marching code gets confused when trains and non-trains
412 * march together. Disallow for now.
414 this_mot = lnd_mobtype(&land);
415 if (this_mot != mobtype) {
416 if (mobtype == MOB_MOVE)
418 else if (mobtype == MOB_MARCH) {
419 pr("%s is a train and can't march with the leader.\n",
423 pr("%s can't rail-march with the leading train.\n",
429 land.lnd_mission = 0;
432 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
433 putland(land.lnd_uid, &land);
434 lnd_insque(&land, list);
440 * Return the new list link.
443 lnd_insque(struct lndstr *lp, struct emp_qelem *list)
445 struct ulist *mlp = malloc(sizeof(struct ulist));
447 mlp->chrp = (struct empobj_chr *)&lchr[lp->lnd_type];
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,
457 int *togetherp, natid actor)
459 struct emp_qelem *qp;
460 struct emp_qelem *next;
472 for (qp = list->q_back; qp != list; qp = next) {
474 llp = (struct ulist *)qp;
475 lp = &llp->unit.land;
476 getland(lp->lnd_uid, lp);
477 if (lp->lnd_own != actor) {
478 mpr(actor, "%s was disbanded at %s\n",
479 prland(lp), xyas(lp->lnd_x, lp->lnd_y, actor));
480 emp_remque(&llp->queue);
484 if (lp->lnd_ship >= 0) {
485 lnd_stays(actor, "is on a ship", llp);
488 if (lp->lnd_land >= 0) {
489 lnd_stays(actor, "is on a unit", llp);
492 if (!getsect(lp->lnd_x, lp->lnd_y, §)) {
493 lnd_stays(actor, "was sucked into the sky by a strange looking spaceland", llp); /* heh -KHS */
496 if (!(lchr[lp->lnd_type].l_flags & L_SPY) &&
497 !(lchr[lp->lnd_type].l_flags & L_TRAIN) &&
498 lp->lnd_item[I_MILIT] == 0) {
499 lnd_stays(actor, "has no mil on it to guide it", llp);
502 if (relations_with(sect.sct_own, actor) != ALLIED &&
503 !(lchr[lp->lnd_type].l_flags & L_SPY) &&
505 sprintf(mess, "has been kidnapped by %s", cname(sect.sct_own));
506 lnd_stays(actor, mess, llp);
514 if (lp->lnd_x != allx || lp->lnd_y != ally)
516 if (lp->lnd_mobil + 1 < (int)llp->mobil) {
517 llp->mobil = lp->lnd_mobil;
519 if (llp->mobil < *minmobp)
520 *minmobp = llp->mobil;
521 if (llp->mobil > *maxmobp)
522 *maxmobp = llp->mobil;
527 lnd_mar_put_one(struct ulist *llp)
529 if (llp->mobil < -127)
531 llp->unit.land.lnd_mobil = llp->mobil;
536 lnd_mar_put(struct emp_qelem *list, natid actor)
538 struct emp_qelem *qp, *next;
542 for (qp = list->q_back; qp != list; qp = next) {
544 llp = (struct ulist *)qp;
545 lp = &llp->unit.land;
546 mpr(actor, "%s stopped at %s\n",
547 prland(lp), xyas(lp->lnd_x, lp->lnd_y, actor));
548 lnd_mar_put_one(llp);
553 lnd_put(struct emp_qelem *list)
555 struct emp_qelem *qp, *next;
557 for (qp = list->q_back; qp != list; qp = next) {
559 lnd_put_one((struct ulist *)qp);
564 lnd_put_one(struct ulist *llp)
566 putland(llp->unit.land.lnd_uid, &llp->unit.land);
567 emp_remque(&llp->queue);
572 * Sweep landmines with engineers in LAND_LIST for ACTOR.
573 * If EXPLICIT is non-zero, this is for an explicit sweep command from
574 * a player. Else it's an automatic "on the move" sweep.
575 * If TAKEMOB is non-zero, require and charge mobility.
578 lnd_sweep(struct emp_qelem *land_list, int explicit, int takemob,
581 struct emp_qelem *qp;
582 struct emp_qelem *next;
585 int mines, m, max, sshells, lshells;
587 for (qp = land_list->q_back; qp != land_list; qp = next) {
589 llp = (struct ulist *)qp;
590 if (!(((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)) {
592 mpr(actor, "%s is not an engineer!\n",
593 prland(&llp->unit.land));
596 if (takemob && llp->mobil < 0.0) {
598 lnd_stays(actor, "is out of mobility", llp);
601 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
602 if (!explicit && relations_with(sect.sct_oldown, actor) == ALLIED)
604 if (SCT_MINES_ARE_SEAMINES(§)) {
606 mpr(actor, "%s is in a %s sector. No landmines there!\n",
607 prland(&llp->unit.land), dchr[sect.sct_type].d_name);
611 llp->mobil -= lnd_pathcost(&llp->unit.land, 0.2);
612 llp->unit.land.lnd_mobil = (int)llp->mobil;
613 llp->unit.land.lnd_harden = 0;
615 putland(llp->unit.land.lnd_uid, &llp->unit.land);
616 if (!(mines = sect.sct_mines))
618 max = ((struct lchrstr *)llp->chrp)->l_item[I_SHELL];
619 lshells = llp->unit.land.lnd_item[I_SHELL];
620 sshells = sect.sct_item[I_SHELL];
621 for (m = 0; mines > 0 && m < max * 2; m++) {
622 if (chance(0.5 * ((struct lchrstr *)llp->chrp)->l_att)) {
623 mpr(actor, "Sweep...\n");
627 else if (sshells < ITEM_MAX)
631 sect.sct_mines = mines;
632 llp->unit.land.lnd_item[I_SHELL] = lshells;
633 sect.sct_item[I_SHELL] = sshells;
634 putland(llp->unit.land.lnd_uid, &llp->unit.land);
640 contains_engineer(struct emp_qelem *list)
642 struct emp_qelem *qp;
643 struct emp_qelem *next;
646 for (qp = list->q_back; qp != list; qp = next) {
648 llp = (struct ulist *)qp;
649 if (((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)
656 lnd_check_mines(struct emp_qelem *land_list)
658 struct emp_qelem *qp;
659 struct emp_qelem *next;
663 int with_eng = contains_engineer(land_list);
665 for (qp = land_list->q_back; qp != land_list; qp = next) {
667 llp = (struct ulist *)qp;
668 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
669 if (SCT_LANDMINES(§) == 0)
671 if (relations_with(sect.sct_oldown, llp->unit.land.lnd_own)
674 if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
675 lnd_hit_mine(&llp->unit.land);
678 putland(llp->unit.land.lnd_uid, &llp->unit.land);
679 if (!llp->unit.land.lnd_own) {
690 lnd_stays(natid actor, char *str, struct ulist *llp)
692 mpr(actor, "%s %s & stays in %s\n",
693 prland(&llp->unit.land), str,
694 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y, actor));
695 lnd_mar_put_one(llp);
698 /* Return whether and why SP would be stuck in SECTP. */
700 lnd_check_mar(struct lndstr *lp, struct sctstr *sectp)
702 if (dchr[sectp->sct_type].d_mob0 < 0)
703 return LND_STUCK_IMPASSABLE;
704 if (lnd_mobtype(lp) == MOB_RAIL && !SCT_HAS_RAIL(sectp))
705 return LND_STUCK_NO_RAIL;
706 return LND_STUCK_NOT;
710 lnd_count(struct emp_qelem *list)
712 struct emp_qelem *qp;
713 struct emp_qelem *next;
716 for (qp = list->q_back; qp != list; qp = next) {
724 lnd_damage(struct emp_qelem *list, int totdam)
726 struct emp_qelem *qp;
727 struct emp_qelem *next;
732 if (!totdam || !(count = lnd_count(list)))
734 dam = ldround((double)totdam / count, 1);
735 for (qp = list->q_back; qp != list; qp = next) {
737 llp = (struct ulist *)qp;
738 /* land unit might have changed (launched SAMs, collateral dmg) */
739 getland(llp->unit.land.lnd_uid, &llp->unit.land);
740 landdamage(&llp->unit.land, dam);
741 putland(llp->unit.land.lnd_uid, &llp->unit.land);
742 if (!llp->unit.land.lnd_own) {
751 lnd_easiest_target(struct emp_qelem *list)
753 struct emp_qelem *qp;
754 struct emp_qelem *next;
757 int easiest = 9876; /* things start great for victim */
760 for (qp = list->q_back; qp != list; qp = next) {
762 llp = (struct ulist *)qp;
763 hard = lnd_hardtarget(&llp->unit.land);
765 easiest = hard; /* things get worse for victim */
768 return easiest - count;
772 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
775 int mindam = lnd_count(list) * 20;
776 int hardtarget = lnd_easiest_target(list);
777 int dam, newdam, sublaunch;
780 struct emp_qelem msl_list, *qp, *newqp;
782 msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
785 for (qp = msl_list.q_back; qp != &msl_list; qp = newqp) {
787 plp = (struct plist *)qp;
789 if (dam < mindam && mission_pln_equip(plp, NULL, 'p') >= 0) {
790 if (msl_launch(&plp->plane, EF_LAND, "troops",
791 newx, newy, victim, &sublaunch) < 0)
794 if (msl_hit(&plp->plane, hardtarget, EF_LAND,
795 N_LND_MISS, N_LND_SMISS, sublaunch, victim)) {
796 newdam = pln_damage(&plp->plane, 'p', 1);
799 newdam = pln_damage(&plp->plane, 'p', 0);
800 collateral_damage(newx, newy, newdam);
803 plp->plane.pln_effic = 0;
804 putplane(plp->plane.pln_uid, &plp->plane);
811 mpr(victim, "missile interdiction mission does %d damage!\n", dam);
812 collateral_damage(newx, newy, dam);
813 lnd_damage(list, dam);
819 /* Steve M. - commented out for now until abuse is decided upon */
820 /* risner: allow forts to interdict land units. */
822 lnd_fort_interdiction(struct emp_qelem *list,
823 coord newx, coord newy, natid victim)
832 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
833 while (nxtsct(&ns, &fsect)) {
834 if (fsect.sct_own == 0)
836 if (relations_with(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);
848 mpr(victim, "Incoming fire does %d damage!\n", dam);
850 "%s fires at %s land units in %s for %d!\n",
851 xyas(fsect.sct_x, fsect.sct_y,
853 cname(victim), xyas(newx, newy, fsect.sct_own), dam);
854 nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
857 lnd_damage(list, totdam);
863 lnd_mission_interdiction(struct emp_qelem *list, coord x, coord y,
868 dam = unit_interdict(x, y, victim, "land units",
869 lnd_easiest_target(list),
872 lnd_damage(list, dam);
877 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
882 if (!opt_NO_FORT_FIRE)
883 /* Steve M. - commented out for now until abuse is decided upon */
884 stopping |= lnd_fort_interdiction(list, newx, newy, victim);
887 stopping |= lnd_mission_interdiction(list, newx, newy, victim);
888 stopping |= lnd_missile_interdiction(list, newx, newy, victim);
892 /* high value of hardtarget is harder to hit */
894 lnd_hardtarget(struct lndstr *lp)
898 getsect(lp->lnd_x, lp->lnd_y, §);
899 return (int)((lp->lnd_effic / 100.0) *
900 (10 + dchr[sect.sct_type].d_dstr * 2 + lnd_spd(lp) / 2.0
905 lnd_hit_mine(struct lndstr *lp)
909 mpr(lp->lnd_own, "Blammo! Landmines detected in %s!\n",
910 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
912 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
915 if (lchr[lp->lnd_type].l_flags & L_ENGINEER)
923 lnd_pathcost(struct lndstr *lp, double pathcost)
927 effspd = lnd_spd(lp);
928 if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
929 effspd *= lp->lnd_effic * 0.01;
932 * The return value must be PATHCOST times a factor that depends
933 * only on the land unit. Anything else breaks path finding. In
934 * particular, you can't add or enforce a minimum cost here. Do
935 * it in sector_mcost().
937 return pathcost * 5.0 * speed_factor(effspd, lp->lnd_tech);
941 lnd_mobtype(struct lndstr *lp)
943 return (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
944 ? MOB_RAIL : MOB_MARCH;
948 lnd_mobcost(struct lndstr *lp, struct sctstr *sp)
950 return lnd_pathcost(lp, sector_mcost(sp, lnd_mobtype(lp)));
954 lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor,
957 struct sctstr sect, osect;
958 struct emp_qelem *qp;
959 struct emp_qelem *qp2;
960 struct emp_qelem *next;
962 struct emp_qelem cur, done;
967 enum lnd_stuck stuck;
975 if (dir <= DIR_STOP || dir >= DIR_VIEW) {
976 lnd_mar_put(list, actor);
981 for (qp = list->q_back; qp != list; qp = next) {
983 llp = (struct ulist *)qp;
984 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &osect);
985 oldown = osect.sct_own;
986 newx = xnorm(llp->unit.land.lnd_x + dx);
987 newy = ynorm(llp->unit.land.lnd_y + dy);
988 getsect(newx, newy, §);
989 rel = relations_with(sect.sct_own, actor);
990 stuck = lnd_check_mar(&llp->unit.land, §);
991 if (stuck != LND_STUCK_NOT
992 || (sect.sct_own && rel != ALLIED
993 && !(lchr[llp->unit.land.lnd_type].l_flags & L_SPY))) {
994 if (stuck == LND_STUCK_NO_RAIL
995 && (!sect.sct_own || rel == ALLIED)) {
997 mpr(actor, "no rail system in %s\n",
998 xyas(newx, newy, actor));
1001 sprintf(dp, "has no rail system in %s",
1002 xyas(newx, newy, actor));
1003 lnd_stays(actor, dp, llp);
1008 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1011 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
1012 lnd_stays(actor, dp, llp);
1017 /* Note we check would_abandon first because we don't want
1018 to always have to do these checks */
1019 if (would_abandon(&osect, I_CIVIL, 0, &llp->unit.land)) {
1021 if (!want_to_abandon(&osect, I_CIVIL, 0, &llp->unit.land)) {
1024 /* now check stuff */
1025 if (!check_sect_ok(§))
1027 if (!check_sect_ok(&osect))
1029 for (qp2 = list->q_back; qp2 != list; qp2 = qp2->q_back) {
1030 if (!check_land_ok(&((struct ulist *)qp2)->unit.land))
1034 lnd_stays(actor, "stops", llp);
1038 if (llp->mobil <= 0.0) {
1039 lnd_stays(actor, "is out of mobility", llp);
1042 llp->unit.land.lnd_x = newx;
1043 llp->unit.land.lnd_y = newy;
1044 llp->mobil -= lnd_mobcost(&llp->unit.land, §);
1045 llp->unit.land.lnd_mobil = (int)llp->mobil;
1046 llp->unit.land.lnd_harden = 0;
1047 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1049 getsect(osect.sct_x, osect.sct_y, &osect);
1050 if (osect.sct_own != oldown && oldown == actor) {
1051 /* It was your sector, now it's not. Simple :) */
1052 mpr(actor, "You no longer own %s\n",
1053 xyas(osect.sct_x, osect.sct_y, actor));
1055 if (rel != ALLIED && sect.sct_own) { /* must be a spy */
1056 /* Always a 10% chance of getting caught. */
1057 if (chance(LND_SPY_DETECT_CHANCE(llp->unit.land.lnd_effic))) {
1058 if (rel == NEUTRAL || rel == FRIENDLY) {
1060 "%s unit spotted in %s\n", cname(actor),
1061 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1062 setrel(sect.sct_own, llp->unit.land.lnd_own, HOSTILE);
1063 } else if (rel <= HOSTILE) {
1065 "%s spy shot in %s\n", cname(actor),
1066 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1067 mpr(actor, "%s was shot and killed.\n",
1068 prland(&llp->unit.land));
1069 llp->unit.land.lnd_effic = 0;
1070 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1078 lnd_sweep(list, 0, 1, actor);
1079 stopping |= lnd_check_mines(list);
1083 /* interdict land units sector by sector */
1086 while (!QEMPTY(list)) {
1087 llp = (struct ulist *)list->q_back;
1088 newx = llp->unit.land.lnd_x;
1089 newy = llp->unit.land.lnd_y;
1090 /* move units in NEWX,NEWY to cur */
1092 for (qp = list->q_back; qp != list; qp = next) {
1094 llp = (struct ulist *)qp;
1095 if (llp->unit.land.lnd_x == newx && llp->unit.land.lnd_y == newy) {
1097 emp_insque(qp, &cur);
1098 if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY))
1102 /* interdict them */
1104 stopping |= lnd_interdict(&cur, newx, newy, actor);
1105 /* move survivors to done */
1106 for (qp = cur.q_back; qp != &cur; qp = next) {
1109 emp_insque(qp, &done);
1112 /* assign surviving land units back to list */
1113 emp_insque(list, &done);
1120 * Fire land unit support against VICTIM for ATTACKER, at X,Y.
1121 * If DEFENDING, this is defensive support, else offensive support.
1122 * Return total damage.
1125 lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
1127 struct nstr_item ni;
1134 snxtitem_all(&ni, EF_LAND);
1135 while (nxtitem(&ni, &land)) {
1136 if ((land.lnd_x == x) && (land.lnd_y == y))
1138 if (!feels_like_helping(land.lnd_own, attacker, victim))
1141 /* are we in range? */
1142 dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1144 range = roundrange(lnd_fire_range(&land));
1148 dam2 = lnd_fire(&land);
1149 putland(land.lnd_uid, &land);
1154 nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
1156 nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1157 if (pct_chance(lnd_acc(&land) - 1))
1160 if (land.lnd_own != attacker)
1162 "%s supported %s at %s\n",
1163 prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1169 lnd_can_attack(struct lndstr *lp)
1171 struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1173 if (lcp->l_flags & L_SUPPLY)
1180 * Increase fortification value of LP.
1181 * Fortification costs mobility. Use up to MOB mobility.
1182 * Return actual fortification increase.
1185 lnd_fortify(struct lndstr *lp, int mob)
1188 double mob_used, mult;
1190 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
1193 mob_used = MIN(lp->lnd_mobil, mob);
1197 mult = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own)
1200 hard_amt = (int)(mob_used * mult);
1201 if (lp->lnd_harden + hard_amt > land_mob_max) {
1202 hard_amt = land_mob_max - lp->lnd_harden;
1203 mob_used = ceil(hard_amt / mult);
1206 lp->lnd_mobil -= (int)mob_used;
1207 lp->lnd_harden += hard_amt;
1208 lp->lnd_harden = MIN(lp->lnd_harden, land_mob_max);
1214 * Is there a engineer unit at X,Y that can help nation CN?
1217 has_helpful_engineer(coord x, coord y, natid cn)
1219 struct nstr_item ni;
1222 snxtitem_xy(&ni, EF_LAND, x, y);
1223 while (nxtitem(&ni, &land)) {
1224 if (relations_with(land.lnd_own, cn) != ALLIED)
1226 if (lchr[(int)land.lnd_type].l_flags & L_ENGINEER)
1234 * Set LP's tech to TLEV along with everything else that depends on it.
1237 lnd_set_tech(struct lndstr *lp, int tlev)
1239 struct lchrstr *lcp = lchr + lp->lnd_type;
1241 if (CANT_HAPPEN(tlev < lcp->l_tech))
1244 lp->lnd_tech = tlev;