2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, 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-2009
48 #include "prototypes.h"
53 static void lnd_stays(natid, char *, struct ulist *);
54 static int lnd_hit_mine(struct lndstr *);
55 static int has_helpful_engineer(coord, coord, natid);
58 attack_val(int combat_mode, struct lndstr *lp)
64 if (lp->lnd_effic < LAND_MINEFF) {
65 putland(lp->lnd_uid, lp);
69 lcp = &lchr[(int)lp->lnd_type];
71 /* Spies always count as 1 during assaults. If they are the only ones
72 in the assault, they get to sneak on anyway. */
74 if (lcp->l_flags & L_SPY && combat_mode == A_ASSAULT)
77 men = lp->lnd_item[I_MILIT];
78 value = men * lnd_att(lp) * lp->lnd_effic / 100.0;
80 switch (combat_mode) {
84 if (!(lcp->l_flags & L_MARINE))
85 return assault_penalty * value;
88 if (!(lcp->l_flags & L_MARINE))
89 return assault_penalty * men;
96 defense_val(struct lndstr *lp)
102 if (lp->lnd_effic < LAND_MINEFF) {
103 putland(lp->lnd_uid, lp);
107 lcp = &lchr[(int)lp->lnd_type];
109 men = lp->lnd_item[I_MILIT];
111 if ((lp->lnd_ship >= 0 || lp->lnd_land >= 0) &&
112 !(lcp->l_flags & L_MARINE))
115 value = men * lnd_def(lp) * lp->lnd_effic / 100.0;
116 value *= ((double)land_mob_max + lp->lnd_harden) / land_mob_max;
118 /* If there are military on the unit, you get at least a 1
119 man defensive unit, except for spies */
120 if (value < 1.0 && men > 0 && !(lcp->l_flags & L_SPY))
127 lnd_reaction_range(struct lndstr *lp)
131 getsect(lp->lnd_x, lp->lnd_y, §);
132 if (sect.sct_type == SCT_HEADQ && sect.sct_effic >= 60)
133 return lchr[lp->lnd_type].l_rad + 1;
134 return lchr[lp->lnd_type].l_rad;
138 lnd_print(natid actor, struct ulist *llp, char *s)
140 if (actor == player->cnum)
141 pr("%s %s\n", prland(&llp->unit.land), s);
143 wu(0, actor, "%s %s\n", prland(&llp->unit.land), s);
147 lnd_delete(struct ulist *llp)
149 putland(llp->unit.land.lnd_uid, &llp->unit.land);
150 emp_remque((struct emp_qelem *)llp);
155 lnd_take_casualty(int combat_mode, struct ulist *llp, int cas)
156 /* attacking or assaulting or paratrooping? */
157 /* number of casualties to take */
170 int nowhere_to_go = 0;
172 double mobcost, bmcost;
176 taken = llp->unit.land.lnd_item[I_MILIT];
177 /* Spies always die */
178 if (((struct lchrstr *)llp->chrp)->l_flags & L_SPY)
179 llp->unit.land.lnd_effic = 0;
181 eff_eq = ldround(cas * 100.0 /
182 ((struct lchrstr *)llp->chrp)->l_item[I_MILIT], 1);
183 llp->unit.land.lnd_effic -= eff_eq;
184 lnd_submil(&llp->unit.land, cas);
187 if (llp->unit.land.lnd_effic < LAND_MINEFF) {
188 sprintf(buf, "dies %s %s!",
189 combat_mode ? att_mode[combat_mode] : "defending",
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 (roll(100) < ret_chance) {
217 lnd_print(llp->unit.land.lnd_own, llp, "fails morale check!");
218 llp->unit.land.lnd_mission = 0;
219 llp->unit.land.lnd_harden = 0;
220 if (llp->unit.land.lnd_ship >= 0 || llp->unit.land.lnd_land >= 0)
222 else if (combat_mode == A_DEFEND) {
224 * defending unit.. find a place to send it
225 * strategy: look for the most-populated
226 * adjacent sector that is owned by the unit
227 * 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, &rsect);
243 civs = sect.sct_item[I_CIVIL];
244 if (civs > biggest) {
254 /* retreat to bx,by */
255 llp->unit.land.lnd_x = bx;
256 llp->unit.land.lnd_y = by;
257 /* FIXME landmines */
258 getsect(bx, by, &rsect);
259 mob = llp->unit.land.lnd_mobil - (int)bmcost;
262 orig = llp->unit.land.lnd_mobil;
263 llp->unit.land.lnd_mobil = (signed char)mob;
264 if (llp->unit.land.lnd_mobil > orig)
265 llp->unit.land.lnd_mobil = -127;
266 sprintf(buf, "retreats at %d%% efficiency to %s!",
267 llp->unit.land.lnd_effic,
268 xyas(bx, by, llp->unit.land.lnd_own));
269 lnd_print(llp->unit.land.lnd_own, llp, buf);
272 } else { /* attacking from a sector */
273 sprintf(buf, "leaves the battlefield at %d%% efficiency",
274 llp->unit.land.lnd_effic);
275 if ((llp->unit.land.lnd_mobil - (int)llp->mobil) < -127)
276 llp->unit.land.lnd_mobil = -127;
278 llp->unit.land.lnd_mobil -= (int)llp->mobil;
280 lnd_print(llp->unit.land.lnd_own, llp, buf);
285 /* nowhere to go.. take more casualties */
286 llp->unit.land.lnd_effic -= 10;
287 lnd_submil(&llp->unit.land,
288 ((struct lchrstr *)llp->chrp)->l_item[I_MILIT] / 10);
289 if (llp->unit.land.lnd_effic < LAND_MINEFF) {
290 lnd_print(llp->unit.land.lnd_own, llp,
291 "has nowhere to retreat, and dies!");
294 lnd_print(llp->unit.land.lnd_own, llp,
295 "has nowhere to retreat and takes extra losses!");
302 lnd_takemob(struct emp_qelem *list, double loss)
304 struct emp_qelem *qp, *next;
307 int mcost = ldround(combat_mob * loss, 1);
309 for (qp = list->q_forw; qp != list; qp = next) {
311 llp = (struct ulist *)qp;
314 use_supply(&llp->unit.land);
316 new = llp->unit.land.lnd_mobil - mcost;
319 llp->unit.land.lnd_mobil = (signed char)new;
324 lnd_submil(struct lndstr *lp, int num)
326 int new = lp->lnd_item[I_MILIT] - num;
327 lp->lnd_item[I_MILIT] = new < 0 ? 0 : new;
331 lnd_spyval(struct lndstr *lp)
333 if (lchr[(int)lp->lnd_type].l_flags & L_RECON)
334 return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0) + 2;
336 return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0);
340 intelligence_report(int destination, struct lndstr *lp, int spy,
343 int vis = lnd_vis(lp);
344 char buf1[80], buf2[80], buf3[80];
346 if (destination == 0)
349 if (lp->lnd_own == 0)
352 memset(buf1, 0, sizeof(buf1));
353 memset(buf2, 0, sizeof(buf2));
354 memset(buf3, 0, sizeof(buf3));
355 if (chance((spy + vis) / 10.0)) {
356 if (destination == player->cnum)
357 pr("%s %s", mess, prland(lp));
359 sprintf(buf1, "%s %s", mess, prland(lp));
361 if (chance((spy + vis) / 20.0)) {
362 if (destination == player->cnum)
363 pr(" (eff %d, mil %d",
364 roundintby(lp->lnd_effic, 5),
365 roundintby(lp->lnd_item[I_MILIT], 10));
367 sprintf(buf2, " (eff %d, mil %d",
368 roundintby(lp->lnd_effic, 5),
369 roundintby(lp->lnd_item[I_MILIT], 10));
371 if (chance((spy + vis) / 20.0)) {
373 t = lp->lnd_tech - 20 + roll(40);
375 if (destination == player->cnum)
376 pr(", tech %d)\n", t);
378 sprintf(buf3, ", tech %d)\n", t);
380 if (destination == player->cnum)
383 sprintf(buf3, ")\n");
386 if (destination == player->cnum)
393 if (destination != player->cnum) {
394 wu(0, destination, "%s%s%s", buf1, buf2, buf3);
399 lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
405 int mobtype = MOB_MOVE; /* indeterminate */
408 while (nxtitem(ni, &land)) {
410 * It would be nice to let deities march foreign land units,
411 * but much of the code assumes that only the land unit's
412 * owner can march it.
414 if (land.lnd_own != player->cnum)
417 if (ontradingblock(EF_LAND, &land)) {
418 pr("unit #%d inelligible - it's for sale.\n",
424 * The marching code gets confused when trains and non-trains
425 * march together. Disallow for now.
427 this_mot = lnd_mobtype(&land);
428 if (this_mot != mobtype) {
429 if (mobtype == MOB_MOVE)
431 else if (mobtype == MOB_MARCH) {
432 pr("%s is a train and can't march with the leader.\n",
436 pr("%s can't rail-march with the leading train.\n",
442 lcp = &lchr[(int)land.lnd_type];
443 land.lnd_mission = 0;
446 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
447 putland(land.lnd_uid, &land);
448 llp = malloc(sizeof(struct ulist));
449 llp->chrp = (struct empobj_chr *)lcp;
450 llp->unit.land = land;
451 llp->mobil = land.lnd_mobil;
452 emp_insque(&llp->queue, list);
456 /* This function assumes that the list was created by lnd_sel */
458 lnd_mar(struct emp_qelem *list, double *minmobp, double *maxmobp,
459 int *togetherp, natid actor)
461 struct emp_qelem *qp;
462 struct emp_qelem *next;
474 for (qp = list->q_back; qp != list; qp = next) {
476 llp = (struct ulist *)qp;
477 getland(llp->unit.land.lnd_uid, &land);
478 if (land.lnd_own != actor) {
479 mpr(actor, "%s was disbanded at %s\n",
480 prland(&land), xyas(land.lnd_x, land.lnd_y, actor));
481 emp_remque((struct emp_qelem *)llp);
485 if (land.lnd_ship >= 0) {
486 lnd_stays(actor, "is on a ship", llp);
489 if (land.lnd_land >= 0) {
490 lnd_stays(actor, "is on a unit", llp);
493 if (!getsect(land.lnd_x, land.lnd_y, §)) {
494 lnd_stays(actor, "was sucked into the sky by a strange looking spaceland", llp); /* heh -KHS */
497 if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY) &&
498 !(lchr[(int)llp->unit.land.lnd_type].l_flags & L_TRAIN) &&
499 llp->unit.land.lnd_item[I_MILIT] == 0) {
500 lnd_stays(actor, "has no mil on it to guide it", llp);
503 if (relations_with(sect.sct_own, actor) != ALLIED &&
504 !(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY) &&
506 sprintf(mess, "has been kidnapped by %s", cname(sect.sct_own));
507 lnd_stays(actor, mess, llp);
515 if (land.lnd_x != allx || land.lnd_y != ally)
517 if (land.lnd_mobil + 1 < (int)llp->mobil) {
518 llp->mobil = land.lnd_mobil;
520 if (llp->mobil < *minmobp)
521 *minmobp = llp->mobil;
522 if (llp->mobil > *maxmobp)
523 *maxmobp = llp->mobil;
524 llp->unit.land = land;
529 lnd_sweep(struct emp_qelem *land_list, int verbose, int takemob,
532 struct emp_qelem *qp;
533 struct emp_qelem *next;
536 int mines, m, max, sshells, lshells;
538 for (qp = land_list->q_back; qp != land_list; qp = next) {
540 llp = (struct ulist *)qp;
541 if (!(((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)) {
543 mpr(actor, "%s is not an engineer!\n",
544 prland(&llp->unit.land));
547 if (takemob && llp->mobil < 0.0) {
549 lnd_stays(actor, "is out of mobility", llp);
552 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
553 if (sect.sct_oldown == llp->unit.land.lnd_own) {
556 "%s is in a sector completely owned by you. Don't bother digging up mines there!\n",
557 prland(&llp->unit.land));
560 if (SCT_MINES_ARE_SEAMINES(§)) {
562 mpr(actor, "%s is in a %s sector. No landmines there!\n",
563 prland(&llp->unit.land), dchr[sect.sct_type].d_name);
567 llp->mobil -= lnd_pathcost(&llp->unit.land, 0.2);
568 llp->unit.land.lnd_mobil = (int)llp->mobil;
569 llp->unit.land.lnd_harden = 0;
571 putland(llp->unit.land.lnd_uid, &llp->unit.land);
572 if (!(mines = sect.sct_mines))
574 max = ((struct lchrstr *)llp->chrp)->l_item[I_SHELL];
575 lshells = llp->unit.land.lnd_item[I_SHELL];
576 sshells = sect.sct_item[I_SHELL];
577 for (m = 0; mines > 0 && m < max * 2; m++) {
578 if (chance(0.5 * ((struct lchrstr *)llp->chrp)->l_att)) {
579 mpr(actor, "Sweep...\n");
583 else if (sshells < ITEM_MAX)
587 sect.sct_mines = mines;
588 llp->unit.land.lnd_item[I_SHELL] = lshells;
589 sect.sct_item[I_SHELL] = sshells;
590 putland(llp->unit.land.lnd_uid, &llp->unit.land);
596 contains_engineer(struct emp_qelem *list)
598 struct emp_qelem *qp;
599 struct emp_qelem *next;
602 for (qp = list->q_back; qp != list; qp = next) {
604 llp = (struct ulist *)qp;
605 if (((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)
612 lnd_check_mines(struct emp_qelem *land_list)
614 struct emp_qelem *qp;
615 struct emp_qelem *next;
619 int with_eng = contains_engineer(land_list);
621 for (qp = land_list->q_back; qp != land_list; qp = next) {
623 llp = (struct ulist *)qp;
624 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
625 if (SCT_LANDMINES(§) == 0)
627 if (relations_with(sect.sct_oldown, llp->unit.land.lnd_own)
630 if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
631 lnd_hit_mine(&llp->unit.land);
634 putland(llp->unit.land.lnd_uid, &llp->unit.land);
635 if (!llp->unit.land.lnd_own) {
646 lnd_stays(natid actor, char *str, struct ulist *llp)
648 mpr(actor, "%s %s & stays in %s\n",
649 prland(&llp->unit.land), str,
650 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y, actor));
651 if (llp->mobil < -127)
653 llp->unit.land.lnd_mobil = llp->mobil;
658 lnd_count(struct emp_qelem *list)
660 struct emp_qelem *qp;
661 struct emp_qelem *next;
664 for (qp = list->q_back; qp != list; qp = next) {
672 lnd_damage(struct emp_qelem *list, int totdam)
674 struct emp_qelem *qp;
675 struct emp_qelem *next;
680 if (!totdam || !(count = lnd_count(list)))
682 dam = ldround((double)totdam / count, 1);
683 for (qp = list->q_back; qp != list; qp = next) {
685 llp = (struct ulist *)qp;
686 /* land unit might have changed (launched SAMs, collateral dmg) */
687 getland(llp->unit.land.lnd_uid, &llp->unit.land);
688 landdamage(&llp->unit.land, dam);
689 putland(llp->unit.land.lnd_uid, &llp->unit.land);
690 if (!llp->unit.land.lnd_own) {
699 lnd_easiest_target(struct emp_qelem *list)
701 struct emp_qelem *qp;
702 struct emp_qelem *next;
705 int easiest = 9876; /* things start great for victim */
708 for (qp = list->q_back; qp != list; qp = next) {
710 llp = (struct ulist *)qp;
711 hard = lnd_hardtarget(&llp->unit.land);
713 easiest = hard; /* things get worse for victim */
716 return easiest - count;
720 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
723 int mindam = lnd_count(list) * 20;
724 int hardtarget = lnd_easiest_target(list);
725 int dam, newdam, sublaunch;
728 struct emp_qelem msl_list, *qp, *newqp;
730 msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
733 for (qp = msl_list.q_back; qp != &msl_list; qp = newqp) {
735 plp = (struct plist *)qp;
737 if (dam < mindam && mission_pln_equip(plp, NULL, 'p') >= 0) {
738 if (msl_launch(&plp->plane, EF_LAND, "troops",
739 newx, newy, victim, &sublaunch) < 0)
742 if (msl_hit(&plp->plane, hardtarget, EF_LAND,
743 N_LND_MISS, N_LND_SMISS, sublaunch, victim)) {
744 newdam = pln_damage(&plp->plane, 'p', 1);
747 newdam = pln_damage(&plp->plane, 'p', 0);
748 collateral_damage(newx, newy, newdam);
751 plp->plane.pln_effic = 0;
752 putplane(plp->plane.pln_uid, &plp->plane);
759 mpr(victim, "missile interdiction mission does %d damage!\n", dam);
760 collateral_damage(newx, newy, dam);
761 lnd_damage(list, dam);
767 /* Steve M. - commented out for now until abuse is decided upon */
768 /* risner: allow forts to interdict land units. */
770 lnd_fort_interdiction(struct emp_qelem *list,
771 coord newx, coord newy, natid victim)
780 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
781 while (nxtsct(&ns, &fsect)) {
782 if (fsect.sct_own == 0)
784 if (relations_with(fsect.sct_own, victim) >= NEUTRAL)
786 range = roundrange(fortrange(&fsect));
787 trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
790 dam = fort_fire(&fsect);
796 mpr(victim, "Incoming fire does %d damage!\n", dam);
798 "%s fires at %s land units in %s for %d!\n",
799 xyas(fsect.sct_x, fsect.sct_y,
801 cname(victim), xyas(newx, newy, fsect.sct_own), dam);
802 nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
805 lnd_damage(list, totdam);
811 lnd_mission_interdiction(struct emp_qelem *list, coord x, coord y,
816 dam = unit_interdict(x, y, victim, "land units",
817 lnd_easiest_target(list),
820 lnd_damage(list, dam);
825 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
830 if (!opt_NO_FORT_FIRE)
831 /* Steve M. - commented out for now until abuse is decided upon */
832 stopping |= lnd_fort_interdiction(list, newx, newy, victim);
835 stopping |= lnd_mission_interdiction(list, newx, newy, victim);
836 stopping |= lnd_missile_interdiction(list, newx, newy, victim);
840 /* high value of hardtarget is harder to hit */
842 lnd_hardtarget(struct lndstr *lp)
846 getsect(lp->lnd_x, lp->lnd_y, §);
847 return (int)((lp->lnd_effic / 100.0) *
848 (10 + dchr[sect.sct_type].d_dstr * 2 + lnd_spd(lp) / 2.0
853 lnd_hit_mine(struct lndstr *lp)
857 mpr(lp->lnd_own, "Blammo! Landmines detected in %s!\n",
858 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
860 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
863 if (lchr[lp->lnd_type].l_flags & L_ENGINEER)
871 lnd_pathcost(struct lndstr *lp, double pathcost)
875 effspd = lnd_spd(lp);
876 if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
877 effspd *= lp->lnd_effic * 0.01;
880 * The return value must be PATHCOST times a factor that depends
881 * only on the land unit. Anything else breaks path finding. In
882 * particular, you can't add or enforce a minimum cost here. Do
883 * it in sector_mcost().
885 return pathcost * 5.0 * speed_factor(effspd, lp->lnd_tech);
889 lnd_mobtype(struct lndstr *lp)
891 return (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
892 ? MOB_RAIL : MOB_MARCH;
896 lnd_mobcost(struct lndstr *lp, struct sctstr *sp)
898 return lnd_pathcost(lp, sector_mcost(sp, lnd_mobtype(lp)));
902 lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor,
905 struct sctstr sect, osect;
906 struct emp_qelem *qp;
907 struct emp_qelem *qp2;
908 struct emp_qelem *next;
910 struct emp_qelem cur, done;
922 if (dir <= DIR_STOP || dir >= DIR_VIEW) {
923 unit_put(list, actor);
928 for (qp = list->q_back; qp != list; qp = next) {
930 llp = (struct ulist *)qp;
931 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, &osect);
932 oldown = osect.sct_own;
933 newx = xnorm(llp->unit.land.lnd_x + dx);
934 newy = ynorm(llp->unit.land.lnd_y + dy);
935 getsect(newx, newy, §);
936 rel = relations_with(sect.sct_own, actor);
937 if ((rel != ALLIED &&
938 !(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY) &&
939 sect.sct_own) || (sect.sct_type == SCT_WATER ||
940 sect.sct_type == SCT_SANCT ||
941 sect.sct_type == SCT_WASTE)) {
943 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
946 sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
947 lnd_stays(actor, dp, llp);
951 if (!SCT_HAS_RAIL(§)
952 && lnd_mobtype(&llp->unit.land) == MOB_RAIL) {
954 mpr(actor, "no rail system in %s\n",
955 xyas(newx, newy, actor));
958 sprintf(dp, "has no rail system in %s",
959 xyas(newx, newy, actor));
960 lnd_stays(actor, dp, llp);
964 /* Note we check would_abandon first because we don't want
965 to always have to do these checks */
966 if (would_abandon(&osect, I_CIVIL, 0, &llp->unit.land)) {
968 if (!want_to_abandon(&osect, I_CIVIL, 0, &llp->unit.land)) {
971 /* now check stuff */
972 if (!check_sect_ok(§))
974 if (!check_sect_ok(&osect))
976 for (qp2 = list->q_back; qp2 != list; qp2 = qp2->q_back) {
977 if (!check_land_ok(&((struct ulist *)qp2)->unit.land))
981 lnd_stays(actor, "stops", llp);
985 if (llp->mobil <= 0.0) {
986 lnd_stays(actor, "is out of mobility", llp);
989 llp->unit.land.lnd_x = newx;
990 llp->unit.land.lnd_y = newy;
991 llp->mobil -= lnd_mobcost(&llp->unit.land, §);
992 llp->unit.land.lnd_mobil = (int)llp->mobil;
993 llp->unit.land.lnd_harden = 0;
994 putland(llp->unit.land.lnd_uid, &llp->unit.land);
996 getsect(osect.sct_x, osect.sct_y, &osect);
997 if (osect.sct_own != oldown && oldown == actor) {
998 /* It was your sector, now it's not. Simple :) */
999 mpr(actor, "You no longer own %s\n",
1000 xyas(osect.sct_x, osect.sct_y, actor));
1002 if (rel != ALLIED && sect.sct_own) { /* must be a spy */
1003 /* Always a 10% chance of getting caught. */
1004 if (chance(LND_SPY_DETECT_CHANCE(llp->unit.land.lnd_effic))) {
1005 if (rel == NEUTRAL || rel == FRIENDLY) {
1007 "%s unit spotted in %s\n", cname(actor),
1008 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1009 setrel(sect.sct_own, llp->unit.land.lnd_own, HOSTILE);
1010 } else if (rel <= HOSTILE) {
1012 "%s spy shot in %s\n", cname(actor),
1013 xyas(sect.sct_x, sect.sct_y, sect.sct_own));
1014 mpr(actor, "%s was shot and killed.\n",
1015 prland(&llp->unit.land));
1016 llp->unit.land.lnd_effic = 0;
1017 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1025 lnd_sweep(list, 0, 1, actor);
1026 stopping |= lnd_check_mines(list);
1030 /* interdict land units sector by sector */
1033 while (!QEMPTY(list)) {
1034 llp = (struct ulist *)list->q_back;
1035 newx = llp->unit.land.lnd_x;
1036 newy = llp->unit.land.lnd_y;
1037 /* move units in NEWX,NEWY to cur */
1039 for (qp = list->q_back; qp != list; qp = next) {
1041 llp = (struct ulist *)qp;
1042 if (llp->unit.land.lnd_x == newx && llp->unit.land.lnd_y == newy) {
1044 emp_insque(qp, &cur);
1045 if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY))
1049 /* interdict them */
1051 stopping |= lnd_interdict(&cur, newx, newy, actor);
1052 /* move survivors to done */
1053 for (qp = cur.q_back; qp != &cur; qp = next) {
1056 emp_insque(qp, &done);
1059 /* assign surviving land units back to list */
1060 emp_insque(list, &done);
1067 * Fire land unit support against VICTIM for ATTACKER, at X,Y.
1068 * If DEFENDING, this is defensive support, else offensive support.
1069 * Return total damage.
1072 lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
1074 struct nstr_item ni;
1081 snxtitem_all(&ni, EF_LAND);
1082 while (nxtitem(&ni, &land)) {
1083 if ((land.lnd_x == x) && (land.lnd_y == y))
1085 if (!feels_like_helping(land.lnd_own, attacker, victim))
1088 /* are we in range? */
1089 dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1091 range = roundrange(lnd_fire_range(&land));
1095 dam2 = lnd_fire(&land);
1096 putland(land.lnd_uid, &land);
1101 nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
1103 nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1104 if (roll(100) < lnd_acc(&land))
1107 if (land.lnd_own != attacker)
1109 "%s supported %s at %s\n",
1110 prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1116 lnd_can_attack(struct lndstr *lp)
1118 struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1120 if (lcp->l_flags & L_SUPPLY)
1127 * Increase fortification value of LP.
1128 * Fortification costs mobility. Use up to MOB mobility.
1129 * Return actual fortification increase.
1132 lnd_fortify(struct lndstr *lp, int mob)
1135 double mob_used, mult;
1137 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
1140 mob_used = MIN(lp->lnd_mobil, mob);
1144 mult = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own)
1147 hard_amt = (int)(mob_used * mult);
1148 if (lp->lnd_harden + hard_amt > land_mob_max) {
1149 hard_amt = land_mob_max - lp->lnd_harden;
1150 mob_used = ceil(hard_amt / mult);
1153 lp->lnd_mobil -= (int)mob_used;
1154 lp->lnd_harden += hard_amt;
1155 lp->lnd_harden = MIN(lp->lnd_harden, land_mob_max);
1161 * Is there a engineer unit at X,Y that can help nation CN?
1164 has_helpful_engineer(coord x, coord y, natid cn)
1166 struct nstr_item ni;
1169 snxtitem_xy(&ni, EF_LAND, x, y);
1170 while (nxtitem(&ni, &land)) {
1171 if (relations_with(land.lnd_own, cn) != ALLIED)
1173 if (lchr[(int)land.lnd_type].l_flags & L_ENGINEER)
1181 * Set LP's tech to TLEV along with everything else that depends on it.
1184 lnd_set_tech(struct lndstr *lp, int tlev)
1186 struct lchrstr *lcp = lchr + lp->lnd_type;
1188 if (CANT_HAPPEN(tlev < lcp->l_tech))
1191 lp->lnd_tech = tlev;