2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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
55 #include "prototypes.h"
57 #define CASUALTY_LUMP 1 /* How big casualty chunks should be */
59 static void ask_olist(int combat_mode, struct combat *off,
60 struct combat *def, struct emp_qelem *olist,
61 s_char *land_answer, int *a_spyp, int *a_engineerp);
62 static void take_move_in_mob(int combat_mode, struct llist *llp,
63 struct combat *off, struct combat *def);
64 static void move_in_land(int combat_mode, struct combat *off,
65 struct emp_qelem *olist, struct combat *def);
66 static void ask_move_in(struct combat *off, struct emp_qelem *olist,
68 static void ask_move_in_off(struct combat *off, struct combat *def);
70 static int board_abort(struct combat *off, struct combat *def);
71 static int land_board_abort(struct combat *off, struct combat *def);
72 static int ask_off(int combat_mode, struct combat *off,
74 static int get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
76 static int get_ototal(int combat_mode, struct combat *off,
77 struct emp_qelem *olist, double osupport, int check);
78 static int get_dtotal(struct combat *def, struct emp_qelem *list,
79 double dsupport, int check);
80 static int take_casualty(int combat_mode, struct combat *off,
81 struct emp_qelem *olist);
83 static void send_reacting_units_home(struct emp_qelem *list);
84 static int take_def(int combat_mode, struct emp_qelem *list,
85 struct combat *off, struct combat *def);
87 static int get_land(int combat_mode, struct combat *def, int uid,
88 struct llist *llp, int victim_land);
91 /* must match combat types in combat.h */
92 "defend", "attack", "assault", "paradrop", "board", "lboard"
97 * The principal object in this code is the "combat" object. A combat object
98 * is either a sector or ship. There are
99 * usually two instances of this, the "def" or defense combat object, and
100 * the array of "off" or offense objects. The number of offense objects is
101 * determined by the value of off->last (e.g. more than one attacking sector).
102 * the type of the object is determined by combat->type which can take the
103 * values EF_SECTOR, EF_SHIP, EF_PLANE, or EF_BAD. Another important parameter
104 * which is often passed to these functions is combat_mode. This can take
105 * the value A_DEFENSE, A_ATTACK, A_ASSAULT, A_PARA, A_BOARD and A_LBOARD.
106 * As these six modes of being in combat affect things like mobcost and combat
107 * value, there are often switches made on combat_mode. Note that in all cases
108 * no mobility is taken from sectors, ships, or land units until the player
109 * has committed to a fight. Instead, the cost is temporarily placed in
110 * combat->mobcost, or llp->mobil as the case may be, and then when the object
111 * is "put" back onto disk, then the amounts in these variables are subtracted
112 * from the object's mobility. It needs to be done this way as the objects
113 * are constantly being re-read from disk, and we don't want to take any mob
114 * unless a fight actually occurrs.
118 /* initialize combat object */
121 att_combat_init(struct combat *com, int type)
123 memset(com, 0, sizeof(*com));
128 /* print a combat object with optional preposition */
131 pr_com(int inon, struct combat *com, natid who)
133 if (com->type == EF_SECTOR) {
135 inon ? inon == 1 ? "in " : "into " : "",
136 xyas(com->x, com->y, who));
137 } else if (com->type == EF_SHIP) {
138 return prbuf("%s%s %s(#%d)",
139 inon ? inon == 1 ? "on " : "onto " : "",
140 com->shp_mcp->m_name, com->shp_name,
142 } else if (com->type == EF_LAND) {
143 return prbuf("%s%s #%d",
144 inon ? inon == 1 ? "on " : "onto " : "",
145 com->lnd_lcp->l_name, com->lnd_uid);
147 return "your forces";
152 prcom(int inon, struct combat *com)
154 return pr_com(inon, com, player->cnum);
158 * This is the combat object "type" based integrity check. It basically
159 * splits along three divisions: ship/sector, attacker/defender,
160 * first time/not first time.
164 att_get_combat(struct combat *com, int isdef)
178 if (!getsect(com->x, com->y, §)) {
179 pr("Bad sector: %s\n", xyas(com->x, com->y, player->cnum));
180 return att_combat_init(com, EF_BAD);
182 com->sct_type = sect.sct_type;
183 com->sct_dcp = &dchr[sect.sct_type];
184 mil = sect.sct_item[I_MILIT];
185 pstage = sect.sct_pstage;
186 owner = sect.sct_own;
187 eff = sect.sct_effic;
188 mob = sect.sct_mobil;
193 if (!getland(com->lnd_uid, &land) || !land.lnd_own) {
195 pr("Land unit #%d is not in the same sector!\n",
197 return att_combat_init(com, EF_BAD);
199 if (isdef && player->owner) {
200 pr("Boarding yourself? Try using the 'load' command.\n");
201 return att_combat_init(com, EF_BAD);
203 com->lnd_lcp = &lchr[(int)land.lnd_type];
204 mil = land.lnd_item[I_MILIT];
205 pstage = land.lnd_pstage;
206 owner = land.lnd_own;
207 eff = land.lnd_effic;
208 mob = land.lnd_mobil;
213 if (!getship(com->shp_uid, &ship) || !ship.shp_own) {
215 pr("Ship #%d is not in the same sector!\n", com->shp_uid);
217 pr("Ship #%d is not your ship!\n", com->shp_uid);
218 return att_combat_init(com, EF_BAD);
221 if (isdef && player->owner &&
222 ontradingblock(EF_SHIP, (int *)&ship)) {
223 pr("%s is on the trading block.\n", prcom(0, com));
224 return att_combat_init(com, EF_BAD);
227 if (isdef && player->owner) {
228 pr("Boarding yourself? Try using the 'tend' command.\n");
229 return att_combat_init(com, EF_BAD);
231 com->shp_mcp = &mchr[(int)ship.shp_type];
232 strncpy(com->shp_name, ship.shp_name, MAXSHPNAMLEN);
233 if (!isdef && !player->owner) {
235 pr("%s was just sunk!\n", prcom(0, com));
237 pr("Ship #%d is not your ship!\n", com->shp_uid);
238 return att_combat_init(com, EF_BAD);
240 mil = ship.shp_item[I_MILIT];
241 pstage = ship.shp_pstage;
242 owner = ship.shp_own;
243 eff = ship.shp_effic;
244 mob = ship.shp_mobil;
253 return att_combat_init(com, EF_BAD);
256 if (!com->set) { /* first time */
257 if (isdef) { /* defender */
259 } else { /* attacker */
261 pr("No mil %s\n", prcom(1, com));
263 pr("Only 1 mil %s\n", prcom(1, com));
264 /* don't abandon attacking sectors or ships */
265 com->troops = MAX(0, mil - 1);
267 com->plague = pstage == PLG_INFECT;
268 } else { /* not first time */
269 if (isdef) { /* defender */
270 if (com->x != x || com->y != y) {
271 pr("%s has moved!\n", prcom(0, com));
272 return att_combat_init(com, EF_BAD);
274 if (owner != com->own) {
276 pr("WARNING: The ownership of %s just changed from %s to %s!\n", prcom(0, com), cname(com->own), cname(owner));
277 } else if (com->type == EF_SECTOR) {
278 pr("WARNING: %s just abandoned sector %s!\n",
279 cname(com->own), xyas(com->x, com->y,
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) { /* must be EF_SECTOR */
292 pr("WARNING: Your %d mil in %s were destroyed because %s just took the sector!\n", com->mil, xyas(com->x, com->y, player->cnum), cname(owner));
294 pr("You no longer own %s\n",
295 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) /* not a takeover */
300 pr("WARNING: Your mil %s has been reduced from %d to %d!\n", prcom(1, com), com->troops, MAX(0, mil - 1));
301 com->troops = MAX(0, mil - 1);
316 * In the course of the fight, the combat object may have lost mil, eff, or
317 * mobility. This is the place where the data in the object gets flushed to
318 * disk to make it "real".
322 put_combat(struct combat *com)
331 getsect(com->x, com->y, §);
332 sect.sct_type = com->sct_type;
333 deff = sect.sct_effic - com->eff;
335 sect.sct_road -= (sect.sct_road * deff / 100.0);
336 sect.sct_rail -= (sect.sct_rail * deff / 100.0);
337 sect.sct_defense -= (sect.sct_defense * deff / 100.0);
338 if (sect.sct_road <= 0)
340 if (sect.sct_rail <= 0)
342 if (sect.sct_defense <= 0)
343 sect.sct_defense = 0;
345 sect.sct_effic = com->eff;
346 if (!opt_DEFENSE_INFRA)
347 sect.sct_defense = sect.sct_effic;
349 if (opt_MOB_ACCESS) {
350 if ((com->mob - com->mobcost) < -127)
351 sect.sct_mobil = -127;
353 sect.sct_mobil = (short)(com->mob - com->mobcost);
355 if ((com->mob - com->mobcost) < 0)
358 sect.sct_mobil = (short)(com->mob - com->mobcost);
361 makelost(EF_SECTOR, sect.sct_own, 0, sect.sct_x, sect.sct_y);
362 makenotlost(EF_SECTOR, com->own, 0, sect.sct_x, sect.sct_y);
363 sect.sct_own = com->own;
365 if (sect.sct_pstage == PLG_HEALTHY)
366 sect.sct_pstage = PLG_EXPOSED;
368 sect.sct_item[I_MILIT] = com->mil;
370 com->own = sect.sct_own; /* avoid WARNING if sector reverts */
373 getland(com->lnd_uid, &land);
374 land.lnd_effic = com->eff;
376 if (com->mob - com->mobcost < -127)
377 land.lnd_mobil = -127;
379 land.lnd_mobil = (s_char)(com->mob - com->mobcost);
381 makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
383 land.lnd_own = com->own;
384 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
387 if (land.lnd_pstage == PLG_HEALTHY)
388 land.lnd_pstage = PLG_EXPOSED;
390 if (!(com->lnd_lcp->l_flags & L_SPY))
391 land.lnd_item[I_MILIT] = com->mil;
392 lnd_count_units(&land);
393 if (com->own == player->cnum) {
394 land.lnd_mission = 0;
396 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
398 putland(com->lnd_uid, &land);
401 getship(com->shp_uid, &ship);
402 ship.shp_effic = com->eff;
404 if (com->mob - com->mobcost < -127)
405 ship.shp_mobil = -127;
407 ship.shp_mobil = (s_char)(com->mob - com->mobcost);
409 makelost(EF_SHIP, ship.shp_own, ship.shp_uid, ship.shp_x,
411 ship.shp_own = com->own;
412 makenotlost(EF_SHIP, ship.shp_own, ship.shp_uid, ship.shp_x,
415 if (ship.shp_pstage == PLG_HEALTHY)
416 ship.shp_pstage = PLG_EXPOSED;
418 ship.shp_item[I_MILIT] = com->mil;
420 if (com->own == player->cnum) {
421 ship.shp_mission = 0;
423 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
425 putship(com->shp_uid, &ship);
428 att_get_combat(com, com->own != player->cnum);
431 /* If pre-attack, abort fight. If post-attack, don't move anything in */
436 return player->aborted = 1;
440 * This is the combat_mode based integrity check. It splits among two main
441 * divisions: first time/not first time, and attack/assault/para/board.
445 att_abort(int combat_mode, struct combat *off, struct combat *def)
454 if (att_get_combat(def, 1) < 0)
455 return abort_attack();
457 if (off && combat_mode != A_ATTACK) {
458 if (att_get_combat(off, 0) < 0)
459 return abort_attack();
460 if (off->type == EF_SHIP &&
461 (!getsect(off->x, off->y, §) ||
462 sect.sct_type != SCT_WATER)) {
463 pr("%s can not %s from that far inland!\n",
464 prcom(0, off), att_mode[combat_mode]);
465 return abort_attack();
468 switch (combat_mode) {
470 if (!neigh(def->x, def->y, player->cnum) &&
471 !adj_units(def->x, def->y, player->cnum)) {
472 pr("You are not adjacent to %s\n",
473 xyas(def->x, def->y, player->cnum));
474 return abort_attack();
476 if (def->own == player->cnum) {
477 pr("You can't attack your own sector.\n");
478 return abort_attack();
482 if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
483 pr("You'll have to get there first...\n");
484 return abort_attack();
486 if (off && def->sct_type == SCT_MOUNT) {
487 pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
488 return abort_attack();
492 if (def->own == player->cnum) {
493 pr("You can't air-assault your own sector.\n");
494 return abort_attack();
496 if (off && (def->sct_type == SCT_MOUNT ||
497 def->sct_type == SCT_WATER ||
498 def->sct_type == SCT_CAPIT ||
499 def->sct_type == SCT_FORTR ||
500 def->sct_type == SCT_WASTE)) {
501 pr("You can't air-assault a %s sector!\n",
502 def->sct_dcp->d_name);
503 return abort_attack();
507 return board_abort(off, def);
509 return land_board_abort(off, def);
512 if (off && def->sct_dcp->d_mcst <= 0) {
513 pr("You can't %s a %s sector!\n", att_mode[combat_mode],
514 def->sct_dcp->d_name);
515 return abort_attack();
517 if (!off || off->relations_checked)
519 off->relations_checked = 1;
522 setcont(player->cnum, def->own, FOUND_SPY);
523 setcont(def->own, player->cnum, FOUND_SPY);
525 if (opt_SLOW_WAR && def->own != player->cnum) {
526 natp = getnatp(player->cnum);
527 rel = getrel(natp, def->own);
530 sprintf(y_or_n, "Sector is owned by %s, your ally, %s [yn]? ",
531 cname(def->own), att_mode[combat_mode]);
532 if (!confirm(y_or_n))
533 return abort_attack();
536 if ((rel != AT_WAR) && (def->own) &&
537 (sect.sct_oldown != player->cnum)) {
538 pr("You're not at war with them!\n");
539 return abort_attack();
546 * Lots of special things need to be checked for boarding, so I put it in
551 board_abort(struct combat *off, struct combat *def)
553 struct shpstr aship, dship; /* for tech levels */
556 if (att_get_combat(def, 1) < 0)
557 return abort_attack();
562 if (att_get_combat(off, 0) < 0)
563 return abort_attack();
565 if (off->x != def->x || off->y != def->y) {
566 pr("Ship #%d is not in the same sector!\n", def->shp_uid);
567 return abort_attack();
569 if (off->type == EF_SHIP) {
571 pr("%s has no mobility!\n", prcom(0, off));
572 return abort_attack();
574 getship(off->shp_uid, &aship);
575 getship(def->shp_uid, &dship);
576 if (techfact(aship.shp_tech, 1.0) * aship.shp_speed * off->eff
577 <= techfact(dship.shp_tech, 1.0) * dship.shp_speed * def->eff) {
578 pr("Victim ship moves faster than you do!\n");
581 "%s (#%d) %s failed to catch %s\n",
582 cname(aship.shp_own), aship.shp_own,
583 pr_com(0, off, def->own), pr_com(0, def, def->own));
584 return abort_attack();
586 } else if (off->type != EF_SECTOR) {
587 pr("Please tell the deity that you got the 'banana boat' error\n");
588 return abort_attack();
590 if (def->shp_mcp->m_flags & M_SUB) {
591 getsect(def->x, def->y, §);
592 if (sect.sct_type == SCT_WATER) {
593 pr("You can't board a submarine!\n");
594 return abort_attack();
601 * Lots of special things need to be checked for boarding, so I put it in
603 * STM - I copied it for land unit boarding. :)
607 land_board_abort(struct combat *off, struct combat *def)
609 if (att_get_combat(def, 1) < 0)
610 return abort_attack();
615 if (att_get_combat(off, 0) < 0)
616 return abort_attack();
618 if (off->x != def->x || off->y != def->y) {
619 pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
620 return abort_attack();
626 /* If we are boarding, then the defending ship gets a chance to fire back */
628 att_approach(struct combat *off, struct combat *def)
634 pr("Approaching %s...\n", prcom(0, def));
637 "%s is being approached by %s...\n",
638 pr_com(0, def, def->own), pr_com(0, off, def->own));
639 if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
642 pr("They're firing at us sir!\n");
645 "Your fleet at %s does %d damage to %s\n",
646 xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
648 if (off->type == EF_SECTOR) {
649 getsect(off->x, off->y, §);
650 sectdamage(§, dam, 0);
652 pr("Enemy fleet at %s does %d damage to %s\n",
653 xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
654 } else if (off->type == EF_SHIP) {
655 getship(off->shp_uid, &ship);
656 shipdamage(&ship, dam);
657 putship(off->shp_uid, &ship);
658 if (def->own && ship.shp_effic < SHIP_MINEFF) {
659 wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
660 nreport(player->cnum, N_SHP_LOSE, def->own, 1);
663 if (att_get_combat(off, 0) < 0)
664 return abort_attack();
668 /* The attack is valid. Tell the attacker about what they're going to hit */
671 att_show(struct combat *def)
673 /* Note that we tell the player about the treaty BEFORE we tell them
674 about the item. If we didn't, then they gain free information */
675 if (def->type == EF_SECTOR) {
676 if (!trechk(player->cnum, def->own, LANATT))
677 return abort_attack();
678 pr("%s is a %d%% %s %s with approximately %d military.\n",
679 xyas(def->x, def->y, player->cnum),
680 roundintby((int)def->eff, 10),
681 cname(def->own), def->sct_dcp->d_name,
682 roundintby(def->troops, 10));
683 if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
684 writemap(player->cnum);
685 } else if (def->type == EF_SHIP || def->type == EF_LAND) {
686 if (def->type == EF_SHIP) {
687 if (!trechk(player->cnum, def->own, SEAATT))
688 return abort_attack();
690 if (!trechk(player->cnum, def->own, LNDATT))
691 return abort_attack();
693 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));
695 /* Ok, everything is fine */
699 /* Attack and assault ask the user which kind of support they want */
702 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
707 *fortp = *shipp = *landp = *planep = 1;
709 if (player->argp[offset] != NULL) {
710 if ((player->argp[offset + 1] == NULL) ||
711 (player->argp[offset + 2] == NULL) ||
712 (player->argp[offset + 3] == NULL)) {
713 pr("If any support arguments are used, all must be!\n");
718 *landp = *planep = 0;
720 if (!(p = getstarg(player->argp[offset], "Use fort support? ",
724 if ((*p == 'y') || (*p == 'Y'))
727 if (!(p = getstarg(player->argp[offset + 1], "Use ship support? ",
731 if ((*p == 'y') || (*p == 'Y'))
734 if (!(p = getstarg(player->argp[offset + 2], "Use land support? ",
738 if ((*p == 'y') || (*p == 'Y'))
741 if (!(p = getstarg(player->argp[offset + 3], "Use plane support? ",
745 if ((*p == 'y') || (*p == 'Y'))
752 * Attack, assault, and board ask the attacker what they'd like to attack
753 * with. This includes mil and land units from each "off" object. Note that
754 * after each sub-prompt, we check to make sure that the attack is still
755 * valid, and if it's not, then we abort the attack.
759 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
760 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
763 s_char land_answer[1024];
766 if (att_abort(combat_mode, off, def))
768 memset(land_answer, 0, sizeof(land_answer));
769 for (n = 0; n <= off->last; ++n) {
770 off[n].troops = ask_off(combat_mode, off + n, def);
771 if (att_abort(combat_mode, off, def))
773 ask_olist(combat_mode, off + n, def, olist, land_answer,
774 a_spyp, a_engineerp);
775 if (att_abort(combat_mode, off, def))
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)
788 switch (combat_mode) {
790 mob_support = off->mob / sector_mcost(getsectp(def->x, def->y),
794 /* mob_support = off->mob / sector_mcost(def->sct_type, def->eff);*/
795 if (mob_support < off->troops)
796 pr("Sector %s has %d mobility which can only support %d mil,\n", xyas(off->x, off->y, player->cnum), off->mob, mob_support);
798 mob_support = off->troops;
801 if (def->own != player->cnum && def->mil) {
802 if (off->shp_mcp->m_flags & M_SEMILAND)
803 return off->troops / 4;
804 else if (!(off->shp_mcp->m_flags & M_LAND))
805 return off->troops / 10;
809 if (off->type == EF_SECTOR && off->mob <= 0)
811 mob_support = def->shp_mcp->m_item[I_MILIT];
812 if (mob_support < off->troops)
813 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
815 mob_support = off->troops;
820 if (def->lnd_lcp->l_flags & L_SPY)
822 mob_support = def->lnd_lcp->l_item[I_MILIT];
823 if (mob_support < off->troops)
824 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
826 mob_support = off->troops;
833 * If the attacker decides to go through with the attack, then the
834 * sectors/ships they are attacking with may be charged some mobility.
835 * This is where that amount of mobility is calculated. It is actually
836 * subtracted "for real" from the object's mobility in put_combat().
840 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
847 switch (combat_mode) {
851 (int)(attacking_mil *
852 sector_mcost(getsectp(def->x, def->y), MOB_ROAD)));
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) * (ship.shp_speed / 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(0, 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(s_char *prompt, s_char army)
922 while (!p || (*p != 'y' && *p != 'n' && *p != 'Y' && *p != 'N')) {
923 if (p && *p == 'q') {
930 pr("y - yes this unit\nn - no this unit\nY - yes to all units in army '%c'\nN - no to all units in army '%c'\nq - quit\n? - this help message\n\n", army, army);
931 p = getstring(prompt, buf);
932 if (player->aborted) {
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, s_char *land_answer, int *a_spyp,
958 if (def->type == EF_LAND)
960 if (def->type == EF_SHIP)
961 maxland = def->shp_mcp->m_nland;
963 snxtitem_xy(&ni, EF_LAND, off->x, off->y);
964 while (nxtitem(&ni, &land)) {
965 if (land.lnd_own != player->cnum)
967 if (land.lnd_effic < LAND_MINEFF)
969 if (land_answer[(int)land.lnd_army] == 'N')
971 if (!lnd_can_attack(&land))
973 lcp = &lchr[(int)land.lnd_type];
975 if (def->type == EF_SHIP && !maxland) {
976 pr("Land units are not able to board this kind of ship\n");
979 if (land.lnd_mobil <= 0) {
980 pr("%s is out of mobility, and cannot %s\n",
981 prland(&land), att_mode[combat_mode]);
986 if (ontradingblock(EF_LAND, (int *)&land)) {
987 pr("%s is on the trading block, and cannot %s\n",
988 prland(&land), att_mode[combat_mode]);
993 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
994 pr("%s is on ship #%d, and cannot %s\n",
995 prland(&land), land.lnd_ship, att_mode[combat_mode]);
997 } else if (off->type == EF_SHIP) {
998 if (land.lnd_ship != off->shp_uid)
1000 } else if (land.lnd_land >= 0) {
1001 pr("%s is on unit #%d, and cannot %s\n",
1002 prland(&land), land.lnd_land, att_mode[combat_mode]);
1005 switch (combat_mode) {
1008 lnd_mobcost(&land, getsectp(def->x, def->y), MOB_NONE);
1009 if (land.lnd_mobil < mobcost) {
1010 pr("%s does not have enough mobility (%d needed)\n",
1011 prland(&land), (int)ceil(mobcost));
1018 if (!(lcp->l_flags & L_ASSAULT))
1021 att_val = attack_val(combat_mode, &land);
1023 pr("%s has no offensive strength\n", prland(&land));
1026 resupply_all(&land);
1027 putland(land.lnd_uid, &land);
1028 if (!has_supply(&land)) {
1029 pr("%s is out of supply, and cannot %s\n",
1030 prland(&land), att_mode[combat_mode]);
1033 if (def->type == EF_SHIP && first_time) {
1035 pr("You may board with a maximum of %d land units\n", maxland);
1037 pr("%s has a base %s value of %d\n",
1038 prland(&land), att_mode[combat_mode], att_val);
1039 if (land_answer[(int)land.lnd_army] != 'Y') {
1041 "%s with %s %s (%c %d%%) [ynYNq?] ",
1042 att_mode[combat_mode],
1045 land.lnd_army == ' ' ? '~' : land.lnd_army,
1047 land_answer[(int)land.lnd_army] =
1048 att_prompt(prompt, land.lnd_army);
1049 if (att_abort(combat_mode, off, def))
1051 if (land_answer[(int)land.lnd_army] != 'y' &&
1052 land_answer[(int)land.lnd_army] != 'Y')
1055 if (!(llp = malloc(sizeof(struct llist)))) {
1056 logerror("Malloc failed in attack!\n");
1060 memset(llp, 0, sizeof(struct llist));
1061 emp_insque(&llp->queue, olist);
1062 llp->mobil = mobcost;
1063 if (!get_land(combat_mode, def, land.lnd_uid, llp, 0))
1065 if (lnd_spyval(&land) > *a_spyp)
1066 *a_spyp = lnd_spyval(&land);
1067 if (llp->lcp->l_flags & L_ENGINEER)
1069 if (def->type == EF_SHIP && ++count >= maxland)
1074 /* What's the offense or defense multiplier? */
1077 att_combat_eff(struct combat *com)
1083 if (com->type == EF_SECTOR) {
1084 eff = com->eff / 100.0;
1085 if (com->own == player->cnum) {
1086 str = com->sct_dcp->d_ostr;
1087 eff = 1.0 + ((str - 1.0) * eff);
1089 eff = sector_strength(getsectp(com->x, com->y));
1090 /* str = com->sct_dcp->d_dstr;*/
1091 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1092 getship(com->shp_uid, &ship);
1093 eff = (1.0 + ship.shp_armor / 100.0);
1099 * Estimate the defense strength and give the attacker a chance to abort
1100 * if the odds are less than 50%
1104 att_estimate_defense(int combat_mode, struct combat *off,
1105 struct emp_qelem *olist, struct combat *def,
1113 * Get the attacker units & mil again in case they changed while the
1114 * attacker was answering sub-prompts.
1117 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1118 if (att_empty_attack(combat_mode, ototal, def))
1119 return abort_attack();
1120 if (combat_mode == A_PARA)
1122 pr("\n Initial attack strength: %8d\n", ototal);
1124 estimate = att_combat_eff(def) * roundintby(def->troops, 10);
1125 estimate += att_combat_eff(def) * get_dlist(def, 0, a_spy, 0);
1128 * Calculate the initial (pre-support) attack odds. If they're less
1129 * than 50%, ask for a confirmation.
1132 odds = (int)(att_calcodds(ototal, estimate) * 100);
1136 pr(" Estimated defense strength: %8d\n", estimate);
1137 pr(" Estimated odds: %8d%%\n\n",odds);
1138 sprintf(prompt, "Are you sure you want to %s [yn]? ",
1139 att_mode[combat_mode]);
1140 if (!confirm(prompt))
1141 return abort_attack();
1142 ototal = get_ototal(combat_mode, off, olist,1.0,1);
1143 if (att_empty_attack(combat_mode, ototal, def))
1144 return abort_attack();
1150 /* Get the defensive units and reacting units */
1152 att_get_defense(struct emp_qelem *olist, struct combat *def,
1153 struct emp_qelem *dlist, int a_spy, int ototal)
1156 struct emp_qelem *qp;
1162 get_dlist(def, dlist, 0, &d_spy);
1163 dtotal = get_dtotal(def, dlist, 1.0, 0);
1166 * Call in reacting units
1169 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1170 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1172 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1173 llp = (struct llist *)qp;
1174 intelligence_report(def->own, &llp->land, d_spy,
1175 "Scouts report attacking unit:");
1178 old_dtotal = dtotal;
1179 dtotal = get_dtotal(def, dlist, 1.0, 0);
1180 if (dtotal != old_dtotal)
1181 pr("Defense strength with reacting units: %8d\n", dtotal);
1186 /* Get the defensive land units in the sector or on the ship */
1189 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1192 struct nstr_item ni;
1197 /* In here is where you need to take out spies and trains from the defending
1198 lists. Spies try to hide, trains get trapped and can be boarded. */
1200 snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1201 while (nxtitem(&ni, &land)) {
1204 if (land.lnd_own != def->own)
1206 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1208 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1210 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1212 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1214 if (!list) { /* Just estimating the enemy strength */
1215 estimate += intelligence_report(player->cnum,
1217 "Scouts report defending unit:");
1220 if (!(llp = malloc(sizeof(struct llist)))) {
1221 logerror("Malloc failed in attack!\n");
1225 memset(llp, 0, sizeof(struct llist));
1226 emp_insque(&llp->queue, list);
1227 llp->supplied = has_supply(&land);
1228 if (!get_land(A_DEFEND, def, land.lnd_uid, llp, 1))
1230 if (lnd_spyval(&land) > *d_spyp)
1231 *d_spyp = lnd_spyval(&land);
1236 /* Calculate the total offensive strength */
1239 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1240 double osupport, int check)
1242 double ototal = 0.0;
1243 struct emp_qelem *qp, *next;
1248 * first, total the attacking mil
1251 for (n = 0; n <= off->last; ++n) {
1252 if (off[n].type == EF_BAD || (check &&
1253 att_get_combat(&off[n], 0) <= 0))
1255 ototal += off[n].troops * att_combat_eff(off + n);
1259 * next, add in the attack_values of all
1260 * the attacking units
1263 for (qp = olist->q_forw; qp != olist; qp = next) {
1265 llp = (struct llist *)qp;
1266 if (check && !get_land(combat_mode, 0, llp->land.lnd_uid, llp, 0))
1268 if (combat_mode == A_ATTACK) {
1270 for (n = 0; n <= off->last; ++n) {
1271 if (off[n].type == EF_BAD)
1273 if ((off[n].x == llp->land.lnd_x) &&
1274 (off[n].y == llp->land.lnd_y))
1278 lnd_delete(llp, "is in a sector not owned by you");
1281 ototal += attack_val(combat_mode, &llp->land) *
1282 att_combat_eff(off + w);
1284 ototal += attack_val(combat_mode, &llp->land);
1289 return ldround(ototal, 1);
1292 /* Calculate the total defensive strength */
1295 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1298 double dtotal = 0.0, eff = 1.0, d_unit;
1299 struct emp_qelem *qp, *next;
1302 if (check && att_get_combat(def, 1) < 0)
1304 eff = att_combat_eff(def);
1305 dtotal = def->troops * eff;
1308 * next, add in the defense_values of all
1309 * the defending non-retreating units
1312 for (qp = list->q_forw; qp != list; qp = next) {
1314 llp = (struct llist *)qp;
1316 if (check && !get_land(A_DEFEND, def, llp->land.lnd_uid, llp, 1))
1318 d_unit = defense_val(&llp->land);
1320 d_unit = ((double)defense_val(&llp->land) / 2.0);
1321 dtotal += d_unit * eff;
1326 return ldround(dtotal, 1);
1330 * This is the land unit integrity check. Note that we don't print
1331 * warnings about victim land units because the attacker may not have seen them
1335 get_land(int combat_mode, struct combat *def, int uid, struct llist *llp,
1338 struct lndstr *lp = &llp->land;
1343 if (!llp->lcp) { /* first time */
1344 llp->x = llp->land.lnd_x;
1345 llp->y = llp->land.lnd_y;
1346 llp->lcp = &lchr[(int)llp->land.lnd_type];
1347 } else { /* not first time */
1348 if (lp->lnd_effic < LAND_MINEFF) {
1349 sprintf(buf, "was destroyed and is no longer a part of the %s",
1350 att_mode[combat_mode]);
1351 lnd_delete(llp, buf);
1355 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1357 "left to go fight another battle and is no longer a part of the defense");
1361 if (lp->lnd_own != player->cnum) {
1363 "was destroyed and is no longer a part of the %s",
1364 att_mode[combat_mode]);
1365 lnd_delete(llp, buf);
1368 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1370 "left to fight another battle and is no longer a part of the %s",
1371 att_mode[combat_mode]);
1372 lnd_delete(llp, buf);
1375 if (lp->lnd_effic < llp->eff) {
1376 sprintf(buf, "damaged from %d%% to %d%%",
1377 llp->eff, lp->lnd_effic);
1378 lnd_print(llp, buf);
1382 llp->eff = llp->land.lnd_effic;
1388 * Put the land unit on the disk. If there was some mobility cost, then
1389 * subtract it from the units mobility. Note that this works the same way
1390 * as sectors & ships in that no mobility is actually taken until the attacker
1391 * has committed to attacking.
1395 kill_land(struct emp_qelem *list)
1397 struct emp_qelem *qp, *next;
1400 for (qp = list->q_forw; qp != list; qp = next) {
1402 llp = (struct llist *)qp;
1403 if (llp->land.lnd_ship >= 0) {
1404 llp->land.lnd_effic = 0;
1405 lnd_delete(llp, "cannot return to the ship, and dies!");
1411 att_infect_units(struct emp_qelem *list, int plague)
1413 struct emp_qelem *qp, *next;
1418 for (qp = list->q_forw; qp != list; qp = next) {
1420 llp = (struct llist *)qp;
1421 if (llp->land.lnd_pstage == PLG_HEALTHY)
1422 llp->land.lnd_pstage = PLG_EXPOSED;
1427 put_land(struct emp_qelem *list)
1429 struct emp_qelem *qp, *next;
1432 for (qp = list->q_forw; qp != list; qp = next) {
1434 llp = (struct llist *)qp;
1435 llp->land.lnd_mission = 0;
1436 llp->land.lnd_harden = 0;
1437 llp->land.lnd_mobil -= (int)llp->mobil;
1439 putland(llp->land.lnd_uid, &llp->land);
1440 if (llp->land.lnd_own != player->cnum) {
1441 emp_remque((struct emp_qelem *)llp);
1444 get_land(A_ATTACK, 0, llp->land.lnd_uid, llp, 0);
1449 * Keep sending in reinforcements until it looks like we're going to win.
1450 * Note that the "strength" command also calls this routine.
1454 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1455 int *d_spyp, int ototal)
1457 struct nstr_item ni;
1459 struct sctstr sect, dsect;
1469 double eff = att_combat_eff(def);
1475 * All units that are within their reaction radius and not damaged
1476 * below their morale value now get to react to the threatened sect.
1477 * Once we've sent enough to counter the threat, stop sending them.
1479 * Not anymore. All units get to react. :)
1483 dtotal = get_dtotal(def, list, 1.0, 1);
1486 snxtitem_all(&ni, EF_LAND);
1487 while (nxtitem(&ni, &land) &&
1488 (dtotal + new_land * eff < (int)(1.2 * (float)ototal))) {
1491 if (!land.lnd_rad_max)
1493 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1495 if (land.lnd_own != def->own)
1497 if (land.lnd_ship >= 0)
1499 if (!defense_val(&land))
1502 if (land.lnd_effic <= land.lnd_retreat)
1505 if (!lnd_can_attack(&land))
1508 /* Only supplied units can react */
1509 if (!(supply_state = has_supply(&land)))
1512 dist = mapdist(land.lnd_x, land.lnd_y, def->x, def->y);
1514 getsect(land.lnd_x, land.lnd_y, §);
1515 /* Units on efficient headquarters can react 1 farther */
1516 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
1517 radius = land.lnd_rad_max + 1;
1519 radius = land.lnd_rad_max;
1521 if (land.lnd_mission == MI_RESERVE)
1527 getsect(def->x, def->y, &dsect);
1528 if (!(p = BestLandPath(buf, §, &dsect, &move_cost, MOB_ROAD)))
1531 mobcost = land.lnd_effic * 0.01 * lchr[(int)land.lnd_type].l_spd;
1534 mobcost = 480.0 / (mobcost + techfact(land.lnd_tech, mobcost));
1535 mobcost *= (move_cost * 5.0);
1537 if (land.lnd_mobil < mobcost)
1540 new_land += defense_val(&land);
1542 if (!list) /* we are in the "strength" command */
1545 /* move to defending sector */
1546 land.lnd_mobil -= ldround(mobcost, 1);
1549 land.lnd_x = def->x;
1550 land.lnd_y = def->y;
1551 putland(land.lnd_uid, &land);
1552 wu(0, land.lnd_own, "%s reacts to %s.\n",
1553 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1555 llp = (struct llist *)
1556 malloc(sizeof(struct llist));
1558 memset(llp, 0, sizeof(struct llist));
1559 llp->supplied = supply_state;
1562 llp->lcp = &lchr[(int)land.lnd_type];
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(s_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);
1587 af = ((double)dam / 100.0);
1591 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1593 as = ((double)dam / 100.0);
1598 dam = lnd_support(def->own, player->cnum, def->x, def->y, 0);
1599 au = ((double)dam / 100.0);
1604 dam = off_support(def->x, def->y, def->own, player->cnum);
1605 ap = (((double)dam) / 100.0);
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(s_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);
1631 df = ((double)dam / 100.0);
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);
1637 ds = ((double)dam / 100.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);
1643 du = ((double)dam / 100.0);
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);
1649 dp = (((double)dam) / 100.0);
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(((double)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 s_char osupports[512];
1706 s_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 * becauase units & mil may have been damaged by collateral damage or
1717 * neclear 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 llist *)qp;
1773 bodies += total_mil(&llp->land);
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) */
1800 ototal = get_ototal(combat_mode, off, olist, osupport,
1801 combat_mode != A_PARA);
1802 dtotal = get_dtotal(def, dlist, dsupport, 0);
1806 a_bodies = count_bodies(off, olist);
1807 d_bodies = count_bodies(def, dlist);
1808 d_mil = def->troops;
1809 for (n = 0; n <= off->last; ++n)
1810 if (off[n].type == EF_BAD)
1813 a_troops[n] = off[n].troops;
1815 /* This switch is required to get the spacing right */
1816 switch (combat_mode) {
1818 pr(" Final attack strength: %8d\n", ototal);
1821 pr(" Final assault strength: %8d\n", ototal);
1824 if (def->sct_type == SCT_MOUNT ||
1825 def->sct_type == SCT_WATER ||
1826 def->sct_type == SCT_CAPIT ||
1827 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1828 pr("You can't air-assault a %s sector!\n",
1829 def->sct_dcp->d_name);
1832 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1834 pr(" Final air-assault strength: %8d\n", ototal);
1838 pr(" Final board strength: %8d\n", ototal);
1842 pr(" Final defense strength: %8d\n", dtotal);
1843 odds = att_calcodds(ototal, dtotal);
1844 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1846 /* spread the plague */
1847 if (combat_mode != A_PARA) {
1849 for (n = 0; n <= off->last; ++n)
1850 if (off[n].type != EF_BAD)
1851 def->plague |= off[n].plague;
1852 for (n = 0; n <= off->last; ++n)
1853 if (off[n].type != EF_BAD)
1854 off[n].plague |= def->plague;
1856 att_infect_units(olist, off->plague);
1857 att_infect_units(dlist, def->plague);
1859 /* Fighting is slightly random. There is always that last little
1860 * effort you see people put in. Or the stray bullet that takes out
1861 * an officer and the rest go into chaos. Things like that.
1862 * Thus, we have added a very slight random factor that will sometimes
1863 * allow the little guy to win. We modify the odds a little
1864 * (either +- 5%) to account for this randomness. We also only
1865 * recalculate the odds every 8-50 casualties, not every cacsualty,
1866 * since a single dead guy normally wouldn't cause a commander to
1867 * rethink his strategies, but 50 dead guys might. */
1868 odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1873 recalctime = 8 + (random() % 43);
1874 while (!success && ototal) {
1877 d_cas += take_casualty(A_DEFEND, def, dlist);
1878 dtotal = get_dtotal(def, dlist, dsupport, 0);
1883 a_cas += take_casualty(combat_mode, off, olist);
1884 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1886 if (((a_cas + d_cas) % 70) == 69)
1888 if (recalctime-- <= 0) {
1889 recalctime = 8 + (random() % 43);
1890 odds = att_calcodds(ototal, dtotal);
1891 odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1899 /* update defense mobility & mil */
1903 if (def->type == EF_SECTOR && d_mil && d_cas) {
1906 /* Make sure we use a positive mobility here */
1907 tmob = ((def->mob < 0) ? -(def->mob) : (def->mob));
1909 MIN(20, MIN(1, tmob - damage(tmob, 100 * d_cas / d_mil)));
1911 def->mil = def->troops;
1914 /* update attack mobility & mil */
1915 for (n = 0; n <= off->last; ++n)
1916 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1917 if (off[n].type == EF_SECTOR && off[n].mil)
1920 off[n].mob - damage(off[n].mob,
1921 100 * (a_troops[n] - off[n].troops)
1923 off[n].mil -= a_troops[n] - off[n].troops;
1926 /* update land unit mobility */
1927 if (d_bodies && d_cas)
1928 lnd_takemob(dlist, (double)d_cas / d_bodies);
1929 if (a_bodies && a_cas)
1930 lnd_takemob(olist, (double)a_cas / a_bodies);
1932 /* damage attacked sector */
1933 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1935 pr("- Casualties -\n Yours: %d\n", a_cas);
1936 pr(" Theirs: %d\n", d_cas);
1937 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
1938 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
1941 switch (combat_mode) {
1943 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
1944 pr("We have captured %s, sir!\n", prcom(0, def));
1948 news_item = def->own ? N_AWON_SECT : N_START_COL;
1949 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
1950 action = "assaulting and taking";
1953 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
1954 pr("We have captured %s, sir!\n", prcom(0, def));
1955 action = "air-assaulting and taking";
1958 news_item = N_BOARD_SHIP;
1959 pr("We have boarded %s, sir!\n", prcom(0, def));
1960 action = "boarding";
1963 news_item = N_BOARD_LAND;
1964 pr("We have boarded %s, sir!\n", prcom(0, def));
1965 action = "boarding";
1969 switch (combat_mode) {
1971 news_item = N_SCT_LOSE;
1972 pr("You have been defeated!\n");
1973 action = "attacking";
1976 news_item = N_ALOSE_SCT;
1977 pr("You have been defeated!\n");
1979 action = "trying to assault";
1982 news_item = N_PLOSE_SCT;
1983 pr("All of your troops were destroyed\n");
1984 action = "trying to air-assault";
1987 news_item = N_SHP_LOSE;
1988 pr("You have been repelled\n");
1990 action = "trying to board";
1993 news_item = N_LND_LOSE;
1994 pr("You have been repelled\n");
1996 action = "trying to board";
2000 nreport(player->cnum, news_item, def->own, 1);
2003 "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
2004 cname(player->cnum), player->cnum, a_cas,
2005 action, pr_com(0, def, def->own), d_cas);
2008 send_reacting_units_home(dlist);
2010 /* putland the defending land */
2013 /* putland the attacking land */
2016 /* put the victim sector/ship/land */
2017 if (!success || !take_def(combat_mode, olist, off, def))
2020 /* put the attacking sectors/ship */
2021 for (n = 0; n <= off->last; ++n)
2022 if (off[n].type != EF_BAD)
2023 put_combat(&off[n]);
2028 switch (combat_mode) {
2030 ask_move_in(off, olist, def);
2032 /* put sectors again to get abandon warnings */
2033 for (n = 0; n <= off->last; ++n)
2034 if (off[n].type != EF_BAD)
2035 put_combat(&off[n]);
2038 att_move_in_off(combat_mode, off, olist, def);
2041 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2045 /* What percentage of the combat forces going head-to-head are we? */
2048 att_calcodds(int ototal, int dtotal)
2052 /* calculate odds */
2055 else if (dtotal <= 0)
2058 odds = ((double)ototal) / (dtotal + ototal);
2063 /* Here's where the dead soldiers get dragged off the battlefield */
2066 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2068 int to_take = CASUALTY_LUMP;
2069 int biggest_troops = 0, index = -1;
2070 int n, tot_troops = 0, biggest_mil, cas;
2071 struct emp_qelem *qp, *biggest;
2074 for (n = 0; n <= off->last; ++n) {
2075 if (off[n].type != EF_BAD) {
2076 tot_troops += off[n].troops;
2077 if (off[n].troops > biggest_troops) {
2078 biggest_troops = off[n].troops;
2085 to_take -= tot_troops;
2088 for (n = 0; n <= off->last; ++n)
2089 if (off[n].type != EF_BAD)
2093 * They can all come off mil. We rotate the casualties,
2094 * starting with the sector containing the most mil.
2096 to_take = CASUALTY_LUMP;
2098 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2101 while (to_take > 0) {
2102 for (n = index; n <= off->last && to_take; ++n) {
2103 if (off[n].type != EF_BAD && off[n].troops > 0) {
2108 for (n = 0; n < index && to_take; ++n) {
2109 if (off[n].type != EF_BAD && off[n].troops > 0) {
2115 return CASUALTY_LUMP;
2119 return CASUALTY_LUMP - to_take;
2122 * Need to take some casualties from attacking units
2123 * Procedure: find the biggest unit remaining (in
2124 * terms of mil) and give it the casualties.
2126 biggest = (struct emp_qelem *)0;
2128 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2129 llp = (struct llist *)qp;
2131 if (total_mil(&llp->land) > biggest_mil) {
2132 biggest_mil = total_mil(&llp->land);
2136 if (biggest == (struct emp_qelem *)0)
2137 return CASUALTY_LUMP - to_take;
2139 llp = (struct llist *)biggest;
2140 cas = lnd_take_casualty(combat_mode, llp, to_take);
2141 return CASUALTY_LUMP - (to_take - cas);
2144 /* Send reacting defense units back to where they came from (at no mob cost) */
2147 send_reacting_units_home(struct emp_qelem *list)
2149 struct emp_qelem *qp, *next;
2153 for (qp = list->q_forw; qp != list; qp = next) {
2155 llp = (struct llist *)qp;
2156 if ((llp->land.lnd_x != llp->x) || (llp->land.lnd_y != llp->y)) {
2157 sprintf(buf, "returns to %s",
2158 xyas(llp->x, llp->y, llp->land.lnd_own));
2159 llp->land.lnd_x = llp->x;
2160 llp->land.lnd_y = llp->y;
2161 lnd_delete(llp, buf);
2166 /* Check for 0 offense strength. This call will always preceed an abort */
2169 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2172 if (def->own && player->cnum != def->own) {
2174 "%s (#%d) considered %sing you @%s\n",
2175 cname(player->cnum), player->cnum,
2176 att_mode[combat_mode], xyas(def->x, def->y, def->own));
2178 pr("No troops for %s...\n", att_mode[combat_mode]);
2185 * Take the defending sector or ship from the defender and give it to the
2190 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2195 struct llist *llp, *delete_me = 0;
2201 for (n = 0; n <= off->last && !occuppied; ++n) {
2202 if (off[n].type != EF_BAD &&
2203 off[n].troops > 0 &&
2204 (off[n].type != EF_SECTOR || off[n].mob)) {
2206 if (def->type == EF_LAND) {
2207 if (def->lnd_lcp->l_flags & L_SPY) {
2214 pr("1 mil from %s moves %s\n",
2215 prcom(0, off + n), prcom(2, def));
2220 pr("%s left unoccupied\n", prcom(0, def));
2223 "No enemy troops moved %s so you still own it!\n",
2224 pr_com(2, def, def->own));
2227 llp = (struct llist *)list->q_forw;
2228 llp->land.lnd_x = def->x;
2229 llp->land.lnd_y = def->y;
2230 take_move_in_mob(combat_mode, llp, off, def);
2231 if (def->type == EF_SHIP) {
2232 llp->land.lnd_ship = def->shp_uid;
2233 sprintf(buf, "boards %s", prcom(0, def));
2236 llp->land.lnd_ship = -1;
2237 sprintf(buf, "moves in to occupy %s",
2238 xyas(def->x, def->y, player->cnum));
2239 lnd_delete(llp, buf);
2244 if (def->type == EF_SECTOR) {
2245 getsect(def->x, def->y, §);
2246 takeover(§, player->cnum);
2247 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2248 caploss(§, def->own,
2249 "* We have captured %s's capital, sir! *\n");
2251 } else if (def->type == EF_SHIP) {
2252 getship(def->shp_uid, &ship);
2253 takeover_ship(&ship, player->cnum, 1);
2254 putship(ship.shp_uid, &ship);
2255 } else if (def->type == EF_LAND) {
2256 getland(def->lnd_uid, &land);
2257 takeover_land(&land, player->cnum, 1);
2258 putland(land.lnd_uid, &land);
2261 lnd_delete(delete_me, buf);
2262 att_get_combat(def, 0);
2267 * Ask the attacker which mil & land units they'd like to move into the
2272 ask_move_in(struct combat *off, struct emp_qelem *olist,
2276 struct emp_qelem *qp, *next;
2280 s_char land_answer[1024];
2283 for (n = 0; n <= off->last; ++n)
2284 if (off[n].type != EF_BAD && off[n].troops > 0)
2286 ask_move_in_off(&off[n], def);
2287 if (player->aborted)
2293 memset(land_answer, 0, sizeof(land_answer));
2294 for (qp = olist->q_forw; qp != olist; qp = next) {
2296 llp = (struct llist *)qp;
2297 answerp = &land_answer[(int)llp->land.lnd_army];
2298 if (player->aborted || att_get_combat(def, 0) < 0)
2300 if (*answerp == 'Y')
2302 if (*answerp != 'N') {
2303 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2305 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2307 llp->land.lnd_army == ' ' ? '~' : llp->land.lnd_army,
2308 llp->land.lnd_effic);
2309 *answerp = att_prompt(prompt, llp->land.lnd_army);
2310 if (player->aborted || att_get_combat(def, 0) < 0)
2312 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2315 if (*answerp == 'y' || *answerp == 'Y')
2317 sprintf(buf, "stays in %s",
2318 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2319 lnd_delete(llp, buf);
2323 if (att_get_combat(def, 0) < 0) {
2324 for (qp = olist->q_forw; qp != olist; qp = next) {
2326 llp = (struct llist *)qp;
2327 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2329 sprintf(buf, "stays in %s",
2330 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2331 lnd_delete(llp, buf);
2335 if (opt_INTERDICT_ATT)
2336 lnd_interdict(olist, def->x, def->y, player->cnum);
2337 move_in_land(A_ATTACK, off, olist, def);
2340 /* Move offensive land units to the conquered sector or ship */
2343 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2346 struct emp_qelem *qp, *next;
2352 for (qp = olist->q_forw; qp != olist; qp = next) {
2354 llp = (struct llist *)qp;
2355 if (!get_land(combat_mode, def, llp->land.lnd_uid, llp, 0))
2357 take_move_in_mob(combat_mode, llp, off, def);
2358 llp->land.lnd_x = def->x;
2359 llp->land.lnd_y = def->y;
2360 if (def->type == EF_SHIP)
2361 llp->land.lnd_ship = def->shp_uid;
2363 llp->land.lnd_ship = -1;
2367 if (def->type == EF_SECTOR) {
2368 if (opt_INTERDICT_ATT) {
2369 lnd_sweep(olist, 0, 0, def->own);
2370 lnd_check_mines(olist);
2372 sprintf(buf, "now occupies %s", prcom(0, def));
2374 sprintf(buf, "boards %s", prcom(0, def));
2378 for (qp = olist->q_forw; qp != olist; qp = next) {
2380 llp = (struct llist *)qp;
2381 lnd_print(llp, buf);
2389 * Move assaulting, paradropping, or boarding mil & units into def
2390 * If the mil are coming from a ship, then pack a lunch.
2394 att_move_in_off(int combat_mode, struct combat *off,
2395 struct emp_qelem *olist, struct combat *def)
2402 move_in_land(combat_mode, off, olist, def);
2404 for (n = 0; n <= off->last; ++n) {
2405 if (off[n].type == EF_BAD || !off[n].troops)
2407 troops = off[n].troops;
2409 off[n].mil -= troops;
2411 put_combat(off + n);
2412 if (combat_mode == A_ASSAULT) {
2413 if (CANT_HAPPEN(off[n].type != EF_SHIP))
2415 getship(off[n].shp_uid, &ship);
2416 lunchbox += (int)((troops + 1) * ship.shp_item[I_FOOD]
2417 / (ship.shp_item[I_MILIT] + troops
2418 + ship.shp_item[I_CIVIL] + 0.5));
2420 ship.shp_item[I_FOOD] -= lunchbox;
2421 putship(ship.shp_uid, &ship);
2427 if (combat_mode == A_ASSAULT) {
2428 if (CANT_HAPPEN(def->type != EF_SECTOR))
2430 getsect(def->x, def->y, §);
2431 if (lunchbox > ITEM_MAX - sect.sct_item[I_FOOD])
2432 lunchbox = ITEM_MAX - sect.sct_item[I_FOOD];
2433 sect.sct_item[I_FOOD] += lunchbox;
2439 /* Ask how many mil to move in from each sector */
2442 ask_move_in_off(struct combat *off, struct combat *def)
2445 int num_mil, dam = 0, left;
2451 if (att_get_combat(off, 0) <= 0)
2453 if (att_get_combat(def, 0) < 0)
2455 if (off->own != player->cnum)
2457 d = sector_mcost(getsectp(def->x, def->y), MOB_ROAD);
2458 if ((mob_support = MIN(off->troops, (int)(off->mob / d))) <= 0)
2460 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2461 xyas(off->x, off->y, player->cnum), mob_support);
2462 if (!(p = getstring(prompt, buf)) || !*p || (num_mil = atoi(p)) <= 0) {
2466 /* Make sure we don't move in more than we can support mobility-wise */
2467 if (num_mil > mob_support)
2468 num_mil = mob_support;
2469 if (att_get_combat(off, 0) <= 0)
2471 if (att_get_combat(def, 0) < 0)
2473 if ((num_mil = MIN(off->troops, num_mil)) <= 0) {
2474 pr("No mil moved in from %s\n",
2475 xyas(off->x, off->y, player->cnum));
2478 mob_support = MAX(1, (int)(num_mil * d));
2479 off->mob -= MIN(off->mob, mob_support);
2480 off->mil -= num_mil;
2481 off->troops -= num_mil;
2484 weight = num_mil * ichr[I_MILIT].i_lbs;
2485 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2486 if (chance(weight / 100.0))
2488 ground_interdict(def->x, def->y, player->cnum, "military");
2489 dam += check_lmines(def->x, def->y, weight);
2493 left = commdamage(num_mil, dam, I_MILIT);
2494 if (left < num_mil) {
2496 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));
2498 pr("All of the mil you were moving were destroyed!\n");
2501 /* maybe got nuked */
2502 if (att_get_combat(def, 0) < 0)
2510 /* Charge land units for moving into a sector or onto a ship */
2513 take_move_in_mob(int combat_mode, struct llist *llp, struct combat *off,
2519 switch (combat_mode) {
2522 lnd_mobcost(&llp->land, getsectp(def->x, def->y), MOB_NONE);
2523 new = llp->land.lnd_mobil - mobcost;
2526 llp->land.lnd_mobil = new;
2529 if (off->shp_mcp->m_flags & M_LAND) {
2530 if (llp->lcp->l_flags & L_MARINE)
2531 llp->land.lnd_mobil -=
2532 (float)etu_per_update * land_mob_scale * 0.5;
2534 llp->land.lnd_mobil -= (float)etu_per_update * land_mob_scale;
2536 if (llp->lcp->l_flags & L_MARINE)
2537 llp->land.lnd_mobil = 0;
2539 llp->land.lnd_mobil = -(float)etu_per_update * land_mob_scale;
2543 /* I arbitrarily chose the numbers 10 and 40 below -KHS */
2544 if (llp->lcp->l_flags & L_MARINE)
2545 llp->land.lnd_mobil -= 10;
2547 llp->land.lnd_mobil -= 40;
2550 llp->land.lnd_harden = 0;
2554 free_list(struct emp_qelem *list)
2556 struct emp_qelem *qp, *next;
2558 if (!list || QEMPTY(list))
2562 while (qp != list) {
2571 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2579 * sector_strength - Everyone starts at 1. You can get up to a max
2580 * of d_dstr, depending on how much you build up the
2581 * defenses of the sector.
2585 sector_strength(struct sctstr *sp)
2591 if (sp->sct_type == SCT_MOUNT)
2594 d = d + ((double)(dchr[sp->sct_type].d_dstr - d) *
2595 ((double)sp->sct_defense / 100.0));
2597 if (d > dchr[sp->sct_type].d_dstr)
2598 d = dchr[sp->sct_type].d_dstr;