]> git.pond.sub.org Git - empserver/blob - src/lib/subs/sect.c
(i_type, I_NONE): shift the enum down 1, so that I_NONE does not need to
[empserver] / src / lib / subs / sect.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, 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  *  sect.c: Sector pre-write and post-read data massage
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  *     Steve McClure, 1996
33  */
34
35 #include "misc.h"
36 #include "player.h"
37 #include "sect.h"
38 #include "xy.h"
39 #include "nat.h"
40 #include "file.h"
41 #include "land.h"
42 #include "nsc.h"
43 #include "plane.h"
44 #include "prototypes.h"
45 #include "optlist.h"
46
47 static int checksect(struct sctstr *);
48 static void give_back_cap(struct sctstr *sp);
49
50 /*ARGSUSED*/
51 int
52 sct_postread(int id, s_char *ptr)
53 {
54     struct sctstr *sp = (struct sctstr *)ptr;
55
56     checksect(sp);
57     player->owner = (player->god || sp->sct_own == player->cnum);
58     if (opt_MOB_ACCESS)
59         sct_do_upd_mob(sp);
60     return 1;
61 }
62
63 /*ARGSUSED*/
64 int
65 sct_prewrite(int id, s_char *ptr)
66 {
67     struct sctstr *sp = (struct sctstr *)ptr;
68     struct sctstr sect;
69
70     time(&sp->sct_timestamp);
71
72     checksect(sp);
73     getsect(sp->sct_x, sp->sct_y, &sect);
74     return 1;
75 }
76
77 void
78 item_prewrite(short *item)
79 {
80     i_type i;
81
82     for (i = I_NONE + 1; i <= I_MAX; ++i) {
83         if (CANT_HAPPEN(item[i] < 0))
84             item[i] = 0;
85         else if (CANT_HAPPEN(item[i] > ITEM_MAX))
86             item[i] = ITEM_MAX;
87     }
88 }
89
90 static int
91 checksect(struct sctstr *sp)
92 {
93     int mil, civs, loyalcivs;
94
95     item_prewrite(sp->sct_item);
96
97     /* shouldn't happen, but... */
98     if (sp->sct_mobil > 127)
99         sp->sct_mobil = 0;
100
101     mil = sp->sct_item[I_MILIT];
102     civs = sp->sct_item[I_CIVIL];
103     if (sp->sct_own == sp->sct_oldown)
104         loyalcivs = civs;
105     else
106         loyalcivs = 0;
107
108     if (sp->sct_effic < SCT_MINEFF) {
109         if (sp->sct_type == SCT_BSPAN)
110             knockdown(sp, 0);
111         else if (sp->sct_type == SCT_BTOWER) {
112             knockdown(sp, 0);
113             bridgefall(sp, 0);
114         }
115     }
116     if (sp->sct_own != 0 && !civs) {
117         sp->sct_work = 100;
118         sp->sct_oldown = sp->sct_own;
119     }
120     /* If they have a military unit there, they still own it */
121     if (sp->sct_own && !loyalcivs && !(sp->sct_flags & MOVE_IN_PROGRESS)) {
122         if (!mil && !has_units(sp->sct_x, sp->sct_y, sp->sct_own, 0)) {
123             /* more cruft! */
124             if (sp->sct_type == SCT_CAPIT || sp->sct_type == SCT_MOUNT)
125                 caploss(sp, sp->sct_own, "");
126
127             if (sp->sct_oldown == sp->sct_own) {
128                 makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
129                 sp->sct_own = 0;
130                 sp->sct_oldown = 0;
131             } else {
132                 /* if oldown gets his cap back make agri */
133                 if (sp->sct_oldown &&
134                     player->cnum == sp->sct_own &&
135                     sp->sct_type == SCT_CAPIT &&
136                     sp->sct_newtype == SCT_CAPIT)
137                     give_back_cap(sp);
138                 takeover(sp, sp->sct_oldown);
139             }
140             sp->sct_mobil = 0;
141         }
142     }
143     if (!opt_DEFENSE_INFRA)
144         sp->sct_defense = sp->sct_effic;
145     return 1;
146 }
147
148 int
149 issector(s_char *arg)
150 {
151     s_char c;
152
153     while (0 != (c = *arg++))
154         if (!isdigit(c) && !isspace(c) && (c != '/'))
155             return 1;
156
157     return 0;
158 }
159
160 static void
161 give_back_cap(struct sctstr *sp)
162 {
163     struct natstr *natp = getnatp(sp->sct_oldown);
164
165     if (xrel(natp, natp->nat_xcap) == xrel(natp, sp->sct_x) &&
166         yrel(natp, natp->nat_ycap) == yrel(natp, sp->sct_y)) {
167         sp->sct_newtype = SCT_AGRI;
168     }
169 }
170
171 void
172 sct_init(coord x, coord y, s_char *ptr)
173 {
174     struct sctstr *sp = (struct sctstr *)ptr;
175
176     sp->ef_type = EF_SECTOR;
177     sp->sct_x = x;
178     sp->sct_y = y;
179     sp->sct_dist_x = x;
180     sp->sct_dist_y = y;
181 }