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-2018
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);
83 if (!strncmp(p, "plane", 5))
85 else if (!strncmp(p, "land", 4))
87 else if (NULL != (ich = item_by_name(p)))
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 if (!strncmp(p, "plane", 5))
213 else if (!strncmp(p, "land", 4))
215 else if (NULL != (ich = item_by_name(p)))
218 pr("Can't %sload '%s'\n", loading ? "" : "un", p);
222 p = getstarg(player->argp[2], "Unit(s): ", buf);
226 if (!snxtitem(&nbst, EF_LAND, p, NULL))
229 noisy = nbst.sel == NS_LIST;
232 while (nxtitem(&nbst, &land)) {
233 if (land.lnd_own == 0)
235 if (!player->owner) {
236 if (!loading || !noisy)
238 if (relations_with(land.lnd_own, player->cnum) != ALLIED)
242 if (!getsect(land.lnd_x, land.lnd_y, §)) /* XXX */
244 if (!player->owner) {
245 if (land.lnd_own != player->cnum)
249 pr("Sector %s is not yours.\n",
250 xyas(sect.sct_x, sect.sct_y, player->cnum));
253 if (relations_with(sect.sct_own, player->cnum) != ALLIED) {
255 pr("Sector %s is not yours.\n",
256 xyas(sect.sct_x, sect.sct_y, player->cnum));
262 if (ontradingblock(EF_LAND, &land)) {
264 pr("%s is on the trading block\n", prland(&land));
271 retval = load_land_land(§, &land, noisy, loading, &nunits);
276 retval = load_plane_land(§, &land, noisy, loading, &nunits);
281 retval = load_comm_land(§, &land, ich, loading, &nunits);
285 /* load/unload plague */
286 if (sect.sct_pstage == PLG_INFECT
287 && land.lnd_pstage == PLG_HEALTHY)
288 land.lnd_pstage = PLG_EXPOSED;
289 if (land.lnd_pstage == PLG_INFECT
290 && sect.sct_pstage == PLG_HEALTHY)
291 sect.sct_pstage = PLG_EXPOSED;
294 putland(land.lnd_uid, &land);
297 pr("No units affected\n");
299 pr("%d unit%s %sloaded\n", nunits, splur(nunits),
300 loading ? "" : "un");
305 move_amount(int sect_amt, int unit_amt, int unit_max,
306 int loading, int amount)
311 move_amt = -amount - unit_amt;
313 move_amt = loading ? amount : -amount;
314 move_amt = LIMIT_TO(move_amt, -unit_amt, unit_max - unit_amt);
315 move_amt = LIMIT_TO(move_amt, sect_amt - ITEM_MAX, sect_amt);
320 load_comm_ok(struct sctstr *sectp, natid unit_own,
321 i_type item, int move_amt)
325 if (move_amt < 0 && !player->god && unit_own != player->cnum)
327 if (move_amt > 0 && !player->god && sectp->sct_own != player->cnum)
329 if (sectp->sct_oldown != unit_own && item == I_CIVIL) {
330 pr("%s civilians refuse to %s at %s!\n",
331 (move_amt < 0 ? unit_own : sectp->sct_oldown) == player->cnum
332 ? "Your" : "Foreign",
333 move_amt < 0 ? "disembark" : "board",
334 xyas(sectp->sct_x, sectp->sct_y, player->cnum));
341 gift(natid givee, natid giver, void *ptr, char *mesg)
344 wu(0, givee, "%s %s %s\n", cname(giver), unit_nameof(ptr), mesg);
345 unit_give_away(ptr, givee, 0);
349 still_ok_ship(struct sctstr *sectp, struct shpstr *shipp)
351 if (!check_sect_ok(sectp))
353 if (!check_ship_ok(shipp))
359 still_ok_land(struct sctstr *sectp, struct lndstr *landp)
361 if (!check_sect_ok(sectp))
363 if (!check_land_ok(landp))
369 plane_loadable(struct plnstr *pp, int noisy)
371 if (pp->pln_ship >= 0) {
373 pr("%s is already on ship #%d!\n",
374 prplane(pp), pp->pln_ship);
377 if (pp->pln_land >= 0) {
379 pr("%s is already on land unit #%d!\n",
380 prplane(pp), pp->pln_land);
383 if (pp->pln_harden) {
385 pr("%s has been hardened and can't be loaded\n",
389 if (pln_is_in_orbit(pp)) {
391 pr("%s is in space\n", prplane(pp));
398 land_loadable(struct lndstr *lp, int noisy)
400 if (lp->lnd_ship >= 0) {
402 pr("%s is already on ship #%d!\n",
403 prland(lp), lp->lnd_ship);
406 if (lp->lnd_land >= 0) {
408 pr("%s is already on land #%d!\n",
409 prland(lp), lp->lnd_land);
412 if (lnd_first_on_land(lp) >= 0) {
413 /* Outlawed to prevent arbitrarily deep recursion */
415 pr("%s cannot be loaded since it is carrying units\n",
419 if (lchr[lp->lnd_type].l_flags & L_HEAVY) {
421 pr("%s is too heavy to load.\n", prland(lp));
428 load_plane_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
429 int loading, int *nshipsp)
437 struct mchrstr *mcp = mchr + sp->shp_type;
439 if (mcp->m_nplanes + mcp->m_nchoppers + mcp->m_nxlight == 0) {
441 pr("%s cannot carry planes\n", prship(sp));
445 shp_nplane(sp, NULL, NULL, NULL)
446 >= mcp->m_nchoppers + mcp->m_nxlight + mcp->m_nplanes) {
447 pr("%s doesn't have room for any more planes\n", prship(sp));
450 sprintf(prompt, "Plane(s) to %s %s? ",
451 loading ? "load onto" : "unload from", prship(sp));
452 p = getstarg(player->argp[3], prompt, buf);
455 if (!snxtitem(&ni, EF_PLANE, p, NULL))
458 if (!still_ok_ship(sectp, sp))
461 noisy = ni.sel == NS_LIST;
463 while (nxtitem(&ni, &pln)) {
466 if (!(plchr[(int)pln.pln_type].pl_flags & P_L)
467 && !(plchr[(int)pln.pln_type].pl_flags & P_E)
468 && !(plchr[(int)pln.pln_type].pl_flags & P_K)
469 && !(plchr[(int)pln.pln_type].pl_flags & P_M)
472 pr("You can only load light planes, helos, xtra-light, or missiles onto ships.\n");
475 if (loading && !plane_loadable(&pln, noisy))
479 if (pln.pln_ship != sp->shp_uid)
481 } else if (sp->shp_x != pln.pln_x || sp->shp_y != pln.pln_y)
484 if (!could_be_on_ship(&pln, sp)) {
486 if (plchr[(int)pln.pln_type].pl_flags & P_K)
488 else if (plchr[(int)pln.pln_type].pl_flags & P_E)
489 p = "extra light planes";
490 else if (plchr[(int)pln.pln_type].pl_flags & P_M)
494 pr("%s cannot carry %s.\n", prship(sp), p);
498 /* Fit plane on ship */
500 if (!put_plane_on_ship(&pln, sp)) {
501 pr("Can't put plane %d on this ship!\n", pln.pln_uid);
504 sprintf(buf, "loaded on your %s at %s",
505 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
506 gift(sp->shp_own, player->cnum, &pln, buf);
507 putplane(pln.pln_uid, &pln);
510 sprintf(buf, "unloaded in your %s at %s",
511 dchr[sectp->sct_type].d_name,
512 xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
513 gift(sectp->sct_own, player->cnum, &pln, buf);
514 putplane(pln.pln_uid, &pln);
516 pr("%s %s %s at %s.\n",
518 loading ? "loaded onto" : "unloaded from",
519 prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
527 load_land_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
528 int loading, int *nshipsp)
538 if (!mchr[(int)sp->shp_type].m_nland
539 && !(mchr[sp->shp_type].m_flags & M_SUB)) {
541 pr("%s cannot carry land units!\n", prship(sp));
545 if ((mchr[(int)sp->shp_type].m_flags & M_SUB) &&
546 (mchr[(int)sp->shp_type].m_nland == 0)) {
547 if (shp_nland(sp) >= 2) {
548 pr("Non-land unit carrying subs can only carry up to two spy units.\n");
551 /* Eh, let 'em load a spy only */
554 if (!load_spy && shp_nland(sp) >= mchr[sp->shp_type].m_nland) {
555 pr("%s doesn't have room for any more land units!\n",
560 sprintf(prompt, "Land unit(s) to %s %s? ",
561 loading ? "load onto" : "unload from", prship(sp));
562 p = getstarg(player->argp[3], prompt, buf);
565 if (!snxtitem(&ni, EF_LAND, p, NULL))
568 if (!still_ok_ship(sectp, sp))
571 noisy = ni.sel == NS_LIST;
573 while (nxtitem(&ni, &land)) {
578 if (!land_loadable(&land, noisy))
580 if (load_spy && !(lchr[(int)land.lnd_type].l_flags & L_SPY)) {
582 pr("Subs can only carry spy units.\n");
587 /* Unit sanity done */
588 /* Find the right ship */
590 if (land.lnd_ship != sp->shp_uid)
592 if (land.lnd_land > -1)
594 } else if (sp->shp_x != land.lnd_x || sp->shp_y != land.lnd_y)
597 if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
598 (!((mchr[(int)sp->shp_type].m_flags & M_SUPPLY) &&
599 (!(mchr[(int)sp->shp_type].m_flags & M_SUB))))) {
601 pr("You can only load light units onto ships,\n");
602 pr("unless the ship is a non-sub supply ship\n");
603 pr("%s not loaded\n", prland(&land));
607 /* Fit unit on ship */
610 if (shp_nland(sp) >= 2) {
611 pr("Non-land unit carrying subs can only carry up to two spy units.\n");
615 if (shp_nland(sp) >= mchr[sp->shp_type].m_nland) {
616 pr("%s doesn't have room for any more land units!\n",
621 sprintf(buf, "loaded on your %s at %s",
622 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
623 gift(sp->shp_own, player->cnum, &land, buf);
624 land.lnd_ship = sp->shp_uid;
626 putland(land.lnd_uid, &land);
629 * FIXME if this supplies from the sector, the putsect in
630 * load() / lload() duplicates those supplies, causing a
633 if (!lnd_supply_all(&land))
634 pr("WARNING: %s is out of supply!\n", prland(&land));
636 if (!lnd_in_supply(&land))
637 pr("WARNING: %s is out of supply!\n", prland(&land));
640 sprintf(buf, "unloaded in your %s at %s",
641 dchr[sectp->sct_type].d_name,
642 xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
644 /* Spies are unloaded quietly, others aren't */
645 if (!(lchr[(int)land.lnd_type].l_flags & L_SPY))
646 gift(sectp->sct_own, player->cnum, &land, buf);
648 putland(land.lnd_uid, &land);
650 pr("%s %s %s at %s.\n",
652 loading ? "loaded onto" : "unloaded from",
653 prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
661 load_comm_ship(struct sctstr *sectp, struct shpstr *sp,
662 struct ichrstr *ich, int loading, int *nshipsp)
664 i_type item = ich->i_uid;
665 struct mchrstr *mcp = &mchr[(int)sp->shp_type];
666 int ship_amt, sect_amt, move_amt;
671 sprintf(prompt, "Number of %s to %s %s at %s? ",
673 loading ? "load onto" : "unload from",
674 prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
675 p = getstarg(player->argp[3], prompt, buf);
679 if (!still_ok_ship(sectp, sp))
682 ship_amt = sp->shp_item[item];
683 sect_amt = sectp->sct_item[item];
684 move_amt = move_amount(sect_amt, ship_amt, mcp->m_item[item],
686 if (!load_comm_ok(sectp, sp->shp_own, item, move_amt))
688 if (!abandon_askyn(sectp, item, move_amt, NULL))
690 if (!still_ok_ship(sectp, sp))
692 sectp->sct_item[item] = sect_amt - move_amt;
693 sp->shp_item[item] = ship_amt + move_amt;
696 pr("%d %s loaded onto %s at %s\n",
697 move_amt, ich->i_name,
698 prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
699 if (sp->shp_own != player->cnum) {
700 wu(0, sp->shp_own, "%s loaded %d %s onto %s at %s\n",
701 cname(player->cnum), move_amt, ich->i_name,
702 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
705 pr("%d %s unloaded from %s at %s\n",
706 -move_amt, ich->i_name,
707 prship(sp), xyas(sp->shp_x, sp->shp_y, player->cnum));
708 if (sectp->sct_own != player->cnum) {
709 wu(0, sectp->sct_own, "%s unloaded %d %s from %s at %s\n",
710 cname(player->cnum), -move_amt, ich->i_name,
711 prship(sp), xyas(sp->shp_x, sp->shp_y, sectp->sct_own));
719 load_plane_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
720 int loading, int *nunitsp)
728 struct lchrstr *lcp = lchr + lp->lnd_type;
730 if (!lcp->l_nxlight) {
732 pr("%s cannot carry extra-light planes.\n", prland(lp));
735 if (loading && lnd_nxlight(lp) >= lcp->l_nxlight) {
736 pr("%s doesn't have room for any more extra-light planes\n",
740 sprintf(prompt, "Plane(s) to %s %s? ",
741 loading ? "load onto" : "unload from", prland(lp));
742 p = getstarg(player->argp[3], prompt, buf);
745 if (!snxtitem(&ni, EF_PLANE, p, NULL))
748 if (!still_ok_land(sectp, lp))
751 noisy = ni.sel == NS_LIST;
753 while (nxtitem(&ni, &pln)) {
757 if (!(plchr[(int)pln.pln_type].pl_flags & P_E)) {
759 pr("You can only load xlight planes onto units.\n");
762 if (loading && !plane_loadable(&pln, noisy))
765 /* Plane sanity done */
766 /* Find the right unit */
768 if (pln.pln_land != lp->lnd_uid)
770 } else if (lp->lnd_x != pln.pln_x || lp->lnd_y != pln.pln_y)
773 /* Fit plane on unit */
775 if (!put_plane_on_land(&pln, lp)) {
776 pr("Can't put plane %d on this unit!\n", pln.pln_uid);
779 sprintf(buf, "loaded on %s at %s",
780 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
781 gift(lp->lnd_own, player->cnum, &pln, buf);
782 putplane(pln.pln_uid, &pln);
785 sprintf(buf, "unloaded at your sector at %s",
786 xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
787 gift(sectp->sct_own, player->cnum, &pln, buf);
788 putplane(pln.pln_uid, &pln);
790 pr("%s %s %s at %s.\n",
792 loading ? "loaded onto" : "unloaded from",
793 prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
801 load_comm_land(struct sctstr *sectp, struct lndstr *lp,
802 struct ichrstr *ich, int loading, int *nunitsp)
804 i_type item = ich->i_uid;
805 struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
806 int land_amt, sect_amt, move_amt;
811 sprintf(prompt, "Number of %s to %s %s at %s? ",
813 loading ? "load onto" : "unload from",
814 prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
815 p = getstarg(player->argp[3], prompt, buf);
819 if (!still_ok_land(sectp, lp))
822 land_amt = lp->lnd_item[item];
823 sect_amt = sectp->sct_item[item];
824 move_amt = move_amount(sect_amt, land_amt, lcp->l_item[item],
826 if (!load_comm_ok(sectp, lp->lnd_own, item, move_amt))
828 sectp->sct_item[item] = sect_amt - move_amt;
829 lp->lnd_item[item] = land_amt + move_amt;
831 /* Did we put mils onto this unit? If so, reset the fortification */
832 if (item == I_MILIT && move_amt > 0)
836 pr("%d %s loaded onto %s at %s\n",
837 move_amt, ich->i_name,
838 prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
839 if (lp->lnd_own != player->cnum) {
840 wu(0, lp->lnd_own, "%s loaded %d %s onto %s at %s\n",
841 cname(player->cnum), move_amt, ich->i_name,
842 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
845 pr("%d %s unloaded from %s at %s\n",
846 -move_amt, ich->i_name,
847 prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));
848 if (sectp->sct_own != player->cnum) {
849 wu(0, sectp->sct_own, "%s unloaded %d %s from %s at %s\n",
850 cname(player->cnum), -move_amt, ich->i_name,
851 prland(lp), xyas(lp->lnd_x, lp->lnd_y, sectp->sct_own));
859 load_land_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
860 int loading, int *nunitsp)
869 if (!lchr[lp->lnd_type].l_nland) {
871 pr("%s cannot carry land units!\n", prland(lp));
874 if (loading && lnd_nland(lp) >= lchr[lp->lnd_type].l_nland) {
875 pr("%s doesn't have room for any more land units!\n",
879 sprintf(prompt, "Land unit(s) to %s %s? ",
880 loading ? "load onto" : "unload from", prland(lp));
881 p = getstarg(player->argp[3], prompt, buf);
884 if (!snxtitem(&ni, EF_LAND, p, NULL))
887 if (!still_ok_land(sectp, lp))
890 noisy = ni.sel == NS_LIST;
892 while (nxtitem(&ni, &land)) {
897 if (land.lnd_uid == lp->lnd_uid) {
899 pr("%s can't be loaded onto itself!\n", prland(&land));
902 if (!land_loadable(&land, noisy))
906 /* Unit sanity done */
907 /* Find the right ship */
909 if (land.lnd_land != lp->lnd_uid)
911 if (land.lnd_ship > -1)
913 } else if (lp->lnd_x != land.lnd_x || lp->lnd_y != land.lnd_y)
916 /* Fit unit on ship */
918 if (lnd_nland(lp) >= lchr[lp->lnd_type].l_nland) {
919 pr("%s doesn't have room for any more land units!\n",
923 sprintf(buf, "loaded on your %s at %s",
924 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
925 gift(lp->lnd_own, player->cnum, &land, buf);
926 land.lnd_land = lp->lnd_uid;
928 putland(land.lnd_uid, &land);
930 /* FIXME same issue as in load_land_ship() */
931 if (!lnd_supply_all(&land))
932 pr("WARNING: %s is out of supply!\n", prland(&land));
934 if (!lnd_in_supply(&land))
935 pr("WARNING: %s is out of supply!\n", prland(&land));
938 sprintf(buf, "unloaded in your %s at %s",
939 dchr[sectp->sct_type].d_name,
940 xyas(sectp->sct_x, sectp->sct_y, sectp->sct_own));
941 gift(sectp->sct_own, player->cnum, &land, buf);
943 putland(land.lnd_uid, &land);
945 pr("%s %s %s at %s.\n",
947 loading ? "loaded onto" : "unloaded from",
948 prland(lp), xyas(lp->lnd_x, lp->lnd_y, player->cnum));