]> git.pond.sub.org Git - empserver/blob - src/lib/subs/sect.c
Import of Empire 4.2.12
[empserver] / src / lib / subs / sect.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 "var.h"
38 #include "sect.h"
39 #include "xy.h"
40 #include "nat.h"
41 #include "deity.h"
42 #include "file.h"
43 #include "land.h"
44 #include "nsc.h"
45 #include "plane.h"
46 #include "prototypes.h"
47 #include "optlist.h"
48
49 static void give_back_cap(struct sctstr *sp);
50
51 /*ARGSUSED*/
52 int
53 sct_postread(int id, s_char *ptr)
54 {
55         struct  sctstr *sp = (struct sctstr *) ptr;
56
57         checksect(sp);
58         player->owner = (player->god || sp->sct_own == player->cnum);
59         if (opt_MOB_ACCESS)
60           sct_do_upd_mob(sp);
61         return 1;
62 }
63
64 /*ARGSUSED*/
65 int
66 sct_prewrite(int id, s_char *ptr)
67 {
68         struct  sctstr *sp = (struct sctstr *) ptr;
69         struct  sctstr sect;
70
71         time(&sp->sct_timestamp);
72
73         checksect(sp);
74         getsect(sp->sct_x, sp->sct_y, &sect);
75         return 1;
76 }
77
78 int
79 checksect(struct sctstr *sp)
80 {
81         int     mil, civs, loyalcivs;
82
83         /* shouldn't happen, but... */
84         if (sp->sct_mobil > 127)
85                 sp->sct_mobil = 0;
86
87         mil = getvar(V_MILIT, (s_char *)sp, EF_SECTOR);
88         civs = getvar(V_CIVIL, (s_char *)sp, EF_SECTOR);
89         if (sp->sct_own == sp->sct_oldown)
90                 loyalcivs = civs;
91         else
92                 loyalcivs = 0;
93
94         if (sp->sct_effic < 20) {
95             if (sp->sct_type == SCT_BSPAN)
96                 knockdown(sp, 0);
97             else if (sp->sct_type == SCT_BTOWER) {
98                 knockdown(sp, 0);
99                 bridgefall(sp, 0);
100             }
101         }
102         if (sp->sct_own != 0 && !civs) {
103                 sp->sct_work = 100;
104                 sp->sct_oldown = sp->sct_own;
105         }
106         /* If they have a military unit there, they still own it */
107         if (sp->sct_own && !loyalcivs && !(sp->sct_flags & MOVE_IN_PROGRESS)) {
108                 if (!mil && !has_units(sp->sct_x,sp->sct_y,sp->sct_own,0)) {
109                         /* more cruft! */
110                         if (sp->sct_type == SCT_CAPIT ||
111                             sp->sct_type == SCT_MOUNT)
112                                 caploss(sp, sp->sct_own, "");
113
114                         if (sp->sct_oldown == sp->sct_own) {
115                                 makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
116                                 sp->sct_own = 0;
117                                 sp->sct_oldown = 0;
118                         } else {
119                                 /* if oldown gets his cap back make agri */
120                                 if (sp->sct_oldown &&
121                                     player->cnum == sp->sct_own &&
122                                     sp->sct_type == SCT_CAPIT &&
123                                     sp->sct_newtype == SCT_CAPIT)
124                                         give_back_cap(sp);
125                                 takeover(sp, sp->sct_oldown);
126                         }
127                         sp->sct_mobil = 0;
128                 }
129         }
130         if (!opt_DEFENSE_INFRA)
131             sp->sct_defense = sp->sct_effic;
132         return 1;
133 }
134
135 int
136 issector(s_char *arg)
137 {
138         s_char c;
139
140         while (0 != (c = *arg++))
141                 if (!isdigit(c) && !isspace(c) && (c != '/'))
142                         return 1;
143
144         return 0;
145 }
146
147 static void
148 give_back_cap(struct sctstr *sp)
149 {
150         struct  natstr *natp = getnatp(sp->sct_oldown);
151
152         if (xrel(natp,natp->nat_xcap) == xrel(natp,sp->sct_x) &&
153             yrel(natp,natp->nat_ycap) == yrel(natp,sp->sct_y)) {
154                 sp->sct_newtype = SCT_AGRI;
155         }
156 }
157
158 void
159 sct_init(coord x, coord y, s_char *ptr)
160 {
161         struct  sctstr *sp = (struct sctstr *) ptr;
162
163         sp->ef_type = EF_SECTOR;
164         sp->sct_x = x;
165         sp->sct_y = y;
166         sp->sct_dist_x = x;
167         sp->sct_dist_y = y;
168 }