]> 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-2006, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "misc.h"
37 #include "player.h"
38 #include "xy.h"
39 #include "nsc.h"
40 #include "sect.h"
41 #include "nat.h"
42 #include "prototypes.h"
43 #include "optlist.h"
44
45
46
47 /*
48  *
49  * the bit offsets for each bit pattern based on the efficiency of
50  * the sector.
51  *
52  * bitmap0:  0-20%
53  * bitmap1: 21-40%
54  * bitmap2: 41-60%
55  * bitmap3: 61-80%
56  * bitmap4: 81-100%
57  */
58
59 #define bitoff(x, y) x, y
60
61 static int bitmap0[] = {
62     bitoff(-1, -1), bitoff(1, -1),
63     bitoff(-2, 0), bitoff(0, 0), bitoff(2, 0),
64     bitoff(-1, 1), bitoff(1, 1),
65     bitoff(9999, 9999),
66 };
67
68 static int bitmap1[] = {
69     bitoff(0, -2),
70     bitoff(-3, -1), bitoff(-1, -1), bitoff(1, -1), bitoff(3, -1),
71     bitoff(-2, 0), bitoff(0, 0), bitoff(2, 0),
72     bitoff(-3, 1), bitoff(-1, 1), bitoff(1, 1), bitoff(3, 1),
73     bitoff(0, 2),
74     bitoff(9999, 9999),
75 };
76
77 static int bitmap2[] = {
78     bitoff(-2, -2), bitoff(0, -2), bitoff(2, -2),
79     bitoff(-3, -1), bitoff(-1, -1), bitoff(1, -1), bitoff(3, -1),
80     bitoff(-4, 0), bitoff(-2, 0), bitoff(0, 0), bitoff(2, 0), bitoff(4, 0),
81     bitoff(-3, 1), bitoff(-1, 1), bitoff(1, 1), bitoff(3, 1),
82     bitoff(-2, 2), bitoff(0, 2), bitoff(2, 2),
83     bitoff(9999, 9999),
84 };
85
86 static int bitmap3[] = {
87     bitoff(-1, -3), bitoff(1, -3),
88     bitoff(-4, -2), bitoff(-2, -2), bitoff(0, -2), bitoff(2, -2), bitoff(4,
89                                                                          -2),
90     bitoff(-5, -1), bitoff(-3, -1), bitoff(-1, -1), bitoff(1, -1),
91     bitoff(3, -1), bitoff(5, -1),
92     bitoff(-4, 0), bitoff(-2, 0), bitoff(0, 0), bitoff(2, 0), bitoff(4, 0),
93     bitoff(-5, 1), bitoff(-3, 1), bitoff(-1, 1), bitoff(1, 1),
94     bitoff(3, 1), bitoff(5, 1),
95     bitoff(-4, 2), bitoff(-2, 2), bitoff(0, 2), bitoff(2, 2), bitoff(4, 2),
96     bitoff(-1, 3), bitoff(1, 3),
97     bitoff(9999, 9999),
98 };
99
100 static int bitmap4[] = {
101     bitoff(-3, -3), bitoff(-1, -3), bitoff(1, -3), bitoff(3, -3),
102     bitoff(-4, -2), bitoff(-2, -2), bitoff(0, -2), bitoff(2, -2), bitoff(4,
103                                                                          -2),
104     bitoff(-5, -1), bitoff(-3, -1), bitoff(-1, -1), bitoff(1, -1),
105     bitoff(3, -1), bitoff(5, -1),
106     bitoff(-6, 0), bitoff(-4, 0), bitoff(-2, 0), bitoff(0, 0), bitoff(2,
107                                                                       0),
108     bitoff(4, 0), bitoff(6, 0),
109     bitoff(-5, 1), bitoff(-3, 1), bitoff(-1, 1), bitoff(1, 1),
110     bitoff(3, 1), bitoff(5, 1),
111     bitoff(-4, 2), bitoff(-2, 2), bitoff(0, 2), bitoff(2, 2), bitoff(4, 2),
112     bitoff(-3, 3), bitoff(-1, 3), bitoff(1, 3), bitoff(3, 3),
113     bitoff(9999, 9999),
114 };
115
116 static int *bitmaps[5] = {
117     bitmap0,
118     bitmap1,
119     bitmap2,
120     bitmap3,
121     bitmap4,
122 };
123
124 #define GCFx(x) ((x + WORLD_X) % WORLD_X)
125 #define GCFy(y) ((y + WORLD_Y) % WORLD_Y)
126
127 int
128 emp_getbit(int x, int y, u_char *bitmap)
129 {
130     int id;
131
132     id = (GCFy(y)) * WORLD_X / 2 + GCFx(x) / 2;
133     return bitmap[id / 8] & bit(id & 07);
134 }
135
136 void
137 emp_setbit(int x, int y, u_char *bitmap)
138 {
139     register int id;
140
141     id = (GCFy(y)) * WORLD_X / 2 + GCFx(x) / 2;
142     bitmap[id / 8] |= bit(id & 07);
143 }
144
145 void
146 emp_setbitmap(register int x, register int y, register u_char *bitmap,
147               int *bitmaps)
148 {
149     register int *mp;
150     register int id;
151     register int dx, dy;
152
153     for (mp = bitmaps; *mp != 9999;) {
154         dx = x + *mp++;
155         dy = y + *mp++;
156         id = (GCFy(dy)) * WORLD_X / 2 + GCFx(dx) / 2;
157         bitmap[id / 8] |= bit(id & 07);
158     }
159 }
160
161 void
162 bitinit2(struct nstr_sect *np, u_char *bitmap, int country)
163 {
164     struct sctstr sect;
165     int eff;
166
167     while (nxtsct(np, &sect)) {
168         if (sect.sct_own != country)
169             continue;
170         eff = sect.sct_effic / 20;
171         if (eff > 4)
172             eff = 4;
173         emp_setbitmap(np->x, np->y, bitmap, bitmaps[eff]);
174     }
175     snxtsct_rewind(np);
176 }