]> git.pond.sub.org Git - empserver/blob - src/lib/commands/desi.c
Update copyright notice.
[empserver] / src / lib / commands / desi.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  *  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                 if (for_real) 
138                     pr("Only %s can make a %s!\n", cname(0), dchr[des].d_name);
139                 continue;
140             }
141             if (des == SCT_WASTE) {
142                 if (for_real)
143                     pr("Only a nuclear device (or %s) can make a %s!\n",
144                        cname(0), dchr[des].d_name);
145                 continue;
146             }
147         }
148         if (sect.sct_type == des && sect.sct_newtype == des)
149             continue;
150         if (sect.sct_type == SCT_SANCT)
151             breaksanct++;
152         if (des == SCT_HARBR) {
153             for (n = 1; n <= 6; n++) {
154                 getsect(nstr.x + diroff[n][0],
155                         nstr.y + diroff[n][1], &check);
156                 if (check.sct_type == SCT_WATER)
157                     break;
158                 if (check.sct_type == SCT_BSPAN)
159                     break;
160                 if (check.sct_type == SCT_BTOWER)
161                     break;
162             }
163             if (n > 6) {
164                 if (for_real)
165                     pr("%s does not border on water.\n",
166                        xyas(nstr.x, nstr.y, player->cnum));
167                 if (player->god) {
168                     if (for_real)
169                         pr("But if it's what you want ...\n");
170                 } else
171                     continue;
172             }
173         }
174         if (des == SCT_BHEAD) {
175             for (n = 1; n <= 6; n++) {
176                 getsect(nstr.x + diroff[n][0],
177                         nstr.y + diroff[n][1], &check);
178                 if (check.sct_type == SCT_WATER)
179                     break;
180                 if (check.sct_type == SCT_BSPAN)
181                     break;
182                 if (check.sct_type == SCT_BTOWER)
183                     break;
184             }
185             if (n > 6) {
186                 if (for_real)
187                     pr("%s does not border on water.\n",
188                        xyas(nstr.x, nstr.y, player->cnum));
189                 if (player->god) {
190                     if (for_real)
191                         pr("But if it's what you want...\n");
192                 } else
193                     continue;
194             }
195         }
196         if (sect.sct_type == SCT_SANCT && !player->god)
197             continue;
198         n = sect.sct_type;
199         if ((sect.sct_newtype != des) && (sect.sct_type != des)
200             && dchr[des].d_cost) {
201             if (for_real) {
202                 if (check_cost(!deschar, dchr[des].d_cost, cash, &warned,
203                                player->argp[3]))
204                     break;
205             } else {
206                 cost += dchr[des].d_cost;
207                 continue;
208             }
209         }
210         if (sect.sct_type != des && (sect.sct_effic < 5 || player->god)) {
211             sect.sct_type = des;
212             sect.sct_effic = 0;
213             changed += map_set(player->cnum, sect.sct_x, sect.sct_y,
214                                dchr[des].d_mnem, 0);
215         }
216         sect.sct_newtype = des;
217         putsect(&sect);
218         if (sect.sct_x == cap_x && sect.sct_y == cap_y
219             && des != SCT_CAPIT && des != SCT_SANCT && des != SCT_MOUNT
220             && for_real)
221             pr("You have redesignated your capital!\n");
222         if (opt_EASY_BRIDGES == 0) {    /* may cause a bridge fall */
223             if (n != SCT_BHEAD)
224                 continue;
225             bridgefall(&sect, 0);
226         }
227     }
228     if (for_real) {
229         if (changed)
230             writemap(player->cnum);
231         if (breaksanct)
232             bsanct();
233         return (long)RET_OK;
234     } else {
235         return cost;
236     }
237 }