]> git.pond.sub.org Git - empserver/blob - src/lib/commands/lten.c
commands: Rename the command functions
[empserver] / src / lib / commands / lten.c
1 /*
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
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  lten.c: Transfer commodity from a ship to a land unit the ship is
28  *          carrying
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Thomas Ruschak
33  *     Ken Stevens, 1995
34  *     Steve McClure, 1998
35  *     Markus Armbruster, 2004-2017
36  */
37
38 #include <config.h>
39
40 #include "commands.h"
41 #include "item.h"
42 #include "land.h"
43 #include "plague.h"
44 #include "ship.h"
45
46 static void expose_land(struct shpstr *s1, struct lndstr *l1);
47
48 int
49 c_ltend(void)
50 {
51     struct nstr_item targets;
52     struct nstr_item tenders;
53     struct shpstr tender;
54     struct lndstr target;
55     struct ichrstr *ip;
56     struct mchrstr *vbase;
57     struct lchrstr *lbase;
58     int amt;
59     int ontender;
60     int ontarget;
61     int maxtender;
62     int maxtarget;
63     int transfer;
64     int total;
65     char *p;
66     char prompt[512];
67     char buf[1024];
68
69     if (!(ip = whatitem(player->argp[1], "Transfer what commodity? ")))
70         return RET_SYN;
71
72     if (!snxtitem(&tenders, EF_SHIP, player->argp[2], "Tender(s)? "))
73         return RET_SYN;
74     while (nxtitem(&tenders, &tender)) {
75         if (!player->owner || !tender.shp_own) {
76             if (tenders.sel == NS_LIST)
77                 pr("You don't own ship #%d!\n", tender.shp_uid);
78             continue;
79         }
80         sprintf(prompt, "Number of %s to tend from %s? ",
81                 ip->i_name, prship(&tender));
82         if (!(p = getstarg(player->argp[3], prompt, buf)))
83             return RET_FAIL;
84         if (!*p)
85             continue;
86         if (!check_ship_ok(&tender))
87             return RET_FAIL;
88         amt = atoi(p);
89         if (!amt) {
90             pr("Amount must be non-zero!\n");
91             return RET_SYN;
92         }
93         ontender = tender.shp_item[ip->i_uid];
94         if (ontender == 0 && amt > 0) {
95             pr("No %s on %s\n", ip->i_name, prship(&tender));
96             continue;
97         }
98         vbase = &mchr[(int)tender.shp_type];
99         maxtender = vbase->m_item[ip->i_uid];
100         if (maxtender == 0) {
101             pr("%s cannot hold any %s\n", prship(&tender), ip->i_name);
102             continue;
103         }
104         if (!snxtitem(&targets, EF_LAND,
105                       player->argp[4], "Units to be tended? "))
106             return RET_FAIL;
107         if (!check_ship_ok(&tender))
108             return RET_FAIL;
109         total = 0;
110         while (nxtitem(&targets, &target)) {
111             if (!player->owner || !target.lnd_own) {
112                 if (targets.sel == NS_LIST)
113                     pr("You don't own land unit #%d!\n", target.lnd_uid);
114                 continue;
115             }
116             if (target.lnd_ship != tender.shp_uid) {
117                 if (targets.sel == NS_LIST)
118                     pr("%s is not on %s!\n",
119                        prland(&target), prship(&tender));
120                 continue;
121             }
122             ontarget = target.lnd_item[ip->i_uid];
123             if (ontarget == 0 && amt < 0) {
124                 pr("No %s on %s\n", ip->i_name, prland(&target));
125                 continue;
126             }
127             lbase = &lchr[(int)target.lnd_type];
128             maxtarget = lbase->l_item[ip->i_uid];
129             if (amt < 0) {
130                 if (!player->owner)
131                     amt = 0;
132
133                 /* take from target and give to tender */
134                 transfer = MIN(ontarget, -amt);
135                 transfer = MIN(maxtender - ontender, transfer);
136                 if (transfer == 0)
137                     continue;
138                 target.lnd_item[ip->i_uid] = ontarget - transfer;
139                 ontender += transfer;
140                 total += transfer;
141             } else {
142                 /* give to target from tender */
143                 transfer = MIN(ontender, amt);
144                 transfer = MIN(transfer, maxtarget - ontarget);
145                 if (transfer == 0)
146                     continue;
147                 target.lnd_item[ip->i_uid] = ontarget + transfer;
148                 ontender -= transfer;
149                 total += transfer;
150             }
151             expose_land(&tender, &target);
152             putland(target.lnd_uid, &target);
153             if (amt > 0 && ontender == 0) {
154                 pr("%s out of %s\n", prship(&tender), ip->i_name);
155                 break;
156             }
157         }
158         pr("%d total %s transferred %s %s\n",
159            total, ip->i_name, (amt > 0) ? "off of" : "to",
160            prship(&tender));
161         tender.shp_item[ip->i_uid] = ontender;
162         tender.shp_mission = 0;
163         putship(tender.shp_uid, &tender);
164     }
165     return RET_OK;
166 }
167
168 static void
169 expose_land(struct shpstr *s1, struct lndstr *l1)
170 {
171     if (s1->shp_pstage == PLG_INFECT && l1->lnd_pstage == PLG_HEALTHY)
172         l1->lnd_pstage = PLG_EXPOSED;
173     if (l1->lnd_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
174         s1->shp_pstage = PLG_EXPOSED;
175 }