2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, 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
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 if (!(p = getstarg(player->argp[1],
71 "Tend what commodity (or 'land')? ", buf)) || !*p)
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,
84 getstarg(player->argp[2], "Tender(s)? ", buf)))
87 while (nxtitem(&tenders, &tender)) {
90 if (type == EF_LAND) {
91 sprintf(prompt, "Land unit(s) to tend from %s? ",
93 p = getstarg(player->argp[3], prompt, buf);
98 if (!check_ship_ok(&tender))
100 if (0 != (retval = tend_land(&tender, p)))
104 sprintf(prompt, "Number of %s to tend from %s? ",
105 ip->i_name, prship(&tender));
106 p = getstarg(player->argp[3], prompt, buf);
111 if (!check_ship_ok(&tender))
113 if (!(amt = atoi(p))) {
114 pr("Amount must be non-zero!\n");
117 ontender = tender.shp_item[ip->i_uid];
118 if (ontender == 0 && amt > 0) {
119 pr("No %s on %s\n", ip->i_name, prship(&tender));
122 vbase = &mchr[(int)tender.shp_type];
123 maxtender = vbase->m_item[ip->i_uid];
124 if (maxtender == 0) {
125 pr("A %s cannot hold any %s\n",
126 mchr[(int)tender.shp_type].m_name, ip->i_name);
129 if (!snxtitem(&targets, EF_SHIP,
130 getstarg(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;
213 struct nstr_item pni;
216 if (!snxtitem(&lni, EF_LAND, units))
219 while (nxtitem(&lni, &land)) {
222 if (land.lnd_ship != tenderp->shp_uid) {
223 pr("%s is not on %s!\n", prland(&land), prship(tenderp));
226 if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
227 pr("%s does not have \"assault\" capability and can't be tended\n",
231 if (!snxtitem(&targets, EF_SHIP,
232 getstarg(player->argp[4], "Ship to be tended? ",
235 if (!check_land_ok(&land))
237 while (nxtitem(&targets, &target)) {
238 if (!player->owner &&
239 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
241 if (target.shp_uid == tenderp->shp_uid)
243 if (tenderp->shp_x != target.shp_x ||
244 tenderp->shp_y != target.shp_y)
247 /* Fit unit on ship */
248 count_units(&target);
249 getship(target.shp_uid, &target);
251 if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
252 (!((mchr[(int)target.shp_type].m_flags & M_SUPPLY) &&
253 (!(mchr[(int)target.shp_type].m_flags & M_SUB))))) {
254 pr("You can only load light units onto ships,\n"
255 "unless the ship is a non-sub supply ship\n"
256 "%s not tended\n", prland(&land));
260 if ((mchr[(int)target.shp_type].m_flags & M_SUB) &&
261 (lchr[(int)land.lnd_type].l_flags & L_SPY) &&
262 !mchr[(int)target.shp_type].m_nland) {
263 if (target.shp_nland > 1) {
264 pr("%s doesn't have room for more than two spy units!\n",
268 } else if (target.shp_nland >= mchr[(int)target.shp_type].m_nland) {
269 if (mchr[(int)target.shp_type].m_nland)
270 pr("%s doesn't have room for any more land units!\n",
273 pr("%s doesn't carry land units!\n", prship(&target));
276 pr("%s transferred from %s to %s\n",
277 prland(&land), prship(tenderp), prship(&target));
278 sprintf(buf, "loaded on your %s at %s",
279 prship(&target), xyas(target.shp_x, target.shp_y,
281 gift(target.shp_own, player->cnum, &land, buf);
282 land.lnd_ship = target.shp_uid;
284 land.lnd_mission = 0;
286 putland(land.lnd_uid, &land);
287 expose_ship(tenderp, &target);
288 putship(target.shp_uid, &target);
289 count_units(tenderp);
290 putship(tenderp->shp_uid, tenderp);
291 snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
292 while (nxtitem(&pni, &plane)) {
293 if (plane.pln_flags & PLN_LAUNCHED)
295 if (plane.pln_land != land.lnd_uid)
297 sprintf(buf, "loaded on %s", prship(&target));
298 gift(target.shp_own, player->cnum, &plane, buf);
299 plane.pln_mission = 0;
300 putplane(plane.pln_uid, &plane);