]> git.pond.sub.org Git - empserver/blob - src/lib/commands/lten.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / commands / lten.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  lten.c: Transfer commodity from a ship to a land unit the ship is
29  *          carrying
30  * 
31  *  Known contributors to this file:
32  *     Dave Pare, 1986
33  *     Thomas Ruschak
34  *     Ken Stevens, 1995
35  *     Steve McClure, 1998
36  */
37
38 #include <config.h>
39
40 #include "misc.h"
41 #include "player.h"
42 #include "plague.h"
43 #include "file.h"
44 #include "ship.h"
45 #include "item.h"
46 #include "nsc.h"
47 #include "plane.h"
48 #include "land.h"
49 #include "commands.h"
50
51 static void expose_land(struct shpstr *s1, struct lndstr *l1);
52
53 int
54 ltend(void)
55 {
56     struct nstr_item targets;
57     struct nstr_item tenders;
58     struct shpstr tender;
59     struct lndstr target;
60     struct ichrstr *ip;
61     struct mchrstr *vbase;
62     struct lchrstr *lbase;
63     int amt;
64     int ontender;
65     int ontarget;
66     int maxtender;
67     int maxtarget;
68     int transfer;
69     int total;
70     s_char *p;
71     s_char buf[1024];
72
73     if (!(ip = whatitem(player->argp[1], "Transfer what commodity? ")))
74         return RET_SYN;
75
76     if (!snxtitem(&tenders, EF_SHIP,
77                   getstarg(player->argp[2], "Tender(s)? ", buf)))
78         return RET_SYN;
79     while (nxtitem(&tenders, &tender)) {
80         if (!player->owner)
81             continue;
82         if ((p =
83              getstarg(player->argp[3], "Amount to transfer? ", buf)) == 0)
84             break;
85         if (!check_ship_ok(&tender))
86             return RET_FAIL;
87         if ((amt = atoi(p)) == 0)
88             break;
89         ontender = tender.shp_item[ip->i_vtype];
90         if (ontender == 0 && amt > 0) {
91             pr("No %s on %s\n", ip->i_name, prship(&tender));
92             return RET_FAIL;
93         }
94         vbase = &mchr[(int)tender.shp_type];
95         maxtender = vbase->m_item[ip->i_vtype];
96         if (maxtender == 0) {
97             pr("A %s cannot hold any %s\n",
98                mchr[(int)tender.shp_type].m_name, ip->i_name);
99             break;
100         }
101         if (!snxtitem(&targets, EF_LAND,
102                       getstarg(player->argp[4], "Units to be tended? ",
103                                buf)))
104             break;
105         if (!check_ship_ok(&tender))
106             return RET_FAIL;
107         total = 0;
108         while (nxtitem(&targets, &target)) {
109             if (!player->owner)
110                 continue;
111
112             if (target.lnd_ship != tender.shp_uid)
113                 continue;
114             ontarget = target.lnd_item[ip->i_vtype];
115             if (ontarget == 0 && amt < 0) {
116                 pr("No %s on %s\n",
117                    ip->i_name, prland(&target));
118                 continue;
119             }
120             lbase = &lchr[(int)target.lnd_type];
121             maxtarget = lbase->l_item[ip->i_vtype];
122             if (amt < 0) {
123                 if (!player->owner)
124                     amt = 0;
125
126                 /* take from target and give to tender */
127                 transfer = MIN(ontarget, -amt);
128                 transfer = MIN(maxtender - ontender, transfer);
129                 if (transfer == 0)
130                     continue;
131                 target.lnd_item[ip->i_vtype] = ontarget - transfer;
132                 ontender += transfer;
133                 total += transfer;
134             } else {
135                 /* give to target from tender */
136                 transfer = MIN(ontender, amt);
137                 transfer = MIN(transfer, maxtarget - ontarget);
138                 if (transfer == 0)
139                     continue;
140                 target.lnd_item[ip->i_vtype] = ontarget + transfer;
141                 ontender -= transfer;
142                 total += transfer;
143             }
144             expose_land(&tender, &target);
145             putland(target.lnd_uid, &target);
146             if (amt > 0 && ontender == 0) {
147                 pr("%s out of %s\n", prship(&tender), ip->i_name);
148                 break;
149             }
150         }
151         pr("%d total %s transferred %s %s\n",
152            total, ip->i_name, (amt > 0) ? "off of" : "to",
153            prship(&tender));
154         tender.shp_item[ip->i_vtype] = ontender;
155         tender.shp_mission = 0;
156         putship(tender.shp_uid, &tender);
157     }
158     return RET_OK;
159 }
160
161 static void
162 expose_land(struct shpstr *s1, struct lndstr *l1)
163 {
164     if (s1->shp_pstage == PLG_INFECT && l1->lnd_pstage == PLG_HEALTHY)
165         l1->lnd_pstage = PLG_EXPOSED;
166     if (l1->lnd_pstage == PLG_INFECT && s1->shp_pstage == PLG_HEALTHY)
167         s1->shp_pstage = PLG_EXPOSED;
168 }