2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2018, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
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.
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.
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/>.
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.
27 * tend.c: Transfer goodies from one ship to another.
29 * Known contributors to this file:
31 * Thomas Ruschak, 1992
33 * Markus Armbruster, 2004-2017
44 static int can_tend_to(struct shpstr *, int, struct shpstr *, int);
45 static void expose_ship(struct shpstr *s1, struct shpstr *s2);
46 static int tend_land(struct shpstr *tenderp, int, char *units);
51 struct nstr_item targets;
52 struct nstr_item tenders;
56 struct mchrstr *vbase;
70 p = getstarg(player->argp[1], "Tend what commodity (or 'land')? ",
75 if (!strncmp(p, "land", 4))
77 else if (NULL != (ip = item_by_name(p)))
80 pr("Can't tend '%s'\n", p);
84 if (!snxtitem(&tenders, EF_SHIP, player->argp[2], "Tender(s)? "))
87 while (nxtitem(&tenders, &tender)) {
88 if (!player->owner || !tender.shp_own) {
89 if (tenders.sel == NS_LIST)
90 pr("You don't own ship #%d!\n", tender.shp_uid);
93 if (type == EF_LAND) {
94 sprintf(prompt, "Land unit(s) to tend from %s? ",
96 p = getstarg(player->argp[3], prompt, buf);
101 if (!check_ship_ok(&tender))
103 retval = tend_land(&tender, tenders.sel == NS_LIST, p);
108 sprintf(prompt, "Number of %s to tend from %s? ",
109 ip->i_name, prship(&tender));
110 p = getstarg(player->argp[3], prompt, buf);
115 if (!check_ship_ok(&tender))
117 if (!(amt = atoi(p))) {
118 pr("Amount must be non-zero!\n");
121 ontender = tender.shp_item[ip->i_uid];
122 if (ontender == 0 && amt > 0) {
123 pr("No %s on %s\n", ip->i_name, prship(&tender));
126 vbase = &mchr[(int)tender.shp_type];
127 maxtender = vbase->m_item[ip->i_uid];
128 if (maxtender == 0) {
129 pr("%s cannot hold any %s\n", prship(&tender), ip->i_name);
132 if (!snxtitem(&targets, EF_SHIP,
133 player->argp[4], "Ships to be tended? "))
135 if (!check_ship_ok(&tender))
138 while (nxtitem(&targets, &target)) {
140 /* take from target and give to tender */
141 if (!player->owner) {
142 if (targets.sel == NS_LIST)
143 pr("You don't own ship #%d!\n", target.shp_uid);
146 if (!can_tend_to(&target, targets.sel == NS_LIST,
147 &tender, tenders.sel == NS_LIST))
149 ontarget = target.shp_item[ip->i_uid];
151 pr("No %s on %s\n", ip->i_name, prship(&target));
154 transfer = MIN(ontarget, -amt);
155 transfer = MIN(maxtender - ontender, transfer);
158 target.shp_item[ip->i_uid] = ontarget - transfer;
159 ontender += transfer;
162 /* give to target from tender */
163 if (!can_tend_to(&tender, tenders.sel == NS_LIST,
164 &target, targets.sel == NS_LIST))
166 if (ip->i_uid == I_CIVIL && tender.shp_own != target.shp_own)
168 ontarget = target.shp_item[ip->i_uid];
169 vbase = &mchr[(int)target.shp_type];
170 maxtarget = vbase->m_item[ip->i_uid];
171 transfer = MIN(ontender, amt);
172 transfer = MIN(transfer, maxtarget - ontarget);
175 target.shp_item[ip->i_uid] = ontarget + transfer;
176 ontender -= transfer;
178 if (transfer && target.shp_own != player->cnum) {
179 wu(0, target.shp_own, "%s tended %d %s to %s\n",
180 cname(player->cnum), transfer, ip->i_name,
184 expose_ship(&tender, &target);
185 putship(target.shp_uid, &target);
186 if (amt > 0 && ontender == 0) {
187 pr("%s out of %s\n", prship(&tender), ip->i_name);
191 pr("%d total %s transferred %s %s\n",
192 total, ip->i_name, (amt > 0) ? "off of" : "to",
194 tender.shp_item[ip->i_uid] = ontender;
195 tender.shp_mission = 0;
196 putship(tender.shp_uid, &tender);
202 can_tend_to(struct shpstr *from, int noisy_from,
203 struct shpstr *to, int noisy_to)
206 * Careful: error messages must not disclose anything on foreign
207 * @to the player doesn't already know, or could trivially learn.
211 pr("You don't own ship #%d!\n", to->shp_uid);
214 if (from->shp_uid == to->shp_uid) {
215 if (noisy_from && noisy_to)
216 pr("%s won't tend to itself\n", prship(from));
219 if (from->shp_x != to->shp_x || from->shp_y != to->shp_y) {
220 if (noisy_from && noisy_to) {
221 /* Don't disclose foreign @to exists elsewhere */
222 if (player->god || to->shp_own == player->cnum)
223 pr("%s is not in the same sector as %s\n",
224 prship(to), prship(from));
226 pr("You don't own ship #%d!\n", to->shp_uid);
230 if (!player->god && to->shp_own != player->cnum
231 && relations_with(to->shp_own, player->cnum) < FRIENDLY) {
234 * Don't disclose unfriendly @to exists here unless
235 * lookout from @from would see it.
237 if ((mchr[from->shp_type].m_flags & M_SUB)
238 || (mchr[to->shp_type].m_flags & M_SUB))
239 pr("You don't own ship #%d!\n", from->shp_uid);
241 pr("You are not on friendly terms with"
242 " the owner of ship #%d!\n",
251 expose_ship(struct shpstr *s1, struct shpstr *s2)
253 if (s1->shp_pstage == PLG_INFECT && s2->shp_pstage == PLG_HEALTHY)
254 s2->shp_pstage = PLG_EXPOSED;
255 if (s2->shp_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
256 s1->shp_pstage = PLG_EXPOSED;
260 tend_land(struct shpstr *tenderp, int noisy, char *units)
262 struct nstr_item lni;
263 struct nstr_item targets;
264 struct shpstr target;
268 if (!snxtitem(&lni, EF_LAND, units, NULL))
271 while (nxtitem(&lni, &land)) {
272 if (!player->owner || !land.lnd_own) {
273 if (lni.sel == NS_LIST)
274 pr("You don't own land unit #%d!\n", land.lnd_uid);
277 if (land.lnd_ship != tenderp->shp_uid) {
278 if (lni.sel == NS_LIST)
279 pr("%s is not on %s!\n", prland(&land), prship(tenderp));
282 if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
283 pr("%s does not have \"assault\" capability and can't be tended\n",
287 if (!snxtitem(&targets, EF_SHIP,
288 player->argp[4], "Ship to be tended? "))
290 if (!check_ship_ok(tenderp) || !check_land_ok(&land))
292 while (nxtitem(&targets, &target)) {
293 if (!can_tend_to(tenderp, noisy,
294 &target, targets.sel == NS_LIST))
297 /* Fit unit on ship */
298 getship(target.shp_uid, &target);
300 if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
301 (!((mchr[(int)target.shp_type].m_flags & M_SUPPLY) &&
302 (!(mchr[(int)target.shp_type].m_flags & M_SUB))))) {
303 pr("You can only load light units onto ships,\n"
304 "unless the ship is a non-sub supply ship\n"
305 "%s not tended\n", prland(&land));
309 if ((mchr[(int)target.shp_type].m_flags & M_SUB) &&
310 (lchr[(int)land.lnd_type].l_flags & L_SPY) &&
311 !mchr[(int)target.shp_type].m_nland) {
312 if (shp_nland(&target) > 1) {
313 pr("%s doesn't have room for more than two spy units!\n",
317 } else if (shp_nland(&target) >= mchr[target.shp_type].m_nland) {
318 if (mchr[(int)target.shp_type].m_nland)
319 pr("%s doesn't have room for any more land units!\n",
322 pr("%s doesn't carry land units!\n", prship(&target));
325 pr("%s transferred from %s to %s\n",
326 prland(&land), prship(tenderp), prship(&target));
327 sprintf(buf, "loaded on your %s at %s",
328 prship(&target), xyas(target.shp_x, target.shp_y,
330 gift(target.shp_own, player->cnum, &land, buf);
331 land.lnd_ship = target.shp_uid;
333 putland(land.lnd_uid, &land);
334 expose_ship(tenderp, &target);
335 putship(target.shp_uid, &target);
336 putship(tenderp->shp_uid, tenderp);