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