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