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