Code Search for Developers
 
 
  

slope.cpp from FreePop at Krugle


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

FreePop

FreePop is a multi-platform tile-based game based on the great old game Populous 2 by Bullfrog Productions Ltd., but much improved.

Project homepage: http://sourceforge.net/projects/freepop
Programming language(s): C++
License: other

  client/
    Makefile.am
    client.cpp
    client.h
    clientmisc.cpp
    clientmisc.h
    clientstate.cpp
    clientstate.h
    connectstate.cpp
    connectstate.h
    cropsclient.cpp
    cropsclient.h
    entityclient.cpp
    entityclient.h
    entityclientfactory.cpp
    entityclientfactory.h
    firecolumnclient.cpp
    firecolumnclient.h
    gamestate.cpp
    gamestate.h
    loadstate.cpp
    loadstate.h
    mapclient.cpp
    mapclient.h
    maptileclient.cpp
    maptileclient.h
    messagebox.cpp
    messagebox.h
    oversprite.cpp
    oversprite.h
    paintable.cpp
    paintable.h
    pausefader.cpp
    pausefader.h
    peepclient.cpp
    peepclient.h
    peepmagnetclient.cpp
    peepmagnetclient.h
    playerclient.cpp
    playerclient.h
    playeroptionsdialog.cpp
    playeroptionsdialog.h
    rockclient.cpp
    rockclient.h
    swampclient.cpp
    swampclient.h
    townclient.cpp
    townclient.h
    treeclient.cpp
    treeclient.h
    worldclient.cpp
    worldclient.h
  server/
    Makefile.am
    contagion.cpp
    contagion.h
  Makefile.am
  common.cpp
  common.h
  corner.cpp
  corner.h
  crops.cpp
  crops.h
  entity.cpp
  entity.h
  entityfactory.cpp
  entityfactory.h
  firecolumn.cpp
  firecolumn.h
  gridmap.h
  identity.cpp
  identity.h
  map.cpp
  map.h
  mappos.cpp
  mappos.h
  maptile.cpp
  maptile.h
  maptilepos.cpp
  maptilepos.h
  misc.h
  peep.cpp
  peep.h
  peepmagnet.cpp
  peepmagnet.h
  player.cpp
  player.h
  rock.cpp
  rock.h
  rotation.cpp
  rotation.h
  rules.cpp
  rules.h
  slope.cpp
  slope.h
  swamp.cpp
  swamp.h
  tempo.cpp
  tempo.h
  town.cpp
  town.h
  tree.cpp
  tree.h
  worldpos.cpp
  worldpos.h