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