]> 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-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  bp.c: The `build pointer' (bp) map
29  *
30  *  Known contributors to this file:
31  *     Ville Virrankoski, 1996
32  *     Markus Armbruster, 2007
33  */
34
35 /*
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
39  * map.
40  */
41
42 #include <config.h>
43
44 #include "budg.h"
45 #include "update.h"
46
47 /* Item types we want to track.  */
48 enum bp_item_idx {
49     BP_NONE = -1,               /* not tracked */
50     BP_MILIT, BP_LCM, BP_HCM,
51     BP_MAX = BP_HCM
52 };
53
54 /*
55  * Stuff to track for a sector.
56  * A bp map is an array of these.
57  */
58 struct bp {
59     short bp_item[BP_MAX + 1];
60     short bp_avail;
61 };
62
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
68 };
69
70 /* Return pointer to the element of BP belonging to SP.  */
71 static struct bp *
72 bp_ref(struct bp *bp, struct sctstr *sp)
73 {
74     return &bp[sp->sct_uid];
75 }
76
77 /*
78  * Return the item value tracked in BP for sector SP's item COMM.
79  * COMM must be a tracked item type.
80  */
81 int
82 bp_get_item(struct bp *bp, struct sctstr *sp, i_type comm)
83 {
84     enum bp_item_idx idx = bud_key[comm];
85
86     if (CANT_HAPPEN(idx < 0))
87         return sp->sct_item[comm];
88     return bp_ref(bp, sp)->bp_item[idx];
89 }
90
91 /* Set the item value tracked in BP for sector SP's item COMM.  */
92 void
93 bp_put_item(struct bp *bp, struct sctstr *sp, i_type comm, int amount)
94 {
95     enum bp_item_idx idx = bud_key[comm];
96
97     if (idx >= 0)
98         bp_ref(bp, sp)->bp_item[idx] = amount;
99 }
100
101 /* Set the item values tracked in BP for sector SP from VEC.  */
102 void
103 bp_put_items(struct bp *bp, struct sctstr *sp, short *vec)
104 {
105     enum bp_item_idx idx;
106     struct bp *p = bp_ref(bp, sp);
107     i_type i;
108
109     for (i = I_NONE + 1; i <= I_MAX; i++) {
110         idx = bud_key[i];
111         if (idx >= 0)
112             p->bp_item[idx] = vec[i];
113     }
114 }
115
116 /* Return avail tracked in BP for sector SP.  */
117 int
118 bp_get_avail(struct bp *bp, struct sctstr *sp)
119 {
120     return bp_ref(bp, sp)->bp_avail;
121 }
122
123 /* Set avail tracked in BP for sector SP.  */
124 void
125 bp_put_avail(struct bp *bp, struct sctstr *sp, int amount)
126 {
127     bp_ref(bp, sp)->bp_avail = amount;
128 }
129
130 /* Set the values tracked in BP for sector SP to the values in SP.  */
131 void
132 bp_set_from_sect(struct bp *bp, struct sctstr *sp)
133 {
134     bp_put_items(bp, sp, sp->sct_item);
135     bp_put_avail(bp, sp, sp->sct_avail);
136 }
137
138 /*
139  * Return a new bp map.
140  * Caller should pass it to free() when done with it.
141  */
142 struct bp *
143 bp_alloc(void)
144 {
145     return calloc(WORLD_SZ(), sizeof(struct bp));
146 }