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