]> git.pond.sub.org Git - empserver/blob - src/lib/subs/getbit.c
Update copyright notice
[empserver] / src / lib / subs / getbit.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2018, 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  *  getbit.c: Replaces old bitmap code
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1989
31  */
32
33 #include <config.h>
34
35 #include "optlist.h"
36 #include "prototypes.h"
37 #include "sect.h"
38
39 /*
40  *
41  * the bit offsets for each bit pattern based on the efficiency of
42  * the sector.
43  *
44  * bitmap0:  0-20%
45  * bitmap1: 21-40%
46  * bitmap2: 41-60%
47  * bitmap3: 61-80%
48  * bitmap4: 81-100%
49  */
50
51 #define bitoff(x, y) x, y
52
53 static int bitmap0[] = {
54     bitoff(-1, -1), bitoff(1, -1),
55     bitoff(-2, 0), bitoff(0, 0), bitoff(2, 0),
56     bitoff(-1, 1), bitoff(1, 1),
57     bitoff(9999, 9999),
58 };
59
60 static int bitmap1[] = {
61     bitoff(0, -2),
62     bitoff(-3, -1), bitoff(-1, -1), bitoff(1, -1), bitoff(3, -1),
63     bitoff(-2, 0), bitoff(0, 0), bitoff(2, 0),
64     bitoff(-3, 1), bitoff(-1, 1), bitoff(1, 1), bitoff(3, 1),
65     bitoff(0, 2),
66     bitoff(9999, 9999),
67 };
68
69 static int bitmap2[] = {
70     bitoff(-2, -2), bitoff(0, -2), bitoff(2, -2),
71     bitoff(-3, -1), bitoff(-1, -1), bitoff(1, -1), bitoff(3, -1),
72     bitoff(-4, 0), bitoff(-2, 0), bitoff(0, 0), bitoff(2, 0), bitoff(4, 0),
73     bitoff(-3, 1), bitoff(-1, 1), bitoff(1, 1), bitoff(3, 1),
74     bitoff(-2, 2), bitoff(0, 2), bitoff(2, 2),
75     bitoff(9999, 9999),
76 };
77
78 static int bitmap3[] = {
79     bitoff(-1, -3), bitoff(1, -3),
80     bitoff(-4, -2), bitoff(-2, -2), bitoff(0, -2), bitoff(2, -2), bitoff(4,
81                                                                          -2),
82     bitoff(-5, -1), bitoff(-3, -1), bitoff(-1, -1), bitoff(1, -1),
83     bitoff(3, -1), bitoff(5, -1),
84     bitoff(-4, 0), bitoff(-2, 0), bitoff(0, 0), bitoff(2, 0), bitoff(4, 0),
85     bitoff(-5, 1), bitoff(-3, 1), bitoff(-1, 1), bitoff(1, 1),
86     bitoff(3, 1), bitoff(5, 1),
87     bitoff(-4, 2), bitoff(-2, 2), bitoff(0, 2), bitoff(2, 2), bitoff(4, 2),
88     bitoff(-1, 3), bitoff(1, 3),
89     bitoff(9999, 9999),
90 };
91
92 static int bitmap4[] = {
93     bitoff(-3, -3), bitoff(-1, -3), bitoff(1, -3), bitoff(3, -3),
94     bitoff(-4, -2), bitoff(-2, -2), bitoff(0, -2), bitoff(2, -2), bitoff(4,
95                                                                          -2),
96     bitoff(-5, -1), bitoff(-3, -1), bitoff(-1, -1), bitoff(1, -1),
97     bitoff(3, -1), bitoff(5, -1),
98     bitoff(-6, 0), bitoff(-4, 0), bitoff(-2, 0), bitoff(0, 0), bitoff(2,
99                                                                       0),
100     bitoff(4, 0), bitoff(6, 0),
101     bitoff(-5, 1), bitoff(-3, 1), bitoff(-1, 1), bitoff(1, 1),
102     bitoff(3, 1), bitoff(5, 1),
103     bitoff(-4, 2), bitoff(-2, 2), bitoff(0, 2), bitoff(2, 2), bitoff(4, 2),
104     bitoff(-3, 3), bitoff(-1, 3), bitoff(1, 3), bitoff(3, 3),
105     bitoff(9999, 9999),
106 };
107
108 static int *bitmaps[5] = {
109     bitmap0,
110     bitmap1,
111     bitmap2,
112     bitmap3,
113     bitmap4,
114 };
115
116 int
117 emp_getbit(int x, int y, unsigned char *bitmap)
118 {
119     int id = sctoff(x, y);
120     if (id < 0)
121         return 0;
122     return bitmap[id >> 3] & bit(id & 07);
123 }
124
125 void
126 emp_setbit(int x, int y, unsigned char *bitmap)
127 {
128     int id = sctoff(x, y);
129     if (id < 0)
130         return;
131     bitmap[id >> 3] |= bit(id & 07);
132 }
133
134 static void
135 emp_setbitmap(int x, int y, unsigned char *bitmap, int *bitmaps)
136 {
137     int *mp;
138     int id;
139     int dx, dy;
140
141     for (mp = bitmaps; *mp != 9999;) {
142         dx = x + *mp++;
143         dy = y + *mp++;
144         id = sctoff(dx, dy);
145         bitmap[id >> 3] |= bit(id & 07);
146     }
147 }
148
149 void
150 bitinit2(struct nstr_sect *np, unsigned char *bitmap, int country)
151 {
152     struct sctstr sect;
153     int eff;
154
155     while (nxtsct(np, &sect)) {
156         if (sect.sct_own != country)
157             continue;
158         eff = sect.sct_effic / 20;
159         if (eff > 4)
160             eff = 4;
161         emp_setbitmap(np->x, np->y, bitmap, bitmaps[eff]);
162     }
163     snxtsct_rewind(np);
164 }