2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * 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
51 static void expose_ship(struct shpstr *s1, struct shpstr *s2);
52 static int tend_land(struct shpstr *tenderp, s_char *units);
57 struct nstr_item targets;
58 struct nstr_item tenders;
62 struct mchrstr *vbase;
76 if (!(p = getstarg(player->argp[1],
77 "Tend what commodity (or 'land')? ", buf)) || !*p)
80 if (!strncmp(p, "land", 4))
82 else if (NULL != (ip = item_by_name(p)))
85 pr("Can't tend '%s'\n", p);
89 if (!snxtitem(&tenders, EF_SHIP,
90 getstarg(player->argp[2], "Tender(s)? ", buf)))
93 while (nxtitem(&tenders, &tender)) {
96 if (type == EF_LAND) {
97 sprintf(prompt, "Land unit(s) to tend from %s? ",
99 if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
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 if (!(p = getstarg(player->argp[3], prompt, buf)) || !*p)
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_vtype];
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_vtype];
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_vtype];
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_vtype];
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_vtype] = 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_vtype] = ontarget + transfer;
171 ontender -= transfer;
174 expose_ship(&tender, &target);
175 putship(target.shp_uid, &target);
176 if (amt > 0 && ontender == 0) {
177 pr("%s out of %s\n", prship(&tender), ip->i_name);
181 pr("%d total %s transferred %s %s\n",
182 total, ip->i_name, (amt > 0) ? "off of" : "to",
184 tender.shp_item[ip->i_vtype] = ontender;
185 tender.shp_mission = 0;
186 putship(tender.shp_uid, &tender);
192 expose_ship(struct shpstr *s1, struct shpstr *s2)
194 if (s1->shp_pstage == PLG_INFECT && s2->shp_pstage == PLG_HEALTHY)
195 s2->shp_pstage = PLG_EXPOSED;
196 if (s2->shp_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
197 s1->shp_pstage = PLG_EXPOSED;
201 tend_land(struct shpstr *tenderp, s_char *units)
203 struct nstr_item lni;
204 struct nstr_item targets;
205 struct shpstr target;
208 struct nstr_item pni;
211 if (!snxtitem(&lni, EF_LAND, units))
214 while (nxtitem(&lni, &land)) {
217 if (land.lnd_ship != tenderp->shp_uid) {
218 pr("%s is not on %s!\n", prland(&land), prship(tenderp));
221 if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
222 pr("%s does not have \"assault\" capability and can't be tended\n", prland(&land));
225 if (!snxtitem(&targets, EF_SHIP,
226 getstarg(player->argp[4], "Ship to be tended? ",
229 if (!check_land_ok(&land))
231 while (nxtitem(&targets, &target)) {
232 if (!player->owner &&
233 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
235 if (target.shp_uid == tenderp->shp_uid)
237 if (tenderp->shp_x != target.shp_x ||
238 tenderp->shp_y != target.shp_y)
241 /* Fit unit on ship */
242 count_units(&target);
243 getship(target.shp_uid, &target);
245 if ((mchr[(int)target.shp_type].m_flags & M_SUB) &&
246 (lchr[(int)land.lnd_type].l_flags & L_SPY) &&
247 !mchr[(int)target.shp_type].m_nland) {
248 if (target.shp_nland > 1) {
249 pr("%s doesn't have room for more than two spy units!\n",
253 } else if (target.shp_nland >= mchr[(int)target.shp_type].m_nland) {
254 if (mchr[(int)target.shp_type].m_nland)
255 pr("%s doesn't have room for any more land units!\n",
258 pr("%s doesn't carry land units!\n", prship(&target));
261 pr("%s transferred from %s to %s\n",
262 prland(&land), prship(tenderp), prship(&target));
263 sprintf(buf, "loaded on your %s at %s",
264 prship(&target), xyas(target.shp_x, target.shp_y,
266 gift(target.shp_own, player->cnum, (s_char *)&land,
268 makelost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
270 land.lnd_own = target.shp_own;
271 makenotlost(EF_LAND, land.lnd_own, land.lnd_uid, land.lnd_x,
273 land.lnd_ship = target.shp_uid;
275 land.lnd_mission = 0;
277 putland(land.lnd_uid, &land);
278 expose_ship(tenderp, &target);
279 putship(target.shp_uid, &target);
280 count_units(tenderp);
281 putship(tenderp->shp_uid, tenderp);
282 snxtitem_xy(&pni, EF_PLANE, land.lnd_x, land.lnd_y);
283 while (nxtitem(&pni, &plane)) {
284 if (plane.pln_flags & PLN_LAUNCHED)
286 if (plane.pln_land != land.lnd_uid)
288 sprintf(buf, "loaded on %s", prship(&target));
289 gift(target.shp_own, player->cnum, (s_char *)&plane,
291 makelost(EF_PLANE, plane.pln_own, plane.pln_uid,
292 plane.pln_x, plane.pln_y);
293 plane.pln_own = target.shp_own;
294 makenotlost(EF_PLANE, plane.pln_own, plane.pln_uid,
295 plane.pln_x, plane.pln_y);
296 plane.pln_mission = 0;
297 putplane(plane.pln_uid, &plane);