]> git.pond.sub.org Git - empserver/blob - src/lib/commands/mine.c
Remove a bunch of redundant casts.
[empserver] / src / lib / commands / mine.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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  *  mine.c: Lay mines from ships or units
29  * 
30  *  Known contributors to this file:
31  *     
32  */
33
34 #include "misc.h"
35 #include "player.h"
36 #include "ship.h"
37 #include "land.h"
38 #include "sect.h"
39 #include "nat.h"
40 #include "xy.h"
41 #include "nsc.h"
42 #include "file.h"
43 #include "commands.h"
44
45 /*
46  * format: mine <SHIPS> <NUMBER MINES>
47  */
48 int
49 mine(void)
50 {
51     struct shpstr ship;
52     struct sctstr sect;
53     struct mchrstr *mp;
54     struct nstr_item ni;
55     int mines;
56     int shells;
57     int mines_avail;
58
59     if (!snxtitem(&ni, EF_SHIP, player->argp[1]))
60         return RET_SYN;
61     mines = onearg(player->argp[2],
62                    "Drop how many mines from each ship?  ");
63     if (mines <= 0)
64         return RET_SYN;
65     while (nxtitem(&ni, &ship)) {
66         if (!player->owner)
67             continue;
68         mp = &mchr[(int)ship.shp_type];
69         if ((mp->m_flags & M_MINE) == 0)
70             continue;
71         if ((shells = ship.shp_item[I_SHELL]) == 0)
72             continue;
73         mines_avail = min(shells, mines);
74         if (getsect(ship.shp_x, ship.shp_y, &sect) == 0 ||
75             (sect.sct_type != SCT_WATER && sect.sct_type != SCT_BSPAN)) {
76             pr("You can't lay mines there!!\n");
77             continue;
78         }
79         sect.sct_mines = min(sect.sct_mines + mines_avail, MINES_MAX);
80         ship.shp_item[I_SHELL] = shells - mines_avail;
81         putsect(&sect);
82         ship.shp_mission = 0;
83         putship(ship.shp_uid, &ship);
84         pr("Laying %d mines from %s\n", mines_avail, prship(&ship));
85         if (mines_avail &&
86             map_set(player->cnum, sect.sct_x, sect.sct_y, 'X', 0))
87             writemap(player->cnum);
88     }
89     return RET_OK;
90 }
91
92 /*
93  * format: landmine <UNITS> <NUMBER MINES>
94  */
95 int
96 landmine(void)
97 {
98     struct lndstr land;
99     struct sctstr sect;
100     struct lchrstr *lp;
101     struct nstr_item ni;
102     int shells;
103     int mines_wanted;
104     int mines_laid;
105     int total_mines_laid;
106     s_char prompt[128];
107
108     if (!snxtitem(&ni, EF_LAND, player->argp[1]))
109         return RET_SYN;
110     while (nxtitem(&ni, &land)) {
111         if (!player->owner)
112             continue;
113         lp = &lchr[(int)land.lnd_type];
114         if (!(lp->l_flags & L_ENGINEER))
115             continue;
116         if (land.lnd_mobil < 1) {
117             pr("Unit %d is out of mobility\n", land.lnd_uid);
118             continue;
119         }
120         resupply_commod(&land, I_SHELL);
121         putland(land.lnd_uid, &land);
122         if (!(shells = land.lnd_item[I_SHELL]))
123             continue;
124         shells = min(shells, land.lnd_mobil);
125         if (!getsect(land.lnd_x, land.lnd_y, &sect) ||
126             sect.sct_type == SCT_WATER || sect.sct_type == SCT_BSPAN) {
127             pr("You can't lay mines there!!\n");
128             continue;
129         }
130         if (sect.sct_own == sect.sct_oldown)
131             pr("There are currently %d mines in %s\n",
132                sect.sct_mines, xyas(sect.sct_x, sect.sct_y, player->cnum));
133         sprintf(prompt, "Drop how many mines from %s?  ", prland(&land));
134         mines_wanted = onearg(player->argp[2], prompt);
135         if (!check_land_ok(&land))
136             continue;
137         if (mines_wanted <= 0)
138             continue;
139         land.lnd_mission = 0;
140         total_mines_laid = 0;
141         while (shells > 0 && total_mines_laid < mines_wanted) {
142             mines_laid = min(shells, mines_wanted - total_mines_laid);
143             land.lnd_item[I_SHELL] = shells - mines_laid;
144             land.lnd_mobil -= mines_laid;
145             putland(land.lnd_uid, &land);
146             resupply_commod(&land, I_SHELL);    /* Get more shells */
147             putland(land.lnd_uid, &land);
148             total_mines_laid += mines_laid;
149             shells = land.lnd_item[I_SHELL];
150             shells = min(shells, land.lnd_mobil);
151         }
152         getsect(sect.sct_x, sect.sct_y, &sect);
153         sect.sct_mines = min(sect.sct_mines + total_mines_laid, MINES_MAX);
154         putsect(&sect);
155         if (total_mines_laid == mines_wanted) {
156             pr("%s laid a total of %d mines in %s",
157                prland(&land), total_mines_laid,
158                xyas(sect.sct_x, sect.sct_y, land.lnd_own));
159             if (!shells)
160                 pr(" but is now out of supply\n");
161             else
162                 pr("\n");
163         } else
164             pr("%s ran out of %s before it could finish the job\nOnly %d mines were laid in %s\n", prland(&land), land.lnd_mobil > 0 ? "supply" : "mobility", total_mines_laid, xyas(sect.sct_x, sect.sct_y, land.lnd_own));
165     }
166     return RET_OK;
167 }