2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2017, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
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.
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.
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/>.
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.
27 * bp.c: The `build pointer' (bp) map
29 * Known contributors to this file:
30 * Ville Virrankoski, 1996
31 * Markus Armbruster, 2007-2016
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
47 #include "prototypes.h"
51 /* Item types we want to track. */
53 BP_NONE = -1, /* not tracked */
54 BP_MILIT, BP_LCM, BP_HCM,
59 BP_UNUSED, /* not tracked, values are invalid */
60 BP_WANTED, /* tracked, values are still invalid */
61 BP_USED /* tracked, values are valid */
65 * Stuff to track for a sector.
66 * A bp map is an array of these.
69 short bp_item[BP_MAX + 1];
71 unsigned char bp_status;
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
81 /* Return true when @bp doesn't track @sp. */
83 bp_skip_sect(struct bp *bp, struct sctstr *sp)
85 return bp && bp[sp->sct_uid].bp_status == BP_UNUSED;
88 /* Return true when @bp doesn't track @unit's sector. */
90 bp_skip_unit(struct bp *bp, struct empobj *unit)
92 return bp && bp[XYOFFSET(unit->x, unit->y)].bp_status == BP_UNUSED;
95 /* If @unit belongs to the player, start tracking its sector in @bp. */
97 bp_consider_unit(struct bp *bp, struct empobj *unit)
101 if (!bp || unit->own != player->cnum)
103 id = XYOFFSET(unit->x, unit->y);
104 if (bp[id].bp_status == BP_UNUSED)
105 bp[id].bp_status = BP_WANTED;
108 /* Set the values tracked in @bp for sector @sp to the values in @sp. */
110 bp_set_from_sect(struct bp *bp, struct sctstr *sp)
113 enum bp_item_idx idx;
117 for (i = I_NONE + 1; i <= I_MAX; i++) {
120 bp[sp->sct_uid].bp_item[idx] = sp->sct_item[i];
122 bp[sp->sct_uid].bp_avail = sp->sct_avail;
123 bp[sp->sct_uid].bp_status = BP_USED;
127 * Copy the values tracked in @bp for sector @sp back to it.
128 * Values must have been set with bp_set_from_sect().
131 bp_to_sect(struct bp *bp, struct sctstr *sp)
134 enum bp_item_idx idx;
136 if (CANT_HAPPEN(bp[sp->sct_uid].bp_status != BP_USED))
139 for (i = I_NONE + 1; i <= I_MAX; i++) {
142 sp->sct_item[i] = bp[sp->sct_uid].bp_item[idx];
144 sp->sct_avail = bp[sp->sct_uid].bp_avail;
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.
156 struct bp *bp = malloc(n * sizeof(*bp));
159 for (i = 0; i < n; i++)
160 bp[i].bp_status = getsectid(i)->sct_own == player->cnum
161 ? BP_WANTED : BP_UNUSED;