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