2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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)
215 if (!getsect(com->x, com->y, §)) {
216 pr("Bad sector: %s\n", xyas(com->x, com->y, player->cnum));
217 return att_combat_init(com, EF_BAD);
219 com->sct_type = sect.sct_type;
220 com->sct_dcp = &dchr[sect.sct_type];
221 mil = sect.sct_item[I_MILIT];
222 pstage = sect.sct_pstage;
223 owner = sect.sct_own;
224 eff = sect.sct_effic;
225 mob = sect.sct_mobil;
230 if (!getland(com->lnd_uid, &land)) {
232 pr("Land unit #%d is not in the same sector!\n",
234 return att_combat_init(com, EF_BAD);
236 if (isdef && player->owner) {
237 pr("Boarding yourself? Try using the 'load' command.\n");
238 return att_combat_init(com, EF_BAD);
240 com->lnd_lcp = &lchr[(int)land.lnd_type];
241 mil = land.lnd_item[I_MILIT];
242 pstage = land.lnd_pstage;
243 owner = land.lnd_own;
244 eff = land.lnd_effic;
245 mob = land.lnd_mobil;
250 if (!getship(com->shp_uid, &ship) || !ship.shp_own) {
252 pr("Ship #%d is not in the same sector!\n", com->shp_uid);
254 pr("Ship #%d is not your ship!\n", com->shp_uid);
255 return att_combat_init(com, EF_BAD);
258 if (isdef && player->owner &&
259 ontradingblock(EF_SHIP, (int *)&ship)) {
260 pr("%s is on the trading block.\n", prcom(0, com));
261 return att_combat_init(com, EF_BAD);
264 if (isdef && player->owner) {
265 pr("Boarding yourself? Try using the 'tend' command.\n");
266 return att_combat_init(com, EF_BAD);
268 com->shp_mcp = &mchr[(int)ship.shp_type];
270 strncpy(com->shp_name, ship.shp_name, MAXSHPNAMLEN);
271 if (!isdef && !player->owner) {
273 pr("%s was just sunk!\n", prcom(0, com));
275 pr("Ship #%d is not your ship!\n", com->shp_uid);
276 return att_combat_init(com, EF_BAD);
278 mil = ship.shp_item[I_MILIT];
279 pstage = ship.shp_pstage;
280 owner = ship.shp_own;
281 eff = ship.shp_effic;
282 mob = ship.shp_mobil;
291 return att_combat_init(com, EF_BAD);
294 if (!com->set) { /* first time */
295 if (isdef) { /* defender */
297 } else { /* attacker */
299 pr("No mil %s\n", prcom(1, com));
301 pr("Only 1 mil %s\n", prcom(1, com));
302 /* don't abandon attacking sectors or ships */
303 com->troops = max(0, mil - 1);
305 com->plague = pstage == PLG_INFECT;
306 } else { /* not first time */
307 if (isdef) { /* defender */
308 if (com->x != x || com->y != y) {
309 pr("%s has moved!\n", prcom(0, com));
310 return att_combat_init(com, EF_BAD);
312 if (owner != com->own) {
314 pr("WARNING: The ownership of %s just changed from %s to %s!\n", prcom(0, com), cname(com->own), cname(owner));
315 } else if (com->type == EF_SECTOR) {
316 pr("WARNING: %s just abandoned sector %s!\n",
317 cname(com->own), xyas(com->x, com->y,
322 pr("WARNING: The enemy mil %s just %s from %d to %d!\n",
324 com->mil < mil ? "increased" : "decreased", com->mil,
327 } else { /* attacker */
328 if (owner != player->cnum && getrel(getnatp(owner), player->cnum) != ALLIED) { /* must be EF_SECTOR */
330 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));
332 pr("You no longer own %s\n",
333 xyas(com->x, com->y, player->cnum));
334 return att_combat_init(com, EF_BAD);
336 if (com->troops && com->troops + 1 > mil) {
337 if (com->own == owner && player->cnum == owner) /* not a takeover */
338 pr("WARNING: Your mil %s has been reduced from %d to %d!\n", prcom(1, com), com->troops, max(0, mil - 1));
339 com->troops = max(0, mil - 1);
354 * In the course of the fight, the combat object may have lost mil, eff, or
355 * mobility. This is the place where the data in the object gets flushed to
356 * disk to make it "real".
360 put_combat(struct combat *com)
369 getsect(com->x, com->y, §);
370 sect.sct_type = com->sct_type;
371 deff = sect.sct_effic - com->eff;
373 sect.sct_road -= (sect.sct_road * deff / 100.0);
374 sect.sct_rail -= (sect.sct_rail * deff / 100.0);
375 sect.sct_defense -= (sect.sct_defense * deff / 100.0);
376 if (sect.sct_road <= 0)
378 if (sect.sct_rail <= 0)
380 if (sect.sct_defense <= 0)
381 sect.sct_defense = 0;
383 sect.sct_effic = com->eff;
384 if (!opt_DEFENSE_INFRA)
385 sect.sct_defense = sect.sct_effic;
387 if (opt_MOB_ACCESS) {
388 if ((com->mob - com->mobcost) < -127)
389 sect.sct_mobil = -127;
391 sect.sct_mobil = (short)(com->mob - com->mobcost);
393 if ((com->mob - com->mobcost) < 0)
396 sect.sct_mobil = (short)(com->mob - com->mobcost);
399 makelost(EF_SECTOR, sect.sct_own, 0, sect.sct_x, sect.sct_y);
400 makenotlost(EF_SECTOR, com->own, 0, sect.sct_x, sect.sct_y);
401 sect.sct_own = com->own;
403 if (sect.sct_pstage == PLG_HEALTHY)
404 sect.sct_pstage = PLG_EXPOSED;
406 sect.sct_item[I_MILIT] = com->mil;
408 com->own = sect.sct_own; /* avoid WARNING if sector reverts */
411 getland(com->lnd_uid, &land);
412 land.lnd_effic = com->eff;
414 if (com->mob - com->mobcost < -127)
415 land.lnd_mobil = -127;
417 land.lnd_mobil = (s_char)(com->mob - com->mobcost);
419 makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
421 land.lnd_own = com->own;
422 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
425 if (land.lnd_pstage == PLG_HEALTHY)
426 land.lnd_pstage = PLG_EXPOSED;
428 if (!(com->lnd_lcp->l_flags & L_SPY))
429 land.lnd_item[I_MILIT] = com->mil;
430 lnd_count_units(&land);
431 if (com->own == player->cnum) {
432 land.lnd_mission = 0;
434 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
436 putland(com->lnd_uid, &land);
439 getship(com->shp_uid, &ship);
440 ship.shp_effic = com->eff;
442 if (com->mob - com->mobcost < -127)
443 ship.shp_mobil = -127;
445 ship.shp_mobil = (s_char)(com->mob - com->mobcost);
447 makelost(EF_SHIP, ship.shp_own, ship.shp_uid, ship.shp_x,
449 ship.shp_own = com->own;
450 makenotlost(EF_SHIP, ship.shp_own, ship.shp_uid, ship.shp_x,
453 if (ship.shp_pstage == PLG_HEALTHY)
454 ship.shp_pstage = PLG_EXPOSED;
456 ship.shp_item[I_MILIT] = com->mil;
458 if (com->own == player->cnum) {
459 ship.shp_mission = 0;
461 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
463 putship(com->shp_uid, &ship);
466 att_get_combat(com, com->own != player->cnum);
469 /* If pre-attack, abort fight. If post-attack, don't move anything in */
474 return player->aborted = 1;
478 * This is the combat_mode based integrity check. It splits among two main
479 * divisions: first time/not first time, and attack/assault/para/board.
483 att_abort(int combat_mode, struct combat *off, struct combat *def)
492 if (att_get_combat(def, 1) < 0)
493 return abort_attack();
495 if (off && combat_mode != A_ATTACK) {
496 if (att_get_combat(off, 0) < 0)
497 return abort_attack();
498 if (off->type == EF_SHIP &&
499 (!getsect(off->x, off->y, §) ||
500 sect.sct_type != SCT_WATER)) {
501 pr("%s can not %s from that far inland!\n",
502 prcom(0, off), att_mode[combat_mode]);
503 return abort_attack();
506 switch (combat_mode) {
508 if (!neigh(def->x, def->y, player->cnum) &&
509 !adj_units(def->x, def->y, player->cnum)) {
510 pr("You are not adjacent to %s\n",
511 xyas(def->x, def->y, player->cnum));
512 return abort_attack();
514 if (def->own == player->cnum) {
515 pr("You can't attack your own sector.\n");
516 return abort_attack();
520 if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
521 pr("You'll have to get there first...\n");
522 return abort_attack();
524 if (off && def->sct_type == SCT_MOUNT) {
525 pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
526 return abort_attack();
530 if (def->own == player->cnum) {
531 pr("You can't air-assault your own sector.\n");
532 return abort_attack();
534 if (off && (def->sct_type == SCT_MOUNT ||
535 def->sct_type == SCT_WATER ||
536 def->sct_type == SCT_CAPIT ||
537 def->sct_type == SCT_FORTR ||
538 def->sct_type == SCT_WASTE)) {
539 pr("You can't air-assault a %s sector!\n",
540 def->sct_dcp->d_name);
541 return abort_attack();
545 return board_abort(off, def);
547 return land_board_abort(off, def);
550 if (off && def->sct_dcp->d_mcst <= 0) {
551 pr("You can't %s a %s sector!\n", att_mode[combat_mode],
552 def->sct_dcp->d_name);
553 return abort_attack();
555 if (!off || off->relations_checked)
557 off->relations_checked = 1;
560 setcont(player->cnum, def->own, FOUND_SPY);
561 setcont(def->own, player->cnum, FOUND_SPY);
563 if (opt_SLOW_WAR && def->own != player->cnum) {
564 natp = getnatp(player->cnum);
565 issneak = getrel(natp, def->own);
567 if (issneak == ALLIED) {
568 sprintf(y_or_n, "Sector is owned by %s, your ally, %s [yn]? ",
569 cname(def->own), att_mode[combat_mode]);
570 if (!confirm(y_or_n))
571 return abort_attack();
574 if (opt_SNEAK_ATTACK) {
575 getsect(def->x, def->y, §);
576 if ((issneak != AT_WAR) && (def->own)
577 && (def->own != player->cnum)
578 && (sect.sct_oldown != player->cnum)
579 && (issneak != SITZKRIEG) && (issneak != MOBILIZATION)) {
580 pr("You're not at war with them!\n");
581 if (!confirm("Do you really want to sneak attack (it will cost you $5000) [yn]? ")) {
582 pr("Sneak attack cancelled!\n");
583 return abort_attack();
586 if ((issneak != AT_WAR) && (def->own)
587 && (def->own != player->cnum)
588 && (sect.sct_oldown != player->cnum)
589 && ((issneak == MOBILIZATION) || (issneak == SITZKRIEG))) {
590 pr("You're not at war with them!\n");
591 return abort_attack();
594 if ((issneak != AT_WAR) && (def->own) &&
595 (sect.sct_oldown != player->cnum)) {
596 pr("You're not at war with them!\n");
597 return abort_attack();
605 * Lots of special things need to be checked for boarding, so I put it in
610 board_abort(struct combat *off, struct combat *def)
612 struct shpstr aship, dship; /* for tech levels */
615 if (att_get_combat(def, 1) < 0)
616 return abort_attack();
621 if (att_get_combat(off, 0) < 0)
622 return abort_attack();
624 if (off->x != def->x || off->y != def->y) {
625 pr("Ship #%d is not in the same sector!\n", def->shp_uid);
626 return abort_attack();
628 if (off->type == EF_SHIP) {
630 pr("%s has no mobility!\n", prcom(0, off));
631 return abort_attack();
633 getship(off->shp_uid, &aship);
634 getship(def->shp_uid, &dship);
635 if (techfact(aship.shp_tech, 1.0) * aship.shp_speed * off->eff
636 <= techfact(dship.shp_tech, 1.0) * dship.shp_speed * def->eff) {
637 pr("Victim ship moves faster than you do!\n");
640 "%s (#%d) %s failed to catch %s\n",
641 cname(aship.shp_own), aship.shp_own,
642 pr_com(0, off, def->own), pr_com(0, def, def->own));
643 return abort_attack();
645 } else if (off->type != EF_SECTOR) {
646 pr("Please tell the deity that you got the 'banana boat' error\n");
647 return abort_attack();
649 if (def->shp_mcp->m_flags & M_SUB) {
650 getsect(def->x, def->y, §);
651 if (sect.sct_type == SCT_WATER) {
652 pr("You can't board a submarine!\n");
653 return abort_attack();
660 * Lots of special things need to be checked for boarding, so I put it in
662 * STM - I copied it for land unit boarding. :)
666 land_board_abort(struct combat *off, struct combat *def)
668 if (att_get_combat(def, 1) < 0)
669 return abort_attack();
674 if (att_get_combat(off, 0) < 0)
675 return abort_attack();
677 if (off->x != def->x || off->y != def->y) {
678 pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
679 return abort_attack();
685 /* If we are boarding, then the defending ship gets a chance to fire back */
687 att_approach(struct combat *off, struct combat *def)
693 pr("Approaching %s...\n", prcom(0, def));
696 "%s is being approached by %s...\n",
697 pr_com(0, def, def->own), pr_com(0, off, def->own));
698 if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
701 pr("They're firing at us sir!\n");
704 "Your fleet at %s does %d damage to %s\n",
705 xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
707 if (off->type == EF_SECTOR) {
708 getsect(off->x, off->y, §);
709 sectdamage(§, dam, 0);
711 pr("Enemy fleet at %s does %d damage to %s\n",
712 xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
713 } else if (off->type == EF_SHIP) {
714 getship(off->shp_uid, &ship);
715 shipdamage(&ship, dam);
716 putship(off->shp_uid, &ship);
717 if (def->own && ship.shp_effic < SHIP_MINEFF) {
718 wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
719 nreport(player->cnum, N_SHP_LOSE, def->own, 1);
722 if (att_get_combat(off, 0) < 0)
723 return abort_attack();
727 /* The attack is valid. Tell the attacker about what they're going to hit */
730 att_show(struct combat *def)
732 /* Note that we tell the player about the treaty BEFORE we tell them
733 about the item. If we didn't, then they gain free information */
734 if (def->type == EF_SECTOR) {
735 if (!trechk(player->cnum, def->own, LANATT))
736 return abort_attack();
737 pr("%s is a %d%% %s %s with approximately %d military.\n",
738 xyas(def->x, def->y, player->cnum),
739 roundintby((int)def->eff, 10),
740 cname(def->own), def->sct_dcp->d_name,
741 roundintby(def->troops, 10));
742 if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
743 writemap(player->cnum);
744 } else if (def->type == EF_SHIP || def->type == EF_LAND) {
745 if (def->type == EF_SHIP) {
746 if (!trechk(player->cnum, def->own, SEAATT))
747 return abort_attack();
749 if (!trechk(player->cnum, def->own, LNDATT))
750 return abort_attack();
752 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));
754 /* Ok, everything is fine */
758 /* Attack and assault ask the user which kind of support they want */
761 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
766 *fortp = *shipp = *landp = *planep = 1;
768 if (player->argp[offset] != (s_char *)0) {
769 if ((player->argp[offset + 1] == (s_char *)0) ||
770 (player->argp[offset + 2] == (s_char *)0) ||
771 (player->argp[offset + 3] == (s_char *)0)) {
772 pr("If any support arguments are used, all must be!\n");
777 *landp = *planep = 0;
779 if (!(p = getstarg(player->argp[offset], "Use fort support? ",
783 if ((*p == 'y') || (*p == 'Y'))
786 if (!(p = getstarg(player->argp[offset + 1], "Use ship support? ",
790 if ((*p == 'y') || (*p == 'Y'))
793 if (!(p = getstarg(player->argp[offset + 2], "Use land support? ",
797 if ((*p == 'y') || (*p == 'Y'))
800 if (!(p = getstarg(player->argp[offset + 3], "Use plane support? ",
804 if ((*p == 'y') || (*p == 'Y'))
811 * Attack, assault, and board ask the attacker what they'd like to attack
812 * with. This includes mil and land units from each "off" object. Note that
813 * after each sub-prompt, we check to make sure that the attack is still
814 * valid, and if it's not, then we abort the attack.
818 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
819 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
822 s_char land_answer[1024];
825 if (att_abort(combat_mode, off, def))
827 memset(land_answer, 0, sizeof(land_answer));
828 for (n = 0; n <= off->last; ++n) {
829 off[n].troops = ask_off(combat_mode, off + n, def);
830 if (att_abort(combat_mode, off, def))
832 ask_olist(combat_mode, off + n, def, olist, land_answer,
833 a_spyp, a_engineerp);
834 if (att_abort(combat_mode, off, def))
840 /* How many mil is off allowed to attack with when it attacks def? */
843 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
847 switch (combat_mode) {
849 mob_support = off->mob / sector_mcost(getsectp(def->x, def->y),
853 /* mob_support = off->mob / sector_mcost(def->sct_type, def->eff);*/
854 if (mob_support < off->troops)
855 pr("Sector %s has %d mobility which can only support %d mil,\n", xyas(off->x, off->y, player->cnum), off->mob, mob_support);
857 mob_support = off->troops;
860 if (def->own != player->cnum && def->mil) {
861 if (off->shp_mcp->m_flags & M_SEMILAND)
862 return off->troops / 4;
863 else if (!(off->shp_mcp->m_flags & M_LAND))
864 return off->troops / 10;
868 if (off->type == EF_SECTOR && off->mob <= 0)
870 mob_support = def->shp_mcp->m_item[I_MILIT];
871 if (mob_support < off->troops)
872 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
874 mob_support = off->troops;
879 if (def->lnd_lcp->l_flags & L_SPY)
881 mob_support = def->lnd_lcp->l_item[I_MILIT];
882 if (mob_support < off->troops)
883 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
885 mob_support = off->troops;
892 * If the attacker decides to go through with the attack, then the
893 * sectors/ships they are attacking with may be charged some mobility.
894 * This is where that amount of mobility is calculated. It is actually
895 * subtracted "for real" from the object's mobility in put_combat().
899 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
906 switch (combat_mode) {
910 (int)(attacking_mil *
911 sector_mcost(getsectp(def->x, def->y), MOB_ROAD)));
914 off->mobcost += max(1, attacking_mil / 5);
919 off->mobcost += max(1, attacking_mil / 5);
922 /* the 2 in the formula below is a fudge factor */
923 getship(def->shp_uid, &ship);
924 off->mobcost += (def->eff / 100) * (ship.shp_speed / 2);
929 /* How many mil to we want to attack from off against def? */
932 ask_off(int combat_mode, struct combat *off, struct combat *def)
938 if (att_get_combat(off, 0) <= 0)
940 if ((off->type == EF_SECTOR) && (off->own != player->cnum))
942 if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
944 if (off->type == EF_SECTOR) {
945 if (off->own != player->cnum)
947 sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
948 off->sct_dcp->d_name,
949 xyas(off->x, off->y, player->cnum), mob_support);
951 sprintf(prompt, "Number of mil from %s (max %d) : ",
952 prcom(0, off), mob_support);
954 if ((attacking_mil = onearg(0, prompt)) < 0)
956 if (att_abort(combat_mode, off, def))
958 if (att_get_combat(off, 0) <= 0)
961 min(attacking_mil, min(mob_support, off->troops))) <= 0)
964 calc_mobcost(combat_mode, off, def, attacking_mil);
965 return attacking_mil;
969 * Which units would you like to attack with or move in with [ynYNq?]
973 att_prompt(s_char *prompt, s_char army)
981 while (!p || (*p != 'y' && *p != 'n' && *p != 'Y' && *p != 'N')) {
982 if (p && *p == 'q') {
989 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);
990 p = getstring(prompt, buf);
991 if (player->aborted) {
999 /* Ask the attacker which units they want to attack/assault/board with */
1002 ask_olist(int combat_mode, struct combat *off, struct combat *def,
1003 struct emp_qelem *olist, s_char *land_answer, int *a_spyp,
1006 struct nstr_item ni;
1010 struct lchrstr *lcp;
1017 if (def->type == EF_LAND)
1019 if (def->type == EF_SHIP)
1020 maxland = def->shp_mcp->m_nland;
1022 snxtitem_xy(&ni, EF_LAND, off->x, off->y);
1023 while (nxtitem(&ni, &land)) {
1024 if (land.lnd_own != player->cnum)
1026 if (land.lnd_effic < LAND_MINEFF)
1028 if (land_answer[(int)land.lnd_army] == 'N')
1030 if (!lnd_can_attack(&land))
1032 lcp = &lchr[(int)land.lnd_type];
1034 if (def->type == EF_SHIP && !maxland) {
1035 pr("Land units are not able to board this kind of ship\n");
1038 if (land.lnd_mobil <= 0) {
1039 pr("%s is out of mobility, and cannot %s\n",
1040 prland(&land), att_mode[combat_mode]);
1045 if (ontradingblock(EF_LAND, (int *)&land)) {
1046 pr("%s is on the trading block, and cannot %s\n",
1047 prland(&land), att_mode[combat_mode]);
1052 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
1053 pr("%s is on ship #%d, and cannot %s\n",
1054 prland(&land), land.lnd_ship, att_mode[combat_mode]);
1056 } else if (off->type == EF_SHIP) {
1057 if (land.lnd_ship != off->shp_uid)
1059 } else if (land.lnd_land >= 0) {
1060 pr("%s is on unit #%d, and cannot %s\n",
1061 prland(&land), land.lnd_land, att_mode[combat_mode]);
1064 switch (combat_mode) {
1067 lnd_mobcost(&land, getsectp(def->x, def->y), MOB_NONE);
1068 if (land.lnd_mobil < mobcost) {
1069 pr("%s does not have enough mobility (%d needed)\n",
1070 prland(&land), (int)ceil(mobcost));
1077 if (!(lcp->l_flags & L_ASSAULT))
1080 att_val = attack_val(combat_mode, &land);
1082 pr("%s has no offensive strength\n", prland(&land));
1085 resupply_all(&land);
1086 putland(land.lnd_uid, &land);
1087 if (!has_supply(&land)) {
1088 pr("%s is out of supply, and cannot %s\n",
1089 prland(&land), att_mode[combat_mode]);
1092 if (def->type == EF_SHIP && first_time) {
1094 pr("You may board with a maximum of %d land units\n", maxland);
1096 pr("%s has a base %s value of %d\n",
1097 prland(&land), att_mode[combat_mode], att_val);
1098 if (land_answer[(int)land.lnd_army] != 'Y') {
1100 "%s with %s %s (%c %d%%) [ynYNq?] ",
1101 att_mode[combat_mode],
1104 land.lnd_army == ' ' ? '~' : land.lnd_army,
1106 land_answer[(int)land.lnd_army] =
1107 att_prompt(prompt, land.lnd_army);
1108 if (att_abort(combat_mode, off, def))
1110 if (land_answer[(int)land.lnd_army] != 'y' &&
1111 land_answer[(int)land.lnd_army] != 'Y')
1114 if (!(llp = malloc(sizeof(struct llist)))) {
1115 logerror("Malloc failed in attack!\n");
1119 memset(llp, 0, sizeof(struct llist));
1120 emp_insque(&llp->queue, olist);
1121 llp->mobil = mobcost;
1122 if (!get_land(combat_mode, def, land.lnd_uid, llp, 0))
1124 if (lnd_spyval(&land) > *a_spyp)
1125 *a_spyp = lnd_spyval(&land);
1126 if (llp->lcp->l_flags & L_ENGINEER)
1128 if (def->type == EF_SHIP && ++count >= maxland)
1133 /* What's the offense or defense multiplier? */
1136 att_combat_eff(struct combat *com)
1142 if (com->type == EF_SECTOR) {
1143 eff = com->eff / 100.0;
1144 if (com->own == player->cnum) {
1145 str = com->sct_dcp->d_ostr;
1146 eff = 1.0 + ((str - 1.0) * eff);
1148 eff = sector_strength(getsectp(com->x, com->y));
1149 /* str = com->sct_dcp->d_dstr;*/
1150 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1151 getship(com->shp_uid, &ship);
1152 eff = (1.0 + ship.shp_armor / 100.0);
1158 * Estimate the defense strength and give the attacker a chance to abort
1159 * if the odds are less than 50%
1163 att_estimate_defense(int combat_mode, struct combat *off,
1164 struct emp_qelem *olist, struct combat *def,
1172 * Get the attacker units & mil again in case they changed while the
1173 * attacker was answering sub-prompts.
1176 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1177 if (att_empty_attack(combat_mode, ototal, def))
1178 return abort_attack();
1179 if (combat_mode == A_PARA)
1181 pr("\n Initial attack strength: %8d\n", ototal);
1183 estimate = att_combat_eff(def) * roundintby(def->troops, 10);
1184 estimate += att_combat_eff(def) * get_dlist(def, 0, a_spy, 0);
1187 * Calculate the initial (pre-support) attack odds. If they're less
1188 * than 50%, ask for a confirmation.
1191 odds = (int)(att_calcodds(ototal, estimate) * 100);
1195 pr(" Estimated defense strength: %8d\n", estimate);
1196 pr(" Estimated odds: %8d%%\n\n",odds);
1197 sprintf(prompt, "Are you sure you want to %s [yn]? ",
1198 att_mode[combat_mode]);
1199 if (!confirm(prompt))
1200 return abort_attack();
1201 ototal = get_ototal(combat_mode, off, olist,1.0,1);
1202 if (att_empty_attack(combat_mode, ototal, def))
1203 return abort_attack();
1209 /* Get the defensive units and reacting units */
1211 att_get_defense(struct emp_qelem *olist, struct combat *def,
1212 struct emp_qelem *dlist, int a_spy, int ototal)
1215 struct emp_qelem *qp;
1221 get_dlist(def, dlist, 0, &d_spy);
1222 dtotal = get_dtotal(def, dlist, 1.0, 0);
1225 * Call in reacting units
1228 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1229 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1231 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1232 llp = (struct llist *)qp;
1233 intelligence_report(def->own, &llp->land, d_spy,
1234 "Scouts report attacking unit:");
1237 old_dtotal = dtotal;
1238 dtotal = get_dtotal(def, dlist, 1.0, 0);
1239 if (dtotal != old_dtotal)
1240 pr("Defense strength with reacting units: %8d\n", dtotal);
1245 /* Get the defensive land units in the sector or on the ship */
1248 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1251 struct nstr_item ni;
1256 /* In here is where you need to take out spies and trains from the defending
1257 lists. Spies try to hide, trains get trapped and can be boarded. */
1259 snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1260 while (nxtitem(&ni, &land)) {
1263 if (land.lnd_own != def->own)
1265 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1267 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1269 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1271 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1273 if (!list) { /* Just estimating the enemy strength */
1274 estimate += intelligence_report(player->cnum,
1276 "Scouts report defending unit:");
1279 if (!(llp = malloc(sizeof(struct llist)))) {
1280 logerror("Malloc failed in attack!\n");
1284 memset(llp, 0, sizeof(struct llist));
1285 emp_insque(&llp->queue, list);
1286 llp->supplied = has_supply(&land);
1287 if (!get_land(A_DEFEND, def, land.lnd_uid, llp, 1))
1289 if (lnd_spyval(&land) > *d_spyp)
1290 *d_spyp = lnd_spyval(&land);
1295 /* Calculate the total offensive strength */
1298 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1299 double osupport, int check)
1301 double ototal = 0.0;
1302 struct emp_qelem *qp, *next;
1307 * first, total the attacking mil
1310 for (n = 0; n <= off->last; ++n) {
1311 if (off[n].type == EF_BAD || (check &&
1312 att_get_combat(&off[n], 0) <= 0))
1314 ototal += off[n].troops * att_combat_eff(off + n);
1318 * next, add in the attack_values of all
1319 * the attacking units
1322 for (qp = olist->q_forw; qp != olist; qp = next) {
1324 llp = (struct llist *)qp;
1325 if (check && !get_land(combat_mode, 0, llp->land.lnd_uid, llp, 0))
1327 if (combat_mode == A_ATTACK) {
1329 for (n = 0; n <= off->last; ++n) {
1330 if (off[n].type == EF_BAD)
1332 if ((off[n].x == llp->land.lnd_x) &&
1333 (off[n].y == llp->land.lnd_y))
1337 lnd_delete(llp, "is in a sector not owned by you");
1340 ototal += attack_val(combat_mode, &llp->land) *
1341 att_combat_eff(off + w);
1343 ototal += attack_val(combat_mode, &llp->land);
1348 return ldround(ototal, 1);
1351 /* Calculate the total defensive strength */
1354 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1357 double dtotal = 0.0, eff = 1.0, d_unit;
1358 struct emp_qelem *qp, *next;
1361 if (check && att_get_combat(def, 1) < 0)
1363 eff = att_combat_eff(def);
1364 dtotal = def->troops * eff;
1367 * next, add in the defense_values of all
1368 * the defending non-retreating units
1371 for (qp = list->q_forw; qp != list; qp = next) {
1373 llp = (struct llist *)qp;
1375 if (check && !get_land(A_DEFEND, def, llp->land.lnd_uid, llp, 1))
1377 d_unit = defense_val(&llp->land);
1379 d_unit = ((double)defense_val(&llp->land) / 2.0);
1380 dtotal += d_unit * eff;
1385 return ldround(dtotal, 1);
1389 * This is the land unit integrity check. Note that we don't print
1390 * warnings about victim land units because the attacker may not have seen them
1394 get_land(int combat_mode, struct combat *def, int uid, struct llist *llp,
1397 struct lndstr *lp = &llp->land;
1402 if (!llp->lcp) { /* first time */
1403 llp->x = llp->land.lnd_x;
1404 llp->y = llp->land.lnd_y;
1405 llp->lcp = &lchr[(int)llp->land.lnd_type];
1406 } else { /* not first time */
1407 if (lp->lnd_effic < LAND_MINEFF) {
1408 sprintf(buf, "was destroyed and is no longer a part of the %s",
1409 att_mode[combat_mode]);
1410 lnd_delete(llp, buf);
1414 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1416 "left to go fight another battle and is no longer a part of the defense");
1420 if (lp->lnd_own != player->cnum) {
1422 "was destroyed and is no longer a part of the %s",
1423 att_mode[combat_mode]);
1424 lnd_delete(llp, buf);
1427 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1429 "left to fight another battle and is no longer a part of the %s",
1430 att_mode[combat_mode]);
1431 lnd_delete(llp, buf);
1434 if (lp->lnd_effic < llp->eff) {
1435 sprintf(buf, "damaged from %d%% to %d%%",
1436 llp->eff, lp->lnd_effic);
1437 lnd_print(llp, buf);
1441 llp->eff = llp->land.lnd_effic;
1447 * Put the land unit on the disk. If there was some mobility cost, then
1448 * subtract it from the units mobility. Note that this works the same way
1449 * as sectors & ships in that no mobility is actually taken until the attacker
1450 * has committed to attacking.
1454 kill_land(struct emp_qelem *list)
1456 struct emp_qelem *qp, *next;
1459 for (qp = list->q_forw; qp != list; qp = next) {
1461 llp = (struct llist *)qp;
1462 if (llp->land.lnd_ship >= 0) {
1463 llp->land.lnd_effic = 0;
1464 lnd_delete(llp, "cannot return to the ship, and dies!");
1470 att_infect_units(struct emp_qelem *list, int plague)
1472 struct emp_qelem *qp, *next;
1477 for (qp = list->q_forw; qp != list; qp = next) {
1479 llp = (struct llist *)qp;
1480 if (llp->land.lnd_pstage == PLG_HEALTHY)
1481 llp->land.lnd_pstage = PLG_EXPOSED;
1486 put_land(struct emp_qelem *list)
1488 struct emp_qelem *qp, *next;
1491 for (qp = list->q_forw; qp != list; qp = next) {
1493 llp = (struct llist *)qp;
1494 llp->land.lnd_mission = 0;
1495 llp->land.lnd_harden = 0;
1496 llp->land.lnd_mobil -= (int)llp->mobil;
1498 putland(llp->land.lnd_uid, &llp->land);
1499 if (llp->land.lnd_own != player->cnum) {
1500 emp_remque((struct emp_qelem *)llp);
1503 get_land(A_ATTACK, 0, llp->land.lnd_uid, llp, 0);
1508 * Keep sending in reinforcements until it looks like we're going to win.
1509 * Note that the "strength" command also calls this routine.
1513 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1514 int *d_spyp, int ototal)
1516 struct nstr_item ni;
1518 struct sctstr sect, dsect;
1528 double eff = att_combat_eff(def);
1534 * All units that are within their reaction radius and not damaged
1535 * below their morale value now get to react to the threatened sect.
1536 * Once we've sent enough to counter the threat, stop sending them.
1538 * Not anymore. All units get to react. :)
1542 dtotal = get_dtotal(def, list, 1.0, 1);
1545 snxtitem_all(&ni, EF_LAND);
1546 while (nxtitem(&ni, &land) &&
1547 (dtotal + new_land * eff < (int)(1.2 * (float)ototal))) {
1550 if (!land.lnd_rad_max)
1552 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1554 if (land.lnd_own != def->own)
1556 if (land.lnd_ship >= 0)
1558 if (!defense_val(&land))
1561 if (land.lnd_effic <= land.lnd_retreat)
1564 if (!lnd_can_attack(&land))
1567 /* Only supplied units can react */
1568 if (!(supply_state = has_supply(&land)))
1571 dist = mapdist(land.lnd_x, land.lnd_y, def->x, def->y);
1573 getsect(land.lnd_x, land.lnd_y, §);
1574 /* Units on efficient headquarters can react 1 farther */
1575 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
1576 radius = land.lnd_rad_max + 1;
1578 radius = land.lnd_rad_max;
1580 if (land.lnd_mission == MI_RESERVE)
1586 getsect(def->x, def->y, &dsect);
1587 if (!(p = BestLandPath(buf, §, &dsect, &move_cost, MOB_ROAD)))
1590 mobcost = land.lnd_effic * 0.01 * lchr[(int)land.lnd_type].l_spd;
1593 mobcost = 480.0 / (mobcost + techfact(land.lnd_tech, mobcost));
1594 mobcost *= (move_cost * 5.0);
1596 if (land.lnd_mobil < mobcost)
1599 new_land += defense_val(&land);
1601 if (!list) /* we are in the "strength" command */
1604 /* move to defending sector */
1605 land.lnd_mobil -= ldround(mobcost, 1);
1608 land.lnd_x = def->x;
1609 land.lnd_y = def->y;
1610 putland(land.lnd_uid, &land);
1611 wu(0, land.lnd_own, "%s reacts to %s.\n",
1612 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1614 llp = (struct llist *)
1615 malloc(sizeof(struct llist));
1617 memset(llp, 0, sizeof(struct llist));
1618 llp->supplied = supply_state;
1621 llp->lcp = &lchr[(int)land.lnd_type];
1623 emp_insque(&llp->queue, list);
1624 if (lnd_spyval(&land) > *d_spyp)
1625 *d_spyp = lnd_spyval(&land);
1627 intelligence_report(player->cnum, &land, a_spy,
1628 "Scouts sight reacting enemy unit:");
1633 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1636 get_osupport(s_char *outs, struct combat *def, int fort_sup, int ship_sup,
1637 int land_sup, int plane_sup)
1639 double osupport = 1.0;
1641 double af, as, au, ap;
1643 af = as = au = ap = 0.0;
1645 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1646 af = ((double)dam / 100.0);
1650 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1652 as = ((double)dam / 100.0);
1657 dam = lnd_support(def->own, player->cnum, def->x, def->y, 0);
1658 au = ((double)dam / 100.0);
1663 dam = off_support(def->x, def->y, def->own, player->cnum);
1664 ap = (((double)dam) / 100.0);
1667 sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1672 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1675 get_dsupport(s_char *outs, struct emp_qelem *list, struct combat *def,
1676 int ototal, int dtotal)
1678 double dsupport = 1.0;
1680 double df, ds, du, dp;
1683 df = ds = du = dp = 0.0;
1684 if (dtotal < 0.1 * ototal) {
1686 } else if (dtotal >= 1.2 * ototal) {
1689 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1690 df = ((double)dam / 100.0);
1693 dtotal = get_dtotal(def, list, dsupport, 0);
1694 if (dtotal < 1.2 * ototal) {
1695 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1696 ds = ((double)dam / 100.0);
1698 dtotal = get_dtotal(def, list, dsupport, 0);
1700 if (dtotal < 1.2 * ototal) {
1701 dam = lnd_support(player->cnum, def->own, def->x, def->y, 1);
1702 du = ((double)dam / 100.0);
1704 dtotal = get_dtotal(def, list, dsupport, 1);
1706 if (dtotal < 1.2 * ototal) {
1707 dam = def_support(def->x, def->y, player->cnum, def->own);
1708 dp = (((double)dam) / 100.0);
1715 sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1720 "\nOdds are bad for us...support cancelled.\n\n");
1723 "\nOdds are good for us...support cancelled.\n\n");
1729 * Land mines add to the defense multiplier. If the attacker has engineers
1730 * then this multiplier is cut in half.
1734 get_mine_dsupport(struct combat *def, int a_engineer)
1739 getsect(def->x, def->y, §);
1741 if (sect.sct_oldown != player->cnum) {
1742 mines = min(sect.sct_mines, 20);
1744 mines = ldround(((double)mines / 2.0), 1);
1747 wu(0, def->own, "Defending mines add %1.2f\n",
1749 pr("Defending mines add %1.2f\n", mines * 0.02);
1750 return mines * 0.02;
1756 /* Get the offensive and defensive support */
1758 att_get_support(int combat_mode, int ofort, int oship, int oland,
1759 int oplane, struct emp_qelem *olist, struct combat *off,
1760 struct emp_qelem *dlist, struct combat *def,
1761 double *osupportp, double *dsupportp, int a_engineer)
1764 s_char osupports[512];
1765 s_char dsupports[512];
1767 if (combat_mode == A_PARA)
1770 *osupportp = get_osupport(osupports, def,
1771 ofort, oship, oland, oplane);
1774 * I need to put a 1 at the end of the next four total_stren calls
1775 * becauase units & mil may have been damaged by collateral damage or
1776 * neclear warheads from the offensive & defensive support.
1779 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1780 if (att_empty_attack(combat_mode, ototal, def))
1781 return abort_attack();
1782 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1785 * Calculate defensive support. If odds are too good or too bad
1786 * then don't call in support.
1789 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1790 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1791 if (att_empty_attack(combat_mode, ototal, def))
1792 return abort_attack();
1794 if ((*osupports || *dsupports) &&
1795 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1796 pr("\n\t\tsupport values\n");
1797 pr("\t\tforts\tships\tunits\tplanes\n");
1798 if (*osupportp != 1.0)
1799 pr("%s", osupports);
1800 if (*dsupportp != 1.0)
1801 pr("%s", dsupports);
1803 wu(0, def->own, "\n\t\tsupport values\n");
1804 wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1805 if (*osupportp != 1.0)
1806 wu(0, def->own, "%s", osupports);
1807 if (*dsupportp != 1.0)
1808 wu(0, def->own, "%s", dsupports);
1812 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1813 if (dtotal && def->type == EF_SECTOR)
1814 *dsupportp += get_mine_dsupport(def, a_engineer);
1818 /* How many two-legged bipeds are in this combat force? */
1821 count_bodies(struct combat *off, struct emp_qelem *list)
1825 struct emp_qelem *qp;
1828 for (n = 0; n <= off->last; ++n)
1829 bodies += off[n].troops;
1830 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1831 llp = (struct llist *)qp;
1832 bodies += total_mil(&llp->land);
1837 /* This is where the fighting actually occurs. */
1840 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1841 double osupport, struct combat *def, struct emp_qelem *dlist,
1845 int a_cas = 0; /* Casualty counts */
1847 int ototal; /* total attacking strength */
1848 int dtotal; /* total defending strength */
1849 int a_bodies; /* total attacking mil (incl. mil in units) */
1850 int d_bodies; /* total defending mil (incl. mil in units) */
1859 ototal = get_ototal(combat_mode, off, olist, osupport,
1860 combat_mode != A_PARA);
1861 dtotal = get_dtotal(def, dlist, dsupport, 0);
1865 a_bodies = count_bodies(off, olist);
1866 d_bodies = count_bodies(def, dlist);
1867 d_mil = def->troops;
1868 for (n = 0; n <= off->last; ++n)
1869 if (off[n].type == EF_BAD)
1872 a_troops[n] = off[n].troops;
1874 /* This switch is required to get the spacing right */
1875 switch (combat_mode) {
1877 pr(" Final attack strength: %8d\n", ototal);
1880 pr(" Final assault strength: %8d\n", ototal);
1883 if (def->sct_type == SCT_MOUNT ||
1884 def->sct_type == SCT_WATER ||
1885 def->sct_type == SCT_CAPIT ||
1886 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1887 pr("You can't air-assault a %s sector!\n",
1888 def->sct_dcp->d_name);
1891 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1893 pr(" Final air-assault strength: %8d\n", ototal);
1897 pr(" Final board strength: %8d\n", ototal);
1901 pr(" Final defense strength: %8d\n", dtotal);
1902 odds = att_calcodds(ototal, dtotal);
1903 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1905 /* spread the plague */
1906 if (combat_mode != A_PARA) {
1908 for (n = 0; n <= off->last; ++n)
1909 if (off[n].type != EF_BAD)
1910 def->plague |= off[n].plague;
1911 for (n = 0; n <= off->last; ++n)
1912 if (off[n].type != EF_BAD)
1913 off[n].plague |= def->plague;
1915 att_infect_units(olist, off->plague);
1916 att_infect_units(dlist, def->plague);
1918 /* Fighting is slightly random. There is always that last little
1919 * effort you see people put in. Or the stray bullet that takes out
1920 * an officer and the rest go into chaos. Things like that.
1921 * Thus, we have added a very slight random factor that will sometimes
1922 * allow the little guy to win. We modify the odds a little
1923 * (either +- 5%) to account for this randomness. We also only
1924 * recalculate the odds every 8-50 casualties, not every cacsualty,
1925 * since a single dead guy normally wouldn't cause a commander to
1926 * rethink his strategies, but 50 dead guys might. */
1927 odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1932 recalctime = 8 + (random() % 43);
1933 while (!success && ototal) {
1936 d_cas += take_casualty(A_DEFEND, def, dlist);
1937 dtotal = get_dtotal(def, dlist, dsupport, 0);
1942 a_cas += take_casualty(combat_mode, off, olist);
1943 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1945 if (((a_cas + d_cas) % 70) == 69)
1947 if (recalctime-- <= 0) {
1948 recalctime = 8 + (random() % 43);
1949 odds = att_calcodds(ototal, dtotal);
1950 odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1958 /* update defense mobility & mil */
1962 if (def->type == EF_SECTOR && d_mil && d_cas) {
1965 /* Make sure we use a positive mobility here */
1966 tmob = ((def->mob < 0) ? -(def->mob) : (def->mob));
1968 min(20, min(1, tmob - damage(tmob, 100 * d_cas / d_mil)));
1970 def->mil = def->troops;
1973 /* update attack mobility & mil */
1974 for (n = 0; n <= off->last; ++n)
1975 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1976 if (off[n].type == EF_SECTOR && off[n].mil)
1980 off[n].mob - damage(off[n].mob,
1981 100 * (a_troops[n] -
1984 off[n].mil -= a_troops[n] - off[n].troops;
1987 /* update land unit mobility */
1988 if (d_bodies && d_cas)
1989 lnd_takemob(dlist, (double)d_cas / d_bodies);
1990 if (a_bodies && a_cas)
1991 lnd_takemob(olist, (double)a_cas / a_bodies);
1993 /* damage attacked sector */
1994 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1996 pr("- Casualties -\n Yours: %d\n", a_cas);
1997 pr(" Theirs: %d\n", d_cas);
1998 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
1999 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
2002 switch (combat_mode) {
2004 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
2005 pr("We have captured %s, sir!\n", prcom(0, def));
2009 news_item = def->own ? N_AWON_SECT : N_START_COL;
2010 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
2011 action = "assaulting and taking";
2014 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
2015 pr("We have captured %s, sir!\n", prcom(0, def));
2016 action = "air-assaulting and taking";
2019 news_item = N_BOARD_SHIP;
2020 pr("We have boarded %s, sir!\n", prcom(0, def));
2021 action = "boarding";
2024 news_item = N_BOARD_LAND;
2025 pr("We have boarded %s, sir!\n", prcom(0, def));
2026 action = "boarding";
2030 switch (combat_mode) {
2032 news_item = N_SCT_LOSE;
2033 pr("You have been defeated!\n");
2034 action = "attacking";
2037 news_item = N_ALOSE_SCT;
2038 pr("You have been defeated!\n");
2040 action = "trying to assault";
2043 news_item = N_PLOSE_SCT;
2044 pr("All of your troops were destroyed\n");
2045 action = "trying to air-assault";
2048 news_item = N_SHP_LOSE;
2049 pr("You have been repelled\n");
2051 action = "trying to board";
2054 news_item = N_LND_LOSE;
2055 pr("You have been repelled\n");
2057 action = "trying to board";
2061 nreport(player->cnum, news_item, def->own, 1);
2064 "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
2065 cname(player->cnum), player->cnum, a_cas,
2066 action, pr_com(0, def, def->own), d_cas);
2069 if (opt_SNEAK_ATTACK) {
2070 do_sneak(def, success);
2073 send_reacting_units_home(dlist);
2075 /* putland the defending land */
2078 /* putland the attacking land */
2081 /* put the victim sector/ship/land */
2082 if (!success || !take_def(combat_mode, olist, off, def))
2085 /* put the attacking sectors/ship */
2086 for (n = 0; n <= off->last; ++n)
2087 if (off[n].type != EF_BAD)
2088 put_combat(&off[n]);
2093 switch (combat_mode) {
2095 ask_move_in(off, olist, def);
2097 /* put sectors again to get abandon warnings */
2098 for (n = 0; n <= off->last; ++n)
2099 if (off[n].type != EF_BAD)
2100 put_combat(&off[n]);
2103 att_move_in_off(combat_mode, off, olist, def);
2106 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2110 /* What percentage of the combat forces going head-to-head are we? */
2113 att_calcodds(int ototal, int dtotal)
2117 /* calculate odds */
2120 else if (dtotal <= 0)
2123 odds = ((double)ototal) / (dtotal + ototal);
2128 /* Here's where the dead soldiers get dragged off the battlefield */
2131 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2133 int to_take = CASUALTY_LUMP;
2134 int biggest_troops = 0, index = -1;
2135 int n, tot_troops = 0, biggest_mil, cas;
2136 struct emp_qelem *qp, *biggest;
2139 for (n = 0; n <= off->last; ++n) {
2140 if (off[n].type != EF_BAD) {
2141 tot_troops += off[n].troops;
2142 if (off[n].troops > biggest_troops) {
2143 biggest_troops = off[n].troops;
2150 to_take -= tot_troops;
2153 for (n = 0; n <= off->last; ++n)
2154 if (off[n].type != EF_BAD)
2158 * They can all come off mil. We rotate the casualties,
2159 * starting with the sector containing the most mil.
2161 to_take = CASUALTY_LUMP;
2163 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2166 while (to_take > 0) {
2167 for (n = index; n <= off->last && to_take; ++n) {
2168 if (off[n].type != EF_BAD && off[n].troops > 0) {
2173 for (n = 0; n < index && to_take; ++n) {
2174 if (off[n].type != EF_BAD && off[n].troops > 0) {
2180 return CASUALTY_LUMP;
2184 return (CASUALTY_LUMP - to_take);
2187 * Need to take some casualties from attacking units
2188 * Procedure: find the biggest unit remaining (in
2189 * terms of mil) and give it the casualties.
2191 biggest = (struct emp_qelem *)0;
2193 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2194 llp = (struct llist *)qp;
2196 if (total_mil(&llp->land) > biggest_mil) {
2197 biggest_mil = total_mil(&llp->land);
2201 if (biggest == (struct emp_qelem *)0)
2202 return (CASUALTY_LUMP - to_take);
2204 llp = (struct llist *)biggest;
2205 cas = lnd_take_casualty(combat_mode, llp, to_take);
2206 return (CASUALTY_LUMP - (to_take - cas));
2209 /* Send reacting defense units back to where they came from (at no mob cost) */
2212 send_reacting_units_home(struct emp_qelem *list)
2214 struct emp_qelem *qp, *next;
2218 for (qp = list->q_forw; qp != list; qp = next) {
2220 llp = (struct llist *)qp;
2221 if ((llp->land.lnd_x != llp->x) || (llp->land.lnd_y != llp->y)) {
2222 sprintf(buf, "returns to %s",
2223 xyas(llp->x, llp->y, llp->land.lnd_own));
2224 llp->land.lnd_x = llp->x;
2225 llp->land.lnd_y = llp->y;
2226 lnd_delete(llp, buf);
2231 /* Check for 0 offense strength. This call will always preceed an abort */
2234 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2237 if (def->own && player->cnum != def->own) {
2239 "%s (#%d) considered %sing you @%s\n",
2240 cname(player->cnum), player->cnum,
2241 att_mode[combat_mode], xyas(def->x, def->y, def->own));
2243 pr("No troops for %s...\n", att_mode[combat_mode]);
2250 * Take the defending sector or ship from the defender and give it to the
2255 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2260 struct llist *llp, *delete_me = 0;
2266 for (n = 0; n <= off->last && !occuppied; ++n) {
2267 if (off[n].type != EF_BAD &&
2268 off[n].troops > 0 &&
2269 (off[n].type != EF_SECTOR || off[n].mob)) {
2271 if (def->type == EF_LAND) {
2272 if (def->lnd_lcp->l_flags & L_SPY) {
2279 pr("1 mil from %s moves %s\n",
2280 prcom(0, off + n), prcom(2, def));
2285 pr("%s left unoccupied\n", prcom(0, def));
2288 "No enemy troops moved %s so you still own it!\n",
2289 pr_com(2, def, def->own));
2292 llp = (struct llist *)list->q_forw;
2293 llp->land.lnd_x = def->x;
2294 llp->land.lnd_y = def->y;
2295 take_move_in_mob(combat_mode, llp, off, def);
2296 if (def->type == EF_SHIP) {
2297 llp->land.lnd_ship = def->shp_uid;
2298 sprintf(buf, "boards %s", prcom(0, def));
2301 llp->land.lnd_ship = -1;
2302 sprintf(buf, "moves in to occupy %s",
2303 xyas(def->x, def->y, player->cnum));
2304 lnd_delete(llp, buf);
2309 if (def->type == EF_SECTOR) {
2310 getsect(def->x, def->y, §);
2311 takeover(§, player->cnum);
2312 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2313 caploss(§, def->own,
2314 "* We have captured %s's capital, sir! *\n");
2316 } else if (def->type == EF_SHIP) {
2317 getship(def->shp_uid, &ship);
2318 takeover_ship(&ship, player->cnum, 1);
2319 putship(ship.shp_uid, &ship);
2320 } else if (def->type == EF_LAND) {
2321 getland(def->lnd_uid, &land);
2322 takeover_land(&land, player->cnum, 1);
2323 putland(land.lnd_uid, &land);
2326 lnd_delete(delete_me, buf);
2327 att_get_combat(def, 0);
2332 * Ask the attacker which mil & land units they'd like to move into the
2337 ask_move_in(struct combat *off, struct emp_qelem *olist,
2341 struct emp_qelem *qp, *next;
2345 s_char land_answer[1024];
2348 for (n = 0; n <= off->last; ++n)
2349 if (off[n].type != EF_BAD && off[n].troops > 0)
2351 ask_move_in_off(&off[n], def);
2352 if (player->aborted)
2358 memset(land_answer, 0, sizeof(land_answer));
2359 for (qp = olist->q_forw; qp != olist; qp = next) {
2361 llp = (struct llist *)qp;
2362 answerp = &land_answer[(int)llp->land.lnd_army];
2363 if (player->aborted || att_get_combat(def, 0) < 0)
2365 if (*answerp == 'Y')
2367 if (*answerp != 'N') {
2368 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2370 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2372 llp->land.lnd_army == ' ' ? '~' : llp->land.lnd_army,
2373 llp->land.lnd_effic);
2374 *answerp = att_prompt(prompt, llp->land.lnd_army);
2375 if (player->aborted || att_get_combat(def, 0) < 0)
2377 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2380 if (*answerp == 'y' || *answerp == 'Y')
2382 sprintf(buf, "stays in %s",
2383 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2384 lnd_delete(llp, buf);
2388 if (att_get_combat(def, 0) < 0) {
2389 for (qp = olist->q_forw; qp != olist; qp = next) {
2391 llp = (struct llist *)qp;
2392 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2394 sprintf(buf, "stays in %s",
2395 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2396 lnd_delete(llp, buf);
2400 if (opt_INTERDICT_ATT)
2401 lnd_interdict(olist, def->x, def->y, player->cnum);
2402 move_in_land(A_ATTACK, off, olist, def);
2405 /* Move offensive land units to the conquered sector or ship */
2408 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2411 struct emp_qelem *qp, *next;
2417 for (qp = olist->q_forw; qp != olist; qp = next) {
2419 llp = (struct llist *)qp;
2420 if (!get_land(combat_mode, def, llp->land.lnd_uid, llp, 0))
2422 take_move_in_mob(combat_mode, llp, off, def);
2423 llp->land.lnd_x = def->x;
2424 llp->land.lnd_y = def->y;
2425 if (def->type == EF_SHIP)
2426 llp->land.lnd_ship = def->shp_uid;
2428 llp->land.lnd_ship = -1;
2432 if (def->type == EF_SECTOR) {
2433 if (opt_INTERDICT_ATT) {
2434 lnd_sweep(olist, 0, 0, def->own);
2435 lnd_check_mines(olist);
2437 sprintf(buf, "now occupies %s", prcom(0, def));
2439 sprintf(buf, "boards %s", prcom(0, def));
2443 for (qp = olist->q_forw; qp != olist; qp = next) {
2445 llp = (struct llist *)qp;
2446 lnd_print(llp, buf);
2454 * Move assaulting, paradropping, or boarding mil & units into def
2455 * If the mil are coming from a ship, then pack a lunch.
2459 att_move_in_off(int combat_mode, struct combat *off,
2460 struct emp_qelem *olist, struct combat *def)
2467 move_in_land(combat_mode, off, olist, def);
2469 getsect(def->x, def->y, §);
2471 for (n = 0; n <= off->last; ++n) {
2472 if (off[n].type == EF_BAD || !off[n].troops)
2474 troops = off[n].troops;
2476 off[n].mil -= troops;
2478 put_combat(off + n);
2479 if (combat_mode == A_ASSAULT) {
2480 if (off[n].type != EF_SHIP || def->type != EF_SECTOR) {
2481 logerror("att_move_in_off: strange assault");
2484 getship(off[n].shp_uid, &ship);
2485 lunchbox = (int)((troops + 1) * ship.shp_item[I_FOOD]
2486 / (ship.shp_item[I_MILIT] + troops
2487 + ship.shp_item[I_CIVIL] + 0.5));
2488 if (lunchbox > ITEM_MAX - sect.sct_item[I_FOOD])
2489 lunchbox = ITEM_MAX - sect.sct_item[I_FOOD];
2491 ship.shp_item[I_FOOD] -= lunchbox;
2492 sect.sct_item[I_FOOD] += lunchbox;
2493 putship(ship.shp_uid, &ship);
2502 /* Ask how many mil to move in from each sector */
2505 ask_move_in_off(struct combat *off, struct combat *def)
2508 int num_mil, dam = 0, left;
2514 if (att_get_combat(off, 0) <= 0)
2516 if (att_get_combat(def, 0) < 0)
2518 if (off->own != player->cnum)
2520 d = sector_mcost(getsectp(def->x, def->y), MOB_ROAD);
2521 if ((mob_support = min(off->troops, (int)(off->mob / d))) <= 0)
2523 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2524 xyas(off->x, off->y, player->cnum), mob_support);
2525 if (!(p = getstring(prompt, buf)) || !*p || (num_mil = atoi(p)) <= 0) {
2529 /* Make sure we don't move in more than we can support mobility-wise */
2530 if (num_mil > mob_support)
2531 num_mil = mob_support;
2532 if (att_get_combat(off, 0) <= 0)
2534 if (att_get_combat(def, 0) < 0)
2536 if ((num_mil = min(off->troops, num_mil)) <= 0) {
2537 pr("No mil moved in from %s\n",
2538 xyas(off->x, off->y, player->cnum));
2541 mob_support = max(1, (int)(num_mil * d));
2542 off->mob -= min(off->mob, mob_support);
2543 off->mil -= num_mil;
2544 off->troops -= num_mil;
2547 weight = num_mil * ichr[I_MILIT].i_lbs;
2548 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2549 if (chance(weight / 100.0))
2551 ground_interdict(def->x, def->y, player->cnum, "military");
2552 dam += check_lmines(def->x, def->y, weight);
2556 left = commdamage(num_mil, dam, I_MILIT);
2557 if (left < num_mil) {
2559 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));
2561 pr("All of the mil you were moving were destroyed!\n");
2564 /* maybe got nuked */
2565 if (att_get_combat(def, 0) < 0)
2573 /* Charge land units for moving into a sector or onto a ship */
2576 take_move_in_mob(int combat_mode, struct llist *llp, struct combat *off,
2582 switch (combat_mode) {
2585 lnd_mobcost(&llp->land, getsectp(def->x, def->y), MOB_NONE);
2586 new = llp->land.lnd_mobil - mobcost;
2589 llp->land.lnd_mobil = new;
2592 if (off->shp_mcp->m_flags & M_LAND) {
2593 if (llp->lcp->l_flags & L_MARINE)
2594 llp->land.lnd_mobil -=
2595 ((float)etu_per_update * land_mob_scale * 0.5);
2597 llp->land.lnd_mobil -= ((float)etu_per_update
2600 if (llp->lcp->l_flags & L_MARINE)
2601 llp->land.lnd_mobil = 0;
2603 llp->land.lnd_mobil = (((float)etu_per_update
2604 * land_mob_scale) * (-1));
2608 /* I arbitrarily chose the numbers 10 and 40 below -KHS */
2609 if (llp->lcp->l_flags & L_MARINE)
2610 llp->land.lnd_mobil -= 10;
2612 llp->land.lnd_mobil -= 40;
2615 llp->land.lnd_harden = 0;
2619 free_list(struct emp_qelem *list)
2621 struct emp_qelem *qp, *next;
2623 if (!list || QEMPTY(list))
2627 while (qp != list) {
2636 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2644 * sector_strength - Everyone starts at 1. You can get up to a max
2645 * of d_dstr, depending on how much you build up the
2646 * defenses of the sector.
2650 sector_strength(struct sctstr *sp)
2656 if (sp->sct_type == SCT_MOUNT)
2659 d = d + ((double)(dchr[sp->sct_type].d_dstr - d) *
2660 ((double)sp->sct_defense / 100.0));
2662 if (d > dchr[sp->sct_type].d_dstr)
2663 d = dchr[sp->sct_type].d_dstr;