]> git.pond.sub.org Git - empserver/blob - src/lib/commands/desi.c
Fix long lines. No functional changes.
[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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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",
130                        cname(0), dchr[des].d_name);
131                 continue;
132             }
133         }
134         if (sect.sct_type == des && sect.sct_newtype == des)
135             continue;
136         if (sect.sct_type == SCT_SANCT)
137             breaksanct++;
138         if ((des == SCT_HARBR) || (des == SCT_BHEAD)) {
139             for (n = 1; n <= 6; n++) {
140                 getsect(nstr.x + diroff[n][0],
141                         nstr.y + diroff[n][1], &check);
142                 if (check.sct_type == SCT_WATER)
143                     break;
144                 if (check.sct_type == SCT_BSPAN)
145                     break;
146                 if (check.sct_type == SCT_BTOWER)
147                     break;
148             }
149             if (n > 6) {
150                 if (for_real)
151                     pr("%s does not border on water.\n",
152                        xyas(nstr.x, nstr.y, player->cnum));
153                 if (player->god) {
154                     if (for_real)
155                         pr("But if it's what you want ...\n");
156                 } else
157                     continue;
158             }
159         }
160         if (sect.sct_type == SCT_SANCT && !player->god)
161             continue;
162         n = sect.sct_type;
163         if ((sect.sct_newtype != des) && (sect.sct_type != des)
164             && dchr[des].d_cost > 0) {
165             if (for_real) {
166                 if (check_cost(!deschar, dchr[des].d_cost, cash, &warned,
167                                player->argp[3]))
168                     break;
169             } else {
170                 cost += dchr[des].d_cost;
171                 continue;
172             }
173         }
174         if (sect.sct_type != des && (sect.sct_effic < 5 || player->god)) {
175             if (player->god)
176                 set_coastal(&sect, des);
177             sect.sct_type = des;
178             sect.sct_effic = 0;
179             changed += map_set(player->cnum, sect.sct_x, sect.sct_y,
180                                dchr[des].d_mnem, 0);
181         }
182         sect.sct_newtype = des;
183         putsect(&sect);
184         if (sect.sct_x == cap_x && sect.sct_y == cap_y
185             && des != SCT_CAPIT && des != SCT_SANCT && des != SCT_MOUNT
186             && for_real)
187             pr("You have redesignated your capital!\n");
188         if (opt_EASY_BRIDGES == 0) {    /* may cause a bridge fall */
189             if (n != SCT_BHEAD)
190                 continue;
191             bridgefall(&sect, 0);
192         }
193     }
194     if (for_real) {
195         if (changed)
196             writemap(player->cnum);
197         if (breaksanct)
198             bsanct();
199         return (long)RET_OK;
200     } else {
201         return cost;
202     }
203 }