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