2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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) *
636 aship.shp_speed * off->eff <=
637 techfact(dship.shp_tech, 1.0) * dship.shp_speed * def->eff) {
638 pr("Victim ship moves faster than you do!\n");
641 "%s (#%d) %s failed to catch %s\n",
642 cname(aship.shp_own), aship.shp_own,
643 pr_com(0, off, def->own), pr_com(0, def, def->own));
644 return abort_attack();
646 } else if (off->type != EF_SECTOR) {
647 pr("Please tell the deity that you got the 'banana boat' error\n");
648 return abort_attack();
650 if (def->shp_mcp->m_flags & M_SUB) {
651 getsect(def->x, def->y, §);
652 if (sect.sct_type == SCT_WATER) {
653 pr("You can't board a submarine!\n");
654 return abort_attack();
661 * Lots of special things need to be checked for boarding, so I put it in
663 * STM - I copied it for land unit boarding. :)
667 land_board_abort(struct combat *off, struct combat *def)
669 if (att_get_combat(def, 1) < 0)
670 return abort_attack();
675 if (att_get_combat(off, 0) < 0)
676 return abort_attack();
678 if (off->x != def->x || off->y != def->y) {
679 pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
680 return abort_attack();
686 /* If we are boarding, then the defending ship gets a chance to fire back */
688 att_approach(struct combat *off, struct combat *def)
694 pr("Approaching %s...\n", prcom(0, def));
697 "%s is being approached by %s...\n",
698 pr_com(0, def, def->own), pr_com(0, off, def->own));
699 if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
702 pr("They're firing at us sir!\n");
705 "Your fleet at %s does %d damage to %s\n",
706 xyas(def->x, def->y, def->own), dam, pr_com(0, off, def->own));
708 if (off->type == EF_SECTOR) {
709 getsect(off->x, off->y, §);
710 sectdamage(§, dam, 0);
712 pr("Enemy fleet at %s does %d damage to %s\n",
713 xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
714 } else if (off->type == EF_SHIP) {
715 getship(off->shp_uid, &ship);
716 shipdamage(&ship, dam);
717 putship(off->shp_uid, &ship);
718 if (def->own && ship.shp_effic < SHIP_MINEFF) {
719 wu(0, def->own, "%s sunk!\n", pr_com(0, off, def->own));
720 nreport(player->cnum, N_SHP_LOSE, def->own, 1);
723 if (att_get_combat(off, 0) < 0)
724 return abort_attack();
728 /* The attack is valid. Tell the attacker about what they're going to hit */
731 att_show(struct combat *def)
733 /* Note that we tell the player about the treaty BEFORE we tell them
734 about the item. If we didn't, then they gain free information */
735 if (def->type == EF_SECTOR) {
736 if (!trechk(player->cnum, def->own, LANATT))
737 return abort_attack();
738 pr("%s is a %d%% %s %s with approximately %d military.\n",
739 xyas(def->x, def->y, player->cnum),
740 roundintby((int)def->eff, 10),
741 cname(def->own), def->sct_dcp->d_name,
742 roundintby(def->troops, 10));
743 if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
744 writemap(player->cnum);
745 } else if (def->type == EF_SHIP || def->type == EF_LAND) {
746 if (def->type == EF_SHIP) {
747 if (!trechk(player->cnum, def->own, SEAATT))
748 return abort_attack();
750 if (!trechk(player->cnum, def->own, LNDATT))
751 return abort_attack();
753 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));
755 /* Ok, everything is fine */
759 /* Attack and assault ask the user which kind of support they want */
762 att_ask_support(int offset, int *fortp, int *shipp, int *landp,
767 *fortp = *shipp = *landp = *planep = 1;
769 if (player->argp[offset] != (s_char *)0) {
770 if ((player->argp[offset + 1] == (s_char *)0) ||
771 (player->argp[offset + 2] == (s_char *)0) ||
772 (player->argp[offset + 3] == (s_char *)0)) {
773 pr("If any support arguments are used, all must be!\n");
778 *landp = *planep = 0;
780 if (!(p = getstarg(player->argp[offset], "Use fort support? ",
784 if ((*p == 'y') || (*p == 'Y'))
787 if (!(p = getstarg(player->argp[offset + 1], "Use ship support? ",
791 if ((*p == 'y') || (*p == 'Y'))
794 if (!(p = getstarg(player->argp[offset + 2], "Use land support? ",
798 if ((*p == 'y') || (*p == 'Y'))
801 if (!(p = getstarg(player->argp[offset + 3], "Use plane support? ",
805 if ((*p == 'y') || (*p == 'Y'))
812 * Attack, assault, and board ask the attacker what they'd like to attack
813 * with. This includes mil and land units from each "off" object. Note that
814 * after each sub-prompt, we check to make sure that the attack is still
815 * valid, and if it's not, then we abort the attack.
819 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
820 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
823 s_char land_answer[1024];
826 if (att_abort(combat_mode, off, def))
828 memset(land_answer, 0, sizeof(land_answer));
829 for (n = 0; n <= off->last; ++n) {
830 off[n].troops = ask_off(combat_mode, off + n, def);
831 if (att_abort(combat_mode, off, def))
833 ask_olist(combat_mode, off + n, def, olist, land_answer,
834 a_spyp, a_engineerp);
835 if (att_abort(combat_mode, off, def))
841 /* How many mil is off allowed to attack with when it attacks def? */
844 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
848 switch (combat_mode) {
850 mob_support = off->mob / sector_mcost(getsectp(def->x, def->y),
854 /* mob_support = off->mob / sector_mcost(def->sct_type, def->eff);*/
855 if (mob_support < off->troops)
856 pr("Sector %s has %d mobility which can only support %d mil,\n", xyas(off->x, off->y, player->cnum), off->mob, mob_support);
858 mob_support = off->troops;
861 if (def->own != player->cnum && def->mil) {
862 if (off->shp_mcp->m_flags & M_SEMILAND)
863 return off->troops / 4;
864 else if (!(off->shp_mcp->m_flags & M_LAND))
865 return off->troops / 10;
869 if (off->type == EF_SECTOR && off->mob <= 0)
871 mob_support = def->shp_mcp->m_item[I_MILIT];
872 if (mob_support < off->troops)
873 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
875 mob_support = off->troops;
880 if (def->lnd_lcp->l_flags & L_SPY)
882 mob_support = def->lnd_lcp->l_item[I_MILIT];
883 if (mob_support < off->troops)
884 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
886 mob_support = off->troops;
893 * If the attacker decides to go through with the attack, then the
894 * sectors/ships they are attacking with may be charged some mobility.
895 * This is where that amount of mobility is calculated. It is actually
896 * subtracted "for real" from the object's mobility in put_combat().
900 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
907 switch (combat_mode) {
911 (int)(attacking_mil *
912 sector_mcost(getsectp(def->x, def->y), MOB_ROAD)));
915 off->mobcost += max(1, attacking_mil / 5);
920 off->mobcost += max(1, attacking_mil / 5);
923 /* the 2 in the formula below is a fudge factor */
924 getship(def->shp_uid, &ship);
925 off->mobcost += (def->eff / 100) * (ship.shp_speed / 2);
930 /* How many mil to we want to attack from off against def? */
933 ask_off(int combat_mode, struct combat *off, struct combat *def)
939 if (att_get_combat(off, 0) <= 0)
941 if ((off->type == EF_SECTOR) && (off->own != player->cnum))
943 if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
945 if (off->type == EF_SECTOR) {
946 if (off->own != player->cnum)
948 sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
949 off->sct_dcp->d_name,
950 xyas(off->x, off->y, player->cnum), mob_support);
952 sprintf(prompt, "Number of mil from %s (max %d) : ",
953 prcom(0, off), mob_support);
955 if ((attacking_mil = onearg(0, prompt)) < 0)
957 if (att_abort(combat_mode, off, def))
959 if (att_get_combat(off, 0) <= 0)
962 min(attacking_mil, min(mob_support, off->troops))) <= 0)
965 calc_mobcost(combat_mode, off, def, attacking_mil);
966 return attacking_mil;
970 * Which units would you like to attack with or move in with [ynYNq?]
974 att_prompt(s_char *prompt, s_char army)
982 while (!p || (*p != 'y' && *p != 'n' && *p != 'Y' && *p != 'N')) {
983 if (p && *p == 'q') {
990 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);
991 p = getstring(prompt, buf);
992 if (player->aborted) {
1000 /* Ask the attacker which units they want to attack/assault/board with */
1003 ask_olist(int combat_mode, struct combat *off, struct combat *def,
1004 struct emp_qelem *olist, s_char *land_answer, int *a_spyp,
1007 struct nstr_item ni;
1011 struct lchrstr *lcp;
1018 if (def->type == EF_LAND)
1020 if (def->type == EF_SHIP)
1021 maxland = def->shp_mcp->m_nland;
1023 snxtitem_xy(&ni, EF_LAND, off->x, off->y);
1024 while (nxtitem(&ni, (s_char *)&land)) {
1025 if (land.lnd_own != player->cnum)
1027 if (land.lnd_effic < LAND_MINEFF)
1029 if (land_answer[(int)land.lnd_army] == 'N')
1031 if (!lnd_can_attack(&land))
1033 lcp = &lchr[(int)land.lnd_type];
1035 if (def->type == EF_SHIP && !maxland) {
1036 pr("Land units are not able to board this kind of ship\n");
1039 if (land.lnd_mobil <= 0) {
1040 pr("%s is out of mobility, and cannot %s\n",
1041 prland(&land), att_mode[combat_mode]);
1046 if (ontradingblock(EF_LAND, (int *)&land)) {
1047 pr("%s is on the trading block, and cannot %s\n",
1048 prland(&land), att_mode[combat_mode]);
1053 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
1054 pr("%s is on ship #%d, and cannot %s\n",
1055 prland(&land), land.lnd_ship, att_mode[combat_mode]);
1057 } else if (off->type == EF_SHIP) {
1058 if (land.lnd_ship != off->shp_uid)
1060 } else if (land.lnd_land >= 0) {
1061 pr("%s is on unit #%d, and cannot %s\n",
1062 prland(&land), land.lnd_land, att_mode[combat_mode]);
1065 switch (combat_mode) {
1068 lnd_mobcost(&land, getsectp(def->x, def->y), MOB_NONE);
1069 if (land.lnd_mobil < mobcost) {
1070 pr("%s does not have enough mobility (%d needed)\n",
1071 prland(&land), (int)ceil(mobcost));
1078 if (!(lcp->l_flags & L_ASSAULT))
1081 att_val = attack_val(combat_mode, &land);
1083 pr("%s has no offensive strength\n", prland(&land));
1086 resupply_all(&land);
1087 putland(land.lnd_uid, &land);
1088 if (!has_supply(&land)) {
1089 pr("%s is out of supply, and cannot %s\n",
1090 prland(&land), att_mode[combat_mode]);
1093 if (def->type == EF_SHIP && first_time) {
1095 pr("You may board with a maximum of %d land units\n", maxland);
1097 pr("%s has a base %s value of %d\n",
1098 prland(&land), att_mode[combat_mode], att_val);
1099 if (land_answer[(int)land.lnd_army] != 'Y') {
1101 "%s with %s %s (%c %d%%) [ynYNq?] ",
1102 att_mode[combat_mode],
1105 land.lnd_army == ' ' ? '~' : land.lnd_army,
1107 land_answer[(int)land.lnd_army] =
1108 att_prompt(prompt, land.lnd_army);
1109 if (att_abort(combat_mode, off, def))
1111 if (land_answer[(int)land.lnd_army] != 'y' &&
1112 land_answer[(int)land.lnd_army] != 'Y')
1115 if (!(llp = (struct llist *)malloc(sizeof(struct llist)))) {
1116 logerror("Malloc failed in attack!\n");
1120 memset(llp, 0, sizeof(struct llist));
1121 emp_insque(&llp->queue, olist);
1122 llp->mobil = mobcost;
1123 if (!get_land(combat_mode, def, land.lnd_uid, llp, 0))
1125 if (lnd_spyval(&land) > *a_spyp)
1126 *a_spyp = lnd_spyval(&land);
1127 if (llp->lcp->l_flags & L_ENGINEER)
1129 if (def->type == EF_SHIP && ++count >= maxland)
1134 /* What's the offense or defense multiplier? */
1137 att_combat_eff(struct combat *com)
1143 if (com->type == EF_SECTOR) {
1144 eff = com->eff / 100.0;
1145 if (com->own == player->cnum) {
1146 str = com->sct_dcp->d_ostr;
1147 eff = 1.0 + ((str - 1.0) * eff);
1149 eff = sector_strength(getsectp(com->x, com->y));
1150 /* str = com->sct_dcp->d_dstr;*/
1151 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1152 getship(com->shp_uid, &ship);
1153 eff = (1.0 + ship.shp_armor / 100.0);
1159 * Estimate the defense strength and give the attacker a chance to abort
1160 * if the odds are less than 50%
1164 att_estimate_defense(int combat_mode, struct combat *off,
1165 struct emp_qelem *olist, struct combat *def,
1173 * Get the attacker units & mil again in case they changed while the
1174 * attacker was answering sub-prompts.
1177 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1178 if (att_empty_attack(combat_mode, ototal, def))
1179 return abort_attack();
1180 if (combat_mode == A_PARA)
1182 pr("\n Initial attack strength: %8d\n", ototal);
1184 estimate = att_combat_eff(def) * roundintby(def->troops, 10);
1185 estimate += att_combat_eff(def) * get_dlist(def, 0, a_spy, 0);
1188 * Calculate the initial (pre-support) attack odds. If they're less
1189 * than 50%, ask for a confirmation.
1192 odds = (int)(att_calcodds(ototal, estimate) * 100);
1196 pr(" Estimated defense strength: %8d\n", estimate);
1197 pr(" Estimated odds: %8d%%\n\n",odds);
1198 sprintf(prompt, "Are you sure you want to %s [yn]? ",
1199 att_mode[combat_mode]);
1200 if (!confirm(prompt))
1201 return abort_attack();
1202 ototal = get_ototal(combat_mode, off, olist,1.0,1);
1203 if (att_empty_attack(combat_mode, ototal, def))
1204 return abort_attack();
1210 /* Get the defensive units and reacting units */
1212 att_get_defense(struct emp_qelem *olist, struct combat *def,
1213 struct emp_qelem *dlist, int a_spy, int ototal)
1216 struct emp_qelem *qp;
1222 get_dlist(def, dlist, 0, &d_spy);
1223 dtotal = get_dtotal(def, dlist, 1.0, 0);
1226 * Call in reacting units
1229 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1230 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1232 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1233 llp = (struct llist *)qp;
1234 intelligence_report(def->own, &llp->land, d_spy,
1235 "Scouts report attacking unit:");
1238 old_dtotal = dtotal;
1239 dtotal = get_dtotal(def, dlist, 1.0, 0);
1240 if (dtotal != old_dtotal)
1241 pr("Defense strength with reacting units: %8d\n", dtotal);
1246 /* Get the defensive land units in the sector or on the ship */
1249 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1252 struct nstr_item ni;
1257 /* In here is where you need to take out spies and trains from the defending
1258 lists. Spies try to hide, trains get trapped and can be boarded. */
1260 snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1261 while (nxtitem(&ni, (s_char *)&land)) {
1264 if (land.lnd_own != def->own)
1266 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1268 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1270 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1272 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1274 if (!list) { /* Just estimating the enemy strength */
1275 estimate += intelligence_report(player->cnum,
1277 "Scouts report defending unit:");
1280 if (!(llp = (struct llist *)malloc(sizeof(struct llist)))) {
1281 logerror("Malloc failed in attack!\n");
1285 memset(llp, 0, sizeof(struct llist));
1286 emp_insque(&llp->queue, list);
1287 llp->supplied = has_supply(&land);
1288 if (!get_land(A_DEFEND, def, land.lnd_uid, llp, 1))
1290 if (lnd_spyval(&land) > *d_spyp)
1291 *d_spyp = lnd_spyval(&land);
1296 /* Calculate the total offensive strength */
1299 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1300 double osupport, int check)
1302 double ototal = 0.0;
1303 struct emp_qelem *qp, *next;
1308 * first, total the attacking mil
1311 for (n = 0; n <= off->last; ++n) {
1312 if (off[n].type == EF_BAD || (check &&
1313 att_get_combat(&off[n], 0) <= 0))
1315 ototal += off[n].troops * att_combat_eff(off + n);
1319 * next, add in the attack_values of all
1320 * the attacking units
1323 for (qp = olist->q_forw; qp != olist; qp = next) {
1325 llp = (struct llist *)qp;
1326 if (check && !get_land(combat_mode, 0, llp->land.lnd_uid, llp, 0))
1328 if (combat_mode == A_ATTACK) {
1330 for (n = 0; n <= off->last; ++n) {
1331 if (off[n].type == EF_BAD)
1333 if ((off[n].x == llp->land.lnd_x) &&
1334 (off[n].y == llp->land.lnd_y))
1338 lnd_delete(llp, "is in a sector not owned by you");
1341 ototal += attack_val(combat_mode, &llp->land) *
1342 att_combat_eff(off + w);
1344 ototal += attack_val(combat_mode, &llp->land);
1349 return ldround(ototal, 1);
1352 /* Calculate the total defensive strength */
1355 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1358 double dtotal = 0.0, eff = 1.0, d_unit;
1359 struct emp_qelem *qp, *next;
1362 if (check && att_get_combat(def, 1) < 0)
1364 eff = att_combat_eff(def);
1365 dtotal = def->troops * eff;
1368 * next, add in the defense_values of all
1369 * the defending non-retreating units
1372 for (qp = list->q_forw; qp != list; qp = next) {
1374 llp = (struct llist *)qp;
1376 if (check && !get_land(A_DEFEND, def, llp->land.lnd_uid, llp, 1))
1378 d_unit = defense_val(&llp->land);
1380 d_unit = ((double)defense_val(&llp->land) / 2.0);
1381 dtotal += d_unit * eff;
1386 return ldround(dtotal, 1);
1390 * This is the land unit integrity check. Note that we don't print
1391 * warnings about victim land units because the attacker may not have seen them
1395 get_land(int combat_mode, struct combat *def, int uid, struct llist *llp,
1398 struct lndstr *lp = &llp->land;
1403 if (!llp->lcp) { /* first time */
1404 llp->x = llp->land.lnd_x;
1405 llp->y = llp->land.lnd_y;
1406 llp->lcp = &lchr[(int)llp->land.lnd_type];
1407 } else { /* not first time */
1408 if (lp->lnd_effic < LAND_MINEFF) {
1409 sprintf(buf, "was destroyed and is no longer a part of the %s",
1410 att_mode[combat_mode]);
1411 lnd_delete(llp, buf);
1415 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1417 "left to go fight another battle and is no longer a part of the defense");
1421 if (lp->lnd_own != player->cnum) {
1423 "was destroyed and is no longer a part of the %s",
1424 att_mode[combat_mode]);
1425 lnd_delete(llp, buf);
1428 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1430 "left to fight another battle and is no longer a part of the %s",
1431 att_mode[combat_mode]);
1432 lnd_delete(llp, buf);
1435 if (lp->lnd_effic < llp->eff) {
1436 sprintf(buf, "damaged from %d%% to %d%%",
1437 llp->eff, lp->lnd_effic);
1438 lnd_print(llp, buf);
1442 llp->eff = llp->land.lnd_effic;
1448 * Put the land unit on the disk. If there was some mobility cost, then
1449 * subtract it from the units mobility. Note that this works the same way
1450 * as sectors & ships in that no mobility is actually taken until the attacker
1451 * has committed to attacking.
1455 kill_land(struct emp_qelem *list)
1457 struct emp_qelem *qp, *next;
1460 for (qp = list->q_forw; qp != list; qp = next) {
1462 llp = (struct llist *)qp;
1463 if (llp->land.lnd_ship >= 0) {
1464 llp->land.lnd_effic = 0;
1465 lnd_delete(llp, "cannot return to the ship, and dies!");
1471 att_infect_units(struct emp_qelem *list, int plague)
1473 struct emp_qelem *qp, *next;
1478 for (qp = list->q_forw; qp != list; qp = next) {
1480 llp = (struct llist *)qp;
1481 if (llp->land.lnd_pstage == PLG_HEALTHY)
1482 llp->land.lnd_pstage = PLG_EXPOSED;
1487 put_land(struct emp_qelem *list)
1489 struct emp_qelem *qp, *next;
1492 for (qp = list->q_forw; qp != list; qp = next) {
1494 llp = (struct llist *)qp;
1495 llp->land.lnd_mission = 0;
1496 llp->land.lnd_harden = 0;
1497 llp->land.lnd_mobil -= (int)llp->mobil;
1499 putland(llp->land.lnd_uid, &llp->land);
1500 if (llp->land.lnd_own != player->cnum) {
1501 emp_remque((struct emp_qelem *)llp);
1502 free((s_char *)llp);
1504 get_land(A_ATTACK, 0, llp->land.lnd_uid, llp, 0);
1509 * Keep sending in reinforcements until it looks like we're going to win.
1510 * Note that the "strength" command also calls this routine.
1514 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1515 int *d_spyp, int ototal)
1517 struct nstr_item ni;
1519 struct sctstr sect, dsect;
1529 double eff = att_combat_eff(def);
1535 * All units that are within their reaction radius and not damaged
1536 * below their morale value now get to react to the threatened sect.
1537 * Once we've sent enough to counter the threat, stop sending them.
1539 * Not anymore. All units get to react. :)
1543 dtotal = get_dtotal(def, list, 1.0, 1);
1546 snxtitem_all(&ni, EF_LAND);
1547 while (nxtitem(&ni, (s_char *)&land) &&
1548 (dtotal + new_land * eff < (int)(1.2 * (float)ototal))) {
1551 if (!land.lnd_rad_max)
1553 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1555 if (land.lnd_own != def->own)
1557 if (land.lnd_ship >= 0)
1559 if (!defense_val(&land))
1562 if (land.lnd_effic <= land.lnd_retreat)
1565 if (!lnd_can_attack(&land))
1568 /* Only supplied units can react */
1569 if (!(supply_state = has_supply(&land)))
1572 dist = mapdist(land.lnd_x, land.lnd_y, def->x, def->y);
1574 getsect(land.lnd_x, land.lnd_y, §);
1575 /* Units on efficient headquarters can react 1 farther */
1576 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
1577 radius = land.lnd_rad_max + 1;
1579 radius = land.lnd_rad_max;
1581 if (land.lnd_mission == MI_RESERVE)
1587 getsect(def->x, def->y, &dsect);
1588 if (!(p = BestLandPath(buf, §, &dsect, &move_cost, MOB_ROAD)))
1591 mobcost = land.lnd_effic * 0.01 * lchr[(int)land.lnd_type].l_spd;
1594 mobcost = 480.0 / (mobcost + techfact(land.lnd_tech, mobcost));
1595 mobcost *= (move_cost * 5.0);
1597 if (land.lnd_mobil < mobcost)
1600 new_land += defense_val(&land);
1602 if (!list) /* we are in the "strength" command */
1605 /* move to defending sector */
1606 land.lnd_mobil -= ldround(mobcost, 1);
1609 land.lnd_x = def->x;
1610 land.lnd_y = def->y;
1611 putland(land.lnd_uid, &land);
1612 wu(0, land.lnd_own, "%s reacts to %s.\n",
1613 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1615 llp = (struct llist *)
1616 malloc(sizeof(struct llist));
1618 memset(llp, 0, sizeof(struct llist));
1619 llp->supplied = supply_state;
1622 llp->lcp = &lchr[(int)land.lnd_type];
1624 emp_insque(&llp->queue, list);
1625 if (lnd_spyval(&land) > *d_spyp)
1626 *d_spyp = lnd_spyval(&land);
1628 intelligence_report(player->cnum, &land, a_spy,
1629 "Scouts sight reacting enemy unit:");
1634 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1637 get_osupport(s_char *outs, struct combat *def, int fort_sup, int ship_sup,
1638 int land_sup, int plane_sup)
1640 double osupport = 1.0;
1642 double af, as, au, ap;
1644 af = as = au = ap = 0.0;
1646 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1647 af = ((double)dam / 100.0);
1651 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1653 as = ((double)dam / 100.0);
1658 dam = lnd_support(def->own, player->cnum, def->x, def->y);
1659 au = ((double)dam / 100.0);
1664 dam = off_support(def->x, def->y, def->own, player->cnum);
1665 ap = (((double)dam) / 100.0);
1668 sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1673 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1676 get_dsupport(s_char *outs, struct emp_qelem *list, struct combat *def,
1677 int ototal, int dtotal)
1679 double dsupport = 1.0;
1681 double df, ds, du, dp;
1684 df = ds = du = dp = 0.0;
1685 if (dtotal < 0.1 * ototal) {
1687 } else if (dtotal >= 1.2 * ototal) {
1690 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1691 df = ((double)dam / 100.0);
1694 dtotal = get_dtotal(def, list, dsupport, 0);
1695 if (dtotal < 1.2 * ototal) {
1696 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1697 ds = ((double)dam / 100.0);
1699 dtotal = get_dtotal(def, list, dsupport, 0);
1701 if (dtotal < 1.2 * ototal) {
1702 dam = lnd_support(player->cnum, def->own, def->x, def->y);
1703 du = ((double)dam / 100.0);
1705 dtotal = get_dtotal(def, list, dsupport, 1);
1707 if (dtotal < 1.2 * ototal) {
1708 dam = def_support(def->x, def->y, player->cnum, def->own);
1709 dp = (((double)dam) / 100.0);
1716 sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1721 "\nOdds are bad for us...support cancelled.\n\n");
1724 "\nOdds are good for us...support cancelled.\n\n");
1730 * Land mines add to the defense multiplier. If the attacker has engineers
1731 * then this multiplier is cut in half.
1735 get_mine_dsupport(struct combat *def, int a_engineer)
1740 getsect(def->x, def->y, §);
1742 if (sect.sct_oldown != player->cnum) {
1743 mines = min(sect.sct_mines, 20);
1745 mines = ldround(((double)mines / 2.0), 1);
1748 wu(0, def->own, "Defending mines add %1.2f\n",
1750 pr("Defending mines add %1.2f\n", mines * 0.02);
1751 return mines * 0.02;
1757 /* Get the offensive and defensive support */
1759 att_get_support(int combat_mode, int ofort, int oship, int oland,
1760 int oplane, struct emp_qelem *olist, struct combat *off,
1761 struct emp_qelem *dlist, struct combat *def,
1762 double *osupportp, double *dsupportp, int a_engineer)
1765 s_char osupports[512];
1766 s_char dsupports[512];
1768 if (combat_mode == A_PARA)
1771 *osupportp = get_osupport(osupports, def,
1772 ofort, oship, oland, oplane);
1775 * I need to put a 1 at the end of the next four total_stren calls
1776 * becauase units & mil may have been damaged by collateral damage or
1777 * neclear warheads from the offensive & defensive support.
1780 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1781 if (att_empty_attack(combat_mode, ototal, def))
1782 return abort_attack();
1783 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1786 * Calculate defensive support. If odds are too good or too bad
1787 * then don't call in support.
1790 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1791 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1792 if (att_empty_attack(combat_mode, ototal, def))
1793 return abort_attack();
1795 if ((*osupports || *dsupports) &&
1796 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1797 pr("\n\t\tsupport values\n");
1798 pr("\t\tforts\tships\tunits\tplanes\n");
1799 if (*osupportp != 1.0)
1800 pr("%s", osupports);
1801 if (*dsupportp != 1.0)
1802 pr("%s", dsupports);
1804 wu(0, def->own, "\n\t\tsupport values\n");
1805 wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1806 if (*osupportp != 1.0)
1807 wu(0, def->own, "%s", osupports);
1808 if (*dsupportp != 1.0)
1809 wu(0, def->own, "%s", dsupports);
1813 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1814 if (dtotal && def->type == EF_SECTOR)
1815 *dsupportp += get_mine_dsupport(def, a_engineer);
1819 /* How many two-legged bipeds are in this combat force? */
1822 count_bodies(struct combat *off, struct emp_qelem *list)
1826 struct emp_qelem *qp;
1829 for (n = 0; n <= off->last; ++n)
1830 bodies += off[n].troops;
1831 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1832 llp = (struct llist *)qp;
1833 bodies += total_mil(&llp->land);
1838 /* This is where the fighting actually occurs. */
1841 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1842 double osupport, struct combat *def, struct emp_qelem *dlist,
1846 int a_cas = 0; /* Casualty counts */
1848 int ototal; /* total attacking strength */
1849 int dtotal; /* total defending strength */
1850 int a_bodies; /* total attacking mil (incl. mil in units) */
1851 int d_bodies; /* total defending mil (incl. mil in units) */
1860 ototal = get_ototal(combat_mode, off, olist, osupport,
1861 combat_mode != A_PARA);
1862 dtotal = get_dtotal(def, dlist, dsupport, 0);
1866 a_bodies = count_bodies(off, olist);
1867 d_bodies = count_bodies(def, dlist);
1868 d_mil = def->troops;
1869 for (n = 0; n <= off->last; ++n)
1870 if (off[n].type == EF_BAD)
1873 a_troops[n] = off[n].troops;
1875 /* This switch is required to get the spacing right */
1876 switch (combat_mode) {
1878 pr(" Final attack strength: %8d\n", ototal);
1881 pr(" Final assault strength: %8d\n", ototal);
1884 if (def->sct_type == SCT_MOUNT ||
1885 def->sct_type == SCT_WATER ||
1886 def->sct_type == SCT_CAPIT ||
1887 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1888 pr("You can't air-assault a %s sector!\n",
1889 def->sct_dcp->d_name);
1892 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1894 pr(" Final air-assault strength: %8d\n", ototal);
1898 pr(" Final board strength: %8d\n", ototal);
1902 pr(" Final defense strength: %8d\n", dtotal);
1903 odds = att_calcodds(ototal, dtotal);
1904 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1906 /* spread the plague */
1907 if (combat_mode != A_PARA) {
1909 for (n = 0; n <= off->last; ++n)
1910 if (off[n].type != EF_BAD)
1911 def->plague |= off[n].plague;
1912 for (n = 0; n <= off->last; ++n)
1913 if (off[n].type != EF_BAD)
1914 off[n].plague |= def->plague;
1916 att_infect_units(olist, off->plague);
1917 att_infect_units(dlist, def->plague);
1919 /* Fighting is slightly random. There is always that last little
1920 * effort you see people put in. Or the stray bullet that takes out
1921 * an officer and the rest go into chaos. Things like that.
1922 * Thus, we have added a very slight random factor that will sometimes
1923 * allow the little guy to win. We modify the odds a little
1924 * (either +- 5%) to account for this randomness. We also only
1925 * recalculate the odds every 8-50 casualties, not every cacsualty,
1926 * since a single dead guy normally wouldn't cause a commander to
1927 * rethink his strategies, but 50 dead guys might. */
1928 odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1933 recalctime = 8 + (random() % 43);
1934 while (!success && ototal) {
1937 d_cas += take_casualty(A_DEFEND, def, dlist);
1938 dtotal = get_dtotal(def, dlist, dsupport, 0);
1943 a_cas += take_casualty(combat_mode, off, olist);
1944 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1946 if (((a_cas + d_cas) % 70) == 69)
1948 if (recalctime-- <= 0) {
1949 recalctime = 8 + (random() % 43);
1950 odds = att_calcodds(ototal, dtotal);
1951 odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1959 /* update defense mobility & mil */
1963 if (def->type == EF_SECTOR && d_mil && d_cas) {
1966 /* Make sure we use a positive mobility here */
1967 tmob = ((def->mob < 0) ? -(def->mob) : (def->mob));
1969 min(20, min(1, tmob - damage(tmob, 100 * d_cas / d_mil)));
1971 def->mil = def->troops;
1974 /* update attack mobility & mil */
1975 for (n = 0; n <= off->last; ++n)
1976 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1977 if (off[n].type == EF_SECTOR && off[n].mil)
1981 off[n].mob - damage(off[n].mob,
1982 100 * (a_troops[n] -
1985 off[n].mil -= a_troops[n] - off[n].troops;
1988 /* update land unit mobility */
1989 if (d_bodies && d_cas)
1990 lnd_takemob(dlist, (double)d_cas / d_bodies);
1991 if (a_bodies && a_cas)
1992 lnd_takemob(olist, (double)a_cas / a_bodies);
1994 /* damage attacked sector */
1995 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
1997 pr("- Casualties -\n Yours: %d\n", a_cas);
1998 pr(" Theirs: %d\n", d_cas);
1999 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
2000 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
2003 switch (combat_mode) {
2005 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
2006 pr("We have captured %s, sir!\n", prcom(0, def));
2010 news_item = def->own ? N_AWON_SECT : N_START_COL;
2011 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
2012 action = "assaulting and taking";
2015 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
2016 pr("We have captured %s, sir!\n", prcom(0, def));
2017 action = "air-assaulting and taking";
2020 news_item = N_BOARD_SHIP;
2021 pr("We have boarded %s, sir!\n", prcom(0, def));
2022 action = "boarding";
2025 news_item = N_BOARD_LAND;
2026 pr("We have boarded %s, sir!\n", prcom(0, def));
2027 action = "boarding";
2031 switch (combat_mode) {
2033 news_item = N_SCT_LOSE;
2034 pr("You have been defeated!\n");
2035 action = "attacking";
2038 news_item = N_ALOSE_SCT;
2039 pr("You have been defeated!\n");
2041 action = "trying to assault";
2044 news_item = N_PLOSE_SCT;
2045 pr("All of your troops were destroyed\n");
2046 action = "trying to air-assault";
2049 news_item = N_SHP_LOSE;
2050 pr("You have been repelled\n");
2052 action = "trying to board";
2055 news_item = N_LND_LOSE;
2056 pr("You have been repelled\n");
2058 action = "trying to board";
2062 nreport(player->cnum, news_item, def->own, 1);
2065 "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
2066 cname(player->cnum), player->cnum, a_cas,
2067 action, pr_com(0, def, def->own), d_cas);
2070 if (opt_SNEAK_ATTACK) {
2071 do_sneak(def, success);
2074 send_reacting_units_home(dlist);
2076 /* putland the defending land */
2079 /* putland the attacking land */
2082 /* put the victim sector/ship/land */
2083 if (!success || !take_def(combat_mode, olist, off, def))
2086 /* put the attacking sectors/ship */
2087 for (n = 0; n <= off->last; ++n)
2088 if (off[n].type != EF_BAD)
2089 put_combat(&off[n]);
2094 switch (combat_mode) {
2096 ask_move_in(off, olist, def);
2098 /* put sectors again to get abandon warnings */
2099 for (n = 0; n <= off->last; ++n)
2100 if (off[n].type != EF_BAD)
2101 put_combat(&off[n]);
2104 att_move_in_off(combat_mode, off, olist, def);
2107 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2111 /* What percentage of the combat forces going head-to-head are we? */
2114 att_calcodds(int ototal, int dtotal)
2118 /* calculate odds */
2121 else if (dtotal <= 0)
2124 odds = ((double)ototal) / (dtotal + ototal);
2129 /* Here's where the dead soldiers get dragged off the battlefield */
2132 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2134 int to_take = CASUALTY_LUMP;
2135 int biggest_troops = 0, index = -1;
2136 int n, tot_troops = 0, biggest_mil, cas;
2137 struct emp_qelem *qp, *biggest;
2140 for (n = 0; n <= off->last; ++n) {
2141 if (off[n].type != EF_BAD) {
2142 tot_troops += off[n].troops;
2143 if (off[n].troops > biggest_troops) {
2144 biggest_troops = off[n].troops;
2151 to_take -= tot_troops;
2154 for (n = 0; n <= off->last; ++n)
2155 if (off[n].type != EF_BAD)
2159 * They can all come off mil. We rotate the casualties,
2160 * starting with the sector containing the most mil.
2162 to_take = CASUALTY_LUMP;
2164 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2167 while (to_take > 0) {
2168 for (n = index; n <= off->last && to_take; ++n) {
2169 if (off[n].type != EF_BAD && off[n].troops > 0) {
2174 for (n = 0; n < index && to_take; ++n) {
2175 if (off[n].type != EF_BAD && off[n].troops > 0) {
2181 return CASUALTY_LUMP;
2185 return (CASUALTY_LUMP - to_take);
2188 * Need to take some casualties from attacking units
2189 * Procedure: find the biggest unit remaining (in
2190 * terms of mil) and give it the casualties.
2192 biggest = (struct emp_qelem *)0;
2194 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2195 llp = (struct llist *)qp;
2197 if (total_mil(&llp->land) > biggest_mil) {
2198 biggest_mil = total_mil(&llp->land);
2202 if (biggest == (struct emp_qelem *)0)
2203 return (CASUALTY_LUMP - to_take);
2205 llp = (struct llist *)biggest;
2206 cas = lnd_take_casualty(combat_mode, llp, to_take);
2207 return (CASUALTY_LUMP - (to_take - cas));
2210 /* Send reacting defense units back to where they came from (at no mob cost) */
2213 send_reacting_units_home(struct emp_qelem *list)
2215 struct emp_qelem *qp, *next;
2219 for (qp = list->q_forw; qp != list; qp = next) {
2221 llp = (struct llist *)qp;
2222 if ((llp->land.lnd_x != llp->x) || (llp->land.lnd_y != llp->y)) {
2223 sprintf(buf, "returns to %s",
2224 xyas(llp->x, llp->y, llp->land.lnd_own));
2225 llp->land.lnd_x = llp->x;
2226 llp->land.lnd_y = llp->y;
2227 lnd_delete(llp, buf);
2232 /* Check for 0 offense strength. This call will always preceed an abort */
2235 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2238 if (def->own && player->cnum != def->own) {
2240 "%s (#%d) considered %sing you @%s\n",
2241 cname(player->cnum), player->cnum,
2242 att_mode[combat_mode], xyas(def->x, def->y, def->own));
2244 pr("No troops for %s...\n", att_mode[combat_mode]);
2251 * Take the defending sector or ship from the defender and give it to the
2256 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2261 struct llist *llp, *delete_me = 0;
2267 for (n = 0; n <= off->last && !occuppied; ++n) {
2268 if (off[n].type != EF_BAD &&
2269 off[n].troops > 0 &&
2270 (off[n].type != EF_SECTOR || off[n].mob)) {
2272 if (def->type == EF_LAND) {
2273 if (def->lnd_lcp->l_flags & L_SPY) {
2280 pr("1 mil from %s moves %s\n",
2281 prcom(0, off + n), prcom(2, def));
2286 pr("%s left unoccupied\n", prcom(0, def));
2289 "No enemy troops moved %s so you still own it!\n",
2290 pr_com(2, def, def->own));
2293 llp = (struct llist *)list->q_forw;
2294 llp->land.lnd_x = def->x;
2295 llp->land.lnd_y = def->y;
2296 take_move_in_mob(combat_mode, llp, off, def);
2297 if (def->type == EF_SHIP) {
2298 llp->land.lnd_ship = def->shp_uid;
2299 sprintf(buf, "boards %s", prcom(0, def));
2302 llp->land.lnd_ship = -1;
2303 sprintf(buf, "moves in to occupy %s",
2304 xyas(def->x, def->y, player->cnum));
2305 lnd_delete(llp, buf);
2310 if (def->type == EF_SECTOR) {
2311 getsect(def->x, def->y, §);
2312 takeover(§, player->cnum);
2313 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2314 caploss(§, def->own,
2315 "* We have captured %s's capital, sir! *\n");
2317 } else if (def->type == EF_SHIP) {
2318 getship(def->shp_uid, &ship);
2319 takeover_ship(&ship, player->cnum, 1);
2320 putship(ship.shp_uid, &ship);
2321 } else if (def->type == EF_LAND) {
2322 getland(def->lnd_uid, &land);
2323 takeover_land(&land, player->cnum, 1);
2324 putland(land.lnd_uid, &land);
2327 lnd_delete(delete_me, buf);
2328 att_get_combat(def, 0);
2333 * Ask the attacker which mil & land units they'd like to move into the
2338 ask_move_in(struct combat *off, struct emp_qelem *olist,
2342 struct emp_qelem *qp, *next;
2346 s_char land_answer[1024];
2349 for (n = 0; n <= off->last; ++n)
2350 if (off[n].type != EF_BAD && off[n].troops > 0)
2352 ask_move_in_off(&off[n], def);
2353 if (player->aborted)
2359 memset(land_answer, 0, sizeof(land_answer));
2360 for (qp = olist->q_forw; qp != olist; qp = next) {
2362 llp = (struct llist *)qp;
2363 answerp = &land_answer[(int)llp->land.lnd_army];
2364 if (player->aborted || att_get_combat(def, 0) < 0)
2366 if (*answerp == 'Y')
2368 if (*answerp != 'N') {
2369 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2371 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2373 llp->land.lnd_army == ' ' ? '~' : llp->land.lnd_army,
2374 llp->land.lnd_effic);
2375 *answerp = att_prompt(prompt, llp->land.lnd_army);
2376 if (player->aborted || att_get_combat(def, 0) < 0)
2378 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2381 if (*answerp == 'y' || *answerp == 'Y')
2383 sprintf(buf, "stays in %s",
2384 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2385 lnd_delete(llp, buf);
2389 if (att_get_combat(def, 0) < 0) {
2390 for (qp = olist->q_forw; qp != olist; qp = next) {
2392 llp = (struct llist *)qp;
2393 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2395 sprintf(buf, "stays in %s",
2396 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2397 lnd_delete(llp, buf);
2401 if (opt_INTERDICT_ATT)
2402 lnd_interdict(olist, def->x, def->y, player->cnum);
2403 move_in_land(A_ATTACK, off, olist, def);
2406 /* Move offensive land units to the conquered sector or ship */
2409 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2412 struct emp_qelem *qp, *next;
2418 for (qp = olist->q_forw; qp != olist; qp = next) {
2420 llp = (struct llist *)qp;
2421 if (!get_land(combat_mode, def, llp->land.lnd_uid, llp, 0))
2423 take_move_in_mob(combat_mode, llp, off, def);
2424 llp->land.lnd_x = def->x;
2425 llp->land.lnd_y = def->y;
2426 if (def->type == EF_SHIP)
2427 llp->land.lnd_ship = def->shp_uid;
2429 llp->land.lnd_ship = -1;
2433 if (def->type == EF_SECTOR) {
2434 if (opt_INTERDICT_ATT) {
2435 lnd_sweep(olist, 0, 0, def->own);
2436 lnd_check_mines(olist);
2438 sprintf(buf, "now occupies %s", prcom(0, def));
2440 sprintf(buf, "boards %s", prcom(0, def));
2444 for (qp = olist->q_forw; qp != olist; qp = next) {
2446 llp = (struct llist *)qp;
2447 lnd_print(llp, buf);
2455 * Move assaulting, paradropping, or boarding mil & units into def
2456 * If the mil are coming from a ship, then pack a lunch.
2460 att_move_in_off(int combat_mode, struct combat *off,
2461 struct emp_qelem *olist, struct combat *def)
2468 move_in_land(combat_mode, off, olist, def);
2470 getsect(def->x, def->y, §);
2472 for (n = 0; n <= off->last; ++n) {
2473 if (off[n].type == EF_BAD || !off[n].troops)
2475 troops = off[n].troops;
2477 off[n].mil -= troops;
2479 put_combat(off + n);
2480 if (combat_mode == A_ASSAULT) {
2481 if (off[n].type != EF_SHIP || def->type != EF_SECTOR) {
2482 logerror("att_move_in_off: strange assault");
2485 getship(off[n].shp_uid, &ship);
2486 lunchbox = (int)((troops + 1) * ship.shp_item[I_FOOD]
2487 / (ship.shp_item[I_MILIT] + troops
2488 + ship.shp_item[I_CIVIL] + 0.5));
2489 if (lunchbox > ITEM_MAX - sect.sct_item[I_FOOD])
2490 lunchbox = ITEM_MAX - sect.sct_item[I_FOOD];
2492 ship.shp_item[I_FOOD] -= lunchbox;
2493 sect.sct_item[I_FOOD] += lunchbox;
2494 putship(ship.shp_uid, &ship);
2503 /* Ask how many mil to move in from each sector */
2506 ask_move_in_off(struct combat *off, struct combat *def)
2509 int num_mil, dam = 0, left;
2515 if (att_get_combat(off, 0) <= 0)
2517 if (att_get_combat(def, 0) < 0)
2519 if (off->own != player->cnum)
2521 d = sector_mcost(getsectp(def->x, def->y), MOB_ROAD);
2522 if ((mob_support = min(off->troops, (int)(off->mob / d))) <= 0)
2524 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2525 xyas(off->x, off->y, player->cnum), mob_support);
2526 if (!(p = getstring(prompt, buf)) || !*p || (num_mil = atoi(p)) <= 0) {
2530 /* Make sure we don't move in more than we can support mobility-wise */
2531 if (num_mil > mob_support)
2532 num_mil = mob_support;
2533 if (att_get_combat(off, 0) <= 0)
2535 if (att_get_combat(def, 0) < 0)
2537 if ((num_mil = min(off->troops, num_mil)) <= 0) {
2538 pr("No mil moved in from %s\n",
2539 xyas(off->x, off->y, player->cnum));
2542 mob_support = max(1, (int)(num_mil * d));
2543 off->mob -= min(off->mob, mob_support);
2544 off->mil -= num_mil;
2545 off->troops -= num_mil;
2548 weight = num_mil * ichr[I_MILIT].i_lbs;
2549 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2550 if (chance(weight / 100.0))
2552 ground_interdict(def->x, def->y, player->cnum, "military");
2553 dam += check_lmines(def->x, def->y, weight);
2557 left = commdamage(num_mil, dam, I_MILIT);
2558 if (left < num_mil) {
2560 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));
2562 pr("All of the mil you were moving were destroyed!\n");
2565 /* maybe got nuked */
2566 if (att_get_combat(def, 0) < 0)
2574 /* Charge land units for moving into a sector or onto a ship */
2577 take_move_in_mob(int combat_mode, struct llist *llp, struct combat *off,
2583 switch (combat_mode) {
2586 lnd_mobcost(&llp->land, getsectp(def->x, def->y), MOB_NONE);
2587 new = llp->land.lnd_mobil - mobcost;
2590 llp->land.lnd_mobil = new;
2593 if (off->shp_mcp->m_flags & M_LAND) {
2594 if (llp->lcp->l_flags & L_MARINE)
2595 llp->land.lnd_mobil -=
2596 ((float)etu_per_update * land_mob_scale * 0.5);
2598 llp->land.lnd_mobil -= ((float)etu_per_update
2601 if (llp->lcp->l_flags & L_MARINE)
2602 llp->land.lnd_mobil = 0;
2604 llp->land.lnd_mobil = (((float)etu_per_update
2605 * land_mob_scale) * (-1));
2609 /* I arbitrarily chose the numbers 10 and 40 below -KHS */
2610 if (llp->lcp->l_flags & L_MARINE)
2611 llp->land.lnd_mobil -= 10;
2613 llp->land.lnd_mobil -= 40;
2616 llp->land.lnd_harden = 0;
2620 free_list(struct emp_qelem *list)
2622 struct emp_qelem *qp, *next;
2624 if (!list || QEMPTY(list))
2628 while (qp != list) {
2637 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2645 * sector_strength - Everyone starts at 1. You can get up to a max
2646 * of d_dstr, depending on how much you build up the
2647 * defenses of the sector.
2651 sector_strength(struct sctstr *sp)
2657 if (sp->sct_type == SCT_MOUNT)
2660 d = d + ((double)(dchr[sp->sct_type].d_dstr - d) *
2661 ((double)sp->sct_defense / 100.0));
2663 if (d > dchr[sp->sct_type].d_dstr)
2664 d = dchr[sp->sct_type].d_dstr;