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 *, struct shpstr *);
45 static void expose_ship(struct shpstr *s1, struct shpstr *s2);
46 static int tend_land(struct shpstr *tenderp, 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 if (0 != (retval = tend_land(&tender, 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 ontender = tender.shp_item[ip->i_uid];
121 if (ontender == 0 && amt > 0) {
122 pr("No %s on %s\n", ip->i_name, prship(&tender));
125 vbase = &mchr[(int)tender.shp_type];
126 maxtender = vbase->m_item[ip->i_uid];
127 if (maxtender == 0) {
128 pr("%s cannot hold any %s\n", prship(&tender), ip->i_name);
131 if (!snxtitem(&targets, EF_SHIP,
132 player->argp[4], "Ships to be tended? "))
134 if (!check_ship_ok(&tender))
137 while (nxtitem(&targets, &target)) {
138 if (ip->i_uid == I_CIVIL && tender.shp_own != target.shp_own)
141 /* take from target and give to tender */
144 if (!can_tend_to(&target, &tender))
146 ontarget = target.shp_item[ip->i_uid];
148 pr("No %s on %s\n", ip->i_name, prship(&target));
151 transfer = MIN(ontarget, -amt);
152 transfer = MIN(maxtender - ontender, transfer);
155 target.shp_item[ip->i_uid] = ontarget - transfer;
156 ontender += transfer;
159 /* give to target from tender */
160 if (!can_tend_to(&tender, &target))
162 ontarget = target.shp_item[ip->i_uid];
163 vbase = &mchr[(int)target.shp_type];
164 maxtarget = vbase->m_item[ip->i_uid];
165 transfer = MIN(ontender, amt);
166 transfer = MIN(transfer, maxtarget - ontarget);
169 target.shp_item[ip->i_uid] = ontarget + transfer;
170 ontender -= transfer;
172 if (transfer && target.shp_own != player->cnum) {
173 wu(0, target.shp_own, "%s tended %d %s to %s\n",
174 cname(player->cnum), transfer, ip->i_name,
178 expose_ship(&tender, &target);
179 putship(target.shp_uid, &target);
180 if (amt > 0 && ontender == 0) {
181 pr("%s out of %s\n", prship(&tender), ip->i_name);
185 pr("%d total %s transferred %s %s\n",
186 total, ip->i_name, (amt > 0) ? "off of" : "to",
188 tender.shp_item[ip->i_uid] = ontender;
189 tender.shp_mission = 0;
190 putship(tender.shp_uid, &tender);
196 can_tend_to(struct shpstr *from, struct shpstr *to)
200 if (to->shp_own != player->cnum && !player->god
201 && relations_with(to->shp_own, player->cnum) < FRIENDLY)
203 if (from->shp_uid == to->shp_uid)
205 if (from->shp_x != to->shp_x || from->shp_y != to->shp_y)
211 expose_ship(struct shpstr *s1, struct shpstr *s2)
213 if (s1->shp_pstage == PLG_INFECT && s2->shp_pstage == PLG_HEALTHY)
214 s2->shp_pstage = PLG_EXPOSED;
215 if (s2->shp_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
216 s1->shp_pstage = PLG_EXPOSED;
220 tend_land(struct shpstr *tenderp, char *units)
222 struct nstr_item lni;
223 struct nstr_item targets;
224 struct shpstr target;
228 if (!snxtitem(&lni, EF_LAND, units, NULL))
231 while (nxtitem(&lni, &land)) {
232 if (!player->owner || !land.lnd_own) {
233 if (lni.sel == NS_LIST)
234 pr("You don't own land unit #%d!\n", land.lnd_uid);
237 if (land.lnd_ship != tenderp->shp_uid) {
238 if (lni.sel == NS_LIST)
239 pr("%s is not on %s!\n", prland(&land), prship(tenderp));
242 if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
243 pr("%s does not have \"assault\" capability and can't be tended\n",
247 if (!snxtitem(&targets, EF_SHIP,
248 player->argp[4], "Ship to be tended? "))
250 if (!check_ship_ok(tenderp) || !check_land_ok(&land))
252 while (nxtitem(&targets, &target)) {
253 if (!can_tend_to(tenderp, &target))
256 /* Fit unit on ship */
257 getship(target.shp_uid, &target);
259 if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
260 (!((mchr[(int)target.shp_type].m_flags & M_SUPPLY) &&
261 (!(mchr[(int)target.shp_type].m_flags & M_SUB))))) {
262 pr("You can only load light units onto ships,\n"
263 "unless the ship is a non-sub supply ship\n"
264 "%s not tended\n", prland(&land));
268 if ((mchr[(int)target.shp_type].m_flags & M_SUB) &&
269 (lchr[(int)land.lnd_type].l_flags & L_SPY) &&
270 !mchr[(int)target.shp_type].m_nland) {
271 if (shp_nland(&target) > 1) {
272 pr("%s doesn't have room for more than two spy units!\n",
276 } else if (shp_nland(&target) >= mchr[target.shp_type].m_nland) {
277 if (mchr[(int)target.shp_type].m_nland)
278 pr("%s doesn't have room for any more land units!\n",
281 pr("%s doesn't carry land units!\n", prship(&target));
284 pr("%s transferred from %s to %s\n",
285 prland(&land), prship(tenderp), prship(&target));
286 sprintf(buf, "loaded on your %s at %s",
287 prship(&target), xyas(target.shp_x, target.shp_y,
289 gift(target.shp_own, player->cnum, &land, buf);
290 land.lnd_ship = target.shp_uid;
292 putland(land.lnd_uid, &land);
293 expose_ship(tenderp, &target);
294 putship(target.shp_uid, &target);
295 putship(tenderp->shp_uid, tenderp);