2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * attsub.c: Attack subroutines
30 * Known contributors to this file:
32 * Steve McClure, 1996-2000
33 * Markus Armbruster, 2006-2008
50 #include "prototypes.h"
55 #define CASUALTY_LUMP 1 /* How big casualty chunks should be */
57 static void ask_olist(int combat_mode, struct combat *off,
58 struct combat *def, struct emp_qelem *olist,
59 char *land_answer, int *a_spyp, int *a_engineerp);
60 static void take_move_in_mob(int combat_mode, struct ulist *llp,
61 struct combat *off, struct combat *def);
62 static void move_in_land(int combat_mode, struct combat *off,
63 struct emp_qelem *olist, struct combat *def);
64 static void ask_move_in(struct combat *off, struct emp_qelem *olist,
66 static void ask_move_in_off(struct combat *off, struct combat *def);
68 static int board_abort(struct combat *off, struct combat *def);
69 static int land_board_abort(struct combat *off, struct combat *def);
70 static int ask_off(int combat_mode, struct combat *off,
72 static void get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
74 static int get_ototal(int combat_mode, struct combat *off,
75 struct emp_qelem *olist, double osupport, int check);
76 static int get_dtotal(struct combat *def, struct emp_qelem *list,
77 double dsupport, int check);
78 static double att_calcodds(int, int);
79 static int take_casualty(int combat_mode, struct combat *off,
80 struct emp_qelem *olist);
82 static void send_reacting_units_home(struct emp_qelem *list);
83 static int take_def(int combat_mode, struct emp_qelem *list,
84 struct combat *off, struct combat *def);
86 static int get_land(int combat_mode, struct combat *def, int uid,
87 struct ulist *llp, int victim_land);
90 /* must match combat types in combat.h */
91 "defend", "attack", "assault", "paradrop", "board", "lboard"
96 * The principal object in this code is the "combat" object. A combat object
97 * is either a sector or ship. There are
98 * usually two instances of this, the "def" or defense combat object, and
99 * the array of "off" or offense objects. The number of offense objects is
100 * determined by the value of off->last (e.g. more than one attacking sector).
101 * the type of the object is determined by combat->type which can take the
102 * values EF_SECTOR, EF_SHIP, EF_PLANE, or EF_BAD. Another important parameter
103 * which is often passed to these functions is combat_mode. This can take
104 * the value A_DEFEND, A_ATTACK, A_ASSAULT, A_PARA, A_BOARD and A_LBOARD.
105 * As these six modes of being in combat affect things like mobcost and combat
106 * value, there are often switches made on combat_mode. Note that in all cases
107 * no mobility is taken from sectors, ships, or land units until the player
108 * has committed to a fight. Instead, the cost is temporarily placed in
109 * combat->mobcost, or llp->mobil as the case may be, and then when the object
110 * is "put" back onto disk, then the amounts in these variables are subtracted
111 * from the object's mobility. It needs to be done this way as the objects
112 * are constantly being re-read from disk, and we don't want to take any mob
113 * unless a fight actually occurrs.
117 /* initialize combat object */
120 att_combat_init(struct combat *com, int type)
122 memset(com, 0, sizeof(*com));
127 /* print a combat object with optional preposition */
130 pr_com(int inon, struct combat *com, natid who)
132 if (com->type == EF_SECTOR) {
134 inon ? inon == 1 ? "in " : "into " : "",
135 xyas(com->x, com->y, who));
136 } else if (com->type == EF_SHIP) {
137 return prbuf("%s%s %s(#%d)",
138 inon ? inon == 1 ? "on " : "onto " : "",
139 com->shp_mcp->m_name, com->shp_name,
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 &&
221 ontradingblock(EF_SHIP, &ship)) {
222 pr("%s is on the trading block.\n", prcom(0, com));
223 return att_combat_init(com, EF_BAD);
226 if (isdef && player->owner) {
227 pr("Boarding yourself? Try using the 'tend' command.\n");
228 return att_combat_init(com, EF_BAD);
230 com->shp_mcp = &mchr[(int)ship.shp_type];
231 strncpy(com->shp_name, ship.shp_name, MAXSHPNAMLEN);
232 if (!isdef && !player->owner) {
234 pr("%s was just sunk!\n", prcom(0, com));
236 pr("Ship #%d is not your ship!\n", com->shp_uid);
237 return att_combat_init(com, EF_BAD);
239 mil = ship.shp_item[I_MILIT];
240 pstage = ship.shp_pstage;
241 owner = ship.shp_own;
242 eff = ship.shp_effic;
243 mob = ship.shp_mobil;
252 return att_combat_init(com, EF_BAD);
255 if (!com->set) { /* first time */
256 if (isdef) { /* defender */
258 } else { /* attacker */
260 pr("No mil %s\n", prcom(1, com));
262 pr("Only 1 mil %s\n", prcom(1, com));
263 /* don't abandon attacking sectors or ships */
264 com->troops = MAX(0, mil - 1);
266 com->plague = pstage == PLG_INFECT;
267 } else { /* not first time */
268 if (isdef) { /* defender */
269 if (com->x != x || com->y != y) {
270 pr("%s has moved!\n", prcom(0, com));
271 return att_combat_init(com, EF_BAD);
273 if (owner != com->own) {
275 pr("WARNING: The ownership of %s just changed from %s to %s!\n",
276 prcom(0, com), cname(com->own), cname(owner));
277 } else if (com->type == EF_SECTOR) {
278 pr("WARNING: %s just abandoned sector %s!\n",
280 xyas(com->x, com->y, player->cnum));
284 pr("WARNING: The enemy mil %s just %s from %d to %d!\n",
286 com->mil < mil ? "increased" : "decreased", com->mil,
289 } else { /* attacker */
290 if (owner != player->cnum && getrel(getnatp(owner), player->cnum) != ALLIED) {
291 /* must be EF_SECTOR */
293 pr("WARNING: Your %d mil in %s were destroyed because %s just took the sector!\n",
294 com->mil, xyas(com->x, com->y, player->cnum),
297 pr("You no longer own %s\n",
298 xyas(com->x, com->y, player->cnum));
299 return att_combat_init(com, EF_BAD);
301 if (com->troops && com->troops + 1 > mil) {
302 if (com->own == owner && player->cnum == owner)
304 pr("WARNING: Your mil %s has been reduced from %d to %d!\n",
305 prcom(1, com), com->troops, MAX(0, mil - 1));
306 com->troops = MAX(0, mil - 1);
321 * In the course of the fight, the combat object may have lost mil, eff, or
322 * mobility. This is the place where the data in the object gets flushed to
323 * disk to make it "real".
327 put_combat(struct combat *com)
336 getsect(com->x, com->y, §);
337 sect.sct_type = com->sct_type;
338 deff = sect.sct_effic - com->eff;
340 sect.sct_road -= sect.sct_road * deff / 100.0;
341 sect.sct_rail -= sect.sct_rail * deff / 100.0;
342 sect.sct_defense -= sect.sct_defense * deff / 100.0;
343 if (sect.sct_road <= 0)
345 if (sect.sct_rail <= 0)
347 if (sect.sct_defense <= 0)
348 sect.sct_defense = 0;
350 sect.sct_effic = com->eff;
352 if (opt_MOB_ACCESS) {
353 if ((com->mob - com->mobcost) < -127)
354 sect.sct_mobil = -127;
356 sect.sct_mobil = (short)(com->mob - com->mobcost);
358 if ((com->mob - com->mobcost) < 0)
361 sect.sct_mobil = (short)(com->mob - com->mobcost);
364 makelost(EF_SECTOR, sect.sct_own, 0, sect.sct_x, sect.sct_y);
365 makenotlost(EF_SECTOR, com->own, 0, sect.sct_x, sect.sct_y);
366 sect.sct_own = com->own;
368 if (sect.sct_pstage == PLG_HEALTHY)
369 sect.sct_pstage = PLG_EXPOSED;
371 sect.sct_item[I_MILIT] = com->mil;
373 com->own = sect.sct_own; /* avoid WARNING if sector reverts */
376 getland(com->lnd_uid, &land);
377 land.lnd_effic = com->eff;
379 if (com->mob - com->mobcost < -127)
380 land.lnd_mobil = -127;
382 land.lnd_mobil = (signed char)(com->mob - com->mobcost);
384 makelost(EF_LAND, land.lnd_own, land.lnd_uid,
385 land.lnd_x, land.lnd_y);
386 land.lnd_own = com->own;
387 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid,
388 land.lnd_x, land.lnd_y);
390 if (land.lnd_pstage == PLG_HEALTHY)
391 land.lnd_pstage = PLG_EXPOSED;
393 if (!(com->lnd_lcp->l_flags & L_SPY))
394 land.lnd_item[I_MILIT] = com->mil;
395 lnd_count_units(&land);
396 if (com->own == player->cnum) {
397 land.lnd_mission = 0;
399 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
401 putland(com->lnd_uid, &land);
404 getship(com->shp_uid, &ship);
405 ship.shp_effic = com->eff;
407 if (com->mob - com->mobcost < -127)
408 ship.shp_mobil = -127;
410 ship.shp_mobil = (signed char)(com->mob - com->mobcost);
412 makelost(EF_SHIP, ship.shp_own, ship.shp_uid,
413 ship.shp_x, ship.shp_y);
414 ship.shp_own = com->own;
415 makenotlost(EF_SHIP, ship.shp_own, ship.shp_uid,
416 ship.shp_x, ship.shp_y);
418 if (ship.shp_pstage == PLG_HEALTHY)
419 ship.shp_pstage = PLG_EXPOSED;
421 ship.shp_item[I_MILIT] = com->mil;
423 if (com->own == player->cnum) {
424 ship.shp_mission = 0;
426 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
428 putship(com->shp_uid, &ship);
431 att_get_combat(com, com->own != player->cnum);
434 /* If pre-attack, abort fight. If post-attack, don't move anything in */
439 return player->aborted = 1;
443 * This is the combat_mode based integrity check. It splits among two main
444 * divisions: first time/not first time, and attack/assault/para/board.
448 att_abort(int combat_mode, struct combat *off, struct combat *def)
457 if (att_get_combat(def, 1) < 0)
458 return abort_attack();
460 if (off && combat_mode != A_ATTACK) {
461 if (att_get_combat(off, 0) < 0)
462 return abort_attack();
463 if (off->type == EF_SHIP &&
464 !(off->x == def->x && off->y == def->y) &&
465 (!getsect(off->x, off->y, §) ||
466 sect.sct_type != SCT_WATER)) {
467 pr("%s can not %s from that far inland!\n",
468 prcom(0, off), att_mode[combat_mode]);
469 return abort_attack();
472 switch (combat_mode) {
474 if (!neigh(def->x, def->y, player->cnum) &&
475 !adj_units(def->x, def->y, player->cnum)) {
476 pr("You are not adjacent to %s\n",
477 xyas(def->x, def->y, player->cnum));
478 return abort_attack();
480 if (def->own == player->cnum) {
481 pr("You can't attack your own sector.\n");
482 return abort_attack();
486 if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
487 pr("You'll have to get there first...\n");
488 return abort_attack();
490 if (off && def->sct_type == SCT_MOUNT) {
491 pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
492 return abort_attack();
496 if (def->own == player->cnum) {
497 pr("You can't air-assault your own sector.\n");
498 return abort_attack();
500 if (off && (def->sct_type == SCT_MOUNT ||
501 def->sct_type == SCT_WATER ||
502 def->sct_type == SCT_CAPIT ||
503 def->sct_type == SCT_FORTR ||
504 def->sct_type == SCT_WASTE)) {
505 pr("You can't air-assault a %s sector!\n",
506 def->sct_dcp->d_name);
507 return abort_attack();
511 return board_abort(off, def);
513 return land_board_abort(off, def);
516 if (off && def->sct_dcp->d_mob0 < 0) {
517 pr("You can't %s a %s sector!\n",
518 att_mode[combat_mode], def->sct_dcp->d_name);
519 return abort_attack();
521 if (!off || off->relations_checked)
523 off->relations_checked = 1;
526 setcont(player->cnum, def->own, FOUND_SPY);
527 setcont(def->own, player->cnum, FOUND_SPY);
529 if (opt_SLOW_WAR && def->own != player->cnum) {
530 natp = getnatp(player->cnum);
531 rel = getrel(natp, def->own);
534 sprintf(y_or_n, "Sector is owned by %s, your ally, %s [yn]? ",
535 cname(def->own), att_mode[combat_mode]);
536 if (!confirm(y_or_n))
537 return abort_attack();
540 if ((rel != AT_WAR) && (def->own) &&
541 (sect.sct_oldown != player->cnum)) {
542 pr("You're not at war with them!\n");
543 return abort_attack();
550 * Lots of special things need to be checked for boarding, so I put it in
555 board_abort(struct combat *off, struct combat *def)
557 struct shpstr aship, dship; /* for tech levels */
560 if (att_get_combat(def, 1) < 0)
561 return abort_attack();
566 if (att_get_combat(off, 0) < 0)
567 return abort_attack();
569 if (off->x != def->x || off->y != def->y) {
570 pr("Ship #%d is not in the same sector!\n", def->shp_uid);
571 return abort_attack();
573 if (off->type == EF_SHIP) {
575 pr("%s has no mobility!\n", prcom(0, off));
576 return abort_attack();
578 getship(off->shp_uid, &aship);
579 getship(def->shp_uid, &dship);
580 if (techfact(aship.shp_tech, shp_speed(&aship)) * off->eff
581 <= techfact(dship.shp_tech, shp_speed(&dship)) * def->eff) {
582 pr("Victim ship moves faster than you do!\n");
585 "%s (#%d) %s failed to catch %s\n",
586 cname(aship.shp_own), aship.shp_own,
587 pr_com(0, off, def->own), pr_com(0, def, def->own));
588 return abort_attack();
590 } else if (off->type != EF_SECTOR) {
591 pr("Please tell the deity that you got the 'banana boat' error\n");
592 return abort_attack();
594 if (def->shp_mcp->m_flags & M_SUB) {
595 getsect(def->x, def->y, §);
596 if (sect.sct_type == SCT_WATER) {
597 pr("You can't board a submarine!\n");
598 return abort_attack();
605 * Lots of special things need to be checked for boarding, so I put it in
607 * STM - I copied it for land unit boarding. :)
611 land_board_abort(struct combat *off, struct combat *def)
613 if (att_get_combat(def, 1) < 0)
614 return abort_attack();
619 if (att_get_combat(off, 0) < 0)
620 return abort_attack();
622 if (off->x != def->x || off->y != def->y) {
623 pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
624 return abort_attack();
630 /* If we are boarding, then the defending ship gets a chance to fire back */
632 att_approach(struct combat *off, struct combat *def)
638 pr("Approaching %s...\n", prcom(0, def));
641 "%s is being approached by %s...\n",
642 pr_com(0, def, def->own), pr_com(0, off, def->own));
643 if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
646 pr("They're firing at us sir!\n");
649 "Your fleet at %s does %d damage to %s\n",
650 xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
652 if (off->type == EF_SECTOR) {
653 getsect(off->x, off->y, §);
654 sectdamage(§, dam);
656 pr("Enemy fleet at %s does %d damage to %s\n",
657 xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
658 } else if (off->type == EF_SHIP) {
659 getship(off->shp_uid, &ship);
660 shipdamage(&ship, dam);
661 putship(off->shp_uid, &ship);
662 if (def->own && ship.shp_effic < SHIP_MINEFF) {
663 wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
664 nreport(player->cnum, N_SHP_LOSE, def->own, 1);
667 if (att_get_combat(off, 0) < 0)
668 return abort_attack();
672 /* The attack is valid. Tell the attacker about what they're going to hit */
675 att_show(struct combat *def)
677 /* Note that we tell the player about the treaty BEFORE we tell them
678 about the item. If we didn't, then they gain free information */
679 if (def->type == EF_SECTOR) {
680 if (!trechk(player->cnum, def->own, LANATT))
681 return abort_attack();
682 pr("%s is a %d%% %s %s with approximately %d military.\n",
683 xyas(def->x, def->y, player->cnum),
684 roundintby((int)def->eff, 10),
685 cname(def->own), def->sct_dcp->d_name,
686 roundintby(def->troops, 10));
687 if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
688 writemap(player->cnum);
689 } else if (def->type == EF_SHIP || def->type == EF_LAND) {
690 if (def->type == EF_SHIP) {
691 if (!trechk(player->cnum, def->own, SEAATT))
692 return abort_attack();
694 if (!trechk(player->cnum, def->own, LNDATT))
695 return abort_attack();
697 pr("%s is about %d%% efficient and has approximately %d mil on board.\n", prcom(0, def), roundintby((int)def->eff, 10), roundintby(def->troops, 10));
699 /* Ok, everything is fine */
703 /* Attack and assault ask the user which kind of support they want */
706 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
711 *fortp = *shipp = *landp = *planep = 1;
713 if (player->argp[offset] != NULL) {
714 if ((player->argp[offset + 1] == NULL) ||
715 (player->argp[offset + 2] == NULL) ||
716 (player->argp[offset + 3] == NULL)) {
717 pr("If any support arguments are used, all must be!\n");
722 *landp = *planep = 0;
724 if (!(p = getstarg(player->argp[offset], "Use fort support? ",
728 if ((*p == 'y') || (*p == 'Y'))
731 if (!(p = getstarg(player->argp[offset + 1], "Use ship support? ",
735 if ((*p == 'y') || (*p == 'Y'))
738 if (!(p = getstarg(player->argp[offset + 2], "Use land support? ",
742 if ((*p == 'y') || (*p == 'Y'))
745 if (!(p = getstarg(player->argp[offset + 3], "Use plane support? ",
749 if ((*p == 'y') || (*p == 'Y'))
756 * Attack, assault, and board ask the attacker what they'd like to attack
757 * with. This includes mil and land units from each "off" object. Note that
758 * after each sub-prompt, we check to make sure that the attack is still
759 * valid, and if it's not, then we abort the attack.
763 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
764 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
767 char land_answer[256];
770 if (att_abort(combat_mode, off, def))
772 memset(land_answer, 0, sizeof(land_answer));
773 for (n = 0; n <= off->last; ++n) {
774 off[n].troops = ask_off(combat_mode, off + n, def);
775 if (att_abort(combat_mode, off, def))
777 ask_olist(combat_mode, off + n, def, olist, land_answer,
778 a_spyp, a_engineerp);
779 if (att_abort(combat_mode, off, def))
786 * Return path cost for ATTACKER to enter sector given by DEF.
787 * MOBTYPE is a mobility type accepted by sector_mcost().
790 att_mobcost(natid attacker, struct combat *def, int mobtype)
795 if (CANT_HAPPEN(def->type != EF_SECTOR))
797 ok = getsect(def->x, def->y, §);
798 if (CANT_HAPPEN(!ok))
802 * We want the cost to move/march into the sector. If we just
803 * called sector_mcost(), we'd get the defender's cost. The
804 * attacker's cost is higher unless he's the old-owner. Note: if
805 * there are no civilians, a victorious attacker will become the
806 * old-owner. But he isn't now.
808 sect.sct_own = attacker;
810 return sector_mcost(§, mobtype);
813 /* How many mil is off allowed to attack with when it attacks def? */
816 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
821 switch (combat_mode) {
823 mobcost = att_mobcost(off->own, def, MOB_MOVE);
824 if (mobcost < 0 || off->mob <= 0)
826 mob_support = off->mob / mobcost;
827 if (mob_support < off->troops)
828 pr("Sector %s has %d mobility which can only support %d mil,\n",
829 xyas(off->x, off->y, player->cnum), off->mob, mob_support);
831 mob_support = off->troops;
834 if (def->own != player->cnum && def->mil) {
835 if (off->shp_mcp->m_flags & M_SEMILAND)
836 return off->troops / 4;
837 else if (!(off->shp_mcp->m_flags & M_LAND))
838 return off->troops / 10;
842 if (off->type == EF_SECTOR && off->mob <= 0)
844 mob_support = def->shp_mcp->m_item[I_MILIT];
845 if (mob_support < off->troops)
846 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
848 mob_support = off->troops;
853 if (def->lnd_lcp->l_flags & L_SPY)
855 mob_support = def->lnd_lcp->l_item[I_MILIT];
856 if (mob_support < off->troops)
857 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
859 mob_support = off->troops;
866 * If the attacker decides to go through with the attack, then the
867 * sectors/ships they are attacking with may be charged some mobility.
868 * This is where that amount of mobility is calculated. It is actually
869 * subtracted "for real" from the object's mobility in put_combat().
873 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
880 switch (combat_mode) {
882 off->mobcost += MAX(1,
884 * att_mobcost(off->own, def, MOB_MOVE)));
887 off->mobcost += MAX(1, attacking_mil / 5);
892 off->mobcost += MAX(1, attacking_mil / 5);
895 /* the 2 in the formula below is a fudge factor */
896 getship(def->shp_uid, &ship);
897 off->mobcost += (def->eff / 100) * (shp_speed(&ship) / 2);
902 /* How many mil to we want to attack from off against def? */
905 ask_off(int combat_mode, struct combat *off, struct combat *def)
911 if (att_get_combat(off, 0) <= 0)
913 if ((off->type == EF_SECTOR) && (off->own != player->cnum))
915 if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
917 if (off->type == EF_SECTOR) {
918 if (off->own != player->cnum)
920 sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
921 off->sct_dcp->d_name,
922 xyas(off->x, off->y, player->cnum), mob_support);
924 sprintf(prompt, "Number of mil from %s (max %d) : ",
925 prcom(0, off), mob_support);
927 if ((attacking_mil = onearg(0, prompt)) < 0)
929 if (att_abort(combat_mode, off, def))
931 if (att_get_combat(off, 0) <= 0)
934 MIN(attacking_mil, MIN(mob_support, off->troops))) <= 0)
937 calc_mobcost(combat_mode, off, def, attacking_mil);
938 return attacking_mil;
942 * Which units would you like to attack with or move in with [ynYNq?]
946 att_prompt(char *prompt, char army)
954 p = getstring(prompt, buf);
955 if (player->aborted || (p && *p == 'q')) {
961 if (tolower(*p) == 'y' || tolower(*p) == 'n')
963 pr("y - yes this unit\n"
965 "Y - yes to all units in army '%c'\n"
966 "N - no to all units in army '%c'\n"
967 "q - quit\n? - this help message\n\n",
972 /* Ask the attacker which units they want to attack/assault/board with */
975 ask_olist(int combat_mode, struct combat *off, struct combat *def,
976 struct emp_qelem *olist, char *land_answer, int *a_spyp,
990 if (def->type == EF_LAND)
992 if (def->type == EF_SHIP)
993 maxland = def->shp_mcp->m_nland;
995 snxtitem_xy(&ni, EF_LAND, off->x, off->y);
996 while (nxtitem(&ni, &land)) {
997 if (land.lnd_own != player->cnum)
999 if (land.lnd_effic < LAND_MINEFF)
1001 if (land_answer[(int)land.lnd_army] == 'N')
1003 if (!lnd_can_attack(&land))
1005 lcp = &lchr[(int)land.lnd_type];
1007 if (def->type == EF_SHIP && !maxland) {
1008 pr("Land units are not able to board this kind of ship\n");
1011 if (def->type == EF_SHIP
1012 && (def->shp_mcp->m_flags & (M_SUPPLY | M_SUB)) != M_SUPPLY
1013 && !(lcp->l_flags & L_LIGHT)) {
1014 pr("Only light land units can board this kind of ship\n");
1017 if (land.lnd_mobil <= 0) {
1018 pr("%s is out of mobility, and cannot %s\n",
1019 prland(&land), att_mode[combat_mode]);
1024 if (ontradingblock(EF_LAND, &land)) {
1025 pr("%s is on the trading block, and cannot %s\n",
1026 prland(&land), att_mode[combat_mode]);
1031 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
1032 pr("%s is on ship #%d, and cannot %s\n",
1033 prland(&land), land.lnd_ship, att_mode[combat_mode]);
1035 } else if (off->type == EF_SHIP) {
1036 if (land.lnd_ship != off->shp_uid)
1038 } else if (land.lnd_land >= 0) {
1039 pr("%s is on unit #%d, and cannot %s\n",
1040 prland(&land), land.lnd_land, att_mode[combat_mode]);
1043 switch (combat_mode) {
1046 * We used to let land units attack only if they have the
1047 * mobility consumed by the attack, not counting combat
1048 * and moving in to occupy. Making sure your land units
1049 * reach attack positions with enough mobility left is a
1050 * pain in the neck. We now require positive mobility,
1051 * just like for marching. Except we don't allow rushing
1052 * of high-mobility sectors (mountains): for those we
1053 * still require attack mobility.
1055 mobcost = att_mobcost(off->own, def, lnd_mobtype(&land));
1056 if (mobcost < 1.0) {
1057 if (land.lnd_mobil <= 0) {
1058 pr("%s is out of mobility\n", prland(&land));
1062 mobcost = lnd_pathcost(&land, mobcost);
1063 if (land.lnd_mobil < mobcost) {
1064 pr("%s does not have enough mobility (%d needed)\n",
1065 prland(&land), (int)ceil(mobcost));
1073 if (!(lcp->l_flags & L_ASSAULT))
1080 att_val = attack_val(combat_mode, &land);
1081 if (att_val < 1.0) {
1082 pr("%s has no offensive strength\n", prland(&land));
1085 resupply_all(&land);
1086 putland(land.lnd_uid, &land);
1087 if (!has_supply(&land)) {
1088 pr("%s is out of supply, and cannot %s\n",
1089 prland(&land), att_mode[combat_mode]);
1092 if (def->type == EF_SHIP && first_time) {
1094 pr("You may board with a maximum of %d land units\n", maxland);
1096 pr("%s has a base %s value of %.0f\n",
1097 prland(&land), att_mode[combat_mode], att_val);
1098 if (land_answer[(int)land.lnd_army] != 'Y') {
1100 "%s with %s %s (%c %d%%) [ynYNq?] ",
1101 att_mode[combat_mode],
1104 land.lnd_army ? land.lnd_army : '~',
1106 land_answer[(int)land.lnd_army] =
1107 att_prompt(prompt, land.lnd_army);
1108 if (att_abort(combat_mode, off, def))
1110 if (land_answer[(int)land.lnd_army] != 'y' &&
1111 land_answer[(int)land.lnd_army] != 'Y')
1114 if (!(llp = malloc(sizeof(struct ulist)))) {
1115 logerror("Malloc failed in attack!\n");
1119 memset(llp, 0, sizeof(struct ulist));
1120 emp_insque(&llp->queue, olist);
1121 llp->mobil = mobcost;
1122 if (!get_land(combat_mode, def, land.lnd_uid, llp, 0))
1124 if (lnd_spyval(&land) > *a_spyp)
1125 *a_spyp = lnd_spyval(&land);
1126 if (((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)
1128 if (def->type == EF_SHIP && ++count >= maxland)
1133 /* What's the offense or defense multiplier? */
1136 att_combat_eff(struct combat *com)
1142 if (com->type == EF_SECTOR) {
1143 eff = com->eff / 100.0;
1144 if (com->own == player->cnum) {
1145 str = com->sct_dcp->d_ostr;
1146 eff = 1.0 + (str - 1.0) * eff;
1148 eff = sector_strength(getsectp(com->x, com->y));
1149 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1150 getship(com->shp_uid, &ship);
1151 eff = 1.0 + shp_armor(&ship) / 100.0;
1157 att_get_offense(int combat_mode, struct combat *off,
1158 struct emp_qelem *olist, struct combat *def)
1163 * Get the attacker units & mil again in case they changed while the
1164 * attacker was answering sub-prompts.
1167 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1168 if (att_empty_attack(combat_mode, ototal, def))
1169 return abort_attack();
1170 if (combat_mode == A_PARA)
1172 pr("\n Initial attack strength: %8d\n", ototal);
1176 /* Get the defensive units and reacting units */
1178 att_get_defense(struct emp_qelem *olist, struct combat *def,
1179 struct emp_qelem *dlist, int a_spy, int ototal)
1182 struct emp_qelem *qp;
1188 get_dlist(def, dlist, 0, &d_spy);
1189 dtotal = get_dtotal(def, dlist, 1.0, 0);
1192 * Call in reacting units
1195 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1196 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1198 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1199 llp = (struct ulist *)qp;
1200 intelligence_report(def->own, &llp->unit.land, d_spy,
1201 "Scouts report attacking unit:");
1204 old_dtotal = dtotal;
1205 dtotal = get_dtotal(def, dlist, 1.0, 0);
1206 if (dtotal != old_dtotal)
1207 pr("Defense strength with reacting units: %8d\n", dtotal);
1212 /* Get the defensive land units in the sector or on the ship */
1215 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1218 struct nstr_item ni;
1222 /* In here is where you need to take out spies and trains from the defending
1223 lists. Spies try to hide, trains get trapped and can be boarded. */
1225 snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1226 while (nxtitem(&ni, &land)) {
1229 if (land.lnd_own != def->own)
1231 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1233 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1235 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1237 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1239 intelligence_report(player->cnum, &land, a_spy,
1240 "Scouts report defending unit:");
1241 if (!(llp = malloc(sizeof(struct ulist)))) {
1242 logerror("Malloc failed in attack!\n");
1246 memset(llp, 0, sizeof(struct ulist));
1247 emp_insque(&llp->queue, list);
1248 llp->supplied = has_supply(&land);
1249 if (!get_land(A_DEFEND, def, land.lnd_uid, llp, 1))
1251 if (lnd_spyval(&land) > *d_spyp)
1252 *d_spyp = lnd_spyval(&land);
1256 /* Calculate the total offensive strength */
1259 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1260 double osupport, int check)
1262 double ototal = 0.0;
1263 struct emp_qelem *qp, *next;
1268 * first, total the attacking mil
1271 for (n = 0; n <= off->last; ++n) {
1272 if (off[n].type == EF_BAD || (check &&
1273 att_get_combat(&off[n], 0) <= 0))
1275 ototal += off[n].troops * att_combat_eff(off + n);
1279 * next, add in the attack_values of all
1280 * the attacking units
1283 for (qp = olist->q_forw; qp != olist; qp = next) {
1285 llp = (struct ulist *)qp;
1286 if (check && !get_land(combat_mode, 0, llp->unit.land.lnd_uid, llp, 0))
1288 if (combat_mode == A_ATTACK) {
1290 for (n = 0; n <= off->last; ++n) {
1291 if (off[n].type == EF_BAD)
1293 if ((off[n].x == llp->unit.land.lnd_x) &&
1294 (off[n].y == llp->unit.land.lnd_y))
1298 lnd_delete(llp, "is in a sector not owned by you");
1301 ototal += attack_val(combat_mode, &llp->unit.land) *
1302 att_combat_eff(off + w);
1304 ototal += attack_val(combat_mode, &llp->unit.land);
1309 return ldround(ototal, 1);
1312 /* Calculate the total defensive strength */
1315 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1318 double dtotal = 0.0, eff = 1.0, d_unit;
1319 struct emp_qelem *qp, *next;
1322 if (check && att_get_combat(def, 1) < 0)
1324 eff = att_combat_eff(def);
1325 dtotal = def->troops * eff;
1328 * next, add in the defense_values of all
1329 * the defending non-retreating units
1332 for (qp = list->q_forw; qp != list; qp = next) {
1334 llp = (struct ulist *)qp;
1335 if (check && !get_land(A_DEFEND, def, llp->unit.land.lnd_uid, llp, 1))
1337 d_unit = defense_val(&llp->unit.land);
1340 dtotal += d_unit * eff;
1345 return ldround(dtotal, 1);
1349 * This is the land unit integrity check. Note that we don't print
1350 * warnings about victim land units because the attacker may not have seen them
1354 get_land(int combat_mode, struct combat *def, int uid, struct ulist *llp,
1357 struct lndstr *lp = &llp->unit.land;
1362 if (!llp->chrp) { /* first time */
1363 llp->x = llp->unit.land.lnd_x;
1364 llp->y = llp->unit.land.lnd_y;
1365 llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
1366 } else { /* not first time */
1367 if (lp->lnd_effic < LAND_MINEFF) {
1368 sprintf(buf, "was destroyed and is no longer a part of the %s",
1369 att_mode[combat_mode]);
1370 lnd_delete(llp, buf);
1374 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1376 "left to go fight another battle and is no longer a part of the defense");
1380 if (lp->lnd_own != player->cnum) {
1382 "was destroyed and is no longer a part of the %s",
1383 att_mode[combat_mode]);
1384 lnd_delete(llp, buf);
1387 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1389 "left to fight another battle and is no longer a part of the %s",
1390 att_mode[combat_mode]);
1391 lnd_delete(llp, buf);
1394 if (lp->lnd_effic < llp->eff) {
1395 sprintf(buf, "damaged from %d%% to %d%%",
1396 llp->eff, lp->lnd_effic);
1397 lnd_print(llp, buf);
1401 llp->eff = llp->unit.land.lnd_effic;
1407 * Put the land unit on the disk. If there was some mobility cost, then
1408 * subtract it from the units mobility. Note that this works the same way
1409 * as sectors & ships in that no mobility is actually taken until the attacker
1410 * has committed to attacking.
1414 kill_land(struct emp_qelem *list)
1416 struct emp_qelem *qp, *next;
1419 for (qp = list->q_forw; qp != list; qp = next) {
1421 llp = (struct ulist *)qp;
1422 if (llp->unit.land.lnd_ship >= 0) {
1423 llp->unit.land.lnd_effic = 0;
1424 lnd_delete(llp, "cannot return to the ship, and dies!");
1430 att_infect_units(struct emp_qelem *list, int plague)
1432 struct emp_qelem *qp, *next;
1437 for (qp = list->q_forw; qp != list; qp = next) {
1439 llp = (struct ulist *)qp;
1440 if (llp->unit.land.lnd_pstage == PLG_HEALTHY)
1441 llp->unit.land.lnd_pstage = PLG_EXPOSED;
1446 put_land(struct emp_qelem *list)
1448 struct emp_qelem *qp, *next;
1451 for (qp = list->q_forw; qp != list; qp = next) {
1453 llp = (struct ulist *)qp;
1454 llp->unit.land.lnd_mission = 0;
1455 llp->unit.land.lnd_harden = 0;
1456 llp->unit.land.lnd_mobil -= (int)llp->mobil;
1458 putland(llp->unit.land.lnd_uid, &llp->unit.land);
1459 if (llp->unit.land.lnd_own != player->cnum) {
1460 emp_remque((struct emp_qelem *)llp);
1463 get_land(A_ATTACK, 0, llp->unit.land.lnd_uid, llp, 0);
1468 * Keep sending in reinforcements until it looks like we're going to win.
1469 * Note that the "strength" command also calls this routine.
1473 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1474 int *d_spyp, int ototal)
1476 struct nstr_item ni;
1478 struct sctstr sect, dsect;
1481 double new_land = 0;
1487 double eff = att_combat_eff(def);
1491 dtotal = get_dtotal(def, list, 1.0, 1);
1494 snxtitem_all(&ni, EF_LAND);
1495 while (nxtitem(&ni, &land) && dtotal + new_land * eff < 1.2 * ototal) {
1498 if (!land.lnd_rad_max)
1500 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1502 if (land.lnd_own != def->own)
1504 if (land.lnd_ship >= 0)
1506 if (land.lnd_land >= 0)
1508 if (defense_val(&land) < 1.0)
1510 if (!lnd_can_attack(&land))
1513 /* Only supplied units can react */
1514 if (!has_supply(&land))
1517 dist = mapdist(land.lnd_x, land.lnd_y, def->x, def->y);
1519 getsect(land.lnd_x, land.lnd_y, §);
1520 /* Units on efficient headquarters can react 1 farther */
1521 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
1522 radius = land.lnd_rad_max + 1;
1524 radius = land.lnd_rad_max;
1526 if (land.lnd_mission == MI_RESERVE)
1532 getsect(def->x, def->y, &dsect);
1533 if (!BestLandPath(buf, §, &dsect, &pathcost,
1534 lnd_mobtype(&land)))
1537 mobcost = lnd_pathcost(&land, pathcost);
1538 if (land.lnd_mobil < mobcost)
1541 new_land += defense_val(&land);
1543 if (!list) /* we are in the "strength" command */
1546 /* move to defending sector */
1547 land.lnd_mobil -= ldround(mobcost, 1);
1550 land.lnd_x = def->x;
1551 land.lnd_y = def->y;
1552 putland(land.lnd_uid, &land);
1553 wu(0, land.lnd_own, "%s reacts to %s.\n",
1554 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1556 llp = malloc(sizeof(struct ulist));
1558 memset(llp, 0, sizeof(struct ulist));
1562 llp->chrp = (struct empobj_chr *)&lchr[(int)land.lnd_type];
1563 llp->unit.land = land;
1564 emp_insque(&llp->queue, list);
1565 if (lnd_spyval(&land) > *d_spyp)
1566 *d_spyp = lnd_spyval(&land);
1568 intelligence_report(player->cnum, &land, a_spy,
1569 "Scouts sight reacting enemy unit:");
1574 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1577 get_osupport(char *outs, struct combat *def, int fort_sup, int ship_sup,
1578 int land_sup, int plane_sup)
1580 double osupport = 1.0;
1582 double af, as, au, ap;
1584 af = as = au = ap = 0.0;
1586 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1591 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1598 dam = lnd_support(def->own, player->cnum, def->x, def->y, 0);
1604 dam = off_support(def->x, def->y, def->own, player->cnum);
1608 sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1613 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1616 get_dsupport(char *outs, struct emp_qelem *list, struct combat *def,
1617 int ototal, int dtotal)
1619 double dsupport = 1.0;
1621 double df, ds, du, dp;
1624 df = ds = du = dp = 0.0;
1625 if (dtotal < 0.1 * ototal) {
1627 } else if (dtotal >= 1.2 * ototal) {
1630 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1634 dtotal = get_dtotal(def, list, dsupport, 0);
1635 if (dtotal < 1.2 * ototal) {
1636 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1639 dtotal = get_dtotal(def, list, dsupport, 0);
1641 if (dtotal < 1.2 * ototal) {
1642 dam = lnd_support(player->cnum, def->own, def->x, def->y, 1);
1645 dtotal = get_dtotal(def, list, dsupport, 1);
1647 if (dtotal < 1.2 * ototal) {
1648 dam = def_support(def->x, def->y, player->cnum, def->own);
1656 sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1661 "\nOdds are bad for us...support cancelled.\n\n");
1664 "\nOdds are good for us...support cancelled.\n\n");
1670 * Land mines add to the defense multiplier. If the attacker has engineers
1671 * then this multiplier is cut in half.
1675 get_mine_dsupport(struct combat *def, int a_engineer)
1680 getsect(def->x, def->y, §);
1682 if (sect.sct_oldown != player->cnum) {
1683 mines = MIN(sect.sct_mines, 20);
1685 mines = ldround(mines / 2.0, 1);
1688 wu(0, def->own, "Defending mines add %1.2f\n",
1690 pr("Defending mines add %1.2f\n", mines * 0.02);
1691 return mines * 0.02;
1697 /* Get the offensive and defensive support */
1699 att_get_support(int combat_mode, int ofort, int oship, int oland,
1700 int oplane, struct emp_qelem *olist, struct combat *off,
1701 struct emp_qelem *dlist, struct combat *def,
1702 double *osupportp, double *dsupportp, int a_engineer)
1705 char osupports[512];
1706 char dsupports[512];
1708 if (combat_mode == A_PARA)
1711 *osupportp = get_osupport(osupports, def,
1712 ofort, oship, oland, oplane);
1715 * I need to put a 1 at the end of the next four total_stren calls
1716 * because units & mil may have been damaged by collateral damage or
1717 * nuclear warheads from the offensive & defensive support.
1720 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1721 if (att_empty_attack(combat_mode, ototal, def))
1722 return abort_attack();
1723 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1726 * Calculate defensive support. If odds are too good or too bad
1727 * then don't call in support.
1730 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1731 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1732 if (att_empty_attack(combat_mode, ototal, def))
1733 return abort_attack();
1735 if ((*osupports || *dsupports) &&
1736 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1737 pr("\n\t\tsupport values\n");
1738 pr("\t\tforts\tships\tunits\tplanes\n");
1739 if (*osupportp != 1.0)
1740 pr("%s", osupports);
1741 if (*dsupportp != 1.0)
1742 pr("%s", dsupports);
1744 wu(0, def->own, "\n\t\tsupport values\n");
1745 wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1746 if (*osupportp != 1.0)
1747 wu(0, def->own, "%s", osupports);
1748 if (*dsupportp != 1.0)
1749 wu(0, def->own, "%s", dsupports);
1753 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1754 if (dtotal && def->type == EF_SECTOR)
1755 *dsupportp += get_mine_dsupport(def, a_engineer);
1759 /* How many two-legged bipeds are in this combat force? */
1762 count_bodies(struct combat *off, struct emp_qelem *list)
1766 struct emp_qelem *qp;
1769 for (n = 0; n <= off->last; ++n)
1770 bodies += off[n].troops;
1771 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1772 llp = (struct ulist *)qp;
1773 bodies += llp->unit.land.lnd_item[I_MILIT];
1778 /* This is where the fighting actually occurs. */
1781 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1782 double osupport, struct combat *def, struct emp_qelem *dlist,
1786 int a_cas = 0; /* Casualty counts */
1788 int ototal; /* total attacking strength */
1789 int dtotal; /* total defending strength */
1790 int a_bodies; /* total attacking mil (incl. mil in units) */
1791 int d_bodies; /* total defending mil (incl. mil in units) */
1801 ototal = get_ototal(combat_mode, off, olist, osupport,
1802 combat_mode != A_PARA);
1803 dtotal = get_dtotal(def, dlist, dsupport, 0);
1807 a_bodies = count_bodies(off, olist);
1808 d_bodies = count_bodies(def, dlist);
1809 d_mil = def->troops;
1810 for (n = 0; n <= off->last; ++n)
1811 if (off[n].type == EF_BAD)
1814 a_troops[n] = off[n].troops;
1816 /* This switch is required to get the spacing right */
1817 switch (combat_mode) {
1819 pr(" Final attack strength: %8d\n", ototal);
1822 pr(" Final assault strength: %8d\n", ototal);
1825 if (def->sct_type == SCT_MOUNT ||
1826 def->sct_type == SCT_WATER ||
1827 def->sct_type == SCT_CAPIT ||
1828 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1829 pr("You can't air-assault a %s sector!\n",
1830 def->sct_dcp->d_name);
1833 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1835 pr(" Final air-assault strength: %8d\n", ototal);
1839 pr(" Final board strength: %8d\n", ototal);
1843 pr(" Final defense strength: %8d\n", dtotal);
1844 odds = att_calcodds(ototal, dtotal);
1845 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1847 /* spread the plague */
1848 if (combat_mode != A_PARA) {
1850 for (n = 0; n <= off->last; ++n)
1851 if (off[n].type != EF_BAD)
1852 def->plague |= off[n].plague;
1853 for (n = 0; n <= off->last; ++n)
1854 if (off[n].type != EF_BAD)
1855 off[n].plague |= def->plague;
1857 att_infect_units(olist, off->plague);
1858 att_infect_units(dlist, def->plague);
1861 * Fighting is slightly random. There is always that last little
1862 * effort you see people put in. Or the stray bullet that takes out
1863 * an officer and the rest go into chaos. Things like that.
1864 * Thus, we have added a very slight random factor that will sometimes
1865 * allow the little guy to win. We modify the odds a little
1866 * (either +- 5%) to account for this randomness. We also only
1867 * recalculate the odds every 8-50 casualties, not every cacsualty,
1868 * since a single dead guy normally wouldn't cause a commander to
1869 * rethink his strategies, but 50 dead guys might.
1871 odds += (random() % 11 - 5) / 100.0;
1876 recalctime = 8 + (random() % 43);
1877 while (!success && ototal) {
1880 d_cas += take_casualty(A_DEFEND, def, dlist);
1881 dtotal = get_dtotal(def, dlist, dsupport, 0);
1886 a_cas += take_casualty(combat_mode, off, olist);
1887 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1889 if (((a_cas + d_cas) % 70) == 69)
1891 if (recalctime-- <= 0) {
1892 recalctime = 8 + (random() % 43);
1893 odds = att_calcodds(ototal, dtotal);
1894 odds += (random() % 11 - 5) / 100.0;
1902 /* update defense mobility & mil */
1906 if (def->type == EF_SECTOR && d_mil && d_cas) {
1910 newmob = damage(def->mob, 100 * d_cas / d_mil);
1911 def->mobcost = MIN(20, def->mob - newmob);
1914 def->mil = def->troops;
1917 /* update attack mobility & mil */
1918 for (n = 0; n <= off->last; ++n)
1919 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1920 if (off[n].type == EF_SECTOR && off[n].mil) {
1921 if (!CANT_HAPPEN(off[n].mob < 0)) {
1922 newmob = damage(off[n].mob,
1923 100 * (a_troops[n] - off[n].troops)
1925 off[n].mobcost += MIN(20, off[n].mob - newmob);
1928 off[n].mil -= a_troops[n] - off[n].troops;
1931 /* update land unit mobility */
1932 if (d_bodies && d_cas)
1933 lnd_takemob(dlist, (double)d_cas / d_bodies);
1934 if (a_bodies && a_cas)
1935 lnd_takemob(olist, (double)a_cas / a_bodies);
1937 /* damage attacked sector */
1938 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1940 pr("- Casualties -\n Yours: %d\n", a_cas);
1941 pr(" Theirs: %d\n", d_cas);
1942 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
1943 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
1946 switch (combat_mode) {
1948 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
1949 pr("We have captured %s, sir!\n", prcom(0, def));
1953 news_item = def->own ? N_AWON_SECT : N_START_COL;
1954 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
1955 action = "assaulting and taking";
1958 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
1959 pr("We have captured %s, sir!\n", prcom(0, def));
1960 action = "air-assaulting and taking";
1963 news_item = N_BOARD_SHIP;
1964 pr("We have boarded %s, sir!\n", prcom(0, def));
1965 action = "boarding";
1968 news_item = N_BOARD_LAND;
1969 pr("We have boarded %s, sir!\n", prcom(0, def));
1970 action = "boarding";
1975 action = "defeating";
1978 switch (combat_mode) {
1980 news_item = N_SCT_LOSE;
1981 pr("You have been defeated!\n");
1982 action = "attacking";
1985 news_item = N_ALOSE_SCT;
1986 pr("You have been defeated!\n");
1988 action = "trying to assault";
1991 news_item = N_PLOSE_SCT;
1992 pr("All of your troops were destroyed\n");
1993 action = "trying to air-assault";
1996 news_item = N_SHP_LOSE;
1997 pr("You have been repelled\n");
1999 action = "trying to board";
2002 news_item = N_LND_LOSE;
2003 pr("You have been repelled\n");
2005 action = "trying to board";
2010 action = "fighting";
2013 nreport(player->cnum, news_item, def->own, 1);
2016 "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
2017 cname(player->cnum), player->cnum, a_cas,
2018 action, pr_com(0, def, def->own), d_cas);
2021 send_reacting_units_home(dlist);
2023 /* putland the defending land */
2026 /* putland the attacking land */
2029 /* put the victim sector/ship/land */
2030 if (!success || !take_def(combat_mode, olist, off, def))
2033 /* put the attacking sectors/ship */
2034 for (n = 0; n <= off->last; ++n)
2035 if (off[n].type != EF_BAD)
2036 put_combat(&off[n]);
2041 switch (combat_mode) {
2043 ask_move_in(off, olist, def);
2045 /* put sectors again to get abandon warnings */
2046 for (n = 0; n <= off->last; ++n)
2047 if (off[n].type != EF_BAD)
2048 put_combat(&off[n]);
2051 att_move_in_off(combat_mode, off, olist, def);
2054 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2058 /* What percentage of the combat forces going head-to-head are we? */
2061 att_calcodds(int ototal, int dtotal)
2065 /* calculate odds */
2068 else if (dtotal <= 0)
2071 odds = (double)ototal / (dtotal + ototal);
2076 /* Here's where the dead soldiers get dragged off the battlefield */
2079 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2081 int to_take = CASUALTY_LUMP;
2082 int biggest_troops = 0, index = -1;
2083 int n, tot_troops = 0, biggest_mil, cas;
2084 struct emp_qelem *qp, *biggest;
2087 for (n = 0; n <= off->last; ++n) {
2088 if (off[n].type != EF_BAD) {
2089 tot_troops += off[n].troops;
2090 if (off[n].troops > biggest_troops) {
2091 biggest_troops = off[n].troops;
2098 to_take -= tot_troops;
2101 for (n = 0; n <= off->last; ++n)
2102 if (off[n].type != EF_BAD)
2106 * They can all come off mil. We rotate the casualties,
2107 * starting with the sector containing the most mil.
2109 to_take = CASUALTY_LUMP;
2111 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2114 while (to_take > 0) {
2115 for (n = index; n <= off->last && to_take; ++n) {
2116 if (off[n].type != EF_BAD && off[n].troops > 0) {
2121 for (n = 0; n < index && to_take; ++n) {
2122 if (off[n].type != EF_BAD && off[n].troops > 0) {
2128 return CASUALTY_LUMP;
2132 return CASUALTY_LUMP - to_take;
2135 * Need to take some casualties from attacking units
2136 * Procedure: find the biggest unit remaining (in
2137 * terms of mil) and give it the casualties.
2141 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2142 llp = (struct ulist *)qp;
2144 if (llp->unit.land.lnd_item[I_MILIT] > biggest_mil) {
2145 biggest_mil = llp->unit.land.lnd_item[I_MILIT];
2149 if (biggest == NULL)
2150 return CASUALTY_LUMP - to_take;
2152 llp = (struct ulist *)biggest;
2153 cas = lnd_take_casualty(combat_mode, llp, to_take);
2154 return CASUALTY_LUMP - (to_take - cas);
2157 /* Send reacting defense units back to where they came from (at no mob cost) */
2160 send_reacting_units_home(struct emp_qelem *list)
2162 struct emp_qelem *qp, *next;
2166 for (qp = list->q_forw; qp != list; qp = next) {
2168 llp = (struct ulist *)qp;
2169 if ((llp->unit.land.lnd_x != llp->x) ||
2170 (llp->unit.land.lnd_y != llp->y)) {
2171 sprintf(buf, "returns to %s",
2172 xyas(llp->x, llp->y, llp->unit.land.lnd_own));
2173 llp->unit.land.lnd_x = llp->x;
2174 llp->unit.land.lnd_y = llp->y;
2175 lnd_delete(llp, buf);
2180 /* Check for 0 offense strength. This call will always preceed an abort */
2183 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2186 if (def->own && player->cnum != def->own) {
2188 "%s (#%d) considered %sing you @%s\n",
2189 cname(player->cnum), player->cnum,
2190 att_mode[combat_mode], xyas(def->x, def->y, def->own));
2192 pr("No troops for %s...\n", att_mode[combat_mode]);
2199 * Take the defending sector or ship from the defender and give it to the
2204 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2209 struct ulist *llp, *delete_me = 0;
2215 for (n = 0; n <= off->last && !occuppied; ++n) {
2216 if (off[n].type != EF_BAD &&
2217 off[n].troops > 0 &&
2218 (off[n].type != EF_SECTOR || off[n].mob)) {
2220 if (def->type == EF_LAND) {
2221 if (def->lnd_lcp->l_flags & L_SPY) {
2228 pr("1 mil from %s moves %s\n",
2229 prcom(0, off + n), prcom(2, def));
2234 pr("%s left unoccupied\n", prcom(0, def));
2237 "No enemy troops moved %s so you still own it!\n",
2238 pr_com(2, def, def->own));
2241 llp = (struct ulist *)list->q_forw;
2242 llp->unit.land.lnd_x = def->x;
2243 llp->unit.land.lnd_y = def->y;
2244 take_move_in_mob(combat_mode, llp, off, def);
2245 if (def->type == EF_SHIP) {
2246 llp->unit.land.lnd_ship = def->shp_uid;
2247 sprintf(buf, "boards %s", prcom(0, def));
2250 llp->unit.land.lnd_ship = -1;
2251 sprintf(buf, "moves in to occupy %s",
2252 xyas(def->x, def->y, player->cnum));
2253 lnd_delete(llp, buf);
2258 if (def->type == EF_SECTOR) {
2259 getsect(def->x, def->y, §);
2260 takeover(§, player->cnum);
2261 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2262 caploss(§, def->own,
2263 "* We have captured %s's capital, sir! *\n");
2265 } else if (def->type == EF_SHIP) {
2266 getship(def->shp_uid, &ship);
2267 takeover_ship(&ship, player->cnum, 1);
2268 putship(ship.shp_uid, &ship);
2269 } else if (def->type == EF_LAND) {
2270 getland(def->lnd_uid, &land);
2271 takeover_land(&land, player->cnum, 1);
2272 putland(land.lnd_uid, &land);
2275 lnd_delete(delete_me, buf);
2276 att_get_combat(def, 0);
2281 * Ask the attacker which mil & land units they'd like to move into the
2286 ask_move_in(struct combat *off, struct emp_qelem *olist,
2290 struct emp_qelem *qp, *next;
2294 char land_answer[256];
2297 for (n = 0; n <= off->last; ++n)
2298 if (off[n].type != EF_BAD && off[n].troops > 0)
2300 ask_move_in_off(&off[n], def);
2301 if (player->aborted)
2307 memset(land_answer, 0, sizeof(land_answer));
2308 for (qp = olist->q_forw; qp != olist; qp = next) {
2310 llp = (struct ulist *)qp;
2311 answerp = &land_answer[(int)llp->unit.land.lnd_army];
2312 if (player->aborted || att_get_combat(def, 0) < 0)
2314 if (*answerp == 'Y')
2316 if (*answerp != 'N') {
2317 if (!get_land(A_ATTACK, def, llp->unit.land.lnd_uid, llp, 0))
2319 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2320 prland(&llp->unit.land),
2321 llp->unit.land.lnd_army ? llp->unit.land.lnd_army : '~',
2322 llp->unit.land.lnd_effic);
2323 *answerp = att_prompt(prompt, llp->unit.land.lnd_army);
2324 if (player->aborted || att_get_combat(def, 0) < 0)
2326 if (!get_land(A_ATTACK, def, llp->unit.land.lnd_uid, llp, 0))
2329 if (*answerp == 'y' || *answerp == 'Y')
2331 sprintf(buf, "stays in %s",
2332 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2334 lnd_delete(llp, buf);
2338 if (att_get_combat(def, 0) < 0) {
2339 for (qp = olist->q_forw; qp != olist; qp = next) {
2341 llp = (struct ulist *)qp;
2342 if (!get_land(A_ATTACK, def, llp->unit.land.lnd_uid, llp, 0))
2344 sprintf(buf, "stays in %s",
2345 xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
2347 lnd_delete(llp, buf);
2351 if (opt_INTERDICT_ATT)
2352 lnd_interdict(olist, def->x, def->y, player->cnum);
2353 move_in_land(A_ATTACK, off, olist, def);
2356 /* Move offensive land units to the conquered sector or ship */
2359 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2362 struct emp_qelem *qp, *next;
2368 for (qp = olist->q_forw; qp != olist; qp = next) {
2370 llp = (struct ulist *)qp;
2371 if (!get_land(combat_mode, def, llp->unit.land.lnd_uid, llp, 0))
2373 take_move_in_mob(combat_mode, llp, off, def);
2374 llp->unit.land.lnd_x = def->x;
2375 llp->unit.land.lnd_y = def->y;
2376 if (def->type == EF_SHIP)
2377 llp->unit.land.lnd_ship = def->shp_uid;
2379 llp->unit.land.lnd_ship = -1;
2383 if (def->type == EF_SECTOR) {
2384 if (opt_INTERDICT_ATT) {
2385 lnd_sweep(olist, 0, 0, def->own);
2386 lnd_check_mines(olist);
2388 sprintf(buf, "now occupies %s", prcom(0, def));
2390 sprintf(buf, "boards %s", prcom(0, def));
2394 for (qp = olist->q_forw; qp != olist; qp = next) {
2396 llp = (struct ulist *)qp;
2397 lnd_print(llp, buf);
2405 * Move assaulting, paradropping, or boarding mil & units into def
2406 * If the mil are coming from a ship, then pack a lunch.
2410 att_move_in_off(int combat_mode, struct combat *off,
2411 struct emp_qelem *olist, struct combat *def)
2418 move_in_land(combat_mode, off, olist, def);
2420 for (n = 0; n <= off->last; ++n) {
2421 if (off[n].type == EF_BAD || !off[n].troops)
2423 troops = off[n].troops;
2425 off[n].mil -= troops;
2427 put_combat(off + n);
2428 if (combat_mode == A_ASSAULT) {
2429 if (CANT_HAPPEN(off[n].type != EF_SHIP))
2431 getship(off[n].shp_uid, &ship);
2432 lunchbox += (int)((troops + 1) * ship.shp_item[I_FOOD]
2433 / (ship.shp_item[I_MILIT] + troops
2434 + ship.shp_item[I_CIVIL] + 0.5));
2436 ship.shp_item[I_FOOD] -= lunchbox;
2437 putship(ship.shp_uid, &ship);
2443 if (combat_mode == A_ASSAULT) {
2444 if (CANT_HAPPEN(def->type != EF_SECTOR))
2446 getsect(def->x, def->y, §);
2447 if (lunchbox > ITEM_MAX - sect.sct_item[I_FOOD])
2448 lunchbox = ITEM_MAX - sect.sct_item[I_FOOD];
2449 sect.sct_item[I_FOOD] += lunchbox;
2455 /* Ask how many mil to move in from each sector */
2458 ask_move_in_off(struct combat *off, struct combat *def)
2461 int num_mil, dam = 0, left;
2467 if (att_get_combat(off, 0) <= 0)
2469 if (att_get_combat(def, 0) < 0)
2471 if (off->own != player->cnum)
2473 d = att_mobcost(off->own, def, MOB_MOVE);
2474 if ((mob_support = MIN(off->troops, (int)(off->mob / d))) <= 0)
2476 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2477 xyas(off->x, off->y, player->cnum), mob_support);
2478 if (!(p = getstring(prompt, buf)) || !*p || (num_mil = atoi(p)) <= 0)
2480 /* Make sure we don't move in more than we can support mobility-wise */
2481 if (num_mil > mob_support)
2482 num_mil = mob_support;
2483 if (att_get_combat(off, 0) <= 0)
2485 if (att_get_combat(def, 0) < 0)
2487 if ((num_mil = MIN(off->troops, num_mil)) <= 0) {
2488 pr("No mil moved in from %s\n",
2489 xyas(off->x, off->y, player->cnum));
2492 mob_support = MAX(1, (int)(num_mil * d));
2493 off->mob -= MIN(off->mob, mob_support);
2494 off->mil -= num_mil;
2495 off->troops -= num_mil;
2498 weight = (double)num_mil * ichr[I_MILIT].i_lbs;
2499 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2500 if (chance(weight / 100.0))
2502 ground_interdict(def->x, def->y, player->cnum, "military");
2503 dam += check_lmines(def->x, def->y, weight);
2507 left = commdamage(num_mil, dam, I_MILIT);
2508 if (left < num_mil) {
2510 pr("%d of the mil you were moving were destroyed!\nOnly %d mil made it to %s\n", num_mil - left, left, xyas(def->x, def->y, player->cnum));
2512 pr("All of the mil you were moving were destroyed!\n");
2515 /* maybe got nuked */
2516 if (att_get_combat(def, 0) < 0)
2524 /* Charge land units for moving into a sector or onto a ship */
2527 take_move_in_mob(int combat_mode, struct ulist *llp, struct combat *off,
2530 double mob = llp->unit.land.lnd_mobil;
2531 double gain = etu_per_update * land_mob_scale;
2532 double mobcost, moblim;
2534 switch (combat_mode) {
2536 mobcost = lnd_pathcost(&llp->unit.land,
2537 att_mobcost(off->own, def,
2538 lnd_mobtype(&llp->unit.land)));
2542 * Set mobcost to basic assault cost, moblim to maximum
2543 * mobility to keep when assaulting from non-landing ship
2545 if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE) {
2546 mobcost = gain / 2.0;
2552 if (!(off->shp_mcp->m_flags & M_LAND))
2553 /* Not a landing ship, ensure we go to or below moblim */
2554 mobcost = MAX(mobcost, mob - moblim);
2557 if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE)
2570 llp->unit.land.lnd_mobil = (signed char)mob;
2571 llp->unit.land.lnd_harden = 0;
2575 free_list(struct emp_qelem *list)
2577 struct emp_qelem *qp, *next;
2579 if (!list || QEMPTY(list))
2583 while (qp != list) {
2592 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2600 * sector_strength - Everyone starts at 1. You can get up to a max
2601 * of d_dstr, depending on how much you build up the
2602 * defenses of the sector.
2606 sector_strength(struct sctstr *sp)
2608 double def = SCT_DEFENSE(sp) / 100.0;
2609 double base = sp->sct_type == SCT_MOUNT ? 2.0 : 1.0;
2610 double d = base + (dchr[sp->sct_type].d_dstr - base) * def;
2612 if (d > dchr[sp->sct_type].d_dstr)
2613 d = dchr[sp->sct_type].d_dstr;