2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * attsub.c: Attack subroutines
30 * Known contributors to this file:
32 * Steve McClure, 1996-2000
53 #include "prototypes.h"
55 #define CASUALTY_LUMP 1 /* How big casualty chunks should be */
57 static void ask_olist(int combat_mode, struct combat *off,
58 struct combat *def, struct emp_qelem *olist,
59 s_char *land_answer, int *a_spyp, int *a_engineerp);
60 static void take_move_in_mob(int combat_mode, struct llist *llp,
61 struct combat *off, struct combat *def);
62 static void move_in_land(int combat_mode, struct combat *off,
63 struct emp_qelem *olist, struct combat *def);
64 static void ask_move_in(struct combat *off, struct emp_qelem *olist,
66 static void ask_move_in_off(struct combat *off, struct combat *def);
68 static int board_abort(struct combat *off, struct combat *def);
69 static int land_board_abort(struct combat *off, struct combat *def);
70 static int ask_off(int combat_mode, struct combat *off,
72 static int get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
74 static int get_ototal(int combat_mode, struct combat *off,
75 struct emp_qelem *olist, double osupport, int check);
76 static int get_dtotal(struct combat *def, struct emp_qelem *list,
77 double dsupport, int check);
78 static int take_casualty(int combat_mode, struct combat *off,
79 struct emp_qelem *olist);
81 static void send_reacting_units_home(struct combat *def,
82 struct emp_qelem *list);
83 static int take_def(int combat_mode, struct emp_qelem *list,
84 struct combat *off, struct combat *def);
86 void att_move_in_off(int combat_mode, struct combat *off,
87 struct emp_qelem *olist, struct combat *def);
88 static int get_land(int combat_mode, struct combat *def, int uid,
89 struct llist *llp, int victim_land);
92 * The principal object in this code is the "combat" object. A combat object
93 * is either a sector or ship. There are
94 * usually two instances of this, the "def" or defense combat object, and
95 * the array of "off" or offense objects. The number of offense objects is
96 * determined by the value of off->last (e.g. more than one attacking sector).
97 * the type of the object is determined by combat->type which can take the
98 * values EF_SECTOR, EF_SHIP, EF_PLANE, or EF_BAD. Another important parameter
99 * which is often passed to these functions is combat_mode. This can take
100 * the value A_DEFENSE, A_ATTACK, A_ASSAULT, A_PARA, A_BOARD and A_LBOARD.
101 * As these six modes of being in combat affect things like mobcost and combat
102 * value, there are often switches made on combat_mode. Note that in all cases
103 * no mobility is taken from sectors, ships, or land units until the player
104 * has committed to a fight. Instead, the cost is temporarily placed in
105 * combat->mobcost, or llp->mobil as the case may be, and then when the object
106 * is "put" back onto disk, then the amounts in these variables are subtracted
107 * from the object's mobility. It needs to be done this way as the objects
108 * are constantly being re-read from disk, and we don't want to take any mob
109 * unless a fight actually occurrs.
113 /* initialize combat object */
116 att_combat_init(struct combat *com, int type)
118 memset(com, 0, sizeof(*com));
123 /* print a combat object with optional preposition */
126 pr_com(int inon, struct combat *com, natid who)
128 if (com->type == EF_SECTOR) {
130 inon ? inon == 1 ? "in " : "into " : "",
131 xyas(com->x, com->y, who));
132 } else if (com->type == EF_SHIP) {
134 return prbuf("%s%s %s(#%d)",
135 inon ? inon == 1 ? "on " : "onto " : "",
136 com->shp_mcp->m_name, com->shp_name,
139 return prbuf("%s%s #%d",
140 inon ? inon == 1 ? "on " : "onto " : "",
141 com->shp_mcp->m_name, com->shp_uid);
143 } else if (com->type == EF_LAND) {
144 return prbuf("%s%s #%d",
145 inon ? inon == 1 ? "on " : "onto " : "",
146 com->lnd_lcp->l_name, com->lnd_uid);
148 return "your forces";
153 prcom(int inon, struct combat *com)
155 return pr_com(inon, com, player->cnum);
158 /* Doing a sneak attack */
160 do_sneak(def, success)
165 struct natstr *natp = getnatp(player->cnum);
166 int issneak = getrel(natp, def->own);
168 if (def->type != EF_SECTOR)
171 getsect(def->x, def->y, §);
173 if (issneak == AT_WAR || !def->own || sect.sct_oldown == player->cnum)
177 pr("Your sneak attack was successful\nBut ");
179 pr("Your sneak attack was unsuccessful\nAnd ");
181 pr("it will cost you $5000\n");
182 pr("War has been declared!!!!\n");
183 wu(0, def->own, "Country %s (#%d) has Sneak Attacked!!\n",
184 cname(player->cnum), player->cnum);
185 wu(0, def->own, "Country %s (#%d) has Declared WAR on you!!\n",
186 cname(player->cnum), player->cnum);
187 player->dolcost += 5000;
188 issneak = min(issneak, MOBILIZATION);
189 nreport(player->cnum, N_DECL_WAR, def->own, 1);
190 setrel(player->cnum, def->own, issneak);
194 * This is the combat object "type" based integrity check. It basically
195 * splits along three divisions: ship/sector, attacker/defender,
196 * first time/not first time.
200 att_get_combat(struct combat *com, int isdef)
214 if (!getsect(com->x, com->y, §)) {
215 pr("Bad sector: %s\n", xyas(com->x, com->y, player->cnum));
216 return att_combat_init(com, EF_BAD);
218 com->sct_type = sect.sct_type;
219 com->sct_dcp = &dchr[sect.sct_type];
220 thing = (s_char *)§
221 owner = sect.sct_own;
222 eff = sect.sct_effic;
223 mob = sect.sct_mobil;
228 if (!getland(com->lnd_uid, &land)) {
230 pr("Land unit #%d is not in the same sector!\n",
232 return att_combat_init(com, EF_BAD);
234 if (isdef && player->owner) {
235 pr("Boarding yourself? Try using the 'load' command.\n");
236 return att_combat_init(com, EF_BAD);
238 com->lnd_lcp = &lchr[(int)land.lnd_type];
239 thing = (s_char *)&land;
240 owner = land.lnd_own;
241 eff = land.lnd_effic;
242 mob = land.lnd_mobil;
247 if (!getship(com->shp_uid, &ship) || !ship.shp_own) {
249 pr("Ship #%d is not in the same sector!\n", com->shp_uid);
251 pr("Ship #%d is not your ship!\n", com->shp_uid);
252 return att_combat_init(com, EF_BAD);
255 if (isdef && player->owner &&
256 ontradingblock(EF_SHIP, (int *)&ship)) {
257 pr("%s is on the trading block.\n", prcom(0, com));
258 return att_combat_init(com, EF_BAD);
261 if (isdef && player->owner) {
262 pr("Boarding yourself? Try using the 'tend' command.\n");
263 return att_combat_init(com, EF_BAD);
265 com->shp_mcp = &mchr[(int)ship.shp_type];
267 strncpy(com->shp_name, ship.shp_name, MAXSHPNAMLEN);
268 if (!isdef && !player->owner) {
270 pr("%s was just sunk!\n", prcom(0, com));
272 pr("Ship #%d is not your ship!\n", com->shp_uid);
273 return att_combat_init(com, EF_BAD);
275 thing = (s_char *)&ship;
276 owner = ship.shp_own;
277 eff = ship.shp_effic;
278 mob = ship.shp_mobil;
287 return att_combat_init(com, EF_BAD);
290 mil = getvar(V_MILIT, thing, com->type);
291 if (!com->set) { /* first time */
292 if (isdef) { /* defender */
294 } else { /* attacker */
296 pr("No mil %s\n", prcom(1, com));
298 pr("Only 1 mil %s\n", prcom(1, com));
299 /* don't abandon attacking sectors or ships */
300 com->troops = max(0, mil - 1);
302 com->plague = (getvar(V_PSTAGE, thing, com->type)) == PLG_INFECT;
303 } else { /* not first time */
304 if (isdef) { /* defender */
305 if (com->x != x || com->y != y) {
306 pr("%s has moved!\n", prcom(0, com));
307 return att_combat_init(com, EF_BAD);
309 if (owner != com->own) {
311 pr("WARNING: The ownership of %s just changed from %s to %s!\n", prcom(0, com), cname(com->own), cname(owner));
312 } else if (com->type == EF_SECTOR) {
313 pr("WARNING: %s just abandoned sector %s!\n",
314 cname(com->own), xyas(com->x, com->y,
319 pr("WARNING: The enemy mil %s just %s from %d to %d!\n",
321 com->mil < mil ? "increased" : "decreased", com->mil,
324 } else { /* attacker */
325 if (owner != player->cnum && getrel(getnatp(owner), player->cnum) != ALLIED) { /* must be EF_SECTOR */
327 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));
329 pr("You no longer own %s\n",
330 xyas(com->x, com->y, player->cnum));
331 return att_combat_init(com, EF_BAD);
333 if (com->troops && com->troops + 1 > mil) {
334 if (com->own == owner && player->cnum == owner) /* not a takeover */
335 pr("WARNING: Your mil %s has been reduced from %d to %d!\n", prcom(1, com), com->troops, max(0, mil - 1));
336 com->troops = max(0, mil - 1);
351 * In the course of the fight, the combat object may have lost mil, eff, or
352 * mobility. This is the place where the data in the object gets flushed to
353 * disk to make it "real".
357 put_combat(struct combat *com)
366 getsect(com->x, com->y, §);
367 sect.sct_type = com->sct_type;
368 deff = sect.sct_effic - com->eff;
370 sect.sct_road -= (sect.sct_road * deff / 100.0);
371 sect.sct_rail -= (sect.sct_rail * deff / 100.0);
372 sect.sct_defense -= (sect.sct_defense * deff / 100.0);
373 if (sect.sct_road <= 0)
375 if (sect.sct_rail <= 0)
377 if (sect.sct_defense <= 0)
378 sect.sct_defense = 0;
380 sect.sct_effic = com->eff;
381 if (!opt_DEFENSE_INFRA)
382 sect.sct_defense = sect.sct_effic;
384 if (opt_MOB_ACCESS) {
385 if ((com->mob - com->mobcost) < -127)
386 sect.sct_mobil = -127;
388 sect.sct_mobil = (short)(com->mob - com->mobcost);
390 if ((com->mob - com->mobcost) < 0)
393 sect.sct_mobil = (short)(com->mob - com->mobcost);
396 makelost(EF_SECTOR, sect.sct_own, 0, sect.sct_x, sect.sct_y);
397 makenotlost(EF_SECTOR, com->own, 0, sect.sct_x, sect.sct_y);
398 sect.sct_own = com->own;
400 if (getvar(V_PSTAGE, (s_char *)§, EF_SECTOR) ==
402 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)§, EF_SECTOR);
404 putvar(V_MILIT, com->mil, (s_char *)§, EF_SECTOR);
406 com->own = sect.sct_own; /* avoid WARNING if sector reverts */
409 getland(com->lnd_uid, &land);
410 land.lnd_effic = com->eff;
412 if (com->mob - com->mobcost < -127)
413 land.lnd_mobil = -127;
415 land.lnd_mobil = (s_char)(com->mob - com->mobcost);
417 makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
419 land.lnd_own = com->own;
420 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
423 if (getvar(V_PSTAGE, (s_char *)&land, EF_LAND) == PLG_HEALTHY)
424 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)&land, EF_LAND);
426 if (!(com->lnd_lcp->l_flags & L_SPY))
427 putvar(V_MILIT, com->mil, (s_char *)&land, EF_LAND);
428 lnd_count_units(&land);
429 if (com->own == player->cnum) {
430 land.lnd_mission = 0;
432 memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
434 putland(com->lnd_uid, &land);
437 getship(com->shp_uid, &ship);
438 ship.shp_effic = com->eff;
440 if (com->mob - com->mobcost < -127)
441 ship.shp_mobil = -127;
443 ship.shp_mobil = (s_char)(com->mob - com->mobcost);
445 makelost(EF_SHIP, ship.shp_own, ship.shp_uid, ship.shp_x,
447 ship.shp_own = com->own;
448 makenotlost(EF_SHIP, ship.shp_own, ship.shp_uid, ship.shp_x,
451 if (getvar(V_PSTAGE, (s_char *)&ship, EF_SHIP) == PLG_HEALTHY)
452 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)&ship, EF_SHIP);
454 putvar(V_MILIT, com->mil, (s_char *)&ship, EF_SHIP);
456 if (com->own == player->cnum) {
457 ship.shp_mission = 0;
459 memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
461 putship(com->shp_uid, &ship);
464 att_get_combat(com, com->own != player->cnum);
467 /* If pre-attack, abort fight. If post-attack, don't move anything in */
472 return player->aborted = 1;
476 * This is the combat_mode based integrity check. It splits among two main
477 * divisions: first time/not first time, and attack/assault/para/board.
481 att_abort(int combat_mode, struct combat *off, struct combat *def)
490 if (att_get_combat(def, 1) < 0)
491 return abort_attack();
493 if (off && combat_mode != A_ATTACK) {
494 if (att_get_combat(off, 0) < 0)
495 return abort_attack();
496 if (off->type == EF_SHIP &&
497 (!getsect(off->x, off->y, §) ||
498 sect.sct_type != SCT_WATER)) {
499 pr("%s can not %s from that far inland!\n",
500 prcom(0, off), att_mode[combat_mode]);
501 return abort_attack();
504 switch (combat_mode) {
506 if (!neigh(def->x, def->y, player->cnum) &&
507 !adj_units(def->x, def->y, player->cnum)) {
508 pr("You are not adjacent to %s\n",
509 xyas(def->x, def->y, player->cnum));
510 return abort_attack();
512 if (def->own == player->cnum) {
513 pr("You can't attack your own sector.\n");
514 return abort_attack();
518 if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
519 pr("You'll have to get there first...\n");
520 return abort_attack();
522 if (off && def->sct_type == SCT_MOUNT) {
523 pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
524 return abort_attack();
528 if (def->own == player->cnum) {
529 pr("You can't air-assault your own sector.\n");
530 return abort_attack();
532 if (off && (def->sct_type == SCT_MOUNT ||
533 def->sct_type == SCT_WATER ||
534 def->sct_type == SCT_CAPIT ||
535 def->sct_type == SCT_FORTR ||
536 def->sct_type == SCT_WASTE)) {
537 pr("You can't air-assault a %s sector!\n",
538 def->sct_dcp->d_name);
539 return abort_attack();
543 return board_abort(off, def);
545 return land_board_abort(off, def);
548 if (off && def->sct_dcp->d_mcst <= 0) {
549 pr("You can't %s a %s sector!\n", att_mode[combat_mode],
550 def->sct_dcp->d_name);
551 return abort_attack();
553 if (!off || off->relations_checked)
555 off->relations_checked = 1;
558 setcont(player->cnum, def->own, FOUND_SPY);
559 setcont(def->own, player->cnum, FOUND_SPY);
561 if (opt_SLOW_WAR && def->own != player->cnum) {
562 natp = getnatp(player->cnum);
563 issneak = getrel(natp, def->own);
565 if (issneak == ALLIED) {
566 sprintf(y_or_n, "Sector is owned by %s, your ally, %s [yn]? ",
567 cname(def->own), att_mode[combat_mode]);
568 if (!confirm(y_or_n))
569 return abort_attack();
572 if (opt_SNEAK_ATTACK) {
573 getsect(def->x, def->y, §);
574 if ((issneak != AT_WAR) && (def->own)
575 && (def->own != player->cnum)
576 && (sect.sct_oldown != player->cnum)
577 && (issneak != SITZKRIEG) && (issneak != MOBILIZATION)) {
578 pr("You're not at war with them!\n");
580 ("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;
782 getstarg(player->argp[offset], "Use fort support? ", buf)))
785 if ((*p == 'y') || (*p == 'Y'))
790 getstarg(player->argp[offset + 1], "Use ship support? ",
794 if ((*p == 'y') || (*p == 'Y'))
799 getstarg(player->argp[offset + 2], "Use land support? ",
803 if ((*p == 'y') || (*p == 'Y'))
808 getstarg(player->argp[offset + 3], "Use plane support? ",
812 if ((*p == 'y') || (*p == 'Y'))
819 * Attack, assault, and board ask the attacker what they'd like to attack
820 * with. This includes mil and land units from each "off" object. Note that
821 * after each sub-prompt, we check to make sure that the attack is still
822 * valid, and if it's not, then we abort the attack.
826 att_ask_offense(int combat_mode, struct combat *off, struct combat *def,
827 struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
830 s_char land_answer[1024];
833 if (att_abort(combat_mode, off, def))
835 memset(land_answer, 0, sizeof(land_answer));
836 for (n = 0; n <= off->last; ++n) {
837 off[n].troops = ask_off(combat_mode, off + n, def);
838 if (att_abort(combat_mode, off, def))
840 ask_olist(combat_mode, off + n, def, olist, land_answer,
841 a_spyp, a_engineerp);
842 if (att_abort(combat_mode, off, def))
848 /* How many mil is off allowed to attack with when it attacks def? */
851 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
855 switch (combat_mode) {
857 mob_support = off->mob / sector_mcost(getsectp(def->x, def->y),
861 /* mob_support = off->mob / sector_mcost(def->sct_type, def->eff);*/
862 if (mob_support < off->troops)
863 pr("Sector %s has %d mobility which can only support %d mil,\n", xyas(off->x, off->y, player->cnum), off->mob, mob_support);
865 mob_support = off->troops;
868 if (def->own != player->cnum && def->mil) {
869 if (off->shp_mcp->m_flags & M_SEMILAND)
870 return off->troops / 4;
871 else if (!(off->shp_mcp->m_flags & M_LAND))
872 return off->troops / 10;
876 if (off->type == EF_SECTOR && off->mob <= 0)
878 mob_support = vl_find(V_MILIT, def->shp_mcp->m_vtype,
879 def->shp_mcp->m_vamt, def->shp_mcp->m_nv);
880 if (mob_support < off->troops)
881 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
883 mob_support = off->troops;
888 if (def->lnd_lcp->l_flags & L_SPY)
890 mob_support = vl_find(V_MILIT, def->lnd_lcp->l_vtype,
891 def->lnd_lcp->l_vamt, def->lnd_lcp->l_nv);
892 if (mob_support < off->troops)
893 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
895 mob_support = off->troops;
902 * If the attacker decides to go through with the attack, then the
903 * sectors/ships they are attacking with may be charged some mobility.
904 * This is where that amount of mobility is calculated. It is actually
905 * subtracted "for real" from the object's mobility in put_combat().
909 calc_mobcost(int combat_mode, struct combat *off, struct combat *def,
916 switch (combat_mode) {
920 (int)(attacking_mil *
921 sector_mcost(getsectp(def->x, def->y), MOB_ROAD)));
924 off->mobcost += max(1, attacking_mil / 5);
929 off->mobcost += max(1, attacking_mil / 5);
932 /* the 2 in the formula below is a fudge factor */
933 getship(def->shp_uid, &ship);
934 off->mobcost += (def->eff / 100) * (ship.shp_speed / 2);
939 /* How many mil to we want to attack from off against def? */
942 ask_off(int combat_mode, struct combat *off, struct combat *def)
948 if (att_get_combat(off, 0) <= 0)
950 if ((off->type == EF_SECTOR) && (off->own != player->cnum))
952 if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
954 if (off->type == EF_SECTOR) {
955 if (off->own != player->cnum)
957 sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
958 off->sct_dcp->d_name,
959 xyas(off->x, off->y, player->cnum), mob_support);
961 sprintf(prompt, "Number of mil from %s (max %d) : ",
962 prcom(0, off), mob_support);
964 if ((attacking_mil = onearg(0, prompt)) < 0)
966 if (att_abort(combat_mode, off, def))
968 if (att_get_combat(off, 0) <= 0)
971 min(attacking_mil, min(mob_support, off->troops))) <= 0)
974 calc_mobcost(combat_mode, off, def, attacking_mil);
975 return attacking_mil;
979 * Which units would you like to attack with or move in with [ynYNq?]
983 att_prompt(s_char *prompt, s_char army)
991 while (!p || (*p != 'y' && *p != 'n' && *p != 'Y' && *p != 'N')) {
992 if (p && *p == 'q') {
999 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);
1000 p = getstring(prompt, buf);
1001 if (player->aborted) {
1009 /* Ask the attacker which units they want to attack/assault/board with */
1012 ask_olist(int combat_mode, struct combat *off, struct combat *def,
1013 struct emp_qelem *olist, s_char *land_answer, int *a_spyp,
1016 struct nstr_item ni;
1020 struct lchrstr *lcp;
1027 if (def->type == EF_LAND)
1029 if (def->type == EF_SHIP)
1030 maxland = def->shp_mcp->m_nland;
1032 snxtitem_xy(&ni, EF_LAND, off->x, off->y);
1033 while (nxtitem(&ni, (s_char *)&land)) {
1034 if (land.lnd_own != player->cnum)
1036 if (land.lnd_effic < LAND_MINEFF)
1038 if (land_answer[(int)land.lnd_army] == 'N')
1040 if (!lnd_can_attack(&land))
1042 lcp = &lchr[(int)land.lnd_type];
1044 if (def->type == EF_SHIP && !maxland) {
1045 pr("Land units are not able to board this kind of ship\n");
1048 if (land.lnd_mobil <= 0) {
1049 pr("%s is out of mobility, and cannot %s\n",
1050 prland(&land), att_mode[combat_mode]);
1055 if (ontradingblock(EF_LAND, (int *)&land)) {
1056 pr("%s is on the trading block, and cannot %s\n",
1057 prland(&land), att_mode[combat_mode]);
1062 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
1063 pr("%s is on ship #%d, and cannot %s\n",
1064 prland(&land), land.lnd_ship, att_mode[combat_mode]);
1066 } else if (off->type == EF_SHIP) {
1067 if (land.lnd_ship != off->shp_uid)
1069 } else if (land.lnd_land >= 0) {
1070 pr("%s is on unit #%d, and cannot %s\n",
1071 prland(&land), land.lnd_land, att_mode[combat_mode]);
1074 switch (combat_mode) {
1077 lnd_mobcost(&land, getsectp(def->x, def->y), MOB_NONE);
1078 if (land.lnd_mobil < mobcost) {
1079 pr("%s does not have enough mobility (%d needed)\n",
1080 prland(&land), (int)ceil(mobcost));
1087 if (!(lcp->l_flags & L_ASSAULT))
1090 att_val = attack_val(combat_mode, &land);
1092 pr("%s has no offensive strength\n", prland(&land));
1095 resupply_all(&land);
1096 putland(land.lnd_uid, &land);
1097 if (!has_supply(&land)) {
1098 pr("%s is out of supply, and cannot %s\n",
1099 prland(&land), att_mode[combat_mode]);
1102 if (def->type == EF_SHIP && first_time) {
1104 pr("You may board with a maximum of %d land units\n", maxland);
1106 pr("%s has a base %s value of %d\n",
1107 prland(&land), att_mode[combat_mode], att_val);
1108 if (land_answer[(int)land.lnd_army] != 'Y') {
1110 "%s with %s %s (%c %d%%) [ynYNq?] ",
1111 att_mode[combat_mode],
1114 land.lnd_army == ' ' ? '~' : land.lnd_army,
1116 land_answer[(int)land.lnd_army] =
1117 att_prompt(prompt, land.lnd_army);
1118 if (att_abort(combat_mode, off, def))
1120 if (land_answer[(int)land.lnd_army] != 'y' &&
1121 land_answer[(int)land.lnd_army] != 'Y')
1124 if (!(llp = (struct llist *)malloc(sizeof(struct llist)))) {
1125 logerror("Malloc failed in attack!\n");
1129 memset(llp, 0, sizeof(struct llist));
1130 emp_insque(&llp->queue, olist);
1131 llp->mobil = mobcost;
1132 if (!get_land(combat_mode, def, land.lnd_uid, llp, 0))
1134 if (lnd_spyval(&land) > *a_spyp)
1135 *a_spyp = lnd_spyval(&land);
1136 if (llp->lcp->l_flags & L_ENGINEER)
1138 if (def->type == EF_SHIP && ++count >= maxland)
1143 /* What's the offense or defense multiplier? */
1146 att_combat_eff(struct combat *com)
1150 double sector_strength(struct sctstr *sp);
1153 if (com->type == EF_SECTOR) {
1154 eff = com->eff / 100.0;
1155 if (com->own == player->cnum) {
1156 str = com->sct_dcp->d_ostr;
1157 eff = 1.0 + ((str - 1.0) * eff);
1159 eff = sector_strength(getsectp(com->x, com->y));
1160 /* str = com->sct_dcp->d_dstr;*/
1161 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1162 getship(com->shp_uid, &ship);
1163 eff = (1.0 + ship.shp_armor / 100.0);
1169 * Estimate the defense strength and give the attacker a chance to abort
1170 * if the odds are less than 50%
1174 att_estimate_defense(int combat_mode, struct combat *off,
1175 struct emp_qelem *olist, struct combat *def,
1183 * Get the attacker units & mil again in case they changed while the
1184 * attacker was answering sub-prompts.
1187 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1188 if (att_empty_attack(combat_mode, ototal, def))
1189 return abort_attack();
1190 if (combat_mode == A_PARA)
1192 pr("\n Initial attack strength: %8d\n", ototal);
1194 estimate = att_combat_eff(def) * roundintby(def->troops, 10);
1195 estimate += att_combat_eff(def) * get_dlist(def, 0, a_spy, 0);
1198 * Calculate the initial (pre-support) attack odds. If they're less
1199 * than 50%, ask for a confirmation.
1202 odds = (int)(att_calcodds(ototal, estimate) * 100);
1206 pr(" Estimated defense strength: %8d\n", estimate);
1207 pr(" Estimated odds: %8d%%\n\n",odds);
1208 sprintf(prompt, "Are you sure you want to %s [yn]? ",
1209 att_mode[combat_mode]);
1210 if (!confirm(prompt))
1211 return abort_attack();
1212 ototal = get_ototal(combat_mode, off, olist,1.0,1);
1213 if (att_empty_attack(combat_mode, ototal, def))
1214 return abort_attack();
1220 /* Get the defensive units and reacting units */
1222 att_get_defense(struct emp_qelem *olist, struct combat *def,
1223 struct emp_qelem *dlist, int a_spy, int ototal)
1226 struct emp_qelem *qp;
1232 get_dlist(def, dlist, 0, &d_spy);
1233 dtotal = get_dtotal(def, dlist, 1.0, 0);
1236 * Call in reacting units
1239 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1240 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1242 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1243 llp = (struct llist *)qp;
1244 intelligence_report(def->own, &llp->land, d_spy,
1245 "Scouts report attacking unit:");
1248 old_dtotal = dtotal;
1249 dtotal = get_dtotal(def, dlist, 1.0, 0);
1250 if (dtotal != old_dtotal)
1251 pr("Defense strength with reacting units: %8d\n", dtotal);
1256 /* Get the defensive land units in the sector or on the ship */
1259 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
1262 struct nstr_item ni;
1267 /* In here is where you need to take out spies and trains from the defending
1268 lists. Spies try to hide, trains get trapped and can be boarded. */
1270 snxtitem_xy(&ni, EF_LAND, def->x, def->y);
1271 while (nxtitem(&ni, (s_char *)&land)) {
1274 if (land.lnd_own != def->own)
1276 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1278 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1280 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1282 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1284 if (!list) { /* Just estimating the enemy strength */
1285 estimate += intelligence_report(player->cnum,
1287 "Scouts report defending unit:");
1290 if (!(llp = (struct llist *)malloc(sizeof(struct llist)))) {
1291 logerror("Malloc failed in attack!\n");
1295 memset(llp, 0, sizeof(struct llist));
1296 emp_insque(&llp->queue, list);
1297 llp->supplied = has_supply(&land);
1298 if (!get_land(A_DEFEND, def, land.lnd_uid, llp, 1))
1300 if (lnd_spyval(&land) > *d_spyp)
1301 *d_spyp = lnd_spyval(&land);
1306 /* Calculate the total offensive strength */
1309 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist,
1310 double osupport, int check)
1312 double ototal = 0.0;
1313 struct emp_qelem *qp, *next;
1318 * first, total the attacking mil
1321 for (n = 0; n <= off->last; ++n) {
1322 if (off[n].type == EF_BAD || (check &&
1323 att_get_combat(&off[n], 0) <= 0))
1325 ototal += off[n].troops * att_combat_eff(off + n);
1329 * next, add in the attack_values of all
1330 * the attacking units
1333 for (qp = olist->q_forw; qp != olist; qp = next) {
1335 llp = (struct llist *)qp;
1336 if (check && !get_land(combat_mode, 0, llp->land.lnd_uid, llp, 0))
1338 if (combat_mode == A_ATTACK) {
1340 for (n = 0; n <= off->last; ++n) {
1341 if (off[n].type == EF_BAD)
1343 if ((off[n].x == llp->land.lnd_x) &&
1344 (off[n].y == llp->land.lnd_y))
1348 lnd_delete(llp, "is in a sector not owned by you");
1351 ototal += attack_val(combat_mode, &llp->land) *
1352 att_combat_eff(off + w);
1354 ototal += attack_val(combat_mode, &llp->land);
1359 return ldround(ototal, 1);
1362 /* Calculate the total defensive strength */
1365 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport,
1368 double dtotal = 0.0, eff = 1.0, d_unit;
1369 struct emp_qelem *qp, *next;
1372 if (check && att_get_combat(def, 1) < 0)
1374 eff = att_combat_eff(def);
1375 dtotal = def->troops * eff;
1378 * next, add in the defense_values of all
1379 * the defending non-retreating units
1382 for (qp = list->q_forw; qp != list; qp = next) {
1384 llp = (struct llist *)qp;
1386 if (check && !get_land(A_DEFEND, def, llp->land.lnd_uid, llp, 1))
1388 d_unit = defense_val(&llp->land);
1390 d_unit = ((double)defense_val(&llp->land) / 2.0);
1391 dtotal += d_unit * eff;
1396 return ldround(dtotal, 1);
1400 * This is the land unit integrity check. Note that we don't print
1401 * warnings about victim land units because the attacker may not have seen them
1405 get_land(int combat_mode, struct combat *def, int uid, struct llist *llp,
1408 struct lndstr *lp = &llp->land;
1413 if (!llp->lcp) { /* first time */
1414 llp->x = llp->land.lnd_x;
1415 llp->y = llp->land.lnd_y;
1416 llp->lcp = &lchr[(int)llp->land.lnd_type];
1417 } else { /* not first time */
1418 if (lp->lnd_effic < LAND_MINEFF) {
1419 sprintf(buf, "was destroyed and is no longer a part of the %s",
1420 att_mode[combat_mode]);
1421 lnd_delete(llp, buf);
1425 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1427 "left to go fight another battle and is no longer a part of the defense");
1431 if (lp->lnd_own != player->cnum) {
1433 "was destroyed and is no longer a part of the %s",
1434 att_mode[combat_mode]);
1435 lnd_delete(llp, buf);
1438 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1440 "left to fight another battle and is no longer a part of the %s",
1441 att_mode[combat_mode]);
1442 lnd_delete(llp, buf);
1445 if (lp->lnd_effic < llp->eff) {
1446 sprintf(buf, "damaged from %d%% to %d%%",
1447 llp->eff, lp->lnd_effic);
1448 lnd_print(llp, buf);
1452 llp->eff = llp->land.lnd_effic;
1458 * Put the land unit on the disk. If there was some mobility cost, then
1459 * subtract it from the units mobility. Note that this works the same way
1460 * as sectors & ships in that no mobility is actually taken until the attacker
1461 * has committed to attacking.
1465 kill_land(struct emp_qelem *list)
1467 struct emp_qelem *qp, *next;
1470 for (qp = list->q_forw; qp != list; qp = next) {
1472 llp = (struct llist *)qp;
1473 if (llp->land.lnd_ship >= 0) {
1474 llp->land.lnd_effic = 0;
1475 lnd_delete(llp, "cannot return to the ship, and dies!");
1481 att_infect_units(struct emp_qelem *list, int plague)
1483 struct emp_qelem *qp, *next;
1488 for (qp = list->q_forw; qp != list; qp = next) {
1490 llp = (struct llist *)qp;
1491 if (getvar(V_PSTAGE, (s_char *)&(llp->land), EF_LAND) ==
1493 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)&(llp->land), EF_LAND);
1498 put_land(struct emp_qelem *list)
1500 struct emp_qelem *qp, *next;
1503 for (qp = list->q_forw; qp != list; qp = next) {
1505 llp = (struct llist *)qp;
1506 llp->land.lnd_mission = 0;
1507 llp->land.lnd_harden = 0;
1508 llp->land.lnd_mobil -= (int)llp->mobil;
1510 putland(llp->land.lnd_uid, &llp->land);
1511 if (llp->land.lnd_own != player->cnum) {
1512 emp_remque((struct emp_qelem *)llp);
1513 free((s_char *)llp);
1515 get_land(A_ATTACK, 0, llp->land.lnd_uid, llp, 0);
1520 * Keep sending in reinforcements until it looks like we're going to win.
1521 * Note that the "strength" command also calls this routine.
1525 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
1526 int *d_spyp, int ototal)
1528 struct nstr_item ni;
1530 struct sctstr sect, dsect;
1540 double eff = att_combat_eff(def);
1546 * All units that are within their reaction radius and not damaged
1547 * below their morale value now get to react to the threatened sect.
1548 * Once we've sent enough to counter the threat, stop sending them.
1550 * Not anymore. All units get to react. :)
1554 dtotal = get_dtotal(def, list, 1.0, 1);
1557 snxtitem_all(&ni, EF_LAND);
1558 while (nxtitem(&ni, (s_char *)&land) &&
1559 (dtotal + new_land * eff < (int)(1.2 * (float)ototal))) {
1562 if (!land.lnd_rad_max)
1564 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1566 if (land.lnd_own != def->own)
1568 if (land.lnd_ship >= 0)
1570 if (!defense_val(&land))
1573 if (land.lnd_effic <= land.lnd_retreat)
1576 if (!lnd_can_attack(&land))
1579 /* Only supplied units can react */
1580 if (!(supply_state = has_supply(&land)))
1583 dist = mapdist(land.lnd_x, land.lnd_y, def->x, def->y);
1585 getsect(land.lnd_x, land.lnd_y, §);
1586 /* Units on efficient headquarters can react 1 farther */
1587 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
1588 radius = land.lnd_rad_max + 1;
1590 radius = land.lnd_rad_max;
1592 if (land.lnd_mission == MI_RESERVE)
1598 getsect(def->x, def->y, &dsect);
1601 (s_char *)BestLandPath(buf, §, &dsect, &move_cost,
1605 mobcost = land.lnd_effic * 0.01 * lchr[(int)land.lnd_type].l_spd;
1608 mobcost = 480.0 / (mobcost + techfact(land.lnd_tech, mobcost));
1609 mobcost *= (move_cost * 5.0);
1611 if (land.lnd_mobil < mobcost)
1614 new_land += defense_val(&land);
1616 if (!list) /* we are in the "strength" command */
1619 /* move to defending sector */
1620 land.lnd_mobil -= ldround(mobcost, 1);
1623 land.lnd_x = def->x;
1624 land.lnd_y = def->y;
1625 putland(land.lnd_uid, &land);
1626 wu(0, land.lnd_own, "%s reacts to %s.\n",
1627 prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
1629 llp = (struct llist *)
1630 malloc(sizeof(struct llist));
1632 memset(llp, 0, sizeof(struct llist));
1633 llp->supplied = supply_state;
1636 llp->lcp = &lchr[(int)land.lnd_type];
1638 emp_insque(&llp->queue, list);
1639 if (lnd_spyval(&land) > *d_spyp)
1640 *d_spyp = lnd_spyval(&land);
1642 intelligence_report(player->cnum, &land, a_spy,
1643 "Scouts sight reacting enemy unit:");
1648 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1651 get_osupport(s_char *outs, struct combat *def, int fort_sup, int ship_sup,
1652 int land_sup, int plane_sup)
1654 double osupport = 1.0;
1656 double af, as, au, ap;
1658 af = as = au = ap = 0.0;
1660 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1661 af = ((double)dam / 100.0);
1665 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1667 as = ((double)dam / 100.0);
1672 dam = lnd_support(def->own, player->cnum, def->x, def->y);
1673 au = ((double)dam / 100.0);
1678 dam = off_support(def->x, def->y, def->own, player->cnum);
1679 ap = (((double)dam) / 100.0);
1682 sprintf(outs, "attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n", af, as, au,
1687 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1690 get_dsupport(s_char *outs, struct emp_qelem *list, struct combat *def,
1691 int ototal, int dtotal)
1693 double dsupport = 1.0;
1695 double df, ds, du, dp;
1698 df = ds = du = dp = 0.0;
1699 if (dtotal < 0.1 * ototal) {
1701 } else if (dtotal >= 1.2 * ototal) {
1704 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1705 df = ((double)dam / 100.0);
1708 dtotal = get_dtotal(def, list, dsupport, 0);
1709 if (dtotal < 1.2 * ototal) {
1710 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1711 ds = ((double)dam / 100.0);
1713 dtotal = get_dtotal(def, list, dsupport, 0);
1715 if (dtotal < 1.2 * ototal) {
1716 dam = lnd_support(player->cnum, def->own, def->x, def->y);
1717 du = ((double)dam / 100.0);
1719 dtotal = get_dtotal(def, list, dsupport, 1);
1721 if (dtotal < 1.2 * ototal) {
1722 dam = def_support(def->x, def->y, player->cnum, def->own);
1723 dp = (((double)dam) / 100.0);
1730 sprintf(outs, "defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n", df, ds,
1735 "\nOdds are bad for us...support cancelled.\n\n");
1738 "\nOdds are good for us...support cancelled.\n\n");
1744 * Land mines add to the defense multiplier. If the attacker has engineers
1745 * then this multiplier is cut in half.
1749 get_mine_dsupport(struct combat *def, int a_engineer)
1754 getsect(def->x, def->y, §);
1756 if (sect.sct_oldown != player->cnum) {
1757 mines = getvar(V_MINE, (s_char *)§, EF_SECTOR);
1758 mines = min(mines, 20);
1760 mines = ldround(((double)mines / 2.0), 1);
1763 wu(0, def->own, "Defending mines add %1.2f\n",
1765 pr("Defending mines add %1.2f\n", mines * 0.02);
1766 return mines * 0.02;
1772 /* Get the offensive and defensive support */
1774 att_get_support(int combat_mode, int ofort, int oship, int oland,
1775 int oplane, struct emp_qelem *olist, struct combat *off,
1776 struct emp_qelem *dlist, struct combat *def,
1777 double *osupportp, double *dsupportp, int a_engineer)
1780 s_char osupports[512];
1781 s_char dsupports[512];
1783 if (combat_mode == A_PARA)
1786 *osupportp = get_osupport(osupports, def,
1787 ofort, oship, oland, oplane);
1790 * I need to put a 1 at the end of the next four total_stren calls
1791 * becauase units & mil may have been damaged by collateral damage or
1792 * neclear warheads from the offensive & defensive support.
1795 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1796 if (att_empty_attack(combat_mode, ototal, def))
1797 return abort_attack();
1798 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1801 * Calculate defensive support. If odds are too good or too bad
1802 * then don't call in support.
1805 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1806 ototal = get_ototal(combat_mode, off, olist, *osupportp, 1);
1807 if (att_empty_attack(combat_mode, ototal, def))
1808 return abort_attack();
1810 if ((*osupports || *dsupports) &&
1811 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1812 pr("\n\t\tsupport values\n");
1813 pr("\t\tforts\tships\tunits\tplanes\n");
1814 if (*osupportp != 1.0)
1815 pr("%s", osupports);
1816 if (*dsupportp != 1.0)
1817 pr("%s", dsupports);
1819 wu(0, def->own, "\n\t\tsupport values\n");
1820 wu(0, def->own, "\t\tforts\tships\tunits\tplanes\n");
1821 if (*osupportp != 1.0)
1822 wu(0, def->own, "%s", osupports);
1823 if (*dsupportp != 1.0)
1824 wu(0, def->own, "%s", dsupports);
1828 dtotal = get_dtotal(def, dlist, *dsupportp, 1);
1829 if (dtotal && def->type == EF_SECTOR)
1830 *dsupportp += get_mine_dsupport(def, a_engineer);
1834 /* How many two-legged bipeds are in this combat force? */
1837 count_bodies(struct combat *off, struct emp_qelem *list)
1841 struct emp_qelem *qp;
1844 for (n = 0; n <= off->last; ++n)
1845 bodies += off[n].troops;
1846 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
1847 llp = (struct llist *)qp;
1848 bodies += total_mil(&llp->land);
1853 /* This is where the fighting actually occurs. */
1856 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
1857 double osupport, struct combat *def, struct emp_qelem *dlist,
1861 int a_cas = 0; /* Casualty counts */
1863 int ototal; /* total attacking strength */
1864 int dtotal; /* total defending strength */
1865 int a_bodies; /* total attacking mil (incl. mil in units) */
1866 int d_bodies; /* total defending mil (incl. mil in units) */
1875 ototal = get_ototal(combat_mode, off, olist, osupport,
1876 combat_mode != A_PARA);
1877 dtotal = get_dtotal(def, dlist, dsupport, 0);
1881 a_bodies = count_bodies(off, olist);
1882 d_bodies = count_bodies(def, dlist);
1883 d_mil = def->troops;
1884 for (n = 0; n <= off->last; ++n)
1885 if (off[n].type == EF_BAD)
1888 a_troops[n] = off[n].troops;
1890 /* This switch is required to get the spacing right */
1891 switch (combat_mode) {
1893 pr(" Final attack strength: %8d\n", ototal);
1896 pr(" Final assault strength: %8d\n", ototal);
1899 if (def->sct_type == SCT_MOUNT ||
1900 def->sct_type == SCT_WATER ||
1901 def->sct_type == SCT_CAPIT ||
1902 def->sct_type == SCT_FORTR || def->sct_type == SCT_WASTE) {
1903 pr("You can't air-assault a %s sector!\n",
1904 def->sct_dcp->d_name);
1907 ototal = get_ototal(A_PARA, off, olist, osupport, 0);
1909 pr(" Final air-assault strength: %8d\n", ototal);
1913 pr(" Final board strength: %8d\n", ototal);
1917 pr(" Final defense strength: %8d\n", dtotal);
1918 odds = att_calcodds(ototal, dtotal);
1919 pr(" Final odds: %8d%%\n", (int)(odds * 100));
1921 /* spread the plague */
1922 if (combat_mode != A_PARA) {
1924 for (n = 0; n <= off->last; ++n)
1925 if (off[n].type != EF_BAD)
1926 def->plague |= off[n].plague;
1927 for (n = 0; n <= off->last; ++n)
1928 if (off[n].type != EF_BAD)
1929 off[n].plague |= def->plague;
1931 att_infect_units(olist, off->plague);
1932 att_infect_units(dlist, def->plague);
1934 /* Fighting is slightly random. There is always that last little
1935 * effort you see people put in. Or the stray bullet that takes out
1936 * an officer and the rest go into chaos. Things like that.
1937 * Thus, we have added a very slight random factor that will sometimes
1938 * allow the little guy to win. We modify the odds a little
1939 * (either +- 5%) to account for this randomness. We also only
1940 * recalculate the odds every 8-50 casualties, not every cacsualty,
1941 * since a single dead guy normally wouldn't cause a commander to
1942 * rethink his strategies, but 50 dead guys might. */
1943 odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1948 recalctime = 8 + (random() % 43);
1949 while (!success && ototal) {
1952 d_cas += take_casualty(A_DEFEND, def, dlist);
1953 dtotal = get_dtotal(def, dlist, dsupport, 0);
1958 a_cas += take_casualty(combat_mode, off, olist);
1959 ototal = get_ototal(combat_mode, off, olist, osupport, 0);
1961 if (((a_cas + d_cas) % 70) == 69)
1963 if (recalctime-- <= 0) {
1964 recalctime = 8 + (random() % 43);
1965 odds = att_calcodds(ototal, dtotal);
1966 odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1974 /* update defense mobility & mil */
1978 if (def->type == EF_SECTOR && d_mil && d_cas) {
1981 /* Make sure we use a positive mobility here */
1982 tmob = ((def->mob < 0) ? -(def->mob) : (def->mob));
1984 min(20, min(1, tmob - damage(tmob, 100 * d_cas / d_mil)));
1986 def->mil = def->troops;
1989 /* update attack mobility & mil */
1990 for (n = 0; n <= off->last; ++n)
1991 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1992 if (off[n].type == EF_SECTOR && off[n].mil)
1996 off[n].mob - damage(off[n].mob,
1997 100 * (a_troops[n] -
2000 off[n].mil -= a_troops[n] - off[n].troops;
2003 /* update land unit mobility */
2004 if (d_bodies && d_cas)
2005 lnd_takemob(dlist, (double)d_cas / d_bodies);
2006 if (a_bodies && a_cas)
2007 lnd_takemob(olist, (double)a_cas / a_bodies);
2009 /* damage attacked sector */
2010 def->eff = effdamage(def->eff, (d_cas + a_cas) / 10);
2012 pr("- Casualties -\n Yours: %d\n", a_cas);
2013 pr(" Theirs: %d\n", d_cas);
2014 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
2015 player->btused += (int)((d_cas + a_cas) * 0.015 + 0.5);
2018 switch (combat_mode) {
2020 news_item = def->own ? N_WON_SECT : N_TOOK_UNOCC;
2021 pr("We have captured %s, sir!\n", prcom(0, def));
2025 news_item = def->own ? N_AWON_SECT : N_START_COL;
2026 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
2027 action = "assaulting and taking";
2030 news_item = def->own ? N_PWON_SECT : N_PARA_UNOCC;
2031 pr("We have captured %s, sir!\n", prcom(0, def));
2032 action = "air-assaulting and taking";
2035 news_item = N_BOARD_SHIP;
2036 pr("We have boarded %s, sir!\n", prcom(0, def));
2037 action = "boarding";
2040 news_item = N_BOARD_LAND;
2041 pr("We have boarded %s, sir!\n", prcom(0, def));
2042 action = "boarding";
2046 switch (combat_mode) {
2048 news_item = N_SCT_LOSE;
2049 pr("You have been defeated!\n");
2050 action = "attacking";
2053 news_item = N_ALOSE_SCT;
2054 pr("You have been defeated!\n");
2056 action = "trying to assault";
2059 news_item = N_PLOSE_SCT;
2060 pr("All of your troops were destroyed\n");
2061 action = "trying to air-assault";
2064 news_item = N_SHP_LOSE;
2065 pr("You have been repelled\n");
2067 action = "trying to board";
2070 news_item = N_LND_LOSE;
2071 pr("You have been repelled\n");
2073 action = "trying to board";
2077 nreport(player->cnum, news_item, def->own, 1);
2080 "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
2081 cname(player->cnum), player->cnum, a_cas,
2082 action, pr_com(0, def, def->own), d_cas);
2085 if (opt_SNEAK_ATTACK) {
2086 do_sneak(def, success);
2089 send_reacting_units_home(def, dlist);
2091 /* putland the defending land */
2094 /* putland the attacking land */
2097 /* put the victim sector/ship/land */
2098 if (!success || !take_def(combat_mode, olist, off, def))
2101 /* put the attacking sectors/ship */
2102 for (n = 0; n <= off->last; ++n)
2103 if (off[n].type != EF_BAD)
2104 put_combat(&off[n]);
2109 switch (combat_mode) {
2111 ask_move_in(off, olist, def);
2113 /* put sectors again to get abandon warnings */
2114 for (n = 0; n <= off->last; ++n)
2115 if (off[n].type != EF_BAD)
2116 put_combat(&off[n]);
2119 att_move_in_off(combat_mode, off, olist, def);
2122 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2126 /* What percentage of the combat forces going head-to-head are we? */
2129 att_calcodds(int ototal, int dtotal)
2133 /* calculate odds */
2136 else if (dtotal <= 0)
2139 odds = ((double)ototal) / (dtotal + ototal);
2144 /* Here's where the dead soldiers get dragged off the battlefield */
2147 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2149 int to_take = CASUALTY_LUMP;
2150 int biggest_troops = 0, index = -1;
2151 int n, tot_troops = 0, biggest_mil, cas;
2152 struct emp_qelem *qp, *biggest;
2155 for (n = 0; n <= off->last; ++n) {
2156 if (off[n].type != EF_BAD) {
2157 tot_troops += off[n].troops;
2158 if (off[n].troops > biggest_troops) {
2159 biggest_troops = off[n].troops;
2166 to_take -= tot_troops;
2169 for (n = 0; n <= off->last; ++n)
2170 if (off[n].type != EF_BAD)
2174 * They can all come off mil. We rotate the casualties,
2175 * starting with the sector containing the most mil.
2177 to_take = CASUALTY_LUMP;
2179 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2182 while (to_take > 0) {
2183 for (n = index; n <= off->last && to_take; ++n) {
2184 if (off[n].type != EF_BAD && off[n].troops > 0) {
2189 for (n = 0; n < index && to_take; ++n) {
2190 if (off[n].type != EF_BAD && off[n].troops > 0) {
2196 return CASUALTY_LUMP;
2200 return (CASUALTY_LUMP - to_take);
2203 * Need to take some casualties from attacking units
2204 * Procedure: find the biggest unit remaining (in
2205 * terms of mil) and give it the casualties.
2207 biggest = (struct emp_qelem *)0;
2209 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
2210 llp = (struct llist *)qp;
2212 if (total_mil(&llp->land) > biggest_mil) {
2213 biggest_mil = total_mil(&llp->land);
2217 if (biggest == (struct emp_qelem *)0)
2218 return (CASUALTY_LUMP - to_take);
2220 llp = (struct llist *)biggest;
2221 cas = lnd_take_casualty(combat_mode, llp, to_take);
2222 return (CASUALTY_LUMP - (to_take - cas));
2225 /* Send reacting defense units back to where they came from (at no mob cost) */
2228 send_reacting_units_home(struct combat *def, struct emp_qelem *list)
2230 struct emp_qelem *qp, *next;
2234 for (qp = list->q_forw; qp != list; qp = next) {
2236 llp = (struct llist *)qp;
2237 if ((llp->land.lnd_x != llp->x) || (llp->land.lnd_y != llp->y)) {
2238 sprintf(buf, "returns to %s",
2239 xyas(llp->x, llp->y, llp->land.lnd_own));
2240 llp->land.lnd_x = llp->x;
2241 llp->land.lnd_y = llp->y;
2242 lnd_delete(llp, buf);
2247 /* Check for 0 offense strength. This call will always preceed an abort */
2250 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2253 if (def->own && player->cnum != def->own) {
2255 "%s (#%d) considered %sing you @%s\n",
2256 cname(player->cnum), player->cnum,
2257 att_mode[combat_mode], xyas(def->x, def->y, def->own));
2259 pr("No troops for %s...\n", att_mode[combat_mode]);
2266 * Take the defending sector or ship from the defender and give it to the
2271 take_def(int combat_mode, struct emp_qelem *list, struct combat *off,
2276 struct llist *llp, *delete_me = 0;
2282 for (n = 0; n <= off->last && !occuppied; ++n) {
2283 if (off[n].type != EF_BAD &&
2284 off[n].troops > 0 &&
2285 (off[n].type != EF_SECTOR || off[n].mob)) {
2287 if (def->type == EF_LAND) {
2288 if (def->lnd_lcp->l_flags & L_SPY) {
2295 pr("1 mil from %s moves %s\n",
2296 prcom(0, off + n), prcom(2, def));
2301 pr("%s left unoccupied\n", prcom(0, def));
2304 "No enemy troops moved %s so you still own it!\n",
2305 pr_com(2, def, def->own));
2308 llp = (struct llist *)list->q_forw;
2309 llp->land.lnd_x = def->x;
2310 llp->land.lnd_y = def->y;
2311 take_move_in_mob(combat_mode, llp, off, def);
2312 if (def->type == EF_SHIP) {
2313 llp->land.lnd_ship = def->shp_uid;
2314 sprintf(buf, "boards %s", prcom(0, def));
2317 llp->land.lnd_ship = -1;
2318 sprintf(buf, "moves in to occupy %s",
2319 xyas(def->x, def->y, player->cnum));
2320 lnd_delete(llp, buf);
2325 if (def->type == EF_SECTOR) {
2326 getsect(def->x, def->y, §);
2327 takeover(§, player->cnum);
2328 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2329 caploss(§, def->own,
2330 "* We have captured %s's capital, sir! *\n");
2332 } else if (def->type == EF_SHIP) {
2333 getship(def->shp_uid, &ship);
2334 takeover_ship(&ship, player->cnum, 1);
2335 putship(ship.shp_uid, &ship);
2336 } else if (def->type == EF_LAND) {
2337 getland(def->lnd_uid, &land);
2338 takeover_land(&land, player->cnum, 1);
2339 putland(land.lnd_uid, &land);
2342 lnd_delete(delete_me, buf);
2343 att_get_combat(def, 0);
2348 * Ask the attacker which mil & land units they'd like to move into the
2353 ask_move_in(struct combat *off, struct emp_qelem *olist,
2357 struct emp_qelem *qp, *next;
2361 s_char land_answer[1024];
2364 for (n = 0; n <= off->last; ++n)
2365 if (off[n].type != EF_BAD && off[n].troops > 0)
2367 ask_move_in_off(&off[n], def);
2368 if (player->aborted)
2374 memset(land_answer, 0, sizeof(land_answer));
2375 for (qp = olist->q_forw; qp != olist; qp = next) {
2377 llp = (struct llist *)qp;
2378 answerp = &land_answer[(int)llp->land.lnd_army];
2379 if (player->aborted || att_get_combat(def, 0) < 0)
2381 if (*answerp == 'Y')
2383 if (*answerp != 'N') {
2384 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2386 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2388 llp->land.lnd_army == ' ' ? '~' : llp->land.lnd_army,
2389 llp->land.lnd_effic);
2390 *answerp = att_prompt(prompt, llp->land.lnd_army);
2391 if (player->aborted || att_get_combat(def, 0) < 0)
2393 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2396 if (*answerp == 'y' || *answerp == 'Y')
2398 sprintf(buf, "stays in %s",
2399 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2400 lnd_delete(llp, buf);
2404 if (att_get_combat(def, 0) < 0) {
2405 for (qp = olist->q_forw; qp != olist; qp = next) {
2407 llp = (struct llist *)qp;
2408 if (!get_land(A_ATTACK, def, llp->land.lnd_uid, llp, 0))
2410 sprintf(buf, "stays in %s",
2411 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2412 lnd_delete(llp, buf);
2416 if (opt_INTERDICT_ATT)
2417 lnd_interdict(olist, def->x, def->y, player->cnum);
2418 move_in_land(A_ATTACK, off, olist, def);
2421 /* Move offensive land units to the conquered sector or ship */
2424 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist,
2427 struct emp_qelem *qp, *next;
2433 for (qp = olist->q_forw; qp != olist; qp = next) {
2435 llp = (struct llist *)qp;
2436 if (!get_land(combat_mode, def, llp->land.lnd_uid, llp, 0))
2438 take_move_in_mob(combat_mode, llp, off, def);
2439 llp->land.lnd_x = def->x;
2440 llp->land.lnd_y = def->y;
2441 if (def->type == EF_SHIP)
2442 llp->land.lnd_ship = def->shp_uid;
2444 llp->land.lnd_ship = -1;
2448 if (def->type == EF_SECTOR) {
2449 if (opt_INTERDICT_ATT) {
2450 lnd_sweep(olist, 0, 0, def->own);
2451 lnd_check_mines(olist);
2453 sprintf(buf, "now occupies %s", prcom(0, def));
2455 sprintf(buf, "boards %s", prcom(0, def));
2459 for (qp = olist->q_forw; qp != olist; qp = next) {
2461 llp = (struct llist *)qp;
2462 lnd_print(llp, buf);
2470 * Move assaulting, paradropping, or boarding mil & units into def
2471 * If the mil are coming from a ship, then pack a lunch.
2475 att_move_in_off(int combat_mode, struct combat *off,
2476 struct emp_qelem *olist, struct combat *def)
2481 int defvec[I_MAX + 1];
2482 int shipvec[I_MAX + 1];
2484 int n, lunchbox = 0;
2487 move_in_land(combat_mode, off, olist, def);
2489 for (n = 0; n <= off->last; ++n) {
2490 if (off[n].type == EF_BAD || !off[n].troops)
2492 troops = off[n].troops;
2494 off[n].mil -= troops;
2496 put_combat(off + n);
2497 if (combat_mode == A_ASSAULT) {
2498 getship(off[n].shp_uid, &ship);
2499 getvec(VT_ITEM, shipvec, (s_char *)&ship, EF_SHIP);
2500 lunchbox += (int)((troops + 1) * shipvec[I_FOOD] /
2501 (shipvec[I_MILIT] + troops +
2502 shipvec[I_CIVIL] + 0.5));
2503 shipvec[I_FOOD] -= lunchbox;
2504 putvec(VT_ITEM, shipvec, (s_char *)&ship, EF_SHIP);
2505 putship(ship.shp_uid, &ship);
2512 if (def->type == EF_SECTOR) {
2513 getsect(def->x, def->y, §);
2514 thing = (s_char *)§
2515 } else if (def->type == EF_SHIP) {
2516 getship(def->shp_uid, &ship);
2517 thing = (s_char *)&ship;
2518 } else if (def->type == EF_LAND) {
2519 getship(def->lnd_uid, &land);
2520 thing = (s_char *)&land;
2522 pr("Please tell the deity that you got the 'hungry mole' error\n");
2525 getvec(VT_ITEM, defvec, thing, def->type);
2526 defvec[I_FOOD] += lunchbox;
2527 putvec(VT_ITEM, defvec, thing, def->type);
2528 if (def->type == EF_SECTOR)
2530 else if (def->type == EF_SHIP)
2531 putship(ship.shp_uid, &ship);
2533 putland(land.lnd_uid, &land);
2537 /* Ask how many mil to move in from each sector */
2540 ask_move_in_off(struct combat *off, struct combat *def)
2543 int num_mil, dam = 0, left;
2549 if (att_get_combat(off, 0) <= 0)
2551 if (att_get_combat(def, 0) < 0)
2553 if (off->own != player->cnum)
2555 d = sector_mcost(getsectp(def->x, def->y), MOB_ROAD);
2556 if ((mob_support = min(off->troops, (int)(off->mob / d))) <= 0)
2558 sprintf(prompt, "How many mil to move in from %s (%d max)? ",
2559 xyas(off->x, off->y, player->cnum), mob_support);
2560 if (!(p = getstring(prompt, buf)) || !*p || (num_mil = atoi(p)) <= 0) {
2564 /* Make sure we don't move in more than we can support mobility-wise */
2565 if (num_mil > mob_support)
2566 num_mil = mob_support;
2567 if (att_get_combat(off, 0) <= 0)
2569 if (att_get_combat(def, 0) < 0)
2571 if ((num_mil = min(off->troops, num_mil)) <= 0) {
2572 pr("No mil moved in from %s\n",
2573 xyas(off->x, off->y, player->cnum));
2576 mob_support = max(1, (int)(num_mil * d));
2577 off->mob -= min(off->mob, mob_support);
2578 off->mil -= num_mil;
2579 off->troops -= num_mil;
2582 weight = num_mil * ichr[I_MILIT].i_lbs;
2583 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2584 if (chance(weight / 100.0))
2586 ground_interdict(def->x, def->y, player->cnum, "military");
2587 dam += check_lmines(def->x, def->y, weight);
2591 left = commdamage(num_mil, dam, V_MILIT);
2592 if (left < num_mil) {
2594 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));
2596 pr("All of the mil you were moving were destroyed!\n");
2599 /* maybe got nuked */
2600 if (att_get_combat(def, 0) < 0)
2608 /* Charge land units for moving into a sector or onto a ship */
2611 take_move_in_mob(int combat_mode, struct llist *llp, struct combat *off,
2614 extern int etu_per_update;
2615 extern float land_mob_scale;
2619 switch (combat_mode) {
2622 lnd_mobcost(&llp->land, getsectp(def->x, def->y), MOB_NONE);
2623 new = llp->land.lnd_mobil - mobcost;
2626 llp->land.lnd_mobil = new;
2629 if (off->shp_mcp->m_flags & M_LAND) {
2630 if (llp->lcp->l_flags & L_MARINE)
2631 llp->land.lnd_mobil -=
2632 ((float)etu_per_update * land_mob_scale * 0.5);
2634 llp->land.lnd_mobil -= ((float)etu_per_update
2637 if (llp->lcp->l_flags & L_MARINE)
2638 llp->land.lnd_mobil = 0;
2640 llp->land.lnd_mobil = (((float)etu_per_update
2641 * land_mob_scale) * (-1));
2645 /* I arbitrarily chose the numbers 10 and 40 below -KHS */
2646 if (llp->lcp->l_flags & L_MARINE)
2647 llp->land.lnd_mobil -= 10;
2649 llp->land.lnd_mobil -= 40;
2652 llp->land.lnd_harden = 0;
2656 free_list(struct emp_qelem *list)
2658 register struct emp_qelem *qp, *next;
2660 if (!list || QEMPTY(list))
2664 while (qp != list) {
2673 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2681 * sector_strength - Everyone starts at 1. You can get up to a max
2682 * of d_dstr, depending on how much you build up the
2683 * defenses of the sector.
2687 sector_strength(struct sctstr *sp)
2693 if (sp->sct_type == SCT_MOUNT)
2696 d = d + ((double)(dchr[sp->sct_type].d_dstr - d) *
2697 ((double)sp->sct_defense / 100.0));
2699 if (d > dchr[sp->sct_type].d_dstr)
2700 d = dchr[sp->sct_type].d_dstr;