]> git.pond.sub.org Git - empserver/blob - src/lib/commands/expl.c
Import of Empire 4.2.12
[empserver] / src / lib / commands / expl.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  *  expl.c: Take over unoccupied sectors
29  * 
30  *  Known contributors to this file:
31  *     Jeff Wallace, 1989
32  */
33
34 #include "misc.h"
35 #include "player.h"
36 #include "var.h"
37 #include "sect.h"
38 #include "xy.h"
39 #include "nsc.h"
40 #include "item.h"
41 #include "file.h"
42 #include "deity.h"
43 #include "nat.h"
44 #include "optlist.h"
45 #include "commands.h"
46
47 static  int explore_map(s_char *what, coord curx, coord cury, s_char *arg);
48
49 extern int etu_per_update;
50 extern int sect_mob_neg_factor;
51
52 int
53 explore(void)
54 {
55         register int amount;
56         struct  sctstr orig, sect;
57         struct  sctstr endsect;
58         struct  sctstr start;
59         struct  sctstr chksect;
60         double  weight;
61         int     mcost, dam;
62         int     infected;
63         int     vtype;
64         int     amt_src;
65         int     amt_dst;
66         struct  ichrstr *ip;
67         int     work;
68         int     loyal;
69         int     own, mob;
70         int     justtook;
71         coord   x, y;
72         s_char  *p;
73         int     n;
74         int     left;
75         s_char  buf[1024];
76         s_char  prompt[128];
77
78         if ((ip = whatitem(player->argp[1], "explore with what? (civ/mil) ")) == 0)
79                 return RET_SYN;
80         vtype = ip->i_vtype;
81         if ((vtype != V_CIVIL) && (vtype != V_MILIT)) {
82                 pr("You can only explore with civs and mil.\n");
83                 return RET_FAIL;
84         }
85         if ((p = getstarg(player->argp[2], "from sector : ", buf)) == 0)
86                 return RET_SYN;
87         if (!sarg_xy(p, &x, &y))
88                 return RET_SYN;
89         if (!getsect(x, y, &sect) || !player->owner) {
90                 pr("Not yours\n");
91                 return RET_FAIL;
92         }
93         bcopy((s_char *)&sect,(s_char *)&orig,sizeof(struct sctstr));
94         infected = getvar(V_PSTAGE, (s_char *)&sect, EF_SECTOR) == PLG_INFECT;
95         if ((amt_src = getvar(vtype, (s_char *)&sect, EF_SECTOR)) <= 0) {
96                 pr("No %s in %s\n", ip->i_name, 
97                         xyas(sect.sct_x, sect.sct_y, player->cnum));
98                 return RET_SYN;
99         }
100         own = sect.sct_own;
101         mob = (int)sect.sct_mobil;
102         if (vtype == V_CIVIL && sect.sct_oldown != own) {
103                 pr("You can't explore with conquered populace!\n");
104                 return RET_SYN;
105         }
106         if (mob <= 0) {
107                 pr("No mobility in %s\n",
108                         xyas(sect.sct_x, sect.sct_y, player->cnum));
109                 return RET_SYN;
110         }
111         if (vtype == V_CIVIL) {
112                 work = sect.sct_work;
113                 if (work != 100)
114                         pr("Warning: civil unrest\n");
115                 loyal = sect.sct_loyal;
116         } else if (vtype == V_MILIT) {
117                 work = 100;
118                 loyal = 0;
119         }
120         sprintf(prompt, "Number of %s to explore with? (max %d) ",
121                 ip->i_name, amt_src);
122         amount = onearg(player->argp[3], prompt);
123         if (!check_sect_ok(&sect))
124             return RET_FAIL;
125         if (amount > amt_src) {
126                 amount = amt_src;
127                 pr("Only exploring with %d.\n", amount);
128         }
129
130         if (!want_to_abandon(&sect, vtype, amount, 0)) {
131             pr("Explore cancelled.\n");
132             return RET_FAIL;
133         }
134
135         if (!check_sect_ok(&sect))
136             return RET_FAIL;
137
138         if (amount <= 0)
139                 return RET_SYN;
140         weight = amount * ip->i_lbs;
141         /* remove commodities from source sector */
142         getsect(x, y, &start);
143         amt_src = getvar(vtype, (s_char *)&start, EF_SECTOR);
144         amt_src -= amount;
145         if (amt_src < 0) {
146                 pr("%s in %s are gone!\n", ip->i_name, 
147                         xyas(start.sct_x, start.sct_y, player->cnum));
148                 return RET_OK;
149         }
150         putvar(vtype, amt_src, (s_char *)&start, EF_SECTOR);
151         start.sct_flags |= MOVE_IN_PROGRESS;
152         putsect(&start);
153         /*
154          * Now parse the path and return ending sector.
155          */
156         dam = 1;
157         mcost = move_ground((s_char *)ip, &sect, &endsect,
158                 (double)sect.sct_mobil, weight, player->argp[4], 
159                 explore_map, 1, &dam);
160
161         if (dam) {
162                 left = effdamage(amount, dam);
163                 if (left < amount) {
164                         if (left) {
165                                 pr("%d of the %s you were exploring with were destroyed!\nOnly %d %s made it to %s\n",
166                                    amount - left,
167                                    ip->i_name,
168                                    left,
169                                    ip->i_name,
170                                    xyas(endsect.sct_x, endsect.sct_y, player->cnum));                      
171                         } else {
172                                 pr("All of the %s you were exploring with were destroyed!\n",ip->i_name);
173                         }
174                         amount = left;
175                 }
176         }
177         if (mcost > 0)
178                 pr("Total movement cost = %d",mcost);
179         else
180                 pr("No mobility used");
181
182         if (mcost < 0) {
183                 pr("\nExplore aborted");
184                 getsect(start.sct_x,start.sct_y,&sect);
185                 sect.sct_mobil = (u_char)mob;
186         }else{
187                 /* Charge mobility */
188                 getsect(sect.sct_x, sect.sct_y, &sect);
189                 n = sect.sct_mobil - mcost;
190                 if (n < 0)
191                         n = 0;
192                 sect.sct_mobil = (u_char)n;
193                 pr(", %d mob left in %s", sect.sct_mobil,
194                         xyas(sect.sct_x,sect.sct_y,player->cnum));
195                 putsect(&sect);
196                 getsect(endsect.sct_x, endsect.sct_y, &sect);
197         }
198         pr("\n");
199         justtook = 0;
200
201         /*
202          *      Check for a multitude of problems
203          */
204         getsect(endsect.sct_x, endsect.sct_y, &chksect);
205         if(chksect.sct_type == '.') {
206                 pr("Bridge disappeared!\n");
207                 getsect(start.sct_x,start.sct_y,&start);
208                 start.sct_flags &= ~MOVE_IN_PROGRESS;
209                 putsect(&start);
210                 return RET_FAIL;
211         }
212         if(!player->god && chksect.sct_own && chksect.sct_own != player->cnum
213                 && chksect.sct_type != SCT_SANCT) {
214                 pr("Somebody beat you there!\n");
215                 /* Send them xback home */
216                 getsect(start.sct_x,start.sct_y,&sect);
217                 if (sect.sct_own != own) {
218                         pr("Someone captured the sector you started from!\n");
219                         pr("Your exploring people die of disappointment!\n");
220                         sect.sct_flags &= ~MOVE_IN_PROGRESS;
221                         putsect(&sect);
222                         return RET_FAIL;
223                 }
224         }
225
226         if (!player->god && !player->owner && sect.sct_type != SCT_SANCT) {
227                 (void) takeover(&sect, player->cnum);
228                 justtook = 1;
229                 sect.sct_oldown = own;
230                 sect.sct_work = work;
231                 sect.sct_loyal = loyal;
232         }
233         if (vtype == V_CIVIL && sect.sct_oldown != player->cnum) {
234                 pr("Your civilians don't want to stay!\n");
235                 getsect(start.sct_x,start.sct_y,&sect);
236                 if (sect.sct_own != own) {
237                         pr("Someone captured the sector you started from!\n");
238                         pr("Your exploring people die of disappointment!\n");
239                         sect.sct_flags &= ~MOVE_IN_PROGRESS;
240                         putsect(&sect);
241                         return RET_FAIL;
242                 }
243         }
244         getsect(start.sct_x,start.sct_y,&start);
245         start.sct_flags &= ~MOVE_IN_PROGRESS;
246         putsect(&start);
247         amt_dst = getvar(vtype, (s_char *)&sect, EF_SECTOR);
248         if (32767 - amt_dst < amount) {
249                 amount = 32767 - amt_dst;
250                 pr("Only %d can be left there.\n", amount);
251                 if (amount <= 0)
252                         getsect(start.sct_x,start.sct_y,&sect);
253         }
254         if (putvar(vtype, amount + amt_dst, (s_char *)&sect, EF_SECTOR) < 0) {
255                 pr("No more room in %s.\n", 
256                         xyas(sect.sct_x, sect.sct_y, player->cnum));
257                 return RET_OK;
258         }
259         /*
260          * Now add commodities to destination sector,
261          * along with plague that came along for the ride.
262          * Takeover unowned sectors if not deity.
263          */
264         if (!player->god && sect.sct_type != SCT_SANCT && justtook) {
265                 pr("Sector %s is now yours.\n", 
266                         xyas(sect.sct_x, sect.sct_y, player->cnum));
267                 if (opt_MOB_ACCESS) {
268                         time(&sect.sct_access);
269                         sect.sct_mobil = -(etu_per_update / sect_mob_neg_factor);
270                 } else {
271                         sect.sct_mobil = 0;
272                 }
273         }
274         if (infected && getvar(V_PSTAGE, (s_char *)&sect, EF_SECTOR) == 0)
275                 putvar(V_PSTAGE, PLG_EXPOSED, (s_char *)&sect, EF_SECTOR);
276         if (vtype == V_CIVIL) {
277                 if (opt_NEW_WORK) {
278                         sect.sct_loyal =( (amt_dst * sect.sct_loyal) + 
279                                          (amount * loyal) ) / (amt_dst+amount);
280                         sect.sct_work =( (amt_dst * sect.sct_work) + 
281                                         (amount * work) ) / (amt_dst+amount);
282                 } else {        /* ! NEW_WORK */
283
284                         /* It only takes one bad apple... */
285                         if (sect.sct_loyal < loyal)
286                                 sect.sct_loyal = loyal;
287                         if (sect.sct_work > work)
288                                 sect.sct_work = work;
289                 } /* end NEW_WORK */
290         }
291         putsect(&sect);
292         return RET_OK;
293 }
294
295 /*ARGSUSED*/
296 static int
297 explore_map(s_char *what, coord curx, coord cury, s_char *arg)
298 {
299         struct  nstr_sect ns;
300         struct  natstr *np;
301         struct  sctstr sect;
302         coord   rel_x, rel_y;
303         s_char  range[128];
304         s_char  view[7];
305         int     i;
306         int     changed = 0;
307
308         np = getnatp(player->cnum);
309         rel_x = xrel(np, curx);
310         rel_y = yrel(np, cury);
311         sprintf(range, "%d:%d,%d:%d", rel_x-2, rel_x+2, rel_y-1, rel_y+1);
312         if (!snxtsct(&ns, range))
313                 return RET_FAIL;
314         i = 0;
315         while (i < 7 && nxtsct(&ns, &sect)) {
316                 view[i] = dchr[sect.sct_type].d_mnem;
317                 switch (sect.sct_type) {
318                 case SCT_WATER:
319                 case SCT_RURAL:
320                 case SCT_MOUNT:
321                 case SCT_WASTE:
322                 case SCT_PLAINS:
323                         break;
324                 default:
325                         if (sect.sct_own != player->cnum && !player->god)
326                                 view[i] = '?';
327                         break;
328                 }
329                 changed += map_set(player->cnum, ns.x, ns.y, view[i], 0);
330                 i++;
331         }
332         if (changed)
333                 writemap(player->cnum);
334         if (!getsect(curx, cury, &sect))
335                 return RET_FAIL;
336         pr("    %c %c         min gold fert  oil uran\n", view[0], view[1]);
337         pr("   %c %c %c        %3d %4d %4d %4d %4d\n",
338                 view[2], view[3], view[4],
339                 sect.sct_min, sect.sct_gmin, sect.sct_fertil, sect.sct_oil,
340                 sect.sct_uran);
341         pr("    %c %c\n", view[5], view[6]);
342         return RET_OK;
343 }