]> 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-2008, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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 <config.h>
35
36 #include "commands.h"
37 #include "game.h"
38 #include "item.h"
39 #include "map.h"
40 #include "optlist.h"
41 #include "plague.h"
42
43 static int explore_map(coord curx, coord cury, char *arg);
44
45 int
46 explore(void)
47 {
48     int amount;
49     struct sctstr sect;
50     struct sctstr endsect;
51     struct sctstr start;
52     struct sctstr chksect;
53     double weight;
54     int mcost, dam;
55     int infected;
56     i_type vtype;
57     int amt_src;
58     int amt_dst;
59     struct ichrstr *ip;
60     int work;
61     int loyal;
62     int own, mob;
63     int justtook;
64     coord x, y;
65     char *p;
66     int n;
67     int left;
68     char buf[1024];
69     char prompt[128];
70
71     if (!(ip = whatitem(player->argp[1], "explore with what? (civ/mil) ")))
72         return RET_SYN;
73     vtype = ip->i_uid;
74     if ((vtype != I_CIVIL) && (vtype != I_MILIT)) {
75         pr("You can only explore with civs and mil.\n");
76         return RET_FAIL;
77     }
78     if ((p = getstarg(player->argp[2], "from sector : ", buf)) == 0)
79         return RET_SYN;
80     if (!sarg_xy(p, &x, &y))
81         return RET_SYN;
82     if (!getsect(x, y, &sect) || !player->owner) {
83         pr("Not yours\n");
84         return RET_FAIL;
85     }
86     infected = sect.sct_pstage == PLG_INFECT;
87     if ((amt_src = sect.sct_item[vtype]) <= 0) {
88         pr("No %s in %s\n", ip->i_name,
89            xyas(sect.sct_x, sect.sct_y, player->cnum));
90         return RET_SYN;
91     }
92     own = sect.sct_own;
93     mob = sect.sct_mobil;
94     if (vtype == I_CIVIL && sect.sct_oldown != own) {
95         pr("You can't explore with conquered populace!\n");
96         return RET_SYN;
97     }
98     if (mob <= 0) {
99         pr("No mobility in %s\n",
100            xyas(sect.sct_x, sect.sct_y, player->cnum));
101         return RET_SYN;
102     }
103
104     /* only used when moving civs; but prevent spurious compiler warnings */
105     work = sect.sct_work;
106     loyal = sect.sct_loyal;
107     if (vtype == I_CIVIL && work != 100)
108         pr("Warning: civil unrest\n");
109
110     sprintf(prompt, "Number of %s to explore with? (max %d) ",
111             ip->i_name, amt_src);
112     amount = onearg(player->argp[3], prompt);
113     if (!check_sect_ok(&sect))
114         return RET_FAIL;
115     if (amount > amt_src) {
116         amount = amt_src;
117         pr("Only exploring with %d.\n", amount);
118     }
119
120     if (!want_to_abandon(&sect, vtype, amount, 0)) {
121         pr("Explore cancelled.\n");
122         return RET_FAIL;
123     }
124
125     if (!check_sect_ok(&sect))
126         return RET_FAIL;
127
128     if (amount <= 0)
129         return RET_SYN;
130     weight = (double)amount * ip->i_lbs;
131     /* remove commodities from source sector */
132     getsect(x, y, &start);
133     amt_src = start.sct_item[vtype];
134     amt_src -= amount;
135     if (amt_src < 0) {
136         pr("%s in %s are gone!\n", ip->i_name,
137            xyas(start.sct_x, start.sct_y, player->cnum));
138         return RET_OK;
139     }
140     start.sct_item[vtype] = amt_src;
141     start.sct_flags |= MOVE_IN_PROGRESS;
142     putsect(&start);
143     /*
144      * Now parse the path and return ending sector.
145      */
146     dam = 1;
147     mcost = move_ground(&sect, &endsect, weight, player->argp[4],
148                         explore_map, 1, &dam);
149
150     if (dam) {
151         left = effdamage(amount, dam);
152         if (left < amount) {
153             if (left) {
154                 pr("%d of the %s you were exploring with were destroyed!\n"
155                    "Only %d %s made it to %s\n",
156                    amount - left, ip->i_name, left, ip->i_name,
157                    xyas(endsect.sct_x, endsect.sct_y, player->cnum));
158             } else {
159                 pr("All of the %s you were exploring with were destroyed!\n",
160                    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 = 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 = 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             game_tick_to_now(&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(coord curx, coord cury, char *arg)
279 {
280     struct nstr_sect ns;
281     struct sctstr sect;
282     char view[7];
283     int i;
284     int changed = 0;
285
286     snxtsct_dist(&ns, curx, cury, 1);
287     i = 0;
288     while (i < 7 && nxtsct(&ns, &sect)) {
289         /* Nasty: this relies on the iteration order */
290         view[i] = dchr[sect.sct_type].d_mnem;
291         switch (sect.sct_type) {
292         case SCT_WATER:
293         case SCT_RURAL:
294         case SCT_MOUNT:
295         case SCT_WASTE:
296         case SCT_PLAINS:
297             break;
298         default:
299             if (sect.sct_own != player->cnum && !player->god)
300                 view[i] = '?';
301             break;
302         }
303         changed += map_set(player->cnum, ns.x, ns.y, view[i], 0);
304         i++;
305     }
306     if (changed)
307         writemap(player->cnum);
308     if (!getsect(curx, cury, &sect))
309         return RET_FAIL;
310     pr("    %c %c         min gold fert  oil uran\n", view[0], view[1]);
311     pr("   %c %c %c        %3d %4d %4d %4d %4d\n",
312        view[2], view[3], view[4],
313        sect.sct_min, sect.sct_gmin, sect.sct_fertil, sect.sct_oil,
314        sect.sct_uran);
315     pr("    %c %c\n", view[5], view[6]);
316     return RET_OK;
317 }