2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * attsub.c: Attack subroutines
29 * Known contributors to this file:
31 * Steve McClure, 1996-2000
32 * Markus Armbruster, 2006-2011
49 #include "prototypes.h"
54 #define CASUALTY_LUMP 1 /* How big casualty chunks should be */
56 static void ask_olist(int combat_mode, struct combat *off,
57 struct combat *def, struct emp_qelem *olist,
58 char *land_answer, int *a_spyp, int *a_engineerp);
59 static void take_move_in_mob(int combat_mode, struct ulist *llp,
60 struct combat *off, struct combat *def);
61 static void move_in_land(int combat_mode, struct combat *off,
62 struct emp_qelem *olist, struct combat *def);
63 static void ask_move_in(struct combat *off, struct emp_qelem *olist,
65 static void ask_move_in_off(struct combat *off, struct combat *def);
67 static int board_abort(struct combat *off, struct combat *def);
68 static int land_board_abort(struct combat *off, struct combat *def);
69 static int ask_off(int combat_mode, struct combat *off,
71 static void get_dlist(struct combat *def, struct emp_qelem *, int a_spy,
73 static int get_ototal(int combat_mode, struct combat *off,
74 struct emp_qelem *olist, double osupport, int check);
75 static int get_dtotal(struct combat *def, struct emp_qelem *list,
76 double dsupport, int check);
77 static double att_calcodds(int, int);
78 static int take_casualty(int combat_mode, struct combat *off,
79 struct emp_qelem *olist);
81 static void send_reacting_units_home(struct emp_qelem *list);
82 static int take_def(int combat_mode, struct emp_qelem *list,
83 struct combat *off, struct combat *def);
85 static int get_oland(int, struct ulist *);
86 static int get_dland(struct combat *, struct ulist *);
89 /* must match combat types in combat.h */
90 "defend", "attack", "assault", "paradrop", "board", "lboard"
95 * The principal object in this code is the "combat" object. A combat object
96 * is either a sector or ship. There are
97 * usually two instances of this, the "def" or defense combat object, and
98 * the array of "off" or offense objects. The number of offense objects is
99 * determined by the value of off->last (e.g. more than one attacking sector).
100 * the type of the object is determined by combat->type which can take the
101 * values EF_SECTOR, EF_SHIP, EF_PLANE, or EF_BAD. Another important parameter
102 * which is often passed to these functions is combat_mode. This can take
103 * the value A_DEFEND, A_ATTACK, A_ASSAULT, A_PARA, A_BOARD and A_LBOARD.
104 * As these six modes of being in combat affect things like mobcost and combat
105 * value, there are often switches made on combat_mode. Note that in all cases
106 * no mobility is taken from sectors, ships, or land units until the player
107 * has committed to a fight. Instead, the cost is temporarily placed in
108 * combat->mobcost, or llp->mobil as the case may be, and then when the object
109 * is "put" back onto disk, then the amounts in these variables are subtracted
110 * from the object's mobility. It needs to be done this way as the objects
111 * are constantly being re-read from disk, and we don't want to take any mob
112 * unless a fight actually occurrs.
116 /* initialize combat object */
119 att_combat_init(struct combat *com, int type)
121 memset(com, 0, sizeof(*com));
126 /* print a combat object with optional preposition */
129 pr_com(int inon, struct combat *com, natid who)
131 if (com->type == EF_SECTOR) {
133 inon ? inon == 1 ? "in " : "into " : "",
134 xyas(com->x, com->y, who));
135 } else if (com->type == EF_SHIP) {
136 return prbuf("%s%s %s(#%d)",
137 inon ? inon == 1 ? "on " : "onto " : "",
138 com->shp_mcp->m_name, com->shp_name, com->shp_uid);
139 } else if (com->type == EF_LAND) {
140 return prbuf("%s%s #%d",
141 inon ? inon == 1 ? "on " : "onto " : "",
142 com->lnd_lcp->l_name, com->lnd_uid);
144 return "your forces";
149 prcom(int inon, struct combat *com)
151 return pr_com(inon, com, player->cnum);
155 * This is the combat object "type" based integrity check. It basically
156 * splits along three divisions: ship/sector, attacker/defender,
157 * first time/not first time.
161 att_get_combat(struct combat *com, int isdef)
175 if (!getsect(com->x, com->y, §)) {
176 pr("Bad sector: %s\n", xyas(com->x, com->y, player->cnum));
177 return att_combat_init(com, EF_BAD);
179 com->sct_type = sect.sct_type;
180 com->sct_dcp = &dchr[sect.sct_type];
181 mil = sect.sct_item[I_MILIT];
182 pstage = sect.sct_pstage;
183 owner = sect.sct_own;
184 eff = sect.sct_effic;
185 mob = sect.sct_mobil;
190 if (!getland(com->lnd_uid, &land) || !land.lnd_own) {
192 pr("Land unit #%d is not in the same sector!\n",
194 return att_combat_init(com, EF_BAD);
196 if (isdef && player->owner) {
197 pr("Boarding yourself? Try using the 'load' command.\n");
198 return att_combat_init(com, EF_BAD);
200 com->lnd_lcp = &lchr[(int)land.lnd_type];
201 mil = land.lnd_item[I_MILIT];
202 pstage = land.lnd_pstage;
203 owner = land.lnd_own;
204 eff = land.lnd_effic;
205 mob = land.lnd_mobil;
210 if (!getship(com->shp_uid, &ship) || !ship.shp_own) {
212 pr("Ship #%d is not in the same sector!\n", com->shp_uid);
214 pr("Ship #%d is not your ship!\n", com->shp_uid);
215 return att_combat_init(com, EF_BAD);
218 if (isdef && player->owner && ontradingblock(EF_SHIP, &ship)) {
219 pr("%s is on the trading block.\n", prcom(0, com));
220 return att_combat_init(com, EF_BAD);
223 if (isdef && player->owner) {
224 pr("Boarding yourself? Try using the 'tend' command.\n");
225 return att_combat_init(com, EF_BAD);
227 com->shp_mcp = &mchr[(int)ship.shp_type];
228 strncpy(com->shp_name, ship.shp_name, MAXSHPNAMLEN);
229 if (!isdef && !player->owner) {
231 pr("%s was just sunk!\n", prcom(0, com));
233 pr("Ship #%d is not your ship!\n", com->shp_uid);
234 return att_combat_init(com, EF_BAD);
236 mil = ship.shp_item[I_MILIT];
237 pstage = ship.shp_pstage;
238 owner = ship.shp_own;
239 eff = ship.shp_effic;
240 mob = ship.shp_mobil;
249 return att_combat_init(com, EF_BAD);
252 if (!com->set) { /* first time */
253 if (isdef) { /* defender */
255 } else { /* attacker */
257 pr("No mil %s\n", prcom(1, com));
259 pr("Only 1 mil %s\n", prcom(1, com));
260 /* don't abandon attacking sectors or ships */
261 com->troops = MAX(0, mil - 1);
263 com->plague = pstage == PLG_INFECT;
264 } else { /* not first time */
265 if (isdef) { /* defender */
266 if (com->x != x || com->y != y) {
267 pr("%s has moved!\n", prcom(0, com));
268 return att_combat_init(com, EF_BAD);
270 if (owner != com->own) {
272 pr("WARNING: The ownership of %s just changed from %s to %s!\n",
273 prcom(0, com), cname(com->own), cname(owner));
274 } else if (com->type == EF_SECTOR) {
275 pr("WARNING: %s just abandoned sector %s!\n",
277 xyas(com->x, com->y, player->cnum));
281 pr("WARNING: The enemy mil %s just %s from %d to %d!\n",
283 com->mil < mil ? "increased" : "decreased", com->mil,
286 } else { /* attacker */
287 if (owner != com->own && owner != player->cnum) {
288 /* must be EF_SECTOR */
290 pr("WARNING: Your %d mil in %s were destroyed because %s just took the sector!\n",
291 com->mil, xyas(com->x, com->y, player->cnum),
294 pr("%s just took %s!\n",
295 cname(owner), xyas(com->x, com->y, player->cnum));
296 return att_combat_init(com, EF_BAD);
298 if (com->troops && com->troops + 1 > mil) {
299 if (com->own == owner && player->cnum == owner)
301 pr("WARNING: Your mil %s has been reduced from %d to %d!\n",
302 prcom(1, com), com->troops, MAX(0, mil - 1));
303 com->troops = MAX(0, mil - 1);
318 * In the course of the fight, the combat object may have lost mil, eff, or
319 * mobility. This is the place where the data in the object gets flushed to
320 * disk to make it "real".
324 put_combat(struct combat *com)
333 getsect(com->x, com->y, §);
334 sect.sct_type = com->sct_type;
335 deff = sect.sct_effic - com->eff;
337 sect.sct_road -= sect.sct_road * deff / 100.0;
338 sect.sct_rail -= sect.sct_rail * deff / 100.0;
339 sect.sct_defense -= sect.sct_defense * deff / 100.0;
340 if (sect.sct_road <= 0)
342 if (sect.sct_rail <= 0)
344 if (sect.sct_defense <= 0)
345 sect.sct_defense = 0;
347 sect.sct_effic = com->eff;
349 if (opt_MOB_ACCESS) {
350 if ((com->mob - com->mobcost) < -127)
351 sect.sct_mobil = -127;
353 sect.sct_mobil = com->mob - com->mobcost;
355 if ((com->mob - com->mobcost) < 0)
358 sect.sct_mobil = com->mob - com->mobcost;
361 sect.sct_own = com->own;
363 if (sect.sct_pstage == PLG_HEALTHY)
364 sect.sct_pstage = PLG_EXPOSED;
366 sect.sct_item[I_MILIT] = com->mil;
368 com->own = sect.sct_own; /* avoid WARNING if sector reverts */
371 getland(com->lnd_uid, &land);
372 land.lnd_effic = com->eff;
374 if (com->mob - com->mobcost < -127)
375 land.lnd_mobil = -127;
377 land.lnd_mobil = (signed char)(com->mob - com->mobcost);
379 land.lnd_own = com->own;
381 if (land.lnd_pstage == PLG_HEALTHY)
382 land.lnd_pstage = PLG_EXPOSED;
384 if (!(com->lnd_lcp->l_flags & L_SPY))
385 land.lnd_item[I_MILIT] = com->mil;
386 if (com->own == player->cnum) {
387 land.lnd_mission = 0;
389 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
391 putland(com->lnd_uid, &land);
394 getship(com->shp_uid, &ship);
395 ship.shp_effic = com->eff;
397 if (com->mob - com->mobcost < -127)
398 ship.shp_mobil = -127;
400 ship.shp_mobil = (signed char)(com->mob - com->mobcost);
402 ship.shp_own = com->own;
404 if (ship.shp_pstage == PLG_HEALTHY)
405 ship.shp_pstage = PLG_EXPOSED;
407 ship.shp_item[I_MILIT] = com->mil;
408 if (com->own == player->cnum) {
409 ship.shp_mission = 0;
411 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
413 putship(com->shp_uid, &ship);
416 att_get_combat(com, com->own != player->cnum);
419 /* If pre-attack, abort fight. If post-attack, don't move anything in */
424 return player->aborted = 1;
428 * This is the combat_mode based integrity check. It splits among two main
429 * divisions: first time/not first time, and attack/assault/para/board.
433 att_abort(int combat_mode, struct combat *off, struct combat *def)
439 if (att_get_combat(def, 1) < 0)
440 return abort_attack();
442 if (off && combat_mode != A_ATTACK) {
443 if (att_get_combat(off, 0) < 0)
444 return abort_attack();
445 if (off->type == EF_SHIP &&
446 !(off->x == def->x && off->y == def->y) &&
447 (!getsect(off->x, off->y, §) ||
448 sect.sct_type != SCT_WATER)) {
449 pr("%s can not %s from that far inland!\n",
450 prcom(0, off), att_mode[combat_mode]);
451 return abort_attack();
454 switch (combat_mode) {
456 if (!neigh(def->x, def->y, player->cnum) &&
457 !adj_units(def->x, def->y, player->cnum)) {
458 pr("You are not adjacent to %s\n",
459 xyas(def->x, def->y, player->cnum));
460 return abort_attack();
462 if (def->own == player->cnum) {
463 pr("You can't attack your own sector.\n");
464 return abort_attack();
468 if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
469 pr("You'll have to get there first...\n");
470 return abort_attack();
472 if (off && def->sct_type == SCT_MOUNT) {
473 pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
474 return abort_attack();
478 if (def->own == player->cnum) {
479 pr("You can't air-assault your own sector.\n");
480 return abort_attack();
482 if (off && (def->sct_type == SCT_MOUNT ||
483 def->sct_type == SCT_WATER ||
484 def->sct_type == SCT_CAPIT ||
485 def->sct_type == SCT_FORTR ||
486 def->sct_type == SCT_WASTE)) {
487 pr("You can't air-assault a %s sector!\n",
488 def->sct_dcp->d_name);
489 return abort_attack();
493 return board_abort(off, def);
495 return land_board_abort(off, def);
498 if (off && def->sct_dcp->d_mob0 < 0) {
499 pr("You can't %s a %s sector!\n",
500 att_mode[combat_mode], def->sct_dcp->d_name);
501 return abort_attack();
503 if (!off || off->relations_checked)
505 off->relations_checked = 1;
508 setcont(player->cnum, def->own, FOUND_SPY);
509 setcont(def->own, player->cnum, FOUND_SPY);
516 * Lots of special things need to be checked for boarding, so I put it in
521 board_abort(struct combat *off, struct combat *def)
523 struct shpstr aship, dship; /* for tech levels */
526 if (att_get_combat(def, 1) < 0)
527 return abort_attack();
532 if (att_get_combat(off, 0) < 0)
533 return abort_attack();
535 if (off->x != def->x || off->y != def->y) {
536 pr("Ship #%d is not in the same sector!\n", def->shp_uid);
537 return abort_attack();
539 if (off->type == EF_SHIP) {
541 pr("%s has no mobility!\n", prcom(0, off));
542 return abort_attack();
544 getship(off->shp_uid, &aship);
545 getship(def->shp_uid, &dship);
546 if (techfact(aship.shp_tech, shp_speed(&aship)) * off->eff
547 <= techfact(dship.shp_tech, shp_speed(&dship)) * def->eff) {
548 pr("Victim ship moves faster than you do!\n");
551 "%s (#%d) %s failed to catch %s\n",
552 cname(aship.shp_own), aship.shp_own,
553 pr_com(0, off, def->own), pr_com(0, def, def->own));
554 return abort_attack();
556 } else if (off->type != EF_SECTOR) {
557 pr("Please tell the deity that you got the 'banana boat' error\n");
558 return abort_attack();
560 if (def->shp_mcp->m_flags & M_SUB) {
561 getsect(def->x, def->y, §);
562 if (sect.sct_type == SCT_WATER) {
563 pr("You can't board a submarine!\n");
564 return abort_attack();
571 * Lots of special things need to be checked for boarding, so I put it in
573 * STM - I copied it for land unit boarding. :)
577 land_board_abort(struct combat *off, struct combat *def)
579 if (att_get_combat(def, 1) < 0)
580 return abort_attack();
585 if (att_get_combat(off, 0) < 0)
586 return abort_attack();
588 if (off->x != def->x || off->y != def->y) {
589 pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
590 return abort_attack();
596 /* If we are boarding, then the defending ship gets a chance to fire back */
598 att_approach(struct combat *off, struct combat *def)
604 pr("Approaching %s...\n", prcom(0, def));
607 "%s is being approached by %s...\n",
608 pr_com(0, def, def->own), pr_com(0, off, def->own));
609 if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
612 pr("They're firing at us sir!\n");
615 "Your fleet at %s does %d damage to %s\n",
616 xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
618 if (off->type == EF_SECTOR) {
619 getsect(off->x, off->y, §);
620 sectdamage(§, dam);
622 pr("Enemy fleet at %s does %d damage to %s\n",
623 xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
624 } else if (off->type == EF_SHIP) {
625 getship(off->shp_uid, &ship);
626 shipdamage(&ship, dam);
627 putship(off->shp_uid, &ship);
628 if (def->own && ship.shp_effic < SHIP_MINEFF) {
629 wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
630 nreport(player->cnum, N_SHP_LOSE, def->own, 1);
633 if (att_get_combat(off, 0) < 0)
634 return abort_attack();
638 /* The attack is valid. Tell the attacker about what they're going to hit */
641 att_show(struct combat *def)
643 /* Note that we tell the player about the treaty BEFORE we tell them
644 about the item. If we didn't, then they gain free information */
645 if (def->type == EF_SECTOR) {
646 if (!trechk(player->cnum, def->own, LANATT))
647 return abort_attack();
648 pr("%s is a %d%% %s %s with approximately %d military.\n",
649 xyas(def->x, def->y, player->cnum),
650 roundintby((int)def->eff, 10),
651 cname(def->own), def->sct_dcp->d_name,
652 roundintby(def->troops, 10));
653 if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
654 writemap(player->cnum);
655 } else if (def->type == EF_SHIP || def->type == EF_LAND) {
656 if (def->type == EF_SHIP) {
657 if (!trechk(player->cnum, def->own, SEAATT))
658 return abort_attack();
660 if (!trechk(player->cnum, def->own, LNDATT))
661 return abort_attack();
663 pr("%s is about %d%% efficient and has approximately %d mil on board.\n",
664 prcom(0, def), roundintby((int)def->eff, 10),
665 roundintby(def->troops, 10));
667 /* Ok, everything is fine */
671 /* Attack and assault ask the user which kind of support they want */
674 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
679 *fortp = *shipp = *landp = *planep = 1;
681 if (player->argp[offset] != NULL) {
682 if ((player->argp[offset + 1] == NULL) ||
683 (player->argp[offset + 2] == NULL) ||
684 (player->argp[offset + 3] == NULL)) {
685 pr("If any support arguments are used, all must be!\n");
690 *landp = *planep = 0;
692 p = getstarg(player->argp[offset], "Use fort support? ", buf);
696 if ((*p == 'y') || (*p == 'Y'))
699 p = getstarg(player->argp[offset + 1], "Use ship support? ", buf);
703 if ((*p == 'y') || (*p == 'Y'))
706 p = getstarg(player->argp[offset + 2], "Use land support? ", buf);
710 if ((*p == 'y') || (*p == 'Y'))
713 p = getstarg(player->argp[offset + 3], "Use plane support? ", buf);
717 if ((*p == 'y') || (*p == 'Y'))
724 * Attack, assault, and board ask the attacker what they'd like to attack
725 * with. This includes mil and land units from each "off" object. Note that
726 * after each sub-prompt, we check to make sure that the attack is still
727 * valid, and if it's not, then we abort the attack.
731 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
732 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
735 char land_answer[256];
738 if (att_abort(combat_mode, off, def))
740 memset(land_answer, 0, sizeof(land_answer));
741 for (n = 0; n <= off->last; ++n) {
742 off[n].troops = ask_off(combat_mode, off + n, def);
743 if (att_abort(combat_mode, off, def))
745 ask_olist(combat_mode, off + n, def, olist, land_answer,
746 a_spyp, a_engineerp);
747 if (att_abort(combat_mode, off, def))
754 * Return path cost for ATTACKER to enter sector given by DEF.
755 * MOBTYPE is a mobility type accepted by sector_mcost().
758 att_mobcost(natid attacker, struct combat *def, int mobtype)
763 if (CANT_HAPPEN(def->type != EF_SECTOR))
765 ok = getsect(def->x, def->y, §);
766 if (CANT_HAPPEN(!ok))
770 * We want the cost to move/march into the sector. If we just
771 * called sector_mcost(), we'd get the defender's cost. The
772 * attacker's cost is higher unless he's the old-owner. Note: if
773 * there are no civilians, a victorious attacker will become the
774 * old-owner. But he isn't now.
776 sect.sct_own = attacker;
778 return sector_mcost(§, mobtype);
781 /* How many mil is off allowed to attack with when it attacks def? */
784 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
789 switch (combat_mode) {
791 mobcost = att_mobcost(off->own, def, MOB_MOVE);
792 if (mobcost < 0 || off->mob <= 0)
794 mob_support = off->mob / mobcost;
795 if (mob_support < off->troops)
796 pr("Sector %s has %d mobility which can only support %d mil,\n",
797 xyas(off->x, off->y, player->cnum), off->mob, mob_support);
799 mob_support = off->troops;
802 if (def->own != player->cnum && def->mil) {
803 if (off->shp_mcp->m_flags & M_SEMILAND)
804 return off->troops / 4;
805 else if (!(off->shp_mcp->m_flags & M_LAND))
806 return off->troops / 10;
810 if (off->type == EF_SECTOR && off->mob <= 0)
812 mob_support = def->shp_mcp->m_item[I_MILIT];
813 if (mob_support < off->troops)
814 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
816 mob_support = off->troops;
821 if (def->lnd_lcp->l_flags & L_SPY)
823 mob_support = def->lnd_lcp->l_item[I_MILIT];
824 if (mob_support < off->troops)
825 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
827 mob_support = off->troops;
834 * If the attacker decides to go through with the attack, then the
835 * sectors/ships they are attacking with may be charged some mobility.
836 * This is where that amount of mobility is calculated. It is actually
837 * subtracted "for real" from the object's mobility in put_combat().
841 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
848 switch (combat_mode) {
850 off->mobcost += MAX(1,
852 * att_mobcost(off->own, def, MOB_MOVE)));
855 off->mobcost += MAX(1, attacking_mil / 5);
860 off->mobcost += MAX(1, attacking_mil / 5);
863 /* the 2 in the formula below is a fudge factor */
864 getship(def->shp_uid, &ship);
865 off->mobcost += (def->eff / 100) * (shp_speed(&ship) / 2);
870 /* How many mil to we want to attack from off against def? */
873 ask_off(int combat_mode, struct combat *off, struct combat *def)
879 if (att_get_combat(off, 0) <= 0)
881 if ((off->type == EF_SECTOR) && (off->own != player->cnum))
883 if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
885 if (off->type == EF_SECTOR) {
886 if (off->own != player->cnum)
888 sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
889 off->sct_dcp->d_name,
890 xyas(off->x, off->y, player->cnum), mob_support);
892 sprintf(prompt, "Number of mil from %s (max %d) : ",
893 prcom(0, off), mob_support);
895 if ((attacking_mil = onearg(NULL, prompt)) < 0)
897 if (att_abort(combat_mode, off, def))
899 if (att_get_combat(off, 0) <= 0)
902 MIN(attacking_mil, MIN(mob_support, off->troops))) <= 0)
905 calc_mobcost(combat_mode, off, def, attacking_mil);
906 return attacking_mil;
910 * Which units would you like to attack with or move in with [ynYNq?]
914 att_prompt(char *prompt, char army)
922 p = getstring(prompt, buf);
923 if (!p || *p == 'q') {
929 if (tolower(*p) == 'y' || tolower(*p) == 'n')
931 pr("y - yes this unit\n"
933 "Y - yes to all units in army '%c'\n"
934 "N - no to all units in army '%c'\n"
935 "q - quit\n? - this help message\n\n",
940 /* Ask the attacker which units they want to attack/assault/board with */
943 ask_olist(int combat_mode, struct combat *off, struct combat *def,
944 struct emp_qelem *olist, char *land_answer, int *a_spyp,
949 double pathcost, mobcost;
959 if (def->type == EF_LAND)
961 if (def->type == EF_SHIP)
962 maxland = def->shp_mcp->m_nland;
964 snxtitem_xy(&ni, EF_LAND, off->x, off->y);
965 while (nxtitem(&ni, &land)) {
966 if (land.lnd_own != player->cnum)
968 if (land.lnd_effic < LAND_MINEFF)
970 if (land_answer[(int)land.lnd_army] == 'N')
972 if (!lnd_can_attack(&land))
974 lcp = &lchr[(int)land.lnd_type];
976 if (def->type == EF_SHIP && !maxland) {
977 pr("Land units are not able to board this kind of ship\n");
980 if (def->type == EF_SHIP
981 && (def->shp_mcp->m_flags & (M_SUPPLY | M_SUB)) != M_SUPPLY
982 && !(lcp->l_flags & L_LIGHT)) {
983 pr("Only light land units can board this kind of ship\n");
986 if (land.lnd_mobil <= 0) {
987 pr("%s is out of mobility, and cannot %s\n",
988 prland(&land), att_mode[combat_mode]);
993 if (ontradingblock(EF_LAND, &land)) {
994 pr("%s is on the trading block, and cannot %s\n",
995 prland(&land), att_mode[combat_mode]);
1000 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
1001 pr("%s is on ship #%d, and cannot %s\n",
1002 prland(&land), land.lnd_ship, att_mode[combat_mode]);
1004 } else if (off->type == EF_SHIP) {
1005 if (land.lnd_ship != off->shp_uid)
1007 } else if (land.lnd_land >= 0) {
1008 pr("%s is on unit #%d, and cannot %s\n",
1009 prland(&land), land.lnd_land, att_mode[combat_mode]);
1012 switch (combat_mode) {
1015 * We used to let land units attack only if they have the
1016 * mobility consumed by the attack, not counting combat
1017 * and moving in to occupy. Making sure your land units
1018 * reach attack positions with enough mobility left is a
1019 * pain in the neck. We now require positive mobility,
1020 * just like for marching. Except we don't allow rushing
1021 * of high-mobility sectors (mountains): for those we
1022 * still require attack mobility.
1024 pathcost = att_mobcost(land.lnd_own, def, lnd_mobtype(&land));
1025 mobcost = lnd_pathcost(&land, pathcost);
1026 if (pathcost < 1.0) {
1027 if (land.lnd_mobil <= 0) {
1028 pr("%s is out of mobility\n", prland(&land));
1032 reqmob = MIN(land_mob_max, (int)ceil(mobcost));
1033 if (land.lnd_mobil < reqmob) {
1034 pr("%s does not have enough mobility (%d needed)\n",
1035 prland(&land), reqmob);
1043 if (!(lcp->l_flags & L_ASSAULT))
1050 att_val = attack_val(combat_mode, &land);
1051 if (att_val < 1.0) {
1052 pr("%s has no offensive strength\n", prland(&land));
1055 if (!lnd_supply_all(&land)) {
1056 pr("%s is out of supply, and cannot %s\n",
1057 prland(&land), att_mode[combat_mode]);
1060 if (def->type == EF_SHIP && first_time) {
1062 pr("You may board with a maximum of %d land units\n", maxland);
1064 pr("%s has a base %s value of %.0f\n",
1065 prland(&land), att_mode[combat_mode], att_val);
1066 if (land_answer[(int)land.lnd_army] != 'Y') {
1068 "%s with %s %s (%c %d%%) [ynYNq?] ",
1069 att_mode[combat_mode],
1072 land.lnd_army ? land.lnd_army : '~',
1074 land_answer[(int)land.lnd_army] =
1075 att_prompt(prompt, land.lnd_army);
1076 if (att_abort(combat_mode, off, def))
1078 if (land_answer[(int)land.lnd_army] != 'y' &&
1079 land_answer[(int)land.lnd_army] != 'Y')
1082 if (!(llp = malloc(sizeof(struct ulist)))) {
1083 logerror("Malloc failed in attack!\n");
1087 memset(llp, 0, sizeof(struct ulist));
1088 emp_insque(&llp->queue, olist);
1089 llp->mobil = mobcost;
1090 llp->unit.land = land;
1091 llp->x = llp->unit.land.lnd_x;
1092 llp->y = llp->unit.land.lnd_y;
1093 llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
1094 llp->eff = llp->unit.land.lnd_effic;
1095 if (lnd_spyval(&land) > *a_spyp)
1096 *a_spyp = lnd_spyval(&land);
1097 if (((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)
1099 if (def->type == EF_SHIP && ++count >= maxland)
1104 /* What's the offense or defense multiplier? */
1107 att_combat_eff(struct combat *com)
1113 if (com->type == EF_SECTOR) {
1114 eff = com->eff / 100.0;
1115 if (com->own == player->cnum) {
1116 str = com->sct_dcp->d_ostr;
1117 eff = 1.0 + (str - 1.0) * eff;
1119 eff = sector_strength(getsectp(com->x, com->y));
1120 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1121 getship(com->shp_uid, &ship);
1122 eff = 1.0 + shp_armor(&ship) / 100.0;
1128 att_get_offense(int combat_mode, struct combat *off,
1129 struct emp_qelem *olist, struct combat *def)
1134 * Get the attacker units & mil again in case they changed while the
1135 * attacker was answering sub-prompts.
1138 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1139 if (att_empty_attack(combat_mode, ototal, def))
1140 return abort_attack();
1141 if (combat_mode == A_PARA)
1143 pr("\n Initial attack strength: %8d\n", ototal);
1147 /* Get the defensive units and reacting units */
1149 att_get_defense(struct emp_qelem *olist, struct combat *def,
1150 struct emp_qelem *dlist, int a_spy, int ototal)
1153 struct emp_qelem *qp;
1159 get_dlist(def, dlist, 0, &d_spy);
1160 dtotal = get_dtotal(def, dlist, 1.0, 0);
1163 * Call in reacting units
1166 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1167 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1169 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1170 llp = (struct ulist *)qp;
1171 intelligence_report(def->own, &llp->unit.land, d_spy,
1172 "Scouts report attacking unit:");
1175 old_dtotal = dtotal;
1176 dtotal = get_dtotal(def, dlist, 1.0, 0);
1177 if (dtotal != old_dtotal)
1178 pr("Defense strength with reacting units: %8d\n", dtotal);
1183 /* Get the defensive land units in the sector or on the ship */
1186 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1189 struct nstr_item ni;
1193 /* In here is where you need to take out spies and trains from the defending
1194 lists. Spies try to hide, trains get trapped and can be boarded. */
1196 snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1197 while (nxtitem(&ni, &land)) {
1200 if (land.lnd_own != def->own)
1202 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1204 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1206 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1208 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1210 intelligence_report(player->cnum, &land, a_spy,
1211 "Scouts report defending unit:");
1212 if (!(llp = malloc(sizeof(struct ulist)))) {
1213 logerror("Malloc failed in attack!\n");
1217 memset(llp, 0, sizeof(struct ulist));
1218 emp_insque(&llp->queue, list);
1219 llp->supplied = lnd_supply_all(&land);
1220 llp->unit.land = land;
1221 llp->x = llp->unit.land.lnd_x;
1222 llp->y = llp->unit.land.lnd_y;
1223 llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
1224 llp->eff = llp->unit.land.lnd_effic;
1225 if (lnd_spyval(&land) > *d_spyp)
1226 *d_spyp = lnd_spyval(&land);
1230 /* Calculate the total offensive strength */
1233 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1234 double osupport, int check)
1236 double ototal = 0.0;
1237 struct emp_qelem *qp, *next;
1242 * first, total the attacking mil
1245 for (n = 0; n <= off->last; ++n) {
1246 if (off[n].type == EF_BAD || (check &&
1247 att_get_combat(&off[n], 0) <= 0))
1249 ototal += off[n].troops * att_combat_eff(off + n);
1253 * next, add in the attack_values of all
1254 * the attacking units
1257 for (qp = olist->q_forw; qp != olist; qp = next) {
1259 llp = (struct ulist *)qp;
1260 if (check && !get_oland(combat_mode, llp))
1262 if (combat_mode == A_ATTACK) {
1264 for (n = 0; n <= off->last; ++n) {
1265 if (off[n].type == EF_BAD)
1267 if ((off[n].x == llp->unit.land.lnd_x) &&
1268 (off[n].y == llp->unit.land.lnd_y))
1272 lnd_print(player->cnum, llp,
1273 "can't attack from this sector now");
1277 ototal += attack_val(combat_mode, &llp->unit.land) *
1278 att_combat_eff(off + w);
1280 ototal += attack_val(combat_mode, &llp->unit.land);
1285 return ldround(ototal, 1);
1288 /* Calculate the total defensive strength */
1291 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1294 double dtotal = 0.0, eff = 1.0, d_unit;
1295 struct emp_qelem *qp, *next;
1298 if (check && att_get_combat(def, 1) < 0)
1300 eff = att_combat_eff(def);
1301 dtotal = def->troops * eff;
1304 * next, add in the defense_values of all
1305 * the defending non-retreating units
1308 for (qp = list->q_forw; qp != list; qp = next) {
1310 llp = (struct ulist *)qp;
1311 if (check && !get_dland(def, llp))
1313 d_unit = defense_val(&llp->unit.land);
1316 dtotal += d_unit * eff;
1321 return ldround(dtotal, 1);
1325 * This is the land unit integrity check.
1329 get_oland(int combat_mode, struct ulist *llp)
1331 struct lndstr *lp = &llp->unit.land;
1334 getland(llp->unit.land.lnd_uid, lp);
1336 if (lp->lnd_own != player->cnum) {
1337 sprintf(buf, "was destroyed and is no longer a part of the %s",
1338 att_mode[combat_mode]);
1339 lnd_print(player->cnum, llp, buf);
1343 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1345 "left to fight another battle and is no longer a part of the %s",
1346 att_mode[combat_mode]);
1347 lnd_print(player->cnum, llp, buf);
1351 if (lp->lnd_effic < llp->eff) {
1352 sprintf(buf, "damaged from %d%% to %d%%",
1353 llp->eff, lp->lnd_effic);
1354 lnd_print(player->cnum, llp, buf);
1357 llp->eff = llp->unit.land.lnd_effic;
1362 get_dland(struct combat *def, struct ulist *llp)
1364 struct lndstr *lp = &llp->unit.land;
1367 getland(llp->unit.land.lnd_uid, lp);
1369 if (lp->lnd_effic < LAND_MINEFF) {
1370 sprintf(buf, "was destroyed and is no longer a part of the defense");
1371 lnd_print(llp->unit.land.lnd_own, llp, buf);
1375 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1376 lnd_print(llp->unit.land.lnd_own, llp,
1377 "left to go fight another battle and is no longer a part of the defense");
1382 llp->eff = llp->unit.land.lnd_effic;
1387 kill_land(struct emp_qelem *list)
1389 struct emp_qelem *qp, *next;
1392 for (qp = list->q_forw; qp != list; qp = next) {
1394 llp = (struct ulist *)qp;
1395 if (llp->unit.land.lnd_ship >= 0) {
1396 llp->unit.land.lnd_effic = 0;
1397 lnd_print(player->cnum, llp,
1398 "cannot return to the ship, and dies!");
1405 att_infect_units(struct emp_qelem *list, int plague)
1407 struct emp_qelem *qp, *next;
1412 for (qp = list->q_forw; qp != list; qp = next) {
1414 llp = (struct ulist *)qp;
1415 if (llp->unit.land.lnd_pstage == PLG_HEALTHY)
1416 llp->unit.land.lnd_pstage = PLG_EXPOSED;
1421 * Put the land unit on the disk. If there was some mobility cost, then
1422 * subtract it from the units mobility. Note that this works the same way
1423 * as sectors & ships in that no mobility is actually taken until the attacker
1424 * has committed to attacking.
1428 put_oland(struct emp_qelem *list)
1430 struct emp_qelem *qp, *next;
1433 for (qp = list->q_forw; qp != list; qp = next) {
1435 llp = (struct ulist *)qp;
1436 llp->unit.land.lnd_mission = 0;
1437 llp->unit.land.lnd_harden = 0;
1438 llp->unit.land.lnd_mobil -= (int)llp->mobil;
1440 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1441 if (llp->unit.land.lnd_own != player->cnum) {
1442 emp_remque((struct emp_qelem *)llp);
1445 get_oland(A_ATTACK, llp);
1450 * Keep sending in reinforcements until it looks like we're going to win.
1451 * Note that the "strength" command also calls this routine.
1455 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1456 int *d_spyp, int ototal)
1458 struct nstr_item ni;
1462 double new_land = 0;
1466 double eff = att_combat_eff(def);
1469 dtotal = get_dtotal(def, list, 1.0, 1);
1472 snxtitem_all(&ni, EF_LAND);
1473 while (nxtitem(&ni, &land) && dtotal + new_land * eff < 1.2 * ototal) {
1476 if (land.lnd_mission != MI_RESERVE)
1478 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1480 if (land.lnd_own != def->own)
1482 if (land.lnd_ship >= 0)
1484 if (land.lnd_land >= 0)
1486 if (defense_val(&land) < 1.0)
1488 if (!lnd_can_attack(&land))
1491 /* Only supplied units can react */
1492 if (list ? !lnd_supply_all(&land) : !lnd_could_be_supplied(&land))
1495 if (!in_oparea((struct empobj *)&land, def->x, def->y))
1498 pathcost = path_find(land.lnd_x, land.lnd_y, def->x, def->y,
1499 def->own, lnd_mobtype(&land));
1502 mobcost = lnd_pathcost(&land, pathcost);
1503 if (land.lnd_mobil < mobcost)
1506 new_land += defense_val(&land);
1508 if (!list) /* we are in the "strength" command */
1511 /* move to defending sector */
1512 land.lnd_mobil -= ldround(mobcost, 1);
1515 land.lnd_x = def->x;
1516 land.lnd_y = def->y;
1517 putland(land.lnd_uid, &land);
1518 wu(0, land.lnd_own, "%s reacts to %s.\n",
1519 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1521 llp = malloc(sizeof(struct ulist));
1523 memset(llp, 0, sizeof(struct ulist));
1527 llp->chrp = (struct empobj_chr *)&lchr[(int)land.lnd_type];
1528 llp->unit.land = land;
1529 llp->eff = land.lnd_effic;
1530 emp_insque(&llp->queue, list);
1531 if (lnd_spyval(&land) > *d_spyp)
1532 *d_spyp = lnd_spyval(&land);
1534 intelligence_report(player->cnum, &land, a_spy,
1535 "Scouts sight reacting enemy unit:");
1540 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1543 get_osupport(char *outs, struct combat *def, int fort_sup, int ship_sup,
1544 int land_sup, int plane_sup)
1546 double osupport = 1.0;
1548 double af, as, au, ap;
1550 af = as = au = ap = 0.0;
1552 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1557 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1564 dam = lnd_support(def->own, player->cnum, def->x, def->y, 0);
1570 dam = off_support(def->x, def->y, def->own, player->cnum);
1574 sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1579 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1582 get_dsupport(char *outs, struct emp_qelem *list, struct combat *def,
1583 int ototal, int dtotal)
1585 double dsupport = 1.0;
1587 double df, ds, du, dp;
1590 df = ds = du = dp = 0.0;
1591 if (dtotal < 0.1 * ototal) {
1593 } else if (dtotal >= 1.2 * ototal) {
1596 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1600 dtotal = get_dtotal(def, list, dsupport, 0);
1601 if (dtotal < 1.2 * ototal) {
1602 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1605 dtotal = get_dtotal(def, list, dsupport, 0);
1607 if (dtotal < 1.2 * ototal) {
1608 dam = lnd_support(player->cnum, def->own, def->x, def->y, 1);
1611 dtotal = get_dtotal(def, list, dsupport, 1);
1613 if (dtotal < 1.2 * ototal) {
1614 dam = def_support(def->x, def->y, player->cnum, def->own);
1622 sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1627 "\nOdds are bad for us...support cancelled.\n\n");
1630 "\nOdds are good for us...support cancelled.\n\n");
1636 * Land mines add to the defense multiplier. If the attacker has engineers
1637 * then this multiplier is cut in half.
1641 get_mine_dsupport(struct combat *def, int a_engineer)
1646 getsect(def->x, def->y, §);
1648 if (sect.sct_oldown != player->cnum) {
1649 mines = SCT_LANDMINES(§);
1650 mines = MIN(mines, 20);
1652 mines = ldround(mines / 2.0, 1);
1655 wu(0, def->own, "Defending mines add %1.2f\n",
1657 pr("Defending mines add %1.2f\n", mines * 0.02);
1658 return mines * 0.02;
1664 /* Get the offensive and defensive support */
1666 att_get_support(int combat_mode, int ofort, int oship, int oland,
1667 int oplane, struct emp_qelem *olist, struct combat *off,
1668 struct emp_qelem *dlist, struct combat *def,
1669 double *osupportp, double *dsupportp, int a_engineer)
1672 char osupports[512];
1673 char dsupports[512];
1675 if (combat_mode == A_PARA)
1678 *osupportp = get_osupport(osupports, def,
1679 ofort, oship, oland, oplane);
1682 * I need to put a 1 at the end of the next four total_stren calls
1683 * because units & mil may have been damaged by collateral damage or
1684 * nuclear warheads from the offensive & defensive support.
1687 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1688 if (att_empty_attack(combat_mode, ototal, def))
1689 return abort_attack();
1690 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1693 * Calculate defensive support. If odds are too good or too bad
1694 * then don't call in support.
1697 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1698 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1699 if (att_empty_attack(combat_mode, ototal, def))
1700 return abort_attack();
1702 if ((*osupports || *dsupports) &&
1703 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1704 pr("\n\t\tsupport values\n");
1705 pr("\t\tforts\tships\tunits\tplanes\n");
1706 if (*osupportp != 1.0)
1707 pr("%s", osupports);
1708 if (*dsupportp != 1.0)
1709 pr("%s", dsupports);
1711 wu(0, def->own, "\n\t\tsupport values\n");
1712 wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1713 if (*osupportp != 1.0)
1714 wu(0, def->own, "%s", osupports);
1715 if (*dsupportp != 1.0)
1716 wu(0, def->own, "%s", dsupports);
1720 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1721 if (dtotal && def->type == EF_SECTOR)
1722 *dsupportp += get_mine_dsupport(def, a_engineer);
1726 /* How many two-legged bipeds are in this combat force? */
1729 count_bodies(struct combat *off, struct emp_qelem *list)
1733 struct emp_qelem *qp;
1736 for (n = 0; n <= off->last; ++n)
1737 bodies += off[n].troops;
1738 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1739 llp = (struct ulist *)qp;
1740 bodies += llp->unit.land.lnd_item[I_MILIT];
1745 /* This is where the fighting actually occurs. */
1748 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1749 double osupport, struct combat *def, struct emp_qelem *dlist,
1753 int a_cas = 0; /* Casualty counts */
1755 int ototal; /* total attacking strength */
1756 int dtotal; /* total defending strength */
1757 int a_bodies; /* total attacking mil (incl. mil in units) */
1758 int d_bodies; /* total defending mil (incl. mil in units) */
1768 ototal = get_ototal(combat_mode, off, olist, osupport,
1769 combat_mode != A_PARA);
1770 dtotal = get_dtotal(def, dlist, dsupport, 0);
1774 a_bodies = count_bodies(off, olist);
1775 d_bodies = count_bodies(def, dlist);
1776 d_mil = def->troops;
1777 for (n = 0; n <= off->last; ++n)
1778 if (off[n].type == EF_BAD)
1781 a_troops[n] = off[n].troops;
1783 /* This switch is required to get the spacing right */
1784 switch (combat_mode) {
1786 pr(" Final attack strength: %8d\n", ototal);
1789 pr(" Final assault strength: %8d\n", ototal);
1792 if (def->sct_type == SCT_MOUNT ||
1793 def->sct_type == SCT_WATER ||
1794 def->sct_type == SCT_CAPIT ||
1795 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1796 pr("You can't air-assault a %s sector!\n",
1797 def->sct_dcp->d_name);
1800 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1802 pr(" Final air-assault strength: %8d\n", ototal);
1806 pr(" Final board strength: %8d\n", ototal);
1810 pr(" Final defense strength: %8d\n", dtotal);
1811 odds = att_calcodds(ototal, dtotal);
1812 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1814 /* spread the plague */
1815 if (combat_mode != A_PARA) {
1817 for (n = 0; n <= off->last; ++n)
1818 if (off[n].type != EF_BAD)
1819 def->plague |= off[n].plague;
1820 for (n = 0; n <= off->last; ++n)
1821 if (off[n].type != EF_BAD)
1822 off[n].plague |= def->plague;
1824 att_infect_units(olist, off->plague);
1825 att_infect_units(dlist, def->plague);
1828 * Fighting is slightly random. There is always that last little
1829 * effort you see people put in. Or the stray bullet that takes out
1830 * an officer and the rest go into chaos. Things like that.
1831 * Thus, we have added a very slight random factor that will sometimes
1832 * allow the little guy to win. We modify the odds a little
1833 * (either +- 5%) to account for this randomness. We also only
1834 * recalculate the odds every 8-50 casualties, not every cacsualty,
1835 * since a single dead guy normally wouldn't cause a commander to
1836 * rethink his strategies, but 50 dead guys might.
1838 odds += (random() % 11 - 5) / 100.0;
1843 recalctime = 8 + (random() % 43);
1844 while (!success && ototal) {
1847 d_cas += take_casualty(A_DEFEND, def, dlist);
1848 dtotal = get_dtotal(def, dlist, dsupport, 0);
1853 a_cas += take_casualty(combat_mode, off, olist);
1854 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1856 if (((a_cas + d_cas) % 70) == 69)
1858 if (recalctime-- <= 0) {
1859 recalctime = 8 + (random() % 43);
1860 odds = att_calcodds(ototal, dtotal);
1861 odds += (random() % 11 - 5) / 100.0;
1869 /* update defense mobility & mil */
1873 if (def->type == EF_SECTOR && d_mil && d_cas) {
1877 newmob = damage(def->mob, 100 * d_cas / d_mil);
1878 def->mobcost = MIN(20, def->mob - newmob);
1881 def->mil = def->troops;
1884 /* update attack mobility & mil */
1885 for (n = 0; n <= off->last; ++n)
1886 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1887 if (off[n].type == EF_SECTOR && off[n].mil) {
1888 if (!CANT_HAPPEN(off[n].mob < 0)) {
1889 newmob = damage(off[n].mob,
1890 100 * (a_troops[n] - off[n].troops)
1892 off[n].mobcost += MIN(20, off[n].mob - newmob);
1895 off[n].mil -= a_troops[n] - off[n].troops;
1898 /* update land unit mobility */
1899 if (d_bodies && d_cas)
1900 lnd_takemob(dlist, (double)d_cas / d_bodies);
1901 if (a_bodies && a_cas)
1902 lnd_takemob(olist, (double)a_cas / a_bodies);
1904 /* damage attacked sector */
1905 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1907 pr("- Casualties -\n Yours: %d\n", a_cas);
1908 pr(" Theirs: %d\n", d_cas);
1909 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
1910 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
1913 switch (combat_mode) {
1915 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
1916 pr("We have captured %s, sir!\n", prcom(0, def));
1920 news_item = def->own ? N_AWON_SECT : N_START_COL;
1921 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
1922 action = "assaulting and taking";
1925 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
1926 pr("We have captured %s, sir!\n", prcom(0, def));
1927 action = "air-assaulting and taking";
1930 news_item = N_BOARD_SHIP;
1931 pr("We have boarded %s, sir!\n", prcom(0, def));
1932 action = "boarding";
1935 news_item = N_BOARD_LAND;
1936 pr("We have boarded %s, sir!\n", prcom(0, def));
1937 action = "boarding";
1942 action = "defeating";
1945 switch (combat_mode) {
1947 news_item = N_SCT_LOSE;
1948 pr("You have been defeated!\n");
1949 action = "attacking";
1952 news_item = N_ALOSE_SCT;
1953 pr("You have been defeated!\n");
1955 action = "trying to assault";
1958 news_item = N_PLOSE_SCT;
1959 pr("All of your troops were destroyed\n");
1960 action = "trying to air-assault";
1963 news_item = N_SHP_LOSE;
1964 pr("You have been repelled\n");
1966 action = "trying to board";
1969 news_item = N_LND_LOSE;
1970 pr("You have been repelled\n");
1972 action = "trying to board";
1977 action = "fighting";
1980 nreport(player->cnum, news_item, def->own, 1);
1983 "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
1984 cname(player->cnum), player->cnum, a_cas,
1985 action, pr_com(0, def, def->own), d_cas);
1988 send_reacting_units_home(dlist);
1990 /* putland the defending land */
1993 /* putland the attacking land */
1996 /* put the victim sector/ship/land */
1997 if (!success || !take_def(combat_mode, olist, off, def))
2000 /* put the attacking sectors/ship */
2001 for (n = 0; n <= off->last; ++n)
2002 if (off[n].type != EF_BAD)
2003 put_combat(&off[n]);
2008 switch (combat_mode) {
2010 ask_move_in(off, olist, def);
2012 /* put sectors again to get abandon warnings */
2013 for (n = 0; n <= off->last; ++n)
2014 if (off[n].type != EF_BAD)
2015 put_combat(&off[n]);
2018 att_move_in_off(combat_mode, off, olist, def);
2021 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2025 /* What percentage of the combat forces going head-to-head are we? */
2028 att_calcodds(int ototal, int dtotal)
2032 /* calculate odds */
2035 else if (dtotal <= 0)
2038 odds = (double)ototal / (dtotal + ototal);
2043 /* Here's where the dead soldiers get dragged off the battlefield */
2046 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2048 int to_take = CASUALTY_LUMP;
2049 int biggest_troops = 0, index = -1;
2050 int n, tot_troops = 0, biggest_mil, cas;
2051 struct emp_qelem *qp, *biggest;
2054 for (n = 0; n <= off->last; ++n) {
2055 if (off[n].type != EF_BAD) {
2056 tot_troops += off[n].troops;
2057 if (off[n].troops > biggest_troops) {
2058 biggest_troops = off[n].troops;
2065 to_take -= tot_troops;
2068 for (n = 0; n <= off->last; ++n)
2069 if (off[n].type != EF_BAD)
2073 * They can all come off mil. We rotate the casualties,
2074 * starting with the sector containing the most mil.
2076 to_take = CASUALTY_LUMP;
2078 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2081 while (to_take > 0) {
2082 for (n = index; n <= off->last && to_take; ++n) {
2083 if (off[n].type != EF_BAD && off[n].troops > 0) {
2088 for (n = 0; n < index && to_take; ++n) {
2089 if (off[n].type != EF_BAD && off[n].troops > 0) {
2095 return CASUALTY_LUMP;
2099 return CASUALTY_LUMP - to_take;
2102 * Need to take some casualties from attacking units
2103 * Procedure: find the biggest unit remaining (in
2104 * terms of mil) and give it the casualties.
2108 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2109 llp = (struct ulist *)qp;
2111 if (llp->unit.land.lnd_item[I_MILIT] > biggest_mil) {
2112 biggest_mil = llp->unit.land.lnd_item[I_MILIT];
2116 if (biggest == NULL)
2117 return CASUALTY_LUMP - to_take;
2119 llp = (struct ulist *)biggest;
2120 cas = lnd_take_casualty(combat_mode, llp, to_take);
2121 return CASUALTY_LUMP - (to_take - cas);
2124 /* Send reacting defense units back to where they came from (at no mob cost) */
2127 send_reacting_units_home(struct emp_qelem *list)
2129 struct emp_qelem *qp, *next;
2133 for (qp = list->q_forw; qp != list; qp = next) {
2135 llp = (struct ulist *)qp;
2136 if ((llp->unit.land.lnd_x != llp->x) ||
2137 (llp->unit.land.lnd_y != llp->y)) {
2138 sprintf(buf, "returns to %s",
2139 xyas(llp->x, llp->y, llp->unit.land.lnd_own));
2140 llp->unit.land.lnd_x = llp->x;
2141 llp->unit.land.lnd_y = llp->y;
2142 lnd_print(llp->unit.land.lnd_own, llp, buf);
2148 /* Check for 0 offense strength. This call will always preceed an abort */
2151 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2154 if (def->own && player->cnum != def->own) {
2156 "%s (#%d) considered %sing you @%s\n",
2157 cname(player->cnum), player->cnum,
2158 att_mode[combat_mode], xyas(def->x, def->y, def->own));
2160 pr("No troops for %s...\n", att_mode[combat_mode]);
2167 * Take the defending sector or ship from the defender and give it to the
2172 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2177 struct ulist *llp, *delete_me = NULL;
2183 for (n = 0; n <= off->last && !occuppied; ++n) {
2184 if (off[n].type != EF_BAD &&
2185 off[n].troops > 0 &&
2186 (off[n].type != EF_SECTOR || off[n].mob)) {
2188 if (def->type == EF_LAND) {
2189 if (def->lnd_lcp->l_flags & L_SPY) {
2196 pr("1 mil from %s moves %s\n",
2197 prcom(0, off + n), prcom(2, def));
2202 pr("%s left unoccupied\n", prcom(0, def));
2205 "No enemy troops moved %s so you still own it!\n",
2206 pr_com(2, def, def->own));
2209 llp = (struct ulist *)list->q_forw;
2210 llp->unit.land.lnd_x = def->x;
2211 llp->unit.land.lnd_y = def->y;
2212 take_move_in_mob(combat_mode, llp, off, def);
2213 if (def->type == EF_SHIP) {
2214 llp->unit.land.lnd_ship = def->shp_uid;
2215 sprintf(buf, "boards %s", prcom(0, def));
2216 lnd_print(player->cnum, llp, buf);
2219 llp->unit.land.lnd_ship = -1;
2220 sprintf(buf, "moves in to occupy %s",
2221 xyas(def->x, def->y, player->cnum));
2222 lnd_print(player->cnum, llp, buf);
2228 if (def->type == EF_SECTOR) {
2229 getsect(def->x, def->y, §);
2230 takeover(§, player->cnum);
2231 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2232 caploss(§, def->own,
2233 "* We have captured %s's capital, sir! *\n");
2235 } else if (def->type == EF_SHIP) {
2236 getship(def->shp_uid, &ship);
2237 takeover_ship(&ship, player->cnum);
2238 putship(ship.shp_uid, &ship);
2239 } else if (def->type == EF_LAND) {
2240 getland(def->lnd_uid, &land);
2241 takeover_land(&land, player->cnum);
2242 putland(land.lnd_uid, &land);
2245 lnd_delete(delete_me);
2246 att_get_combat(def, 0);
2251 * Ask the attacker which mil & land units they'd like to move into the
2256 ask_move_in(struct combat *off, struct emp_qelem *olist,
2260 struct emp_qelem *qp, *next;
2264 char land_answer[256];
2267 for (n = 0; n <= off->last; ++n)
2268 if (off[n].type != EF_BAD && off[n].troops > 0)
2270 ask_move_in_off(&off[n], def);
2271 if (player->aborted)
2277 memset(land_answer, 0, sizeof(land_answer));
2278 for (qp = olist->q_forw; qp != olist; qp = next) {
2280 llp = (struct ulist *)qp;
2281 answerp = &land_answer[(int)llp->unit.land.lnd_army];
2282 if (player->aborted || att_get_combat(def, 0) < 0)
2284 if (*answerp == 'Y')
2286 if (!get_oland(A_ATTACK, llp))
2288 if (*answerp != 'N') {
2289 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2290 prland(&llp->unit.land),
2291 llp->unit.land.lnd_army ? llp->unit.land.lnd_army : '~',
2292 llp->unit.land.lnd_effic);
2293 *answerp = att_prompt(prompt, llp->unit.land.lnd_army);
2294 if (player->aborted || att_get_combat(def, 0) < 0)
2296 if (!get_oland(A_ATTACK, llp))
2299 if (*answerp == 'y' || *answerp == 'Y')
2301 sprintf(buf, "stays in %s",
2302 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2304 lnd_print(player->cnum, llp, buf);
2309 if (att_get_combat(def, 0) < 0) {
2310 for (qp = olist->q_forw; qp != olist; qp = next) {
2312 llp = (struct ulist *)qp;
2313 if (!get_oland(A_ATTACK, llp))
2315 sprintf(buf, "stays in %s",
2316 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2318 lnd_print(player->cnum, llp, buf);
2323 if (opt_INTERDICT_ATT)
2324 lnd_interdict(olist, def->x, def->y, player->cnum);
2325 move_in_land(A_ATTACK, off, olist, def);
2328 /* Move offensive land units to the conquered sector or ship */
2331 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2334 struct emp_qelem *qp, *next;
2340 for (qp = olist->q_forw; qp != olist; qp = next) {
2342 llp = (struct ulist *)qp;
2343 if (!get_oland(combat_mode, llp))
2345 take_move_in_mob(combat_mode, llp, off, def);
2346 llp->unit.land.lnd_x = def->x;
2347 llp->unit.land.lnd_y = def->y;
2348 if (def->type == EF_SHIP)
2349 llp->unit.land.lnd_ship = def->shp_uid;
2351 llp->unit.land.lnd_ship = -1;
2355 if (def->type == EF_SECTOR) {
2356 if (opt_INTERDICT_ATT) {
2357 lnd_sweep(olist, 0, 0, player->cnum);
2358 lnd_check_mines(olist);
2360 sprintf(buf, "now occupies %s", prcom(0, def));
2362 sprintf(buf, "boards %s", prcom(0, def));
2366 for (qp = olist->q_forw; qp != olist; qp = next) {
2368 llp = (struct ulist *)qp;
2369 lnd_print(player->cnum, llp, buf);
2377 * Move assaulting, paradropping, or boarding mil & units into def
2378 * If the mil are coming from a ship, then pack a lunch.
2382 att_move_in_off(int combat_mode, struct combat *off,
2383 struct emp_qelem *olist, struct combat *def)
2390 move_in_land(combat_mode, off, olist, def);
2392 for (n = 0; n <= off->last; ++n) {
2393 if (off[n].type == EF_BAD || !off[n].troops)
2395 troops = off[n].troops;
2397 off[n].mil -= troops;
2399 put_combat(off + n);
2400 if (combat_mode == A_ASSAULT) {
2401 if (CANT_HAPPEN(off[n].type != EF_SHIP))
2403 getship(off[n].shp_uid, &ship);
2404 lunchbox += (int)((troops + 1) * ship.shp_item[I_FOOD]
2405 / (ship.shp_item[I_MILIT] + troops
2406 + ship.shp_item[I_CIVIL] + 0.5));
2408 ship.shp_item[I_FOOD] -= lunchbox;
2409 putship(ship.shp_uid, &ship);
2415 if (combat_mode == A_ASSAULT) {
2416 if (CANT_HAPPEN(def->type != EF_SECTOR))
2418 getsect(def->x, def->y, §);
2419 if (lunchbox > ITEM_MAX - sect.sct_item[I_FOOD])
2420 lunchbox = ITEM_MAX - sect.sct_item[I_FOOD];
2421 sect.sct_item[I_FOOD] += lunchbox;
2427 /* Ask how many mil to move in from each sector */
2430 ask_move_in_off(struct combat *off, struct combat *def)
2433 int num_mil, dam = 0, left;
2439 if (att_get_combat(off, 0) <= 0)
2441 if (att_get_combat(def, 0) < 0)
2443 if (off->own != player->cnum)
2445 d = att_mobcost(off->own, def, MOB_MOVE);
2446 if ((mob_support = MIN(off->troops, (int)(off->mob / d))) <= 0)
2448 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2449 xyas(off->x, off->y, player->cnum), mob_support);
2450 p = getstring(prompt, buf);
2451 if (!p || !*p || (num_mil = atoi(p)) <= 0)
2453 /* Make sure we don't move in more than we can support mobility-wise */
2454 if (num_mil > mob_support)
2455 num_mil = mob_support;
2456 if (att_get_combat(off, 0) <= 0)
2458 if (att_get_combat(def, 0) < 0)
2460 if ((num_mil = MIN(off->troops, num_mil)) <= 0) {
2461 pr("No mil moved in from %s\n",
2462 xyas(off->x, off->y, player->cnum));
2465 mob_support = MAX(1, (int)(num_mil * d));
2466 off->mob -= MIN(off->mob, mob_support);
2467 off->mil -= num_mil;
2468 off->troops -= num_mil;
2471 weight = (double)num_mil * ichr[I_MILIT].i_lbs;
2472 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2473 if (chance(weight / 100.0))
2475 ground_interdict(def->x, def->y, player->cnum, "military");
2476 dam += check_lmines(def->x, def->y, weight);
2480 left = commdamage(num_mil, dam, I_MILIT);
2481 if (left < num_mil) {
2483 pr("%d of the mil you were moving were destroyed!\n"
2484 "Only %d mil made it to %s\n",
2485 num_mil - left, left,
2486 xyas(def->x, def->y, player->cnum));
2488 pr("All of the mil you were moving were destroyed!\n");
2491 /* maybe got nuked */
2492 if (att_get_combat(def, 0) < 0)
2500 /* Charge land units for moving into a sector or onto a ship */
2503 take_move_in_mob(int combat_mode, struct ulist *llp, struct combat *off,
2506 double mob = llp->unit.land.lnd_mobil;
2507 double gain = etu_per_update * land_mob_scale;
2508 double mobcost, moblim;
2510 switch (combat_mode) {
2512 mobcost = lnd_pathcost(&llp->unit.land,
2513 att_mobcost(llp->unit.land.lnd_own, def,
2514 lnd_mobtype(&llp->unit.land)));
2518 * Set mobcost to basic assault cost, moblim to maximum
2519 * mobility to keep when assaulting from non-landing ship
2521 if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE) {
2522 mobcost = gain / 2.0;
2528 if (!(off->shp_mcp->m_flags & M_LAND))
2529 /* Not a landing ship, ensure we go to or below moblim */
2530 mobcost = MAX(mobcost, mob - moblim);
2533 if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE)
2546 llp->unit.land.lnd_mobil = (signed char)mob;
2547 llp->unit.land.lnd_harden = 0;
2551 free_list(struct emp_qelem *list)
2553 struct emp_qelem *qp, *next;
2555 if (!list || QEMPTY(list))
2559 while (qp != list) {
2568 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2576 * sector_strength - Everyone starts at 1. You can get up to a max
2577 * of d_dstr, depending on how much you build up the
2578 * defenses of the sector.
2582 sector_strength(struct sctstr *sp)
2584 double def = SCT_DEFENSE(sp) / 100.0;
2585 double base = sp->sct_type == SCT_MOUNT ? 2.0 : 1.0;
2586 double d = base + (dchr[sp->sct_type].d_dstr - base) * def;
2588 if (d > dchr[sp->sct_type].d_dstr)
2589 d = dchr[sp->sct_type].d_dstr;