2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * attsub.c: Attack subroutines
30 * Known contributors to this file:
32 * Steve McClure, 1996-2000
33 * Markus Armbruster, 2006-2009
50 #include "prototypes.h"
55 #define CASUALTY_LUMP 1 /* How big casualty chunks should be */
57 static void ask_olist(int combat_mode, struct combat *off,
58 struct combat *def, struct emp_qelem *olist,
59 char *land_answer, int *a_spyp, int *a_engineerp);
60 static void take_move_in_mob(int combat_mode, struct ulist *llp,
61 struct combat *off, struct combat *def);
62 static void move_in_land(int combat_mode, struct combat *off,
63 struct emp_qelem *olist, struct combat *def);
64 static void ask_move_in(struct combat *off, struct emp_qelem *olist,
66 static void ask_move_in_off(struct combat *off, struct combat *def);
68 static int board_abort(struct combat *off, struct combat *def);
69 static int land_board_abort(struct combat *off, struct combat *def);
70 static int ask_off(int combat_mode, struct combat *off,
72 static void get_dlist(struct combat *def, struct emp_qelem *, int a_spy,
74 static int get_ototal(int combat_mode, struct combat *off,
75 struct emp_qelem *olist, double osupport, int check);
76 static int get_dtotal(struct combat *def, struct emp_qelem *list,
77 double dsupport, int check);
78 static double att_calcodds(int, int);
79 static int take_casualty(int combat_mode, struct combat *off,
80 struct emp_qelem *olist);
82 static void send_reacting_units_home(struct emp_qelem *list);
83 static int take_def(int combat_mode, struct emp_qelem *list,
84 struct combat *off, struct combat *def);
86 static int get_oland(int, struct ulist *);
87 static int get_dland(struct combat *, struct ulist *);
90 /* must match combat types in combat.h */
91 "defend", "attack", "assault", "paradrop", "board", "lboard"
96 * The principal object in this code is the "combat" object. A combat object
97 * is either a sector or ship. There are
98 * usually two instances of this, the "def" or defense combat object, and
99 * the array of "off" or offense objects. The number of offense objects is
100 * determined by the value of off->last (e.g. more than one attacking sector).
101 * the type of the object is determined by combat->type which can take the
102 * values EF_SECTOR, EF_SHIP, EF_PLANE, or EF_BAD. Another important parameter
103 * which is often passed to these functions is combat_mode. This can take
104 * the value A_DEFEND, A_ATTACK, A_ASSAULT, A_PARA, A_BOARD and A_LBOARD.
105 * As these six modes of being in combat affect things like mobcost and combat
106 * value, there are often switches made on combat_mode. Note that in all cases
107 * no mobility is taken from sectors, ships, or land units until the player
108 * has committed to a fight. Instead, the cost is temporarily placed in
109 * combat->mobcost, or llp->mobil as the case may be, and then when the object
110 * is "put" back onto disk, then the amounts in these variables are subtracted
111 * from the object's mobility. It needs to be done this way as the objects
112 * are constantly being re-read from disk, and we don't want to take any mob
113 * unless a fight actually occurrs.
117 /* initialize combat object */
120 att_combat_init(struct combat *com, int type)
122 memset(com, 0, sizeof(*com));
127 /* print a combat object with optional preposition */
130 pr_com(int inon, struct combat *com, natid who)
132 if (com->type == EF_SECTOR) {
134 inon ? inon == 1 ? "in " : "into " : "",
135 xyas(com->x, com->y, who));
136 } else if (com->type == EF_SHIP) {
137 return prbuf("%s%s %s(#%d)",
138 inon ? inon == 1 ? "on " : "onto " : "",
139 com->shp_mcp->m_name, com->shp_name, com->shp_uid);
140 } else if (com->type == EF_LAND) {
141 return prbuf("%s%s #%d",
142 inon ? inon == 1 ? "on " : "onto " : "",
143 com->lnd_lcp->l_name, com->lnd_uid);
145 return "your forces";
150 prcom(int inon, struct combat *com)
152 return pr_com(inon, com, player->cnum);
156 * This is the combat object "type" based integrity check. It basically
157 * splits along three divisions: ship/sector, attacker/defender,
158 * first time/not first time.
162 att_get_combat(struct combat *com, int isdef)
176 if (!getsect(com->x, com->y, §)) {
177 pr("Bad sector: %s\n", xyas(com->x, com->y, player->cnum));
178 return att_combat_init(com, EF_BAD);
180 com->sct_type = sect.sct_type;
181 com->sct_dcp = &dchr[sect.sct_type];
182 mil = sect.sct_item[I_MILIT];
183 pstage = sect.sct_pstage;
184 owner = sect.sct_own;
185 eff = sect.sct_effic;
186 mob = sect.sct_mobil;
191 if (!getland(com->lnd_uid, &land) || !land.lnd_own) {
193 pr("Land unit #%d is not in the same sector!\n",
195 return att_combat_init(com, EF_BAD);
197 if (isdef && player->owner) {
198 pr("Boarding yourself? Try using the 'load' command.\n");
199 return att_combat_init(com, EF_BAD);
201 com->lnd_lcp = &lchr[(int)land.lnd_type];
202 mil = land.lnd_item[I_MILIT];
203 pstage = land.lnd_pstage;
204 owner = land.lnd_own;
205 eff = land.lnd_effic;
206 mob = land.lnd_mobil;
211 if (!getship(com->shp_uid, &ship) || !ship.shp_own) {
213 pr("Ship #%d is not in the same sector!\n", com->shp_uid);
215 pr("Ship #%d is not your ship!\n", com->shp_uid);
216 return att_combat_init(com, EF_BAD);
219 if (isdef && player->owner && ontradingblock(EF_SHIP, &ship)) {
220 pr("%s is on the trading block.\n", prcom(0, com));
221 return att_combat_init(com, EF_BAD);
224 if (isdef && player->owner) {
225 pr("Boarding yourself? Try using the 'tend' command.\n");
226 return att_combat_init(com, EF_BAD);
228 com->shp_mcp = &mchr[(int)ship.shp_type];
229 strncpy(com->shp_name, ship.shp_name, MAXSHPNAMLEN);
230 if (!isdef && !player->owner) {
232 pr("%s was just sunk!\n", prcom(0, com));
234 pr("Ship #%d is not your ship!\n", com->shp_uid);
235 return att_combat_init(com, EF_BAD);
237 mil = ship.shp_item[I_MILIT];
238 pstage = ship.shp_pstage;
239 owner = ship.shp_own;
240 eff = ship.shp_effic;
241 mob = ship.shp_mobil;
250 return att_combat_init(com, EF_BAD);
253 if (!com->set) { /* first time */
254 if (isdef) { /* defender */
256 } else { /* attacker */
258 pr("No mil %s\n", prcom(1, com));
260 pr("Only 1 mil %s\n", prcom(1, com));
261 /* don't abandon attacking sectors or ships */
262 com->troops = MAX(0, mil - 1);
264 com->plague = pstage == PLG_INFECT;
265 } else { /* not first time */
266 if (isdef) { /* defender */
267 if (com->x != x || com->y != y) {
268 pr("%s has moved!\n", prcom(0, com));
269 return att_combat_init(com, EF_BAD);
271 if (owner != com->own) {
273 pr("WARNING: The ownership of %s just changed from %s to %s!\n",
274 prcom(0, com), cname(com->own), cname(owner));
275 } else if (com->type == EF_SECTOR) {
276 pr("WARNING: %s just abandoned sector %s!\n",
278 xyas(com->x, com->y, player->cnum));
282 pr("WARNING: The enemy mil %s just %s from %d to %d!\n",
284 com->mil < mil ? "increased" : "decreased", com->mil,
287 } else { /* attacker */
288 if (owner != player->cnum
289 && getrel(getnatp(owner), player->cnum) != ALLIED) {
290 /* must be EF_SECTOR */
292 pr("WARNING: Your %d mil in %s were destroyed because %s just took the sector!\n",
293 com->mil, xyas(com->x, com->y, player->cnum),
296 pr("You no longer own %s\n",
297 xyas(com->x, com->y, player->cnum));
298 return att_combat_init(com, EF_BAD);
300 if (com->troops && com->troops + 1 > mil) {
301 if (com->own == owner && player->cnum == owner)
303 pr("WARNING: Your mil %s has been reduced from %d to %d!\n",
304 prcom(1, com), com->troops, MAX(0, mil - 1));
305 com->troops = MAX(0, mil - 1);
320 * In the course of the fight, the combat object may have lost mil, eff, or
321 * mobility. This is the place where the data in the object gets flushed to
322 * disk to make it "real".
326 put_combat(struct combat *com)
335 getsect(com->x, com->y, §);
336 sect.sct_type = com->sct_type;
337 deff = sect.sct_effic - com->eff;
339 sect.sct_road -= sect.sct_road * deff / 100.0;
340 sect.sct_rail -= sect.sct_rail * deff / 100.0;
341 sect.sct_defense -= sect.sct_defense * deff / 100.0;
342 if (sect.sct_road <= 0)
344 if (sect.sct_rail <= 0)
346 if (sect.sct_defense <= 0)
347 sect.sct_defense = 0;
349 sect.sct_effic = com->eff;
351 if (opt_MOB_ACCESS) {
352 if ((com->mob - com->mobcost) < -127)
353 sect.sct_mobil = -127;
355 sect.sct_mobil = com->mob - com->mobcost;
357 if ((com->mob - com->mobcost) < 0)
360 sect.sct_mobil = com->mob - com->mobcost;
363 sect.sct_own = com->own;
365 if (sect.sct_pstage == PLG_HEALTHY)
366 sect.sct_pstage = PLG_EXPOSED;
368 sect.sct_item[I_MILIT] = com->mil;
370 com->own = sect.sct_own; /* avoid WARNING if sector reverts */
373 getland(com->lnd_uid, &land);
374 land.lnd_effic = com->eff;
376 if (com->mob - com->mobcost < -127)
377 land.lnd_mobil = -127;
379 land.lnd_mobil = (signed char)(com->mob - com->mobcost);
381 land.lnd_own = com->own;
383 if (land.lnd_pstage == PLG_HEALTHY)
384 land.lnd_pstage = PLG_EXPOSED;
386 if (!(com->lnd_lcp->l_flags & L_SPY))
387 land.lnd_item[I_MILIT] = com->mil;
388 if (com->own == player->cnum) {
389 land.lnd_mission = 0;
391 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
393 putland(com->lnd_uid, &land);
396 getship(com->shp_uid, &ship);
397 ship.shp_effic = com->eff;
399 if (com->mob - com->mobcost < -127)
400 ship.shp_mobil = -127;
402 ship.shp_mobil = (signed char)(com->mob - com->mobcost);
404 ship.shp_own = com->own;
406 if (ship.shp_pstage == PLG_HEALTHY)
407 ship.shp_pstage = PLG_EXPOSED;
409 ship.shp_item[I_MILIT] = com->mil;
410 if (com->own == player->cnum) {
411 ship.shp_mission = 0;
413 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
415 putship(com->shp_uid, &ship);
418 att_get_combat(com, com->own != player->cnum);
421 /* If pre-attack, abort fight. If post-attack, don't move anything in */
426 return player->aborted = 1;
430 * This is the combat_mode based integrity check. It splits among two main
431 * divisions: first time/not first time, and attack/assault/para/board.
435 att_abort(int combat_mode, struct combat *off, struct combat *def)
444 if (att_get_combat(def, 1) < 0)
445 return abort_attack();
447 if (off && combat_mode != A_ATTACK) {
448 if (att_get_combat(off, 0) < 0)
449 return abort_attack();
450 if (off->type == EF_SHIP &&
451 !(off->x == def->x && off->y == def->y) &&
452 (!getsect(off->x, off->y, §) ||
453 sect.sct_type != SCT_WATER)) {
454 pr("%s can not %s from that far inland!\n",
455 prcom(0, off), att_mode[combat_mode]);
456 return abort_attack();
459 switch (combat_mode) {
461 if (!neigh(def->x, def->y, player->cnum) &&
462 !adj_units(def->x, def->y, player->cnum)) {
463 pr("You are not adjacent to %s\n",
464 xyas(def->x, def->y, player->cnum));
465 return abort_attack();
467 if (def->own == player->cnum) {
468 pr("You can't attack your own sector.\n");
469 return abort_attack();
473 if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
474 pr("You'll have to get there first...\n");
475 return abort_attack();
477 if (off && def->sct_type == SCT_MOUNT) {
478 pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
479 return abort_attack();
483 if (def->own == player->cnum) {
484 pr("You can't air-assault your own sector.\n");
485 return abort_attack();
487 if (off && (def->sct_type == SCT_MOUNT ||
488 def->sct_type == SCT_WATER ||
489 def->sct_type == SCT_CAPIT ||
490 def->sct_type == SCT_FORTR ||
491 def->sct_type == SCT_WASTE)) {
492 pr("You can't air-assault a %s sector!\n",
493 def->sct_dcp->d_name);
494 return abort_attack();
498 return board_abort(off, def);
500 return land_board_abort(off, def);
503 if (off && def->sct_dcp->d_mob0 < 0) {
504 pr("You can't %s a %s sector!\n",
505 att_mode[combat_mode], def->sct_dcp->d_name);
506 return abort_attack();
508 if (!off || off->relations_checked)
510 off->relations_checked = 1;
513 setcont(player->cnum, def->own, FOUND_SPY);
514 setcont(def->own, player->cnum, FOUND_SPY);
516 if (opt_SLOW_WAR && def->own != player->cnum) {
517 natp = getnatp(player->cnum);
518 rel = getrel(natp, def->own);
521 sprintf(y_or_n, "Sector is owned by %s, your ally, %s [yn]? ",
522 cname(def->own), att_mode[combat_mode]);
523 if (!confirm(y_or_n))
524 return abort_attack();
527 if ((rel != AT_WAR) && (def->own) &&
528 (sect.sct_oldown != player->cnum)) {
529 pr("You're not at war with them!\n");
530 return abort_attack();
537 * Lots of special things need to be checked for boarding, so I put it in
542 board_abort(struct combat *off, struct combat *def)
544 struct shpstr aship, dship; /* for tech levels */
547 if (att_get_combat(def, 1) < 0)
548 return abort_attack();
553 if (att_get_combat(off, 0) < 0)
554 return abort_attack();
556 if (off->x != def->x || off->y != def->y) {
557 pr("Ship #%d is not in the same sector!\n", def->shp_uid);
558 return abort_attack();
560 if (off->type == EF_SHIP) {
562 pr("%s has no mobility!\n", prcom(0, off));
563 return abort_attack();
565 getship(off->shp_uid, &aship);
566 getship(def->shp_uid, &dship);
567 if (techfact(aship.shp_tech, shp_speed(&aship)) * off->eff
568 <= techfact(dship.shp_tech, shp_speed(&dship)) * def->eff) {
569 pr("Victim ship moves faster than you do!\n");
572 "%s (#%d) %s failed to catch %s\n",
573 cname(aship.shp_own), aship.shp_own,
574 pr_com(0, off, def->own), pr_com(0, def, def->own));
575 return abort_attack();
577 } else if (off->type != EF_SECTOR) {
578 pr("Please tell the deity that you got the 'banana boat' error\n");
579 return abort_attack();
581 if (def->shp_mcp->m_flags & M_SUB) {
582 getsect(def->x, def->y, §);
583 if (sect.sct_type == SCT_WATER) {
584 pr("You can't board a submarine!\n");
585 return abort_attack();
592 * Lots of special things need to be checked for boarding, so I put it in
594 * STM - I copied it for land unit boarding. :)
598 land_board_abort(struct combat *off, struct combat *def)
600 if (att_get_combat(def, 1) < 0)
601 return abort_attack();
606 if (att_get_combat(off, 0) < 0)
607 return abort_attack();
609 if (off->x != def->x || off->y != def->y) {
610 pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
611 return abort_attack();
617 /* If we are boarding, then the defending ship gets a chance to fire back */
619 att_approach(struct combat *off, struct combat *def)
625 pr("Approaching %s...\n", prcom(0, def));
628 "%s is being approached by %s...\n",
629 pr_com(0, def, def->own), pr_com(0, off, def->own));
630 if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
633 pr("They're firing at us sir!\n");
636 "Your fleet at %s does %d damage to %s\n",
637 xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
639 if (off->type == EF_SECTOR) {
640 getsect(off->x, off->y, §);
641 sectdamage(§, dam);
643 pr("Enemy fleet at %s does %d damage to %s\n",
644 xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
645 } else if (off->type == EF_SHIP) {
646 getship(off->shp_uid, &ship);
647 shipdamage(&ship, dam);
648 putship(off->shp_uid, &ship);
649 if (def->own && ship.shp_effic < SHIP_MINEFF) {
650 wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
651 nreport(player->cnum, N_SHP_LOSE, def->own, 1);
654 if (att_get_combat(off, 0) < 0)
655 return abort_attack();
659 /* The attack is valid. Tell the attacker about what they're going to hit */
662 att_show(struct combat *def)
664 /* Note that we tell the player about the treaty BEFORE we tell them
665 about the item. If we didn't, then they gain free information */
666 if (def->type == EF_SECTOR) {
667 if (!trechk(player->cnum, def->own, LANATT))
668 return abort_attack();
669 pr("%s is a %d%% %s %s with approximately %d military.\n",
670 xyas(def->x, def->y, player->cnum),
671 roundintby((int)def->eff, 10),
672 cname(def->own), def->sct_dcp->d_name,
673 roundintby(def->troops, 10));
674 if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
675 writemap(player->cnum);
676 } else if (def->type == EF_SHIP || def->type == EF_LAND) {
677 if (def->type == EF_SHIP) {
678 if (!trechk(player->cnum, def->own, SEAATT))
679 return abort_attack();
681 if (!trechk(player->cnum, def->own, LNDATT))
682 return abort_attack();
684 pr("%s is about %d%% efficient and has approximately %d mil on board.\n",
685 prcom(0, def), roundintby((int)def->eff, 10),
686 roundintby(def->troops, 10));
688 /* Ok, everything is fine */
692 /* Attack and assault ask the user which kind of support they want */
695 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
700 *fortp = *shipp = *landp = *planep = 1;
702 if (player->argp[offset] != NULL) {
703 if ((player->argp[offset + 1] == NULL) ||
704 (player->argp[offset + 2] == NULL) ||
705 (player->argp[offset + 3] == NULL)) {
706 pr("If any support arguments are used, all must be!\n");
711 *landp = *planep = 0;
713 p = getstarg(player->argp[offset], "Use fort support? ", buf);
717 if ((*p == 'y') || (*p == 'Y'))
720 p = getstarg(player->argp[offset + 1], "Use ship support? ", buf);
724 if ((*p == 'y') || (*p == 'Y'))
727 p = getstarg(player->argp[offset + 2], "Use land support? ", buf);
731 if ((*p == 'y') || (*p == 'Y'))
734 p = getstarg(player->argp[offset + 3], "Use plane support? ", buf);
738 if ((*p == 'y') || (*p == 'Y'))
745 * Attack, assault, and board ask the attacker what they'd like to attack
746 * with. This includes mil and land units from each "off" object. Note that
747 * after each sub-prompt, we check to make sure that the attack is still
748 * valid, and if it's not, then we abort the attack.
752 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
753 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
756 char land_answer[256];
759 if (att_abort(combat_mode, off, def))
761 memset(land_answer, 0, sizeof(land_answer));
762 for (n = 0; n <= off->last; ++n) {
763 off[n].troops = ask_off(combat_mode, off + n, def);
764 if (att_abort(combat_mode, off, def))
766 ask_olist(combat_mode, off + n, def, olist, land_answer,
767 a_spyp, a_engineerp);
768 if (att_abort(combat_mode, off, def))
775 * Return path cost for ATTACKER to enter sector given by DEF.
776 * MOBTYPE is a mobility type accepted by sector_mcost().
779 att_mobcost(natid attacker, struct combat *def, int mobtype)
784 if (CANT_HAPPEN(def->type != EF_SECTOR))
786 ok = getsect(def->x, def->y, §);
787 if (CANT_HAPPEN(!ok))
791 * We want the cost to move/march into the sector. If we just
792 * called sector_mcost(), we'd get the defender's cost. The
793 * attacker's cost is higher unless he's the old-owner. Note: if
794 * there are no civilians, a victorious attacker will become the
795 * old-owner. But he isn't now.
797 sect.sct_own = attacker;
799 return sector_mcost(§, mobtype);
802 /* How many mil is off allowed to attack with when it attacks def? */
805 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
810 switch (combat_mode) {
812 mobcost = att_mobcost(off->own, def, MOB_MOVE);
813 if (mobcost < 0 || off->mob <= 0)
815 mob_support = off->mob / mobcost;
816 if (mob_support < off->troops)
817 pr("Sector %s has %d mobility which can only support %d mil,\n",
818 xyas(off->x, off->y, player->cnum), off->mob, mob_support);
820 mob_support = off->troops;
823 if (def->own != player->cnum && def->mil) {
824 if (off->shp_mcp->m_flags & M_SEMILAND)
825 return off->troops / 4;
826 else if (!(off->shp_mcp->m_flags & M_LAND))
827 return off->troops / 10;
831 if (off->type == EF_SECTOR && off->mob <= 0)
833 mob_support = def->shp_mcp->m_item[I_MILIT];
834 if (mob_support < off->troops)
835 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
837 mob_support = off->troops;
842 if (def->lnd_lcp->l_flags & L_SPY)
844 mob_support = def->lnd_lcp->l_item[I_MILIT];
845 if (mob_support < off->troops)
846 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
848 mob_support = off->troops;
855 * If the attacker decides to go through with the attack, then the
856 * sectors/ships they are attacking with may be charged some mobility.
857 * This is where that amount of mobility is calculated. It is actually
858 * subtracted "for real" from the object's mobility in put_combat().
862 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
869 switch (combat_mode) {
871 off->mobcost += MAX(1,
873 * att_mobcost(off->own, def, MOB_MOVE)));
876 off->mobcost += MAX(1, attacking_mil / 5);
881 off->mobcost += MAX(1, attacking_mil / 5);
884 /* the 2 in the formula below is a fudge factor */
885 getship(def->shp_uid, &ship);
886 off->mobcost += (def->eff / 100) * (shp_speed(&ship) / 2);
891 /* How many mil to we want to attack from off against def? */
894 ask_off(int combat_mode, struct combat *off, struct combat *def)
900 if (att_get_combat(off, 0) <= 0)
902 if ((off->type == EF_SECTOR) && (off->own != player->cnum))
904 if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
906 if (off->type == EF_SECTOR) {
907 if (off->own != player->cnum)
909 sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
910 off->sct_dcp->d_name,
911 xyas(off->x, off->y, player->cnum), mob_support);
913 sprintf(prompt, "Number of mil from %s (max %d) : ",
914 prcom(0, off), mob_support);
916 if ((attacking_mil = onearg(NULL, prompt)) < 0)
918 if (att_abort(combat_mode, off, def))
920 if (att_get_combat(off, 0) <= 0)
923 MIN(attacking_mil, MIN(mob_support, off->troops))) <= 0)
926 calc_mobcost(combat_mode, off, def, attacking_mil);
927 return attacking_mil;
931 * Which units would you like to attack with or move in with [ynYNq?]
935 att_prompt(char *prompt, char army)
943 p = getstring(prompt, buf);
944 if (!p || *p == 'q') {
950 if (tolower(*p) == 'y' || tolower(*p) == 'n')
952 pr("y - yes this unit\n"
954 "Y - yes to all units in army '%c'\n"
955 "N - no to all units in army '%c'\n"
956 "q - quit\n? - this help message\n\n",
961 /* Ask the attacker which units they want to attack/assault/board with */
964 ask_olist(int combat_mode, struct combat *off, struct combat *def,
965 struct emp_qelem *olist, char *land_answer, int *a_spyp,
970 double pathcost, mobcost;
980 if (def->type == EF_LAND)
982 if (def->type == EF_SHIP)
983 maxland = def->shp_mcp->m_nland;
985 snxtitem_xy(&ni, EF_LAND, off->x, off->y);
986 while (nxtitem(&ni, &land)) {
987 if (land.lnd_own != player->cnum)
989 if (land.lnd_effic < LAND_MINEFF)
991 if (land_answer[(int)land.lnd_army] == 'N')
993 if (!lnd_can_attack(&land))
995 lcp = &lchr[(int)land.lnd_type];
997 if (def->type == EF_SHIP && !maxland) {
998 pr("Land units are not able to board this kind of ship\n");
1001 if (def->type == EF_SHIP
1002 && (def->shp_mcp->m_flags & (M_SUPPLY | M_SUB)) != M_SUPPLY
1003 && !(lcp->l_flags & L_LIGHT)) {
1004 pr("Only light land units can board this kind of ship\n");
1007 if (land.lnd_mobil <= 0) {
1008 pr("%s is out of mobility, and cannot %s\n",
1009 prland(&land), att_mode[combat_mode]);
1014 if (ontradingblock(EF_LAND, &land)) {
1015 pr("%s is on the trading block, and cannot %s\n",
1016 prland(&land), att_mode[combat_mode]);
1021 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
1022 pr("%s is on ship #%d, and cannot %s\n",
1023 prland(&land), land.lnd_ship, att_mode[combat_mode]);
1025 } else if (off->type == EF_SHIP) {
1026 if (land.lnd_ship != off->shp_uid)
1028 } else if (land.lnd_land >= 0) {
1029 pr("%s is on unit #%d, and cannot %s\n",
1030 prland(&land), land.lnd_land, att_mode[combat_mode]);
1033 switch (combat_mode) {
1036 * We used to let land units attack only if they have the
1037 * mobility consumed by the attack, not counting combat
1038 * and moving in to occupy. Making sure your land units
1039 * reach attack positions with enough mobility left is a
1040 * pain in the neck. We now require positive mobility,
1041 * just like for marching. Except we don't allow rushing
1042 * of high-mobility sectors (mountains): for those we
1043 * still require attack mobility.
1045 pathcost = att_mobcost(off->own, def, lnd_mobtype(&land));
1046 mobcost = lnd_pathcost(&land, pathcost);
1047 if (pathcost < 1.0) {
1048 if (land.lnd_mobil <= 0) {
1049 pr("%s is out of mobility\n", prland(&land));
1053 reqmob = MIN(land_mob_max, (int)ceil(mobcost));
1054 if (land.lnd_mobil < reqmob) {
1055 pr("%s does not have enough mobility (%d needed)\n",
1056 prland(&land), reqmob);
1064 if (!(lcp->l_flags & L_ASSAULT))
1071 att_val = attack_val(combat_mode, &land);
1072 if (att_val < 1.0) {
1073 pr("%s has no offensive strength\n", prland(&land));
1076 if (!lnd_supply_all(&land)) {
1077 pr("%s is out of supply, and cannot %s\n",
1078 prland(&land), att_mode[combat_mode]);
1081 if (def->type == EF_SHIP && first_time) {
1083 pr("You may board with a maximum of %d land units\n", maxland);
1085 pr("%s has a base %s value of %.0f\n",
1086 prland(&land), att_mode[combat_mode], att_val);
1087 if (land_answer[(int)land.lnd_army] != 'Y') {
1089 "%s with %s %s (%c %d%%) [ynYNq?] ",
1090 att_mode[combat_mode],
1093 land.lnd_army ? land.lnd_army : '~',
1095 land_answer[(int)land.lnd_army] =
1096 att_prompt(prompt, land.lnd_army);
1097 if (att_abort(combat_mode, off, def))
1099 if (land_answer[(int)land.lnd_army] != 'y' &&
1100 land_answer[(int)land.lnd_army] != 'Y')
1103 if (!(llp = malloc(sizeof(struct ulist)))) {
1104 logerror("Malloc failed in attack!\n");
1108 memset(llp, 0, sizeof(struct ulist));
1109 emp_insque(&llp->queue, olist);
1110 llp->mobil = mobcost;
1111 llp->unit.land = land;
1112 llp->x = llp->unit.land.lnd_x;
1113 llp->y = llp->unit.land.lnd_y;
1114 llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
1115 llp->eff = llp->unit.land.lnd_effic;
1116 if (lnd_spyval(&land) > *a_spyp)
1117 *a_spyp = lnd_spyval(&land);
1118 if (((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)
1120 if (def->type == EF_SHIP && ++count >= maxland)
1125 /* What's the offense or defense multiplier? */
1128 att_combat_eff(struct combat *com)
1134 if (com->type == EF_SECTOR) {
1135 eff = com->eff / 100.0;
1136 if (com->own == player->cnum) {
1137 str = com->sct_dcp->d_ostr;
1138 eff = 1.0 + (str - 1.0) * eff;
1140 eff = sector_strength(getsectp(com->x, com->y));
1141 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1142 getship(com->shp_uid, &ship);
1143 eff = 1.0 + shp_armor(&ship) / 100.0;
1149 att_get_offense(int combat_mode, struct combat *off,
1150 struct emp_qelem *olist, struct combat *def)
1155 * Get the attacker units & mil again in case they changed while the
1156 * attacker was answering sub-prompts.
1159 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1160 if (att_empty_attack(combat_mode, ototal, def))
1161 return abort_attack();
1162 if (combat_mode == A_PARA)
1164 pr("\n Initial attack strength: %8d\n", ototal);
1168 /* Get the defensive units and reacting units */
1170 att_get_defense(struct emp_qelem *olist, struct combat *def,
1171 struct emp_qelem *dlist, int a_spy, int ototal)
1174 struct emp_qelem *qp;
1180 get_dlist(def, dlist, 0, &d_spy);
1181 dtotal = get_dtotal(def, dlist, 1.0, 0);
1184 * Call in reacting units
1187 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1188 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1190 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1191 llp = (struct ulist *)qp;
1192 intelligence_report(def->own, &llp->unit.land, d_spy,
1193 "Scouts report attacking unit:");
1196 old_dtotal = dtotal;
1197 dtotal = get_dtotal(def, dlist, 1.0, 0);
1198 if (dtotal != old_dtotal)
1199 pr("Defense strength with reacting units: %8d\n", dtotal);
1204 /* Get the defensive land units in the sector or on the ship */
1207 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1210 struct nstr_item ni;
1214 /* In here is where you need to take out spies and trains from the defending
1215 lists. Spies try to hide, trains get trapped and can be boarded. */
1217 snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1218 while (nxtitem(&ni, &land)) {
1221 if (land.lnd_own != def->own)
1223 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1225 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1227 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1229 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1231 intelligence_report(player->cnum, &land, a_spy,
1232 "Scouts report defending unit:");
1233 if (!(llp = malloc(sizeof(struct ulist)))) {
1234 logerror("Malloc failed in attack!\n");
1238 memset(llp, 0, sizeof(struct ulist));
1239 emp_insque(&llp->queue, list);
1240 llp->supplied = lnd_supply_all(&land);
1241 llp->unit.land = land;
1242 llp->x = llp->unit.land.lnd_x;
1243 llp->y = llp->unit.land.lnd_y;
1244 llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
1245 llp->eff = llp->unit.land.lnd_effic;
1246 if (lnd_spyval(&land) > *d_spyp)
1247 *d_spyp = lnd_spyval(&land);
1251 /* Calculate the total offensive strength */
1254 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1255 double osupport, int check)
1257 double ototal = 0.0;
1258 struct emp_qelem *qp, *next;
1263 * first, total the attacking mil
1266 for (n = 0; n <= off->last; ++n) {
1267 if (off[n].type == EF_BAD || (check &&
1268 att_get_combat(&off[n], 0) <= 0))
1270 ototal += off[n].troops * att_combat_eff(off + n);
1274 * next, add in the attack_values of all
1275 * the attacking units
1278 for (qp = olist->q_forw; qp != olist; qp = next) {
1280 llp = (struct ulist *)qp;
1281 if (check && !get_oland(combat_mode, llp))
1283 if (combat_mode == A_ATTACK) {
1285 for (n = 0; n <= off->last; ++n) {
1286 if (off[n].type == EF_BAD)
1288 if ((off[n].x == llp->unit.land.lnd_x) &&
1289 (off[n].y == llp->unit.land.lnd_y))
1293 lnd_delete(llp, "is in a sector not owned by you");
1296 ototal += attack_val(combat_mode, &llp->unit.land) *
1297 att_combat_eff(off + w);
1299 ototal += attack_val(combat_mode, &llp->unit.land);
1304 return ldround(ototal, 1);
1307 /* Calculate the total defensive strength */
1310 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1313 double dtotal = 0.0, eff = 1.0, d_unit;
1314 struct emp_qelem *qp, *next;
1317 if (check && att_get_combat(def, 1) < 0)
1319 eff = att_combat_eff(def);
1320 dtotal = def->troops * eff;
1323 * next, add in the defense_values of all
1324 * the defending non-retreating units
1327 for (qp = list->q_forw; qp != list; qp = next) {
1329 llp = (struct ulist *)qp;
1330 if (check && !get_dland(def, llp))
1332 d_unit = defense_val(&llp->unit.land);
1335 dtotal += d_unit * eff;
1340 return ldround(dtotal, 1);
1344 * This is the land unit integrity check.
1348 get_oland(int combat_mode, struct ulist *llp)
1350 struct lndstr *lp = &llp->unit.land;
1353 getland(llp->unit.land.lnd_uid, lp);
1355 if (lp->lnd_own != player->cnum) {
1356 sprintf(buf, "was destroyed and is no longer a part of the %s",
1357 att_mode[combat_mode]);
1358 lnd_delete(llp, buf);
1361 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1363 "left to fight another battle and is no longer a part of the %s",
1364 att_mode[combat_mode]);
1365 lnd_delete(llp, buf);
1368 if (lp->lnd_effic < llp->eff) {
1369 sprintf(buf, "damaged from %d%% to %d%%",
1370 llp->eff, lp->lnd_effic);
1371 lnd_print(llp, buf);
1374 llp->eff = llp->unit.land.lnd_effic;
1379 get_dland(struct combat *def, struct ulist *llp)
1381 struct lndstr *lp = &llp->unit.land;
1384 getland(llp->unit.land.lnd_uid, lp);
1386 if (lp->lnd_effic < LAND_MINEFF) {
1387 sprintf(buf, "was destroyed and is no longer a part of the defense");
1388 lnd_delete(llp, buf);
1391 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1393 "left to go fight another battle and is no longer a part of the defense");
1397 llp->eff = llp->unit.land.lnd_effic;
1402 kill_land(struct emp_qelem *list)
1404 struct emp_qelem *qp, *next;
1407 for (qp = list->q_forw; qp != list; qp = next) {
1409 llp = (struct ulist *)qp;
1410 if (llp->unit.land.lnd_ship >= 0) {
1411 llp->unit.land.lnd_effic = 0;
1412 lnd_delete(llp, "cannot return to the ship, and dies!");
1418 att_infect_units(struct emp_qelem *list, int plague)
1420 struct emp_qelem *qp, *next;
1425 for (qp = list->q_forw; qp != list; qp = next) {
1427 llp = (struct ulist *)qp;
1428 if (llp->unit.land.lnd_pstage == PLG_HEALTHY)
1429 llp->unit.land.lnd_pstage = PLG_EXPOSED;
1434 * Put the land unit on the disk. If there was some mobility cost, then
1435 * subtract it from the units mobility. Note that this works the same way
1436 * as sectors & ships in that no mobility is actually taken until the attacker
1437 * has committed to attacking.
1441 put_land(struct emp_qelem *list)
1443 struct emp_qelem *qp, *next;
1446 for (qp = list->q_forw; qp != list; qp = next) {
1448 llp = (struct ulist *)qp;
1449 llp->unit.land.lnd_mission = 0;
1450 llp->unit.land.lnd_harden = 0;
1451 llp->unit.land.lnd_mobil -= (int)llp->mobil;
1453 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1454 if (llp->unit.land.lnd_own != player->cnum) {
1455 emp_remque((struct emp_qelem *)llp);
1458 get_oland(A_ATTACK, llp);
1463 * Keep sending in reinforcements until it looks like we're going to win.
1464 * Note that the "strength" command also calls this routine.
1468 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1469 int *d_spyp, int ototal)
1471 struct nstr_item ni;
1473 struct sctstr sect, dsect;
1476 double new_land = 0;
1480 double eff = att_combat_eff(def);
1484 dtotal = get_dtotal(def, list, 1.0, 1);
1487 snxtitem_all(&ni, EF_LAND);
1488 while (nxtitem(&ni, &land) && dtotal + new_land * eff < 1.2 * ototal) {
1491 if (land.lnd_mission != MI_RESERVE)
1493 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1495 if (land.lnd_own != def->own)
1497 if (land.lnd_ship >= 0)
1499 if (land.lnd_land >= 0)
1501 if (defense_val(&land) < 1.0)
1503 if (!lnd_can_attack(&land))
1506 /* Only supplied units can react */
1507 if (list ? !lnd_supply_all(&land) : !lnd_could_be_supplied(&land))
1510 if (!in_oparea((struct empobj *)&land, def->x, def->y))
1513 getsect(land.lnd_x, land.lnd_y, §);
1514 getsect(def->x, def->y, &dsect);
1515 if (!BestLandPath(buf, §, &dsect, &pathcost,
1516 lnd_mobtype(&land)))
1519 mobcost = lnd_pathcost(&land, pathcost);
1520 if (land.lnd_mobil < mobcost)
1523 new_land += defense_val(&land);
1525 if (!list) /* we are in the "strength" command */
1528 /* move to defending sector */
1529 land.lnd_mobil -= ldround(mobcost, 1);
1532 land.lnd_x = def->x;
1533 land.lnd_y = def->y;
1534 putland(land.lnd_uid, &land);
1535 wu(0, land.lnd_own, "%s reacts to %s.\n",
1536 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1538 llp = malloc(sizeof(struct ulist));
1540 memset(llp, 0, sizeof(struct ulist));
1544 llp->chrp = (struct empobj_chr *)&lchr[(int)land.lnd_type];
1545 llp->unit.land = land;
1546 llp->eff = land.lnd_effic;
1547 emp_insque(&llp->queue, list);
1548 if (lnd_spyval(&land) > *d_spyp)
1549 *d_spyp = lnd_spyval(&land);
1551 intelligence_report(player->cnum, &land, a_spy,
1552 "Scouts sight reacting enemy unit:");
1557 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1560 get_osupport(char *outs, struct combat *def, int fort_sup, int ship_sup,
1561 int land_sup, int plane_sup)
1563 double osupport = 1.0;
1565 double af, as, au, ap;
1567 af = as = au = ap = 0.0;
1569 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1574 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1581 dam = lnd_support(def->own, player->cnum, def->x, def->y, 0);
1587 dam = off_support(def->x, def->y, def->own, player->cnum);
1591 sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1596 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1599 get_dsupport(char *outs, struct emp_qelem *list, struct combat *def,
1600 int ototal, int dtotal)
1602 double dsupport = 1.0;
1604 double df, ds, du, dp;
1607 df = ds = du = dp = 0.0;
1608 if (dtotal < 0.1 * ototal) {
1610 } else if (dtotal >= 1.2 * ototal) {
1613 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1617 dtotal = get_dtotal(def, list, dsupport, 0);
1618 if (dtotal < 1.2 * ototal) {
1619 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1622 dtotal = get_dtotal(def, list, dsupport, 0);
1624 if (dtotal < 1.2 * ototal) {
1625 dam = lnd_support(player->cnum, def->own, def->x, def->y, 1);
1628 dtotal = get_dtotal(def, list, dsupport, 1);
1630 if (dtotal < 1.2 * ototal) {
1631 dam = def_support(def->x, def->y, player->cnum, def->own);
1639 sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1644 "\nOdds are bad for us...support cancelled.\n\n");
1647 "\nOdds are good for us...support cancelled.\n\n");
1653 * Land mines add to the defense multiplier. If the attacker has engineers
1654 * then this multiplier is cut in half.
1658 get_mine_dsupport(struct combat *def, int a_engineer)
1663 getsect(def->x, def->y, §);
1665 if (sect.sct_oldown != player->cnum) {
1666 mines = SCT_LANDMINES(§);
1667 mines = MIN(mines, 20);
1669 mines = ldround(mines / 2.0, 1);
1672 wu(0, def->own, "Defending mines add %1.2f\n",
1674 pr("Defending mines add %1.2f\n", mines * 0.02);
1675 return mines * 0.02;
1681 /* Get the offensive and defensive support */
1683 att_get_support(int combat_mode, int ofort, int oship, int oland,
1684 int oplane, struct emp_qelem *olist, struct combat *off,
1685 struct emp_qelem *dlist, struct combat *def,
1686 double *osupportp, double *dsupportp, int a_engineer)
1689 char osupports[512];
1690 char dsupports[512];
1692 if (combat_mode == A_PARA)
1695 *osupportp = get_osupport(osupports, def,
1696 ofort, oship, oland, oplane);
1699 * I need to put a 1 at the end of the next four total_stren calls
1700 * because units & mil may have been damaged by collateral damage or
1701 * nuclear warheads from the offensive & defensive support.
1704 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1705 if (att_empty_attack(combat_mode, ototal, def))
1706 return abort_attack();
1707 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1710 * Calculate defensive support. If odds are too good or too bad
1711 * then don't call in support.
1714 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1715 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1716 if (att_empty_attack(combat_mode, ototal, def))
1717 return abort_attack();
1719 if ((*osupports || *dsupports) &&
1720 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1721 pr("\n\t\tsupport values\n");
1722 pr("\t\tforts\tships\tunits\tplanes\n");
1723 if (*osupportp != 1.0)
1724 pr("%s", osupports);
1725 if (*dsupportp != 1.0)
1726 pr("%s", dsupports);
1728 wu(0, def->own, "\n\t\tsupport values\n");
1729 wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1730 if (*osupportp != 1.0)
1731 wu(0, def->own, "%s", osupports);
1732 if (*dsupportp != 1.0)
1733 wu(0, def->own, "%s", dsupports);
1737 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1738 if (dtotal && def->type == EF_SECTOR)
1739 *dsupportp += get_mine_dsupport(def, a_engineer);
1743 /* How many two-legged bipeds are in this combat force? */
1746 count_bodies(struct combat *off, struct emp_qelem *list)
1750 struct emp_qelem *qp;
1753 for (n = 0; n <= off->last; ++n)
1754 bodies += off[n].troops;
1755 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1756 llp = (struct ulist *)qp;
1757 bodies += llp->unit.land.lnd_item[I_MILIT];
1762 /* This is where the fighting actually occurs. */
1765 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1766 double osupport, struct combat *def, struct emp_qelem *dlist,
1770 int a_cas = 0; /* Casualty counts */
1772 int ototal; /* total attacking strength */
1773 int dtotal; /* total defending strength */
1774 int a_bodies; /* total attacking mil (incl. mil in units) */
1775 int d_bodies; /* total defending mil (incl. mil in units) */
1785 ototal = get_ototal(combat_mode, off, olist, osupport,
1786 combat_mode != A_PARA);
1787 dtotal = get_dtotal(def, dlist, dsupport, 0);
1791 a_bodies = count_bodies(off, olist);
1792 d_bodies = count_bodies(def, dlist);
1793 d_mil = def->troops;
1794 for (n = 0; n <= off->last; ++n)
1795 if (off[n].type == EF_BAD)
1798 a_troops[n] = off[n].troops;
1800 /* This switch is required to get the spacing right */
1801 switch (combat_mode) {
1803 pr(" Final attack strength: %8d\n", ototal);
1806 pr(" Final assault strength: %8d\n", ototal);
1809 if (def->sct_type == SCT_MOUNT ||
1810 def->sct_type == SCT_WATER ||
1811 def->sct_type == SCT_CAPIT ||
1812 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1813 pr("You can't air-assault a %s sector!\n",
1814 def->sct_dcp->d_name);
1817 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1819 pr(" Final air-assault strength: %8d\n", ototal);
1823 pr(" Final board strength: %8d\n", ototal);
1827 pr(" Final defense strength: %8d\n", dtotal);
1828 odds = att_calcodds(ototal, dtotal);
1829 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1831 /* spread the plague */
1832 if (combat_mode != A_PARA) {
1834 for (n = 0; n <= off->last; ++n)
1835 if (off[n].type != EF_BAD)
1836 def->plague |= off[n].plague;
1837 for (n = 0; n <= off->last; ++n)
1838 if (off[n].type != EF_BAD)
1839 off[n].plague |= def->plague;
1841 att_infect_units(olist, off->plague);
1842 att_infect_units(dlist, def->plague);
1845 * Fighting is slightly random. There is always that last little
1846 * effort you see people put in. Or the stray bullet that takes out
1847 * an officer and the rest go into chaos. Things like that.
1848 * Thus, we have added a very slight random factor that will sometimes
1849 * allow the little guy to win. We modify the odds a little
1850 * (either +- 5%) to account for this randomness. We also only
1851 * recalculate the odds every 8-50 casualties, not every cacsualty,
1852 * since a single dead guy normally wouldn't cause a commander to
1853 * rethink his strategies, but 50 dead guys might.
1855 odds += (random() % 11 - 5) / 100.0;
1860 recalctime = 8 + (random() % 43);
1861 while (!success && ototal) {
1864 d_cas += take_casualty(A_DEFEND, def, dlist);
1865 dtotal = get_dtotal(def, dlist, dsupport, 0);
1870 a_cas += take_casualty(combat_mode, off, olist);
1871 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1873 if (((a_cas + d_cas) % 70) == 69)
1875 if (recalctime-- <= 0) {
1876 recalctime = 8 + (random() % 43);
1877 odds = att_calcodds(ototal, dtotal);
1878 odds += (random() % 11 - 5) / 100.0;
1886 /* update defense mobility & mil */
1890 if (def->type == EF_SECTOR && d_mil && d_cas) {
1894 newmob = damage(def->mob, 100 * d_cas / d_mil);
1895 def->mobcost = MIN(20, def->mob - newmob);
1898 def->mil = def->troops;
1901 /* update attack mobility & mil */
1902 for (n = 0; n <= off->last; ++n)
1903 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1904 if (off[n].type == EF_SECTOR && off[n].mil) {
1905 if (!CANT_HAPPEN(off[n].mob < 0)) {
1906 newmob = damage(off[n].mob,
1907 100 * (a_troops[n] - off[n].troops)
1909 off[n].mobcost += MIN(20, off[n].mob - newmob);
1912 off[n].mil -= a_troops[n] - off[n].troops;
1915 /* update land unit mobility */
1916 if (d_bodies && d_cas)
1917 lnd_takemob(dlist, (double)d_cas / d_bodies);
1918 if (a_bodies && a_cas)
1919 lnd_takemob(olist, (double)a_cas / a_bodies);
1921 /* damage attacked sector */
1922 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1924 pr("- Casualties -\n Yours: %d\n", a_cas);
1925 pr(" Theirs: %d\n", d_cas);
1926 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
1927 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
1930 switch (combat_mode) {
1932 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
1933 pr("We have captured %s, sir!\n", prcom(0, def));
1937 news_item = def->own ? N_AWON_SECT : N_START_COL;
1938 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
1939 action = "assaulting and taking";
1942 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
1943 pr("We have captured %s, sir!\n", prcom(0, def));
1944 action = "air-assaulting and taking";
1947 news_item = N_BOARD_SHIP;
1948 pr("We have boarded %s, sir!\n", prcom(0, def));
1949 action = "boarding";
1952 news_item = N_BOARD_LAND;
1953 pr("We have boarded %s, sir!\n", prcom(0, def));
1954 action = "boarding";
1959 action = "defeating";
1962 switch (combat_mode) {
1964 news_item = N_SCT_LOSE;
1965 pr("You have been defeated!\n");
1966 action = "attacking";
1969 news_item = N_ALOSE_SCT;
1970 pr("You have been defeated!\n");
1972 action = "trying to assault";
1975 news_item = N_PLOSE_SCT;
1976 pr("All of your troops were destroyed\n");
1977 action = "trying to air-assault";
1980 news_item = N_SHP_LOSE;
1981 pr("You have been repelled\n");
1983 action = "trying to board";
1986 news_item = N_LND_LOSE;
1987 pr("You have been repelled\n");
1989 action = "trying to board";
1994 action = "fighting";
1997 nreport(player->cnum, news_item, def->own, 1);
2000 "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
2001 cname(player->cnum), player->cnum, a_cas,
2002 action, pr_com(0, def, def->own), d_cas);
2005 send_reacting_units_home(dlist);
2007 /* putland the defending land */
2010 /* putland the attacking land */
2013 /* put the victim sector/ship/land */
2014 if (!success || !take_def(combat_mode, olist, off, def))
2017 /* put the attacking sectors/ship */
2018 for (n = 0; n <= off->last; ++n)
2019 if (off[n].type != EF_BAD)
2020 put_combat(&off[n]);
2025 switch (combat_mode) {
2027 ask_move_in(off, olist, def);
2029 /* put sectors again to get abandon warnings */
2030 for (n = 0; n <= off->last; ++n)
2031 if (off[n].type != EF_BAD)
2032 put_combat(&off[n]);
2035 att_move_in_off(combat_mode, off, olist, def);
2038 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2042 /* What percentage of the combat forces going head-to-head are we? */
2045 att_calcodds(int ototal, int dtotal)
2049 /* calculate odds */
2052 else if (dtotal <= 0)
2055 odds = (double)ototal / (dtotal + ototal);
2060 /* Here's where the dead soldiers get dragged off the battlefield */
2063 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2065 int to_take = CASUALTY_LUMP;
2066 int biggest_troops = 0, index = -1;
2067 int n, tot_troops = 0, biggest_mil, cas;
2068 struct emp_qelem *qp, *biggest;
2071 for (n = 0; n <= off->last; ++n) {
2072 if (off[n].type != EF_BAD) {
2073 tot_troops += off[n].troops;
2074 if (off[n].troops > biggest_troops) {
2075 biggest_troops = off[n].troops;
2082 to_take -= tot_troops;
2085 for (n = 0; n <= off->last; ++n)
2086 if (off[n].type != EF_BAD)
2090 * They can all come off mil. We rotate the casualties,
2091 * starting with the sector containing the most mil.
2093 to_take = CASUALTY_LUMP;
2095 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2098 while (to_take > 0) {
2099 for (n = index; n <= off->last && to_take; ++n) {
2100 if (off[n].type != EF_BAD && off[n].troops > 0) {
2105 for (n = 0; n < index && to_take; ++n) {
2106 if (off[n].type != EF_BAD && off[n].troops > 0) {
2112 return CASUALTY_LUMP;
2116 return CASUALTY_LUMP - to_take;
2119 * Need to take some casualties from attacking units
2120 * Procedure: find the biggest unit remaining (in
2121 * terms of mil) and give it the casualties.
2125 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2126 llp = (struct ulist *)qp;
2128 if (llp->unit.land.lnd_item[I_MILIT] > biggest_mil) {
2129 biggest_mil = llp->unit.land.lnd_item[I_MILIT];
2133 if (biggest == NULL)
2134 return CASUALTY_LUMP - to_take;
2136 llp = (struct ulist *)biggest;
2137 cas = lnd_take_casualty(combat_mode, llp, to_take);
2138 return CASUALTY_LUMP - (to_take - cas);
2141 /* Send reacting defense units back to where they came from (at no mob cost) */
2144 send_reacting_units_home(struct emp_qelem *list)
2146 struct emp_qelem *qp, *next;
2150 for (qp = list->q_forw; qp != list; qp = next) {
2152 llp = (struct ulist *)qp;
2153 if ((llp->unit.land.lnd_x != llp->x) ||
2154 (llp->unit.land.lnd_y != llp->y)) {
2155 sprintf(buf, "returns to %s",
2156 xyas(llp->x, llp->y, llp->unit.land.lnd_own));
2157 llp->unit.land.lnd_x = llp->x;
2158 llp->unit.land.lnd_y = llp->y;
2159 lnd_delete(llp, buf);
2164 /* Check for 0 offense strength. This call will always preceed an abort */
2167 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2170 if (def->own && player->cnum != def->own) {
2172 "%s (#%d) considered %sing you @%s\n",
2173 cname(player->cnum), player->cnum,
2174 att_mode[combat_mode], xyas(def->x, def->y, def->own));
2176 pr("No troops for %s...\n", att_mode[combat_mode]);
2183 * Take the defending sector or ship from the defender and give it to the
2188 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2193 struct ulist *llp, *delete_me = NULL;
2199 for (n = 0; n <= off->last && !occuppied; ++n) {
2200 if (off[n].type != EF_BAD &&
2201 off[n].troops > 0 &&
2202 (off[n].type != EF_SECTOR || off[n].mob)) {
2204 if (def->type == EF_LAND) {
2205 if (def->lnd_lcp->l_flags & L_SPY) {
2212 pr("1 mil from %s moves %s\n",
2213 prcom(0, off + n), prcom(2, def));
2218 pr("%s left unoccupied\n", prcom(0, def));
2221 "No enemy troops moved %s so you still own it!\n",
2222 pr_com(2, def, def->own));
2225 llp = (struct ulist *)list->q_forw;
2226 llp->unit.land.lnd_x = def->x;
2227 llp->unit.land.lnd_y = def->y;
2228 take_move_in_mob(combat_mode, llp, off, def);
2229 if (def->type == EF_SHIP) {
2230 llp->unit.land.lnd_ship = def->shp_uid;
2231 sprintf(buf, "boards %s", prcom(0, def));
2234 llp->unit.land.lnd_ship = -1;
2235 sprintf(buf, "moves in to occupy %s",
2236 xyas(def->x, def->y, player->cnum));
2237 lnd_delete(llp, buf);
2242 if (def->type == EF_SECTOR) {
2243 getsect(def->x, def->y, §);
2244 takeover(§, player->cnum);
2245 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2246 caploss(§, def->own,
2247 "* We have captured %s's capital, sir! *\n");
2249 } else if (def->type == EF_SHIP) {
2250 getship(def->shp_uid, &ship);
2251 takeover_ship(&ship, player->cnum);
2252 putship(ship.shp_uid, &ship);
2253 } else if (def->type == EF_LAND) {
2254 getland(def->lnd_uid, &land);
2255 takeover_land(&land, player->cnum);
2256 putland(land.lnd_uid, &land);
2259 lnd_delete(delete_me, buf);
2260 att_get_combat(def, 0);
2265 * Ask the attacker which mil & land units they'd like to move into the
2270 ask_move_in(struct combat *off, struct emp_qelem *olist,
2274 struct emp_qelem *qp, *next;
2278 char land_answer[256];
2281 for (n = 0; n <= off->last; ++n)
2282 if (off[n].type != EF_BAD && off[n].troops > 0)
2284 ask_move_in_off(&off[n], def);
2285 if (player->aborted)
2291 memset(land_answer, 0, sizeof(land_answer));
2292 for (qp = olist->q_forw; qp != olist; qp = next) {
2294 llp = (struct ulist *)qp;
2295 answerp = &land_answer[(int)llp->unit.land.lnd_army];
2296 if (player->aborted || att_get_combat(def, 0) < 0)
2298 if (*answerp == 'Y')
2300 if (!get_oland(A_ATTACK, llp))
2302 if (*answerp != 'N') {
2303 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2304 prland(&llp->unit.land),
2305 llp->unit.land.lnd_army ? llp->unit.land.lnd_army : '~',
2306 llp->unit.land.lnd_effic);
2307 *answerp = att_prompt(prompt, llp->unit.land.lnd_army);
2308 if (player->aborted || att_get_combat(def, 0) < 0)
2310 if (!get_oland(A_ATTACK, llp))
2313 if (*answerp == 'y' || *answerp == 'Y')
2315 sprintf(buf, "stays in %s",
2316 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2318 lnd_delete(llp, buf);
2322 if (att_get_combat(def, 0) < 0) {
2323 for (qp = olist->q_forw; qp != olist; qp = next) {
2325 llp = (struct ulist *)qp;
2326 if (!get_oland(A_ATTACK, llp))
2328 sprintf(buf, "stays in %s",
2329 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2331 lnd_delete(llp, buf);
2335 if (opt_INTERDICT_ATT)
2336 lnd_interdict(olist, def->x, def->y, player->cnum);
2337 move_in_land(A_ATTACK, off, olist, def);
2340 /* Move offensive land units to the conquered sector or ship */
2343 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2346 struct emp_qelem *qp, *next;
2352 for (qp = olist->q_forw; qp != olist; qp = next) {
2354 llp = (struct ulist *)qp;
2355 if (!get_oland(combat_mode, llp))
2357 take_move_in_mob(combat_mode, llp, off, def);
2358 llp->unit.land.lnd_x = def->x;
2359 llp->unit.land.lnd_y = def->y;
2360 if (def->type == EF_SHIP)
2361 llp->unit.land.lnd_ship = def->shp_uid;
2363 llp->unit.land.lnd_ship = -1;
2367 if (def->type == EF_SECTOR) {
2368 if (opt_INTERDICT_ATT) {
2369 lnd_sweep(olist, 0, 0, def->own);
2370 lnd_check_mines(olist);
2372 sprintf(buf, "now occupies %s", prcom(0, def));
2374 sprintf(buf, "boards %s", prcom(0, def));
2378 for (qp = olist->q_forw; qp != olist; qp = next) {
2380 llp = (struct ulist *)qp;
2381 lnd_print(llp, buf);
2389 * Move assaulting, paradropping, or boarding mil & units into def
2390 * If the mil are coming from a ship, then pack a lunch.
2394 att_move_in_off(int combat_mode, struct combat *off,
2395 struct emp_qelem *olist, struct combat *def)
2402 move_in_land(combat_mode, off, olist, def);
2404 for (n = 0; n <= off->last; ++n) {
2405 if (off[n].type == EF_BAD || !off[n].troops)
2407 troops = off[n].troops;
2409 off[n].mil -= troops;
2411 put_combat(off + n);
2412 if (combat_mode == A_ASSAULT) {
2413 if (CANT_HAPPEN(off[n].type != EF_SHIP))
2415 getship(off[n].shp_uid, &ship);
2416 lunchbox += (int)((troops + 1) * ship.shp_item[I_FOOD]
2417 / (ship.shp_item[I_MILIT] + troops
2418 + ship.shp_item[I_CIVIL] + 0.5));
2420 ship.shp_item[I_FOOD] -= lunchbox;
2421 putship(ship.shp_uid, &ship);
2427 if (combat_mode == A_ASSAULT) {
2428 if (CANT_HAPPEN(def->type != EF_SECTOR))
2430 getsect(def->x, def->y, §);
2431 if (lunchbox > ITEM_MAX - sect.sct_item[I_FOOD])
2432 lunchbox = ITEM_MAX - sect.sct_item[I_FOOD];
2433 sect.sct_item[I_FOOD] += lunchbox;
2439 /* Ask how many mil to move in from each sector */
2442 ask_move_in_off(struct combat *off, struct combat *def)
2445 int num_mil, dam = 0, left;
2451 if (att_get_combat(off, 0) <= 0)
2453 if (att_get_combat(def, 0) < 0)
2455 if (off->own != player->cnum)
2457 d = att_mobcost(off->own, def, MOB_MOVE);
2458 if ((mob_support = MIN(off->troops, (int)(off->mob / d))) <= 0)
2460 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2461 xyas(off->x, off->y, player->cnum), mob_support);
2462 p = getstring(prompt, buf);
2463 if (!p || !*p || (num_mil = atoi(p)) <= 0)
2465 /* Make sure we don't move in more than we can support mobility-wise */
2466 if (num_mil > mob_support)
2467 num_mil = mob_support;
2468 if (att_get_combat(off, 0) <= 0)
2470 if (att_get_combat(def, 0) < 0)
2472 if ((num_mil = MIN(off->troops, num_mil)) <= 0) {
2473 pr("No mil moved in from %s\n",
2474 xyas(off->x, off->y, player->cnum));
2477 mob_support = MAX(1, (int)(num_mil * d));
2478 off->mob -= MIN(off->mob, mob_support);
2479 off->mil -= num_mil;
2480 off->troops -= num_mil;
2483 weight = (double)num_mil * ichr[I_MILIT].i_lbs;
2484 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2485 if (chance(weight / 100.0))
2487 ground_interdict(def->x, def->y, player->cnum, "military");
2488 dam += check_lmines(def->x, def->y, weight);
2492 left = commdamage(num_mil, dam, I_MILIT);
2493 if (left < num_mil) {
2495 pr("%d of the mil you were moving were destroyed!\n"
2496 "Only %d mil made it to %s\n",
2497 num_mil - left, left,
2498 xyas(def->x, def->y, player->cnum));
2500 pr("All of the mil you were moving were destroyed!\n");
2503 /* maybe got nuked */
2504 if (att_get_combat(def, 0) < 0)
2512 /* Charge land units for moving into a sector or onto a ship */
2515 take_move_in_mob(int combat_mode, struct ulist *llp, struct combat *off,
2518 double mob = llp->unit.land.lnd_mobil;
2519 double gain = etu_per_update * land_mob_scale;
2520 double mobcost, moblim;
2522 switch (combat_mode) {
2524 mobcost = lnd_pathcost(&llp->unit.land,
2525 att_mobcost(off->own, def,
2526 lnd_mobtype(&llp->unit.land)));
2530 * Set mobcost to basic assault cost, moblim to maximum
2531 * mobility to keep when assaulting from non-landing ship
2533 if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE) {
2534 mobcost = gain / 2.0;
2540 if (!(off->shp_mcp->m_flags & M_LAND))
2541 /* Not a landing ship, ensure we go to or below moblim */
2542 mobcost = MAX(mobcost, mob - moblim);
2545 if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE)
2558 llp->unit.land.lnd_mobil = (signed char)mob;
2559 llp->unit.land.lnd_harden = 0;
2563 free_list(struct emp_qelem *list)
2565 struct emp_qelem *qp, *next;
2567 if (!list || QEMPTY(list))
2571 while (qp != list) {
2580 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2588 * sector_strength - Everyone starts at 1. You can get up to a max
2589 * of d_dstr, depending on how much you build up the
2590 * defenses of the sector.
2594 sector_strength(struct sctstr *sp)
2596 double def = SCT_DEFENSE(sp) / 100.0;
2597 double base = sp->sct_type == SCT_MOUNT ? 2.0 : 1.0;
2598 double d = base + (dchr[sp->sct_type].d_dstr - base) * def;
2600 if (d > dchr[sp->sct_type].d_dstr)
2601 d = dchr[sp->sct_type].d_dstr;