]> git.pond.sub.org Git - empserver/blob - src/lib/commands/lten.c
Clean up superfluous includes
[empserver] / src / lib / commands / lten.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, 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  */
36
37 #include <config.h>
38
39 #include "commands.h"
40 #include "item.h"
41 #include "land.h"
42 #include "plague.h"
43 #include "ship.h"
44
45 static void expose_land(struct shpstr *s1, struct lndstr *l1);
46
47 int
48 ltend(void)
49 {
50     struct nstr_item targets;
51     struct nstr_item tenders;
52     struct shpstr tender;
53     struct lndstr target;
54     struct ichrstr *ip;
55     struct mchrstr *vbase;
56     struct lchrstr *lbase;
57     int amt;
58     int ontender;
59     int ontarget;
60     int maxtender;
61     int maxtarget;
62     int transfer;
63     int total;
64     char *p;
65     char buf[1024];
66
67     if (!(ip = whatitem(player->argp[1], "Transfer what commodity? ")))
68         return RET_SYN;
69
70     if (!snxtitem(&tenders, EF_SHIP, player->argp[2], "Tender(s)? "))
71         return RET_SYN;
72     while (nxtitem(&tenders, &tender)) {
73         if (!player->owner)
74             continue;
75         if (!(p = getstarg(player->argp[3], "Amount to transfer? ", buf)))
76             return RET_FAIL;
77         if (!check_ship_ok(&tender))
78             return RET_FAIL;
79         if ((amt = atoi(p)) == 0)
80             break;
81         ontender = tender.shp_item[ip->i_uid];
82         if (ontender == 0 && amt > 0) {
83             pr("No %s on %s\n", ip->i_name, prship(&tender));
84             return RET_FAIL;
85         }
86         vbase = &mchr[(int)tender.shp_type];
87         maxtender = vbase->m_item[ip->i_uid];
88         if (maxtender == 0) {
89             pr("A %s cannot hold any %s\n",
90                mchr[(int)tender.shp_type].m_name, ip->i_name);
91             return RET_FAIL;
92         }
93         if (!snxtitem(&targets, EF_LAND,
94                       player->argp[4], "Units to be tended? "))
95             return RET_FAIL;
96         if (!check_ship_ok(&tender))
97             return RET_FAIL;
98         total = 0;
99         while (nxtitem(&targets, &target)) {
100             if (!player->owner)
101                 continue;
102
103             if (target.lnd_ship != tender.shp_uid)
104                 continue;
105             ontarget = target.lnd_item[ip->i_uid];
106             if (ontarget == 0 && amt < 0) {
107                 pr("No %s on %s\n", ip->i_name, prland(&target));
108                 continue;
109             }
110             lbase = &lchr[(int)target.lnd_type];
111             maxtarget = lbase->l_item[ip->i_uid];
112             if (amt < 0) {
113                 if (!player->owner)
114                     amt = 0;
115
116                 /* take from target and give to tender */
117                 transfer = MIN(ontarget, -amt);
118                 transfer = MIN(maxtender - ontender, transfer);
119                 if (transfer == 0)
120                     continue;
121                 target.lnd_item[ip->i_uid] = ontarget - transfer;
122                 ontender += transfer;
123                 total += transfer;
124             } else {
125                 /* give to target from tender */
126                 transfer = MIN(ontender, amt);
127                 transfer = MIN(transfer, maxtarget - ontarget);
128                 if (transfer == 0)
129                     continue;
130                 target.lnd_item[ip->i_uid] = ontarget + transfer;
131                 ontender -= transfer;
132                 total += transfer;
133             }
134             expose_land(&tender, &target);
135             putland(target.lnd_uid, &target);
136             if (amt > 0 && ontender == 0) {
137                 pr("%s out of %s\n", prship(&tender), ip->i_name);
138                 break;
139             }
140         }
141         pr("%d total %s transferred %s %s\n",
142            total, ip->i_name, (amt > 0) ? "off of" : "to",
143            prship(&tender));
144         tender.shp_item[ip->i_uid] = ontender;
145         tender.shp_mission = 0;
146         putship(tender.shp_uid, &tender);
147     }
148     return RET_OK;
149 }
150
151 static void
152 expose_land(struct shpstr *s1, struct lndstr *l1)
153 {
154     if (s1->shp_pstage == PLG_INFECT && l1->lnd_pstage == PLG_HEALTHY)
155         l1->lnd_pstage = PLG_EXPOSED;
156     if (l1->lnd_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
157         s1->shp_pstage = PLG_EXPOSED;
158 }