]> git.pond.sub.org Git - empserver/blob - src/lib/update/bp.c
include: Rename budg.h to update.h
[empserver] / src / lib / update / bp.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, 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 "optlist.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     unsigned char bp_tracked;
62 };
63
64 /* Map i_type to enum bp_item_idx. */
65 static enum bp_item_idx bud_key[I_MAX + 1] = {
66     BP_NONE, BP_MILIT, BP_NONE, BP_NONE,
67     BP_NONE, BP_NONE, BP_NONE, BP_NONE, BP_NONE, BP_NONE,
68     BP_LCM, BP_HCM, BP_NONE, BP_NONE
69 };
70
71 /* Set the values tracked in @bp for sector @sp to the values in @sp. */
72 void
73 bp_set_from_sect(struct bp *bp, struct sctstr *sp)
74 {
75     i_type i;
76     enum bp_item_idx idx;
77
78     if (!bp)
79         return;
80     for (i = I_NONE + 1; i <= I_MAX; i++) {
81         idx = bud_key[i];
82         if (idx >= 0)
83             bp[sp->sct_uid].bp_item[idx] = sp->sct_item[i];
84     }
85     bp[sp->sct_uid].bp_avail = sp->sct_avail;
86     bp[sp->sct_uid].bp_tracked = 1;
87 }
88
89 /*
90  * Copy the values tracked in @bp for sector @sp back to @sp.
91  * Values must have been set with bp_set_from_sect().
92  */
93 void
94 bp_to_sect(struct bp *bp, struct sctstr *sp)
95 {
96     i_type i;
97     enum bp_item_idx idx;
98
99     if (CANT_HAPPEN(!bp[sp->sct_uid].bp_tracked))
100         return;
101
102     for (i = I_NONE + 1; i <= I_MAX; i++) {
103         idx = bud_key[i];
104         if (idx >= 0)
105             sp->sct_item[i] = bp[sp->sct_uid].bp_item[idx];
106     }
107     sp->sct_avail = bp[sp->sct_uid].bp_avail;
108 }
109
110 /*
111  * Return a new bp map.
112  * Caller should pass it to free() when done with it.
113  */
114 struct bp *
115 bp_alloc(void)
116 {
117     int n = WORLD_SZ();
118     struct bp *bp = malloc(n * sizeof(*bp));
119     int i;
120
121     for (i = 0; i < n; i++)
122         bp[i].bp_tracked = 0;
123     return bp;
124 }