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