1 /*
2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * ---
20 *
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.
24 *
25 * ---
26 *
27 * lndsub.c: Land unit subroutines
28 *
29 * Known contributors to this file:
30 * Ken Stevens, 1995
31 * Steve McClure, 1998-2000
32 * Markus Armbruster, 2004-2016
33 */
34
35 #include <config.h>
36
37 #include <math.h>
38 #include <stdlib.h>
39 #include "chance.h"
40 #include "combat.h"
41 #include "damage.h"
42 #include "empobj.h"
43 #include "misc.h"
44 #include "mission.h"
45 #include "news.h"
46 #include "nsc.h"
47 #include "optlist.h"
48 #include "path.h"
49 #include "player.h"
50 #include "prototypes.h"
51 #include "unit.h"
52 #include "xy.h"
53
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);
58
59 static struct ulist *
lnd_find_capable(struct emp_qelem * list,int flags)60 lnd_find_capable(struct emp_qelem *list, int flags)
61 {
62 struct emp_qelem *qp;
63 struct ulist *llp;
64
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)
68 return llp;
69 }
70 return NULL;
71 }
72
73 double
attack_val(int combat_mode,struct lndstr * lp)74 attack_val(int combat_mode, struct lndstr *lp)
75 {
76 int men;
77 double value;
78 struct lchrstr *lcp;
79
80 if (lp->lnd_effic < LAND_MINEFF) {
81 putland(lp->lnd_uid, lp);
82 return 0;
83 }
84
85 lcp = &lchr[(int)lp->lnd_type];
86
87 men = lp->lnd_item[I_MILIT];
88 value = men * lnd_att(lp) * lp->lnd_effic / 100.0;
89
90 switch (combat_mode) {
91 case A_ATTACK:
92 return value;
93 case A_ASSAULT:
94 if (!(lcp->l_flags & L_MARINE))
95 return assault_penalty * value;
96 break;
97 case A_BOARD:
98 if (!(lcp->l_flags & L_MARINE))
99 return assault_penalty * men;
100 }
101
102 return value;
103 }
104
105 double
defense_val(struct lndstr * lp)106 defense_val(struct lndstr *lp)
107 {
108 int men;
109 double value;
110 struct lchrstr *lcp;
111
112 if (lp->lnd_effic < LAND_MINEFF) {
113 putland(lp->lnd_uid, lp);
114 return 0;
115 }
116
117 lcp = &lchr[(int)lp->lnd_type];
118
119 men = lp->lnd_item[I_MILIT];
120
121 if ((lp->lnd_ship >= 0 || lp->lnd_land >= 0) &&
122 !(lcp->l_flags & L_MARINE))
123 return men;
124
125 value = men * lnd_def(lp) * lp->lnd_effic / 100.0;
126 value *= ((double)land_mob_max + lp->lnd_harden) / land_mob_max;
127
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))
131 return 1;
132
133 return value;
134 }
135
136 int
lnd_reaction_range(struct lndstr * lp)137 lnd_reaction_range(struct lndstr *lp)
138 {
139 struct sctstr sect;
140
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;
145 }
146
147 void
lnd_print(natid actor,struct ulist * llp,char * s)148 lnd_print(natid actor, struct ulist *llp, char *s)
149 {
150 if (actor == player->cnum)
151 pr("%s %s\n", prland(&llp->unit.land), s);
152 else
153 wu(0, actor, "%s %s\n", prland(&llp->unit.land), s);
154 }
155
156 int
lnd_take_casualty(int combat_mode,struct ulist * llp,int cas)157 lnd_take_casualty(int combat_mode, struct ulist *llp, int cas)
158 /* attacking or assaulting or paratrooping? */
159 /* number of casualties to take */
160 {
161 int eff_eq;
162 int n;
163 int biggest;
164 int civs;
165 coord ret_x, ret_y;
166 coord bx, by;
167 struct sctstr sect;
168 int ret_chance;
169 char buf[1024];
170 int taken;
171 int nowhere_to_go = 0;
172 double mobcost, bmcost;
173 signed char orig;
174 int mob;
175
176 if (CANT_HAPPEN(lchr[llp->unit.land.lnd_type].l_flags & L_SPY))
177 return 0;
178
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);
184
185 if (llp->unit.land.lnd_effic < LAND_MINEFF) {
186 if (llp->unit.land.lnd_own == player->cnum)
187 pr("\n");
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);
193 lnd_put_one(llp);
194 /* Since we killed the unit, we killed all the mil on it */
195 return taken;
196 } else {
197 /* Ok, now, how many did we take off? (sould be the diff) */
198 taken = taken - llp->unit.land.lnd_item[I_MILIT];
199 }
200
201 if (llp->unit.land.lnd_effic >= llp->unit.land.lnd_retreat)
202 return taken;
203
204 /* we're being boarded */
205 if (llp->unit.land.lnd_ship >= 0 && combat_mode == A_DEFEND)
206 return taken;
207
208 /* we're being boarded */
209 if (llp->unit.land.lnd_land >= 0 && combat_mode == A_DEFEND)
210 return taken;
211
212 /* Have to make a retreat check */
213
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)
217 pr("\n");
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)
222 nowhere_to_go = 1;
223 else if (combat_mode == A_DEFEND) {
224 /*
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..
229 */
230 biggest = -1;
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)
236 continue;
237 if (sect.sct_type == SCT_MOUNT)
238 continue;
239 mobcost = lnd_mobcost(&llp->unit.land, §);
240 if (mobcost < 0)
241 continue;
242 civs = sect.sct_item[I_CIVIL];
243 if (civs > biggest) {
244 biggest = civs;
245 bx = sect.sct_x;
246 by = sect.sct_y;
247 bmcost = mobcost;
248 }
249 }
250 if (biggest < 0)
251 nowhere_to_go = 1;
252 else {
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;
258 if (mob < -127)
259 mob = -127;
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);
268 lnd_put_one(llp);
269 }
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;
275 else
276 llp->unit.land.lnd_mobil -= (int)llp->mobil;
277 llp->mobil = 0.0;
278 lnd_print(llp->unit.land.lnd_own, llp, buf);
279 lnd_put_one(llp);
280 }
281 }
282 if (nowhere_to_go) {
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!");
290 lnd_put_one(llp);
291 } else
292 lnd_print(llp->unit.land.lnd_own, llp,
293 "has nowhere to retreat and takes extra losses!");
294 }
295
296 return taken;
297 }
298
299 void
lnd_takemob(struct emp_qelem * list,double loss)300 lnd_takemob(struct emp_qelem *list, double loss)
301 {
302 struct emp_qelem *qp, *next;
303 struct ulist *llp;
304 int new;
305 int mcost = ldround(combat_mob * loss, 1);
306
307 for (qp = list->q_forw; qp != list; qp = next) {
308 next = qp->q_forw;
309 llp = (struct ulist *)qp;
310 #if 0
311 if (chance(loss))
312 use_supply(&llp->unit.land);
313 #endif
314 new = llp->unit.land.lnd_mobil - mcost;
315 if (new < -127)
316 new = -127;
317 llp->unit.land.lnd_mobil = (signed char)new;
318 }
319 }
320
321 void
lnd_submil(struct lndstr * lp,int num)322 lnd_submil(struct lndstr *lp, int num)
323 {
324 int new = lp->lnd_item[I_MILIT] - num;
325 lp->lnd_item[I_MILIT] = new < 0 ? 0 : new;
326 }
327
328 int
lnd_spyval(struct lndstr * lp)329 lnd_spyval(struct lndstr *lp)
330 {
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;
333 else
334 return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0);
335 }
336
337 void
intelligence_report(natid destination,struct lndstr * lp,int spy,char * mess)338 intelligence_report(natid destination, struct lndstr *lp, int spy,
339 char *mess)
340 {
341 int vis = lnd_vis(lp);
342 char buf1[80], buf2[80], buf3[80];
343
344 if (!destination || !lp->lnd_own)
345 return;
346
347 if (chance((spy + vis) / 10.0)) {
348 sprintf(buf1, "%s %s", mess, prland(lp));
349
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));
354
355 if (chance((spy + vis) / 20.0)) {
356 int t;
357 t = lp->lnd_tech - 20 + roll(40);
358 t = MAX(t, 0);
359 sprintf(buf3, ", tech %d)", t);
360 } else {
361 sprintf(buf3, ")");
362 }
363 } else {
364 buf2[0] = buf3[0] = 0;
365 }
366 if (destination == player->cnum)
367 pr("%s%s%s\n", buf1, buf2, buf3);
368 else
369 wu(0, destination, "%s%s%s\n", buf1, buf2, buf3);
370 }
371 }
372
373 int
lnd_may_mar(struct lndstr * lp,struct lndstr * ldr,char * suffix)374 lnd_may_mar(struct lndstr *lp, struct lndstr *ldr, char *suffix)
375 {
376 struct sctstr sect;
377 int mobtype;
378
379 if (!lp->lnd_own || !getsect(lp->lnd_x, lp->lnd_y, §)) {
380 CANT_REACH();
381 return 0;
382 }
383
384 if (opt_MARKET && ontradingblock(EF_LAND, lp)) {
385 mpr(lp->lnd_own, "%s is on the trading block%s\n",
386 prland(lp), suffix);
387 return 0;
388 }
389
390 if (lp->lnd_ship >= 0) {
391 mpr(lp->lnd_own, "%s is on a ship%s\n", prland(lp), suffix);
392 return 0;
393 }
394 if (lp->lnd_land >= 0) {
395 mpr(lp->lnd_own, "%s is on a unit%s\n", prland(lp), suffix);
396 return 0;
397 }
398
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",
403 prland(lp), suffix);
404 return 0;
405 }
406
407 switch (lnd_check_mar(lp, §)) {
408 case LND_STUCK_NOT:
409 break;
410 case LND_STUCK_NO_RAIL:
411 mpr(lp->lnd_own, "%s is stuck off the rail system%s\n",
412 prland(lp), suffix);
413 return 0;
414 default:
415 CANT_REACH();
416 /* fall through */
417 case LND_STUCK_IMPASSABLE:
418 mpr(lp->lnd_own, "%s is stuck%s\n", prland(lp), suffix);
419 return 0;
420 }
421
422 if (relations_with(sect.sct_own, lp->lnd_own) != ALLIED &&
423 !(lchr[lp->lnd_type].l_flags & L_SPY) &&
424 sect.sct_own) {
425 mpr(lp->lnd_own, "%s has been kidnapped by %s%s\n",
426 prland(lp), cname(sect.sct_own), suffix);
427 return 0;
428 }
429
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",
432 prland(lp), suffix);
433 return 0;
434 }
435
436 /*
437 * The marching code gets confused when trains and non-trains
438 * march together. Disallow for now.
439 */
440 mobtype = lnd_mobtype(lp);
441 if (!ldr || mobtype == lnd_mobtype(ldr))
442 ;
443 else if (mobtype == MOB_RAIL) {
444 mpr(lp->lnd_own,
445 "%s is a train and can't march with the leader%s\n",
446 prland(lp), suffix);
447 return 0;
448 } else {
449 mpr(lp->lnd_own, "%s can't rail-march with the leading train%s\n",
450 prland(lp), suffix);
451 return 0;
452 }
453
454 return 1;
455 }
456
457 void
lnd_sel(struct nstr_item * ni,struct emp_qelem * list)458 lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
459 {
460 struct lndstr land, *ldr = NULL;
461 struct ulist *llp;
462
463 emp_initque(list);
464 while (nxtitem(ni, &land)) {
465 /*
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.
469 */
470 if (!land.lnd_own || land.lnd_own != player->cnum)
471 continue;
472 if (!lnd_may_mar(&land, ldr, ""))
473 continue;
474
475 land.lnd_mission = 0;
476 land.lnd_rflags = 0;
477 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
478 putland(land.lnd_uid, &land);
479 llp = lnd_insque(&land, list);
480 if (!ldr)
481 ldr = &llp->unit.land;
482 }
483 }
484
485 /*
486 * Append @lp to @list.
487 * Return the new list link.
488 */
489 struct ulist *
lnd_insque(struct lndstr * lp,struct emp_qelem * list)490 lnd_insque(struct lndstr *lp, struct emp_qelem *list)
491 {
492 struct ulist *mlp = malloc(sizeof(struct ulist));
493
494 mlp->unit.land = *lp;
495 mlp->mobil = lp->lnd_mobil;
496 emp_insque(&mlp->queue, list);
497 return mlp;
498 }
499
500 void
lnd_mar_stay_behind(struct emp_qelem * list,natid actor)501 lnd_mar_stay_behind(struct emp_qelem *list, natid actor)
502 {
503 struct emp_qelem *qp;
504 struct emp_qelem *next;
505 struct ulist *llp;
506 struct lndstr *lp, *ldr = NULL;
507 char and_stays[32];
508
509 for (qp = list->q_back; qp != list; qp = next) {
510 next = qp->q_back;
511 llp = (struct ulist *)qp;
512 lp = &llp->unit.land;
513 getland(lp->lnd_uid, lp);
514
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);
519 free(llp);
520 continue;
521 }
522
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);
527 continue;
528 }
529
530 if (!ldr)
531 ldr = lp;
532 if (lp->lnd_mobil + 1 < (int)llp->mobil) {
533 llp->mobil = lp->lnd_mobil;
534 }
535 }
536 }
537
538 static void
lnd_mar_put_one(struct ulist * llp)539 lnd_mar_put_one(struct ulist *llp)
540 {
541 if (llp->mobil < -127)
542 llp->mobil = -127;
543 llp->unit.land.lnd_mobil = llp->mobil;
544 lnd_put_one(llp);
545 }
546
547 void
lnd_mar_put(struct emp_qelem * list,natid actor)548 lnd_mar_put(struct emp_qelem *list, natid actor)
549 {
550 struct emp_qelem *qp, *next;
551 struct ulist *llp;
552 struct lndstr *lp;
553
554 for (qp = list->q_back; qp != list; qp = next) {
555 next = qp->q_back;
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);
561 }
562 }
563
564 void
lnd_put(struct emp_qelem * list)565 lnd_put(struct emp_qelem *list)
566 {
567 struct emp_qelem *qp, *next;
568
569 for (qp = list->q_back; qp != list; qp = next) {
570 next = qp->q_back;
571 lnd_put_one((struct ulist *)qp);
572 }
573 }
574
575 void
lnd_put_one(struct ulist * llp)576 lnd_put_one(struct ulist *llp)
577 {
578 putland(llp->unit.land.lnd_uid, &llp->unit.land);
579 emp_remque(&llp->queue);
580 free(llp);
581 }
582
583 /*
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.
590 */
591 int
lnd_sweep(struct emp_qelem * land_list,int explicit,int takemob,natid actor)592 lnd_sweep(struct emp_qelem *land_list, int explicit, int takemob,
593 natid actor)
594 {
595 struct emp_qelem *qp;
596 struct emp_qelem *next;
597 struct ulist *llp;
598 struct sctstr sect;
599 int mines, m, max, sshells, lshells;
600 int stopping = 0, first = 1;
601
602 llp = lnd_find_capable(land_list, L_ENGINEER);
603 if (!llp) {
604 if (explicit)
605 mpr(actor, "No engineers!\n");
606 return 0;
607 }
608
609 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
610 if (!explicit
611 && relations_with(sect.sct_oldown, actor) == ALLIED)
612 return 0;
613 if (SCT_MINES_ARE_SEAMINES(§)) {
614 if (explicit)
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);
618 return 0;
619 }
620
621 for (qp = land_list->q_back; qp != land_list; qp = next) {
622 next = qp->q_back;
623 llp = (struct ulist *)qp;
624 if (!(lchr[llp->unit.land.lnd_type].l_flags & L_ENGINEER))
625 continue;
626 if (takemob) {
627 if (llp->mobil <= 0.0) {
628 if (explicit)
629 mpr(actor, "%s is out of mobility!\n",
630 prland(&llp->unit.land));
631 continue;
632 }
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;
636 }
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))
640 continue;
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)) {
646 if (first) {
647 mpr(actor, "Approaching minefield at %s...\n",
648 xyas(sect.sct_x, sect.sct_y, actor));
649 first = 0;
650 }
651 mpr(actor, "Sweep...\n");
652 mines--;
653 if (lshells < max)
654 ++lshells;
655 else if (sshells < ITEM_MAX)
656 ++sshells;
657 }
658 }
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);
663 putsect(§);
664 stopping |= lnd_check_one_mines(llp, 1);
665 }
666 return stopping;
667 }
668
669 static int
lnd_check_one_mines(struct ulist * llp,int with_eng)670 lnd_check_one_mines(struct ulist *llp, int with_eng)
671 {
672 struct sctstr sect;
673
674 getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
675 if (SCT_LANDMINES(§) == 0)
676 return 0;
677 if (relations_with(sect.sct_oldown, llp->unit.land.lnd_own) == ALLIED)
678 return 0;
679 if (chance(DMINE_LHITCHANCE(sect.sct_mines) / (1 + 2 * with_eng))) {
680 lnd_hit_mine(&llp->unit.land);
681 sect.sct_mines--;
682 putsect(§);
683 putland(llp->unit.land.lnd_uid, &llp->unit.land);
684 if (!llp->unit.land.lnd_own) {
685 emp_remque(&llp->queue);
686 free(llp);
687 }
688 return 1;
689 }
690 return 0;
691 }
692
693 int
lnd_check_mines(struct emp_qelem * land_list)694 lnd_check_mines(struct emp_qelem *land_list)
695 {
696 struct emp_qelem *qp;
697 struct emp_qelem *next;
698 int stopping = 0;
699 int with_eng = !!lnd_find_capable(land_list, L_ENGINEER);
700
701 for (qp = land_list->q_back; qp != land_list; qp = next) {
702 next = qp->q_back;
703 stopping |= lnd_check_one_mines((struct ulist *)qp, with_eng);
704 }
705 return stopping;
706 }
707
708 /* Return whether and why SP would be stuck in SECTP. */
709 enum lnd_stuck
lnd_check_mar(struct lndstr * lp,struct sctstr * sectp)710 lnd_check_mar(struct lndstr *lp, struct sctstr *sectp)
711 {
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;
717 }
718
719 static int
lnd_damage(struct emp_qelem * list,int totdam)720 lnd_damage(struct emp_qelem *list, int totdam)
721 {
722 struct emp_qelem *qp;
723 struct emp_qelem *next;
724 struct ulist *llp;
725 int dam;
726 int count;
727
728 if (!totdam || !(count = emp_quelen(list)))
729 return 0;
730 dam = ldround((double)totdam / count, 1);
731 for (qp = list->q_back; qp != list; qp = next) {
732 next = qp->q_back;
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) {
739 emp_remque(qp);
740 free(qp);
741 }
742 }
743 return dam;
744 }
745
746 static int
lnd_easiest_target(struct emp_qelem * list)747 lnd_easiest_target(struct emp_qelem *list)
748 {
749 struct emp_qelem *qp;
750 struct emp_qelem *next;
751 struct ulist *llp;
752 int hard;
753 int easiest = 9876; /* things start great for victim */
754 int count = 0;
755
756 for (qp = list->q_back; qp != list; qp = next) {
757 next = qp->q_back;
758 llp = (struct ulist *)qp;
759 hard = lnd_hardtarget(&llp->unit.land);
760 if (hard < easiest)
761 easiest = hard; /* things get worse for victim */
762 ++count;
763 }
764 return easiest - count;
765 }
766
767 static int
lnd_missile_interdiction(struct emp_qelem * list,coord newx,coord newy,natid victim)768 lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
769 natid victim)
770 {
771 int mindam = emp_quelen(list) * 20;
772 int hardtarget = lnd_easiest_target(list);
773 int dam, newdam, sublaunch;
774 int stopping = 0;
775 struct plist *plp;
776 struct emp_qelem msl_list, *qp, *newqp;
777
778 msl_sel(&msl_list, newx, newy, victim, P_T, P_MAR, MI_INTERDICT);
779
780 dam = 0;
781 for (qp = msl_list.q_back; qp != &msl_list; qp = newqp) {
782 newqp = qp->q_back;
783 plp = (struct plist *)qp;
784
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)
788 goto use_up_msl;
789 stopping = 1;
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', "");
793 dam += newdam;
794 } else {
795 newdam = pln_damage(&plp->plane, 'p', NULL);
796 collateral_damage(newx, newy, newdam);
797 }
798 use_up_msl:
799 plp->plane.pln_effic = 0;
800 putplane(plp->plane.pln_uid, &plp->plane);
801 }
802 emp_remque(qp);
803 free(qp);
804 }
805
806 if (dam) {
807 mpr(victim, "missile interdiction mission does %d damage!\n", dam);
808 collateral_damage(newx, newy, dam);
809 lnd_damage(list, dam);
810 }
811 return stopping;
812 }
813
814 #if 0
815 /* Steve M. - commented out for now until abuse is decided upon */
816 /* risner: allow forts to interdict land units. */
817 static int
818 lnd_fort_interdiction(struct emp_qelem *list,
819 coord newx, coord newy, natid victim)
820 {
821 struct nstr_sect ns;
822 struct sctstr fsect;
823 int trange, range;
824 int dam;
825 int stopping = 0;
826 int totdam = 0;
827
828 snxtsct_dist(&ns, newx, newy, fort_max_interdiction_range);
829 while (nxtsct(&ns, &fsect)) {
830 if (fsect.sct_own == 0)
831 continue;
832 if (relations_with(fsect.sct_own, victim) >= NEUTRAL)
833 continue;
834 range = roundrange(fortrange(&fsect));
835 trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
836 if (trange > range)
837 continue;
838 dam = fort_fire(&fsect);
839 putsect(&fsect);
840 if (dam < 0)
841 continue;
842 stopping = 1;
843 totdam += dam;
844 mpr(victim, "Incoming fire does %d damage!\n", dam);
845 wu(0, fsect.sct_own,
846 "%s fires at %s land units in %s for %d!\n",
847 xyas(fsect.sct_x, fsect.sct_y,
848 fsect.sct_own),
849 cname(victim), xyas(newx, newy, fsect.sct_own), dam);
850 nreport(fsect.sct_own, N_SCT_SHELL, victim, 1);
851 }
852 if (totdam > 0)
853 lnd_damage(list, totdam);
854 return stopping;
855 }
856 #endif
857
858 static int
lnd_mission_interdiction(struct emp_qelem * list,coord x,coord y,natid victim)859 lnd_mission_interdiction(struct emp_qelem *list, coord x, coord y,
860 natid victim)
861 {
862 int dam;
863
864 dam = unit_interdict(x, y, victim, "land units",
865 lnd_easiest_target(list),
866 MI_INTERDICT);
867 if (dam >= 0)
868 lnd_damage(list, dam);
869 return dam >= 0;
870 }
871
872 int
lnd_interdict(struct emp_qelem * list,coord newx,coord newy,natid victim)873 lnd_interdict(struct emp_qelem *list, coord newx, coord newy, natid victim)
874 {
875 int stopping = 0;
876
877 #if 0
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);
881 #endif
882
883 stopping |= lnd_mission_interdiction(list, newx, newy, victim);
884 stopping |= lnd_missile_interdiction(list, newx, newy, victim);
885 return stopping;
886 }
887
888 /* high value of hardtarget is harder to hit */
889 int
lnd_hardtarget(struct lndstr * lp)890 lnd_hardtarget(struct lndstr *lp)
891 {
892 struct sctstr sect;
893
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
897 - lnd_vis(lp)));
898 }
899
900 static int
lnd_hit_mine(struct lndstr * lp)901 lnd_hit_mine(struct lndstr *lp)
902 {
903 int m;
904
905 mpr(lp->lnd_own, "Blammo! Landmines detected in %s!\n",
906 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
907
908 nreport(lp->lnd_own, N_LHIT_MINE, 0, 1);
909
910 m = MINE_LDAMAGE();
911 if (lchr[lp->lnd_type].l_flags & L_ENGINEER)
912 m /= 2;
913
914 landdamage(lp, m);
915 return m;
916 }
917
918 double
lnd_pathcost(struct lndstr * lp,double pathcost)919 lnd_pathcost(struct lndstr *lp, double pathcost)
920 {
921 double effspd;
922
923 effspd = lnd_spd(lp);
924 if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
925 effspd *= lp->lnd_effic * 0.01;
926
927 /*
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().
932 */
933 return pathcost * 5.0 * speed_factor(effspd, lp->lnd_tech);
934 }
935
936 int
lnd_mobtype(struct lndstr * lp)937 lnd_mobtype(struct lndstr *lp)
938 {
939 return (lchr[(int)lp->lnd_type].l_flags & L_TRAIN)
940 ? MOB_RAIL : MOB_MARCH;
941 }
942
943 double
lnd_mobcost(struct lndstr * lp,struct sctstr * sp)944 lnd_mobcost(struct lndstr *lp, struct sctstr *sp)
945 {
946 return lnd_pathcost(lp, sector_mcost(sp, lnd_mobtype(lp)));
947 }
948
949 /*
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.
956 */
lnd_abandon_askyn(struct emp_qelem * list)957 int lnd_abandon_askyn(struct emp_qelem *list)
958 {
959 struct ulist *llp;
960 struct sctstr sect;
961 int abandon;
962 struct emp_qelem *qp;
963
964 if (QEMPTY(list))
965 return 1;
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(§))
970 return 0;
971 for (qp = list->q_back; qp != list; qp = qp->q_back) {
972 if (!check_land_ok(&((struct ulist *)qp)->unit.land))
973 return 0;
974 }
975 return abandon;
976 }
977
978 int
lnd_mar_dir(struct emp_qelem * list,int dir,natid actor)979 lnd_mar_dir(struct emp_qelem *list, int dir, natid actor)
980 {
981 struct sctstr sect, osect;
982 struct emp_qelem *qp;
983 struct emp_qelem *next;
984 struct ulist *llp;
985 coord dx;
986 coord dy;
987 coord newx;
988 coord newy;
989 int move;
990 enum relations rel;
991 int oldown;
992
993 if (CANT_HAPPEN(QEMPTY(list)))
994 return 1;
995
996 if (dir <= DIR_STOP || dir > DIR_LAST) {
997 CANT_HAPPEN(dir != DIR_STOP);
998 lnd_mar_put(list, actor);
999 return 1;
1000 }
1001 dx = diroff[dir][0];
1002 dy = diroff[dir][1];
1003
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;
1011
1012 move = 0;
1013 for (qp = list->q_back; qp != list; qp = next) {
1014 next = qp->q_back;
1015 llp = (struct ulist *)qp;
1016 switch (lnd_check_mar(&llp->unit.land, §)) {
1017 case LND_STUCK_NOT:
1018 if (rel == ALLIED
1019 || (lchr[llp->unit.land.lnd_type].l_flags & L_SPY))
1020 move = 1;
1021 break;
1022 case LND_STUCK_NO_RAIL:
1023 if (rel == ALLIED)
1024 mpr(actor, "no rail system in %s\n",
1025 xyas(newx, newy, actor));
1026 else
1027 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1028 return 1;
1029 default:
1030 CANT_REACH();
1031 /* fall through */
1032 case LND_STUCK_IMPASSABLE:
1033 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1034 return 1;
1035 }
1036 }
1037 if (!move) {
1038 mpr(actor, "can't go to %s\n", xyas(newx, newy, actor));
1039 return 1;
1040 }
1041
1042 for (qp = list->q_back; qp != list; qp = next) {
1043 next = qp->q_back;
1044 llp = (struct ulist *)qp;
1045 if (rel != ALLIED
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);
1051 continue;
1052 }
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);
1058 continue;
1059 }
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);
1066 putsect(&osect);
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));
1072 }
1073 if (rel != ALLIED) {
1074 /* must be a spy */
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) {
1078 wu(0, sect.sct_own,
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) {
1083 wu(0, sect.sct_own,
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);
1090 lnd_put_one(llp);
1091 }
1092 }
1093 }
1094 }
1095
1096 return QEMPTY(list);
1097 }
1098
1099 int
lnd_mar_gauntlet(struct emp_qelem * list,int interdict,natid actor)1100 lnd_mar_gauntlet(struct emp_qelem *list, int interdict, natid actor)
1101 {
1102 struct ulist *mlp = (struct ulist *)list->q_back;
1103 coord newx, newy;
1104 int stopping, visible;
1105 struct emp_qelem *qp, *next;
1106 struct ulist *llp;
1107
1108 if (CANT_HAPPEN(QEMPTY(list)))
1109 return 1;
1110 newx = mlp->unit.land.lnd_x;
1111 newy = mlp->unit.land.lnd_y;
1112 stopping = lnd_sweep(list, 0, 1, actor);
1113 if (QEMPTY(list))
1114 return 1;
1115 stopping |= lnd_check_mines(list);
1116 if (QEMPTY(list))
1117 return 1;
1118
1119 visible = 0;
1120 for (qp = list->q_back; qp != list; qp = next) {
1121 next = qp->q_back;
1122 llp = (struct ulist *)qp;
1123 if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY))
1124 visible = 1;
1125 }
1126 if (visible && interdict)
1127 stopping |= lnd_interdict(list, newx, newy, actor);
1128
1129 return stopping;
1130 }
1131
1132 /*
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.
1136 */
1137 int
lnd_support(natid victim,natid attacker,coord x,coord y,int defending)1138 lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
1139 {
1140 struct nstr_item ni;
1141 struct lndstr land;
1142 int dam, dam2;
1143 int dist;
1144 int range;
1145
1146 dam = 0;
1147 snxtitem_all(&ni, EF_LAND);
1148 while (nxtitem(&ni, &land)) {
1149 if ((land.lnd_x == x) && (land.lnd_y == y))
1150 continue;
1151 if (!feels_like_helping(land.lnd_own, attacker, victim))
1152 continue;
1153
1154 /* are we in range? */
1155 dist = mapdist(land.lnd_x, land.lnd_y, x, y);
1156
1157 range = roundrange(lnd_fire_range(&land));
1158 if (dist > range)
1159 continue;
1160
1161 dam2 = lnd_fire(&land);
1162 putland(land.lnd_uid, &land);
1163 if (dam2 < 0)
1164 continue;
1165
1166 if (defending)
1167 nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
1168 else
1169 nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
1170 if (pct_chance(lnd_acc(&land) - 1))
1171 dam2 /= 2;
1172 dam += dam2;
1173 if (land.lnd_own != attacker)
1174 wu(0, land.lnd_own,
1175 "%s supported %s at %s\n",
1176 prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
1177 }
1178 return dam;
1179 }
1180
1181 int
lnd_can_attack(struct lndstr * lp)1182 lnd_can_attack(struct lndstr *lp)
1183 {
1184 struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
1185
1186 if (lcp->l_flags & L_SUPPLY)
1187 return 0;
1188
1189 return 1;
1190 }
1191
1192 /*
1193 * Increase fortification value of @lp.
1194 * Fortification costs mobility. Use up to @mob mobility.
1195 * Return actual fortification increase.
1196 */
1197 int
lnd_fortify(struct lndstr * lp,int mob)1198 lnd_fortify(struct lndstr *lp, int mob)
1199 {
1200 int hard_amt;
1201 double mob_used, mult;
1202
1203 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
1204 return 0;
1205
1206 mob_used = MIN(lp->lnd_mobil, mob);
1207 if (mob_used < 0)
1208 return 0;
1209
1210 mult = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own)
1211 ? 1.5 : 1.0;
1212
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);
1217 }
1218
1219 lp->lnd_mobil -= (int)mob_used;
1220 lp->lnd_harden += hard_amt;
1221 lp->lnd_harden = MIN(lp->lnd_harden, land_mob_max);
1222
1223 return hard_amt;
1224 }
1225
1226 /*
1227 * Is there a engineer unit at @x,@y that can help nation @cn?
1228 */
1229 static int
has_helpful_engineer(coord x,coord y,natid cn)1230 has_helpful_engineer(coord x, coord y, natid cn)
1231 {
1232 struct nstr_item ni;
1233 struct lndstr land;
1234
1235 snxtitem_xy(&ni, EF_LAND, x, y);
1236 while (nxtitem(&ni, &land)) {
1237 if (relations_with(land.lnd_own, cn) != ALLIED)
1238 continue;
1239 if (lchr[(int)land.lnd_type].l_flags & L_ENGINEER)
1240 return 1;
1241 }
1242
1243 return 0;
1244 }
1245
1246 /*
1247 * Set @lp's tech to @tlev along with everything else that depends on it.
1248 */
1249 void
lnd_set_tech(struct lndstr * lp,int tlev)1250 lnd_set_tech(struct lndstr *lp, int tlev)
1251 {
1252 struct lchrstr *lcp = lchr + lp->lnd_type;
1253
1254 if (CANT_HAPPEN(tlev < lcp->l_tech))
1255 tlev = lcp->l_tech;
1256
1257 lp->lnd_tech = tlev;
1258 }
1259