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 int tend_comm_to(struct shpstr *, struct ichrstr *, int,
47 static void expose_ship(struct shpstr *s1, struct shpstr *s2);
48 static int tend_land(struct shpstr *tenderp, int, char *units);
53 struct nstr_item targets;
54 struct nstr_item tenders;
58 struct mchrstr *vbase;
69 p = getstarg(player->argp[1], "Tend what commodity (or 'land')? ",
74 if (!strncmp(p, "land", 4))
76 else if (NULL != (ip = item_by_name(p)))
79 pr("Can't tend '%s'\n", p);
83 if (!snxtitem(&tenders, EF_SHIP, player->argp[2], "Tender(s)? "))
86 while (nxtitem(&tenders, &tender)) {
87 if (!player->owner || !tender.shp_own) {
88 if (tenders.sel == NS_LIST)
89 pr("You don't own ship #%d!\n", tender.shp_uid);
92 if (type == EF_LAND) {
93 sprintf(prompt, "Land unit(s) to tend from %s? ",
95 p = getstarg(player->argp[3], prompt, buf);
100 if (!check_ship_ok(&tender))
102 retval = tend_land(&tender, tenders.sel == NS_LIST, p);
107 sprintf(prompt, "Number of %s to tend from %s? ",
108 ip->i_name, prship(&tender));
109 p = getstarg(player->argp[3], prompt, buf);
114 if (!check_ship_ok(&tender))
116 if (!(amt = atoi(p))) {
117 pr("Amount must be non-zero!\n");
120 if (!tender.shp_item[ip->i_uid] && amt > 0) {
121 pr("No %s on %s\n", ip->i_name, prship(&tender));
124 vbase = &mchr[(int)tender.shp_type];
125 maxtender = vbase->m_item[ip->i_uid];
126 if (maxtender == 0) {
127 pr("%s cannot hold any %s\n", prship(&tender), ip->i_name);
130 if (!snxtitem(&targets, EF_SHIP,
131 player->argp[4], "Ships to be tended? "))
133 if (!check_ship_ok(&tender))
136 while (nxtitem(&targets, &target)) {
138 if (!can_tend_to(&tender, tenders.sel == NS_LIST,
139 &target, targets.sel == NS_LIST))
141 transfer = tend_comm_to(&tender, ip, amt, &target);
143 if (!player->owner) {
144 if (targets.sel == NS_LIST)
145 pr("You don't own ship #%d!\n", target.shp_uid);
148 if (!can_tend_to(&target, targets.sel == NS_LIST,
149 &tender, tenders.sel == NS_LIST))
151 transfer = tend_comm_to(&target, ip, -amt, &tender);
156 expose_ship(&tender, &target);
157 putship(target.shp_uid, &target);
158 if (amt > 0 && !tender.shp_item[ip->i_uid]) {
159 pr("%s out of %s\n", prship(&tender), ip->i_name);
163 pr("%d total %s transferred %s %s\n",
164 total, ip->i_name, (amt > 0) ? "off of" : "to",
166 tender.shp_mission = 0;
167 putship(tender.shp_uid, &tender);
173 can_tend_to(struct shpstr *from, int noisy_from,
174 struct shpstr *to, int noisy_to)
177 * Careful: error messages must not disclose anything on foreign
178 * @to the player doesn't already know, or could trivially learn.
182 pr("You don't own ship #%d!\n", to->shp_uid);
185 if (from->shp_uid == to->shp_uid) {
186 if (noisy_from && noisy_to)
187 pr("%s won't tend to itself\n", prship(from));
190 if (from->shp_x != to->shp_x || from->shp_y != to->shp_y) {
191 if (noisy_from && noisy_to) {
192 /* Don't disclose foreign @to exists elsewhere */
193 if (player->god || to->shp_own == player->cnum)
194 pr("%s is not in the same sector as %s\n",
195 prship(to), prship(from));
197 pr("You don't own ship #%d!\n", to->shp_uid);
201 if (!player->god && to->shp_own != player->cnum
202 && relations_with(to->shp_own, player->cnum) < FRIENDLY) {
205 * Don't disclose unfriendly @to exists here unless
206 * lookout from @from would see it.
208 if ((mchr[from->shp_type].m_flags & M_SUB)
209 || (mchr[to->shp_type].m_flags & M_SUB))
210 pr("You don't own ship #%d!\n", from->shp_uid);
212 pr("You are not on friendly terms with"
213 " the owner of ship #%d!\n",
222 tend_comm_to(struct shpstr *from, struct ichrstr *ip, int amt,
225 int can_give = from->shp_item[ip->i_uid];
226 int to_max = mchr[to->shp_type].m_item[ip->i_uid];
227 int can_take = to_max - to->shp_item[ip->i_uid];
230 if (ip->i_uid == I_CIVIL && from->shp_own != to->shp_own)
233 pr("No %s on %s\n", ip->i_name, prship(from));
237 transfer = MIN(can_give, amt);
238 transfer = MIN(can_take, transfer);
241 from->shp_item[ip->i_uid] -= transfer;
242 to->shp_item[ip->i_uid] += transfer;
243 if (to->shp_own != player->cnum) {
244 wu(0, to->shp_own, "%s tended %d %s to %s\n",
245 cname(player->cnum), transfer, ip->i_name, prship(to));
252 expose_ship(struct shpstr *s1, struct shpstr *s2)
254 if (s1->shp_pstage == PLG_INFECT && s2->shp_pstage == PLG_HEALTHY)
255 s2->shp_pstage = PLG_EXPOSED;
256 if (s2->shp_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
257 s1->shp_pstage = PLG_EXPOSED;
261 tend_land(struct shpstr *tenderp, int noisy, char *units)
263 struct nstr_item lni;
264 struct nstr_item targets;
265 struct shpstr target;
269 if (!snxtitem(&lni, EF_LAND, units, NULL))
272 while (nxtitem(&lni, &land)) {
273 if (!player->owner || !land.lnd_own) {
274 if (lni.sel == NS_LIST)
275 pr("You don't own land unit #%d!\n", land.lnd_uid);
278 if (land.lnd_ship != tenderp->shp_uid) {
279 if (lni.sel == NS_LIST)
280 pr("%s is not on %s!\n", prland(&land), prship(tenderp));
283 if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
284 pr("%s does not have \"assault\" capability and can't be tended\n",
288 if (!snxtitem(&targets, EF_SHIP,
289 player->argp[4], "Ship to be tended? "))
291 if (!check_ship_ok(tenderp) || !check_land_ok(&land))
293 while (nxtitem(&targets, &target)) {
294 if (!can_tend_to(tenderp, noisy,
295 &target, targets.sel == NS_LIST))
298 /* Fit unit on ship */
299 getship(target.shp_uid, &target);
301 if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
302 (!((mchr[(int)target.shp_type].m_flags & M_SUPPLY) &&
303 (!(mchr[(int)target.shp_type].m_flags & M_SUB))))) {
304 pr("You can only load light units onto ships,\n"
305 "unless the ship is a non-sub supply ship\n"
306 "%s not tended\n", prland(&land));
310 if ((mchr[(int)target.shp_type].m_flags & M_SUB) &&
311 (lchr[(int)land.lnd_type].l_flags & L_SPY) &&
312 !mchr[(int)target.shp_type].m_nland) {
313 if (shp_nland(&target) > 1) {
314 pr("%s doesn't have room for more than two spy units!\n",
318 } else if (shp_nland(&target) >= mchr[target.shp_type].m_nland) {
319 if (mchr[(int)target.shp_type].m_nland)
320 pr("%s doesn't have room for any more land units!\n",
323 pr("%s doesn't carry land units!\n", prship(&target));
326 pr("%s transferred from %s to %s\n",
327 prland(&land), prship(tenderp), prship(&target));
328 sprintf(buf, "loaded on your %s at %s",
329 prship(&target), xyas(target.shp_x, target.shp_y,
331 gift(target.shp_own, player->cnum, &land, buf);
332 land.lnd_ship = target.shp_uid;
334 putland(land.lnd_uid, &land);
335 expose_ship(tenderp, &target);
336 putship(target.shp_uid, &target);
337 putship(tenderp->shp_uid, tenderp);