2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2021, 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 * load.c: load/unload goods from a sector onto a ship or land unit
29 * Known contributors to this file:
30 * David Sharnoff, 1987
31 * Ken Stevens, 1995 (rewritten)
32 * Steve McClure, 1998-2000
33 * Markus Armbruster, 2004-2021
48 static int load_plane_ship(struct sctstr *sectp, struct shpstr *sp,
49 int noisy, int loading, int *nshipsp);
50 static int load_land_ship(struct sctstr *sectp, struct shpstr *sp,
51 int noisy, int loading, int *nshipsp);
52 static int load_comm_ship(struct sctstr *sectp, struct shpstr *sp,
53 struct ichrstr *ich, int loading,
55 static int load_plane_land(struct sctstr *sectp, struct lndstr *lp,
56 int noisy, int loading, int *nunitsp);
57 static int load_land_land(struct sctstr *sectp, struct lndstr *lp,
58 int noisy, int loading, int *nunitsp);
59 static int load_comm_land(struct sctstr *sectp, struct lndstr *lp,
60 struct ichrstr *ich, int loading,
66 int loading = **player->argp == 'l';
69 struct nstr_item nbst;
78 p = getstarg(player->argp[1],
79 "What commodity (or 'plane' or 'land')? ", buf);
82 ich = item_by_name(p);
83 if (!strncmp(p, "plane", 5))
85 else if (!strncmp(p, "land", 4))
90 pr("Can't %sload '%s'\n", loading ? "" : "un", p);
94 p = getstarg(player->argp[2], "Ship(s): ", buf);
98 if (!snxtitem(&nbst, EF_SHIP, p, NULL))
101 noisy = nbst.sel == NS_LIST;
104 while (nxtitem(&nbst, &ship)) {
107 if (!player->owner) {
108 if (!loading || !noisy)
110 if (relations_with(ship.shp_own, player->cnum) < FRIENDLY)
114 if (!getsect(ship.shp_x, ship.shp_y, §)) /* XXX */
116 if (!player->owner) {
117 if (ship.shp_own != player->cnum)
119 if (!sect_has_dock(§))
123 pr("You don't own %s\n",
124 xyas(sect.sct_x, sect.sct_y, player->cnum));
128 if (!sect_has_dock(§)) {
130 pr("Sector %s is not a harbor or canal.\n",
131 xyas(sect.sct_x, sect.sct_y, player->cnum));
136 && relations_with(sect.sct_own, player->cnum) < FRIENDLY) {
138 pr("You can't unload into an unfriendly %s\n",
139 dchr[sect.sct_type].d_name);
142 if (sect.sct_effic < 2) {
144 pr("The %s at %s is not 2%% efficient yet.\n",
145 dchr[sect.sct_type].d_name,
146 xyas(sect.sct_x, sect.sct_y, player->cnum));
151 if (ontradingblock(EF_SHIP, &ship)) {
153 pr("%s is on the trading block\n", prship(&ship));
160 retval = load_plane_ship(§, &ship, noisy, loading, &nships);
165 retval = load_land_ship(§, &ship, noisy, loading, &nships);
170 retval = load_comm_ship(§, &ship, ich, loading, &nships);
174 /* load/unload plague */
175 if (sect.sct_pstage == PLG_INFECT
176 && ship.shp_pstage == PLG_HEALTHY)
177 ship.shp_pstage = PLG_EXPOSED;
178 if (ship.shp_pstage == PLG_INFECT
179 && sect.sct_pstage == PLG_HEALTHY)
180 sect.sct_pstage = PLG_EXPOSED;
182 putship(ship.shp_uid, &ship);
185 pr("No ships affected\n");
187 pr("%d ship%s %sloaded\n", nships, splur(nships),
188 loading ? "" : "un");
195 int loading = player->argp[0][1] == 'l';
198 struct nstr_item nbst;
207 p = getstarg(player->argp[1],
208 "What commodity (or 'plane' or 'land')? ", buf);
211 ich = item_by_name(p);
212 if (!strncmp(p, "plane", 5))
214 else if (!strncmp(p, "land", 4))
219 pr("Can't %sload '%s'\n", loading ? "" : "un", p);
223 p = getstarg(player->argp[2], "Unit(s): ", buf);
227 if (!snxtitem(&nbst, EF_LAND, p, NULL))
230 noisy = nbst.sel == NS_LIST;
233 while (nxtitem(&nbst, &land)) {
234 if (land.lnd_own == 0)
236 if (!player->owner) {
237 if (!loading || !noisy)
239 if (relations_with(land.lnd_own, player->cnum) != ALLIED)
243 if (!getsect(land.lnd_x, land.lnd_y, §)) /* XXX */
245 if (!player->owner) {
246 if (land.lnd_own != player->cnum)
250 pr("Sector %s is not yours.\n",
251 xyas(sect.sct_x, sect.sct_y, player->cnum));
254 if (relations_with(sect.sct_own, player->cnum) != ALLIED) {
256 pr("Sector %s is not yours.\n",
257 xyas(sect.sct_x, sect.sct_y, player->cnum));
263 if (ontradingblock(EF_LAND, &land)) {
265 pr("%s is on the trading block\n", prland(&land));
272 retval = load_land_land(§, &land, noisy, loading, &nunits);
277 retval = load_plane_land(§, &land, noisy, loading, &nunits);
282 retval = load_comm_land(§, &land, ich, loading, &nunits);
286 /* load/unload plague */
287 if (sect.sct_pstage == PLG_INFECT
288 && land.lnd_pstage == PLG_HEALTHY)
289 land.lnd_pstage = PLG_EXPOSED;
290 if (land.lnd_pstage == PLG_INFECT
291 && sect.sct_pstage == PLG_HEALTHY)
292 sect.sct_pstage = PLG_EXPOSED;
295 putland(land.lnd_uid, &land);
298 pr("No units affected\n");
300 pr("%d unit%s %sloaded\n", nunits, splur(nunits),
301 loading ? "" : "un");
306 move_amount(int sect_amt, int unit_amt, int unit_max,
307 int loading, int amount)
312 move_amt = -amount - unit_amt;
314 move_amt = loading ? amount : -amount;
315 move_amt = LIMIT_TO(move_amt, -unit_amt, unit_max - unit_amt);
316 move_amt = LIMIT_TO(move_amt, sect_amt - ITEM_MAX, sect_amt);
321 load_comm_ok(struct sctstr *sectp, natid unit_own,
322 i_type item, int move_amt)
326 if (move_amt < 0 && !player->god && unit_own != player->cnum)
328 if (move_amt > 0 && !player->god && sectp->sct_own != player->cnum)
330 if (sectp->sct_oldown != unit_own && item == I_CIVIL) {
331 pr("%s civilians refuse to %s at %s!\n",
332 (move_amt < 0 ? unit_own : sectp->sct_oldown) == player->cnum
333 ? "Your" : "Foreign",
334 move_amt < 0 ? "disembark" : "board",
335 xyas(sectp->sct_x, sectp->sct_y, player->cnum));
342 gift(natid givee, natid giver, void *ptr, char *mesg)
345 wu(0, givee, "%s %s %s\n", cname(giver), unit_nameof(ptr), mesg);
346 unit_give_away(ptr, givee, 0);
350 still_ok_ship(struct sctstr *sectp, struct shpstr *shipp)
352 if (!check_sect_ok(sectp))
354 if (!check_ship_ok(shipp))
360 still_ok_land(struct sctstr *sectp, struct lndstr *landp)
362 if (!check_sect_ok(sectp))
364 if (!check_land_ok(landp))
370 plane_loadable(struct plnstr *pp, int noisy)
372 if (pp->pln_ship >= 0) {
374 pr("%s is already on ship #%d!\n",
375 prplane(pp), pp->pln_ship);
378 if (pp->pln_land >= 0) {
380 pr("%s is already on land unit #%d!\n",
381 prplane(pp), pp->pln_land);
384 if (pp->pln_harden) {
386 pr("%s has been hardened and can't be loaded\n",
390 if (pln_is_in_orbit(pp)) {
392 pr("%s is in space\n", prplane(pp));
399 land_loadable(struct lndstr *lp, int noisy)
401 if (lp->lnd_ship >= 0) {
403 pr("%s is already on ship #%d!\n",
404 prland(lp), lp->lnd_ship);
407 if (lp->lnd_land >= 0) {
409 pr("%s is already on land #%d!\n",
410 prland(lp), lp->lnd_land);
413 if (lnd_first_on_land(lp) >= 0) {
414 /* Outlawed to prevent arbitrarily deep recursion */
416 pr("%s cannot be loaded since it is carrying units\n",
420 if (lchr[lp->lnd_type].l_flags & L_HEAVY) {
422 pr("%s is too heavy to load.\n", prland(lp));
429 load_plane_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
430 int loading, int *nshipsp)
438 struct mchrstr *mcp = mchr + sp->shp_type;
440 if (mcp->m_nplanes + mcp->m_nchoppers + mcp->m_nxlight == 0) {
442 pr("%s cannot carry planes\n", prship(sp));
446 shp_nplane(sp, NULL, NULL, NULL)
447 >= mcp->m_nchoppers + mcp->m_nxlight + mcp->m_nplanes) {
448 pr("%s doesn't have room for any more planes\n", prship(sp));
451 sprintf(prompt, "Plane(s) to %s %s? ",
452 loading ? "load onto" : "unload from", prship(sp));
453 p = getstarg(player->argp[3], prompt, buf);
456 if (!snxtitem(&ni, EF_PLANE, p, NULL))
459 if (!still_ok_ship(sectp, sp))
462 noisy = ni.sel == NS_LIST;
464 while (nxtitem(&ni, &pln)) {
467 if (!(plchr[(int)pln.pln_type].pl_flags & P_L)
468 && !(plchr[(int)pln.pln_type].pl_flags & P_E)
469 && !(plchr[(int)pln.pln_type].pl_flags & P_K)
470 && !(plchr[(int)pln.pln_type].pl_flags & P_M)
473 pr("You can only load light planes, helos, xtra-light, or missiles onto ships.\n");
476 if (loading && !plane_loadable(&pln, noisy))
480 if (pln.pln_ship != sp->shp_uid)
482 } else if (sp->shp_x != pln.pln_x || sp->shp_y != pln.pln_y)
485 if (!could_be_on_ship(&pln, sp)) {
487 if (plchr[(int)pln.pln_type].pl_flags & P_K)
489 else if (plchr[(int)pln.pln_type].pl_flags & P_E)
490 p = "extra light planes";
491 else if (plchr[(int)pln.pln_type].pl_flags & P_M)
495 pr("%s cannot carry %s.\n", prship(sp), p);
499 /* Fit plane on ship */
501 if (!put_plane_on_ship(&pln, sp)) {
502 pr("Can't put plane %d on this ship!\n", pln.pln_uid);
505 sprintf(buf, "loaded on your %s at %s",
506 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
507 gift(sp->shp_own, player->cnum, &pln, buf);
508 putplane(pln.pln_uid, &pln);
511 sprintf(buf, "unloaded in your %s at %s",
512 dchr[sectp->sct_type].d_name,
513 xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
514 gift(sectp->sct_own, player->cnum, &pln, buf);
515 putplane(pln.pln_uid, &pln);
517 pr("%s %s %s at %s.\n",
519 loading ? "loaded onto" : "unloaded from",
520 prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
528 load_land_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
529 int loading, int *nshipsp)
539 if (!mchr[(int)sp->shp_type].m_nland
540 && !(mchr[sp->shp_type].m_flags & M_SUB)) {
542 pr("%s cannot carry land units!\n", prship(sp));
546 if ((mchr[(int)sp->shp_type].m_flags & M_SUB) &&
547 (mchr[(int)sp->shp_type].m_nland == 0)) {
548 if (shp_nland(sp) >= 2) {
549 pr("Non-land unit carrying subs can only carry up to two spy units.\n");
552 /* Eh, let 'em load a spy only */
555 if (!load_spy && shp_nland(sp) >= mchr[sp->shp_type].m_nland) {
556 pr("%s doesn't have room for any more land units!\n",
561 sprintf(prompt, "Land unit(s) to %s %s? ",
562 loading ? "load onto" : "unload from", prship(sp));
563 p = getstarg(player->argp[3], prompt, buf);
566 if (!snxtitem(&ni, EF_LAND, p, NULL))
569 if (!still_ok_ship(sectp, sp))
572 noisy = ni.sel == NS_LIST;
574 while (nxtitem(&ni, &land)) {
579 if (!land_loadable(&land, noisy))
581 if (load_spy && !(lchr[(int)land.lnd_type].l_flags & L_SPY)) {
583 pr("Subs can only carry spy units.\n");
588 /* Unit sanity done */
589 /* Find the right ship */
591 if (land.lnd_ship != sp->shp_uid)
593 if (land.lnd_land > -1)
595 } else if (sp->shp_x != land.lnd_x || sp->shp_y != land.lnd_y)
598 if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
599 (!((mchr[(int)sp->shp_type].m_flags & M_SUPPLY) &&
600 (!(mchr[(int)sp->shp_type].m_flags & M_SUB))))) {
602 pr("You can only load light units onto ships,\n");
603 pr("unless the ship is a non-sub supply ship\n");
604 pr("%s not loaded\n", prland(&land));
608 /* Fit unit on ship */
611 if (shp_nland(sp) >= 2) {
612 pr("Non-land unit carrying subs can only carry up to two spy units.\n");
616 if (shp_nland(sp) >= mchr[sp->shp_type].m_nland) {
617 pr("%s doesn't have room for any more land units!\n",
622 sprintf(buf, "loaded on your %s at %s",
623 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
624 gift(sp->shp_own, player->cnum, &land, buf);
625 land.lnd_ship = sp->shp_uid;
627 putland(land.lnd_uid, &land);
630 * FIXME if this supplies from the sector, the putsect in
631 * load() / lload() duplicates those supplies, causing a
634 if (!lnd_supply_all(&land))
635 pr("WARNING: %s is out of supply!\n", prland(&land));
637 if (!lnd_in_supply(&land))
638 pr("WARNING: %s is out of supply!\n", prland(&land));
641 sprintf(buf, "unloaded in your %s at %s",
642 dchr[sectp->sct_type].d_name,
643 xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
645 /* Spies are unloaded quietly, others aren't */
646 if (!(lchr[(int)land.lnd_type].l_flags & L_SPY))
647 gift(sectp->sct_own, player->cnum, &land, buf);
649 putland(land.lnd_uid, &land);
651 pr("%s %s %s at %s.\n",
653 loading ? "loaded onto" : "unloaded from",
654 prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
662 load_comm_ship(struct sctstr *sectp, struct shpstr *sp,
663 struct ichrstr *ich, int loading, int *nshipsp)
665 i_type item = ich->i_uid;
666 struct mchrstr *mcp = &mchr[(int)sp->shp_type];
667 int ship_amt, sect_amt, move_amt;
672 sprintf(prompt, "Number of %s to %s %s at %s? ",
674 loading ? "load onto" : "unload from",
675 prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
676 p = getstarg(player->argp[3], prompt, buf);
680 if (!still_ok_ship(sectp, sp))
683 ship_amt = sp->shp_item[item];
684 sect_amt = sectp->sct_item[item];
685 move_amt = move_amount(sect_amt, ship_amt, mcp->m_item[item],
687 if (!load_comm_ok(sectp, sp->shp_own, item, move_amt))
689 if (!abandon_askyn(sectp, item, move_amt, NULL))
691 if (!still_ok_ship(sectp, sp))
693 sectp->sct_item[item] = sect_amt - move_amt;
694 sp->shp_item[item] = ship_amt + move_amt;
697 pr("%d %s loaded onto %s at %s\n",
698 move_amt, ich->i_name,
699 prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
700 if (sp->shp_own != player->cnum) {
701 wu(0, sp->shp_own, "%s loaded %d %s onto %s at %s\n",
702 cname(player->cnum), move_amt, ich->i_name,
703 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
706 pr("%d %s unloaded from %s at %s\n",
707 -move_amt, ich->i_name,
708 prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
709 if (sectp->sct_own != player->cnum) {
710 wu(0, sectp->sct_own, "%s unloaded %d %s from %s at %s\n",
711 cname(player->cnum), -move_amt, ich->i_name,
712 prship(sp), xyas(sp->shp_x, sp->shp_y, sectp->sct_own));
720 load_plane_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
721 int loading, int *nunitsp)
729 struct lchrstr *lcp = lchr + lp->lnd_type;
731 if (!lcp->l_nxlight) {
733 pr("%s cannot carry extra-light planes.\n", prland(lp));
736 if (loading && lnd_nxlight(lp) >= lcp->l_nxlight) {
737 pr("%s doesn't have room for any more extra-light planes\n",
741 sprintf(prompt, "Plane(s) to %s %s? ",
742 loading ? "load onto" : "unload from", prland(lp));
743 p = getstarg(player->argp[3], prompt, buf);
746 if (!snxtitem(&ni, EF_PLANE, p, NULL))
749 if (!still_ok_land(sectp, lp))
752 noisy = ni.sel == NS_LIST;
754 while (nxtitem(&ni, &pln)) {
758 if (!(plchr[(int)pln.pln_type].pl_flags & P_E)) {
760 pr("You can only load xlight planes onto units.\n");
763 if (loading && !plane_loadable(&pln, noisy))
766 /* Plane sanity done */
767 /* Find the right unit */
769 if (pln.pln_land != lp->lnd_uid)
771 } else if (lp->lnd_x != pln.pln_x || lp->lnd_y != pln.pln_y)
774 /* Fit plane on unit */
776 if (!put_plane_on_land(&pln, lp)) {
777 pr("Can't put plane %d on this unit!\n", pln.pln_uid);
780 sprintf(buf, "loaded on %s at %s",
781 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
782 gift(lp->lnd_own, player->cnum, &pln, buf);
783 putplane(pln.pln_uid, &pln);
786 sprintf(buf, "unloaded at your sector at %s",
787 xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
788 gift(sectp->sct_own, player->cnum, &pln, buf);
789 putplane(pln.pln_uid, &pln);
791 pr("%s %s %s at %s.\n",
793 loading ? "loaded onto" : "unloaded from",
794 prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
802 load_comm_land(struct sctstr *sectp, struct lndstr *lp,
803 struct ichrstr *ich, int loading, int *nunitsp)
805 i_type item = ich->i_uid;
806 struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
807 int land_amt, sect_amt, move_amt;
812 sprintf(prompt, "Number of %s to %s %s at %s? ",
814 loading ? "load onto" : "unload from",
815 prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
816 p = getstarg(player->argp[3], prompt, buf);
820 if (!still_ok_land(sectp, lp))
823 land_amt = lp->lnd_item[item];
824 sect_amt = sectp->sct_item[item];
825 move_amt = move_amount(sect_amt, land_amt, lcp->l_item[item],
827 if (!load_comm_ok(sectp, lp->lnd_own, item, move_amt))
829 sectp->sct_item[item] = sect_amt - move_amt;
830 lp->lnd_item[item] = land_amt + move_amt;
832 /* Did we put mils onto this unit? If so, reset the fortification */
833 if (item == I_MILIT && move_amt > 0)
837 pr("%d %s loaded onto %s at %s\n",
838 move_amt, ich->i_name,
839 prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
840 if (lp->lnd_own != player->cnum) {
841 wu(0, lp->lnd_own, "%s loaded %d %s onto %s at %s\n",
842 cname(player->cnum), move_amt, ich->i_name,
843 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
846 pr("%d %s unloaded from %s at %s\n",
847 -move_amt, ich->i_name,
848 prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
849 if (sectp->sct_own != player->cnum) {
850 wu(0, sectp->sct_own, "%s unloaded %d %s from %s at %s\n",
851 cname(player->cnum), -move_amt, ich->i_name,
852 prland(lp), xyas(lp->lnd_x, lp->lnd_y, sectp->sct_own));
860 load_land_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
861 int loading, int *nunitsp)
870 if (!lchr[lp->lnd_type].l_nland) {
872 pr("%s cannot carry land units!\n", prland(lp));
875 if (loading && lnd_nland(lp) >= lchr[lp->lnd_type].l_nland) {
876 pr("%s doesn't have room for any more land units!\n",
880 sprintf(prompt, "Land unit(s) to %s %s? ",
881 loading ? "load onto" : "unload from", prland(lp));
882 p = getstarg(player->argp[3], prompt, buf);
885 if (!snxtitem(&ni, EF_LAND, p, NULL))
888 if (!still_ok_land(sectp, lp))
891 noisy = ni.sel == NS_LIST;
893 while (nxtitem(&ni, &land)) {
898 if (land.lnd_uid == lp->lnd_uid) {
900 pr("%s can't be loaded onto itself!\n", prland(&land));
903 if (!land_loadable(&land, noisy))
907 /* Unit sanity done */
908 /* Find the right ship */
910 if (land.lnd_land != lp->lnd_uid)
912 if (land.lnd_ship > -1)
914 } else if (lp->lnd_x != land.lnd_x || lp->lnd_y != land.lnd_y)
917 /* Fit unit on ship */
919 if (lnd_nland(lp) >= lchr[lp->lnd_type].l_nland) {
920 pr("%s doesn't have room for any more land units!\n",
924 sprintf(buf, "loaded on your %s at %s",
925 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
926 gift(lp->lnd_own, player->cnum, &land, buf);
927 land.lnd_land = lp->lnd_uid;
929 putland(land.lnd_uid, &land);
931 /* FIXME same issue as in load_land_ship() */
932 if (!lnd_supply_all(&land))
933 pr("WARNING: %s is out of supply!\n", prland(&land));
935 if (!lnd_in_supply(&land))
936 pr("WARNING: %s is out of supply!\n", prland(&land));
939 sprintf(buf, "unloaded in your %s at %s",
940 dchr[sectp->sct_type].d_name,
941 xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
942 gift(sectp->sct_own, player->cnum, &land, buf);
944 putland(land.lnd_uid, &land);
946 pr("%s %s %s at %s.\n",
948 loading ? "loaded onto" : "unloaded from",
949 prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));