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