Show map.cpp syntax highlighted
/***************************************************************************
map.cpp - description
-------------------
begin : Wed Aug 14 2002
copyright : (C) 2002 by Brendon Higgins
email : freepop-devel@lists.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <map.h>
#include <corner.h>
#include <worldpos.h>
#include <maptilepos.h>
#include <iostream>
#include <cstdlib>
#include <cassert>
#include <ClanLib/network.h>
const std::string Map::NET_RAISE = "raise";
const std::string Map::NET_LOWER = "lower";
Map::Map(int w, int h): width(w), height(h) {
}
Map::Map(CL_InputSource& is):
width(is.read_uint16()),
height(is.read_uint16()) {
}
Map::~Map() {
}
int Map::getWidth() const {
return width;
}
int Map::getHeight() const {
return height;
}
bool Map::isFlat(const MapPos& pos) const {
return getTile(pos)->isFlat();
}
bool Map::isFlat(const WorldPos& pos) const {
return isFlat(pos.getMap());
}
bool Map::isSea(const WorldPos& pos) const {
return getZ(pos) <= 0.0f;
}
MapPos Map::findPos(const MapTile& t) const {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
if (&t == getTile(MapPos(x, y))) {
return MapPos(x, y);
}
}
}
assert(0 && "Found no tile!");
return MapPos(0, 0);
}
bool Map::isValid(const MapPos& p) const {
return p.getX() >= 0 && p.getY() >= 0 &&
p.getX() < getWidth() && p.getY() < getHeight();
}
bool Map::isValid(const WorldPos& p) const {
return p.getX() > 0.0f && p.getY() > 0.0f &&
p.getX() < getWidth() && p.getY() < getHeight();
}
const MapTile* Map::getTileLocal(const MapPos& p) const {
return getTile(p);
}
MapTile* Map::getTileLocal(const MapPos& p) {
return getTile(p);
}
void Map::pullTileCornerUp(const MapPos& p, unsigned int z, const Corner& c) {
if (getTileLocal(p)->getCornerPos(c) < z) {
raiseTileCornerInt(p, c);
}
}
void Map::pullTileCornerDown(const MapPos& p, unsigned int z, const Corner& c) {
if (getTileLocal(p)->getCornerPos(c) > z) {
lowerTileCornerInt(p, c);
}
}
void Map::raiseTileCorner(const MapPos& p, const Corner& c) {
raiseTileCornerInt(p, c);
}
void Map::lowerTileCorner(const MapPos& p, const Corner& c) {
lowerTileCornerInt(p, c);
}
void Map::raiseTileCornerInt(const MapPos& p, const Corner& c) {
MapPos pos(p);
MapTile& t = *(getTileLocal(p));
t.raiseCorner(c);
pos -= MapPos(1, 1);
if (isValid(pos)) {
pullTileCornerUp(pos, t.getCornerPos(Corner::NORTH), Corner::SOUTH);
}
pos += MapPos(1, 0);
if (isValid(pos)) {
pullTileCornerUp(pos, t.getCornerPos(Corner::NORTH), Corner::WEST);
pullTileCornerUp(pos, t.getCornerPos(Corner::EAST), Corner::SOUTH);
}
pos += MapPos(1, 0);
if (isValid(pos)) {
pullTileCornerUp(pos, t.getCornerPos(Corner::EAST), Corner::WEST);
}
pos += MapPos(0, 1);
if (isValid(pos)) {
pullTileCornerUp(pos, t.getCornerPos(Corner::EAST), Corner::NORTH);
pullTileCornerUp(pos, t.getCornerPos(Corner::SOUTH), Corner::WEST);
}
pos += MapPos(0, 1);
if (isValid(pos)) {
pullTileCornerUp(pos, t.getCornerPos(Corner::SOUTH), Corner::NORTH);
}
pos -= MapPos(1, 0);
if (isValid(pos)) {
pullTileCornerUp(pos, t.getCornerPos(Corner::WEST), Corner::NORTH);
pullTileCornerUp(pos, t.getCornerPos(Corner::SOUTH), Corner::EAST);
}
pos -= MapPos(1, 0);
if (isValid(pos)) {
pullTileCornerUp(pos, t.getCornerPos(Corner::WEST), Corner::EAST);
}
pos -= MapPos(0, 1);
if (isValid(pos)) {
pullTileCornerUp(pos, t.getCornerPos(Corner::WEST), Corner::SOUTH);
pullTileCornerUp(pos, t.getCornerPos(Corner::NORTH), Corner::EAST);
}
}
void Map::lowerTileCornerInt(const MapPos& p, const Corner& c) {
MapPos pos(p);
MapTile& t = *(getTileLocal(p));
if (t.isCornerAtSeaLevel(c)) return;
t.lowerCorner(c);
pos -= MapPos(1, 1);
if (isValid(pos)) {
pullTileCornerDown(pos, t.getCornerPos(Corner::NORTH), Corner::SOUTH);
}
pos += MapPos(1, 0);
if (isValid(pos)) {
pullTileCornerDown(pos, t.getCornerPos(Corner::NORTH), Corner::WEST);
pullTileCornerDown(pos, t.getCornerPos(Corner::EAST), Corner::SOUTH);
}
pos += MapPos(1, 0);
if (isValid(pos)) {
pullTileCornerDown(pos, t.getCornerPos(Corner::EAST), Corner::WEST);
}
pos += MapPos(0, 1);
if (isValid(pos)) {
pullTileCornerDown(pos, t.getCornerPos(Corner::EAST), Corner::NORTH);
pullTileCornerDown(pos, t.getCornerPos(Corner::SOUTH), Corner::WEST);
}
pos += MapPos(0, 1);
if (isValid(pos)) {
pullTileCornerDown(pos, t.getCornerPos(Corner::SOUTH), Corner::NORTH);
}
pos -= MapPos(1, 0);
if (isValid(pos)) {
pullTileCornerDown(pos, t.getCornerPos(Corner::WEST), Corner::NORTH);
pullTileCornerDown(pos, t.getCornerPos(Corner::SOUTH), Corner::EAST);
}
pos -= MapPos(1, 0);
if (isValid(pos)) {
pullTileCornerDown(pos, t.getCornerPos(Corner::WEST), Corner::EAST);
}
pos -= MapPos(0, 1);
if (isValid(pos)) {
pullTileCornerDown(pos, t.getCornerPos(Corner::WEST), Corner::SOUTH);
pullTileCornerDown(pos, t.getCornerPos(Corner::NORTH), Corner::EAST);
}
}
float Map::getZ(const WorldPos& p) const {
std::pair<MapPos, MapTilePos> pp = p.split();
return getTile(pp.first)->getZ(pp.second);
}
MapPos Map::getRandomPos() const {
return MapPos(int(double(width) * rand() / (RAND_MAX + 1.0)),
int(double(height) * rand() / (RAND_MAX + 1.0)));
}
See more files for this project here