2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * attsub.c: Attack subroutines
29 * Known contributors to this file:
31 * Steve McClure, 1996-2000
32 * Markus Armbruster, 2006-2013
52 #include "prototypes.h"
56 #define CASUALTY_LUMP 1 /* How big casualty chunks should be */
58 static void ask_olist(int combat_mode, struct combat *off,
59 struct combat *def, struct emp_qelem *olist,
60 char *land_answer, int *a_spyp, int *a_engineerp);
61 static void take_move_in_mob(int combat_mode, struct ulist *llp,
62 struct combat *off, struct combat *def);
63 static void move_in_land(int combat_mode, struct combat *off,
64 struct emp_qelem *olist, struct combat *def);
65 static void ask_move_in(struct combat *off, struct emp_qelem *olist,
67 static void ask_move_in_off(struct combat *off, struct combat *def);
69 static int board_abort(struct combat *off, struct combat *def);
70 static int land_board_abort(struct combat *off, struct combat *def);
71 static int ask_off(int combat_mode, struct combat *off,
73 static void get_dlist(struct combat *def, struct emp_qelem *, int a_spy,
75 static int get_ototal(int combat_mode, struct combat *off,
76 struct emp_qelem *olist, double osupport, int check);
77 static int get_dtotal(struct combat *def, struct emp_qelem *list,
78 double dsupport, int check);
79 static double att_calcodds(int, int);
80 static int take_casualty(int combat_mode, struct combat *off,
81 struct emp_qelem *olist);
83 static void send_reacting_units_home(struct emp_qelem *list);
84 static int take_def(int combat_mode, struct emp_qelem *list,
85 struct combat *off, struct combat *def);
87 static int get_oland(int, struct ulist *);
88 static int get_dland(struct combat *, struct ulist *);
91 /* must match combat types in combat.h */
92 "defend", "attack", "assault", "paradrop", "board", "lboard"
97 * The principal object in this code is the "combat" object. A combat object
98 * is either a sector or ship. There are
99 * usually two instances of this, the "def" or defense combat object, and
100 * the array of "off" or offense objects. The number of offense objects is
101 * determined by the value of off->last (e.g. more than one attacking sector).
102 * the type of the object is determined by combat->type which can take the
103 * values EF_SECTOR, EF_SHIP, EF_PLANE, or EF_BAD. Another important parameter
104 * which is often passed to these functions is combat_mode. This can take
105 * the value A_DEFEND, A_ATTACK, A_ASSAULT, A_PARA, A_BOARD and A_LBOARD.
106 * As these six modes of being in combat affect things like mobcost and combat
107 * value, there are often switches made on combat_mode. Note that in all cases
108 * no mobility is taken from sectors, ships, or land units until the player
109 * has committed to a fight. Instead, the cost is temporarily placed in
110 * combat->mobcost, or llp->mobil as the case may be, and then when the object
111 * is "put" back onto disk, then the amounts in these variables are subtracted
112 * from the object's mobility. It needs to be done this way as the objects
113 * are constantly being re-read from disk, and we don't want to take any mob
114 * unless a fight actually occurrs.
118 /* initialize combat object */
121 att_combat_init(struct combat *com, int type)
123 memset(com, 0, sizeof(*com));
128 /* print a combat object with optional preposition */
131 pr_com(int inon, struct combat *com, natid who)
133 if (com->type == EF_SECTOR) {
135 inon ? inon == 1 ? "in " : "into " : "",
136 xyas(com->x, com->y, who));
137 } else if (com->type == EF_SHIP) {
138 return prbuf("%s%s %s(#%d)",
139 inon ? inon == 1 ? "on " : "onto " : "",
140 com->shp_mcp->m_name, com->shp_name, com->shp_uid);
141 } else if (com->type == EF_LAND) {
142 return prbuf("%s%s #%d",
143 inon ? inon == 1 ? "on " : "onto " : "",
144 com->lnd_lcp->l_name, com->lnd_uid);
146 return "your forces";
151 prcom(int inon, struct combat *com)
153 return pr_com(inon, com, player->cnum);
157 * This is the combat object "type" based integrity check. It basically
158 * splits along three divisions: ship/sector, attacker/defender,
159 * first time/not first time.
163 att_get_combat(struct combat *com, int isdef)
177 if (!getsect(com->x, com->y, §)) {
178 pr("Bad sector: %s\n", xyas(com->x, com->y, player->cnum));
179 return att_combat_init(com, EF_BAD);
181 com->sct_type = sect.sct_type;
182 com->sct_dcp = &dchr[sect.sct_type];
183 mil = sect.sct_item[I_MILIT];
184 pstage = sect.sct_pstage;
185 owner = sect.sct_own;
186 eff = sect.sct_effic;
187 mob = sect.sct_mobil;
192 if (!getland(com->lnd_uid, &land) || !land.lnd_own) {
194 pr("Land unit #%d is not in the same sector!\n",
196 return att_combat_init(com, EF_BAD);
198 if (isdef && player->owner) {
199 pr("Boarding yourself? Try using the 'load' command.\n");
200 return att_combat_init(com, EF_BAD);
202 com->lnd_lcp = &lchr[(int)land.lnd_type];
203 mil = land.lnd_item[I_MILIT];
204 pstage = land.lnd_pstage;
205 owner = land.lnd_own;
206 eff = land.lnd_effic;
207 mob = land.lnd_mobil;
212 if (!getship(com->shp_uid, &ship) || !ship.shp_own) {
214 pr("Ship #%d is not in the same sector!\n", com->shp_uid);
216 pr("Ship #%d is not your ship!\n", com->shp_uid);
217 return att_combat_init(com, EF_BAD);
220 if (isdef && player->owner && ontradingblock(EF_SHIP, &ship)) {
221 pr("%s is on the trading block.\n", prcom(0, com));
222 return att_combat_init(com, EF_BAD);
225 if (isdef && player->owner) {
226 pr("Boarding yourself? Try using the 'tend' command.\n");
227 return att_combat_init(com, EF_BAD);
229 com->shp_mcp = &mchr[(int)ship.shp_type];
230 strncpy(com->shp_name, ship.shp_name, MAXSHPNAMLEN);
231 if (!isdef && !player->owner) {
233 pr("%s was just sunk!\n", prcom(0, com));
235 pr("Ship #%d is not your ship!\n", com->shp_uid);
236 return att_combat_init(com, EF_BAD);
238 mil = ship.shp_item[I_MILIT];
239 pstage = ship.shp_pstage;
240 owner = ship.shp_own;
241 eff = ship.shp_effic;
242 mob = ship.shp_mobil;
251 return att_combat_init(com, EF_BAD);
254 if (!com->set) { /* first time */
255 if (isdef) { /* defender */
257 } else { /* attacker */
259 pr("No mil %s\n", prcom(1, com));
261 pr("Only 1 mil %s\n", prcom(1, com));
262 /* don't abandon attacking sectors or ships */
263 com->troops = MAX(0, mil - 1);
265 com->plague = pstage == PLG_INFECT;
266 } else { /* not first time */
267 if (isdef) { /* defender */
268 if (com->x != x || com->y != y) {
269 pr("%s has moved!\n", prcom(0, com));
270 return att_combat_init(com, EF_BAD);
272 if (owner != com->own) {
274 pr("WARNING: The ownership of %s just changed from %s to %s!\n",
275 prcom(0, com), cname(com->own), cname(owner));
276 } else if (com->type == EF_SECTOR) {
277 pr("WARNING: %s just abandoned sector %s!\n",
279 xyas(com->x, com->y, player->cnum));
283 pr("WARNING: The enemy mil %s just %s from %d to %d!\n",
285 com->mil < mil ? "increased" : "decreased", com->mil,
288 } else { /* attacker */
289 if (owner != com->own && owner != player->cnum) {
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("%s just took %s!\n",
297 cname(owner), 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)
441 if (att_get_combat(def, 1) < 0)
442 return abort_attack();
444 if (off && combat_mode != A_ATTACK) {
445 if (att_get_combat(off, 0) < 0)
446 return abort_attack();
447 if (off->type == EF_SHIP &&
448 !(off->x == def->x && off->y == def->y) &&
449 (!getsect(off->x, off->y, §) ||
450 sect.sct_type != SCT_WATER)) {
451 pr("%s can not %s from that far inland!\n",
452 prcom(0, off), att_mode[combat_mode]);
453 return abort_attack();
456 switch (combat_mode) {
458 if (!neigh(def->x, def->y, player->cnum) &&
459 !adj_units(def->x, def->y, player->cnum)) {
460 pr("You are not adjacent to %s\n",
461 xyas(def->x, def->y, player->cnum));
462 return abort_attack();
464 if (def->own == player->cnum) {
465 pr("You can't attack your own sector.\n");
466 return abort_attack();
470 if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
471 pr("You'll have to get there first...\n");
472 return abort_attack();
474 if (off && def->sct_type == SCT_MOUNT) {
475 pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
476 return abort_attack();
480 if (def->own == player->cnum) {
481 pr("You can't air-assault your own sector.\n");
482 return abort_attack();
484 if (off && (def->sct_type == SCT_MOUNT ||
485 def->sct_type == SCT_WATER ||
486 def->sct_type == SCT_CAPIT ||
487 def->sct_type == SCT_FORTR ||
488 def->sct_type == SCT_WASTE)) {
489 pr("You can't air-assault a %s sector!\n",
490 def->sct_dcp->d_name);
491 return abort_attack();
495 return board_abort(off, def);
497 return land_board_abort(off, def);
500 if (off && def->sct_dcp->d_mob0 < 0) {
501 pr("You can't %s a %s sector!\n",
502 att_mode[combat_mode], def->sct_dcp->d_name);
503 return abort_attack();
505 if (!off || off->relations_checked)
507 off->relations_checked = 1;
510 setcont(player->cnum, def->own, FOUND_SPY);
511 setcont(def->own, player->cnum, FOUND_SPY);
518 * Lots of special things need to be checked for boarding, so I put it in
523 board_abort(struct combat *off, struct combat *def)
525 struct shpstr aship, dship; /* for tech levels */
528 if (att_get_combat(def, 1) < 0)
529 return abort_attack();
534 if (att_get_combat(off, 0) < 0)
535 return abort_attack();
537 if (off->x != def->x || off->y != def->y) {
538 pr("Ship #%d is not in the same sector!\n", def->shp_uid);
539 return abort_attack();
541 if (off->type == EF_SHIP) {
543 pr("%s has no mobility!\n", prcom(0, off));
544 return abort_attack();
546 getship(off->shp_uid, &aship);
547 getship(def->shp_uid, &dship);
548 if (techfact(aship.shp_tech, shp_speed(&aship)) * off->eff
549 <= techfact(dship.shp_tech, shp_speed(&dship)) * def->eff) {
550 pr("Victim ship moves faster than you do!\n");
553 "%s %s failed to catch %s\n",
554 prnatid(aship.shp_own),
555 pr_com(0, off, def->own), pr_com(0, def, def->own));
556 return abort_attack();
558 } else if (off->type != EF_SECTOR) {
559 pr("Please tell the deity that you got the 'banana boat' error\n");
560 return abort_attack();
562 if (def->shp_mcp->m_flags & M_SUB) {
563 getsect(def->x, def->y, §);
564 if (sect.sct_type == SCT_WATER) {
565 pr("You can't board a submarine!\n");
566 return abort_attack();
573 * Lots of special things need to be checked for boarding, so I put it in
575 * STM - I copied it for land unit boarding. :)
579 land_board_abort(struct combat *off, struct combat *def)
581 if (att_get_combat(def, 1) < 0)
582 return abort_attack();
587 if (att_get_combat(off, 0) < 0)
588 return abort_attack();
590 if (off->x != def->x || off->y != def->y) {
591 pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
592 return abort_attack();
598 /* If we are boarding, then the defending ship gets a chance to fire back */
600 att_approach(struct combat *off, struct combat *def)
606 pr("Approaching %s...\n", prcom(0, def));
609 "%s is being approached by %s...\n",
610 pr_com(0, def, def->own), pr_com(0, off, def->own));
611 if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
614 pr("They're firing at us sir!\n");
617 "Your fleet at %s does %d damage to %s\n",
618 xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
620 if (off->type == EF_SECTOR) {
621 getsect(off->x, off->y, §);
622 sectdamage(§, dam);
624 pr("Enemy fleet at %s does %d damage to %s\n",
625 xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
626 } else if (off->type == EF_SHIP) {
627 getship(off->shp_uid, &ship);
628 shipdamage(&ship, dam);
629 putship(off->shp_uid, &ship);
630 if (def->own && ship.shp_effic < SHIP_MINEFF) {
631 wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
632 nreport(player->cnum, N_SHP_LOSE, def->own, 1);
635 if (att_get_combat(off, 0) < 0)
636 return abort_attack();
640 /* The attack is valid. Tell the attacker about what they're going to hit */
643 att_show(struct combat *def)
645 /* Note that we tell the player about the treaty BEFORE we tell them
646 about the item. If we didn't, then they gain free information */
647 if (def->type == EF_SECTOR) {
648 if (!trechk(player->cnum, def->own, LANATT))
649 return abort_attack();
650 pr("%s is a %d%% %s %s with approximately %d military.\n",
651 xyas(def->x, def->y, player->cnum),
652 roundintby((int)def->eff, 10),
653 cname(def->own), def->sct_dcp->d_name,
654 roundintby(def->troops, 10));
655 if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
656 writemap(player->cnum);
657 } else if (def->type == EF_SHIP || def->type == EF_LAND) {
658 if (def->type == EF_SHIP) {
659 if (!trechk(player->cnum, def->own, SEAATT))
660 return abort_attack();
662 if (!trechk(player->cnum, def->own, LNDATT))
663 return abort_attack();
665 pr("%s is about %d%% efficient and has approximately %d mil on board.\n",
666 prcom(0, def), roundintby((int)def->eff, 10),
667 roundintby(def->troops, 10));
669 /* Ok, everything is fine */
673 /* Attack and assault ask the user which kind of support they want */
676 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
681 *fortp = *shipp = *landp = *planep = 1;
683 if (player->argp[offset] != NULL) {
684 if ((player->argp[offset + 1] == NULL) ||
685 (player->argp[offset + 2] == NULL) ||
686 (player->argp[offset + 3] == NULL)) {
687 pr("If any support arguments are used, all must be!\n");
692 *landp = *planep = 0;
694 p = getstarg(player->argp[offset], "Use fort support? ", buf);
698 if ((*p == 'y') || (*p == 'Y'))
701 p = getstarg(player->argp[offset + 1], "Use ship support? ", buf);
705 if ((*p == 'y') || (*p == 'Y'))
708 p = getstarg(player->argp[offset + 2], "Use land support? ", buf);
712 if ((*p == 'y') || (*p == 'Y'))
715 p = getstarg(player->argp[offset + 3], "Use plane support? ", buf);
719 if ((*p == 'y') || (*p == 'Y'))
726 * Attack, assault, and board ask the attacker what they'd like to attack
727 * with. This includes mil and land units from each "off" object. Note that
728 * after each sub-prompt, we check to make sure that the attack is still
729 * valid, and if it's not, then we abort the attack.
733 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
734 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
737 char land_answer[256];
740 if (att_abort(combat_mode, off, def))
742 memset(land_answer, 0, sizeof(land_answer));
743 for (n = 0; n <= off->last; ++n) {
744 off[n].troops = ask_off(combat_mode, off + n, def);
745 if (att_abort(combat_mode, off, def))
747 ask_olist(combat_mode, off + n, def, olist, land_answer,
748 a_spyp, a_engineerp);
749 if (att_abort(combat_mode, off, def))
756 * Return path cost for ATTACKER to enter sector given by DEF.
757 * MOBTYPE is a mobility type accepted by sector_mcost().
760 att_mobcost(natid attacker, struct combat *def, int mobtype)
765 if (CANT_HAPPEN(def->type != EF_SECTOR))
767 ok = getsect(def->x, def->y, §);
768 if (CANT_HAPPEN(!ok))
772 * We want the cost to move/march into the sector. If we just
773 * called sector_mcost(), we'd get the defender's cost. The
774 * attacker's cost is higher unless he's the old-owner. Note: if
775 * there are no civilians, a victorious attacker will become the
776 * old-owner. But he isn't now.
778 sect.sct_own = attacker;
780 return sector_mcost(§, mobtype);
783 /* How many mil is off allowed to attack with when it attacks def? */
786 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
791 switch (combat_mode) {
793 mobcost = att_mobcost(off->own, def, MOB_MOVE);
794 if (mobcost < 0 || off->mob <= 0)
796 mob_support = off->mob / mobcost;
797 if (mob_support < off->troops)
798 pr("Sector %s has %d mobility which can only support %d mil,\n",
799 xyas(off->x, off->y, player->cnum), off->mob, mob_support);
801 mob_support = off->troops;
804 if (def->own != player->cnum && def->mil) {
805 if (off->shp_mcp->m_flags & M_SEMILAND)
806 return off->troops / 4;
807 else if (!(off->shp_mcp->m_flags & M_LAND))
808 return off->troops / 10;
812 if (off->type == EF_SECTOR && off->mob <= 0)
814 mob_support = def->shp_mcp->m_item[I_MILIT];
815 if (mob_support < off->troops)
816 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
818 mob_support = off->troops;
823 if (def->lnd_lcp->l_flags & L_SPY)
825 mob_support = def->lnd_lcp->l_item[I_MILIT];
826 if (mob_support < off->troops)
827 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
829 mob_support = off->troops;
836 * If the attacker decides to go through with the attack, then the
837 * sectors/ships they are attacking with may be charged some mobility.
838 * This is where that amount of mobility is calculated. It is actually
839 * subtracted "for real" from the object's mobility in put_combat().
843 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
850 switch (combat_mode) {
852 off->mobcost += MAX(1,
854 * att_mobcost(off->own, def, MOB_MOVE)));
857 off->mobcost += MAX(1, attacking_mil / 5);
862 off->mobcost += MAX(1, attacking_mil / 5);
865 /* the 2 in the formula below is a fudge factor */
866 getship(def->shp_uid, &ship);
867 off->mobcost += (def->eff / 100) * (shp_speed(&ship) / 2);
872 /* How many mil to we want to attack from off against def? */
875 ask_off(int combat_mode, struct combat *off, struct combat *def)
881 if (att_get_combat(off, 0) <= 0)
883 if ((off->type == EF_SECTOR) && (off->own != player->cnum))
885 if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
887 if (off->type == EF_SECTOR) {
888 if (off->own != player->cnum)
890 sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
891 off->sct_dcp->d_name,
892 xyas(off->x, off->y, player->cnum), mob_support);
894 sprintf(prompt, "Number of mil from %s (max %d) : ",
895 prcom(0, off), mob_support);
897 if ((attacking_mil = onearg(NULL, prompt)) < 0)
899 if (att_abort(combat_mode, off, def))
901 if (att_get_combat(off, 0) <= 0)
904 MIN(attacking_mil, MIN(mob_support, off->troops))) <= 0)
907 calc_mobcost(combat_mode, off, def, attacking_mil);
908 return attacking_mil;
912 * Which units would you like to attack with or move in with [ynYNq?]
916 att_prompt(char *prompt, char army)
924 p = getstring(prompt, buf);
925 if (!p || *p == 'q') {
931 if (tolower(*p) == 'y' || tolower(*p) == 'n')
933 pr("y - yes this unit\n"
935 "Y - yes to all units in army '%c'\n"
936 "N - no to all units in army '%c'\n"
937 "q - quit\n? - this help message\n\n",
942 /* Ask the attacker which units they want to attack/assault/board with */
945 ask_olist(int combat_mode, struct combat *off, struct combat *def,
946 struct emp_qelem *olist, char *land_answer, int *a_spyp,
951 double pathcost, mobcost;
961 if (def->type == EF_LAND)
963 if (def->type == EF_SHIP)
964 maxland = def->shp_mcp->m_nland;
966 snxtitem_xy(&ni, EF_LAND, off->x, off->y);
967 while (nxtitem(&ni, &land)) {
968 if (land.lnd_own != player->cnum)
970 if (land.lnd_effic < LAND_MINEFF)
972 if (land_answer[(int)land.lnd_army] == 'N')
974 if (!lnd_can_attack(&land))
976 lcp = &lchr[(int)land.lnd_type];
978 if (def->type == EF_SHIP && !maxland) {
979 pr("Land units are not able to board this kind of ship\n");
982 if (def->type == EF_SHIP
983 && (def->shp_mcp->m_flags & (M_SUPPLY | M_SUB)) != M_SUPPLY
984 && !(lcp->l_flags & L_LIGHT)) {
985 pr("Only light land units can board this kind of ship\n");
988 if (land.lnd_mobil <= 0) {
989 pr("%s is out of mobility, and cannot %s\n",
990 prland(&land), att_mode[combat_mode]);
995 if (ontradingblock(EF_LAND, &land)) {
996 pr("%s is on the trading block, and cannot %s\n",
997 prland(&land), att_mode[combat_mode]);
1002 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
1003 pr("%s is on ship #%d, and cannot %s\n",
1004 prland(&land), land.lnd_ship, att_mode[combat_mode]);
1006 } else if (off->type == EF_SHIP) {
1007 if (land.lnd_ship != off->shp_uid)
1009 } else if (land.lnd_land >= 0) {
1010 pr("%s is on unit #%d, and cannot %s\n",
1011 prland(&land), land.lnd_land, att_mode[combat_mode]);
1014 switch (combat_mode) {
1017 * We used to let land units attack only if they have the
1018 * mobility consumed by the attack, not counting combat
1019 * and moving in to occupy. Making sure your land units
1020 * reach attack positions with enough mobility left is a
1021 * pain in the neck. We now require positive mobility,
1022 * just like for marching. Except we don't allow rushing
1023 * of high-mobility sectors (mountains): for those we
1024 * still require attack mobility.
1026 pathcost = att_mobcost(land.lnd_own, def, lnd_mobtype(&land));
1027 mobcost = lnd_pathcost(&land, pathcost);
1028 if (pathcost < 1.0) {
1029 if (land.lnd_mobil <= 0) {
1030 pr("%s is out of mobility\n", prland(&land));
1034 reqmob = MIN(land_mob_max, (int)ceil(mobcost));
1035 if (land.lnd_mobil < reqmob) {
1036 pr("%s does not have enough mobility (%d needed)\n",
1037 prland(&land), reqmob);
1045 if (!(lcp->l_flags & L_ASSAULT))
1052 att_val = attack_val(combat_mode, &land);
1053 if (att_val < 1.0) {
1054 pr("%s has no offensive strength\n", prland(&land));
1057 if (!lnd_supply_all(&land)) {
1058 pr("%s is out of supply, and cannot %s\n",
1059 prland(&land), att_mode[combat_mode]);
1062 if (def->type == EF_SHIP && first_time) {
1064 pr("You may board with a maximum of %d land units\n", maxland);
1066 pr("%s has a base %s value of %.0f\n",
1067 prland(&land), att_mode[combat_mode], att_val);
1068 if (land_answer[(int)land.lnd_army] != 'Y') {
1070 "%s with %s %s (%c %d%%) [ynYNq?] ",
1071 att_mode[combat_mode],
1074 land.lnd_army ? land.lnd_army : '~',
1076 land_answer[(int)land.lnd_army] =
1077 att_prompt(prompt, land.lnd_army);
1078 if (att_abort(combat_mode, off, def))
1080 if (land_answer[(int)land.lnd_army] != 'y' &&
1081 land_answer[(int)land.lnd_army] != 'Y')
1084 if (!(llp = malloc(sizeof(struct ulist)))) {
1085 logerror("Malloc failed in attack!\n");
1089 memset(llp, 0, sizeof(struct ulist));
1090 emp_insque(&llp->queue, olist);
1091 llp->mobil = mobcost;
1092 llp->unit.land = land;
1093 llp->x = llp->unit.land.lnd_x;
1094 llp->y = llp->unit.land.lnd_y;
1095 llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
1096 llp->eff = llp->unit.land.lnd_effic;
1097 if (lnd_spyval(&land) > *a_spyp)
1098 *a_spyp = lnd_spyval(&land);
1099 if (((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)
1101 if (def->type == EF_SHIP && ++count >= maxland)
1106 /* What's the offense or defense multiplier? */
1109 att_combat_eff(struct combat *com)
1115 if (com->type == EF_SECTOR) {
1116 eff = com->eff / 100.0;
1117 if (com->own == player->cnum) {
1118 str = com->sct_dcp->d_ostr;
1119 eff = 1.0 + (str - 1.0) * eff;
1121 eff = sector_strength(getsectp(com->x, com->y));
1122 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1123 getship(com->shp_uid, &ship);
1124 eff = 1.0 + shp_armor(&ship) / 100.0;
1130 att_get_offense(int combat_mode, struct combat *off,
1131 struct emp_qelem *olist, struct combat *def)
1136 * Get the attacker units & mil again in case they changed while the
1137 * attacker was answering sub-prompts.
1140 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1141 if (att_empty_attack(combat_mode, ototal, def))
1142 return abort_attack();
1143 if (combat_mode == A_PARA)
1145 pr("\n Initial attack strength: %8d\n", ototal);
1149 /* Get the defensive units and reacting units */
1151 att_get_defense(struct emp_qelem *olist, struct combat *def,
1152 struct emp_qelem *dlist, int a_spy, int ototal)
1155 struct emp_qelem *qp;
1161 get_dlist(def, dlist, 0, &d_spy);
1162 dtotal = get_dtotal(def, dlist, 1.0, 0);
1165 * Call in reacting units
1168 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1169 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1171 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1172 llp = (struct ulist *)qp;
1173 intelligence_report(def->own, &llp->unit.land, d_spy,
1174 "Scouts report attacking unit:");
1177 old_dtotal = dtotal;
1178 dtotal = get_dtotal(def, dlist, 1.0, 0);
1179 if (dtotal != old_dtotal)
1180 pr("Defense strength with reacting units: %8d\n", dtotal);
1185 /* Get the defensive land units in the sector or on the ship */
1188 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1191 struct nstr_item ni;
1195 /* In here is where you need to take out spies and trains from the defending
1196 lists. Spies try to hide, trains get trapped and can be boarded. */
1198 snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1199 while (nxtitem(&ni, &land)) {
1202 if (land.lnd_own != def->own)
1204 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1206 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1208 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1210 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1212 intelligence_report(player->cnum, &land, a_spy,
1213 "Scouts report defending unit:");
1214 if (!(llp = malloc(sizeof(struct ulist)))) {
1215 logerror("Malloc failed in attack!\n");
1219 memset(llp, 0, sizeof(struct ulist));
1220 emp_insque(&llp->queue, list);
1221 llp->supplied = lnd_supply_all(&land);
1222 llp->unit.land = land;
1223 llp->x = llp->unit.land.lnd_x;
1224 llp->y = llp->unit.land.lnd_y;
1225 llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
1226 llp->eff = llp->unit.land.lnd_effic;
1227 if (lnd_spyval(&land) > *d_spyp)
1228 *d_spyp = lnd_spyval(&land);
1232 /* Calculate the total offensive strength */
1235 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1236 double osupport, int check)
1238 double ototal = 0.0;
1239 struct emp_qelem *qp, *next;
1244 * first, total the attacking mil
1247 for (n = 0; n <= off->last; ++n) {
1248 if (off[n].type == EF_BAD || (check &&
1249 att_get_combat(&off[n], 0) <= 0))
1251 ototal += off[n].troops * att_combat_eff(off + n);
1255 * next, add in the attack_values of all
1256 * the attacking units
1259 for (qp = olist->q_forw; qp != olist; qp = next) {
1261 llp = (struct ulist *)qp;
1262 if (check && !get_oland(combat_mode, llp))
1264 if (combat_mode == A_ATTACK) {
1266 for (n = 0; n <= off->last; ++n) {
1267 if (off[n].type == EF_BAD)
1269 if ((off[n].x == llp->unit.land.lnd_x) &&
1270 (off[n].y == llp->unit.land.lnd_y))
1274 lnd_print(player->cnum, llp,
1275 "can't attack from this sector now");
1279 ototal += attack_val(combat_mode, &llp->unit.land) *
1280 att_combat_eff(off + w);
1282 ototal += attack_val(combat_mode, &llp->unit.land);
1287 return ldround(ototal, 1);
1290 /* Calculate the total defensive strength */
1293 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1296 double dtotal = 0.0, eff = 1.0, d_unit;
1297 struct emp_qelem *qp, *next;
1300 if (check && att_get_combat(def, 1) < 0)
1302 eff = att_combat_eff(def);
1303 dtotal = def->troops * eff;
1306 * next, add in the defense_values of all
1307 * the defending non-retreating units
1310 for (qp = list->q_forw; qp != list; qp = next) {
1312 llp = (struct ulist *)qp;
1313 if (check && !get_dland(def, llp))
1315 d_unit = defense_val(&llp->unit.land);
1318 dtotal += d_unit * eff;
1323 return ldround(dtotal, 1);
1327 * This is the land unit integrity check.
1331 get_oland(int combat_mode, struct ulist *llp)
1333 struct lndstr *lp = &llp->unit.land;
1336 getland(llp->unit.land.lnd_uid, lp);
1338 if (lp->lnd_own != player->cnum) {
1339 sprintf(buf, "was destroyed and is no longer a part of the %s",
1340 att_mode[combat_mode]);
1341 lnd_print(player->cnum, llp, buf);
1345 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1347 "left to fight another battle and is no longer a part of the %s",
1348 att_mode[combat_mode]);
1349 lnd_print(player->cnum, llp, buf);
1353 if (lp->lnd_effic < llp->eff) {
1354 sprintf(buf, "damaged from %d%% to %d%%",
1355 llp->eff, lp->lnd_effic);
1356 lnd_print(player->cnum, llp, buf);
1359 llp->eff = llp->unit.land.lnd_effic;
1364 get_dland(struct combat *def, struct ulist *llp)
1366 struct lndstr *lp = &llp->unit.land;
1369 getland(llp->unit.land.lnd_uid, lp);
1371 if (lp->lnd_effic < LAND_MINEFF) {
1372 sprintf(buf, "was destroyed and is no longer a part of the defense");
1373 lnd_print(llp->unit.land.lnd_own, llp, buf);
1377 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1378 lnd_print(llp->unit.land.lnd_own, llp,
1379 "left to go fight another battle and is no longer a part of the defense");
1384 llp->eff = llp->unit.land.lnd_effic;
1389 kill_land(struct emp_qelem *list)
1391 struct emp_qelem *qp, *next;
1394 for (qp = list->q_forw; qp != list; qp = next) {
1396 llp = (struct ulist *)qp;
1397 if (llp->unit.land.lnd_ship >= 0) {
1398 llp->unit.land.lnd_effic = 0;
1399 lnd_print(player->cnum, llp,
1400 "cannot return to the ship, and dies!");
1407 att_infect_units(struct emp_qelem *list, int plague)
1409 struct emp_qelem *qp, *next;
1414 for (qp = list->q_forw; qp != list; qp = next) {
1416 llp = (struct ulist *)qp;
1417 if (llp->unit.land.lnd_pstage == PLG_HEALTHY)
1418 llp->unit.land.lnd_pstage = PLG_EXPOSED;
1423 * Put the land unit on the disk. If there was some mobility cost, then
1424 * subtract it from the units mobility. Note that this works the same way
1425 * as sectors & ships in that no mobility is actually taken until the attacker
1426 * has committed to attacking.
1430 put_oland(struct emp_qelem *list)
1432 struct emp_qelem *qp, *next;
1435 for (qp = list->q_forw; qp != list; qp = next) {
1437 llp = (struct ulist *)qp;
1438 llp->unit.land.lnd_mission = 0;
1439 llp->unit.land.lnd_harden = 0;
1440 llp->unit.land.lnd_mobil -= (int)llp->mobil;
1442 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1443 if (llp->unit.land.lnd_own != player->cnum) {
1444 emp_remque((struct emp_qelem *)llp);
1447 get_oland(A_ATTACK, llp);
1452 * Keep sending in reinforcements until it looks like we're going to win.
1453 * Note that the "strength" command also calls this routine.
1457 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1458 int *d_spyp, int ototal)
1460 struct nstr_item ni;
1464 double new_land = 0;
1468 double eff = att_combat_eff(def);
1471 dtotal = get_dtotal(def, list, 1.0, 1);
1474 snxtitem_all(&ni, EF_LAND);
1475 while (nxtitem(&ni, &land) && dtotal + new_land * eff < 1.2 * ototal) {
1478 if (land.lnd_mission != MI_RESERVE)
1480 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1482 if (land.lnd_own != def->own)
1484 if (land.lnd_ship >= 0)
1486 if (land.lnd_land >= 0)
1488 if (defense_val(&land) < 1.0)
1490 if (!lnd_can_attack(&land))
1493 /* Only supplied units can react */
1494 if (list ? !lnd_supply_all(&land) : !lnd_could_be_supplied(&land))
1497 if (!in_oparea((struct empobj *)&land, def->x, def->y))
1500 pathcost = path_find(land.lnd_x, land.lnd_y, def->x, def->y,
1501 def->own, lnd_mobtype(&land));
1504 mobcost = lnd_pathcost(&land, pathcost);
1505 if (land.lnd_mobil < mobcost)
1508 new_land += defense_val(&land);
1510 if (!list) /* we are in the "strength" command */
1513 /* move to defending sector */
1514 land.lnd_mobil -= ldround(mobcost, 1);
1517 land.lnd_x = def->x;
1518 land.lnd_y = def->y;
1519 putland(land.lnd_uid, &land);
1520 wu(0, land.lnd_own, "%s reacts to %s.\n",
1521 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1523 llp = malloc(sizeof(struct ulist));
1525 memset(llp, 0, sizeof(struct ulist));
1529 llp->chrp = (struct empobj_chr *)&lchr[(int)land.lnd_type];
1530 llp->unit.land = land;
1531 llp->eff = land.lnd_effic;
1532 emp_insque(&llp->queue, list);
1533 if (lnd_spyval(&land) > *d_spyp)
1534 *d_spyp = lnd_spyval(&land);
1536 intelligence_report(player->cnum, &land, a_spy,
1537 "Scouts sight reacting enemy unit:");
1542 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1545 get_osupport(char *outs, struct combat *def, int fort_sup, int ship_sup,
1546 int land_sup, int plane_sup)
1548 double osupport = 1.0;
1550 double af, as, au, ap;
1552 af = as = au = ap = 0.0;
1554 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1559 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1566 dam = lnd_support(def->own, player->cnum, def->x, def->y, 0);
1572 dam = off_support(def->x, def->y, def->own, player->cnum);
1576 sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1581 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1584 get_dsupport(char *outs, struct emp_qelem *list, struct combat *def,
1585 int ototal, int dtotal)
1587 double dsupport = 1.0;
1589 double df, ds, du, dp;
1592 df = ds = du = dp = 0.0;
1593 if (dtotal < 0.1 * ototal) {
1595 } else if (dtotal >= 1.2 * ototal) {
1598 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1602 dtotal = get_dtotal(def, list, dsupport, 0);
1603 if (dtotal < 1.2 * ototal) {
1604 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1607 dtotal = get_dtotal(def, list, dsupport, 0);
1609 if (dtotal < 1.2 * ototal) {
1610 dam = lnd_support(player->cnum, def->own, def->x, def->y, 1);
1613 dtotal = get_dtotal(def, list, dsupport, 1);
1615 if (dtotal < 1.2 * ototal) {
1616 dam = def_support(def->x, def->y, player->cnum, def->own);
1624 sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1629 "\nOdds are bad for us...support cancelled.\n\n");
1632 "\nOdds are good for us...support cancelled.\n\n");
1638 * Land mines add to the defense multiplier. If the attacker has engineers
1639 * then this multiplier is cut in half.
1643 get_mine_dsupport(struct combat *def, int a_engineer)
1648 getsect(def->x, def->y, §);
1650 if (sect.sct_oldown != player->cnum) {
1651 mines = SCT_LANDMINES(§);
1652 mines = MIN(mines, 20);
1654 mines = ldround(mines / 2.0, 1);
1657 wu(0, def->own, "Defending mines add %1.2f\n",
1659 pr("Defending mines add %1.2f\n", mines * 0.02);
1660 return mines * 0.02;
1666 /* Get the offensive and defensive support */
1668 att_get_support(int combat_mode, int ofort, int oship, int oland,
1669 int oplane, struct emp_qelem *olist, struct combat *off,
1670 struct emp_qelem *dlist, struct combat *def,
1671 double *osupportp, double *dsupportp, int a_engineer)
1674 char osupports[512];
1675 char dsupports[512];
1677 if (combat_mode == A_PARA)
1680 *osupportp = get_osupport(osupports, def,
1681 ofort, oship, oland, oplane);
1684 * I need to put a 1 at the end of the next four total_stren calls
1685 * because units & mil may have been damaged by collateral damage or
1686 * nuclear warheads from the offensive & defensive support.
1689 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1690 if (att_empty_attack(combat_mode, ototal, def))
1691 return abort_attack();
1692 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1695 * Calculate defensive support. If odds are too good or too bad
1696 * then don't call in support.
1699 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1700 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1701 if (att_empty_attack(combat_mode, ototal, def))
1702 return abort_attack();
1704 if ((*osupports || *dsupports) &&
1705 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1706 pr("\n\t\tsupport values\n");
1707 pr("\t\tforts\tships\tunits\tplanes\n");
1708 if (*osupportp != 1.0)
1709 pr("%s", osupports);
1710 if (*dsupportp != 1.0)
1711 pr("%s", dsupports);
1713 wu(0, def->own, "\n\t\tsupport values\n");
1714 wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1715 if (*osupportp != 1.0)
1716 wu(0, def->own, "%s", osupports);
1717 if (*dsupportp != 1.0)
1718 wu(0, def->own, "%s", dsupports);
1722 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1723 if (dtotal && def->type == EF_SECTOR)
1724 *dsupportp += get_mine_dsupport(def, a_engineer);
1728 /* How many two-legged bipeds are in this combat force? */
1731 count_bodies(struct combat *off, struct emp_qelem *list)
1735 struct emp_qelem *qp;
1738 for (n = 0; n <= off->last; ++n)
1739 bodies += off[n].troops;
1740 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1741 llp = (struct ulist *)qp;
1742 bodies += llp->unit.land.lnd_item[I_MILIT];
1747 /* This is where the fighting actually occurs. */
1750 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1751 double osupport, struct combat *def, struct emp_qelem *dlist,
1755 int a_cas = 0; /* Casualty counts */
1757 int ototal; /* total attacking strength */
1758 int dtotal; /* total defending strength */
1759 int a_bodies; /* total attacking mil (incl. mil in units) */
1760 int d_bodies; /* total defending mil (incl. mil in units) */
1770 ototal = get_ototal(combat_mode, off, olist, osupport,
1771 combat_mode != A_PARA);
1772 dtotal = get_dtotal(def, dlist, dsupport, 0);
1776 a_bodies = count_bodies(off, olist);
1777 d_bodies = count_bodies(def, dlist);
1778 d_mil = def->troops;
1779 for (n = 0; n <= off->last; ++n)
1780 if (off[n].type == EF_BAD)
1783 a_troops[n] = off[n].troops;
1785 /* This switch is required to get the spacing right */
1786 switch (combat_mode) {
1788 pr(" Final attack strength: %8d\n", ototal);
1791 pr(" Final assault strength: %8d\n", ototal);
1794 if (def->sct_type == SCT_MOUNT ||
1795 def->sct_type == SCT_WATER ||
1796 def->sct_type == SCT_CAPIT ||
1797 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1798 pr("You can't air-assault a %s sector!\n",
1799 def->sct_dcp->d_name);
1802 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1804 pr(" Final air-assault strength: %8d\n", ototal);
1808 pr(" Final board strength: %8d\n", ototal);
1812 pr(" Final defense strength: %8d\n", dtotal);
1813 odds = att_calcodds(ototal, dtotal);
1814 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1816 /* spread the plague */
1817 if (combat_mode != A_PARA) {
1819 for (n = 0; n <= off->last; ++n)
1820 if (off[n].type != EF_BAD)
1821 def->plague |= off[n].plague;
1822 for (n = 0; n <= off->last; ++n)
1823 if (off[n].type != EF_BAD)
1824 off[n].plague |= def->plague;
1826 att_infect_units(olist, off->plague);
1827 att_infect_units(dlist, def->plague);
1830 * Fighting is slightly random. There is always that last little
1831 * effort you see people put in. Or the stray bullet that takes out
1832 * an officer and the rest go into chaos. Things like that.
1833 * Thus, we have added a very slight random factor that will sometimes
1834 * allow the little guy to win. We modify the odds a little
1835 * (either +- 5%) to account for this randomness. We also only
1836 * recalculate the odds every 8-50 casualties, not every cacsualty,
1837 * since a single dead guy normally wouldn't cause a commander to
1838 * rethink his strategies, but 50 dead guys might.
1840 odds += (roll(11) - 6) / 100.0;
1845 recalctime = 7 + roll(43);
1846 while (!success && ototal) {
1849 d_cas += take_casualty(A_DEFEND, def, dlist);
1850 dtotal = get_dtotal(def, dlist, dsupport, 0);
1855 a_cas += take_casualty(combat_mode, off, olist);
1856 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1858 if (((a_cas + d_cas) % 70) == 69)
1860 if (recalctime-- <= 0) {
1861 recalctime = 7 + roll(43);
1862 odds = att_calcodds(ototal, dtotal);
1863 odds += (roll(11) - 6) / 100.0;
1871 /* update defense mobility & mil */
1875 if (def->type == EF_SECTOR && d_mil && d_cas) {
1879 newmob = damage(def->mob, 100 * d_cas / d_mil);
1880 def->mobcost = MIN(20, def->mob - newmob);
1883 def->mil = def->troops;
1886 /* update attack mobility & mil */
1887 for (n = 0; n <= off->last; ++n)
1888 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1889 if (off[n].type == EF_SECTOR && off[n].mil) {
1890 if (!CANT_HAPPEN(off[n].mob < 0)) {
1891 newmob = damage(off[n].mob,
1892 100 * (a_troops[n] - off[n].troops)
1894 off[n].mobcost += MIN(20, off[n].mob - newmob);
1897 off[n].mil -= a_troops[n] - off[n].troops;
1900 /* update land unit mobility */
1901 if (d_bodies && d_cas)
1902 lnd_takemob(dlist, (double)d_cas / d_bodies);
1903 if (a_bodies && a_cas)
1904 lnd_takemob(olist, (double)a_cas / a_bodies);
1906 /* damage attacked sector */
1907 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1909 pr("- Casualties -\n Yours: %d\n", a_cas);
1910 pr(" Theirs: %d\n", d_cas);
1911 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
1912 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
1915 switch (combat_mode) {
1917 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
1918 pr("We have captured %s, sir!\n", prcom(0, def));
1922 news_item = def->own ? N_AWON_SECT : N_START_COL;
1923 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
1924 action = "assaulting and taking";
1927 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
1928 pr("We have captured %s, sir!\n", prcom(0, def));
1929 action = "air-assaulting and taking";
1932 news_item = N_BOARD_SHIP;
1933 pr("We have boarded %s, sir!\n", prcom(0, def));
1934 action = "boarding";
1937 news_item = N_BOARD_LAND;
1938 pr("We have boarded %s, sir!\n", prcom(0, def));
1939 action = "boarding";
1944 action = "defeating";
1947 switch (combat_mode) {
1949 news_item = N_SCT_LOSE;
1950 pr("You have been defeated!\n");
1951 action = "attacking";
1954 news_item = N_ALOSE_SCT;
1955 pr("You have been defeated!\n");
1957 action = "trying to assault";
1960 news_item = N_PLOSE_SCT;
1961 pr("All of your troops were destroyed\n");
1962 action = "trying to air-assault";
1965 news_item = N_SHP_LOSE;
1966 pr("You have been repelled\n");
1968 action = "trying to board";
1971 news_item = N_LND_LOSE;
1972 pr("You have been repelled\n");
1974 action = "trying to board";
1979 action = "fighting";
1982 nreport(player->cnum, news_item, def->own, 1);
1985 "%s lost %d troops %s %s\nWe lost %d troops defending\n",
1986 prnatid(player->cnum), a_cas,
1987 action, pr_com(0, def, def->own), d_cas);
1990 send_reacting_units_home(dlist);
1992 /* putland the defending land */
1995 /* putland the attacking land */
1998 /* put the victim sector/ship/land */
1999 if (!success || !take_def(combat_mode, olist, off, def))
2002 /* put the attacking sectors/ship */
2003 for (n = 0; n <= off->last; ++n)
2004 if (off[n].type != EF_BAD)
2005 put_combat(&off[n]);
2010 switch (combat_mode) {
2012 ask_move_in(off, olist, def);
2014 /* put sectors again to get abandon warnings */
2015 for (n = 0; n <= off->last; ++n)
2016 if (off[n].type != EF_BAD)
2017 put_combat(&off[n]);
2020 att_move_in_off(combat_mode, off, olist, def);
2023 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2027 /* What percentage of the combat forces going head-to-head are we? */
2030 att_calcodds(int ototal, int dtotal)
2034 /* calculate odds */
2037 else if (dtotal <= 0)
2040 odds = (double)ototal / (dtotal + ototal);
2045 /* Here's where the dead soldiers get dragged off the battlefield */
2048 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2050 int to_take = CASUALTY_LUMP;
2051 int biggest_troops = 0, index = -1;
2052 int n, tot_troops = 0, biggest_mil, cas;
2053 struct emp_qelem *qp, *biggest;
2056 for (n = 0; n <= off->last; ++n) {
2057 if (off[n].type != EF_BAD) {
2058 tot_troops += off[n].troops;
2059 if (off[n].troops > biggest_troops) {
2060 biggest_troops = off[n].troops;
2067 to_take -= tot_troops;
2070 for (n = 0; n <= off->last; ++n)
2071 if (off[n].type != EF_BAD)
2075 * They can all come off mil. We rotate the casualties,
2076 * starting with the sector containing the most mil.
2078 to_take = CASUALTY_LUMP;
2080 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2083 while (to_take > 0) {
2084 for (n = index; n <= off->last && to_take; ++n) {
2085 if (off[n].type != EF_BAD && off[n].troops > 0) {
2090 for (n = 0; n < index && to_take; ++n) {
2091 if (off[n].type != EF_BAD && off[n].troops > 0) {
2097 return CASUALTY_LUMP;
2101 return CASUALTY_LUMP - to_take;
2104 * Need to take some casualties from attacking units
2105 * Procedure: find the biggest unit remaining (in
2106 * terms of mil) and give it the casualties.
2110 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2111 llp = (struct ulist *)qp;
2113 if (llp->unit.land.lnd_item[I_MILIT] > biggest_mil) {
2114 biggest_mil = llp->unit.land.lnd_item[I_MILIT];
2118 if (biggest == NULL)
2119 return CASUALTY_LUMP - to_take;
2121 llp = (struct ulist *)biggest;
2122 cas = lnd_take_casualty(combat_mode, llp, to_take);
2123 return CASUALTY_LUMP - (to_take - cas);
2126 /* Send reacting defense units back to where they came from (at no mob cost) */
2129 send_reacting_units_home(struct emp_qelem *list)
2131 struct emp_qelem *qp, *next;
2135 for (qp = list->q_forw; qp != list; qp = next) {
2137 llp = (struct ulist *)qp;
2138 if ((llp->unit.land.lnd_x != llp->x) ||
2139 (llp->unit.land.lnd_y != llp->y)) {
2140 sprintf(buf, "returns to %s",
2141 xyas(llp->x, llp->y, llp->unit.land.lnd_own));
2142 llp->unit.land.lnd_x = llp->x;
2143 llp->unit.land.lnd_y = llp->y;
2144 lnd_print(llp->unit.land.lnd_own, llp, buf);
2150 /* Check for 0 offense strength. This call will always preceed an abort */
2153 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2156 if (def->own && player->cnum != def->own) {
2158 "%s considered %sing you @%s\n",
2159 prnatid(player->cnum),
2160 att_mode[combat_mode], xyas(def->x, def->y, def->own));
2162 pr("No troops for %s...\n", att_mode[combat_mode]);
2169 * Take the defending sector or ship from the defender and give it to the
2174 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2179 struct ulist *llp, *delete_me = NULL;
2185 for (n = 0; n <= off->last && !occuppied; ++n) {
2186 if (off[n].type != EF_BAD &&
2187 off[n].troops > 0 &&
2188 (off[n].type != EF_SECTOR || off[n].mob)) {
2190 if (def->type == EF_LAND) {
2191 if (def->lnd_lcp->l_flags & L_SPY) {
2198 pr("1 mil from %s moves %s\n",
2199 prcom(0, off + n), prcom(2, def));
2204 pr("%s left unoccupied\n", prcom(0, def));
2207 "No enemy troops moved %s so you still own it!\n",
2208 pr_com(2, def, def->own));
2211 llp = (struct ulist *)list->q_forw;
2212 llp->unit.land.lnd_x = def->x;
2213 llp->unit.land.lnd_y = def->y;
2214 take_move_in_mob(combat_mode, llp, off, def);
2215 if (def->type == EF_SHIP) {
2216 llp->unit.land.lnd_ship = def->shp_uid;
2217 sprintf(buf, "boards %s", prcom(0, def));
2218 lnd_print(player->cnum, llp, buf);
2221 llp->unit.land.lnd_ship = -1;
2222 sprintf(buf, "moves in to occupy %s",
2223 xyas(def->x, def->y, player->cnum));
2224 lnd_print(player->cnum, llp, buf);
2230 if (def->type == EF_SECTOR) {
2231 getsect(def->x, def->y, §);
2232 takeover(§, player->cnum);
2233 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2234 caploss(§, def->own,
2235 "* We have captured %s's capital, sir! *\n");
2237 } else if (def->type == EF_SHIP) {
2238 getship(def->shp_uid, &ship);
2239 takeover_ship(&ship, player->cnum);
2240 putship(ship.shp_uid, &ship);
2241 } else if (def->type == EF_LAND) {
2242 getland(def->lnd_uid, &land);
2243 takeover_land(&land, player->cnum);
2244 putland(land.lnd_uid, &land);
2247 lnd_delete(delete_me);
2248 att_get_combat(def, 0);
2253 * Ask the attacker which mil & land units they'd like to move into the
2258 ask_move_in(struct combat *off, struct emp_qelem *olist,
2262 struct emp_qelem *qp, *next;
2266 char land_answer[256];
2269 for (n = 0; n <= off->last; ++n)
2270 if (off[n].type != EF_BAD && off[n].troops > 0)
2272 ask_move_in_off(&off[n], def);
2273 if (player->aborted)
2279 memset(land_answer, 0, sizeof(land_answer));
2280 for (qp = olist->q_forw; qp != olist; qp = next) {
2282 llp = (struct ulist *)qp;
2283 answerp = &land_answer[(int)llp->unit.land.lnd_army];
2284 if (player->aborted || att_get_combat(def, 0) < 0)
2286 if (*answerp == 'Y')
2288 if (!get_oland(A_ATTACK, llp))
2290 if (*answerp != 'N') {
2291 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2292 prland(&llp->unit.land),
2293 llp->unit.land.lnd_army ? llp->unit.land.lnd_army : '~',
2294 llp->unit.land.lnd_effic);
2295 *answerp = att_prompt(prompt, llp->unit.land.lnd_army);
2296 if (player->aborted || att_get_combat(def, 0) < 0)
2298 if (!get_oland(A_ATTACK, llp))
2301 if (*answerp == 'y' || *answerp == 'Y')
2303 sprintf(buf, "stays in %s",
2304 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2306 lnd_print(player->cnum, llp, buf);
2311 if (att_get_combat(def, 0) < 0) {
2312 for (qp = olist->q_forw; qp != olist; qp = next) {
2314 llp = (struct ulist *)qp;
2315 if (!get_oland(A_ATTACK, llp))
2317 sprintf(buf, "stays in %s",
2318 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2320 lnd_print(player->cnum, llp, buf);
2325 if (opt_INTERDICT_ATT)
2326 lnd_interdict(olist, def->x, def->y, player->cnum);
2327 move_in_land(A_ATTACK, off, olist, def);
2330 /* Move offensive land units to the conquered sector or ship */
2333 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2336 struct emp_qelem *qp, *next;
2342 for (qp = olist->q_forw; qp != olist; qp = next) {
2344 llp = (struct ulist *)qp;
2345 if (!get_oland(combat_mode, llp))
2347 take_move_in_mob(combat_mode, llp, off, def);
2348 llp->unit.land.lnd_x = def->x;
2349 llp->unit.land.lnd_y = def->y;
2350 if (def->type == EF_SHIP)
2351 llp->unit.land.lnd_ship = def->shp_uid;
2353 llp->unit.land.lnd_ship = -1;
2357 if (def->type == EF_SECTOR) {
2358 if (opt_INTERDICT_ATT) {
2359 lnd_sweep(olist, 0, 0, player->cnum);
2360 lnd_check_mines(olist);
2362 sprintf(buf, "now occupies %s", prcom(0, def));
2364 sprintf(buf, "boards %s", prcom(0, def));
2368 for (qp = olist->q_forw; qp != olist; qp = next) {
2370 llp = (struct ulist *)qp;
2371 lnd_print(player->cnum, llp, buf);
2379 * Move assaulting, paradropping, or boarding mil & units into def
2380 * If the mil are coming from a ship, then pack a lunch.
2384 att_move_in_off(int combat_mode, struct combat *off,
2385 struct emp_qelem *olist, struct combat *def)
2392 move_in_land(combat_mode, off, olist, def);
2394 for (n = 0; n <= off->last; ++n) {
2395 if (off[n].type == EF_BAD || !off[n].troops)
2397 troops = off[n].troops;
2399 off[n].mil -= troops;
2401 put_combat(off + n);
2402 if (combat_mode == A_ASSAULT) {
2403 if (CANT_HAPPEN(off[n].type != EF_SHIP))
2405 getship(off[n].shp_uid, &ship);
2406 lunchbox += (int)((troops + 1) * ship.shp_item[I_FOOD]
2407 / (ship.shp_item[I_MILIT] + troops
2408 + ship.shp_item[I_CIVIL] + 0.5));
2410 ship.shp_item[I_FOOD] -= lunchbox;
2411 putship(ship.shp_uid, &ship);
2417 if (combat_mode == A_ASSAULT) {
2418 if (CANT_HAPPEN(def->type != EF_SECTOR))
2420 getsect(def->x, def->y, §);
2421 if (lunchbox > ITEM_MAX - sect.sct_item[I_FOOD])
2422 lunchbox = ITEM_MAX - sect.sct_item[I_FOOD];
2423 sect.sct_item[I_FOOD] += lunchbox;
2429 /* Ask how many mil to move in from each sector */
2432 ask_move_in_off(struct combat *off, struct combat *def)
2435 int num_mil, dam = 0, left;
2441 if (att_get_combat(off, 0) <= 0)
2443 if (att_get_combat(def, 0) < 0)
2445 if (off->own != player->cnum)
2447 d = att_mobcost(off->own, def, MOB_MOVE);
2448 if ((mob_support = MIN(off->troops, (int)(off->mob / d))) <= 0)
2450 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2451 xyas(off->x, off->y, player->cnum), mob_support);
2452 p = getstring(prompt, buf);
2453 if (!p || !*p || (num_mil = atoi(p)) <= 0)
2455 /* Make sure we don't move in more than we can support mobility-wise */
2456 if (num_mil > mob_support)
2457 num_mil = mob_support;
2458 if (att_get_combat(off, 0) <= 0)
2460 if (att_get_combat(def, 0) < 0)
2462 if ((num_mil = MIN(off->troops, num_mil)) <= 0) {
2463 pr("No mil moved in from %s\n",
2464 xyas(off->x, off->y, player->cnum));
2467 mob_support = MAX(1, (int)(num_mil * d));
2468 off->mob -= MIN(off->mob, mob_support);
2469 off->mil -= num_mil;
2470 off->troops -= num_mil;
2473 weight = (double)num_mil * ichr[I_MILIT].i_lbs;
2474 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2475 if (chance(weight / 100.0))
2477 ground_interdict(def->x, def->y, player->cnum, "military");
2478 dam += check_lmines(def->x, def->y, weight);
2482 left = commdamage(num_mil, dam, I_MILIT);
2483 if (left < num_mil) {
2485 pr("%d of the mil you were moving were destroyed!\n"
2486 "Only %d mil made it to %s\n",
2487 num_mil - left, left,
2488 xyas(def->x, def->y, player->cnum));
2490 pr("All of the mil you were moving were destroyed!\n");
2493 /* maybe got nuked */
2494 if (att_get_combat(def, 0) < 0)
2502 /* Charge land units for moving into a sector or onto a ship */
2505 take_move_in_mob(int combat_mode, struct ulist *llp, struct combat *off,
2508 double mob = llp->unit.land.lnd_mobil;
2509 double gain = etu_per_update * land_mob_scale;
2510 double mobcost, moblim;
2512 switch (combat_mode) {
2514 mobcost = lnd_pathcost(&llp->unit.land,
2515 att_mobcost(llp->unit.land.lnd_own, def,
2516 lnd_mobtype(&llp->unit.land)));
2520 * Set mobcost to basic assault cost, moblim to maximum
2521 * mobility to keep when assaulting from non-landing ship
2523 if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE) {
2524 mobcost = gain / 2.0;
2530 if (!(off->shp_mcp->m_flags & M_LAND))
2531 /* Not a landing ship, ensure we go to or below moblim */
2532 mobcost = MAX(mobcost, mob - moblim);
2535 if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE)
2548 llp->unit.land.lnd_mobil = (signed char)mob;
2549 llp->unit.land.lnd_harden = 0;
2553 free_list(struct emp_qelem *list)
2555 struct emp_qelem *qp, *next;
2557 if (!list || QEMPTY(list))
2561 while (qp != list) {
2570 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2578 * sector_strength - Everyone starts at 1. You can get up to a max
2579 * of d_dstr, depending on how much you build up the
2580 * defenses of the sector.
2584 sector_strength(struct sctstr *sp)
2586 double def = SCT_DEFENSE(sp) / 100.0;
2587 double base = sp->sct_type == SCT_MOUNT ? 2.0 : 1.0;
2588 double d = base + (dchr[sp->sct_type].d_dstr - base) * def;
2590 return LIMIT_TO(d, base, dchr[sp->sct_type].d_dstr);