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