]> git.pond.sub.org Git - empserver/blob - src/lib/subs/getbit.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / subs / getbit.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 #define GCFx(x) ((x + WORLD_X) % WORLD_X)
44 #define GCFy(y) ((y + WORLD_Y) % WORLD_Y)
45
46 int
47 emp_getbit(int x, int y, u_char *bitmap)
48 {
49     int id;
50
51     id = (GCFy(y)) * WORLD_X / 2 + GCFx(x) / 2;
52     return bitmap[id / 8] & bit(id & 07);
53 }
54
55 void
56 emp_setbit(int x, int y, u_char *bitmap)
57 {
58     register int id;
59
60     id = (GCFy(y)) * WORLD_X / 2 + GCFx(x) / 2;
61     bitmap[id / 8] |= bit(id & 07);
62 }
63
64 void
65 emp_setbitmap(register int x, register int y, register u_char *bitmap,
66               int *bitmaps)
67 {
68     register int *mp;
69     register int id;
70     register int dx, dy;
71
72     for (mp = bitmaps; *mp != 9999;) {
73         dx = x + *mp++;
74         dy = y + *mp++;
75         id = (GCFy(dy)) * WORLD_X / 2 + GCFx(dx) / 2;
76         bitmap[id / 8] |= bit(id & 07);
77     }
78 }
79
80
81 /*
82  * if we're not player->god, we have to set up the bitmaps
83  * correctly for the owned sectors.  If we are player->god,
84  * forego all this (expensive) nonsense!
85  */
86 void
87 emp_bitinit(struct nstr_sect *np, u_char *bitmap)
88 {
89     extern int *bitmaps[];
90     struct sctstr sect;
91     int eff;
92
93     while (nxtsct(np, &sect)) {
94         if (!player->owner)
95             continue;
96         eff = sect.sct_effic / 20;
97         if (eff > 4)
98             eff = 4;
99         emp_setbitmap(np->x, np->y, bitmap, bitmaps[eff]);
100     }
101     snxtsct_rewind(np);
102 }
103
104 /*
105  *
106  * the bit offsets for each bit pattern based on the efficiency of
107  * the sector.
108  *
109  * bitmap0:  0-20%
110  * bitmap1: 21-40%
111  * bitmap2: 41-60%
112  * bitmap3: 61-80%
113  * bitmap4: 81-100%
114  */
115
116 #define bitoff(x, y) x, y
117
118 int bitmap0[] = {
119     bitoff(-1, -1), bitoff(1, -1),
120     bitoff(-2, 0), bitoff(0, 0), bitoff(2, 0),
121     bitoff(-1, 1), bitoff(1, 1),
122     bitoff(9999, 9999),
123 };
124
125 int bitmap1[] = {
126     bitoff(0, -2),
127     bitoff(-3, -1), bitoff(-1, -1), bitoff(1, -1), bitoff(3, -1),
128     bitoff(-2, 0), bitoff(0, 0), bitoff(2, 0),
129     bitoff(-3, 1), bitoff(-1, 1), bitoff(1, 1), bitoff(3, 1),
130     bitoff(0, 2),
131     bitoff(9999, 9999),
132 };
133
134 int bitmap2[] = {
135     bitoff(-2, -2), bitoff(0, -2), bitoff(2, -2),
136     bitoff(-3, -1), bitoff(-1, -1), bitoff(1, -1), bitoff(3, -1),
137     bitoff(-4, 0), bitoff(-2, 0), bitoff(0, 0), bitoff(2, 0), bitoff(4, 0),
138     bitoff(-3, 1), bitoff(-1, 1), bitoff(1, 1), bitoff(3, 1),
139     bitoff(-2, 2), bitoff(0, 2), bitoff(2, 2),
140     bitoff(9999, 9999),
141 };
142
143 int bitmap3[] = {
144     bitoff(-1, -3), bitoff(1, -3),
145     bitoff(-4, -2), bitoff(-2, -2), bitoff(0, -2), bitoff(2, -2), bitoff(4,
146                                                                          -2),
147     bitoff(-5, -1), bitoff(-3, -1), bitoff(-1, -1), bitoff(1, -1),
148     bitoff(3, -1), bitoff(5, -1),
149     bitoff(-4, 0), bitoff(-2, 0), bitoff(0, 0), bitoff(2, 0), bitoff(4, 0),
150     bitoff(-5, 1), bitoff(-3, 1), bitoff(-1, 1), bitoff(1, 1),
151     bitoff(3, 1), bitoff(5, 1),
152     bitoff(-4, 2), bitoff(-2, 2), bitoff(0, 2), bitoff(2, 2), bitoff(4, 2),
153     bitoff(-1, 3), bitoff(1, 3),
154     bitoff(9999, 9999),
155 };
156
157 int bitmap4[] = {
158     bitoff(-3, -3), bitoff(-1, -3), bitoff(1, -3), bitoff(3, -3),
159     bitoff(-4, -2), bitoff(-2, -2), bitoff(0, -2), bitoff(2, -2), bitoff(4,
160                                                                          -2),
161     bitoff(-5, -1), bitoff(-3, -1), bitoff(-1, -1), bitoff(1, -1),
162     bitoff(3, -1), bitoff(5, -1),
163     bitoff(-6, 0), bitoff(-4, 0), bitoff(-2, 0), bitoff(0, 0), bitoff(2,
164                                                                       0),
165     bitoff(4, 0), bitoff(6, 0),
166     bitoff(-5, 1), bitoff(-3, 1), bitoff(-1, 1), bitoff(1, 1),
167     bitoff(3, 1), bitoff(5, 1),
168     bitoff(-4, 2), bitoff(-2, 2), bitoff(0, 2), bitoff(2, 2), bitoff(4, 2),
169     bitoff(-3, 3), bitoff(-1, 3), bitoff(1, 3), bitoff(3, 3),
170     bitoff(9999, 9999),
171 };
172
173 int *bitmaps[5] = {
174     bitmap0,
175     bitmap1,
176     bitmap2,
177     bitmap3,
178     bitmap4,
179 };