src/lib/subs/getbit.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  getbit.c: Replaces old bitmap code
00029  * 
00030  *  Known contributors to this file:
00031  *     Dave Pare, 1989
00032  */
00033 
00034 #include <config.h>
00035 
00036 #include "optlist.h"
00037 #include "prototypes.h"
00038 #include "sect.h"
00039 
00040 /*
00041  *
00042  * the bit offsets for each bit pattern based on the efficiency of
00043  * the sector.
00044  *
00045  * bitmap0:  0-20%
00046  * bitmap1: 21-40%
00047  * bitmap2: 41-60%
00048  * bitmap3: 61-80%
00049  * bitmap4: 81-100%
00050  */
00051 
00052 #define bitoff(x, y) x, y
00053 
00054 static int bitmap0[] = {
00055     bitoff(-1, -1), bitoff(1, -1),
00056     bitoff(-2, 0), bitoff(0, 0), bitoff(2, 0),
00057     bitoff(-1, 1), bitoff(1, 1),
00058     bitoff(9999, 9999),
00059 };
00060 
00061 static int bitmap1[] = {
00062     bitoff(0, -2),
00063     bitoff(-3, -1), bitoff(-1, -1), bitoff(1, -1), bitoff(3, -1),
00064     bitoff(-2, 0), bitoff(0, 0), bitoff(2, 0),
00065     bitoff(-3, 1), bitoff(-1, 1), bitoff(1, 1), bitoff(3, 1),
00066     bitoff(0, 2),
00067     bitoff(9999, 9999),
00068 };
00069 
00070 static int bitmap2[] = {
00071     bitoff(-2, -2), bitoff(0, -2), bitoff(2, -2),
00072     bitoff(-3, -1), bitoff(-1, -1), bitoff(1, -1), bitoff(3, -1),
00073     bitoff(-4, 0), bitoff(-2, 0), bitoff(0, 0), bitoff(2, 0), bitoff(4, 0),
00074     bitoff(-3, 1), bitoff(-1, 1), bitoff(1, 1), bitoff(3, 1),
00075     bitoff(-2, 2), bitoff(0, 2), bitoff(2, 2),
00076     bitoff(9999, 9999),
00077 };
00078 
00079 static int bitmap3[] = {
00080     bitoff(-1, -3), bitoff(1, -3),
00081     bitoff(-4, -2), bitoff(-2, -2), bitoff(0, -2), bitoff(2, -2), bitoff(4,
00082                                                                          -2),
00083     bitoff(-5, -1), bitoff(-3, -1), bitoff(-1, -1), bitoff(1, -1),
00084     bitoff(3, -1), bitoff(5, -1),
00085     bitoff(-4, 0), bitoff(-2, 0), bitoff(0, 0), bitoff(2, 0), bitoff(4, 0),
00086     bitoff(-5, 1), bitoff(-3, 1), bitoff(-1, 1), bitoff(1, 1),
00087     bitoff(3, 1), bitoff(5, 1),
00088     bitoff(-4, 2), bitoff(-2, 2), bitoff(0, 2), bitoff(2, 2), bitoff(4, 2),
00089     bitoff(-1, 3), bitoff(1, 3),
00090     bitoff(9999, 9999),
00091 };
00092 
00093 static int bitmap4[] = {
00094     bitoff(-3, -3), bitoff(-1, -3), bitoff(1, -3), bitoff(3, -3),
00095     bitoff(-4, -2), bitoff(-2, -2), bitoff(0, -2), bitoff(2, -2), bitoff(4,
00096                                                                          -2),
00097     bitoff(-5, -1), bitoff(-3, -1), bitoff(-1, -1), bitoff(1, -1),
00098     bitoff(3, -1), bitoff(5, -1),
00099     bitoff(-6, 0), bitoff(-4, 0), bitoff(-2, 0), bitoff(0, 0), bitoff(2,
00100                                                                       0),
00101     bitoff(4, 0), bitoff(6, 0),
00102     bitoff(-5, 1), bitoff(-3, 1), bitoff(-1, 1), bitoff(1, 1),
00103     bitoff(3, 1), bitoff(5, 1),
00104     bitoff(-4, 2), bitoff(-2, 2), bitoff(0, 2), bitoff(2, 2), bitoff(4, 2),
00105     bitoff(-3, 3), bitoff(-1, 3), bitoff(1, 3), bitoff(3, 3),
00106     bitoff(9999, 9999),
00107 };
00108 
00109 static int *bitmaps[5] = {
00110     bitmap0,
00111     bitmap1,
00112     bitmap2,
00113     bitmap3,
00114     bitmap4,
00115 };
00116 
00117 #define GCFx(x) ((x + WORLD_X) % WORLD_X)
00118 #define GCFy(y) ((y + WORLD_Y) % WORLD_Y)
00119 
00120 int
00121 emp_getbit(int x, int y, unsigned char *bitmap)
00122 {
00123     int id;
00124 
00125     id = (GCFy(y)) * WORLD_X / 2 + GCFx(x) / 2;
00126     return bitmap[id / 8] & bit(id & 07);
00127 }
00128 
00129 void
00130 emp_setbit(int x, int y, unsigned char *bitmap)
00131 {
00132     int id;
00133 
00134     id = (GCFy(y)) * WORLD_X / 2 + GCFx(x) / 2;
00135     bitmap[id / 8] |= bit(id & 07);
00136 }
00137 
00138 static void
00139 emp_setbitmap(int x, int y, unsigned char *bitmap, int *bitmaps)
00140 {
00141     int *mp;
00142     int id;
00143     int dx, dy;
00144 
00145     for (mp = bitmaps; *mp != 9999;) {
00146         dx = x + *mp++;
00147         dy = y + *mp++;
00148         id = (GCFy(dy)) * WORLD_X / 2 + GCFx(dx) / 2;
00149         bitmap[id / 8] |= bit(id & 07);
00150     }
00151 }
00152 
00153 void
00154 bitinit2(struct nstr_sect *np, unsigned char *bitmap, int country)
00155 {
00156     struct sctstr sect;
00157     int eff;
00158 
00159     while (nxtsct(np, &sect)) {
00160         if (sect.sct_own != country)
00161             continue;
00162         eff = sect.sct_effic / 20;
00163         if (eff > 4)
00164             eff = 4;
00165         emp_setbitmap(np->x, np->y, bitmap, bitmaps[eff]);
00166     }
00167     snxtsct_rewind(np);
00168 }

Generated on Fri Mar 28 11:01:15 2008 for empserver by  doxygen 1.5.2