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
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, player->argp[2], "Tender(s)? "))
86 while (nxtitem(&tenders, &tender)) {
89 if (type == EF_LAND) {
90 sprintf(prompt, "Land unit(s) to tend from %s? ",
92 p = getstarg(player->argp[3], prompt, buf);
97 if (!check_ship_ok(&tender))
99 if (0 != (retval = tend_land(&tender, p)))
103 sprintf(prompt, "Number of %s to tend from %s? ",
104 ip->i_name, prship(&tender));
105 p = getstarg(player->argp[3], prompt, buf);
110 if (!check_ship_ok(&tender))
112 if (!(amt = atoi(p))) {
113 pr("Amount must be non-zero!\n");
116 ontender = tender.shp_item[ip->i_uid];
117 if (ontender == 0 && amt > 0) {
118 pr("No %s on %s\n", ip->i_name, prship(&tender));
121 vbase = &mchr[(int)tender.shp_type];
122 maxtender = vbase->m_item[ip->i_uid];
123 if (maxtender == 0) {
124 pr("A %s cannot hold any %s\n",
125 mchr[(int)tender.shp_type].m_name, ip->i_name);
128 if (!snxtitem(&targets, EF_SHIP,
129 player->argp[4], "Ships to be tended? "))
131 if (!check_ship_ok(&tender))
134 while (nxtitem(&targets, &target)) {
135 if (!player->owner &&
136 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
138 if (target.shp_uid == tender.shp_uid)
140 if (tender.shp_x != target.shp_x ||
141 tender.shp_y != target.shp_y)
143 ontarget = target.shp_item[ip->i_uid];
144 if (ontarget == 0 && amt < 0) {
145 pr("No %s on %s\n", ip->i_name, prship(&target));
148 vbase = &mchr[(int)target.shp_type];
149 maxtarget = vbase->m_item[ip->i_uid];
154 /* take from target and give to tender */
155 transfer = MIN(ontarget, -amt);
156 transfer = MIN(maxtender - ontender, transfer);
159 target.shp_item[ip->i_uid] = ontarget - transfer;
160 ontender += transfer;
163 /* give to target from tender */
164 transfer = MIN(ontender, amt);
165 transfer = MIN(transfer, maxtarget - ontarget);
168 target.shp_item[ip->i_uid] = ontarget + transfer;
169 ontender -= transfer;
171 if (transfer && target.shp_own != player->cnum) {
172 wu(0, target.shp_own, "%s tended %d %s to %s\n",
173 cname(player->cnum), total, ip->i_name,
177 expose_ship(&tender, &target);
178 putship(target.shp_uid, &target);
179 if (amt > 0 && ontender == 0) {
180 pr("%s out of %s\n", prship(&tender), ip->i_name);
184 pr("%d total %s transferred %s %s\n",
185 total, ip->i_name, (amt > 0) ? "off of" : "to",
187 tender.shp_item[ip->i_uid] = ontender;
188 tender.shp_mission = 0;
189 putship(tender.shp_uid, &tender);
195 expose_ship(struct shpstr *s1, struct shpstr *s2)
197 if (s1->shp_pstage == PLG_INFECT && s2->shp_pstage == PLG_HEALTHY)
198 s2->shp_pstage = PLG_EXPOSED;
199 if (s2->shp_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
200 s1->shp_pstage = PLG_EXPOSED;
204 tend_land(struct shpstr *tenderp, char *units)
206 struct nstr_item lni;
207 struct nstr_item targets;
208 struct shpstr target;
212 if (!snxtitem(&lni, EF_LAND, units, NULL))
215 while (nxtitem(&lni, &land)) {
218 if (land.lnd_ship != tenderp->shp_uid) {
219 pr("%s is not on %s!\n", prland(&land), prship(tenderp));
222 if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
223 pr("%s does not have \"assault\" capability and can't be tended\n",
227 if (!snxtitem(&targets, EF_SHIP,
228 player->argp[4], "Ship to be tended? "))
230 if (!check_land_ok(&land))
232 while (nxtitem(&targets, &target)) {
233 if (!player->owner &&
234 (getrel(getnatp(target.shp_own), player->cnum) < FRIENDLY))
236 if (target.shp_uid == tenderp->shp_uid)
238 if (tenderp->shp_x != target.shp_x ||
239 tenderp->shp_y != target.shp_y)
242 /* Fit unit on ship */
243 getship(target.shp_uid, &target);
245 if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
246 (!((mchr[(int)target.shp_type].m_flags & M_SUPPLY) &&
247 (!(mchr[(int)target.shp_type].m_flags & M_SUB))))) {
248 pr("You can only load light units onto ships,\n"
249 "unless the ship is a non-sub supply ship\n"
250 "%s not tended\n", prland(&land));
254 if ((mchr[(int)target.shp_type].m_flags & M_SUB) &&
255 (lchr[(int)land.lnd_type].l_flags & L_SPY) &&
256 !mchr[(int)target.shp_type].m_nland) {
257 if (shp_nland(&target) > 1) {
258 pr("%s doesn't have room for more than two spy units!\n",
262 } else if (shp_nland(&target) >= mchr[target.shp_type].m_nland) {
263 if (mchr[(int)target.shp_type].m_nland)
264 pr("%s doesn't have room for any more land units!\n",
267 pr("%s doesn't carry land units!\n", prship(&target));
270 pr("%s transferred from %s to %s\n",
271 prland(&land), prship(tenderp), prship(&target));
272 sprintf(buf, "loaded on your %s at %s",
273 prship(&target), xyas(target.shp_x, target.shp_y,
275 gift(target.shp_own, player->cnum, &land, buf);
276 land.lnd_ship = target.shp_uid;
278 putland(land.lnd_uid, &land);
279 expose_ship(tenderp, &target);
280 putship(target.shp_uid, &target);
281 putship(tenderp->shp_uid, tenderp);