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 void expose_ship(struct shpstr *s1, struct shpstr *s2);
45 static int tend_land(struct shpstr *tenderp, char *units);
50 struct nstr_item targets;
51 struct nstr_item tenders;
55 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)) {
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 if (0 != (retval = tend_land(&tender, p)))
106 sprintf(prompt, "Number of %s to tend from %s? ",
107 ip->i_name, prship(&tender));
108 p = getstarg(player->argp[3], prompt, buf);
113 if (!check_ship_ok(&tender))
115 if (!(amt = atoi(p))) {
116 pr("Amount must be non-zero!\n");
119 ontender = tender.shp_item[ip->i_uid];
120 if (ontender == 0 && 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 && relations_with(target.shp_own, player->cnum) < FRIENDLY)
140 if (target.shp_uid == tender.shp_uid)
142 if (tender.shp_x != target.shp_x ||
143 tender.shp_y != target.shp_y)
145 if (ip->i_uid == I_CIVIL && tender.shp_own != target.shp_own)
147 ontarget = target.shp_item[ip->i_uid];
148 vbase = &mchr[(int)target.shp_type];
149 maxtarget = vbase->m_item[ip->i_uid];
151 /* take from target and give to tender */
155 pr("No %s on %s\n", ip->i_name, prship(&target));
158 transfer = MIN(ontarget, -amt);
159 transfer = MIN(maxtender - ontender, transfer);
162 target.shp_item[ip->i_uid] = ontarget - transfer;
163 ontender += transfer;
166 /* give to target from tender */
167 transfer = MIN(ontender, amt);
168 transfer = MIN(transfer, maxtarget - ontarget);
171 target.shp_item[ip->i_uid] = ontarget + transfer;
172 ontender -= transfer;
174 if (transfer && target.shp_own != player->cnum) {
175 wu(0, target.shp_own, "%s tended %d %s to %s\n",
176 cname(player->cnum), total, ip->i_name,
180 expose_ship(&tender, &target);
181 putship(target.shp_uid, &target);
182 if (amt > 0 && ontender == 0) {
183 pr("%s out of %s\n", prship(&tender), ip->i_name);
187 pr("%d total %s transferred %s %s\n",
188 total, ip->i_name, (amt > 0) ? "off of" : "to",
190 tender.shp_item[ip->i_uid] = ontender;
191 tender.shp_mission = 0;
192 putship(tender.shp_uid, &tender);
198 expose_ship(struct shpstr *s1, struct shpstr *s2)
200 if (s1->shp_pstage == PLG_INFECT && s2->shp_pstage == PLG_HEALTHY)
201 s2->shp_pstage = PLG_EXPOSED;
202 if (s2->shp_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
203 s1->shp_pstage = PLG_EXPOSED;
207 tend_land(struct shpstr *tenderp, char *units)
209 struct nstr_item lni;
210 struct nstr_item targets;
211 struct shpstr target;
215 if (!snxtitem(&lni, EF_LAND, units, NULL))
218 while (nxtitem(&lni, &land)) {
219 if (!player->owner) {
220 if (lni.sel == NS_LIST)
221 pr("You don't own land unit #%d!\n", land.lnd_uid);
224 if (land.lnd_ship != tenderp->shp_uid) {
225 if (lni.sel == NS_LIST)
226 pr("%s is not on %s!\n", prland(&land), prship(tenderp));
229 if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
230 pr("%s does not have \"assault\" capability and can't be tended\n",
234 if (!snxtitem(&targets, EF_SHIP,
235 player->argp[4], "Ship to be tended? "))
237 if (!check_ship_ok(tenderp) || !check_land_ok(&land))
239 while (nxtitem(&targets, &target)) {
241 && relations_with(target.shp_own, player->cnum) < FRIENDLY)
243 if (target.shp_uid == tenderp->shp_uid)
245 if (tenderp->shp_x != target.shp_x ||
246 tenderp->shp_y != target.shp_y)
249 /* Fit unit on ship */
250 getship(target.shp_uid, &target);
252 if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
253 (!((mchr[(int)target.shp_type].m_flags & M_SUPPLY) &&
254 (!(mchr[(int)target.shp_type].m_flags & M_SUB))))) {
255 pr("You can only load light units onto ships,\n"
256 "unless the ship is a non-sub supply ship\n"
257 "%s not tended\n", prland(&land));
261 if ((mchr[(int)target.shp_type].m_flags & M_SUB) &&
262 (lchr[(int)land.lnd_type].l_flags & L_SPY) &&
263 !mchr[(int)target.shp_type].m_nland) {
264 if (shp_nland(&target) > 1) {
265 pr("%s doesn't have room for more than two spy units!\n",
269 } else if (shp_nland(&target) >= mchr[target.shp_type].m_nland) {
270 if (mchr[(int)target.shp_type].m_nland)
271 pr("%s doesn't have room for any more land units!\n",
274 pr("%s doesn't carry land units!\n", prship(&target));
277 pr("%s transferred from %s to %s\n",
278 prland(&land), prship(tenderp), prship(&target));
279 sprintf(buf, "loaded on your %s at %s",
280 prship(&target), xyas(target.shp_x, target.shp_y,
282 gift(target.shp_own, player->cnum, &land, buf);
283 land.lnd_ship = target.shp_uid;
285 putland(land.lnd_uid, &land);
286 expose_ship(tenderp, &target);
287 putship(target.shp_uid, &target);
288 putship(tenderp->shp_uid, tenderp);