]> git.pond.sub.org Git - empserver/blob - src/lib/subs/sect.c
053214fe5658dc01e827efd53d3f9620a974a8ea
[empserver] / src / lib / subs / sect.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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  *  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 <config.h>
36
37 #include <ctype.h>
38 #include "file.h"
39 #include "land.h"
40 #include "lost.h"
41 #include "misc.h"
42 #include "nat.h"
43 #include "nsc.h"
44 #include "optlist.h"
45 #include "plane.h"
46 #include "player.h"
47 #include "prototypes.h"
48 #include "sect.h"
49 #include "xy.h"
50
51 static int checksect(struct sctstr *);
52
53 void
54 sct_postread(int id, void *ptr)
55 {
56     struct sctstr *sp = ptr;
57
58     checksect(sp);
59     player->owner = (player->god || sp->sct_own == player->cnum);
60     if (opt_MOB_ACCESS)
61         sct_do_upd_mob(sp);
62 }
63
64 void
65 sct_prewrite(int id, void *ptr)
66 {
67     struct sctstr *sp = ptr;
68     struct sctstr sect;
69
70     bridge_damaged(sp);
71     checksect(sp);
72     getsect(sp->sct_x, sp->sct_y, &sect);
73 }
74
75 void
76 item_prewrite(short *item)
77 {
78     i_type i;
79
80     for (i = I_NONE + 1; i <= I_MAX; ++i) {
81         if (CANT_HAPPEN(item[i] < 0))
82             item[i] = 0;
83         else if (CANT_HAPPEN(item[i] > ITEM_MAX))
84             item[i] = ITEM_MAX;
85     }
86 }
87
88 static int
89 checksect(struct sctstr *sp)
90 {
91     int mil, civs, loyalcivs;
92     natid own;
93
94     item_prewrite(sp->sct_item);
95
96     /* shouldn't happen, but... */
97     if (sp->sct_mobil > 127)
98         sp->sct_mobil = 0;
99
100     mil = sp->sct_item[I_MILIT];
101     civs = sp->sct_item[I_CIVIL];
102     if (sp->sct_own == sp->sct_oldown)
103         loyalcivs = civs;
104     else
105         loyalcivs = 0;
106
107     if (sp->sct_own != 0 && !civs) {
108         sp->sct_work = 100;
109         sp->sct_oldown = sp->sct_own;
110     }
111     /* If they have a military unit there, they still own it */
112     if (sp->sct_own && !loyalcivs && !(sp->sct_flags & MOVE_IN_PROGRESS)) {
113         if (!mil && !has_units(sp->sct_x, sp->sct_y, sp->sct_own, 0)) {
114             /* more cruft! */
115             own = sp->sct_own;
116             if (sp->sct_oldown == sp->sct_own) {
117                 makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
118                 sp->sct_own = 0;
119                 sp->sct_oldown = 0;
120             } else
121                 takeover(sp, sp->sct_oldown);
122             sp->sct_mobil = 0;
123             if (sp->sct_type == SCT_CAPIT || sp->sct_type == SCT_MOUNT)
124                 caploss(sp, own, "");
125         }
126     }
127     return 1;
128 }
129
130 int
131 issector(char *arg)
132 {
133     char c;
134
135     while (0 != (c = *arg++))
136         if (!isdigit(c) && !isspace(c) && (c != '/'))
137             return 1;
138
139     return 0;
140 }