]> git.pond.sub.org Git - empserver/blob - src/lib/commands/boar.c
b153153a66bb5c254069686b72d3220f584c3f61
[empserver] / src / lib / commands / boar.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  *  boar.c: Board an enemy ship
28  *
29  *  Known contributors to this file:
30  *     Ken Stevens, 1995
31  */
32
33 #include <config.h>
34
35 #include "combat.h"
36 #include "commands.h"
37 #include "land.h"
38 #include "mission.h"
39 #include "path.h"
40 #include "retreat.h"
41 #include "ship.h"
42
43 int
44 boar(void)
45 {
46     struct combat off[1];       /* boarding ship or sector */
47     struct combat def[1];       /* defending ship */
48     struct emp_qelem olist;     /* boarding units */
49     struct emp_qelem dlist;     /* defending units */
50     int ototal;                 /* total boarding strength */
51     int a_engineer = 0;         /* boarder engineers are present */
52     int a_spy = 0;              /* the best boarder scout */
53     struct shpstr ship;         /* for retreating */
54     struct sctstr sect;
55     struct lndstr land;
56     struct nstr_item ni;
57     int foundland;
58     char *p;
59     char buf[1024];
60
61     att_combat_init(def, EF_SHIP);
62     /*
63      * Collect input from the boarder
64      */
65
66     /* What are we boarding? */
67     p = getstarg(player->argp[1], "Victim ship #?  ", buf);
68     if (!p || (def->shp_uid = atoi(p)) < 0)
69         return RET_SYN;
70
71     /*
72      * Ask the boarder what he wants to board with
73      */
74
75     if (!(p = getstarg(player->argp[2], "Boarding party from? ", buf)))
76         return RET_SYN;
77     if (issector(p)) {
78         att_combat_init(off, EF_SECTOR);
79         if (!sarg_xy(p, &off->x, &off->y))
80             return RET_SYN;
81         getsect(off->x, off->y, &sect);
82         if (sect.sct_own != player->cnum) {
83             pr("You don't own %s!\n", xyas(off->x, off->y, player->cnum));
84             return RET_SYN;
85         }
86         if (sect.sct_mobil <= 0) {
87             /* Look for land units with mobility */
88             snxtitem_xy(&ni, EF_LAND, off->x, off->y);
89             foundland = 0;
90             while (nxtitem(&ni, &land)) {
91                 if (land.lnd_own != player->cnum)
92                     continue;
93                 if (land.lnd_ship >= 0)
94                     continue;
95                 if (land.lnd_mobil <= 0)
96                     continue;
97                 /* Only land units with assault can board */
98                 if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT))
99                     continue;
100                 foundland = 1;
101             }
102             if (!foundland) {
103                 pr("You don't have any mobility (sector or land units) in %s!\n",
104                    xyas(off->x, off->y, player->cnum));
105                 return RET_SYN;
106             }
107         }
108     } else {
109         att_combat_init(off, EF_SHIP);
110         if ((off->shp_uid = atoi(p)) < 0)
111             return RET_SYN;
112     }
113     if (att_abort(A_BOARD, off, def)) {
114         pr("Board aborted\n");
115         return RET_OK;
116     }
117
118     /* Fire at the attacking ship */
119
120     att_approach(off, def);
121     if (att_abort(A_BOARD, off, def)) {
122         pr("Board aborted\n");
123         att_empty_attack(A_BOARD, 0, def);
124         return RET_OK;
125     }
126
127     /* Show what we're boarding, and check treaties */
128
129     if (att_show(def))
130         return RET_FAIL;
131
132     /* Ask the player what he wants to board with */
133
134     att_ask_offense(A_BOARD, off, def, &olist, &a_spy, &a_engineer);
135     if (att_abort(A_BOARD, off, def)) {
136         pr("Board aborted\n");
137         att_empty_attack(A_BOARD, 0, def);
138         return att_free_lists(&olist, NULL);
139     }
140
141     ototal = att_get_offense(A_BOARD, off, &olist, def);
142     if (att_abort(A_BOARD, off, def)) {
143         pr("Board aborted\n");
144         att_empty_attack(A_BOARD, 0, def);
145         return att_free_lists(&olist, NULL);
146     }
147
148     /*
149      * We have now got all the answers from the boarder.  From this point
150      * forward, we can assume that this battle is the _only_ thing
151      * happening in the game.
152      */
153
154     /* Get the real defense */
155
156     att_get_defense(&olist, def, &dlist, a_spy, ototal);
157
158     /*
159      * Death, carnage, and destruction.
160      */
161
162     if (!(att_fight(A_BOARD, off, &olist, 1.0, def, &dlist, 1.0))) {
163         getship(def->shp_uid, &ship);
164         if (ship.shp_rflags & RET_BOARDED) {
165             retreat_ship(&ship, 'u');
166             putship(def->shp_uid, &ship);
167         }
168     }
169
170     return RET_OK;
171 }