]> git.pond.sub.org Git - empserver/blob - src/lib/commands/tend.c
tend: Factor can_tend_to() out of tend(), tend_land()
[empserver] / src / lib / commands / tend.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2018, 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 *, struct shpstr *);
45 static void expose_ship(struct shpstr *s1, struct shpstr *s2);
46 static int tend_land(struct shpstr *tenderp, char *units);
47
48 int
49 tend(void)
50 {
51     struct nstr_item targets;
52     struct nstr_item tenders;
53     struct shpstr tender;
54     struct shpstr target;
55     struct ichrstr *ip;
56     struct mchrstr *vbase;
57     int amt;
58     int retval;
59     int ontender;
60     int ontarget;
61     int maxtender;
62     int maxtarget;
63     int transfer;
64     int total;
65     int type;
66     char *p;
67     char prompt[512];
68     char buf[1024];
69
70     p = getstarg(player->argp[1], "Tend what commodity (or 'land')? ",
71                  buf);
72     if (!p || !*p)
73         return RET_SYN;
74
75     if (!strncmp(p, "land", 4))
76         type = EF_LAND;
77     else if (NULL != (ip = item_by_name(p)))
78         type = EF_SECTOR;
79     else {
80         pr("Can't tend '%s'\n", p);
81         return RET_SYN;
82     }
83
84     if (!snxtitem(&tenders, EF_SHIP, player->argp[2], "Tender(s)? "))
85         return RET_SYN;
86
87     while (nxtitem(&tenders, &tender)) {
88         if (!player->owner) {
89             if (tenders.sel == NS_LIST)
90                 pr("You don't own ship #%d!\n", tender.shp_uid);
91             continue;
92         }
93         if (type == EF_LAND) {
94             sprintf(prompt, "Land unit(s) to tend from %s? ",
95                     prship(&tender));
96             p = getstarg(player->argp[3], prompt, buf);
97             if (!p)
98                 return RET_FAIL;
99             if (!*p)
100                 continue;
101             if (!check_ship_ok(&tender))
102                 return RET_SYN;
103             if (0 != (retval = tend_land(&tender, p)))
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         ontender = tender.shp_item[ip->i_uid];
121         if (ontender == 0 && amt > 0) {
122             pr("No %s on %s\n", ip->i_name, prship(&tender));
123             continue;
124         }
125         vbase = &mchr[(int)tender.shp_type];
126         maxtender = vbase->m_item[ip->i_uid];
127         if (maxtender == 0) {
128             pr("%s cannot hold any %s\n", prship(&tender), ip->i_name);
129             continue;
130         }
131         if (!snxtitem(&targets, EF_SHIP,
132                       player->argp[4], "Ships to be tended? "))
133             return RET_FAIL;
134         if (!check_ship_ok(&tender))
135             return RET_SYN;
136         total = 0;
137         while (nxtitem(&targets, &target)) {
138             if (ip->i_uid == I_CIVIL && tender.shp_own != target.shp_own)
139                 continue;
140             if (amt < 0) {
141                 /* take from target and give to tender */
142                 if (!player->owner)
143                     continue;
144                 if (!can_tend_to(&target, &tender))
145                     continue;
146                 ontarget = target.shp_item[ip->i_uid];
147                 if (ontarget == 0) {
148                     pr("No %s on %s\n", ip->i_name, prship(&target));
149                     continue;
150                 }
151                 transfer = MIN(ontarget, -amt);
152                 transfer = MIN(maxtender - ontender, transfer);
153                 if (transfer == 0)
154                     continue;
155                 target.shp_item[ip->i_uid] = ontarget - transfer;
156                 ontender += transfer;
157                 total += transfer;
158             } else {
159                 /* give to target from tender */
160                 if (!can_tend_to(&tender, &target))
161                     continue;
162                 ontarget = target.shp_item[ip->i_uid];
163                 vbase = &mchr[(int)target.shp_type];
164                 maxtarget = vbase->m_item[ip->i_uid];
165                 transfer = MIN(ontender, amt);
166                 transfer = MIN(transfer, maxtarget - ontarget);
167                 if (transfer == 0)
168                     continue;
169                 target.shp_item[ip->i_uid] = ontarget + transfer;
170                 ontender -= transfer;
171                 total += transfer;
172                 if (transfer && target.shp_own != player->cnum) {
173                     wu(0, target.shp_own, "%s tended %d %s to %s\n",
174                        cname(player->cnum), transfer, ip->i_name,
175                        prship(&target));
176                 }
177             }
178             expose_ship(&tender, &target);
179             putship(target.shp_uid, &target);
180             if (amt > 0 && ontender == 0) {
181                 pr("%s out of %s\n", prship(&tender), ip->i_name);
182                 break;
183             }
184         }
185         pr("%d total %s transferred %s %s\n",
186            total, ip->i_name, (amt > 0) ? "off of" : "to",
187            prship(&tender));
188         tender.shp_item[ip->i_uid] = ontender;
189         tender.shp_mission = 0;
190         putship(tender.shp_uid, &tender);
191     }
192     return RET_OK;
193 }
194
195 static int
196 can_tend_to(struct shpstr *from, struct shpstr *to)
197 {
198     if (to->shp_own != player->cnum && !player->god
199         && relations_with(to->shp_own, player->cnum) < FRIENDLY)
200         return 0;
201     if (from->shp_uid == to->shp_uid)
202         return 0;
203     if (from->shp_x != to->shp_x || from->shp_y != to->shp_y)
204         return 0;
205     return 1;
206 }
207
208 static void
209 expose_ship(struct shpstr *s1, struct shpstr *s2)
210 {
211     if (s1->shp_pstage == PLG_INFECT && s2->shp_pstage == PLG_HEALTHY)
212         s2->shp_pstage = PLG_EXPOSED;
213     if (s2->shp_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
214         s1->shp_pstage = PLG_EXPOSED;
215 }
216
217 static int
218 tend_land(struct shpstr *tenderp, char *units)
219 {
220     struct nstr_item lni;
221     struct nstr_item targets;
222     struct shpstr target;
223     struct lndstr land;
224     char buf[1024];
225
226     if (!snxtitem(&lni, EF_LAND, units, NULL))
227         return RET_SYN;
228
229     while (nxtitem(&lni, &land)) {
230         if (!player->owner) {
231             if (lni.sel == NS_LIST)
232                 pr("You don't own land unit #%d!\n", land.lnd_uid);
233             continue;
234         }
235         if (land.lnd_ship != tenderp->shp_uid) {
236             if (lni.sel == NS_LIST)
237                 pr("%s is not on %s!\n", prland(&land), prship(tenderp));
238             continue;
239         }
240         if (!(lchr[(int)land.lnd_type].l_flags & L_ASSAULT)) {
241             pr("%s does not have \"assault\" capability and can't be tended\n",
242                prland(&land));
243             continue;
244         }
245         if (!snxtitem(&targets, EF_SHIP,
246                       player->argp[4], "Ship to be tended? "))
247             return RET_FAIL;
248         if (!check_ship_ok(tenderp) || !check_land_ok(&land))
249             return RET_SYN;
250         while (nxtitem(&targets, &target)) {
251             if (!can_tend_to(tenderp, &target))
252                 continue;
253
254             /* Fit unit on ship */
255             getship(target.shp_uid, &target);
256
257             if ((!(lchr[(int)land.lnd_type].l_flags & L_LIGHT)) &&
258                 (!((mchr[(int)target.shp_type].m_flags & M_SUPPLY) &&
259                    (!(mchr[(int)target.shp_type].m_flags & M_SUB))))) {
260                 pr("You can only load light units onto ships,\n"
261                    "unless the ship is a non-sub supply ship\n"
262                    "%s not tended\n", prland(&land));
263                 continue;
264             }
265
266             if ((mchr[(int)target.shp_type].m_flags & M_SUB) &&
267                 (lchr[(int)land.lnd_type].l_flags & L_SPY) &&
268                 !mchr[(int)target.shp_type].m_nland) {
269                 if (shp_nland(&target) > 1) {
270                     pr("%s doesn't have room for more than two spy units!\n",
271                        prship(&target));
272                     continue;
273                 }
274             } else if (shp_nland(&target) >= mchr[target.shp_type].m_nland) {
275                 if (mchr[(int)target.shp_type].m_nland)
276                     pr("%s doesn't have room for any more land units!\n",
277                        prship(&target));
278                 else
279                     pr("%s doesn't carry land units!\n", prship(&target));
280                 continue;
281             }
282             pr("%s transferred from %s to %s\n",
283                prland(&land), prship(tenderp), prship(&target));
284             sprintf(buf, "loaded on your %s at %s",
285                     prship(&target), xyas(target.shp_x, target.shp_y,
286                                           target.shp_own));
287             gift(target.shp_own, player->cnum, &land, buf);
288             land.lnd_ship = target.shp_uid;
289             land.lnd_harden = 0;
290             putland(land.lnd_uid, &land);
291             expose_ship(tenderp, &target);
292             putship(target.shp_uid, &target);
293             putship(tenderp->shp_uid, tenderp);
294             break;
295         }
296     }
297     return 0;
298 }