1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  mine.c: Lay mines from ships or units
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2004-2010
31  */
32 
33 #include <config.h>
34 
35 #include "commands.h"
36 #include "land.h"
37 #include "map.h"
38 #include "ship.h"
39 
40 /*
41  * format: mine <SHIPS> <NUMBER MINES>
42  */
43 int
c_mine(void)44 c_mine(void)
45 {
46     struct shpstr ship;
47     struct sctstr sect;
48     struct mchrstr *mp;
49     struct nstr_item ni;
50     int mines;
51     int shells;
52     int mines_avail;
53 
54     if (!snxtitem(&ni, EF_SHIP, player->argp[1], NULL))
55 	return RET_SYN;
56     mines = onearg(player->argp[2],
57 		   "Drop how many mines from each ship? ");
58     if (mines <= 0)
59 	return RET_SYN;
60     while (nxtitem(&ni, &ship)) {
61 	if (!player->owner)
62 	    continue;
63 	mp = &mchr[(int)ship.shp_type];
64 	if ((mp->m_flags & M_MINE) == 0)
65 	    continue;
66 	if ((shells = ship.shp_item[I_SHELL]) == 0)
67 	    continue;
68 	mines_avail = MIN(shells, mines);
69 	if (getsect(ship.shp_x, ship.shp_y, &sect) == 0 ||
70 	    sect.sct_type != SCT_WATER) {
71 	    pr("You can't lay mines there!!\n");
72 	    continue;
73 	}
74 	sect.sct_mines = MIN(sect.sct_mines + mines_avail, MINES_MAX);
75 	ship.shp_item[I_SHELL] = shells - mines_avail;
76 	putsect(&sect);
77 	ship.shp_mission = 0;
78 	putship(ship.shp_uid, &ship);
79 	pr("Laying %d mines from %s\n", mines_avail, prship(&ship));
80 	if (mines_avail &&
81 	    map_set(player->cnum, sect.sct_x, sect.sct_y, 'X', 0))
82 	    writemap(player->cnum);
83     }
84     return RET_OK;
85 }
86 
87 /*
88  * format: landmine <UNITS> <NUMBER MINES>
89  */
90 int
c_lmine(void)91 c_lmine(void)
92 {
93     struct lndstr land;
94     struct sctstr sect;
95     struct nstr_item ni;
96     int todo;
97     int mines_wanted;
98     int mines_laid;
99     int total_mines_laid;
100     char prompt[128];
101 
102     if (!snxtitem(&ni, EF_LAND, player->argp[1], NULL))
103 	return RET_SYN;
104     while (nxtitem(&ni, &land)) {
105 	if (!player->owner)
106 	    continue;
107 	if (!(lchr[land.lnd_type].l_flags & L_ENGINEER))
108 	    continue;
109 	if (land.lnd_ship >= 0 || land.lnd_land >= 0) {
110 	    pr("%s is on a %s\n", prland(&land),
111 	       land.lnd_ship >= 0 ? "ship" : "land unit");
112 	    continue;
113 	}
114 	if (land.lnd_mobil < 1) {
115 	    pr("%s is out of mobility\n", prland(&land));
116 	    continue;
117 	}
118 	if (!getsect(land.lnd_x, land.lnd_y, &sect)
119 	    || SCT_MINES_ARE_SEAMINES(&sect)
120 	    || sect.sct_own != land.lnd_own) {
121 	    pr("You can't lay mines there!!\n");
122 	    continue;
123 	}
124 	if (sect.sct_own == sect.sct_oldown)
125 	    pr("There are currently %d mines in %s\n",
126 	       sect.sct_mines, xyas(sect.sct_x, sect.sct_y, player->cnum));
127 	sprintf(prompt, "Drop how many mines from %s? ", prland(&land));
128 	mines_wanted = onearg(player->argp[2], prompt);
129 	if (mines_wanted < 0)
130 	    return RET_FAIL;
131 	if (mines_wanted == 0)
132 	    continue;
133 	if (!check_land_ok(&land) || !check_sect_ok(&sect))
134 	    continue;
135 	land.lnd_mission = 0;
136 	todo = MIN(mines_wanted, land.lnd_mobil);
137 	total_mines_laid = 0;
138 	do {
139 	    lnd_supply(&land, I_SHELL, todo);
140 	    mines_laid = MIN(todo, land.lnd_item[I_SHELL]);
141 	    land.lnd_item[I_SHELL] -= mines_laid;
142 	    land.lnd_mobil -= mines_laid;
143 	    putland(land.lnd_uid, &land);
144 	    total_mines_laid += mines_laid;
145 	    todo -= mines_laid;
146 	} while (todo && mines_laid);
147 	lnd_supply_all(&land);
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, player->cnum));
155 	    if (!land.lnd_item[I_SHELL])
156 		pr(" but is now out of shells\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 ? "shells" : "mobility",
164 	       total_mines_laid,
165 	       xyas(sect.sct_x, sect.sct_y, player->cnum));
166     }
167     return RET_OK;
168 }
169