]> git.pond.sub.org Git - empserver/blob - src/lib/commands/new.c
2a8a8917612b86fb4090c640926092869fb8f30e
[empserver] / src / lib / commands / new.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  *  new.c: Create a new capital for a player
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  */
33
34 #include <config.h>
35
36 #include <fcntl.h>
37 #if defined(_WIN32) && !defined(__GNUC__)
38 #include <io.h>
39 #endif
40 #if !defined(_WIN32)
41 #include <unistd.h>
42 #endif
43 #include "commands.h"
44 #include "land.h"
45 #include "optlist.h"
46 #include "path.h"
47 #include "prototypes.h"
48 #include "tel.h"
49
50 static int isok(int x, int y);
51 static void ok(signed char *map, int x, int y);
52 static void init_sanct(struct natstr *, coord, coord);
53
54 static struct range defrealm = { -8, -5, 10, 5, 0, 0 };
55
56 #define MAXAVAIL        300
57
58 int
59 new(void)
60 {
61     struct sctstr sect;
62     struct natstr *natp;
63     struct realmstr newrealm;
64     struct range absrealm;
65     natid num;
66     coord x, y;
67     int i;
68     char *p;
69     char buf[1024];
70     time_t current_time = time(NULL);
71
72     natp = getnatp(player->cnum);
73     if (natp->nat_xorg != 0 || natp->nat_yorg != 0) {
74         pr("Must be at 0,0 to add a new country\n");
75         return RET_FAIL;
76     }
77     if (!(natp = natargp(player->argp[1], "Country? ")))
78         return RET_SYN;
79     num = natp->nat_cnum;
80     if (natp->nat_stat != STAT_NEW) {
81         pr("Country #%d (%s) isn't a new country!\n", num, cname(num));
82         return RET_SYN;
83     }
84     if (player->argp[2] != 0) {
85         if ((p = getstarg(player->argp[2], "sanctuary pair : ", buf)) == 0)
86             return RET_SYN;
87         if (!sarg_xy(p, &x, &y) || !getsect(x, y, &sect))
88             return RET_SYN;
89         if (sect.sct_type != SCT_RURAL) {
90             pr("%s is a %s; try again...\n",
91                xyas(x, y, player->cnum), dchr[sect.sct_type].d_name);
92             return RET_SYN;
93         }
94         getsect(x + 2, y, &sect);
95         if (sect.sct_type != SCT_RURAL) {
96             pr("%s is a %s; try again...\n",
97                xyas(x + 2, y, player->cnum), dchr[sect.sct_type].d_name);
98             return RET_SYN;
99         }
100     } else {
101         for (i = 0; i < 300 && !player->aborted; i++) {
102             /* Both x and y should be either odd or even */
103             x = (random() % WORLD_X) - (WORLD_X / 2);
104             y = (((random() % WORLD_Y) - (WORLD_Y / 2)) & ~1) | (x & 1);
105             /*
106              * If either of the two potential
107              * sanctuary sectors are already
108              * owned by someone else, pick
109              * another place on the map.
110              */
111             getsect(x, y, &sect);
112             if (sect.sct_type == SCT_WATER || sect.sct_own != 0)
113                 continue;
114             getsect(x + 2, y, &sect);
115             if (sect.sct_type == SCT_WATER || sect.sct_own != 0)
116                 continue;
117             if (isok(x, y))
118                 break;
119         }
120         if (i == 300) {
121             pr("couldn't find an empty slot!\n");
122             return RET_FAIL;
123         }
124     }
125
126     if (player->aborted)
127         return RET_FAIL;
128     pr("added country %d at %s\n", num, xyas(x, y, player->cnum));
129     natp->nat_btu = max_btus;
130     natp->nat_stat = STAT_SANCT;
131     natp->nat_xcap = x;
132     natp->nat_ycap = y;
133     natp->nat_xorg = x;
134     natp->nat_yorg = y;
135     xyabsrange(natp, &defrealm, &absrealm);
136     for (i = 0; i < MAXNOR; i++) {
137         getrealm(i, num, &newrealm);
138         newrealm.r_xl = absrealm.lx;
139         newrealm.r_xh = absrealm.hx;
140         newrealm.r_yl = absrealm.ly;
141         newrealm.r_yh = absrealm.hy;
142         newrealm.r_timestamp = current_time;
143         putrealm(&newrealm);
144     }
145     if (players_at_00) {
146         natp->nat_xorg = 0;
147         natp->nat_yorg = 0;
148     }
149     natp->nat_money = start_cash;
150     natp->nat_level[NAT_HLEV] = start_happiness;
151     natp->nat_level[NAT_RLEV] = start_research;
152     natp->nat_level[NAT_TLEV] = start_technology;
153     natp->nat_level[NAT_ELEV] = start_education;
154     natp->nat_tgms = 0;
155     (void)close(open(mailbox(buf, num), O_RDWR | O_TRUNC | O_CREAT, 0660));
156     init_sanct(natp, x, y);
157     init_sanct(natp, x + 2, y);
158     putnat(natp);
159     return RET_OK;
160 }
161
162 static void
163 init_sanct(struct natstr *natp, coord x, coord y)
164 {
165     struct sctstr sect;
166
167     getsect(x, y, &sect);
168     sect.sct_own = natp->nat_cnum;
169     sect.sct_type = SCT_SANCT;
170     sect.sct_newtype = SCT_SANCT;
171     sect.sct_effic = 100;
172     sect.sct_road = 0;
173     sect.sct_rail = 0;
174     sect.sct_defense = 0;
175     sect.sct_mobil = startmob;
176     sect.sct_work = 100;
177     sect.sct_oldown = natp->nat_cnum;
178     if (at_least_one_100) {
179         sect.sct_oil = 100;
180         sect.sct_fertil = 100;
181         sect.sct_uran = 100;
182         sect.sct_min = 100;
183         sect.sct_gmin = 100;
184     }
185     sect.sct_item[I_CIVIL] = max_pop(start_research, &sect);
186     sect.sct_item[I_MILIT] = 55;
187     sect.sct_item[I_FOOD] = opt_NOFOOD ? 0 : 550;
188     sect.sct_item[I_UW] = 75;
189     putsect(&sect);
190 }
191
192 static int nmin, ngold, noil, nur;
193 static int nfree, navail, nowned;
194
195 static int
196 isok(int x, int y)
197 {
198     signed char *map;
199     char *p;
200     char buf[1024];
201
202     nmin = ngold = noil = nur = 0;
203     navail = nfree = nowned = 0;
204     if ((map = malloc((WORLD_X * WORLD_Y) / 2)) == 0) {
205         logerror("malloc failed in isok\n");
206         pr("Memory error.  Tell the deity.\n");
207         return 0;
208     }
209     memset(map, 0, (WORLD_X * WORLD_Y) / 2);
210     ok(map, x, y);
211     free(map);
212     if (nfree < 5)
213         return 0;
214     pr("Cap at %s; owned sectors: %d, free sectors: %d, avail: %d\n",
215        xyas(x, y, player->cnum), nowned, nfree, navail);
216     pr("min: %d, oil: %d, gold: %d, uranium: %d\n",
217        nmin, noil, ngold, nur);
218     p = getstring("This setup ok? ", buf);
219     if (p == 0 || *p != 'y')
220         return 0;
221     return 1;
222 }
223
224 static void
225 ok(signed char *map, int x, int y)
226 {
227     struct sctstr sect;
228     int dir;
229     int id;
230
231     if (navail > MAXAVAIL)
232         return;
233     id = sctoff(x, y);
234     if (map[id])
235         return;
236     if (!ef_read(EF_SECTOR, id, &sect))
237         return;
238     if (sect.sct_type == SCT_WATER || sect.sct_type == SCT_BSPAN)
239         return;
240     navail++;
241     if (navail >= MAXAVAIL) {
242         pr("At least %d...\n", MAXAVAIL);
243         return;
244     }
245     if (sect.sct_type != SCT_MOUNT && sect.sct_type != SCT_PLAINS) {
246         if (sect.sct_own == 0)
247             nfree++;
248         else
249             nowned++;
250         if (sect.sct_min > 9)
251             nmin++;
252         if (sect.sct_gmin > 9)
253             ngold++;
254         if (sect.sct_uran > 9)
255             nur++;
256         if (sect.sct_oil > 9)
257             noil++;
258     }
259     map[id] = 1;
260     for (dir = DIR_FIRST; dir <= DIR_LAST; dir++)
261         ok(map, diroff[dir][0] + x, diroff[dir][1] + y);
262 }