Show slope.cpp syntax highlighted
/***************************************************************************
slope.cpp - description
-------------------
begin : Wed Aug 21 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 <slope.h>
#include <corner.h>
#include <rotation.h>
#include <maptilepos.h>
#include <ClanLib/network.h>
#include <iostream>
#include <cassert>
Slope::Slope(int ss): s(ss) {
}
Slope::Slope() {
s = 0;
}
Slope::Slope(const Slope& copy) {
s = copy.s;
}
Slope::~Slope() {
}
Slope& Slope::operator =(const Slope& copy) {
s = copy.s;
return *this;
}
bool Slope::isFlat() const {
return s == 0;
}
bool Slope::isValid() const {
return 0 <= s && s <= 19 && s != 15;
}
int Slope::getRaw() const {
return s;
}
int Slope::raiseCorner(const Corner &c) {
// Lots of binary arithmetic to avoid unnecessary and ghastly
// switch blocks. If you don't understand it, don't worry.
// TODO: Someone do some tests to work out if this is faster than switches.
if (s < 15) {
if (s & (1 << c.getCornerNum())) {
if (s & (1 << (-c).getCornerNum())) {
s = 1 << c.getCornerNum();
return 1;
} else {
s = 16 + c.getCornerNum();
return 0;
}
} else {
s |= 1 << c.getCornerNum();
if (s == 15) {
s = 0;
return 1;
} else {
return 0;
}
}
} else {
if (s - 16 == c.getCornerNum()) {
} else if (s - 16 == (-c).getCornerNum()) {
s = 1 << (-c).getCornerNum();
} else {
s = 1 << (s - 16) | 1 << c.getCornerNum();
}
return 1;
}
}
int Slope::lowerCorner(const Corner &c) {
// Lots of binary arithmetic to avoid unnecessary and ghastly
// switch blocks. If you don't understand it, don't worry.
// TODO: Someone do some tests to work out if this is faster than switches.
if (s < 15) {
if (s & (1 << c.getCornerNum())) {
s ^= 1 << c.getCornerNum();
return 0;
} else {
if (s & (1 << (-c).getCornerNum())) {
s = 16 + (-c).getCornerNum();
} else {
s = (1 << c.getCornerNum()) ^ 0xf;
}
return -1;
}
} else {
if (s == 16 + c.getCornerNum()) {
s = (1 << (-c).getCornerNum()) ^ 0xf;
} else if (s == 16 + (-c).getCornerNum()) {
return -1;
} else {
s = 1 << (s - 16) | 1 << (-c).getCornerNum();
}
return 0;
}
}
int Slope::getCornerPos(const Corner& c) const {
if (s < 15) {
return (s >> c.getCornerNum()) & 1;
} else {
if (s - 16 == c.getCornerNum()) {
return 2;
} else if (s - 16 == (-c).getCornerNum()) {
return 0;
} else {
return 1;
}
}
}
float Slope::getZ(const MapTilePos& p) const {
float temp;
switch (s) {
case 1:
temp = 1.0f - p.getX() - p.getY();
return temp > 0.0f ? temp : 0.0f;
case 2:
temp = p.getX() - p.getY();
return temp > 0.0f ? temp : 0.0f;
case 3:
return 1.0f - p.getY();
case 4:
temp = p.getX() + p.getY() - 1.0f;
return temp > 0.0f ? temp : 0.0f;
case 5:
temp = p.getX() + p.getY() - 1.0f;
return temp >= 0.0f ? temp : 2.0f - p.getX() - p.getY();
case 6:
return p.getX();
case 7:
temp = 1.0f + p.getX() - p.getY();
return temp < 1.0f ? temp : 1.0f;
case 8:
temp = p.getY() - p.getX();
return temp > 0.0f ? temp : 0.0f;
case 9:
return 1.0f - p.getX();
case 10:
temp = p.getY() - p.getX();
return temp >= 0.0f ? temp : p.getX() - p.getY();
case 11:
temp = 2.0f - p.getX() - p.getY();
return temp < 1.0f ? temp : 1.0f;
case 12:
return p.getY();
case 13:
temp = 1.0f - p.getX() + p.getY();
return temp < 1.0f ? temp : 1.0f;
case 14:
temp = p.getX() + p.getY();
return temp < 1.0f ? temp : 1.0f;
case 16:
return 2.0f - p.getX() - p.getY();
case 17:
return 1.0f + p.getX() - p.getY();
case 18:
return p.getX() + p.getY();
case 19:
return 1.0f - p.getX() + p.getY();
default:
return 0.0f;
}
}
Slope& Slope::operator*=(const Rotation& r) {
int rr = r.getClock();
if (rr != 0) {
if (s < 15) {
s = (s << rr) & 0xf | (s >> (4 - rr));
if (s < 0 || s > 19 || s == 15) {
// TODO Remove debug output (merge with the assert)
std::cout << "Bad slope: " << s << std::endl;
assert(0 && "Bad slope state in *=rot part1.");
}
} else {
s = (s + rr) & 0x13;
// if (s < 0 || s > 19 || s == 15) {
// // TODO Remove debug output (merge with the assert)
// std::cout << "Bad slope: " << s << std::endl;
// assert(0 && "Bad slope state in *= rot part2.");
// }
}
}
return *this;
}
void Slope::inject(CL_OutputSource& os) const {
os.write_int8(s);
}
void Slope::extract(CL_InputSource& is) {
s = is.read_int8();
}
See more files for this project here