2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * tend.c: Transfer goodies from one ship to another.
30 * Known contributors to this file:
32 * Thomas Ruschak, 1992
34 * Markus Armbruster, 2004-2009
46 static void expose_ship(struct shpstr *s1, struct shpstr *s2);
47 static int tend_land(struct shpstr *tenderp, char *units);
52 struct nstr_item targets;
53 struct nstr_item tenders;
57 struct mchrstr *vbase;
71 p = getstarg(player->argp[1], "Tend what commodity (or 'land')? ",
76 if (!strncmp(p, "land", 4))
78 else if (NULL != (ip = item_by_name(p)))
81 pr("Can't tend '%s'\n", p);
85 if (!snxtitem(&tenders, EF_SHIP, player->argp[2], "Tender(s)? "))
88 while (nxtitem(&tenders, &tender)) {
91 if (type == EF_LAND) {
92 sprintf(prompt, "Land unit(s) to tend from %s? ",
94 p = getstarg(player->argp[3], prompt, buf);
99 if (!check_ship_ok(&tender))
101 if (0 != (retval = tend_land(&tender, p)))
105 sprintf(prompt, "Number of %s to tend from %s? ",
106 ip->i_name, prship(&tender));
107 p = getstarg(player->argp[3], prompt, buf);
112 if (!check_ship_ok(&tender))
114 if (!(amt = atoi(p))) {
115 pr("Amount must be non-zero!\n");
118 ontender = tender.shp_item[ip->i_uid];
119 if (ontender == 0 && amt > 0) {
120 pr("No %s on %s\n", ip->i_name, prship(&tender));
123 vbase = &mchr[(int)tender.shp_type];
124 maxtender = vbase->m_item[ip->i_uid];
125 if (maxtender == 0) {
126 pr("A %s cannot hold any %s\n",
127 mchr[(int)tender.shp_type].m_name, 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)) {
137 if (!player->owner &&
138 (getrel(getnatp(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 ontarget = target.shp_item[ip->i_uid];
146 if (ontarget == 0 && amt < 0) {
147 pr("No %s on %s\n", ip->i_name, prship(&target));
150 vbase = &mchr[(int)target.shp_type];
151 maxtarget = vbase->m_item[ip->i_uid];
156 /* take from target and give to tender */
157 transfer = MIN(ontarget, -amt);
158 transfer = MIN(maxtender - ontender, transfer);
161 target.shp_item[ip->i_uid] = ontarget - transfer;
162 ontender += transfer;
165 /* give to target from tender */
166 transfer = MIN(ontender, amt);
167 transfer = MIN(transfer, maxtarget - ontarget);
170 target.shp_item[ip->i_uid] = ontarget + transfer;
171 ontender -= transfer;
173 if (transfer && target.shp_own != player->cnum) {
174 wu(0, target.shp_own, "%s tended %d %s to %s\n",
175 cname(player->cnum), total, ip->i_name,
179 expose_ship(&tender, &target);
180 putship(target.shp_uid, &target);
181 if (amt > 0 && ontender == 0) {
182 pr("%s out of %s\n", prship(&tender), ip->i_name);
186 pr("%d total %s transferred %s %s\n",
187 total, ip->i_name, (amt > 0) ? "off of" : "to",
189 tender.shp_item[ip->i_uid] = ontender;
190 tender.shp_mission = 0;
191 putship(tender.shp_uid, &tender);
197 expose_ship(struct shpstr *s1, struct shpstr *s2)
199 if (s1->shp_pstage == PLG_INFECT && s2->shp_pstage == PLG_HEALTHY)
200 s2->shp_pstage = PLG_EXPOSED;
201 if (s2->shp_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
202 s1->shp_pstage = PLG_EXPOSED;
206 tend_land(struct shpstr *tenderp, char *units)
208 struct nstr_item lni;
209 struct nstr_item targets;
210 struct shpstr target;
214 if (!snxtitem(&lni, EF_LAND, units, NULL))
217 while (nxtitem(&lni, &land)) {
220 if (land.lnd_ship != tenderp->shp_uid) {
221 pr("%s is not on %s!\n", prland(&land), prship(tenderp));
224 if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
225 pr("%s does not have \"assault\" capability and can't be tended\n",
229 if (!snxtitem(&targets, EF_SHIP,
230 player->argp[4], "Ship to be tended? "))
232 if (!check_ship_ok(tenderp) || !check_land_ok(&land))
234 while (nxtitem(&targets, &target)) {
235 if (!player->owner &&
236 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
238 if (target.shp_uid == tenderp->shp_uid)
240 if (tenderp->shp_x != target.shp_x ||
241 tenderp->shp_y != target.shp_y)
244 /* Fit unit on ship */
245 getship(target.shp_uid, &target);
247 if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
248 (!((mchr[(int)target.shp_type].m_flags & M_SUPPLY) &&
249 (!(mchr[(int)target.shp_type].m_flags & M_SUB))))) {
250 pr("You can only load light units onto ships,\n"
251 "unless the ship is a non-sub supply ship\n"
252 "%s not tended\n", prland(&land));
256 if ((mchr[(int)target.shp_type].m_flags & M_SUB) &&
257 (lchr[(int)land.lnd_type].l_flags & L_SPY) &&
258 !mchr[(int)target.shp_type].m_nland) {
259 if (shp_nland(&target) > 1) {
260 pr("%s doesn't have room for more than two spy units!\n",
264 } else if (shp_nland(&target) >= mchr[target.shp_type].m_nland) {
265 if (mchr[(int)target.shp_type].m_nland)
266 pr("%s doesn't have room for any more land units!\n",
269 pr("%s doesn't carry land units!\n", prship(&target));
272 pr("%s transferred from %s to %s\n",
273 prland(&land), prship(tenderp), prship(&target));
274 sprintf(buf, "loaded on your %s at %s",
275 prship(&target), xyas(target.shp_x, target.shp_y,
277 gift(target.shp_own, player->cnum, &land, buf);
278 land.lnd_ship = target.shp_uid;
280 putland(land.lnd_uid, &land);
281 expose_ship(tenderp, &target);
282 putship(target.shp_uid, &target);
283 putship(tenderp->shp_uid, tenderp);