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