]> 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-2006, 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 <config.h>
35
36 #include "misc.h"
37 #include "player.h"
38 #include "sect.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     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 && dchr[sect.sct_type].d_cost < 0)
101             continue;
102         sprintf(prompt, "%s %d%% %s  desig? ",
103                 xyas(sect.sct_x, sect.sct_y, player->cnum),
104                 sect.sct_effic, dchr[sect.sct_type].d_name);
105         if ((p = getstarg(deschar, prompt, buf)) == 0)
106             continue;
107
108         if (!check_sect_ok(&sect))
109             continue;
110
111         des = sct_typematch(p);
112         if (des < 0 || (((des == SCT_BSPAN) || (des == SCT_BTOWER)) &&
113                         !player->god)) {
114             pr("See \"info Sector-types\"\n");
115             if (for_real)
116                 return (long)RET_FAIL;
117             else
118                 return (long)-RET_FAIL;
119         }
120         if (!player->god) {
121             if (des == SCT_WASTE) {
122                 if (for_real)
123                     pr("Only a nuclear device (or %s) can make a %s!\n",
124                        cname(0), dchr[des].d_name);
125                 continue;
126             }
127             if (dchr[des].d_cost < 0) {
128                 if (for_real)
129                     pr("Only %s can make a %s!\n", cname(0), dchr[des].d_name);
130                 continue;
131             }
132         }
133         if (sect.sct_type == des && sect.sct_newtype == des)
134             continue;
135         if (sect.sct_type == SCT_SANCT)
136             breaksanct++;
137         if ((des == SCT_HARBR) || (des == SCT_BHEAD)) {
138             for (n = 1; n <= 6; n++) {
139                 getsect(nstr.x + diroff[n][0],
140                         nstr.y + diroff[n][1], &check);
141                 if (check.sct_type == SCT_WATER)
142                     break;
143                 if (check.sct_type == SCT_BSPAN)
144                     break;
145                 if (check.sct_type == SCT_BTOWER)
146                     break;
147             }
148             if (n > 6) {
149                 if (for_real)
150                     pr("%s does not border on water.\n",
151                        xyas(nstr.x, nstr.y, player->cnum));
152                 if (player->god) {
153                     if (for_real)
154                         pr("But if it's what you want ...\n");
155                 } else
156                     continue;
157             }
158         }
159         if (sect.sct_type == SCT_SANCT && !player->god)
160             continue;
161         n = sect.sct_type;
162         if ((sect.sct_newtype != des) && (sect.sct_type != des)
163             && dchr[des].d_cost > 0) {
164             if (for_real) {
165                 if (check_cost(!deschar, dchr[des].d_cost, cash, &warned,
166                                player->argp[3]))
167                     break;
168             } else {
169                 cost += dchr[des].d_cost;
170                 continue;
171             }
172         }
173         if (sect.sct_type != des && (sect.sct_effic < 5 || player->god)) {
174             if (player->god)
175                 set_coastal(&sect, des);
176             sect.sct_type = des;
177             sect.sct_effic = 0;
178             changed += map_set(player->cnum, sect.sct_x, sect.sct_y,
179                                dchr[des].d_mnem, 0);
180         }
181         sect.sct_newtype = des;
182         putsect(&sect);
183         if (sect.sct_x == cap_x && sect.sct_y == cap_y
184             && des != SCT_CAPIT && des != SCT_SANCT && des != SCT_MOUNT
185             && for_real)
186             pr("You have redesignated your capital!\n");
187         if (opt_EASY_BRIDGES == 0) {    /* may cause a bridge fall */
188             if (n != SCT_BHEAD)
189                 continue;
190             bridgefall(&sect, 0);
191         }
192     }
193     if (for_real) {
194         if (changed)
195             writemap(player->cnum);
196         if (breaksanct)
197             bsanct();
198         return (long)RET_OK;
199     } else {
200         return cost;
201     }
202 }