Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: a3dbf5c401
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 255 lines (219 sloc) 6.578 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
/*
* ai/SparseIntersectMap.cpp
*
* This file is part of Leges Motus, a networked, 2D shooter set in zero gravity.
*
* Copyright 2009-2011 Andrew Ayer, Nathan Partlan, Jeffrey Pfau
*
* Leges Motus is free and open source software. You may redistribute it and/or
* modify it under the terms of version 2, or (at your option) version 3, of the
* GNU General Public License (GPL), as published by the Free Software Foundation.
*
* Leges Motus is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the full text of the GNU General Public License for
* further detail.
*
* For a full copy of the GNU General Public License, please see the COPYING file
* in the root of the source code tree. You may also retrieve a copy from
* <http://www.gnu.org/licenses/gpl-2.0.txt>, or request a copy by writing to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*
*/

#include "SparseIntersectMap.hpp"
#include "common/misc.hpp"
#include "common/file.hpp"

#include <cstring>

using namespace LM;
using namespace std;

class SparseIntersectMap::ConstMapIterator : public ConstIterator<const SparseIntersectMap::Intersect&>::OpaqueIterator {
private:
const SparseIntersectMap* m_map;
int m_bucket;
int m_i;

void seek_next();

public:
ConstMapIterator(const SparseIntersectMap* map);

virtual bool has_more() const;
virtual const SparseIntersectMap::Intersect& next();
virtual ConstMapIterator* clone() const;
};

SparseIntersectMap::ConstMapIterator::ConstMapIterator(const SparseIntersectMap* map) {
m_map = map;
m_bucket = 0;
m_i = 0;
seek_next();
}

void SparseIntersectMap::ConstMapIterator::seek_next() {
ASSERT(m_bucket < m_map->m_nbuckets);

if (++m_i < m_map->m_buckets[m_bucket].nsize) {
return;
}
for (m_i = 0; m_bucket < m_map->m_nbuckets; ++m_bucket) {
if (m_map->m_buckets[m_bucket].nsize) {
return;
}
}
}

bool SparseIntersectMap::ConstMapIterator::has_more() const {
return m_bucket >= m_map->m_nbuckets;
}

const SparseIntersectMap::Intersect& SparseIntersectMap::ConstMapIterator::next() {
const Intersect& n = m_map->m_buckets[m_bucket].elts[m_i].i;
seek_next();
return n;
}

SparseIntersectMap::ConstMapIterator* SparseIntersectMap::ConstMapIterator::clone() const {
return new ConstMapIterator(*this);
}

SparseIntersectMap::SparseIntersectMap(int granularity, int est_elts) {
m_grain = granularity;
m_count = 0;
m_nbuckets = est_elts >> 5;
if (m_nbuckets < 16) {
m_nbuckets = 16;
}
m_buckets = new Bucket[m_nbuckets];
memset(m_buckets, 0, m_nbuckets * sizeof(Bucket));
}

SparseIntersectMap::SparseIntersectMap(std::istream* f) {
try {
static uint16_t v = 0;
expect(f, "LMSM", 4);
expect(f, &v, 2);
read32(f, &m_count);
read32(f, &m_grain);
read32(f, &m_nbuckets);
m_buckets = new Bucket[m_nbuckets];
for (int i = 0; i < m_nbuckets; ++i) {
Bucket& bucket = m_buckets[i];
read32(f, &bucket.psize);
bucket.elts = new Element[bucket.psize];
int j;
for (j = 0; j < bucket.psize; ++j) {
read32(f, &bucket.elts[j].x);
read32(f, &bucket.elts[j].y);
read32(f, &bucket.elts[j].t);
read32(f, &bucket.elts[j].i.x);
read32(f, &bucket.elts[j].i.y);
read32(f, &bucket.elts[j].i.dist);
}
}
} catch (const Exception&) {
throw Exception("Could not read file");
}
}

SparseIntersectMap::~SparseIntersectMap() {
for (int i = 0; i < m_nbuckets; ++i) {
delete m_buckets[i].elts;
}
delete m_buckets;
}

int SparseIntersectMap::make_hash(int x, int y, int theta) const {
int hash = x + y + theta;
// Ensure hash is non-negative
if (hash < 0) {
hash = -1 - hash;
}
return hash % m_nbuckets;
}

int SparseIntersectMap::grain_x(float x) const {
return int(x) >> m_grain;
}

int SparseIntersectMap::grain_y(float y) const {
return int(y) >> m_grain;
}

int SparseIntersectMap::grain_theta(float theta) const {
return int(theta / 360.0f * 4096.0f) >> m_grain;
}

void SparseIntersectMap::set(float x, float y, float theta, const Intersect& isect) {
int gx = grain_x(x);
int gy = grain_y(y);
int gt = grain_theta(theta);
Bucket& bucket = m_buckets[make_hash(gx, gy, gt)];

ASSERT(bucket.psize >= bucket.nsize);

if (bucket.psize == 0) {
bucket.psize = 32;
bucket.elts = new Element[bucket.psize];
bucket.elts[0].x = gx;
bucket.elts[0].y = gy;
bucket.elts[0].t = gt;
bucket.elts[0].i = isect;
bucket.nsize = 1;
++m_count;
return;
}

for (int i = 0; i < bucket.nsize; ++i) {
Element& e = bucket.elts[i];
if (e.x == gx && e.y == gy && e.t == gt) {
e.i = isect;
return;
}
}

if (bucket.psize == bucket.nsize) {
bucket.psize <<= 1;
Element* nelts = new Element[bucket.psize];
memcpy(nelts, bucket.elts, sizeof(Element)*bucket.nsize);
delete bucket.elts;
bucket.elts = nelts;
}

bucket.elts[bucket.nsize].x = gx;
bucket.elts[bucket.nsize].y = gy;
bucket.elts[bucket.nsize].t = gt;
bucket.elts[bucket.nsize].i = isect;
++bucket.nsize;
++m_count;
}

bool SparseIntersectMap::get(float x, float y, float theta, Intersect* isect) const {
int gx = grain_x(x);
int gy = grain_y(y);
int gt = grain_theta(theta);
const Bucket& bucket = m_buckets[make_hash(gx, gy, gt)];

for (int i = 0; i < bucket.nsize; ++i) {
const Element& e = bucket.elts[i];
if (e.x == gx && e.y == gy && e.t == gt) {
*isect = e.i;
return true;
}
}

return false;
}

int SparseIntersectMap::count() const {
return m_count;
}

ConstIterator<const SparseIntersectMap::Intersect&> SparseIntersectMap::iterate() const {
return ConstIterator<const SparseIntersectMap::Intersect&>(new ConstMapIterator(this));
}

void SparseIntersectMap::write(ostream* f) const {
(*f) << "LMSM";
write16(f, 0);
write32(f, m_count);
write32(f, m_grain);
write32(f, m_nbuckets);
for (int i = 0; i < m_nbuckets; ++i) {
const Bucket& bucket = m_buckets[i];
write32(f, bucket.psize);
int j;
for (j = 0; j < bucket.nsize; ++j) {
write32(f, bucket.elts[j].x);
write32(f, bucket.elts[j].y);
write32(f, bucket.elts[j].t);
write32(f, bucket.elts[j].i.x);
write32(f, bucket.elts[j].i.y);
write32(f, bucket.elts[j].i.dist);
}
for (; j < bucket.psize; ++j) {
write0(f, 4*6);
}
}
}

float SparseIntersectMap::get_granularity_x() const {
return 1 << m_grain;
}

float SparseIntersectMap::get_granularity_y() const {
return 1 << m_grain;
}

float SparseIntersectMap::get_granularity_theta() const {
return (1 << m_grain) * 360.0f / 4096.0f;
}
Something went wrong with that request. Please try again.