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