]> git.pond.sub.org Git - empserver/blob - src/lib/commands/tend.c
5bce717724129dbddd6a99bc12bc5c1f372948f6
[empserver] / src / lib / commands / tend.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  *  tend.c: Transfer goodies from one ship to another.
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Thomas Ruschak, 1992
32  *     Steve McClure, 2000
33  *     Markus Armbruster, 2004-2017
34  */
35
36 #include <config.h>
37
38 #include "commands.h"
39 #include "item.h"
40 #include "land.h"
41 #include "plague.h"
42 #include "ship.h"
43
44 static int can_tend_to(struct shpstr *, int, struct shpstr *, int);
45 static int tend_comm_to(struct shpstr *, struct ichrstr *, int,
46                         struct shpstr *);
47 static void expose_ship(struct shpstr *s1, struct shpstr *s2);
48 static int tend_land(struct shpstr *tenderp, int, char *units);
49
50 int
51 c_tend(void)
52 {
53     struct nstr_item targets;
54     struct nstr_item tenders;
55     struct shpstr tender;
56     struct shpstr target;
57     struct ichrstr *ip;
58     struct mchrstr *vbase;
59     int amt;
60     int retval;
61     int maxtender;
62     int transfer;
63     int total;
64     int type;
65     char *p;
66     char prompt[512];
67     char buf[1024];
68
69     p = getstarg(player->argp[1], "Tend what commodity (or 'land')? ",
70                  buf);
71     if (!p || !*p)
72         return RET_SYN;
73
74     if (!strncmp(p, "land", 4))
75         type = EF_LAND;
76     else if (NULL != (ip = item_by_name(p)))
77         type = EF_SECTOR;
78     else {
79         pr("Can't tend '%s'\n", p);
80         return RET_SYN;
81     }
82
83     if (!snxtitem(&tenders, EF_SHIP, player->argp[2], "Tender(s)? "))
84         return RET_SYN;
85
86     while (nxtitem(&tenders, &tender)) {
87         if (!player->owner || !tender.shp_own) {
88             if (tenders.sel == NS_LIST)
89                 pr("You don't own ship #%d!\n", tender.shp_uid);
90             continue;
91         }
92         if (type == EF_LAND) {
93             sprintf(prompt, "Land unit(s) to tend from %s? ",
94                     prship(&tender));
95             p = getstarg(player->argp[3], prompt, buf);
96             if (!p)
97                 return RET_FAIL;
98             if (!*p)
99                 continue;
100             if (!check_ship_ok(&tender))
101                 return RET_SYN;
102             retval = tend_land(&tender, tenders.sel == NS_LIST, p);
103             if (retval)
104                 return retval;
105             continue;
106         }
107         sprintf(prompt, "Number of %s to tend from %s? ",
108                 ip->i_name, prship(&tender));
109         p = getstarg(player->argp[3], prompt, buf);
110         if (!p)
111             return RET_FAIL;
112         if (!*p)
113             continue;
114         if (!check_ship_ok(&tender))
115             return RET_SYN;
116         if (!(amt = atoi(p))) {
117             pr("Amount must be non-zero!\n");
118             return RET_SYN;
119         }
120         if (!tender.shp_item[ip->i_uid] && amt > 0) {
121             pr("No %s on %s\n", ip->i_name, prship(&tender));
122             continue;
123         }
124         vbase = &mchr[(int)tender.shp_type];
125         maxtender = vbase->m_item[ip->i_uid];
126         if (maxtender == 0) {
127             pr("%s cannot hold any %s\n", prship(&tender), ip->i_name);
128             continue;
129         }
130         if (!snxtitem(&targets, EF_SHIP,
131                       player->argp[4], "Ships to be tended? "))
132             return RET_FAIL;
133         if (!check_ship_ok(&tender))
134             return RET_SYN;
135         total = 0;
136         while (nxtitem(&targets, &target)) {
137             if (amt > 0) {
138                 if (!can_tend_to(&tender, tenders.sel == NS_LIST,
139                                  &target, targets.sel == NS_LIST))
140                     continue;
141                 transfer = tend_comm_to(&tender, ip, amt, &target);
142             } else {
143                 if (!player->owner) {
144                     if (targets.sel == NS_LIST)
145                         pr("You don't own ship #%d!\n", target.shp_uid);
146                     continue;
147                 }
148                 if (!can_tend_to(&target, targets.sel == NS_LIST,
149                                  &tender, tenders.sel == NS_LIST))
150                     continue;
151                 transfer = tend_comm_to(&target, ip, -amt, &tender);
152             }
153             if (!transfer)
154                 continue;
155             total += transfer;
156             expose_ship(&tender, &target);
157             putship(target.shp_uid, &target);
158             if (amt > 0 && !tender.shp_item[ip->i_uid]) {
159                 pr("%s out of %s\n", prship(&tender), ip->i_name);
160                 break;
161             }
162             if (amt < 0 && tender.shp_item[ip->i_uid] == maxtender)
163                 break;
164         }
165         pr("%d total %s transferred %s %s\n",
166            total, ip->i_name, (amt > 0) ? "off of" : "to",
167            prship(&tender));
168         tender.shp_mission = 0;
169         putship(tender.shp_uid, &tender);
170     }
171     return RET_OK;
172 }
173
174 static int
175 can_tend_to(struct shpstr *from, int noisy_from,
176             struct shpstr *to, int noisy_to)
177 {
178     /*
179      * Careful: error messages must not disclose anything on foreign
180      * @to the player doesn't already know, or could trivially learn.
181      */
182     if (!to->shp_own) {
183         if (noisy_to)
184             pr("You don't own ship #%d!\n", to->shp_uid);
185         return 0;
186     }
187     if (from->shp_uid == to->shp_uid) {
188         if (noisy_from && noisy_to)
189             pr("%s won't tend to itself\n", prship(from));
190         return 0;
191     }
192     if (from->shp_x != to->shp_x || from->shp_y != to->shp_y) {
193         if (noisy_from && noisy_to) {
194             /* Don't disclose foreign @to exists elsewhere */
195             if (player->god || to->shp_own == player->cnum)
196                 pr("%s is not in the same sector as %s\n",
197                    prship(to), prship(from));
198             else
199                 pr("You don't own ship #%d!\n", to->shp_uid);
200         }
201         return 0;
202     }
203     if (!player->god && to->shp_own != player->cnum
204         && relations_with(to->shp_own, player->cnum) < FRIENDLY) {
205         if (noisy_to) {
206             /*
207              * Don't disclose unfriendly @to exists here unless
208              * lookout from @from would see it.
209              */
210             if ((mchr[from->shp_type].m_flags & M_SUB)
211                 || (mchr[to->shp_type].m_flags & M_SUB))
212                 pr("You don't own ship #%d!\n", from->shp_uid);
213             else
214                 pr("You are not on friendly terms with"
215                    " the owner of ship #%d!\n",
216                    to->shp_uid);
217         }
218         return 0;
219     }
220     return 1;
221 }
222
223 static int
224 tend_comm_to(struct shpstr *from, struct ichrstr *ip, int amt,
225              struct shpstr *to)
226 {
227     int can_give = from->shp_item[ip->i_uid];
228     int to_max = mchr[to->shp_type].m_item[ip->i_uid];
229     int can_take = to_max - to->shp_item[ip->i_uid];
230     int transfer;
231
232     if (!to_max) {
233         pr("%s cannot hold any %s\n", prship(to), ip->i_name);
234         return 0;
235     }
236     if (ip->i_uid == I_CIVIL && from->shp_own != to->shp_own) {
237         pr("%s civilians refuse to board %s!\n",
238            from->shp_own == player->cnum ? "Your" : "Foreign",
239            prship(to));
240         return 0;
241     }
242     if (!can_give) {
243         pr("No %s on %s\n", ip->i_name, prship(from));
244         return 0;
245     }
246     if (!can_take) {
247         pr("%s can't hold more %s\n", prship(to), ip->i_name);
248         return 0;
249     }
250
251     transfer = MIN(can_give, amt);
252     transfer = MIN(can_take, transfer);
253     from->shp_item[ip->i_uid] -= transfer;
254     to->shp_item[ip->i_uid] += transfer;
255     if (to->shp_own != player->cnum) {
256         wu(0, to->shp_own, "%s tended %d %s to %s\n",
257            cname(player->cnum), transfer, ip->i_name, prship(to));
258     }
259
260     return transfer;
261 }
262
263 static void
264 expose_ship(struct shpstr *s1, struct shpstr *s2)
265 {
266     if (s1->shp_pstage == PLG_INFECT && s2->shp_pstage == PLG_HEALTHY)
267         s2->shp_pstage = PLG_EXPOSED;
268     if (s2->shp_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
269         s1->shp_pstage = PLG_EXPOSED;
270 }
271
272 static int
273 tend_land(struct shpstr *tenderp, int noisy, char *units)
274 {
275     struct nstr_item lni;
276     struct nstr_item targets;
277     struct shpstr target;
278     struct lndstr land;
279     char buf[1024];
280
281     if (!snxtitem(&lni, EF_LAND, units, NULL))
282         return RET_SYN;
283
284     while (nxtitem(&lni, &land)) {
285         if (!player->owner || !land.lnd_own) {
286             if (lni.sel == NS_LIST)
287                 pr("You don't own land unit #%d!\n", land.lnd_uid);
288             continue;
289         }
290         if (land.lnd_ship != tenderp->shp_uid) {
291             if (lni.sel == NS_LIST)
292                 pr("%s is not on %s!\n", prland(&land), prship(tenderp));
293             continue;
294         }
295         if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
296             pr("%s does not have \"assault\" capability and can't be tended\n",
297                prland(&land));
298             continue;
299         }
300         if (!snxtitem(&targets, EF_SHIP,
301                       player->argp[4], "Ship to be tended? "))
302             return RET_FAIL;
303         if (!check_ship_ok(tenderp) || !check_land_ok(&land))
304             return RET_SYN;
305         while (nxtitem(&targets, &target)) {
306             if (!can_tend_to(tenderp, noisy,
307                              &target, targets.sel == NS_LIST))
308                 continue;
309
310             /* Fit unit on ship */
311             getship(target.shp_uid, &target);
312
313             if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
314                 (!((mchr[(int)target.shp_type].m_flags & M_SUPPLY) &&
315                    (!(mchr[(int)target.shp_type].m_flags & M_SUB))))) {
316                 pr("You can only load light units onto ships,\n"
317                    "unless the ship is a non-sub supply ship\n"
318                    "%s not tended\n", prland(&land));
319                 continue;
320             }
321
322             if ((mchr[(int)target.shp_type].m_flags & M_SUB) &&
323                 (lchr[(int)land.lnd_type].l_flags & L_SPY) &&
324                 !mchr[(int)target.shp_type].m_nland) {
325                 if (shp_nland(&target) > 1) {
326                     pr("%s doesn't have room for more than two spy units!\n",
327                        prship(&target));
328                     continue;
329                 }
330             } else if (shp_nland(&target) >= mchr[target.shp_type].m_nland) {
331                 if (mchr[(int)target.shp_type].m_nland)
332                     pr("%s doesn't have room for any more land units!\n",
333                        prship(&target));
334                 else
335                     pr("%s doesn't carry land units!\n", prship(&target));
336                 continue;
337             }
338             pr("%s transferred from %s to %s\n",
339                prland(&land), prship(tenderp), prship(&target));
340             sprintf(buf, "loaded on your %s at %s",
341                     prship(&target), xyas(target.shp_x, target.shp_y,
342                                           target.shp_own));
343             gift(target.shp_own, player->cnum, &land, buf);
344             land.lnd_ship = target.shp_uid;
345             land.lnd_harden = 0;
346             putland(land.lnd_uid, &land);
347             expose_ship(tenderp, &target);
348             putship(target.shp_uid, &target);
349             putship(tenderp->shp_uid, tenderp);
350             break;
351         }
352     }
353     return 0;
354 }