]> git.pond.sub.org Git - empserver/blob - src/lib/update/bp.c
Update copyright notice
[empserver] / src / lib / update / bp.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2018, 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  *  bp.c: The `build pointer' (bp) map
28  *
29  *  Known contributors to this file:
30  *     Ville Virrankoski, 1996
31  *     Markus Armbruster, 2007-2016
32  */
33
34 /*
35  * Some commands call update functions to simulate the update.
36  * Naturally, these functions must not change game state then.
37  * Instead, they track values separate data structure, namely the bp
38  * map.
39  */
40
41 #include <config.h>
42
43 #include <stdlib.h>
44 #include "empobj.h"
45 #include "optlist.h"
46 #include "player.h"
47 #include "prototypes.h"
48 #include "update.h"
49 #include "xy.h"
50
51 /* Item types we want to track. */
52 enum bp_item_idx {
53     BP_NONE = -1,               /* not tracked */
54     BP_MILIT, BP_LCM, BP_HCM,
55     BP_MAX = BP_HCM
56 };
57
58 enum bp_status {
59     BP_UNUSED,                  /* not tracked, values are invalid */
60     BP_WANTED,                  /* tracked, values are still invalid */
61     BP_USED                     /* tracked, values are valid */
62 };
63
64 /*
65  * Stuff to track for a sector.
66  * A bp map is an array of these.
67  */
68 struct bp {
69     short bp_item[BP_MAX + 1];
70     short bp_avail;
71     unsigned char bp_status;
72 };
73
74 /* Map i_type to enum bp_item_idx. */
75 static enum bp_item_idx bud_key[I_MAX + 1] = {
76     BP_NONE, BP_MILIT, BP_NONE, BP_NONE,
77     BP_NONE, BP_NONE, BP_NONE, BP_NONE, BP_NONE, BP_NONE,
78     BP_LCM, BP_HCM, BP_NONE, BP_NONE
79 };
80
81 /* Return true when @bp doesn't track @sp. */
82 int
83 bp_skip_sect(struct bp *bp, struct sctstr *sp)
84 {
85     return bp && bp[sp->sct_uid].bp_status == BP_UNUSED;
86 }
87
88 /* Return true when @bp doesn't track @unit's sector. */
89 int
90 bp_skip_unit(struct bp *bp, struct empobj *unit)
91 {
92     return bp && bp[XYOFFSET(unit->x, unit->y)].bp_status == BP_UNUSED;
93 }
94
95 /* If @unit belongs to the player, start tracking its sector in @bp. */
96 void
97 bp_consider_unit(struct bp *bp, struct empobj *unit)
98 {
99     int id;
100
101     if (!bp || unit->own != player->cnum)
102         return;
103     id = XYOFFSET(unit->x, unit->y);
104     if (bp[id].bp_status == BP_UNUSED)
105         bp[id].bp_status = BP_WANTED;
106 }
107
108 /* Set the values tracked in @bp for sector @sp to the values in @sp. */
109 void
110 bp_set_from_sect(struct bp *bp, struct sctstr *sp)
111 {
112     i_type i;
113     enum bp_item_idx idx;
114
115     if (!bp)
116         return;
117     for (i = I_NONE + 1; i <= I_MAX; i++) {
118         idx = bud_key[i];
119         if (idx >= 0)
120             bp[sp->sct_uid].bp_item[idx] = sp->sct_item[i];
121     }
122     bp[sp->sct_uid].bp_avail = sp->sct_avail;
123     bp[sp->sct_uid].bp_status = BP_USED;
124 }
125
126 /*
127  * Copy the values tracked in @bp for sector @sp back to it.
128  * Values must have been set with bp_set_from_sect().
129  */
130 void
131 bp_to_sect(struct bp *bp, struct sctstr *sp)
132 {
133     i_type i;
134     enum bp_item_idx idx;
135
136     if (CANT_HAPPEN(bp[sp->sct_uid].bp_status != BP_USED))
137         return;
138
139     for (i = I_NONE + 1; i <= I_MAX; i++) {
140         idx = bud_key[i];
141         if (idx >= 0)
142             sp->sct_item[i] = bp[sp->sct_uid].bp_item[idx];
143     }
144     sp->sct_avail = bp[sp->sct_uid].bp_avail;
145 }
146
147 /*
148  * Return a new bp map.
149  * Caller should pass it to free() when done with it.
150  * The map initially tracks the sectors belonging to the player.
151  */
152 struct bp *
153 bp_alloc(void)
154 {
155     int n = WORLD_SZ();
156     struct bp *bp = malloc(n * sizeof(*bp));
157     int i;
158
159     for (i = 0; i < n; i++)
160         bp[i].bp_status = getsectid(i)->sct_own == player->cnum
161             ? BP_WANTED : BP_UNUSED;
162
163     return bp;
164 }