]> git.pond.sub.org Git - empserver/blob - src/lib/commands/desi.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / commands / desi.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  *  desi.c: Redesignate sectors
29  * 
30  *  Known contributors to this file:
31  *      Steve McClure, 1998-2000
32  */
33
34 #include "misc.h"
35 #include "player.h"
36 #include "var.h"
37 #include "sect.h"
38 #include "item.h"
39 #include "xy.h"
40 #include "path.h"
41 #include "nsc.h"
42 #include "deity.h"
43 #include "file.h"
44 #include "nat.h"
45 #include "optlist.h"
46 #include "commands.h"
47
48 long do_desi(struct natstr *natp, s_char *sects, s_char *deschar,
49              long int cash, int for_real);
50
51 int
52 desi(void)
53 {
54     long cash;
55     long cost;
56     struct natstr *natp;
57
58     natp = getnatp(player->cnum);
59     cash = natp->nat_money;
60     if (player->argp[2]) {
61         cost = do_desi(natp, player->argp[1], player->argp[2], cash, 0);
62         if (cost < 0)
63             return (int)(-cost);
64         if (chkmoney(cost, cash, player->argp[3]))
65             return RET_SYN;
66     }
67     return (int)do_desi(natp, player->argp[1], player->argp[2], cash, 1);
68 }
69
70 long
71 do_desi(struct natstr *natp, s_char *sects, s_char *deschar, long int cash,
72         int for_real)
73 {
74     extern int opt_NO_LCMS;
75     extern int opt_NO_HCMS;
76     register int n;
77     s_char *p;
78     int breaksanct;
79     int cap_x;
80     int cap_y;
81     int des;
82     struct nstr_sect nstr;
83     struct sctstr sect;
84     struct sctstr check;
85     s_char prompt[128];
86     s_char buf[1024];
87     long cost = 0;
88     int changed = 0;
89     int warned = 0;
90
91     breaksanct = 0;
92     if (!snxtsct(&nstr, sects)) {
93         if (for_real)
94             return (long)RET_SYN;
95         else
96             return (long)-RET_SYN;
97     }
98     cap_x = natp->nat_xcap;
99     cap_y = natp->nat_ycap;
100     while (!player->aborted && nxtsct(&nstr, &sect)) {
101         if (!player->owner)
102             continue;
103         if (!player->god) {
104             if (sect.sct_type == SCT_MOUNT ||
105                 sect.sct_type == SCT_BTOWER ||
106                 sect.sct_type == SCT_BSPAN ||
107                 sect.sct_type == SCT_WASTE ||
108                 sect.sct_type == SCT_PLAINS ||
109                 (opt_NO_LCMS && sect.sct_type == SCT_LIGHT) ||
110                 (opt_NO_HCMS && sect.sct_type == SCT_HEAVY) ||
111                 (opt_NO_OIL && sect.sct_type == SCT_OIL) ||
112                 (opt_NO_OIL && sect.sct_type == SCT_REFINE))
113                 continue;
114         }
115         sprintf(prompt, "%s %d%% %s  desig? ",
116                 xyas(sect.sct_x, sect.sct_y, player->cnum),
117                 sect.sct_effic, dchr[sect.sct_type].d_name);
118         if ((p = getstarg(deschar, prompt, buf)) == 0)
119             continue;
120
121         if (!check_sect_ok(&sect))
122             continue;
123
124         des = typematch(p, EF_SECTOR);
125         if (des < 0 || (((des == SCT_BSPAN) || (des == SCT_BTOWER)) &&
126                         !player->god)) {
127             pr("See \"info Sector-types\"\n");
128             if (for_real)
129                 return (long)RET_FAIL;
130             else
131                 return (long)-RET_FAIL;
132         }
133         if (!player->god) {
134             if (des == SCT_WATER || des == SCT_MOUNT ||
135                 des == SCT_SANCT || des == SCT_PLAINS ||
136                 (opt_NO_LCMS && des == SCT_LIGHT) ||
137                 (opt_NO_HCMS && des == SCT_HEAVY) ||
138                 (opt_NO_OIL && des == SCT_OIL) ||
139                 (opt_NO_OIL && des == SCT_REFINE)) {
140                 pr("Only %s can make a %s!\n", cname(0), dchr[des].d_name);
141                 continue;
142             }
143             if (des == SCT_WASTE) {
144                 pr("Only a nuclear device (or %s) can make a %s!\n",
145                    cname(0), dchr[des].d_name);
146                 continue;
147             }
148         }
149         if (sect.sct_type == des && sect.sct_newtype == des)
150             continue;
151         if (sect.sct_type == SCT_SANCT)
152             breaksanct++;
153         if (sect.sct_x == cap_x && sect.sct_y == cap_y &&
154             des != SCT_CAPIT && des != SCT_SANCT &&
155             des != SCT_MOUNT && for_real)
156             pr("You have redesignated your capital!\n");
157         if (des == SCT_HARBR) {
158             for (n = 1; n <= 6; n++) {
159                 getsect(nstr.x + diroff[n][0],
160                         nstr.y + diroff[n][1], &check);
161                 if (check.sct_type == SCT_WATER)
162                     break;
163                 if (check.sct_type == SCT_BSPAN)
164                     break;
165                 if (check.sct_type == SCT_BTOWER)
166                     break;
167             }
168             if (n > 6) {
169                 if (for_real)
170                     pr("%s does not border on water.\n",
171                        xyas(nstr.x, nstr.y, player->cnum));
172                 if (player->god) {
173                     if (for_real)
174                         pr("But if it's what you want ...\n");
175                 } else
176                     continue;
177             }
178         }
179         if (des == SCT_BHEAD) {
180             for (n = 1; n <= 6; n++) {
181                 getsect(nstr.x + diroff[n][0],
182                         nstr.y + diroff[n][1], &check);
183                 if (check.sct_type == SCT_WATER)
184                     break;
185                 if (check.sct_type == SCT_BSPAN)
186                     break;
187                 if (check.sct_type == SCT_BTOWER)
188                     break;
189             }
190             if (n > 6) {
191                 if (for_real)
192                     pr("%s does not border on water.\n",
193                        xyas(nstr.x, nstr.y, player->cnum));
194                 if (player->god) {
195                     if (for_real)
196                         pr("But if it's what you want...\n");
197                 } else
198                     continue;
199             }
200         }
201         if (sect.sct_type == SCT_SANCT && !player->god)
202             continue;
203         n = sect.sct_type;
204         if ((sect.sct_newtype != des) && (sect.sct_type != des)
205             && dchr[des].d_cost) {
206             if (for_real) {
207                 if (check_cost
208                     (!deschar, dchr[des].d_cost, cash, &warned,
209                      player->argp[3]))
210                     break;
211             } else {
212                 cost += dchr[des].d_cost;
213                 continue;
214             }
215         }
216         if (sect.sct_effic < 5 || player->god) {
217             sect.sct_type = des;
218             sect.sct_effic = 0;
219             /* No longer tear down infrasturcture
220                sect.sct_road = 0;
221                sect.sct_defense = 0;
222              */
223             changed += map_set(player->cnum, sect.sct_x, sect.sct_y,
224                                dchr[des].d_mnem, 0);
225         }
226         sect.sct_newtype = des;
227         putsect(&sect);
228         if (opt_EASY_BRIDGES == 0) {    /* may cause a bridge fall */
229             if (n != SCT_BHEAD)
230                 continue;
231             bridgefall(&sect, 0);
232         }
233     }
234     if (for_real) {
235         if (changed)
236             writemap(player->cnum);
237         if (breaksanct)
238             bsanct();
239         return (long)RET_OK;
240     } else {
241         return cost;
242     }
243 }