]> git.pond.sub.org Git - empserver/blob - src/lib/subs/sect.c
subs: Simplify MOB_ACCESS mobility update
[empserver] / src / lib / subs / sect.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  sect.c: Sector pre-write and post-read data massage
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1989
31  *     Steve McClure, 1996
32  *     Markus Armbruster, 2004-2016
33  */
34
35 #include <config.h>
36
37 #include <ctype.h>
38 #include "file.h"
39 #include "game.h"
40 #include "lost.h"
41 #include "misc.h"
42 #include "nsc.h"
43 #include "optlist.h"
44 #include "player.h"
45 #include "prototypes.h"
46 #include "sect.h"
47 #include "server.h"
48 #include "update.h"
49 #include "xy.h"
50
51 void
52 sct_postread(int id, void *ptr)
53 {
54     struct sctstr *sp = ptr;
55
56     player->owner = (player->god || sp->sct_own == player->cnum);
57
58     if (opt_MOB_ACCESS && sp->sct_own && sp->sct_type != SCT_SANCT
59         && !update_running)
60         mob_inc_sect(sp, game_tick_to_now(&sp->sct_access));
61 }
62
63 void
64 sct_prewrite(int id, void *old, void *new)
65 {
66     struct sctstr *oldsp = old;
67     struct sctstr *sp = new;
68     coord x, y;
69     int mil, civs;
70     natid own, prev_own;
71
72     sctoff2xy(&x, &y, sp->sct_uid);
73     if (CANT_HAPPEN(sp->sct_x != x || sp->sct_y != y)) {
74         sp->sct_x = x;
75         sp->sct_y = y;
76     }
77
78     bridge_damaged(sp);
79     item_prewrite(sp->sct_item);
80
81     mil = sp->sct_item[I_MILIT];
82     civs = sp->sct_item[I_CIVIL];
83     own = sp->sct_own;
84     prev_own = oldsp->sct_own;
85
86     if (!civs) {
87         sp->sct_work = 100;
88         sp->sct_loyal = 0;
89         sp->sct_oldown = own;
90     }
91
92     /*
93      * Without civilians, military and land units, revert to deity.
94      * Note: would_abandon() must match this condition.
95      */
96     if (own && !civs && !mil && !has_units(sp->sct_x, sp->sct_y, own)
97         && !(sp->sct_flags & MOVE_IN_PROGRESS)) {
98         own = sp->sct_own = 0;
99         sp->sct_oldown = 0;
100         sp->sct_mobil = 0;
101         if (sp->sct_type == SCT_CAPIT || sp->sct_type == SCT_MOUNT)
102             caploss(sp, prev_own, "");
103     }
104
105     if (prev_own != own)
106         lost_and_found(EF_SECTOR, prev_own, own, 0, sp->sct_x, sp->sct_y);
107 }
108
109 void
110 item_prewrite(short *item)
111 {
112     i_type i;
113
114     for (i = I_NONE + 1; i <= I_MAX; ++i) {
115         if (CANT_HAPPEN(item[i] < 0))
116             item[i] = 0;
117         else if (CANT_HAPPEN(item[i] > ITEM_MAX))
118             item[i] = ITEM_MAX;
119     }
120 }
121
122 int
123 issector(char *arg)
124 {
125     char c;
126
127     while (0 != (c = *arg++))
128         if (!isdigit(c) && !isspace(c) && (c != '/'))
129             return 1;
130
131     return 0;
132 }