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