]> git.pond.sub.org Git - empserver/blob - src/lib/commands/lten.c
Import of Empire 4.2.12
[empserver] / src / lib / commands / lten.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "misc.h"
39 #include "player.h"
40 #include "var.h"
41 #include "xy.h"
42 #include "file.h"
43 #include "ship.h"
44 #include "item.h"
45 #include "nsc.h"
46 #include "nat.h"
47 #include "plane.h"
48 #include "nuke.h"
49 #include "land.h"
50 #include "genitem.h"
51 #include "commands.h"
52
53 static void expose_land(struct shpstr *s1, struct lndstr *l1);
54
55 int
56 ltend(void)
57 {
58         struct  nstr_item       targets;
59         struct  nstr_item       tenders;
60         struct  shpstr  tender;
61         struct  lndstr  target;
62         struct  ichrstr *ip;
63         struct  mchrstr *vbase;
64         struct  lchrstr *lbase;
65         int     amt;
66         int     ontender;
67         int     ontarget;
68         int     maxtender;
69         int     maxtarget;
70         int     transfer;
71         int     total;
72         s_char  *p;
73         s_char  buf[1024];
74
75         if (!(ip = whatitem(player->argp[1], "Transfer what commodity? ")))
76                 return RET_SYN;
77         
78         if (!snxtitem(&tenders, EF_SHIP, getstarg(player->argp[2], "Tender(s)? ", buf)))
79                 return RET_SYN;
80         while (nxtitem(&tenders, (s_char *)&tender)) {
81                 if (!player->owner)
82                         continue;
83                 if ((p = 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 = getvar(ip->i_vtype, (s_char *)&tender, EF_SHIP);
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 = vl_find(ip->i_vtype, vbase->m_vtype,
96                         vbase->m_vamt, (int)vbase->m_nv);
97                 if (maxtender == 0) {
98                         pr("A %s cannot hold any %s\n",
99                                mchr[(int)tender.shp_type].m_name,
100                                ip->i_name);
101                         break;
102                 }
103                 if (!snxtitem(&targets, EF_LAND,
104                     getstarg(player->argp[4], "Units to be tended? ", buf)))
105                         break;
106                 if (!check_ship_ok(&tender))
107                     return RET_FAIL;
108                 total = 0;
109                 while (tend_nxtitem(&targets, (s_char *)&target)) {
110                         if (!player->owner)
111                                 continue;
112
113                         if (target.lnd_ship != tender.shp_uid)
114                                 continue;
115                         ontarget = getvar(ip->i_vtype,(s_char *)&target, EF_LAND);
116                         if (ontarget == 0 && amt < 0) {
117                                 pr("No %s on %s\n",
118                                         ip->i_name, prship((struct shpstr *)&target));
119                                 continue;
120                         }
121                         lbase = &lchr[(int)target.lnd_type];
122                         maxtarget = vl_find(ip->i_vtype, lbase->l_vtype,
123                                 lbase->l_vamt, (int)lbase->l_nv);
124                         if (amt < 0) {
125                                 if (!player->owner)
126                                         amt=0;
127
128                                 /* take from target and give to tender */
129                                 transfer = min(ontarget, -amt);
130                                 transfer = min(maxtender - ontender, transfer);
131                                 if (transfer == 0)
132                                         continue;
133                                 putvar(ip->i_vtype, ontarget - transfer,
134                                         (s_char *)&target, EF_LAND);
135                                 ontender += transfer;
136                                 total += transfer;
137                         } else {
138                                 /* give to target from tender */
139                                 transfer = min(ontender, amt);
140                                 transfer = min(transfer, maxtarget - ontarget);
141                                 if (transfer == 0)
142                                         continue;
143                                 putvar(ip->i_vtype, ontarget + transfer,
144                                         (s_char *)&target, EF_LAND);
145                                 ontender -= transfer;
146                                 total += transfer;
147                         }
148                         expose_land(&tender, &target);
149                         putland(target.lnd_uid, &target);
150                         if (amt > 0 && ontender == 0) {
151                                 pr("%s out of %s\n", prship(&tender),
152                                    ip->i_name);
153                                 break;
154                         }
155                 }
156                 pr("%d total %s transferred %s %s\n",
157                    total, ip->i_name, (amt > 0) ? "off of" : "to",
158                    prship(&tender));
159                 putvar(ip->i_vtype, ontender, (s_char *)&tender, EF_SHIP);
160                 tender.shp_mission = 0;
161                 putship(tender.shp_uid, &tender);
162         }
163         return RET_OK;
164 }
165
166 static void
167 expose_land(struct shpstr *s1, struct lndstr *l1)
168 {
169         if (getvar(V_PSTAGE, (s_char *)s1, EF_SHIP) == PLG_INFECT &&
170             getvar(V_PSTAGE, (s_char *)l1, EF_LAND) == PLG_HEALTHY)
171                 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)l1, EF_LAND);
172         if (getvar(V_PSTAGE, (s_char *)l1, EF_LAND) == PLG_INFECT &&
173             getvar(V_PSTAGE, (s_char *)s1, EF_SHIP) == PLG_HEALTHY)
174                 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)s1, EF_SHIP);
175 }