2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * attsub.c: Attack subroutines
30 * Known contributors to this file:
32 * Steve McClure, 1996-2000
53 #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 s_char *land_answer, int *a_spyp, int *a_engineerp);
60 static void take_move_in_mob(int combat_mode, struct llist *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 int 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 int take_casualty(int combat_mode, struct combat *off,
79 struct emp_qelem *olist);
81 static void send_reacting_units_home(struct emp_qelem *list);
82 static int take_def(int combat_mode, struct emp_qelem *list,
83 struct combat *off, struct combat *def);
85 static int get_land(int combat_mode, struct combat *def, int uid,
86 struct llist *llp, int victim_land);
88 s_char *att_mode[] = {
89 /* must match combat types in combat.h */
90 "defend", "attack", "assault", "paradrop", "board", "lboard"
95 * The principal object in this code is the "combat" object. A combat object
96 * is either a sector or ship. There are
97 * usually two instances of this, the "def" or defense combat object, and
98 * the array of "off" or offense objects. The number of offense objects is
99 * determined by the value of off->last (e.g. more than one attacking sector).
100 * the type of the object is determined by combat->type which can take the
101 * values EF_SECTOR, EF_SHIP, EF_PLANE, or EF_BAD. Another important parameter
102 * which is often passed to these functions is combat_mode. This can take
103 * the value A_DEFENSE, A_ATTACK, A_ASSAULT, A_PARA, A_BOARD and A_LBOARD.
104 * As these six modes of being in combat affect things like mobcost and combat
105 * value, there are often switches made on combat_mode. Note that in all cases
106 * no mobility is taken from sectors, ships, or land units until the player
107 * has committed to a fight. Instead, the cost is temporarily placed in
108 * combat->mobcost, or llp->mobil as the case may be, and then when the object
109 * is "put" back onto disk, then the amounts in these variables are subtracted
110 * from the object's mobility. It needs to be done this way as the objects
111 * are constantly being re-read from disk, and we don't want to take any mob
112 * unless a fight actually occurrs.
116 /* initialize combat object */
119 att_combat_init(struct combat *com, int type)
121 memset(com, 0, sizeof(*com));
126 /* print a combat object with optional preposition */
129 pr_com(int inon, struct combat *com, natid who)
131 if (com->type == EF_SECTOR) {
133 inon ? inon == 1 ? "in " : "into " : "",
134 xyas(com->x, com->y, who));
135 } else if (com->type == EF_SHIP) {
137 return prbuf("%s%s %s(#%d)",
138 inon ? inon == 1 ? "on " : "onto " : "",
139 com->shp_mcp->m_name, com->shp_name,
142 return prbuf("%s%s #%d",
143 inon ? inon == 1 ? "on " : "onto " : "",
144 com->shp_mcp->m_name, com->shp_uid);
146 } else if (com->type == EF_LAND) {
147 return prbuf("%s%s #%d",
148 inon ? inon == 1 ? "on " : "onto " : "",
149 com->lnd_lcp->l_name, com->lnd_uid);
151 return "your forces";
156 prcom(int inon, struct combat *com)
158 return pr_com(inon, com, player->cnum);
161 /* Doing a sneak attack */
163 do_sneak(struct combat *def, int success)
166 struct natstr *natp = getnatp(player->cnum);
167 int issneak = getrel(natp, def->own);
169 if (def->type != EF_SECTOR)
172 getsect(def->x, def->y, §);
174 if (issneak == AT_WAR || !def->own || sect.sct_oldown == player->cnum)
178 pr("Your sneak attack was successful\nBut ");
180 pr("Your sneak attack was unsuccessful\nAnd ");
182 pr("it will cost you $5000\n");
183 pr("War has been declared!!!!\n");
184 wu(0, def->own, "Country %s (#%d) has Sneak Attacked!!\n",
185 cname(player->cnum), player->cnum);
186 wu(0, def->own, "Country %s (#%d) has Declared WAR on you!!\n",
187 cname(player->cnum), player->cnum);
188 player->dolcost += 5000;
189 issneak = min(issneak, MOBILIZATION);
190 nreport(player->cnum, N_DECL_WAR, def->own, 1);
191 setrel(player->cnum, def->own, issneak);
195 * This is the combat object "type" based integrity check. It basically
196 * splits along three divisions: ship/sector, attacker/defender,
197 * first time/not first time.
201 att_get_combat(struct combat *com, int isdef)
216 if (!getsect(com->x, com->y, §)) {
217 pr("Bad sector: %s\n", xyas(com->x, com->y, player->cnum));
218 return att_combat_init(com, EF_BAD);
220 com->sct_type = sect.sct_type;
221 com->sct_dcp = &dchr[sect.sct_type];
222 item = sect.sct_item;
223 pstage = sect.sct_pstage;
224 owner = sect.sct_own;
225 eff = sect.sct_effic;
226 mob = sect.sct_mobil;
231 if (!getland(com->lnd_uid, &land)) {
233 pr("Land unit #%d is not in the same sector!\n",
235 return att_combat_init(com, EF_BAD);
237 if (isdef && player->owner) {
238 pr("Boarding yourself? Try using the 'load' command.\n");
239 return att_combat_init(com, EF_BAD);
241 com->lnd_lcp = &lchr[(int)land.lnd_type];
242 item = land.lnd_item;
243 pstage = land.lnd_pstage;
244 owner = land.lnd_own;
245 eff = land.lnd_effic;
246 mob = land.lnd_mobil;
251 if (!getship(com->shp_uid, &ship) || !ship.shp_own) {
253 pr("Ship #%d is not in the same sector!\n", com->shp_uid);
255 pr("Ship #%d is not your ship!\n", com->shp_uid);
256 return att_combat_init(com, EF_BAD);
259 if (isdef && player->owner &&
260 ontradingblock(EF_SHIP, (int *)&ship)) {
261 pr("%s is on the trading block.\n", prcom(0, com));
262 return att_combat_init(com, EF_BAD);
265 if (isdef && player->owner) {
266 pr("Boarding yourself? Try using the 'tend' command.\n");
267 return att_combat_init(com, EF_BAD);
269 com->shp_mcp = &mchr[(int)ship.shp_type];
271 strncpy(com->shp_name, ship.shp_name, MAXSHPNAMLEN);
272 if (!isdef && !player->owner) {
274 pr("%s was just sunk!\n", prcom(0, com));
276 pr("Ship #%d is not your ship!\n", com->shp_uid);
277 return att_combat_init(com, EF_BAD);
279 item = ship.shp_item;
280 pstage = ship.shp_pstage;
281 owner = ship.shp_own;
282 eff = ship.shp_effic;
283 mob = ship.shp_mobil;
292 return att_combat_init(com, EF_BAD);
296 if (!com->set) { /* first time */
297 if (isdef) { /* defender */
299 } else { /* attacker */
301 pr("No mil %s\n", prcom(1, com));
303 pr("Only 1 mil %s\n", prcom(1, com));
304 /* don't abandon attacking sectors or ships */
305 com->troops = max(0, mil - 1);
307 com->plague = pstage == PLG_INFECT;
308 } else { /* not first time */
309 if (isdef) { /* defender */
310 if (com->x != x || com->y != y) {
311 pr("%s has moved!\n", prcom(0, com));
312 return att_combat_init(com, EF_BAD);
314 if (owner != com->own) {
316 pr("WARNING: The ownership of %s just changed from %s to %s!\n", prcom(0, com), cname(com->own), cname(owner));
317 } else if (com->type == EF_SECTOR) {
318 pr("WARNING: %s just abandoned sector %s!\n",
319 cname(com->own), xyas(com->x, com->y,
324 pr("WARNING: The enemy mil %s just %s from %d to %d!\n",
326 com->mil < mil ? "increased" : "decreased", com->mil,
329 } else { /* attacker */
330 if (owner != player->cnum && getrel(getnatp(owner), player->cnum) != ALLIED) { /* must be EF_SECTOR */
332 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));
334 pr("You no longer own %s\n",
335 xyas(com->x, com->y, player->cnum));
336 return att_combat_init(com, EF_BAD);
338 if (com->troops && com->troops + 1 > mil) {
339 if (com->own == owner && player->cnum == owner) /* not a takeover */
340 pr("WARNING: Your mil %s has been reduced from %d to %d!\n", prcom(1, com), com->troops, max(0, mil - 1));
341 com->troops = max(0, mil - 1);
356 * In the course of the fight, the combat object may have lost mil, eff, or
357 * mobility. This is the place where the data in the object gets flushed to
358 * disk to make it "real".
362 put_combat(struct combat *com)
371 getsect(com->x, com->y, §);
372 sect.sct_type = com->sct_type;
373 deff = sect.sct_effic - com->eff;
375 sect.sct_road -= (sect.sct_road * deff / 100.0);
376 sect.sct_rail -= (sect.sct_rail * deff / 100.0);
377 sect.sct_defense -= (sect.sct_defense * deff / 100.0);
378 if (sect.sct_road <= 0)
380 if (sect.sct_rail <= 0)
382 if (sect.sct_defense <= 0)
383 sect.sct_defense = 0;
385 sect.sct_effic = com->eff;
386 if (!opt_DEFENSE_INFRA)
387 sect.sct_defense = sect.sct_effic;
389 if (opt_MOB_ACCESS) {
390 if ((com->mob - com->mobcost) < -127)
391 sect.sct_mobil = -127;
393 sect.sct_mobil = (short)(com->mob - com->mobcost);
395 if ((com->mob - com->mobcost) < 0)
398 sect.sct_mobil = (short)(com->mob - com->mobcost);
401 makelost(EF_SECTOR, sect.sct_own, 0, sect.sct_x, sect.sct_y);
402 makenotlost(EF_SECTOR, com->own, 0, sect.sct_x, sect.sct_y);
403 sect.sct_own = com->own;
405 if (sect.sct_pstage == PLG_HEALTHY)
406 sect.sct_pstage = PLG_EXPOSED;
408 sect.sct_item[I_MILIT] = com->mil;
410 com->own = sect.sct_own; /* avoid WARNING if sector reverts */
413 getland(com->lnd_uid, &land);
414 land.lnd_effic = com->eff;
416 if (com->mob - com->mobcost < -127)
417 land.lnd_mobil = -127;
419 land.lnd_mobil = (s_char)(com->mob - com->mobcost);
421 makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
423 land.lnd_own = com->own;
424 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
427 if (land.lnd_pstage == PLG_HEALTHY)
428 land.lnd_pstage = PLG_EXPOSED;
430 if (!(com->lnd_lcp->l_flags & L_SPY))
431 land.lnd_item[I_MILIT] = com->mil;
432 lnd_count_units(&land);
433 if (com->own == player->cnum) {
434 land.lnd_mission = 0;
436 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
438 putland(com->lnd_uid, &land);
441 getship(com->shp_uid, &ship);
442 ship.shp_effic = com->eff;
444 if (com->mob - com->mobcost < -127)
445 ship.shp_mobil = -127;
447 ship.shp_mobil = (s_char)(com->mob - com->mobcost);
449 makelost(EF_SHIP, ship.shp_own, ship.shp_uid, ship.shp_x,
451 ship.shp_own = com->own;
452 makenotlost(EF_SHIP, ship.shp_own, ship.shp_uid, ship.shp_x,
455 if (ship.shp_pstage == PLG_HEALTHY)
456 ship.shp_pstage = PLG_EXPOSED;
458 ship.shp_item[I_MILIT] = com->mil;
460 if (com->own == player->cnum) {
461 ship.shp_mission = 0;
463 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
465 putship(com->shp_uid, &ship);
468 att_get_combat(com, com->own != player->cnum);
471 /* If pre-attack, abort fight. If post-attack, don't move anything in */
476 return player->aborted = 1;
480 * This is the combat_mode based integrity check. It splits among two main
481 * divisions: first time/not first time, and attack/assault/para/board.
485 att_abort(int combat_mode, struct combat *off, struct combat *def)
494 if (att_get_combat(def, 1) < 0)
495 return abort_attack();
497 if (off && combat_mode != A_ATTACK) {
498 if (att_get_combat(off, 0) < 0)
499 return abort_attack();
500 if (off->type == EF_SHIP &&
501 (!getsect(off->x, off->y, §) ||
502 sect.sct_type != SCT_WATER)) {
503 pr("%s can not %s from that far inland!\n",
504 prcom(0, off), att_mode[combat_mode]);
505 return abort_attack();
508 switch (combat_mode) {
510 if (!neigh(def->x, def->y, player->cnum) &&
511 !adj_units(def->x, def->y, player->cnum)) {
512 pr("You are not adjacent to %s\n",
513 xyas(def->x, def->y, player->cnum));
514 return abort_attack();
516 if (def->own == player->cnum) {
517 pr("You can't attack your own sector.\n");
518 return abort_attack();
522 if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
523 pr("You'll have to get there first...\n");
524 return abort_attack();
526 if (off && def->sct_type == SCT_MOUNT) {
527 pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
528 return abort_attack();
532 if (def->own == player->cnum) {
533 pr("You can't air-assault your own sector.\n");
534 return abort_attack();
536 if (off && (def->sct_type == SCT_MOUNT ||
537 def->sct_type == SCT_WATER ||
538 def->sct_type == SCT_CAPIT ||
539 def->sct_type == SCT_FORTR ||
540 def->sct_type == SCT_WASTE)) {
541 pr("You can't air-assault a %s sector!\n",
542 def->sct_dcp->d_name);
543 return abort_attack();
547 return board_abort(off, def);
549 return land_board_abort(off, def);
552 if (off && def->sct_dcp->d_mcst <= 0) {
553 pr("You can't %s a %s sector!\n", att_mode[combat_mode],
554 def->sct_dcp->d_name);
555 return abort_attack();
557 if (!off || off->relations_checked)
559 off->relations_checked = 1;
562 setcont(player->cnum, def->own, FOUND_SPY);
563 setcont(def->own, player->cnum, FOUND_SPY);
565 if (opt_SLOW_WAR && def->own != player->cnum) {
566 natp = getnatp(player->cnum);
567 issneak = getrel(natp, def->own);
569 if (issneak == ALLIED) {
570 sprintf(y_or_n, "Sector is owned by %s, your ally, %s [yn]? ",
571 cname(def->own), att_mode[combat_mode]);
572 if (!confirm(y_or_n))
573 return abort_attack();
576 if (opt_SNEAK_ATTACK) {
577 getsect(def->x, def->y, §);
578 if ((issneak != AT_WAR) && (def->own)
579 && (def->own != player->cnum)
580 && (sect.sct_oldown != player->cnum)
581 && (issneak != SITZKRIEG) && (issneak != MOBILIZATION)) {
582 pr("You're not at war with them!\n");
583 if (!confirm("Do you really want to sneak attack (it will cost you $5000) [yn]? ")) {
584 pr("Sneak attack cancelled!\n");
585 return abort_attack();
588 if ((issneak != AT_WAR) && (def->own)
589 && (def->own != player->cnum)
590 && (sect.sct_oldown != player->cnum)
591 && ((issneak == MOBILIZATION) || (issneak == SITZKRIEG))) {
592 pr("You're not at war with them!\n");
593 return abort_attack();
596 if ((issneak != AT_WAR) && (def->own) &&
597 (sect.sct_oldown != player->cnum)) {
598 pr("You're not at war with them!\n");
599 return abort_attack();
607 * Lots of special things need to be checked for boarding, so I put it in
612 board_abort(struct combat *off, struct combat *def)
614 struct shpstr aship, dship; /* for tech levels */
617 if (att_get_combat(def, 1) < 0)
618 return abort_attack();
623 if (att_get_combat(off, 0) < 0)
624 return abort_attack();
626 if (off->x != def->x || off->y != def->y) {
627 pr("Ship #%d is not in the same sector!\n", def->shp_uid);
628 return abort_attack();
630 if (off->type == EF_SHIP) {
632 pr("%s has no mobility!\n", prcom(0, off));
633 return abort_attack();
635 getship(off->shp_uid, &aship);
636 getship(def->shp_uid, &dship);
637 if (techfact(aship.shp_tech, 1.0) *
638 aship.shp_speed * off->eff <=
639 techfact(dship.shp_tech, 1.0) * dship.shp_speed * def->eff) {
640 pr("Victim ship moves faster than you do!\n");
643 "%s (#%d) %s failed to catch %s\n",
644 cname(aship.shp_own), aship.shp_own,
645 pr_com(0, off, def->own), pr_com(0, def, def->own));
646 return abort_attack();
648 } else if (off->type != EF_SECTOR) {
649 pr("Please tell the deity that you got the 'banana boat' error\n");
650 return abort_attack();
652 if (def->shp_mcp->m_flags & M_SUB) {
653 getsect(def->x, def->y, §);
654 if (sect.sct_type == SCT_WATER) {
655 pr("You can't board a submarine!\n");
656 return abort_attack();
663 * Lots of special things need to be checked for boarding, so I put it in
665 * STM - I copied it for land unit boarding. :)
669 land_board_abort(struct combat *off, struct combat *def)
671 if (att_get_combat(def, 1) < 0)
672 return abort_attack();
677 if (att_get_combat(off, 0) < 0)
678 return abort_attack();
680 if (off->x != def->x || off->y != def->y) {
681 pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
682 return abort_attack();
688 /* If we are boarding, then the defending ship gets a chance to fire back */
690 att_approach(struct combat *off, struct combat *def)
696 pr("Approaching %s...\n", prcom(0, def));
699 "%s is being approached by %s...\n",
700 pr_com(0, def, def->own), pr_com(0, off, def->own));
701 if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
704 pr("They're firing at us sir!\n");
707 "Your fleet at %s does %d damage to %s\n",
708 xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
710 if (off->type == EF_SECTOR) {
711 getsect(off->x, off->y, §);
712 sectdamage(§, dam, 0);
714 pr("Enemy fleet at %s does %d damage to %s\n",
715 xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
716 } else if (off->type == EF_SHIP) {
717 getship(off->shp_uid, &ship);
718 shipdamage(&ship, dam);
719 putship(off->shp_uid, &ship);
720 if (def->own && ship.shp_effic < SHIP_MINEFF) {
721 wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
722 nreport(player->cnum, N_SHP_LOSE, def->own, 1);
725 if (att_get_combat(off, 0) < 0)
726 return abort_attack();
730 /* The attack is valid. Tell the attacker about what they're going to hit */
733 att_show(struct combat *def)
735 /* Note that we tell the player about the treaty BEFORE we tell them
736 about the item. If we didn't, then they gain free information */
737 if (def->type == EF_SECTOR) {
738 if (!trechk(player->cnum, def->own, LANATT))
739 return abort_attack();
740 pr("%s is a %d%% %s %s with approximately %d military.\n",
741 xyas(def->x, def->y, player->cnum),
742 roundintby((int)def->eff, 10),
743 cname(def->own), def->sct_dcp->d_name,
744 roundintby(def->troops, 10));
745 if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
746 writemap(player->cnum);
747 } else if (def->type == EF_SHIP || def->type == EF_LAND) {
748 if (def->type == EF_SHIP) {
749 if (!trechk(player->cnum, def->own, SEAATT))
750 return abort_attack();
752 if (!trechk(player->cnum, def->own, LNDATT))
753 return abort_attack();
755 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));
757 /* Ok, everything is fine */
761 /* Attack and assault ask the user which kind of support they want */
764 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
769 *fortp = *shipp = *landp = *planep = 1;
771 if (player->argp[offset] != (s_char *)0) {
772 if ((player->argp[offset + 1] == (s_char *)0) ||
773 (player->argp[offset + 2] == (s_char *)0) ||
774 (player->argp[offset + 3] == (s_char *)0)) {
775 pr("If any support arguments are used, all must be!\n");
780 *landp = *planep = 0;
782 if (!(p = getstarg(player->argp[offset], "Use fort support? ",
786 if ((*p == 'y') || (*p == 'Y'))
789 if (!(p = getstarg(player->argp[offset + 1], "Use ship support? ",
793 if ((*p == 'y') || (*p == 'Y'))
796 if (!(p = getstarg(player->argp[offset + 2], "Use land support? ",
800 if ((*p == 'y') || (*p == 'Y'))
803 if (!(p = getstarg(player->argp[offset + 3], "Use plane support? ",
807 if ((*p == 'y') || (*p == 'Y'))
814 * Attack, assault, and board ask the attacker what they'd like to attack
815 * with. This includes mil and land units from each "off" object. Note that
816 * after each sub-prompt, we check to make sure that the attack is still
817 * valid, and if it's not, then we abort the attack.
821 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
822 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
825 s_char land_answer[1024];
828 if (att_abort(combat_mode, off, def))
830 memset(land_answer, 0, sizeof(land_answer));
831 for (n = 0; n <= off->last; ++n) {
832 off[n].troops = ask_off(combat_mode, off + n, def);
833 if (att_abort(combat_mode, off, def))
835 ask_olist(combat_mode, off + n, def, olist, land_answer,
836 a_spyp, a_engineerp);
837 if (att_abort(combat_mode, off, def))
843 /* How many mil is off allowed to attack with when it attacks def? */
846 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
850 switch (combat_mode) {
852 mob_support = off->mob / sector_mcost(getsectp(def->x, def->y),
856 /* mob_support = off->mob / sector_mcost(def->sct_type, def->eff);*/
857 if (mob_support < off->troops)
858 pr("Sector %s has %d mobility which can only support %d mil,\n", xyas(off->x, off->y, player->cnum), off->mob, mob_support);
860 mob_support = off->troops;
863 if (def->own != player->cnum && def->mil) {
864 if (off->shp_mcp->m_flags & M_SEMILAND)
865 return off->troops / 4;
866 else if (!(off->shp_mcp->m_flags & M_LAND))
867 return off->troops / 10;
871 if (off->type == EF_SECTOR && off->mob <= 0)
873 mob_support = vl_find(V_MILIT, def->shp_mcp->m_vtype,
874 def->shp_mcp->m_vamt, def->shp_mcp->m_nv);
875 if (mob_support < off->troops)
876 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
878 mob_support = off->troops;
883 if (def->lnd_lcp->l_flags & L_SPY)
885 mob_support = vl_find(V_MILIT, def->lnd_lcp->l_vtype,
886 def->lnd_lcp->l_vamt, def->lnd_lcp->l_nv);
887 if (mob_support < off->troops)
888 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
890 mob_support = off->troops;
897 * If the attacker decides to go through with the attack, then the
898 * sectors/ships they are attacking with may be charged some mobility.
899 * This is where that amount of mobility is calculated. It is actually
900 * subtracted "for real" from the object's mobility in put_combat().
904 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
911 switch (combat_mode) {
915 (int)(attacking_mil *
916 sector_mcost(getsectp(def->x, def->y), MOB_ROAD)));
919 off->mobcost += max(1, attacking_mil / 5);
924 off->mobcost += max(1, attacking_mil / 5);
927 /* the 2 in the formula below is a fudge factor */
928 getship(def->shp_uid, &ship);
929 off->mobcost += (def->eff / 100) * (ship.shp_speed / 2);
934 /* How many mil to we want to attack from off against def? */
937 ask_off(int combat_mode, struct combat *off, struct combat *def)
943 if (att_get_combat(off, 0) <= 0)
945 if ((off->type == EF_SECTOR) && (off->own != player->cnum))
947 if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
949 if (off->type == EF_SECTOR) {
950 if (off->own != player->cnum)
952 sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
953 off->sct_dcp->d_name,
954 xyas(off->x, off->y, player->cnum), mob_support);
956 sprintf(prompt, "Number of mil from %s (max %d) : ",
957 prcom(0, off), mob_support);
959 if ((attacking_mil = onearg(0, prompt)) < 0)
961 if (att_abort(combat_mode, off, def))
963 if (att_get_combat(off, 0) <= 0)
966 min(attacking_mil, min(mob_support, off->troops))) <= 0)
969 calc_mobcost(combat_mode, off, def, attacking_mil);
970 return attacking_mil;
974 * Which units would you like to attack with or move in with [ynYNq?]
978 att_prompt(s_char *prompt, s_char army)
986 while (!p || (*p != 'y' && *p != 'n' && *p != 'Y' && *p != 'N')) {
987 if (p && *p == 'q') {
994 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);
995 p = getstring(prompt, buf);
996 if (player->aborted) {
1004 /* Ask the attacker which units they want to attack/assault/board with */
1007 ask_olist(int combat_mode, struct combat *off, struct combat *def,
1008 struct emp_qelem *olist, s_char *land_answer, int *a_spyp,
1011 struct nstr_item ni;
1015 struct lchrstr *lcp;
1022 if (def->type == EF_LAND)
1024 if (def->type == EF_SHIP)
1025 maxland = def->shp_mcp->m_nland;
1027 snxtitem_xy(&ni, EF_LAND, off->x, off->y);
1028 while (nxtitem(&ni, (s_char *)&land)) {
1029 if (land.lnd_own != player->cnum)
1031 if (land.lnd_effic < LAND_MINEFF)
1033 if (land_answer[(int)land.lnd_army] == 'N')
1035 if (!lnd_can_attack(&land))
1037 lcp = &lchr[(int)land.lnd_type];
1039 if (def->type == EF_SHIP && !maxland) {
1040 pr("Land units are not able to board this kind of ship\n");
1043 if (land.lnd_mobil <= 0) {
1044 pr("%s is out of mobility, and cannot %s\n",
1045 prland(&land), att_mode[combat_mode]);
1050 if (ontradingblock(EF_LAND, (int *)&land)) {
1051 pr("%s is on the trading block, and cannot %s\n",
1052 prland(&land), att_mode[combat_mode]);
1057 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
1058 pr("%s is on ship #%d, and cannot %s\n",
1059 prland(&land), land.lnd_ship, att_mode[combat_mode]);
1061 } else if (off->type == EF_SHIP) {
1062 if (land.lnd_ship != off->shp_uid)
1064 } else if (land.lnd_land >= 0) {
1065 pr("%s is on unit #%d, and cannot %s\n",
1066 prland(&land), land.lnd_land, att_mode[combat_mode]);
1069 switch (combat_mode) {
1072 lnd_mobcost(&land, getsectp(def->x, def->y), MOB_NONE);
1073 if (land.lnd_mobil < mobcost) {
1074 pr("%s does not have enough mobility (%d needed)\n",
1075 prland(&land), (int)ceil(mobcost));
1082 if (!(lcp->l_flags & L_ASSAULT))
1085 att_val = attack_val(combat_mode, &land);
1087 pr("%s has no offensive strength\n", prland(&land));
1090 resupply_all(&land);
1091 putland(land.lnd_uid, &land);
1092 if (!has_supply(&land)) {
1093 pr("%s is out of supply, and cannot %s\n",
1094 prland(&land), att_mode[combat_mode]);
1097 if (def->type == EF_SHIP && first_time) {
1099 pr("You may board with a maximum of %d land units\n", maxland);
1101 pr("%s has a base %s value of %d\n",
1102 prland(&land), att_mode[combat_mode], att_val);
1103 if (land_answer[(int)land.lnd_army] != 'Y') {
1105 "%s with %s %s (%c %d%%) [ynYNq?] ",
1106 att_mode[combat_mode],
1109 land.lnd_army == ' ' ? '~' : land.lnd_army,
1111 land_answer[(int)land.lnd_army] =
1112 att_prompt(prompt, land.lnd_army);
1113 if (att_abort(combat_mode, off, def))
1115 if (land_answer[(int)land.lnd_army] != 'y' &&
1116 land_answer[(int)land.lnd_army] != 'Y')
1119 if (!(llp = (struct llist *)malloc(sizeof(struct llist)))) {
1120 logerror("Malloc failed in attack!\n");
1124 memset(llp, 0, sizeof(struct llist));
1125 emp_insque(&llp->queue, olist);
1126 llp->mobil = mobcost;
1127 if (!get_land(combat_mode, def, land.lnd_uid, llp, 0))
1129 if (lnd_spyval(&land) > *a_spyp)
1130 *a_spyp = lnd_spyval(&land);
1131 if (llp->lcp->l_flags & L_ENGINEER)
1133 if (def->type == EF_SHIP && ++count >= maxland)
1138 /* What's the offense or defense multiplier? */
1141 att_combat_eff(struct combat *com)
1147 if (com->type == EF_SECTOR) {
1148 eff = com->eff / 100.0;
1149 if (com->own == player->cnum) {
1150 str = com->sct_dcp->d_ostr;
1151 eff = 1.0 + ((str - 1.0) * eff);
1153 eff = sector_strength(getsectp(com->x, com->y));
1154 /* str = com->sct_dcp->d_dstr;*/
1155 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1156 getship(com->shp_uid, &ship);
1157 eff = (1.0 + ship.shp_armor / 100.0);
1163 * Estimate the defense strength and give the attacker a chance to abort
1164 * if the odds are less than 50%
1168 att_estimate_defense(int combat_mode, struct combat *off,
1169 struct emp_qelem *olist, struct combat *def,
1177 * Get the attacker units & mil again in case they changed while the
1178 * attacker was answering sub-prompts.
1181 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1182 if (att_empty_attack(combat_mode, ototal, def))
1183 return abort_attack();
1184 if (combat_mode == A_PARA)
1186 pr("\n Initial attack strength: %8d\n", ototal);
1188 estimate = att_combat_eff(def) * roundintby(def->troops, 10);
1189 estimate += att_combat_eff(def) * get_dlist(def, 0, a_spy, 0);
1192 * Calculate the initial (pre-support) attack odds. If they're less
1193 * than 50%, ask for a confirmation.
1196 odds = (int)(att_calcodds(ototal, estimate) * 100);
1200 pr(" Estimated defense strength: %8d\n", estimate);
1201 pr(" Estimated odds: %8d%%\n\n",odds);
1202 sprintf(prompt, "Are you sure you want to %s [yn]? ",
1203 att_mode[combat_mode]);
1204 if (!confirm(prompt))
1205 return abort_attack();
1206 ototal = get_ototal(combat_mode, off, olist,1.0,1);
1207 if (att_empty_attack(combat_mode, ototal, def))
1208 return abort_attack();
1214 /* Get the defensive units and reacting units */
1216 att_get_defense(struct emp_qelem *olist, struct combat *def,
1217 struct emp_qelem *dlist, int a_spy, int ototal)
1220 struct emp_qelem *qp;
1226 get_dlist(def, dlist, 0, &d_spy);
1227 dtotal = get_dtotal(def, dlist, 1.0, 0);
1230 * Call in reacting units
1233 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1234 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1236 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1237 llp = (struct llist *)qp;
1238 intelligence_report(def->own, &llp->land, d_spy,
1239 "Scouts report attacking unit:");
1242 old_dtotal = dtotal;
1243 dtotal = get_dtotal(def, dlist, 1.0, 0);
1244 if (dtotal != old_dtotal)
1245 pr("Defense strength with reacting units: %8d\n", dtotal);
1250 /* Get the defensive land units in the sector or on the ship */
1253 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1256 struct nstr_item ni;
1261 /* In here is where you need to take out spies and trains from the defending
1262 lists. Spies try to hide, trains get trapped and can be boarded. */
1264 snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1265 while (nxtitem(&ni, (s_char *)&land)) {
1268 if (land.lnd_own != def->own)
1270 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1272 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1274 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1276 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1278 if (!list) { /* Just estimating the enemy strength */
1279 estimate += intelligence_report(player->cnum,
1281 "Scouts report defending unit:");
1284 if (!(llp = (struct llist *)malloc(sizeof(struct llist)))) {
1285 logerror("Malloc failed in attack!\n");
1289 memset(llp, 0, sizeof(struct llist));
1290 emp_insque(&llp->queue, list);
1291 llp->supplied = has_supply(&land);
1292 if (!get_land(A_DEFEND, def, land.lnd_uid, llp, 1))
1294 if (lnd_spyval(&land) > *d_spyp)
1295 *d_spyp = lnd_spyval(&land);
1300 /* Calculate the total offensive strength */
1303 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1304 double osupport, int check)
1306 double ototal = 0.0;
1307 struct emp_qelem *qp, *next;
1312 * first, total the attacking mil
1315 for (n = 0; n <= off->last; ++n) {
1316 if (off[n].type == EF_BAD || (check &&
1317 att_get_combat(&off[n], 0) <= 0))
1319 ototal += off[n].troops * att_combat_eff(off + n);
1323 * next, add in the attack_values of all
1324 * the attacking units
1327 for (qp = olist->q_forw; qp != olist; qp = next) {
1329 llp = (struct llist *)qp;
1330 if (check && !get_land(combat_mode, 0, llp->land.lnd_uid, llp, 0))
1332 if (combat_mode == A_ATTACK) {
1334 for (n = 0; n <= off->last; ++n) {
1335 if (off[n].type == EF_BAD)
1337 if ((off[n].x == llp->land.lnd_x) &&
1338 (off[n].y == llp->land.lnd_y))
1342 lnd_delete(llp, "is in a sector not owned by you");
1345 ototal += attack_val(combat_mode, &llp->land) *
1346 att_combat_eff(off + w);
1348 ototal += attack_val(combat_mode, &llp->land);
1353 return ldround(ototal, 1);
1356 /* Calculate the total defensive strength */
1359 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1362 double dtotal = 0.0, eff = 1.0, d_unit;
1363 struct emp_qelem *qp, *next;
1366 if (check && att_get_combat(def, 1) < 0)
1368 eff = att_combat_eff(def);
1369 dtotal = def->troops * eff;
1372 * next, add in the defense_values of all
1373 * the defending non-retreating units
1376 for (qp = list->q_forw; qp != list; qp = next) {
1378 llp = (struct llist *)qp;
1380 if (check && !get_land(A_DEFEND, def, llp->land.lnd_uid, llp, 1))
1382 d_unit = defense_val(&llp->land);
1384 d_unit = ((double)defense_val(&llp->land) / 2.0);
1385 dtotal += d_unit * eff;
1390 return ldround(dtotal, 1);
1394 * This is the land unit integrity check. Note that we don't print
1395 * warnings about victim land units because the attacker may not have seen them
1399 get_land(int combat_mode, struct combat *def, int uid, struct llist *llp,
1402 struct lndstr *lp = &llp->land;
1407 if (!llp->lcp) { /* first time */
1408 llp->x = llp->land.lnd_x;
1409 llp->y = llp->land.lnd_y;
1410 llp->lcp = &lchr[(int)llp->land.lnd_type];
1411 } else { /* not first time */
1412 if (lp->lnd_effic < LAND_MINEFF) {
1413 sprintf(buf, "was destroyed and is no longer a part of the %s",
1414 att_mode[combat_mode]);
1415 lnd_delete(llp, buf);
1419 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1421 "left to go fight another battle and is no longer a part of the defense");
1425 if (lp->lnd_own != player->cnum) {
1427 "was destroyed and is no longer a part of the %s",
1428 att_mode[combat_mode]);
1429 lnd_delete(llp, buf);
1432 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1434 "left to fight another battle and is no longer a part of the %s",
1435 att_mode[combat_mode]);
1436 lnd_delete(llp, buf);
1439 if (lp->lnd_effic < llp->eff) {
1440 sprintf(buf, "damaged from %d%% to %d%%",
1441 llp->eff, lp->lnd_effic);
1442 lnd_print(llp, buf);
1446 llp->eff = llp->land.lnd_effic;
1452 * Put the land unit on the disk. If there was some mobility cost, then
1453 * subtract it from the units mobility. Note that this works the same way
1454 * as sectors & ships in that no mobility is actually taken until the attacker
1455 * has committed to attacking.
1459 kill_land(struct emp_qelem *list)
1461 struct emp_qelem *qp, *next;
1464 for (qp = list->q_forw; qp != list; qp = next) {
1466 llp = (struct llist *)qp;
1467 if (llp->land.lnd_ship >= 0) {
1468 llp->land.lnd_effic = 0;
1469 lnd_delete(llp, "cannot return to the ship, and dies!");
1475 att_infect_units(struct emp_qelem *list, int plague)
1477 struct emp_qelem *qp, *next;
1482 for (qp = list->q_forw; qp != list; qp = next) {
1484 llp = (struct llist *)qp;
1485 if (llp->land.lnd_pstage == PLG_HEALTHY)
1486 llp->land.lnd_pstage = PLG_EXPOSED;
1491 put_land(struct emp_qelem *list)
1493 struct emp_qelem *qp, *next;
1496 for (qp = list->q_forw; qp != list; qp = next) {
1498 llp = (struct llist *)qp;
1499 llp->land.lnd_mission = 0;
1500 llp->land.lnd_harden = 0;
1501 llp->land.lnd_mobil -= (int)llp->mobil;
1503 putland(llp->land.lnd_uid, &llp->land);
1504 if (llp->land.lnd_own != player->cnum) {
1505 emp_remque((struct emp_qelem *)llp);
1506 free((s_char *)llp);
1508 get_land(A_ATTACK, 0, llp->land.lnd_uid, llp, 0);
1513 * Keep sending in reinforcements until it looks like we're going to win.
1514 * Note that the "strength" command also calls this routine.
1518 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1519 int *d_spyp, int ototal)
1521 struct nstr_item ni;
1523 struct sctstr sect, dsect;
1533 double eff = att_combat_eff(def);
1539 * All units that are within their reaction radius and not damaged
1540 * below their morale value now get to react to the threatened sect.
1541 * Once we've sent enough to counter the threat, stop sending them.
1543 * Not anymore. All units get to react. :)
1547 dtotal = get_dtotal(def, list, 1.0, 1);
1550 snxtitem_all(&ni, EF_LAND);
1551 while (nxtitem(&ni, (s_char *)&land) &&
1552 (dtotal + new_land * eff < (int)(1.2 * (float)ototal))) {
1555 if (!land.lnd_rad_max)
1557 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1559 if (land.lnd_own != def->own)
1561 if (land.lnd_ship >= 0)
1563 if (!defense_val(&land))
1566 if (land.lnd_effic <= land.lnd_retreat)
1569 if (!lnd_can_attack(&land))
1572 /* Only supplied units can react */
1573 if (!(supply_state = has_supply(&land)))
1576 dist = mapdist(land.lnd_x, land.lnd_y, def->x, def->y);
1578 getsect(land.lnd_x, land.lnd_y, §);
1579 /* Units on efficient headquarters can react 1 farther */
1580 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
1581 radius = land.lnd_rad_max + 1;
1583 radius = land.lnd_rad_max;
1585 if (land.lnd_mission == MI_RESERVE)
1591 getsect(def->x, def->y, &dsect);
1592 if (!(p = BestLandPath(buf, §, &dsect, &move_cost, MOB_ROAD)))
1595 mobcost = land.lnd_effic * 0.01 * lchr[(int)land.lnd_type].l_spd;
1598 mobcost = 480.0 / (mobcost + techfact(land.lnd_tech, mobcost));
1599 mobcost *= (move_cost * 5.0);
1601 if (land.lnd_mobil < mobcost)
1604 new_land += defense_val(&land);
1606 if (!list) /* we are in the "strength" command */
1609 /* move to defending sector */
1610 land.lnd_mobil -= ldround(mobcost, 1);
1613 land.lnd_x = def->x;
1614 land.lnd_y = def->y;
1615 putland(land.lnd_uid, &land);
1616 wu(0, land.lnd_own, "%s reacts to %s.\n",
1617 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1619 llp = (struct llist *)
1620 malloc(sizeof(struct llist));
1622 memset(llp, 0, sizeof(struct llist));
1623 llp->supplied = supply_state;
1626 llp->lcp = &lchr[(int)land.lnd_type];
1628 emp_insque(&llp->queue, list);
1629 if (lnd_spyval(&land) > *d_spyp)
1630 *d_spyp = lnd_spyval(&land);
1632 intelligence_report(player->cnum, &land, a_spy,
1633 "Scouts sight reacting enemy unit:");
1638 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1641 get_osupport(s_char *outs, struct combat *def, int fort_sup, int ship_sup,
1642 int land_sup, int plane_sup)
1644 double osupport = 1.0;
1646 double af, as, au, ap;
1648 af = as = au = ap = 0.0;
1650 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1651 af = ((double)dam / 100.0);
1655 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1657 as = ((double)dam / 100.0);
1662 dam = lnd_support(def->own, player->cnum, def->x, def->y);
1663 au = ((double)dam / 100.0);
1668 dam = off_support(def->x, def->y, def->own, player->cnum);
1669 ap = (((double)dam) / 100.0);
1672 sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1677 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1680 get_dsupport(s_char *outs, struct emp_qelem *list, struct combat *def,
1681 int ototal, int dtotal)
1683 double dsupport = 1.0;
1685 double df, ds, du, dp;
1688 df = ds = du = dp = 0.0;
1689 if (dtotal < 0.1 * ototal) {
1691 } else if (dtotal >= 1.2 * ototal) {
1694 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1695 df = ((double)dam / 100.0);
1698 dtotal = get_dtotal(def, list, dsupport, 0);
1699 if (dtotal < 1.2 * ototal) {
1700 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1701 ds = ((double)dam / 100.0);
1703 dtotal = get_dtotal(def, list, dsupport, 0);
1705 if (dtotal < 1.2 * ototal) {
1706 dam = lnd_support(player->cnum, def->own, def->x, def->y);
1707 du = ((double)dam / 100.0);
1709 dtotal = get_dtotal(def, list, dsupport, 1);
1711 if (dtotal < 1.2 * ototal) {
1712 dam = def_support(def->x, def->y, player->cnum, def->own);
1713 dp = (((double)dam) / 100.0);
1720 sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1725 "\nOdds are bad for us...support cancelled.\n\n");
1728 "\nOdds are good for us...support cancelled.\n\n");
1734 * Land mines add to the defense multiplier. If the attacker has engineers
1735 * then this multiplier is cut in half.
1739 get_mine_dsupport(struct combat *def, int a_engineer)
1744 getsect(def->x, def->y, §);
1746 if (sect.sct_oldown != player->cnum) {
1747 mines = min(sect.sct_mines, 20);
1749 mines = ldround(((double)mines / 2.0), 1);
1752 wu(0, def->own, "Defending mines add %1.2f\n",
1754 pr("Defending mines add %1.2f\n", mines * 0.02);
1755 return mines * 0.02;
1761 /* Get the offensive and defensive support */
1763 att_get_support(int combat_mode, int ofort, int oship, int oland,
1764 int oplane, struct emp_qelem *olist, struct combat *off,
1765 struct emp_qelem *dlist, struct combat *def,
1766 double *osupportp, double *dsupportp, int a_engineer)
1769 s_char osupports[512];
1770 s_char dsupports[512];
1772 if (combat_mode == A_PARA)
1775 *osupportp = get_osupport(osupports, def,
1776 ofort, oship, oland, oplane);
1779 * I need to put a 1 at the end of the next four total_stren calls
1780 * becauase units & mil may have been damaged by collateral damage or
1781 * neclear warheads from the offensive & defensive support.
1784 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1785 if (att_empty_attack(combat_mode, ototal, def))
1786 return abort_attack();
1787 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1790 * Calculate defensive support. If odds are too good or too bad
1791 * then don't call in support.
1794 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1795 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1796 if (att_empty_attack(combat_mode, ototal, def))
1797 return abort_attack();
1799 if ((*osupports || *dsupports) &&
1800 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1801 pr("\n\t\tsupport values\n");
1802 pr("\t\tforts\tships\tunits\tplanes\n");
1803 if (*osupportp != 1.0)
1804 pr("%s", osupports);
1805 if (*dsupportp != 1.0)
1806 pr("%s", dsupports);
1808 wu(0, def->own, "\n\t\tsupport values\n");
1809 wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1810 if (*osupportp != 1.0)
1811 wu(0, def->own, "%s", osupports);
1812 if (*dsupportp != 1.0)
1813 wu(0, def->own, "%s", dsupports);
1817 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1818 if (dtotal && def->type == EF_SECTOR)
1819 *dsupportp += get_mine_dsupport(def, a_engineer);
1823 /* How many two-legged bipeds are in this combat force? */
1826 count_bodies(struct combat *off, struct emp_qelem *list)
1830 struct emp_qelem *qp;
1833 for (n = 0; n <= off->last; ++n)
1834 bodies += off[n].troops;
1835 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1836 llp = (struct llist *)qp;
1837 bodies += total_mil(&llp->land);
1842 /* This is where the fighting actually occurs. */
1845 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1846 double osupport, struct combat *def, struct emp_qelem *dlist,
1850 int a_cas = 0; /* Casualty counts */
1852 int ototal; /* total attacking strength */
1853 int dtotal; /* total defending strength */
1854 int a_bodies; /* total attacking mil (incl. mil in units) */
1855 int d_bodies; /* total defending mil (incl. mil in units) */
1864 ototal = get_ototal(combat_mode, off, olist, osupport,
1865 combat_mode != A_PARA);
1866 dtotal = get_dtotal(def, dlist, dsupport, 0);
1870 a_bodies = count_bodies(off, olist);
1871 d_bodies = count_bodies(def, dlist);
1872 d_mil = def->troops;
1873 for (n = 0; n <= off->last; ++n)
1874 if (off[n].type == EF_BAD)
1877 a_troops[n] = off[n].troops;
1879 /* This switch is required to get the spacing right */
1880 switch (combat_mode) {
1882 pr(" Final attack strength: %8d\n", ototal);
1885 pr(" Final assault strength: %8d\n", ototal);
1888 if (def->sct_type == SCT_MOUNT ||
1889 def->sct_type == SCT_WATER ||
1890 def->sct_type == SCT_CAPIT ||
1891 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1892 pr("You can't air-assault a %s sector!\n",
1893 def->sct_dcp->d_name);
1896 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1898 pr(" Final air-assault strength: %8d\n", ototal);
1902 pr(" Final board strength: %8d\n", ototal);
1906 pr(" Final defense strength: %8d\n", dtotal);
1907 odds = att_calcodds(ototal, dtotal);
1908 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1910 /* spread the plague */
1911 if (combat_mode != A_PARA) {
1913 for (n = 0; n <= off->last; ++n)
1914 if (off[n].type != EF_BAD)
1915 def->plague |= off[n].plague;
1916 for (n = 0; n <= off->last; ++n)
1917 if (off[n].type != EF_BAD)
1918 off[n].plague |= def->plague;
1920 att_infect_units(olist, off->plague);
1921 att_infect_units(dlist, def->plague);
1923 /* Fighting is slightly random. There is always that last little
1924 * effort you see people put in. Or the stray bullet that takes out
1925 * an officer and the rest go into chaos. Things like that.
1926 * Thus, we have added a very slight random factor that will sometimes
1927 * allow the little guy to win. We modify the odds a little
1928 * (either +- 5%) to account for this randomness. We also only
1929 * recalculate the odds every 8-50 casualties, not every cacsualty,
1930 * since a single dead guy normally wouldn't cause a commander to
1931 * rethink his strategies, but 50 dead guys might. */
1932 odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1937 recalctime = 8 + (random() % 43);
1938 while (!success && ototal) {
1941 d_cas += take_casualty(A_DEFEND, def, dlist);
1942 dtotal = get_dtotal(def, dlist, dsupport, 0);
1947 a_cas += take_casualty(combat_mode, off, olist);
1948 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1950 if (((a_cas + d_cas) % 70) == 69)
1952 if (recalctime-- <= 0) {
1953 recalctime = 8 + (random() % 43);
1954 odds = att_calcodds(ototal, dtotal);
1955 odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1963 /* update defense mobility & mil */
1967 if (def->type == EF_SECTOR && d_mil && d_cas) {
1970 /* Make sure we use a positive mobility here */
1971 tmob = ((def->mob < 0) ? -(def->mob) : (def->mob));
1973 min(20, min(1, tmob - damage(tmob, 100 * d_cas / d_mil)));
1975 def->mil = def->troops;
1978 /* update attack mobility & mil */
1979 for (n = 0; n <= off->last; ++n)
1980 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1981 if (off[n].type == EF_SECTOR && off[n].mil)
1985 off[n].mob - damage(off[n].mob,
1986 100 * (a_troops[n] -
1989 off[n].mil -= a_troops[n] - off[n].troops;
1992 /* update land unit mobility */
1993 if (d_bodies && d_cas)
1994 lnd_takemob(dlist, (double)d_cas / d_bodies);
1995 if (a_bodies && a_cas)
1996 lnd_takemob(olist, (double)a_cas / a_bodies);
1998 /* damage attacked sector */
1999 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
2001 pr("- Casualties -\n Yours: %d\n", a_cas);
2002 pr(" Theirs: %d\n", d_cas);
2003 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
2004 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
2007 switch (combat_mode) {
2009 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
2010 pr("We have captured %s, sir!\n", prcom(0, def));
2014 news_item = def->own ? N_AWON_SECT : N_START_COL;
2015 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
2016 action = "assaulting and taking";
2019 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
2020 pr("We have captured %s, sir!\n", prcom(0, def));
2021 action = "air-assaulting and taking";
2024 news_item = N_BOARD_SHIP;
2025 pr("We have boarded %s, sir!\n", prcom(0, def));
2026 action = "boarding";
2029 news_item = N_BOARD_LAND;
2030 pr("We have boarded %s, sir!\n", prcom(0, def));
2031 action = "boarding";
2035 switch (combat_mode) {
2037 news_item = N_SCT_LOSE;
2038 pr("You have been defeated!\n");
2039 action = "attacking";
2042 news_item = N_ALOSE_SCT;
2043 pr("You have been defeated!\n");
2045 action = "trying to assault";
2048 news_item = N_PLOSE_SCT;
2049 pr("All of your troops were destroyed\n");
2050 action = "trying to air-assault";
2053 news_item = N_SHP_LOSE;
2054 pr("You have been repelled\n");
2056 action = "trying to board";
2059 news_item = N_LND_LOSE;
2060 pr("You have been repelled\n");
2062 action = "trying to board";
2066 nreport(player->cnum, news_item, def->own, 1);
2069 "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
2070 cname(player->cnum), player->cnum, a_cas,
2071 action, pr_com(0, def, def->own), d_cas);
2074 if (opt_SNEAK_ATTACK) {
2075 do_sneak(def, success);
2078 send_reacting_units_home(dlist);
2080 /* putland the defending land */
2083 /* putland the attacking land */
2086 /* put the victim sector/ship/land */
2087 if (!success || !take_def(combat_mode, olist, off, def))
2090 /* put the attacking sectors/ship */
2091 for (n = 0; n <= off->last; ++n)
2092 if (off[n].type != EF_BAD)
2093 put_combat(&off[n]);
2098 switch (combat_mode) {
2100 ask_move_in(off, olist, def);
2102 /* put sectors again to get abandon warnings */
2103 for (n = 0; n <= off->last; ++n)
2104 if (off[n].type != EF_BAD)
2105 put_combat(&off[n]);
2108 att_move_in_off(combat_mode, off, olist, def);
2111 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2115 /* What percentage of the combat forces going head-to-head are we? */
2118 att_calcodds(int ototal, int dtotal)
2122 /* calculate odds */
2125 else if (dtotal <= 0)
2128 odds = ((double)ototal) / (dtotal + ototal);
2133 /* Here's where the dead soldiers get dragged off the battlefield */
2136 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2138 int to_take = CASUALTY_LUMP;
2139 int biggest_troops = 0, index = -1;
2140 int n, tot_troops = 0, biggest_mil, cas;
2141 struct emp_qelem *qp, *biggest;
2144 for (n = 0; n <= off->last; ++n) {
2145 if (off[n].type != EF_BAD) {
2146 tot_troops += off[n].troops;
2147 if (off[n].troops > biggest_troops) {
2148 biggest_troops = off[n].troops;
2155 to_take -= tot_troops;
2158 for (n = 0; n <= off->last; ++n)
2159 if (off[n].type != EF_BAD)
2163 * They can all come off mil. We rotate the casualties,
2164 * starting with the sector containing the most mil.
2166 to_take = CASUALTY_LUMP;
2168 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2171 while (to_take > 0) {
2172 for (n = index; n <= off->last && to_take; ++n) {
2173 if (off[n].type != EF_BAD && off[n].troops > 0) {
2178 for (n = 0; n < index && to_take; ++n) {
2179 if (off[n].type != EF_BAD && off[n].troops > 0) {
2185 return CASUALTY_LUMP;
2189 return (CASUALTY_LUMP - to_take);
2192 * Need to take some casualties from attacking units
2193 * Procedure: find the biggest unit remaining (in
2194 * terms of mil) and give it the casualties.
2196 biggest = (struct emp_qelem *)0;
2198 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2199 llp = (struct llist *)qp;
2201 if (total_mil(&llp->land) > biggest_mil) {
2202 biggest_mil = total_mil(&llp->land);
2206 if (biggest == (struct emp_qelem *)0)
2207 return (CASUALTY_LUMP - to_take);
2209 llp = (struct llist *)biggest;
2210 cas = lnd_take_casualty(combat_mode, llp, to_take);
2211 return (CASUALTY_LUMP - (to_take - cas));
2214 /* Send reacting defense units back to where they came from (at no mob cost) */
2217 send_reacting_units_home(struct emp_qelem *list)
2219 struct emp_qelem *qp, *next;
2223 for (qp = list->q_forw; qp != list; qp = next) {
2225 llp = (struct llist *)qp;
2226 if ((llp->land.lnd_x != llp->x) || (llp->land.lnd_y != llp->y)) {
2227 sprintf(buf, "returns to %s",
2228 xyas(llp->x, llp->y, llp->land.lnd_own));
2229 llp->land.lnd_x = llp->x;
2230 llp->land.lnd_y = llp->y;
2231 lnd_delete(llp, buf);
2236 /* Check for 0 offense strength. This call will always preceed an abort */
2239 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2242 if (def->own && player->cnum != def->own) {
2244 "%s (#%d) considered %sing you @%s\n",
2245 cname(player->cnum), player->cnum,
2246 att_mode[combat_mode], xyas(def->x, def->y, def->own));
2248 pr("No troops for %s...\n", att_mode[combat_mode]);
2255 * Take the defending sector or ship from the defender and give it to the
2260 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2265 struct llist *llp, *delete_me = 0;
2271 for (n = 0; n <= off->last && !occuppied; ++n) {
2272 if (off[n].type != EF_BAD &&
2273 off[n].troops > 0 &&
2274 (off[n].type != EF_SECTOR || off[n].mob)) {
2276 if (def->type == EF_LAND) {
2277 if (def->lnd_lcp->l_flags & L_SPY) {
2284 pr("1 mil from %s moves %s\n",
2285 prcom(0, off + n), prcom(2, def));
2290 pr("%s left unoccupied\n", prcom(0, def));
2293 "No enemy troops moved %s so you still own it!\n",
2294 pr_com(2, def, def->own));
2297 llp = (struct llist *)list->q_forw;
2298 llp->land.lnd_x = def->x;
2299 llp->land.lnd_y = def->y;
2300 take_move_in_mob(combat_mode, llp, off, def);
2301 if (def->type == EF_SHIP) {
2302 llp->land.lnd_ship = def->shp_uid;
2303 sprintf(buf, "boards %s", prcom(0, def));
2306 llp->land.lnd_ship = -1;
2307 sprintf(buf, "moves in to occupy %s",
2308 xyas(def->x, def->y, player->cnum));
2309 lnd_delete(llp, buf);
2314 if (def->type == EF_SECTOR) {
2315 getsect(def->x, def->y, §);
2316 takeover(§, player->cnum);
2317 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2318 caploss(§, def->own,
2319 "* We have captured %s's capital, sir! *\n");
2321 } else if (def->type == EF_SHIP) {
2322 getship(def->shp_uid, &ship);
2323 takeover_ship(&ship, player->cnum, 1);
2324 putship(ship.shp_uid, &ship);
2325 } else if (def->type == EF_LAND) {
2326 getland(def->lnd_uid, &land);
2327 takeover_land(&land, player->cnum, 1);
2328 putland(land.lnd_uid, &land);
2331 lnd_delete(delete_me, buf);
2332 att_get_combat(def, 0);
2337 * Ask the attacker which mil & land units they'd like to move into the
2342 ask_move_in(struct combat *off, struct emp_qelem *olist,
2346 struct emp_qelem *qp, *next;
2350 s_char land_answer[1024];
2353 for (n = 0; n <= off->last; ++n)
2354 if (off[n].type != EF_BAD && off[n].troops > 0)
2356 ask_move_in_off(&off[n], def);
2357 if (player->aborted)
2363 memset(land_answer, 0, sizeof(land_answer));
2364 for (qp = olist->q_forw; qp != olist; qp = next) {
2366 llp = (struct llist *)qp;
2367 answerp = &land_answer[(int)llp->land.lnd_army];
2368 if (player->aborted || att_get_combat(def, 0) < 0)
2370 if (*answerp == 'Y')
2372 if (*answerp != 'N') {
2373 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2375 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2377 llp->land.lnd_army == ' ' ? '~' : llp->land.lnd_army,
2378 llp->land.lnd_effic);
2379 *answerp = att_prompt(prompt, llp->land.lnd_army);
2380 if (player->aborted || att_get_combat(def, 0) < 0)
2382 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2385 if (*answerp == 'y' || *answerp == 'Y')
2387 sprintf(buf, "stays in %s",
2388 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2389 lnd_delete(llp, buf);
2393 if (att_get_combat(def, 0) < 0) {
2394 for (qp = olist->q_forw; qp != olist; qp = next) {
2396 llp = (struct llist *)qp;
2397 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2399 sprintf(buf, "stays in %s",
2400 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2401 lnd_delete(llp, buf);
2405 if (opt_INTERDICT_ATT)
2406 lnd_interdict(olist, def->x, def->y, player->cnum);
2407 move_in_land(A_ATTACK, off, olist, def);
2410 /* Move offensive land units to the conquered sector or ship */
2413 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2416 struct emp_qelem *qp, *next;
2422 for (qp = olist->q_forw; qp != olist; qp = next) {
2424 llp = (struct llist *)qp;
2425 if (!get_land(combat_mode, def, llp->land.lnd_uid, llp, 0))
2427 take_move_in_mob(combat_mode, llp, off, def);
2428 llp->land.lnd_x = def->x;
2429 llp->land.lnd_y = def->y;
2430 if (def->type == EF_SHIP)
2431 llp->land.lnd_ship = def->shp_uid;
2433 llp->land.lnd_ship = -1;
2437 if (def->type == EF_SECTOR) {
2438 if (opt_INTERDICT_ATT) {
2439 lnd_sweep(olist, 0, 0, def->own);
2440 lnd_check_mines(olist);
2442 sprintf(buf, "now occupies %s", prcom(0, def));
2444 sprintf(buf, "boards %s", prcom(0, def));
2448 for (qp = olist->q_forw; qp != olist; qp = next) {
2450 llp = (struct llist *)qp;
2451 lnd_print(llp, buf);
2459 * Move assaulting, paradropping, or boarding mil & units into def
2460 * If the mil are coming from a ship, then pack a lunch.
2464 att_move_in_off(int combat_mode, struct combat *off,
2465 struct emp_qelem *olist, struct combat *def)
2470 int defvec[I_MAX + 1];
2471 int shipvec[I_MAX + 1];
2473 int n, lunchbox = 0;
2476 move_in_land(combat_mode, off, olist, def);
2478 for (n = 0; n <= off->last; ++n) {
2479 if (off[n].type == EF_BAD || !off[n].troops)
2481 troops = off[n].troops;
2483 off[n].mil -= troops;
2485 put_combat(off + n);
2486 if (combat_mode == A_ASSAULT) {
2487 getship(off[n].shp_uid, &ship);
2488 getvec(VT_ITEM, shipvec, (s_char *)&ship, EF_SHIP);
2489 lunchbox += (int)((troops + 1) * shipvec[I_FOOD] /
2490 (shipvec[I_MILIT] + troops +
2491 shipvec[I_CIVIL] + 0.5));
2492 shipvec[I_FOOD] -= lunchbox;
2493 putvec(VT_ITEM, shipvec, (s_char *)&ship, EF_SHIP);
2494 putship(ship.shp_uid, &ship);
2501 if (def->type == EF_SECTOR) {
2502 getsect(def->x, def->y, §);
2503 thing = (s_char *)§
2504 } else if (def->type == EF_SHIP) {
2505 getship(def->shp_uid, &ship);
2506 thing = (s_char *)&ship;
2507 } else if (def->type == EF_LAND) {
2508 getship(def->lnd_uid, &land);
2509 thing = (s_char *)&land;
2511 pr("Please tell the deity that you got the 'hungry mole' error\n");
2514 getvec(VT_ITEM, defvec, thing, def->type);
2515 defvec[I_FOOD] += lunchbox;
2516 putvec(VT_ITEM, defvec, thing, def->type);
2517 if (def->type == EF_SECTOR)
2519 else if (def->type == EF_SHIP)
2520 putship(ship.shp_uid, &ship);
2522 putland(land.lnd_uid, &land);
2526 /* Ask how many mil to move in from each sector */
2529 ask_move_in_off(struct combat *off, struct combat *def)
2532 int num_mil, dam = 0, left;
2538 if (att_get_combat(off, 0) <= 0)
2540 if (att_get_combat(def, 0) < 0)
2542 if (off->own != player->cnum)
2544 d = sector_mcost(getsectp(def->x, def->y), MOB_ROAD);
2545 if ((mob_support = min(off->troops, (int)(off->mob / d))) <= 0)
2547 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2548 xyas(off->x, off->y, player->cnum), mob_support);
2549 if (!(p = getstring(prompt, buf)) || !*p || (num_mil = atoi(p)) <= 0) {
2553 /* Make sure we don't move in more than we can support mobility-wise */
2554 if (num_mil > mob_support)
2555 num_mil = mob_support;
2556 if (att_get_combat(off, 0) <= 0)
2558 if (att_get_combat(def, 0) < 0)
2560 if ((num_mil = min(off->troops, num_mil)) <= 0) {
2561 pr("No mil moved in from %s\n",
2562 xyas(off->x, off->y, player->cnum));
2565 mob_support = max(1, (int)(num_mil * d));
2566 off->mob -= min(off->mob, mob_support);
2567 off->mil -= num_mil;
2568 off->troops -= num_mil;
2571 weight = num_mil * ichr[I_MILIT].i_lbs;
2572 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2573 if (chance(weight / 100.0))
2575 ground_interdict(def->x, def->y, player->cnum, "military");
2576 dam += check_lmines(def->x, def->y, weight);
2580 left = commdamage(num_mil, dam, V_MILIT);
2581 if (left < num_mil) {
2583 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));
2585 pr("All of the mil you were moving were destroyed!\n");
2588 /* maybe got nuked */
2589 if (att_get_combat(def, 0) < 0)
2597 /* Charge land units for moving into a sector or onto a ship */
2600 take_move_in_mob(int combat_mode, struct llist *llp, struct combat *off,
2606 switch (combat_mode) {
2609 lnd_mobcost(&llp->land, getsectp(def->x, def->y), MOB_NONE);
2610 new = llp->land.lnd_mobil - mobcost;
2613 llp->land.lnd_mobil = new;
2616 if (off->shp_mcp->m_flags & M_LAND) {
2617 if (llp->lcp->l_flags & L_MARINE)
2618 llp->land.lnd_mobil -=
2619 ((float)etu_per_update * land_mob_scale * 0.5);
2621 llp->land.lnd_mobil -= ((float)etu_per_update
2624 if (llp->lcp->l_flags & L_MARINE)
2625 llp->land.lnd_mobil = 0;
2627 llp->land.lnd_mobil = (((float)etu_per_update
2628 * land_mob_scale) * (-1));
2632 /* I arbitrarily chose the numbers 10 and 40 below -KHS */
2633 if (llp->lcp->l_flags & L_MARINE)
2634 llp->land.lnd_mobil -= 10;
2636 llp->land.lnd_mobil -= 40;
2639 llp->land.lnd_harden = 0;
2643 free_list(struct emp_qelem *list)
2645 register struct emp_qelem *qp, *next;
2647 if (!list || QEMPTY(list))
2651 while (qp != list) {
2660 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2668 * sector_strength - Everyone starts at 1. You can get up to a max
2669 * of d_dstr, depending on how much you build up the
2670 * defenses of the sector.
2674 sector_strength(struct sctstr *sp)
2680 if (sp->sct_type == SCT_MOUNT)
2683 d = d + ((double)(dchr[sp->sct_type].d_dstr - d) *
2684 ((double)sp->sct_defense / 100.0));
2686 if (d > dchr[sp->sct_type].d_dstr)
2687 d = dchr[sp->sct_type].d_dstr;