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