]> git.pond.sub.org Git - empserver/blob - src/lib/update/bp.c
Fix and clean up some comments
[empserver] / src / lib / update / bp.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2015, 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
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 "budg.h"
44 #include "update.h"
45
46 /* Item types we want to track. */
47 enum bp_item_idx {
48     BP_NONE = -1,               /* not tracked */
49     BP_MILIT, BP_LCM, BP_HCM,
50     BP_MAX = BP_HCM
51 };
52
53 /*
54  * Stuff to track for a sector.
55  * A bp map is an array of these.
56  */
57 struct bp {
58     short bp_item[BP_MAX + 1];
59     short bp_avail;
60 };
61
62 /* Map i_type to enum bp_item_idx. */
63 static enum bp_item_idx bud_key[I_MAX + 1] = {
64     BP_NONE, BP_MILIT, BP_NONE, BP_NONE,
65     BP_NONE, BP_NONE, BP_NONE, BP_NONE, BP_NONE, BP_NONE,
66     BP_LCM, BP_HCM, BP_NONE, BP_NONE
67 };
68
69 /* Return pointer to the element of BP belonging to SP. */
70 static struct bp *
71 bp_ref(struct bp *bp, struct sctstr *sp)
72 {
73     return &bp[sp->sct_uid];
74 }
75
76 /*
77  * Return the item value tracked in @bp for sector @sp's item @comm.
78  * @comm must be a tracked item type.
79  */
80 int
81 bp_get_item(struct bp *bp, struct sctstr *sp, i_type comm)
82 {
83     enum bp_item_idx idx = bud_key[comm];
84
85     if (CANT_HAPPEN(idx < 0))
86         return sp->sct_item[comm];
87     return bp_ref(bp, sp)->bp_item[idx];
88 }
89
90 /*
91  * Set item value tracked in @bp for sector @sp's item @comm to @amount.
92  */
93 void
94 bp_put_item(struct bp *bp, struct sctstr *sp, i_type comm, int amount)
95 {
96     enum bp_item_idx idx = bud_key[comm];
97
98     if (idx >= 0)
99         bp_ref(bp, sp)->bp_item[idx] = amount;
100 }
101
102 /* Set the item values tracked in @bp for sector @sp from @vec. */
103 void
104 bp_put_items(struct bp *bp, struct sctstr *sp, short *vec)
105 {
106     enum bp_item_idx idx;
107     struct bp *p = bp_ref(bp, sp);
108     i_type i;
109
110     for (i = I_NONE + 1; i <= I_MAX; i++) {
111         idx = bud_key[i];
112         if (idx >= 0)
113             p->bp_item[idx] = vec[i];
114     }
115 }
116
117 /* Return avail tracked in @bp for sector @sp. */
118 int
119 bp_get_avail(struct bp *bp, struct sctstr *sp)
120 {
121     return bp_ref(bp, sp)->bp_avail;
122 }
123
124 /* Set avail tracked in @bp for sector @sp to @amount. */
125 void
126 bp_put_avail(struct bp *bp, struct sctstr *sp, int amount)
127 {
128     bp_ref(bp, sp)->bp_avail = amount;
129 }
130
131 /* Set the values tracked in @bp for sector @sp to the values in @sp. */
132 void
133 bp_set_from_sect(struct bp *bp, struct sctstr *sp)
134 {
135     bp_put_items(bp, sp, sp->sct_item);
136     bp_put_avail(bp, sp, sp->sct_avail);
137 }
138
139 /*
140  * Return a new bp map.
141  * Caller should pass it to free() when done with it.
142  */
143 struct bp *
144 bp_alloc(void)
145 {
146     return calloc(WORLD_SZ(), sizeof(struct bp));
147 }