2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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.
28 * bp.c: The `build pointer' (bp) map
30 * Known contributors to this file:
31 * Ville Virrankoski, 1996
32 * Markus Armbruster, 2007
36 * Some commands call update functions to simulate the update.
37 * Naturally, these functions must not change game state then.
38 * Instead, they track values separate data structure, namely the bp
47 /* Item types we want to track. */
49 BP_NONE = -1, /* not tracked */
50 BP_MILIT, BP_LCM, BP_HCM,
55 * Stuff to track for a sector.
56 * A bp map is an array of these.
59 short bp_item[BP_MAX + 1];
63 /* Map i_type to enum bp_item_idx. */
64 static enum bp_item_idx bud_key[I_MAX + 1] = {
65 BP_NONE, BP_MILIT, BP_NONE, BP_NONE,
66 BP_NONE, BP_NONE, BP_NONE, BP_NONE, BP_NONE, BP_NONE,
67 BP_LCM, BP_HCM, BP_NONE, BP_NONE
70 /* Return pointer to the element of BP belonging to SP. */
72 bp_ref(struct bp *bp, struct sctstr *sp)
74 return &bp[sp->sct_x + sp->sct_y * WORLD_X];
78 * Return the item value tracked in BP for sector SP's item COMM.
79 * COMM must be a tracked item type.
82 bp_get_item(struct bp *bp, struct sctstr *sp, i_type comm)
84 enum bp_item_idx idx = bud_key[comm];
86 if (CANT_HAPPEN(idx < 0))
87 return sp->sct_item[comm];
88 return bp_ref(bp, sp)->bp_item[idx];
91 /* Set the item value tracked in BP for sector SP's item COMM. */
93 bp_put_item(struct bp *bp, struct sctstr *sp, i_type comm, int amount)
95 enum bp_item_idx idx = bud_key[comm];
98 bp_ref(bp, sp)->bp_item[idx] = amount;
101 /* Set the item values tracked in BP for sector SP from VEC. */
103 bp_put_items(struct bp *bp, struct sctstr *sp, short *vec)
105 enum bp_item_idx idx;
106 struct bp *p = bp_ref(bp, sp);
109 for (i = I_NONE + 1; i <= I_MAX; i++) {
112 p->bp_item[idx] = sp->sct_item[i];
116 /* Return avail tracked in BP for sector SP. */
118 bp_get_avail(struct bp *bp, struct sctstr *sp)
120 return bp_ref(bp, sp)->bp_avail;
123 /* Set avail tracked in BP for sector SP. */
125 bp_put_avail(struct bp *bp, struct sctstr *sp, int amount)
127 bp_ref(bp, sp)->bp_avail = amount;
130 /* Set the values tracked in BP for sector SP to the values in SP. */
132 bp_set_from_sect(struct bp *bp, struct sctstr *sp)
134 bp_put_items(bp, sp, sp->sct_item);
135 bp_put_avail(bp, sp, sp->sct_avail);
139 * Return a new bp map.
140 * Caller should pass it to free() when done with it.
145 return calloc(WORLD_X * WORLD_Y, sizeof(struct bp));