]> git.pond.sub.org Git - empserver/blob - src/lib/commands/assa.c
Import of Empire 4.2.12
[empserver] / src / lib / commands / assa.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  assa.c: Hit the beaches!
29  * 
30  *  Known contributors to this file:
31  *     Ken Stevens, 1995
32  *     Steve McClure, 1997
33  */
34
35 #include <math.h>
36 #include "misc.h"
37 #include "player.h"
38 #include "file.h"
39 #include "var.h"
40 #include "sect.h"
41 #include "path.h"
42 #include "news.h"
43 #include "treaty.h"
44 #include "nat.h"
45 #include "xy.h"
46 #include "land.h"
47 #include "nsc.h"
48 #include "mission.h"
49 #include "ship.h"
50 #include "combat.h"
51 #include "commands.h"
52
53 int
54 assa(void) {
55         struct  combat off[1];  /* assaulting ship */
56         struct  combat def[1];  /* defending sector */
57         int     fort_sup, ship_sup, land_sup, plane_sup;
58         struct  emp_qelem olist;        /* assaulting units */
59         struct  emp_qelem dlist;        /* defending units */
60         int     ototal;         /* total assaulting strength */
61         int     a_engineer = 0; /* assaulter engineers are present */
62         int     a_spy = 0;      /* the best assaulter scout */
63         double  osupport = 1.0; /* assault support */
64         double  dsupport = 1.0; /* defense support */
65         s_char  *p;
66         s_char  buf[1024];
67         int     n;
68         int     ourtotal;
69         struct emp_qelem *qp, *next;
70         struct llist *llp;
71         int rel;
72
73         att_combat_init(off, EF_SHIP);
74         att_combat_init(def, EF_SECTOR);
75         /*
76          * Collect input from the assaulter
77          */
78
79         /* What are we assaulting? */
80
81         if (!(p = getstarg(player->argp[1], "Sector :  ", buf)))
82                 return RET_SYN;
83         if (!sarg_xy(p, &def->x, &def->y))
84                 return RET_SYN;
85         if (att_abort(A_ASSAULT, 0, def))
86                 return RET_FAIL;
87
88         /*
89          * Ask the assaulter what he wants to assault with
90          */
91
92         if ((off->shp_uid = onearg(player->argp[2], "Assault from ship #")) < 0) {
93                 pr("You may only assault from one ship!\n");
94                 return RET_FAIL;
95         }
96         if (att_abort(A_ASSAULT, off, def)) {
97                 pr("Assault aborted\n");
98                 return RET_OK;
99         }
100
101         /* Show what we're assaulting, and check treaties */
102
103         if (att_show(def))
104             return RET_FAIL;
105
106         /* Ask about offensive support */
107
108         att_ask_support(3, &fort_sup, &ship_sup, &land_sup, &plane_sup);
109         if (att_abort(A_ASSAULT, off, def)) {
110                 att_empty_attack(A_ASSAULT, 0, def);
111                 return RET_OK;
112         }
113
114         /* Ask the player what he wants to assault with */
115
116         att_ask_offense(A_ASSAULT, off, def, &olist, &a_spy, &a_engineer);
117         if (att_abort(A_ASSAULT, off, def)) {
118                 pr("Assault aborted\n");
119                 att_empty_attack(A_ASSAULT, 0, def);
120                 return att_free_lists(&olist, 0);
121         }
122
123         /* If we're assaulting our own sector, end here */
124         if (def->own == player->cnum) {
125                 if (off->troops)
126                         pr("You reinforce %s with %d troops\n",
127                            xyas(def->x, def->y, player->cnum), off->troops);
128                 if (off->troops || !QEMPTY(&olist))
129                         att_move_in_off(A_ASSAULT, off, &olist, def);
130                 return RET_OK;
131         }
132
133         /*
134          * Estimate the defense strength and give the player a chance to abort
135          */
136
137         ototal = att_estimate_defense(A_ASSAULT, off, &olist, def, a_spy);
138         if (att_abort(A_ASSAULT, off, def)) {
139                 pr("Assault aborted\n");
140                 att_empty_attack(A_ASSAULT, 0, def);
141                 return att_free_lists(&olist, 0);
142         }
143
144         /*
145          * We have now got all the answers from the assaulter.  From this point
146          * forward, we can assume that this battle is the _only_ thing
147          * happening in the game.
148          */
149
150         /* First, we check to see if the only thing we have are spies
151          * assaulting.  If so, we try to sneak them on land.  If they
152          * make it, the defenders don't see a thing.  If they fail, well,
153          * the spies die, and the defenders see them. */
154
155         ourtotal = 0;
156         for (n = 0; n <= off->last; n++) {
157             if (off[n].type == EF_BAD)
158                 continue;
159             ourtotal += off[n].troops * att_combat_eff(off + n);
160         }
161         for (qp = olist.q_forw; qp != &olist; qp = next ) {
162             next = qp->q_forw;
163             llp = (struct llist *)qp;
164             if (lchr[(int)llp->land.lnd_type].l_flags & L_SPY)
165                 continue;
166             ourtotal++;
167         }
168
169         /* If no attacking forces (i.e. we got here with only spies)
170          * then try to sneak on-land. */
171
172         if (!ourtotal) {
173             pr("Trying to sneak on shore...\n");
174             
175             for (qp = olist.q_forw; qp != &olist; qp = next ) {
176                 next = qp->q_forw;
177                 llp = (struct llist *)qp;
178                 rel = getrel(getnatp(def->own), player->cnum);
179                 if (chance(0.10) || rel == ALLIED || !def->own) {
180                     pr("%s made it on shore safely.\n", prland(&llp->land));
181                     llp->land.lnd_x = def->x;
182                     llp->land.lnd_y = def->y;
183                     llp->land.lnd_ship = -1;
184                     putland(llp->land.lnd_uid, &llp->land);
185                 } else {
186                     pr("%s was spotted", prland(&llp->land));
187                     if (rel == HOSTILE || rel == AT_WAR || rel == SITZKRIEG ||
188                         rel == MOBILIZATION) {
189                         wu(0, def->own, "%s spy shot and killed in %s.\n",
190                          cname(player->cnum), xyas(def->x, def->y, def->own));
191                         pr(" and was killed in the attempt.\n");
192                         llp->land.lnd_effic = 0;
193                         putland(llp->land.lnd_uid, &llp->land);
194                         lnd_delete(llp, "");
195                     } else {
196                         wu(0, def->own, "%s spy spotted in %s.\n",
197                          cname(player->cnum), xyas(def->x, def->y, def->own));
198                         pr(" but made it ok.\n");
199                         llp->land.lnd_x = def->x;
200                         llp->land.lnd_y = def->y;
201                         llp->land.lnd_ship = -1;
202                         putland(llp->land.lnd_uid, &llp->land);
203                     }
204                 }
205             }
206             return RET_OK;
207         }
208
209         /* Get the real defense */
210
211         att_get_defense(&olist, def, &dlist, a_spy, ototal);
212
213         /* Get assaulter and defender support */
214
215         att_get_support(A_ASSAULT, fort_sup, ship_sup, land_sup, plane_sup,
216                         &olist, off, &dlist, def, &osupport, &dsupport,
217                         a_engineer);
218         if (att_abort(A_ASSAULT, off, def)) {
219                 pr("Assault aborted\n");
220                 att_empty_attack(A_ASSAULT, 0, def);
221                 return att_free_lists(&olist, &dlist);
222         }
223
224         /*
225          * Death, carnage, and destruction.
226          */
227
228         att_fight(A_ASSAULT,off,&olist,osupport,def,&dlist,dsupport);
229
230         return RET_OK;
231 }