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, struct combat *def, struct emp_qelem *olist, s_char *land_answer, int *a_spyp, int *a_engineerp);
58 static void take_move_in_mob(int combat_mode, struct llist *llp, struct combat *off, struct combat *def);
59 static void move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist, struct combat *def);
60 static void ask_move_in(struct combat *off, struct emp_qelem *olist, struct combat *def);
61 static void ask_move_in_off(struct combat *off, struct combat *def);
63 static int board_abort(struct combat *off, struct combat *def);
64 static int land_board_abort(struct combat *off, struct combat *def);
65 static int ask_off(int combat_mode, struct combat *off, struct combat *def);
66 static int get_dlist(struct combat *def, struct emp_qelem *list, int a_spy, int *d_spyp);
67 static int get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist, double osupport, int check);
68 static int get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport, int check);
69 static int take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist);
71 static void send_reacting_units_home(struct combat *def, struct emp_qelem *list);
72 static int take_def(int combat_mode, struct emp_qelem *list, struct combat *off, struct combat *def);
74 void att_move_in_off(int combat_mode, struct combat *off, struct emp_qelem *olist, struct combat *def);
75 static int get_land(int combat_mode, struct combat *def, int uid, struct llist *llp, int victim_land);
78 * The principal object in this code is the "combat" object. A combat object
79 * is either a sector or ship. There are
80 * usually two instances of this, the "def" or defense combat object, and
81 * the array of "off" or offense objects. The number of offense objects is
82 * determined by the value of off->last (e.g. more than one attacking sector).
83 * the type of the object is determined by combat->type which can take the
84 * values EF_SECTOR, EF_SHIP, EF_PLANE, or EF_BAD. Another important parameter
85 * which is often passed to these functions is combat_mode. This can take
86 * the value A_DEFENSE, A_ATTACK, A_ASSAULT, A_PARA, A_BOARD and A_LBOARD.
87 * As these six modes of being in combat affect things like mobcost and combat
88 * value, there are often switches made on combat_mode. Note that in all cases
89 * no mobility is taken from sectors, ships, or land units until the player
90 * has committed to a fight. Instead, the cost is temporarily placed in
91 * combat->mobcost, or llp->mobil as the case may be, and then when the object
92 * is "put" back onto disk, then the amounts in these variables are subtracted
93 * from the object's mobility. It needs to be done this way as the objects
94 * are constantly being re-read from disk, and we don't want to take any mob
95 * unless a fight actually occurrs.
99 /* initialize combat object */
102 att_combat_init(struct combat *com, int type)
104 bzero((s_char *)com, sizeof(*com));
109 /* print a combat object with optional preposition */
112 pr_com(int inon, struct combat *com, natid who)
114 if (com->type == EF_SECTOR) {
116 inon?inon==1?"in ":"into ":"",
117 xyas(com->x, com->y, who));
118 } else if (com->type == EF_SHIP) {
120 return prbuf("%s%s %s(#%d)",
121 inon?inon==1?"on ":"onto ":"",
122 com->shp_mcp->m_name, com->shp_name, com->shp_uid);
124 return prbuf("%s%s #%d",
125 inon?inon==1?"on ":"onto ":"",
126 com->shp_mcp->m_name, com->shp_uid);
128 } else if (com->type == EF_LAND) {
129 return prbuf("%s%s #%d",
130 inon?inon==1?"on ":"onto ":"",
131 com->lnd_lcp->l_name, com->lnd_uid);
133 return "your forces";
138 prcom(int inon, struct combat *com)
140 return pr_com(inon, com, player->cnum);
143 /* Doing a sneak attack */
145 do_sneak(def, success)
150 struct natstr *natp = getnatp(player->cnum);
151 int issneak = getrel(natp, def->own);
153 if (def->type != EF_SECTOR)
156 getsect(def->x, def->y, §);
158 if (issneak == AT_WAR ||
160 sect.sct_oldown == player->cnum)
164 pr("Your sneak attack was successful\nBut ");
166 pr("Your sneak attack was unsuccessful\nAnd ");
168 pr("it will cost you $5000\n");
169 pr("War has been declared!!!!\n");
170 wu(0,def->own, "Country %s (#%d) has Sneak Attacked!!\n",
171 cname(player->cnum), player->cnum);
172 wu(0,def->own,"Country %s (#%d) has Declared WAR on you!!\n",
173 cname(player->cnum), player->cnum);
174 player->dolcost += 5000;
175 issneak = min(issneak, MOBILIZATION);
176 nreport(player->cnum, N_DECL_WAR, def->own, 1);
177 setrel(player->cnum,def->own,issneak);
181 * This is the combat object "type" based integrity check. It basically
182 * splits along three divisions: ship/sector, attacker/defender,
183 * first time/not first time.
187 att_get_combat(struct combat *com, int isdef)
201 if (!getsect(com->x, com->y, §)) {
202 pr("Bad sector: %s\n",
203 xyas(com->x, com->y, player->cnum));
204 return att_combat_init(com, EF_BAD);
206 com->sct_type = sect.sct_type;
207 com->sct_dcp = &dchr[sect.sct_type];
208 thing = (s_char *)§
209 owner = sect.sct_own;
210 eff = sect.sct_effic;
211 mob = sect.sct_mobil;
216 if (!getland(com->lnd_uid, &land)) {
218 pr("Land unit #%d is not in the same sector!\n", com->lnd_uid);
219 return att_combat_init(com, EF_BAD);
221 if (isdef && player->owner) {
222 pr("Boarding yourself? Try using the 'load' command.\n");
223 return att_combat_init(com, EF_BAD);
225 com->lnd_lcp = &lchr[(int)land.lnd_type];
226 thing = (s_char *)&land;
227 owner = land.lnd_own;
228 eff = land.lnd_effic;
229 mob = land.lnd_mobil;
234 if (!getship(com->shp_uid, &ship) || !ship.shp_own) {
236 pr("Ship #%d is not in the same sector!\n", com->shp_uid);
238 pr("Ship #%d is not your ship!\n", com->shp_uid);
239 return att_combat_init(com, EF_BAD);
242 if (isdef && player->owner &&
243 ontradingblock(EF_SHIP, (int *)&ship)) {
244 pr("%s is on the trading block.\n", prcom(0, com));
245 return att_combat_init(com, EF_BAD);
248 if (isdef && player->owner) {
249 pr("Boarding yourself? Try using the 'tend' command.\n");
250 return att_combat_init(com, EF_BAD);
252 com->shp_mcp = &mchr[(int)ship.shp_type];
254 strncpy(com->shp_name, ship.shp_name, MAXSHPNAMLEN);
255 if (!isdef && !player->owner) {
257 pr("%s was just sunk!\n", prcom(0, com));
259 pr("Ship #%d is not your ship!\n", com->shp_uid);
260 return att_combat_init(com, EF_BAD);
262 thing = (s_char *)&ship;
263 owner = ship.shp_own;
264 eff = ship.shp_effic;
265 mob = ship.shp_mobil;
274 return att_combat_init(com, EF_BAD);
277 mil = getvar(V_MILIT, thing, com->type);
278 if (!com->set) { /* first time */
279 if (isdef) { /* defender */
281 } else { /* attacker */
283 pr("No mil %s\n", prcom(1, com));
285 pr("Only 1 mil %s\n", prcom(1, com));
286 /* don't abandon attacking sectors or ships */
287 com->troops = max(0, mil-1);
289 com->plague = (getvar(V_PSTAGE, thing, com->type)) ==
291 } else { /* not first time */
292 if (isdef) { /* defender */
293 if (com->x != x || com->y != y) {
294 pr("%s has moved!\n", prcom(0, com));
295 return att_combat_init(com, EF_BAD);
297 if (owner != com->own) {
299 pr("WARNING: The ownership of %s just changed from %s to %s!\n", prcom(0, com), cname(com->own), cname(owner));
300 } else if (com->type == EF_SECTOR) {
301 pr("WARNING: %s just abandoned sector %s!\n", cname(com->own), xyas(com->x,com->y,player->cnum));
305 pr("WARNING: The enemy mil %s just %s from %d to %d!\n", prcom(1, com),
306 com->mil < mil?"increased":"decreased",
309 } else { /* attacker */
310 if (owner != player->cnum &&
311 getrel(getnatp(owner), player->cnum) != ALLIED) { /* must be EF_SECTOR */
313 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));
315 pr("You no longer own %s\n",
316 xyas(com->x,com->y,player->cnum));
317 return att_combat_init(com, EF_BAD);
319 if (com->troops && com->troops + 1 > mil) {
320 if (com->own == owner &&
321 player->cnum == owner) /* not a takeover */
322 pr("WARNING: Your mil %s has been reduced from %d to %d!\n", prcom(1, com), com->troops, max(0,mil-1));
323 com->troops = max(0,mil-1);
338 * In the course of the fight, the combat object may have lost mil, eff, or
339 * mobility. This is the place where the data in the object gets flushed to
340 * disk to make it "real".
344 put_combat(struct combat *com)
353 getsect(com->x, com->y, §);
354 sect.sct_type = com->sct_type;
355 deff = sect.sct_effic - com->eff;
357 sect.sct_road -= (sect.sct_road * deff / 100.0);
358 sect.sct_rail -= (sect.sct_rail * deff / 100.0);
359 sect.sct_defense -= (sect.sct_defense * deff / 100.0);
360 if (sect.sct_road <= 0)
362 if (sect.sct_rail <= 0)
364 if (sect.sct_defense <= 0)
365 sect.sct_defense = 0;
367 sect.sct_effic = com->eff;
368 if (!opt_DEFENSE_INFRA)
369 sect.sct_defense = sect.sct_effic;
371 if (opt_MOB_ACCESS) {
372 if ((com->mob - com->mobcost) < -127)
373 sect.sct_mobil = -127;
375 sect.sct_mobil = (short)(com->mob - com->mobcost);
377 if ((com->mob - com->mobcost) < 0)
380 sect.sct_mobil = (short)(com->mob - com->mobcost);
383 makelost(EF_SECTOR, sect.sct_own, 0, sect.sct_x, sect.sct_y);
384 makenotlost(EF_SECTOR, com->own, 0, sect.sct_x, sect.sct_y);
385 sect.sct_own = com->own;
387 if (getvar(V_PSTAGE, (s_char *)§, EF_SECTOR) == PLG_HEALTHY)
388 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)§, EF_SECTOR);
390 putvar(V_MILIT, com->mil, (s_char *)§, EF_SECTOR);
392 com->own = sect.sct_own; /* avoid WARNING if sector reverts */
395 getland(com->lnd_uid, &land);
396 land.lnd_effic = com->eff;
398 if (com->mob - com->mobcost < -127)
399 land.lnd_mobil = -127;
401 land.lnd_mobil = (s_char)(com->mob - com->mobcost);
403 makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x, land.lnd_y);
404 land.lnd_own = com->own;
405 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x, land.lnd_y);
407 if (getvar(V_PSTAGE, (s_char *)&land, EF_LAND) == PLG_HEALTHY)
408 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)&land, EF_LAND);
410 if (!(com->lnd_lcp->l_flags & L_SPY))
411 putvar(V_MILIT, com->mil, (s_char *)&land, EF_LAND);
412 lnd_count_units(&land);
413 if (com->own == player->cnum) {
414 land.lnd_mission = 0;
416 bzero(land.lnd_rpath, RET_LEN);
418 putland(com->lnd_uid, &land);
421 getship(com->shp_uid, &ship);
422 ship.shp_effic = com->eff;
424 if (com->mob - com->mobcost < -127)
425 ship.shp_mobil = -127;
428 (s_char)(com->mob - com->mobcost);
430 makelost(EF_SHIP, ship.shp_own, ship.shp_uid, ship.shp_x, ship.shp_y);
431 ship.shp_own = com->own;
432 makenotlost(EF_SHIP, ship.shp_own, ship.shp_uid, ship.shp_x, ship.shp_y);
434 if (getvar(V_PSTAGE, (s_char *)&ship, EF_SHIP) == PLG_HEALTHY)
435 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)&ship, EF_SHIP);
437 putvar(V_MILIT, com->mil, (s_char *)&ship, EF_SHIP);
439 if (com->own == player->cnum) {
440 ship.shp_mission = 0;
442 bzero(ship.shp_rpath,RET_LEN);
444 putship(com->shp_uid, &ship);
447 att_get_combat(com, com->own != player->cnum);
450 /* If pre-attack, abort fight. If post-attack, don't move anything in */
455 return player->aborted = 1;
459 * This is the combat_mode based integrity check. It splits among two main
460 * divisions: first time/not first time, and attack/assault/para/board.
464 att_abort(int combat_mode, struct combat *off, struct combat *def)
473 if (att_get_combat(def, 1) < 0)
474 return abort_attack();
476 if (off && combat_mode != A_ATTACK) {
477 if (att_get_combat(off, 0) < 0)
478 return abort_attack();
479 if (off->type == EF_SHIP &&
480 (!getsect(off->x, off->y, §) ||
481 sect.sct_type != SCT_WATER)) {
482 pr("%s can not %s from that far inland!\n",
484 att_mode[combat_mode]);
485 return abort_attack();
488 switch (combat_mode) {
490 if (!neigh(def->x, def->y, player->cnum) &&
491 !adj_units(def->x, def->y, player->cnum)) {
492 pr("You are not adjacent to %s\n", xyas(def->x, def->y, player->cnum));
493 return abort_attack();
495 if (def->own == player->cnum) {
496 pr("You can't attack your own sector.\n");
497 return abort_attack();
501 if (off && mapdist(off->x, off->y, def->x, def->y) > 1) {
502 pr("You'll have to get there first...\n");
503 return abort_attack();
505 if (off && def->sct_type == SCT_MOUNT) {
506 pr("You can't assault a %s sector!\n", def->sct_dcp->d_name);
507 return abort_attack();
511 if (def->own == player->cnum) {
512 pr("You can't air-assault your own sector.\n");
513 return abort_attack();
515 if (off && (def->sct_type == SCT_MOUNT ||
516 def->sct_type == SCT_WATER ||
517 def->sct_type == SCT_CAPIT ||
518 def->sct_type == SCT_FORTR ||
519 def->sct_type == SCT_WASTE)) {
520 pr("You can't air-assault a %s sector!\n",
521 def->sct_dcp->d_name);
522 return abort_attack();
526 return board_abort(off, def);
528 return land_board_abort(off, def);
531 if (off && def->sct_dcp->d_mcst <= 0) {
532 pr("You can't %s a %s sector!\n", att_mode[combat_mode],
533 def->sct_dcp->d_name);
534 return abort_attack();
536 if (!off || off->relations_checked)
538 off->relations_checked = 1;
541 setcont(player->cnum, def->own, FOUND_SPY);
542 setcont(def->own, player->cnum, FOUND_SPY);
544 if (opt_SLOW_WAR && def->own != player->cnum) {
545 natp = getnatp(player->cnum);
546 issneak = getrel(natp, def->own);
548 if (issneak == ALLIED){
549 sprintf(y_or_n,"Sector is owned by %s, your ally, %s [yn]? ",cname(def->own), att_mode[combat_mode]);
550 if (!confirm(y_or_n))
551 return abort_attack();
554 if (opt_SNEAK_ATTACK) {
555 getsect(def->x, def->y, §);
556 if ((issneak != AT_WAR) && (def->own) && (def->own != player->cnum) &&
557 (sect.sct_oldown != player->cnum) && (issneak != SITZKRIEG) &&
558 (issneak != MOBILIZATION)) {
559 pr("You're not at war with them!\n");
560 if (!confirm("Do you really want to sneak attack (it will cost you $5000) [yn]? ")){
561 pr("Sneak attack cancelled!\n");
562 return abort_attack();
565 if ((issneak != AT_WAR) && (def->own) && (def->own != player->cnum) &&
566 (sect.sct_oldown != player->cnum) && ((issneak == MOBILIZATION) ||
567 (issneak == SITZKRIEG))) {
568 pr("You're not at war with them!\n");
569 return abort_attack();
572 if ((issneak != AT_WAR) && (def->own) &&
573 (sect.sct_oldown != player->cnum)) {
574 pr("You're not at war with them!\n");
575 return abort_attack();
583 * Lots of special things need to be checked for boarding, so I put it in
588 board_abort(struct combat *off, struct combat *def)
590 struct shpstr aship, dship; /* for tech levels */
593 if (att_get_combat(def, 1) < 0)
594 return abort_attack();
599 if (att_get_combat(off, 0) < 0)
600 return abort_attack();
602 if (off->x != def->x || off->y != def->y) {
603 pr("Ship #%d is not in the same sector!\n", def->shp_uid);
604 return abort_attack();
606 if (off->type == EF_SHIP) {
608 pr("%s has no mobility!\n", prcom(0, off));
609 return abort_attack();
611 getship(off->shp_uid, &aship);
612 getship(def->shp_uid, &dship);
613 if (techfact(aship.shp_tech, 1.0) *
614 aship.shp_speed * off->eff <=
615 techfact(dship.shp_tech, 1.0) *
616 dship.shp_speed * def->eff) {
617 pr("Victim ship moves faster than you do!\n");
620 "%s (#%d) %s failed to catch %s\n",
621 cname(aship.shp_own), aship.shp_own,
622 pr_com(0, off, def->own),
623 pr_com(0, def, def->own));
624 return abort_attack();
626 } else if (off->type != EF_SECTOR) {
627 pr("Please tell the deity that you got the 'banana boat' error\n");
628 return abort_attack();
630 if (def->shp_mcp->m_flags & M_SUB) {
631 getsect(def->x, def->y, §);
632 if (sect.sct_type == SCT_WATER) {
633 pr("You can't board a submarine!\n");
634 return abort_attack();
641 * Lots of special things need to be checked for boarding, so I put it in
643 * STM - I copied it for land unit boarding. :)
647 land_board_abort(struct combat *off, struct combat *def)
649 if (att_get_combat(def, 1) < 0)
650 return abort_attack();
655 if (att_get_combat(off, 0) < 0)
656 return abort_attack();
658 if (off->x != def->x || off->y != def->y) {
659 pr("Land unit #%d is not in the same sector!\n", def->lnd_uid);
660 return abort_attack();
666 /* If we are boarding, then the defending ship gets a chance to fire back */
668 att_approach(struct combat *off, struct combat *def)
674 pr("Approaching %s...\n", prcom(0, def));
677 "%s is being approached by %s...\n",
678 pr_com(0, def, def->own),
679 pr_com(0, off, def->own));
680 if (!(dam = shipdef(player->cnum, def->own, def->x, def->y)))
683 pr("They're firing at us sir!\n");
686 "Your fleet at %s does %d damage to %s\n",
687 xyas(def->x, def->y, def->own),
689 pr_com(0, off, def->own));
691 if (off->type == EF_SECTOR) {
692 getsect(off->x, off->y, §);
693 sectdamage(§, dam, 0);
695 pr("Enemy fleet at %s does %d damage to %s\n",
696 xyas(def->x, def->y, player->cnum),
698 } else if (off->type == EF_SHIP) {
699 getship(off->shp_uid, &ship);
700 shipdamage(&ship, dam);
701 putship(off->shp_uid, &ship);
702 if (def->own && ship.shp_effic < SHIP_MINEFF) {
704 "%s sunk!\n", pr_com(0, off, def->own));
705 nreport(player->cnum, N_SHP_LOSE, def->own, 1);
708 if (att_get_combat(off, 0) < 0)
709 return abort_attack();
713 /* The attack is valid. Tell the attacker about what they're going to hit */
716 att_show(struct combat *def)
718 /* Note that we tell the player about the treaty BEFORE we tell them
719 about the item. If we didn't, then they gain free information */
720 if (def->type == EF_SECTOR) {
721 if (!trechk(player->cnum, def->own, LANATT))
722 return abort_attack();
723 pr("%s is a %d%% %s %s with approximately %d military.\n",
724 xyas(def->x, def->y, player->cnum),
725 roundintby((int)def->eff, 10),
726 cname(def->own), def->sct_dcp->d_name,
727 roundintby(def->troops, 10));
728 if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
729 writemap(player->cnum);
730 } else if (def->type == EF_SHIP || def->type == EF_LAND) {
731 if (def->type == EF_SHIP) {
732 if (!trechk(player->cnum, def->own, SEAATT))
733 return abort_attack();
735 if (!trechk(player->cnum, def->own, LNDATT))
736 return abort_attack();
738 pr("%s is about %d%% efficient and has approximately %d mil on board.\n",
740 roundintby((int)def->eff, 10),
741 roundintby(def->troops, 10));
743 /* Ok, everything is fine */
747 /* Attack and assault ask the user which kind of support they want */
750 att_ask_support(int offset, int *fortp, int *shipp, int *landp, int *planep)
754 *fortp = *shipp = *landp = *planep = 1;
756 if (player->argp[offset] != (s_char *)0){
757 if ((player->argp[offset+1] == (s_char *)0) ||
758 (player->argp[offset+2] == (s_char *)0) ||
759 (player->argp[offset+3] == (s_char *)0)){
760 pr("If any support arguments are used, all must be!\n");
767 if (!(p = getstarg(player->argp[offset], "Use fort support? ", buf)))
770 if ((*p == 'y') || (*p == 'Y'))
773 if (!(p = getstarg(player->argp[offset+1], "Use ship support? ", buf)))
776 if ((*p == 'y') || (*p == 'Y'))
779 if (!(p = getstarg(player->argp[offset+2], "Use land support? ", buf)))
782 if ((*p == 'y') || (*p == 'Y'))
785 if (!(p = getstarg(player->argp[offset+3], "Use plane support? ", buf)))
788 if ((*p == 'y') || (*p == 'Y'))
795 * Attack, assault, and board ask the attacker what they'd like to attack
796 * with. This includes mil and land units from each "off" object. Note that
797 * after each sub-prompt, we check to make sure that the attack is still
798 * valid, and if it's not, then we abort the attack.
802 att_ask_offense(int combat_mode, struct combat *off, struct combat *def, struct emp_qelem *olist, int *a_spyp, int *a_engineerp)
805 s_char land_answer[1024];
808 if (att_abort(combat_mode, off, def))
810 bzero(land_answer, sizeof(land_answer));
811 for (n = 0; n <= off->last; ++n) {
812 off[n].troops = ask_off(combat_mode, off + n, def);
813 if (att_abort(combat_mode, off, def))
815 ask_olist(combat_mode, off + n, def, olist, land_answer,
816 a_spyp, a_engineerp);
817 if (att_abort(combat_mode, off, def))
823 /* How many mil is off allowed to attack with when it attacks def? */
826 get_mob_support(int combat_mode, struct combat *off, struct combat *def)
830 switch (combat_mode) {
832 mob_support = off->mob / sector_mcost(getsectp(def->x, def->y),
836 /* mob_support = off->mob / sector_mcost(def->sct_type, def->eff);*/
837 if (mob_support < off->troops)
838 pr("Sector %s has %d mobility which can only support %d mil,\n",
839 xyas(off->x, off->y, player->cnum),
843 mob_support = off->troops;
846 if (def->own != player->cnum && def->mil) {
847 if (off->shp_mcp->m_flags & M_SEMILAND)
848 return off->troops/4;
849 else if (!(off->shp_mcp->m_flags & M_LAND))
850 return off->troops/10;
854 if (off->type == EF_SECTOR && off->mob <= 0)
856 mob_support = vl_find(V_MILIT, def->shp_mcp->m_vtype,
857 def->shp_mcp->m_vamt,def->shp_mcp->m_nv);
858 if (mob_support < off->troops)
859 pr("The size of the ship you are trying to board limits your party to %d mil,\n", mob_support);
861 mob_support = off->troops;
866 if (def->lnd_lcp->l_flags & L_SPY)
868 mob_support = vl_find(V_MILIT, def->lnd_lcp->l_vtype,
869 def->lnd_lcp->l_vamt,def->lnd_lcp->l_nv);
870 if (mob_support < off->troops)
871 pr("The size of the unit you are trying to board limits your party to %d mil,\n", mob_support);
873 mob_support = off->troops;
880 * If the attacker decides to go through with the attack, then the
881 * sectors/ships they are attacking with may be charged some mobility.
882 * This is where that amount of mobility is calculated. It is actually
883 * subtracted "for real" from the object's mobility in put_combat().
887 calc_mobcost(int combat_mode, struct combat *off, struct combat *def, int attacking_mil)
893 switch (combat_mode) {
896 max(1, (int)(attacking_mil * sector_mcost(getsectp(def->x, def->y), MOB_ROAD)));
899 off->mobcost += max(1, attacking_mil / 5);
904 off->mobcost += max(1, attacking_mil / 5);
907 /* the 2 in the formula below is a fudge factor */
908 getship(def->shp_uid, &ship);
909 off->mobcost += (def->eff / 100) *
910 (ship.shp_speed / 2);
915 /* How many mil to we want to attack from off against def? */
918 ask_off(int combat_mode, struct combat *off, struct combat *def)
924 if (att_get_combat(off, 0) <= 0)
926 if ((off->type == EF_SECTOR) && (off->own != player->cnum))
928 if ((mob_support = get_mob_support(combat_mode, off, def)) <= 0)
930 if (off->type == EF_SECTOR) {
931 if (off->own != player->cnum)
933 sprintf(prompt, "Number of mil from %s at %s (max %d) : ",
934 off->sct_dcp->d_name,
935 xyas(off->x, off->y, player->cnum), mob_support);
937 sprintf(prompt, "Number of mil from %s (max %d) : ",
938 prcom(0, off), mob_support);
940 if ((attacking_mil = onearg(0, prompt)) < 0)
942 if (att_abort(combat_mode, off, def))
944 if (att_get_combat(off, 0) <= 0)
946 if ((attacking_mil = min(attacking_mil, min(mob_support, off->troops))) <= 0)
949 calc_mobcost(combat_mode, off, def, attacking_mil);
950 return attacking_mil;
954 * Which units would you like to attack with or move in with [ynYNq?]
958 att_prompt(s_char *prompt, s_char army)
971 if (p && *p == 'q') {
978 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);
979 p = getstring(prompt, buf);
980 if (player->aborted) {
988 /* Ask the attacker which units they want to attack/assault/board with */
991 ask_olist(int combat_mode, struct combat *off, struct combat *def, struct emp_qelem *olist, s_char *land_answer, int *a_spyp, int *a_engineerp)
1004 if (def->type == EF_LAND)
1006 if (def->type == EF_SHIP)
1007 maxland = def->shp_mcp->m_nland;
1009 snxtitem_xy(&ni,EF_LAND,off->x,off->y);
1010 while(nxtitem(&ni,(s_char *)&land)){
1011 if (land.lnd_own != player->cnum)
1013 if (land.lnd_effic < LAND_MINEFF)
1015 if (land_answer[(int)land.lnd_army] == 'N')
1017 if (!lnd_can_attack(&land))
1019 lcp = &lchr[(int)land.lnd_type];
1021 if (def->type == EF_SHIP && !maxland) {
1022 pr("Land units are not able to board this kind of ship\n");
1025 if (land.lnd_mobil <= 0) {
1026 pr("%s is out of mobility, and cannot %s\n",
1028 att_mode[combat_mode]);
1033 if (ontradingblock(EF_LAND, (int *)&land)) {
1034 pr("%s is on the trading block, and cannot %s\n",
1035 prland(&land), att_mode[combat_mode]);
1040 if (off->type == EF_SECTOR && land.lnd_ship >= 0) {
1041 pr("%s is on ship #%d, and cannot %s\n",
1042 prland(&land), land.lnd_ship,
1043 att_mode[combat_mode]);
1045 } else if (off->type == EF_SHIP) {
1046 if (land.lnd_ship != off->shp_uid)
1048 } else if (land.lnd_land >= 0) {
1049 pr("%s is on unit #%d, and cannot %s\n",
1050 prland(&land), land.lnd_land, att_mode[combat_mode]);
1053 switch (combat_mode) {
1055 mobcost = lnd_mobcost(&land, getsectp(def->x, def->y), MOB_NONE);
1056 if (land.lnd_mobil < mobcost) {
1057 pr("%s does not have enough mobility (%d needed)\n",
1066 if (!(lcp->l_flags & L_ASSAULT))
1069 att_val = attack_val(combat_mode, &land);
1071 pr("%s has no offensive strength\n",
1075 resupply_all(&land);
1076 putland(land.lnd_uid,&land);
1077 if (!has_supply(&land)){
1078 pr("%s is out of supply, and cannot %s\n",
1079 prland(&land), att_mode[combat_mode]);
1082 if (def->type == EF_SHIP && first_time) {
1084 pr("You may board with a maximum of %d land units\n", maxland);
1086 pr("%s has a base %s value of %d\n",
1088 att_mode[combat_mode],
1090 if (land_answer[(int)land.lnd_army] != 'Y') {
1092 "%s with %s %s (%c %d%%) [ynYNq?] ",
1093 att_mode[combat_mode],
1096 land.lnd_army==' '?'~':land.lnd_army,
1098 land_answer[(int)land.lnd_army] =
1099 att_prompt(prompt, land.lnd_army);
1100 if (att_abort(combat_mode, off, def))
1102 if (land_answer[(int)land.lnd_army] != 'y' &&
1103 land_answer[(int)land.lnd_army] != 'Y')
1106 if (!(llp = (struct llist *)malloc(sizeof(struct llist)))) {
1107 logerror("Malloc failed in attack!\n");
1111 bzero((s_char *)llp,sizeof(struct llist));
1112 emp_insque(&llp->queue,olist);
1113 llp->mobil = mobcost;
1114 if (!get_land(combat_mode, def, land.lnd_uid, llp, 0))
1116 if (lnd_spyval(&land) > *a_spyp)
1117 *a_spyp = lnd_spyval(&land);
1118 if (llp->lcp->l_flags & L_ENGINEER)
1120 if (def->type == EF_SHIP && ++count >= maxland)
1125 /* What's the offense or defense multiplier? */
1128 att_combat_eff(struct combat *com)
1132 double sector_strength(struct sctstr *sp);
1135 if (com->type == EF_SECTOR) {
1136 eff = com->eff / 100.0;
1137 if (com->own == player->cnum) {
1138 str = com->sct_dcp->d_ostr;
1139 eff = 1.0 + ((str - 1.0) * eff);
1141 eff = sector_strength(getsectp(com->x, com->y));
1142 /* str = com->sct_dcp->d_dstr;*/
1143 } else if (com->type == EF_SHIP && com->own != player->cnum) {
1144 getship(com->shp_uid, &ship);
1145 eff = (1.0 + ship.shp_armor / 100.0);
1151 * Estimate the defense strength and give the attacker a chance to abort
1152 * if the odds are less than 50%
1156 att_estimate_defense(int combat_mode, struct combat *off, struct emp_qelem *olist, struct combat *def, int a_spy)
1163 * Get the attacker units & mil again in case they changed while the
1164 * attacker was answering sub-prompts.
1167 ototal = get_ototal(combat_mode, off, olist, 1.0, 1);
1168 if (att_empty_attack(combat_mode, ototal, def))
1169 return abort_attack();
1170 if (combat_mode == A_PARA)
1172 pr("\n Initial attack strength: %8d\n", ototal);
1174 estimate = att_combat_eff(def) * roundintby(def->troops, 10);
1175 estimate += att_combat_eff(def) * get_dlist(def, 0, a_spy, 0);
1178 * Calculate the initial (pre-support) attack odds. If they're less
1179 * than 50%, ask for a confirmation.
1182 odds = (int)(att_calcodds(ototal,estimate) * 100);
1186 pr(" Estimated defense strength: %8d\n", estimate);
1187 pr(" Estimated odds: %8d%%\n\n",odds);
1188 sprintf(prompt, "Are you sure you want to %s [yn]? ",
1189 att_mode[combat_mode]);
1190 if (!confirm(prompt))
1191 return abort_attack();
1192 ototal = get_ototal(combat_mode, off, olist,1.0,1);
1193 if (att_empty_attack(combat_mode, ototal, def))
1194 return abort_attack();
1200 /* Get the defensive units and reacting units */
1202 att_get_defense(struct emp_qelem *olist, struct combat *def, struct emp_qelem *dlist, int a_spy, int ototal)
1205 struct emp_qelem *qp;
1211 get_dlist(def, dlist, 0, &d_spy);
1212 dtotal = get_dtotal(def,dlist,1.0,0);
1215 * Call in reacting units
1218 if (def->type == EF_SECTOR && def->sct_type != SCT_MOUNT)
1219 att_reacting_units(def, dlist, a_spy, &d_spy, ototal);
1221 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw) {
1222 llp = (struct llist *)qp;
1223 intelligence_report(def->own, &llp->land, d_spy,
1224 "Scouts report attacking unit:");
1227 old_dtotal = dtotal;
1228 dtotal = get_dtotal(def,dlist,1.0,0);
1229 if (dtotal != old_dtotal)
1230 pr("Defense strength with reacting units: %8d\n", dtotal);
1235 /* Get the defensive land units in the sector or on the ship */
1238 get_dlist(struct combat *def, struct emp_qelem *list, int a_spy, int *d_spyp)
1240 struct nstr_item ni;
1245 /* In here is where you need to take out spies and trains from the defending
1246 lists. Spies try to hide, trains get trapped and can be boarded. */
1248 snxtitem_xy(&ni,EF_LAND,def->x,def->y);
1249 while(nxtitem(&ni,(s_char *)&land)){
1252 if (land.lnd_own != def->own)
1254 if (def->type == EF_SECTOR && land.lnd_ship >= 0)
1256 if (def->type == EF_SECTOR && land.lnd_land >= 0)
1258 if (def->type == EF_SHIP && land.lnd_ship != def->shp_uid)
1260 if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
1262 if (!list) { /* Just estimating the enemy strength */
1263 estimate += intelligence_report(player->cnum,
1265 "Scouts report defending unit:");
1268 if (!(llp = (struct llist *)malloc(sizeof(struct llist)))) {
1269 logerror("Malloc failed in attack!\n");
1273 bzero((s_char *)llp,sizeof(struct llist));
1274 emp_insque(&llp->queue,list);
1275 llp->supplied = has_supply(&land);
1276 if (!get_land(A_DEFEND,def,land.lnd_uid, llp, 1))
1278 if (lnd_spyval(&land) > *d_spyp)
1279 *d_spyp = lnd_spyval(&land);
1284 /* Calculate the total offensive strength */
1287 get_ototal(int combat_mode, struct combat *off, struct emp_qelem *olist, double osupport, int check)
1290 struct emp_qelem *qp, *next;
1295 * first, total the attacking mil
1298 for (n = 0; n <= off->last; ++n) {
1299 if (off[n].type == EF_BAD || (check &&
1300 att_get_combat(&off[n], 0) <= 0))
1302 ototal += off[n].troops * att_combat_eff(off + n);
1306 * next, add in the attack_values of all
1307 * the attacking units
1310 for (qp = olist->q_forw; qp != olist; qp = next) {
1312 llp = (struct llist *)qp;
1313 if (check && !get_land(combat_mode,0,llp->land.lnd_uid, llp, 0))
1315 if (combat_mode == A_ATTACK) {
1317 for(n = 0;n <= off->last; ++n) {
1318 if (off[n].type == EF_BAD)
1320 if ((off[n].x == llp->land.lnd_x) &&
1321 (off[n].y == llp->land.lnd_y))
1325 lnd_delete(llp, "is in a sector not owned by you");
1328 ototal += attack_val(combat_mode, &llp->land) *
1329 att_combat_eff(off + w);
1331 ototal += attack_val(combat_mode, &llp->land);
1336 return ldround(ototal, 1);
1339 /* Calculate the total defensive strength */
1342 get_dtotal(struct combat *def, struct emp_qelem *list, double dsupport, int check)
1344 double dtotal=0.0, eff = 1.0, d_unit;
1345 struct emp_qelem *qp, *next;
1348 if (check && att_get_combat(def, 1) < 0)
1350 eff = att_combat_eff(def);
1351 dtotal = def->troops * eff;
1354 * next, add in the defense_values of all
1355 * the defending non-retreating units
1358 for (qp = list->q_forw; qp != list; qp = next) {
1360 llp = (struct llist *)qp;
1362 if (check && !get_land(A_DEFEND,def,llp->land.lnd_uid, llp, 1))
1364 d_unit = defense_val(&llp->land);
1366 d_unit = ((double)defense_val(&llp->land)/2.0);
1367 dtotal += d_unit * eff;
1372 return ldround(dtotal,1);
1376 * This is the land unit integrity check. Note that we don't print
1377 * warnings about victim land units because the attacker may not have seen them
1381 get_land(int combat_mode, struct combat *def, int uid, struct llist *llp, int victim_land)
1383 struct lndstr *lp = &llp->land;
1388 if (!llp->lcp) { /* first time */
1389 llp->x = llp->land.lnd_x;
1390 llp->y = llp->land.lnd_y;
1391 llp->lcp = &lchr[(int)llp->land.lnd_type];
1392 } else { /* not first time */
1393 if (lp->lnd_effic < LAND_MINEFF) {
1394 sprintf(buf, "was destroyed and is no longer a part of the %s", att_mode[combat_mode]);
1395 lnd_delete(llp, buf);
1399 if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
1400 lnd_delete(llp, "left to go fight another battle and is no longer a part of the defense");
1404 if (lp->lnd_own != player->cnum) {
1405 sprintf(buf, "was destroyed and is no longer a part of the %s", att_mode[combat_mode]);
1406 lnd_delete(llp, buf);
1409 if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
1410 sprintf(buf, "left to fight another battle and is no longer a part of the %s", att_mode[combat_mode]);
1411 lnd_delete(llp, buf);
1414 if (lp->lnd_effic < llp->eff) {
1415 sprintf(buf, "damaged from %d%% to %d%%",
1416 llp->eff, lp->lnd_effic);
1417 lnd_print(llp, buf);
1421 llp->eff = llp->land.lnd_effic;
1427 * Put the land unit on the disk. If there was some mobility cost, then
1428 * subtract it from the units mobility. Note that this works the same way
1429 * as sectors & ships in that no mobility is actually taken until the attacker
1430 * has committed to attacking.
1434 kill_land(struct emp_qelem *list)
1436 struct emp_qelem *qp, *next;
1439 for (qp = list->q_forw; qp != list; qp = next) {
1441 llp = (struct llist *)qp;
1442 if (llp->land.lnd_ship >= 0) {
1443 llp->land.lnd_effic=0;
1444 lnd_delete(llp, "cannot return to the ship, and dies!");
1450 att_infect_units(struct emp_qelem *list, int plague)
1452 struct emp_qelem *qp, *next;
1457 for (qp = list->q_forw; qp != list; qp = next) {
1459 llp = (struct llist *)qp;
1460 if (getvar(V_PSTAGE, (s_char *)&(llp->land), EF_LAND) == PLG_HEALTHY)
1461 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)&(llp->land), EF_LAND);
1466 put_land(struct emp_qelem *list)
1468 struct emp_qelem *qp, *next;
1471 for (qp = list->q_forw; qp != list; qp = next) {
1473 llp = (struct llist *)qp;
1474 llp->land.lnd_mission=0;
1475 llp->land.lnd_harden=0;
1476 llp->land.lnd_mobil -= (int)llp->mobil;
1478 putland(llp->land.lnd_uid, &llp->land);
1479 if (llp->land.lnd_own != player->cnum) {
1480 emp_remque((struct emp_qelem *)llp);
1481 free((s_char *)llp);
1483 get_land(A_ATTACK,0, llp->land.lnd_uid, llp, 0);
1488 * Keep sending in reinforcements until it looks like we're going to win.
1489 * Note that the "strength" command also calls this routine.
1493 att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy, int *d_spyp, int ototal)
1495 struct nstr_item ni;
1497 struct sctstr sect, dsect;
1507 double eff = att_combat_eff(def);
1513 * All units that are within their reaction radius and not damaged
1514 * below their morale value now get to react to the threatened sect.
1515 * Once we've sent enough to counter the threat, stop sending them.
1517 * Not anymore. All units get to react. :)
1521 dtotal = get_dtotal(def, list, 1.0, 1);
1524 snxtitem_all(&ni,EF_LAND);
1525 while(nxtitem(&ni,(s_char *)&land) &&
1526 (dtotal + new_land * eff < (int)(1.2*(float)ototal))){
1529 if (!land.lnd_rad_max)
1531 if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
1533 if (land.lnd_own != def->own)
1535 if (land.lnd_ship >= 0)
1537 if (!defense_val(&land))
1540 if (land.lnd_effic <= land.lnd_retreat)
1543 if (!lnd_can_attack(&land))
1546 /* Only supplied units can react */
1547 if (!(supply_state=has_supply(&land)))
1550 dist = mapdist(land.lnd_x,land.lnd_y,def->x,def->y);
1552 getsect(land.lnd_x, land.lnd_y, §);
1553 /* Units on efficient headquarters can react 1 farther */
1554 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
1555 radius=land.lnd_rad_max+1;
1557 radius=land.lnd_rad_max;
1559 if (land.lnd_mission == MI_RESERVE)
1565 getsect(def->x, def->y, &dsect);
1566 if (!(p = (s_char *)BestLandPath(buf, §, &dsect, &move_cost, MOB_ROAD)))
1569 mobcost = land.lnd_effic * 0.01*lchr[(int)land.lnd_type].l_spd;
1572 mobcost = 480.0 / (mobcost + techfact(land.lnd_tech, mobcost));
1573 mobcost *= (move_cost * 5.0);
1575 if (land.lnd_mobil < mobcost)
1578 new_land += defense_val(&land);
1580 if (!list) /* we are in the "strength" command */
1583 /* move to defending sector */
1584 land.lnd_mobil -= ldround(mobcost,1);
1587 land.lnd_x = def->x;
1588 land.lnd_y = def->y;
1589 putland(land.lnd_uid,&land);
1590 wu(0,land.lnd_own,"%s reacts to %s.\n",
1592 xyas(land.lnd_x,land.lnd_y,land.lnd_own));
1594 llp = (struct llist *)
1595 malloc(sizeof(struct llist));
1597 bzero((s_char *)llp,sizeof(struct llist));
1598 llp->supplied = supply_state;
1601 llp->lcp = &lchr[(int)land.lnd_type];
1602 bcopy((s_char *)&land,(s_char *)&llp->land,sizeof(struct lndstr));
1603 emp_insque(&llp->queue, list);
1604 if (lnd_spyval(&land) > *d_spyp)
1605 *d_spyp = lnd_spyval(&land);
1607 intelligence_report(player->cnum,&land,a_spy,
1608 "Scouts sight reacting enemy unit:");
1613 /* Pop off shells and fly bombing missions to get your attack multiplier up */
1616 get_osupport(s_char *outs, struct combat *def, int fort_sup, int ship_sup, int land_sup, int plane_sup)
1618 double osupport = 1.0;
1620 double af, as, au, ap;
1622 af = as = au = ap = 0.0;
1624 dam = dd(def->own, player->cnum, def->x, def->y, 0, 0);
1625 af = ((double)dam/100.0);
1629 dam = sd(def->own, player->cnum, def->x, def->y, 0, 0, 0);
1631 as = ((double)dam/100.0);
1636 dam = lnd_support(def->own, player->cnum, def->x, def->y);
1637 au = ((double)dam/100.0);
1642 dam = off_support(def->x,def->y,def->own,player->cnum);
1643 ap = (((double)dam)/100.0);
1646 sprintf(outs,"attacker\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n",af,as,au,ap);
1650 /* Pop off shells and fly bombing missions to get your defense multiplier up */
1653 get_dsupport(s_char *outs, struct emp_qelem *list, struct combat *def, int ototal, int dtotal)
1655 double dsupport = 1.0;
1657 double df, ds, du, dp;
1660 df = ds = du = dp = 0.0;
1661 if (dtotal < 0.1 * ototal) {
1663 } else if (dtotal >= 1.2 * ototal) {
1666 dam = dd(player->cnum, def->own, def->x, def->y, 0, 1);
1667 df = ((double)dam/100.0);
1670 dtotal = get_dtotal(def,list,dsupport,0);
1671 if ( dtotal < 1.2 * ototal) {
1672 dam = sd(player->cnum, def->own, def->x, def->y, 0, 1, 0);
1673 ds = ((double)dam/100.0);
1675 dtotal = get_dtotal(def,list,dsupport,0);
1677 if ( dtotal < 1.2 * ototal) {
1678 dam = lnd_support(player->cnum, def->own, def->x, def->y);
1679 du = ((double)dam/100.0);
1681 dtotal = get_dtotal(def,list,dsupport,1);
1683 if ( dtotal < 1.2 * ototal) {
1684 dam = def_support(def->x,def->y,player->cnum,def->own);
1685 dp = (((double)dam)/100.0);
1692 sprintf(outs,"defender\t%1.2f\t%1.2f\t%1.2f\t%1.2f\n\n",df,ds,du,dp);
1695 wu(0,def->own,"\nOdds are bad for us...support cancelled.\n\n");
1697 wu(0,def->own,"\nOdds are good for us...support cancelled.\n\n");
1703 * Land mines add to the defense multiplier. If the attacker has engineers
1704 * then this multipluer is cut in half.
1708 get_mine_dsupport(struct combat *def, int a_engineer)
1713 getsect(def->x, def->y, §);
1715 if (sect.sct_oldown != player->cnum){
1716 mines = getvar(V_MINE, (s_char *)§, EF_SECTOR);
1717 mines = min(mines,20);
1719 mines = ldround(((double)mines/2.0),1);
1722 wu(0,def->own,"Defending mines add %1.2f\n", mines * 0.02);
1723 pr("Defending mines add %1.2f\n", mines * 0.02);
1724 return mines * 0.02;
1730 /* Get the offensive and defensive support */
1732 att_get_support(int combat_mode, int ofort, int oship, int oland, int oplane, struct emp_qelem *olist, struct combat *off, struct emp_qelem *dlist, struct combat *def, double *osupportp, double *dsupportp, int a_engineer)
1735 s_char osupports[512];
1736 s_char dsupports[512];
1738 if (combat_mode == A_PARA)
1741 *osupportp = get_osupport(osupports, def,
1742 ofort, oship, oland, oplane);
1745 * I need to put a 1 at the end of the next four total_stren calls
1746 * becauase units & mil may have been damaged by collateral damage or
1747 * neclear warheads from the offensive & defensive support.
1750 ototal = get_ototal(combat_mode,off,olist,*osupportp,1);
1751 if (att_empty_attack(combat_mode, ototal, def))
1752 return abort_attack();
1753 dtotal = get_dtotal(def,dlist,*dsupportp,1);
1756 * Calculate defensive support. If odds are too good or too bad
1757 * then don't call in support.
1760 *dsupportp = get_dsupport(dsupports, dlist, def, ototal, dtotal);
1761 ototal = get_ototal(combat_mode,off,olist,*osupportp,1);
1762 if (att_empty_attack(combat_mode, ototal, def))
1763 return abort_attack();
1765 if ((*osupports || *dsupports) &&
1766 (*osupportp != 1.0 || *dsupportp != 1.0)) {
1767 pr("\n\t\tsupport values\n");
1768 pr("\t\tforts\tships\tunits\tplanes\n");
1769 if (*osupportp != 1.0)
1770 pr("%s", osupports);
1771 if (*dsupportp != 1.0)
1772 pr("%s", dsupports);
1774 wu(0,def->own,"\n\t\tsupport values\n");
1775 wu(0,def->own,"\t\tforts\tships\tunits\tplanes\n");
1776 if (*osupportp != 1.0)
1777 wu(0, def->own, "%s", osupports);
1778 if (*dsupportp != 1.0)
1779 wu(0, def->own, "%s", dsupports);
1783 dtotal = get_dtotal(def,dlist,*dsupportp,1);
1784 if (dtotal && def->type == EF_SECTOR)
1785 *dsupportp += get_mine_dsupport(def, a_engineer);
1789 /* How many two-legged bipeds are in this combat force? */
1792 count_bodies(struct combat *off, struct emp_qelem *list)
1796 struct emp_qelem *qp;
1799 for (n = 0; n <= off->last; ++n)
1800 bodies += off[n].troops;
1801 for (qp=list->q_forw;qp!=list;qp=qp->q_forw) {
1802 llp = (struct llist *)qp;
1803 bodies += total_mil(&llp->land);
1808 /* This is where the fighting actually occurs. */
1811 att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist, double osupport, struct combat *def, struct emp_qelem *dlist, double dsupport)
1814 int a_cas = 0; /* Casualty counts */
1816 int ototal; /* total attacking strength */
1817 int dtotal; /* total defending strength */
1818 int a_bodies; /* total attacking mil (incl. mil in units) */
1819 int d_bodies; /* total defending mil (incl. mil in units) */
1828 ototal = get_ototal(combat_mode,off,olist,osupport,
1829 combat_mode != A_PARA);
1830 dtotal = get_dtotal(def,dlist,dsupport,0);
1834 a_bodies = count_bodies(off, olist);
1835 d_bodies = count_bodies(def, dlist);
1836 d_mil = def->troops;
1837 for (n=0; n <= off->last; ++n)
1838 if (off[n].type == EF_BAD)
1841 a_troops[n] = off[n].troops;
1843 /* This switch is required to get the spacing right */
1844 switch (combat_mode) {
1846 pr(" Final attack strength: %8d\n", ototal);
1849 pr(" Final assault strength: %8d\n", ototal);
1852 if (def->sct_type == SCT_MOUNT ||
1853 def->sct_type == SCT_WATER ||
1854 def->sct_type == SCT_CAPIT ||
1855 def->sct_type == SCT_FORTR ||
1856 def->sct_type == SCT_WASTE) {
1857 pr("You can't air-assault a %s sector!\n",
1858 def->sct_dcp->d_name);
1861 ototal = get_ototal(A_PARA,off,olist,osupport, 0);
1863 pr(" Final air-assault strength: %8d\n", ototal);
1867 pr(" Final board strength: %8d\n", ototal);
1871 pr(" Final defense strength: %8d\n",dtotal);
1872 odds = att_calcodds(ototal,dtotal);
1873 pr(" Final odds: %8d%%\n",(int)(odds*100));
1875 /* spread the plague */
1876 if (combat_mode != A_PARA) {
1878 for (n = 0; n <= off->last; ++n)
1879 if (off[n].type != EF_BAD)
1880 def->plague |= off[n].plague;
1881 for (n = 0; n <= off->last; ++n)
1882 if (off[n].type != EF_BAD)
1883 off[n].plague |= def->plague;
1885 att_infect_units(olist, off->plague);
1886 att_infect_units(dlist, def->plague);
1888 /* Fighting is slightly random. There is always that last little
1889 * effort you see people put in. Or the stray bullet that takes out
1890 * an officer and the rest go into chaos. Things like that.
1891 * Thus, we have added a very slight random factor that will sometimes
1892 * allow the little guy to win. We modify the odds a little
1893 * (either +- 5%) to account for this randomness. We also only
1894 * recalculate the odds every 8-50 casualties, not every cacsualty,
1895 * since a single dead guy normally wouldn't cause a commander to
1896 * rethink his strategies, but 50 dead guys might. */
1897 odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1902 recalctime = 8 + (random() % 43);
1903 while (!success && ototal){
1906 d_cas += take_casualty(A_DEFEND,def,dlist);
1907 dtotal = get_dtotal(def,dlist,dsupport,0);
1912 a_cas += take_casualty(combat_mode,off,olist);
1913 ototal = get_ototal(combat_mode,off,olist,osupport,0);
1915 if (((a_cas + d_cas) % 70) == 69)
1917 if (recalctime-- <= 0) {
1918 recalctime = 8 + (random() % 43);
1919 odds = att_calcodds(ototal,dtotal);
1920 odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
1928 /* update defense mobility & mil */
1932 if (def->type == EF_SECTOR && d_mil && d_cas) {
1935 /* Make sure we use a positive mobility here */
1936 tmob = ((def->mob < 0) ? -(def->mob) : (def->mob));
1937 def->mobcost = min(20, min(1, tmob - damage(tmob, 100 * d_cas / d_mil)));
1939 def->mil = def->troops;
1942 /* update attack mobility & mil */
1943 for (n=0; n <= off->last; ++n)
1944 if (off[n].type != EF_BAD && off[n].troops < a_troops[n]) {
1945 if (off[n].type == EF_SECTOR && off[n].mil)
1946 off[n].mobcost += min(20,min(1, off[n].mob - damage(off[n].mob, 100 * (a_troops[n] - off[n].troops) / off[n].mil)));
1947 off[n].mil -= a_troops[n] - off[n].troops;
1950 /* update land unit mobility */
1951 if (d_bodies && d_cas)
1952 lnd_takemob(dlist, (double)d_cas / d_bodies);
1953 if (a_bodies && a_cas)
1954 lnd_takemob(olist, (double)a_cas / a_bodies);
1956 /* damage attacked sector */
1957 def->eff = effdamage(def->eff, (d_cas + a_cas)/10);
1959 pr("- Casualties -\n Yours: %d\n", a_cas);
1960 pr(" Theirs: %d\n", d_cas);
1961 pr("Papershuffling ... %.1f B.T.U\n", (d_cas + a_cas) * 0.15);
1962 player->btused += (int) ((d_cas + a_cas) * 0.015 + 0.5);
1965 switch (combat_mode) {
1967 news_item = def->own?N_WON_SECT:N_TOOK_UNOCC;
1968 pr("We have captured %s, sir!\n", prcom(0, def));
1969 action = "taking"; break;
1971 news_item = def->own?N_AWON_SECT:N_START_COL;
1972 pr("We have secured a beachhead at %s, sir!\n", prcom(0, def));
1973 action = "assaulting and taking"; break;
1975 news_item = def->own?N_PWON_SECT:N_PARA_UNOCC;
1976 pr("We have captured %s, sir!\n", prcom(0, def));
1977 action = "air-assaulting and taking"; break;
1979 news_item = N_BOARD_SHIP;
1980 pr("We have boarded %s, sir!\n", prcom(0, def));
1981 action = "boarding"; break;
1983 news_item = N_BOARD_LAND;
1984 pr("We have boarded %s, sir!\n", prcom(0, def));
1985 action = "boarding"; break;
1988 switch (combat_mode) {
1990 news_item = N_SCT_LOSE;
1991 pr("You have been defeated!\n");
1992 action = "attacking";
1995 news_item = N_ALOSE_SCT;
1996 pr("You have been defeated!\n");
1998 action = "trying to assault";
2001 news_item = N_PLOSE_SCT;
2002 pr("All of your troops were destroyed\n");
2003 action = "trying to air-assault";
2006 news_item = N_SHP_LOSE;
2007 pr("You have been repelled\n");
2009 action = "trying to board";
2012 news_item = N_LND_LOSE;
2013 pr("You have been repelled\n");
2015 action = "trying to board";
2019 nreport(player->cnum, news_item, def->own, 1);
2022 "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
2023 cname(player->cnum), player->cnum, a_cas,
2025 pr_com(0, def, def->own),
2029 if (opt_SNEAK_ATTACK) {
2030 do_sneak(def, success);
2033 send_reacting_units_home(def, dlist);
2035 /* putland the defending land */
2038 /* putland the attacking land */
2041 /* put the victim sector/ship/land */
2042 if (!success || !take_def(combat_mode, olist, off, def))
2045 /* put the attacking sectors/ship */
2046 for (n = 0; n <= off->last; ++n)
2047 if (off[n].type != EF_BAD)
2048 put_combat(&off[n]);
2053 switch (combat_mode) {
2055 ask_move_in(off, olist, def);
2057 /* put sectors again to get abandon warnings */
2058 for (n = 0; n <= off->last; ++n)
2059 if (off[n].type != EF_BAD)
2060 put_combat(&off[n]);
2063 att_move_in_off(combat_mode, off, olist, def);
2066 pr("%d of your troops now occupy %s\n", def->mil, prcom(0, def));
2070 /* What percentage of the combat forces going head-to-head are we? */
2073 att_calcodds(int ototal, int dtotal)
2077 /* calculate odds */
2080 else if (dtotal <=0)
2083 odds = ((double)ototal) / (dtotal + ototal);
2088 /* Here's where the dead soldiers get dragged off the battlefield */
2091 take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
2093 int to_take = CASUALTY_LUMP;
2094 int biggest_troops = 0, index = -1;
2095 int n, tot_troops = 0, biggest_mil, cas;
2096 struct emp_qelem *qp, *biggest;
2099 for (n = 0; n <= off->last; ++n) {
2100 if (off[n].type != EF_BAD) {
2101 tot_troops += off[n].troops;
2102 if (off[n].troops > biggest_troops) {
2103 biggest_troops = off[n].troops;
2110 to_take -= tot_troops;
2113 for(n = 0; n <= off->last; ++n)
2114 if (off[n].type != EF_BAD)
2118 * They can all come off mil. We rotate the casualties,
2119 * starting with the sector containing the most mil.
2121 to_take = CASUALTY_LUMP;
2123 pr("ERROR: Tell the deity that you got the 'green librarian' error\n");
2126 while (to_take > 0) {
2127 for (n = index; n <= off->last && to_take; ++n) {
2128 if (off[n].type != EF_BAD &&
2129 off[n].troops > 0) {
2134 for (n = 0; n < index && to_take; ++n) {
2135 if (off[n].type != EF_BAD &&
2136 off[n].troops > 0) {
2142 return CASUALTY_LUMP;
2146 return (CASUALTY_LUMP - to_take);
2149 * Need to take some casualties from attacking units
2150 * Procedure: find the biggest unit remaining (in
2151 * terms of mil) and give it the casualties.
2153 biggest = (struct emp_qelem *)0;
2155 for (qp = olist->q_forw; qp != olist; qp = qp->q_forw){
2156 llp = (struct llist *)qp;
2158 if (total_mil(&llp->land) > biggest_mil){
2159 biggest_mil = total_mil(&llp->land);
2163 if (biggest == (struct emp_qelem *)0)
2164 return (CASUALTY_LUMP-to_take);
2166 llp = (struct llist *)biggest;
2167 cas = lnd_take_casualty(combat_mode,llp,to_take);
2168 return (CASUALTY_LUMP-(to_take-cas));
2171 /* Send reacting defense units back to where they came from (at no mob cost) */
2174 send_reacting_units_home(struct combat *def, struct emp_qelem *list)
2176 struct emp_qelem *qp, *next;
2180 for (qp = list->q_forw; qp != list; qp = next) {
2182 llp = (struct llist *)qp;
2183 if ((llp->land.lnd_x != llp->x) ||
2184 (llp->land.lnd_y != llp->y)) {
2185 sprintf(buf, "returns to %s",
2186 xyas(llp->x, llp->y, llp->land.lnd_own));
2187 llp->land.lnd_x = llp->x;
2188 llp->land.lnd_y = llp->y;
2189 lnd_delete(llp, buf);
2194 /* Check for 0 offense strength. This call will always preceed an abort */
2197 att_empty_attack(int combat_mode, int ototal, struct combat *def)
2200 if (def->own && player->cnum != def->own) {
2202 "%s (#%d) considered %sing you @%s\n",
2203 cname(player->cnum), player->cnum,
2204 att_mode[combat_mode],
2205 xyas(def->x, def->y, def->own));
2207 pr("No troops for %s...\n", att_mode[combat_mode]);
2214 * Take the defending sector or ship from the defender and give it to the
2219 take_def(int combat_mode, struct emp_qelem *list, struct combat *off, struct combat *def)
2223 struct llist *llp, *delete_me = 0;
2229 for (n = 0; n <= off->last && !occuppied; ++n) {
2230 if (off[n].type != EF_BAD &&
2231 off[n].troops > 0 &&
2232 (off[n].type != EF_SECTOR || off[n].mob)) {
2234 if (def->type == EF_LAND) {
2235 if (def->lnd_lcp->l_flags & L_SPY) {
2242 pr("1 mil from %s moves %s\n",
2243 prcom(0, off + n), prcom(2, def));
2248 pr("%s left unoccupied\n", prcom(0, def));
2251 "No enemy troops moved %s so you still own it!\n", pr_com(2, def, def->own));
2254 llp = (struct llist *)list->q_forw;
2255 llp->land.lnd_x = def->x;
2256 llp->land.lnd_y = def->y;
2257 take_move_in_mob(combat_mode, llp, off, def);
2258 if (def->type == EF_SHIP) {
2259 llp->land.lnd_ship = def->shp_uid;
2260 sprintf(buf, "boards %s", prcom(0, def));
2263 llp->land.lnd_ship = -1;
2264 sprintf(buf, "moves in to occupy %s",
2265 xyas(def->x, def->y, player->cnum));
2266 lnd_delete(llp, buf);
2271 if (def->type == EF_SECTOR) {
2272 getsect(def->x, def->y, §);
2273 takeover(§, player->cnum);
2274 if (sect.sct_type == SCT_CAPIT || sect.sct_type == SCT_MOUNT)
2275 caploss(§, def->own,
2276 "* We have captured %s's capital, sir! *\n");
2278 } else if (def->type == EF_SHIP) {
2279 getship(def->shp_uid, &ship);
2280 takeover_ship(&ship, player->cnum, 1);
2281 putship(ship.shp_uid, &ship);
2282 } else if (def->type == EF_LAND) {
2283 getland(def->lnd_uid, &land);
2284 takeover_land(&land, player->cnum, 1);
2285 putland(land.lnd_uid, &land);
2288 lnd_delete(delete_me, buf);
2289 att_get_combat(def, 0);
2294 * Ask the attacker which mil & land units they'd like to move into the
2299 ask_move_in(struct combat *off, struct emp_qelem *olist, struct combat *def)
2302 struct emp_qelem *qp, *next;
2306 s_char land_answer[1024];
2309 for (n = 0; n <= off->last; ++n)
2310 if (off[n].type != EF_BAD && off[n].troops > 0)
2312 ask_move_in_off(&off[n], def);
2313 if (player->aborted)
2319 bzero(land_answer, sizeof(land_answer));
2320 for (qp = olist->q_forw; qp != olist; qp = next) {
2322 llp = (struct llist *)qp;
2323 answerp = &land_answer[(int)llp->land.lnd_army];
2324 if (player->aborted ||
2325 att_get_combat(def, 0) < 0)
2327 if (*answerp == 'Y')
2329 if (*answerp != 'N') {
2330 if (!get_land(A_ATTACK, def,llp->land.lnd_uid, llp, 0))
2332 sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
2334 llp->land.lnd_army==' '?'~':llp->land.lnd_army,
2335 llp->land.lnd_effic);
2336 *answerp = att_prompt(prompt, llp->land.lnd_army);
2337 if (player->aborted ||
2338 att_get_combat(def, 0) < 0)
2340 if (!get_land(A_ATTACK,def, llp->land.lnd_uid, llp, 0))
2343 if (*answerp == 'y' || *answerp == 'Y')
2345 sprintf(buf, "stays in %s",
2346 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2347 lnd_delete(llp, buf);
2351 if (att_get_combat(def, 0) < 0) {
2352 for (qp = olist->q_forw; qp != olist; qp = next) {
2354 llp = (struct llist *)qp;
2355 if (!get_land(A_ATTACK,def,llp->land.lnd_uid, llp, 0))
2357 sprintf(buf, "stays in %s",
2358 xyas(llp->land.lnd_x, llp->land.lnd_y, player->cnum));
2359 lnd_delete(llp, buf);
2363 if (opt_INTERDICT_ATT)
2364 lnd_interdict(olist, def->x, def->y, player->cnum);
2365 move_in_land(A_ATTACK, off, olist, def);
2368 /* Move offensive land units to the conquered sector or ship */
2371 move_in_land(int combat_mode, struct combat *off, struct emp_qelem *olist, struct combat *def)
2373 struct emp_qelem *qp, *next;
2379 for (qp = olist->q_forw; qp != olist; qp = next) {
2381 llp = (struct llist *)qp;
2382 if (!get_land(combat_mode, def,llp->land.lnd_uid, llp, 0))
2384 take_move_in_mob(combat_mode, llp, off, def);
2385 llp->land.lnd_x = def->x;
2386 llp->land.lnd_y = def->y;
2387 if (def->type == EF_SHIP)
2388 llp->land.lnd_ship = def->shp_uid;
2390 llp->land.lnd_ship = -1;
2394 if (def->type == EF_SECTOR) {
2395 if (opt_INTERDICT_ATT) {
2396 lnd_sweep(olist, 0, 0, def->own);
2397 lnd_check_mines(olist);
2399 sprintf(buf, "now occupies %s", prcom(0, def));
2401 sprintf(buf, "boards %s", prcom(0, def));
2405 for (qp = olist->q_forw; qp != olist; qp = next) {
2407 llp = (struct llist *)qp;
2408 lnd_print(llp, buf);
2416 * Move assaulting, paradropping, or boarding mil & units into def
2417 * If the mil are coming from a ship, then pack a lunch.
2421 att_move_in_off(int combat_mode, struct combat *off, struct emp_qelem *olist, struct combat *def)
2426 int defvec[I_MAX+1];
2427 int shipvec[I_MAX+1];
2429 int n, lunchbox = 0;
2432 move_in_land(combat_mode, off, olist, def);
2434 for (n = 0; n <= off->last; ++n) {
2435 if (off[n].type == EF_BAD || !off[n].troops)
2437 troops = off[n].troops;
2439 off[n].mil -= troops;
2441 put_combat(off + n);
2442 if (combat_mode == A_ASSAULT) {
2443 getship(off[n].shp_uid, &ship);
2444 getvec(VT_ITEM, shipvec, (s_char *)&ship, EF_SHIP);
2445 lunchbox += (int)((troops + 1) * shipvec[I_FOOD] /
2446 (shipvec[I_MILIT] + troops + shipvec[I_CIVIL] + 0.5));
2447 shipvec[I_FOOD] -= lunchbox;
2448 putvec(VT_ITEM, shipvec, (s_char *)&ship, EF_SHIP);
2449 putship(ship.shp_uid, &ship);
2456 if (def->type == EF_SECTOR) {
2457 getsect(def->x, def->y, §);
2458 thing = (s_char *)§
2459 } else if (def->type == EF_SHIP) {
2460 getship(def->shp_uid, &ship);
2461 thing = (s_char *)&ship;
2462 } else if (def->type == EF_LAND) {
2463 getship(def->lnd_uid, &land);
2464 thing = (s_char *)&land;
2466 pr("Please tell the deity that you got the 'hungry mole' error\n");
2469 getvec(VT_ITEM, defvec, thing, def->type);
2470 defvec[I_FOOD] += lunchbox;
2471 putvec(VT_ITEM, defvec, thing, def->type);
2472 if (def->type == EF_SECTOR)
2474 else if (def->type == EF_SHIP)
2475 putship(ship.shp_uid, &ship);
2477 putland(land.lnd_uid, &land);
2481 /* Ask how many mil to move in from each sector */
2484 ask_move_in_off(struct combat *off, struct combat *def)
2487 int num_mil, dam = 0, left;
2493 if (att_get_combat(off, 0) <= 0)
2495 if (att_get_combat(def, 0) < 0)
2497 if (off->own != player->cnum)
2499 d = sector_mcost(getsectp(def->x, def->y), MOB_ROAD);
2500 if ((mob_support = min(off->troops, (int)(off->mob / d))) <= 0)
2502 sprintf(prompt,"How many mil to move in from %s (%d max)? ", xyas(off->x, off->y, player->cnum), mob_support);
2503 if (!(p = getstring(prompt, buf)) ||
2505 (num_mil = atoi(p)) <= 0) {
2509 /* Make sure we don't move in more than we can support mobility-wise */
2510 if (num_mil > mob_support)
2511 num_mil = mob_support;
2512 if (att_get_combat(off, 0) <= 0)
2514 if (att_get_combat(def, 0) < 0)
2516 if ((num_mil = min(off->troops, num_mil)) <= 0) {
2517 pr("No mil moved in from %s\n",
2518 xyas(off->x, off->y, player->cnum));
2521 mob_support = max(1,(int)(num_mil * d));
2522 off->mob -= min(off->mob, mob_support);
2523 off->mil -= num_mil;
2524 off->troops -= num_mil;
2527 weight = num_mil * ichr[I_MILIT].i_lbs;
2528 if (opt_INTERDICT_ATT && chance(weight / 200.0)) {
2529 if (chance(weight/100.0))
2530 dam += ground_interdict(def->x, def->y, player->cnum, "military");
2531 dam += check_lmines(def->x, def->y, weight);
2535 left = commdamage(num_mil, dam, V_MILIT);
2536 if (left < num_mil) {
2538 pr("%d of the mil you were moving were destroyed!\nOnly %d mil made it to %s\n",
2541 xyas(def->x, def->y, player->cnum));
2543 pr("All of the mil you were moving were destroyed!\n");
2546 /* maybe got nuked */
2547 if (att_get_combat(def, 0) < 0)
2555 /* Charge land units for moving into a sector or onto a ship */
2558 take_move_in_mob(int combat_mode, struct llist *llp, struct combat *off, struct combat *def)
2560 extern int etu_per_update;
2561 extern float land_mob_scale;
2565 switch (combat_mode) {
2567 mobcost = lnd_mobcost(&llp->land, getsectp(def->x, def->y), MOB_NONE);
2568 new = llp->land.lnd_mobil - mobcost;
2571 llp->land.lnd_mobil = new;
2574 if (off->shp_mcp->m_flags & M_LAND) {
2575 if (llp->lcp->l_flags & L_MARINE)
2576 llp->land.lnd_mobil -= ((float)etu_per_update *land_mob_scale*0.5);
2578 llp->land.lnd_mobil -= ((float)etu_per_update
2581 if (llp->lcp->l_flags & L_MARINE)
2582 llp->land.lnd_mobil = 0;
2584 llp->land.lnd_mobil = (((float)etu_per_update
2585 *land_mob_scale)*(-1));
2589 /* I arbitrarily chose the numbers 10 and 40 below -KHS */
2590 if (llp->lcp->l_flags & L_MARINE)
2591 llp->land.lnd_mobil -= 10;
2593 llp->land.lnd_mobil -= 40;
2599 free_list(struct emp_qelem *list)
2601 register struct emp_qelem *qp, *next;
2603 if (!list || QEMPTY(list))
2616 att_free_lists(struct emp_qelem *olist, struct emp_qelem *dlist)
2624 * sector_strength - Everyone starts at 1. You can get up to a max
2625 * of d_dstr, depending on how much you build up the
2626 * defenses of the sector.
2630 sector_strength(struct sctstr *sp)
2636 if (sp->sct_type == SCT_MOUNT)
2639 d = d + ((double)(dchr[sp->sct_type].d_dstr - d) *
2640 ((double)sp->sct_defense / 100.0));
2642 if (d > dchr[sp->sct_type].d_dstr)
2643 d = dchr[sp->sct_type].d_dstr;