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