src/proto2/Terrain.cc
author nireco
Mon, 01 Dec 2008 17:46:52 +0000
changeset 160 ba0b6f421a3c
parent 159 109c7612ae2d
child 162 f760591b7481
permissions -rw-r--r--
not anything big
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
     1
#include "Terrain.hh"
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
     2
#include "Engine.hh"
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
     3
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
     4
#include <cmath>
151
9fc900fbfa79 Now getNormal might work a little better
saiam
parents: 150
diff changeset
     5
#include <cassert>
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
     6
#include <algorithm>
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
     7
#include <ClanLib/display.h>
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
     8
144
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
     9
Terrain::Terrain() {}
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
    10
Terrain::Terrain(const int &seed) 
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
    11
    : terrain(MAP_WIDTH, std::vector<TerrainType>(MAP_HEIGHT, DIRT)){
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    12
    this->generateTerrain(seed);
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    13
}
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    14
Terrain::Terrain(const Terrain &t) {
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    15
    this->terrain = t.getTerrain();
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    16
    this->generatePixelBuffer();
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    17
}
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    18
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    19
void Terrain::generatePixelBuffer() {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    20
    this->pixbuf = CL_PixelBuffer(MAP_WIDTH, MAP_HEIGHT, 4*MAP_WIDTH, 
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    21
                                  CL_PixelFormat::rgba8888);
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    22
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    23
    CL_Color color;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    24
    for (uint16_t i = 0; i < MAP_WIDTH; i++) {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    25
        for (uint16_t j = 0; j < MAP_HEIGHT; j++) {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    26
            switch(terrain[i][j]) {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    27
            case EMPTY:
144
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
    28
                color = COLOR_EMPTY;
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    29
                break;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    30
            case DIRT:
144
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
    31
                color = COLOR_DIRT;
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    32
                break;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    33
            case ROCK:
144
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
    34
                color = COLOR_ROCK;
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    35
                break;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    36
            default: // TODO: Shouldn't be here.
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    37
                break; 
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    38
            }
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    39
            this->pixbuf.draw_pixel(i, j, color);
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    40
        }
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    41
    }
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    42
}
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    43
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
    44
Vector Terrain::getPixelLocation(Vector point) const{
144
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
    45
    return Vector(scale(point.x), 
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
    46
                  scale(point.y));
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
    47
}
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
    48
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
    49
uint16_t Terrain::scale(float x) const {
144
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
    50
    return (uint16_t)round(x/MAP_SCALE);
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    51
}
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    52
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
    53
TerrainType Terrain::getType(int32_t x, int32_t y) const {
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    54
    if ((x < 0) || (y < 0) ||(x >= MAP_WIDTH) || (y >= MAP_HEIGHT)) {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    55
        return ROCK;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    56
    }
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    57
    return terrain[x][y];
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    58
}
157
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
    59
TerrainType Terrain::getType(Vector point) const {
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
    60
    return getType((int32_t)point.x, (int32_t)point.y);
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
    61
}
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    62
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    63
bool Terrain::collides(const Vector &point) const {
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    64
    Vector coor = getPixelLocation(point);
142
00672d0682ac Fixed some obvious bugs.
saiam
parents: 141
diff changeset
    65
    return (getType(coor.x, coor.y) != EMPTY);
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    66
}
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    67
139
77220921ae7d Toteutin viel? yhen funktion Terrainiin. Jos joku ehtii huomenna ennen mua toteuttamaan loputkin (l?hinn? noi collidet)
saiam
parents: 137
diff changeset
    68
bool Terrain::collides(const Vector &begin, const Vector &end) const {
140
b264b39224e5 Added some notes
saiam
parents: 139
diff changeset
    69
    // TODO: Maybe we should have another function prototype that also
b264b39224e5 Added some notes
saiam
parents: 139
diff changeset
    70
    // returns the point where we collided.
b264b39224e5 Added some notes
saiam
parents: 139
diff changeset
    71
141
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    72
    // We'll use Bresenhams line algorithm to go trough all the
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    73
    // "pixels" of the line.
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    74
    Vector b = getPixelLocation(begin);
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    75
    Vector e = getPixelLocation(end);
140
b264b39224e5 Added some notes
saiam
parents: 139
diff changeset
    76
141
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    77
    bool steep = (abs(e.y - b.y) > abs(e.x - b.x)); // k > 1
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    78
    if (steep) { // Line is steep -> swap x and y coordinates
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    79
        std::swap(b.x, b.y);
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    80
        std::swap(e.x, e.y);
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    81
    }
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    82
    if (b.x > e.x) { // Line goes down -> make it go up
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    83
        std::swap(b, e);
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    84
    }
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    85
    uint16_t dx = e.x - b.x;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    86
    uint16_t dy = abs(e.y - b.y);
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
    87
    int32_t err = dx/2;
141
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    88
    uint16_t ystep;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    89
    uint16_t y = b.y;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    90
    // Is the line ascending or descending
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    91
    if (b.y < e.y) ystep = 1;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    92
    else ystep = -1;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    93
    // Go trough the line
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    94
    for (uint16_t x =  b.x; x <= e.x; x++) {
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    95
        if (steep) { // X and Y coordinates must be switched if steep
142
00672d0682ac Fixed some obvious bugs.
saiam
parents: 141
diff changeset
    96
            if (getType(y,x) != EMPTY) { // Collision!
141
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    97
                return true;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    98
            }
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    99
        } else {
142
00672d0682ac Fixed some obvious bugs.
saiam
parents: 141
diff changeset
   100
            if (getType(x,y) != EMPTY) { // Collision!
141
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
   101
                return true;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
   102
            }
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
   103
        }
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
   104
        err = err - dy;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
   105
        if (err < 0) { // Check if we want to make an ystep
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
   106
            y = y + ystep;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
   107
            err = err + dx;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
   108
        }
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
   109
    }
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
   110
    return false; // No Collision
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
   111
}
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
   112
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   113
void Terrain::removeGround(const Vector &pos, const float &radius) {
144
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
   114
    // TODO: Implement. Some circle algoritmh should be usefull here,
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
   115
    // though the current impelementation doesn't seem too bad either.
140
b264b39224e5 Added some notes
saiam
parents: 139
diff changeset
   116
145
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   117
    Vector mid = getPixelLocation(pos);
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   118
    uint16_t r = scale(radius);
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   119
    for (uint16_t i = mid.x-r; i < mid.x+r; i++) {
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   120
        for (uint16_t j = mid.y-r; j < mid.y+r; j++) {
145
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   121
            if (getType(i, j) != ROCK) { // getType returns ROCK if
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   122
                                         // out of bounds
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   123
                if ((i-mid.x)*(i-mid.x)+(j-mid.y)*(j-mid.y) < r*r) {
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   124
                    terrain[i][j] = EMPTY;
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   125
                    pixbuf.draw_pixel(i, j, COLOR_EMPTY);
145
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   126
                }
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   127
            }
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   128
        }
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   129
    }
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
   130
}
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
   131
157
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   132
/**
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   133
 * Gets the index of the given coordinate direction
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   134
 * referring to the DIRECTIONS table in Physics.hh
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   135
 */
158
0215ace86018 some stuff
nireco
parents: 157
diff changeset
   136
int getDirectionIndex (Vector direction) {
0215ace86018 some stuff
nireco
parents: 157
diff changeset
   137
    Vector dir = direction.roundToInt();
157
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   138
    if(dir.x == 0 && dir.y == -1) {
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   139
        return 0;
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   140
    } else if(dir.x == 1 && dir.y == -1) {
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   141
        return 1;
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   142
    } else if(dir.x == 1 && dir.y == 0) {
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   143
        return 2;
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   144
    } else if(dir.x == 1 && dir.y == 1) {
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   145
        return 3;
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   146
    } else if(dir.x == 0 && dir.y == 1) {
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   147
        return 4;
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   148
    } else if(dir.x == -1 && dir.y == 1) {
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   149
        return 5;
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   150
    } else if(dir.x == -1 && dir.y == 0) {
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   151
        return 6;
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   152
    } else if(dir.x == -1 && dir.y == -1) {
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   153
        return 7;
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   154
    }
158
0215ace86018 some stuff
nireco
parents: 157
diff changeset
   155
    Engine::log(DEBUG, "Terrain.getDirectionIndex ") << "invalid direction: " << direction;
157
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   156
    return 0;
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   157
}
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   158
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   159
/**
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   160
 * point should be ground and prevPoint air, but it's easy to assure that
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   161
 * @param point - pixel on ground to which was collided
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   162
 * @param prevPoint - pixel where we are when we collide
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   163
 */
151
9fc900fbfa79 Now getNormal might work a little better
saiam
parents: 150
diff changeset
   164
Vector Terrain::getNormal(Vector point, Vector prevPoint) const {
149
ce4d8f12373a Now the terrain class is basicly ready to be put in action. Not tested :)
saiam
parents: 146
diff changeset
   165
    Vector p = getPixelLocation(point);
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
   166
158
0215ace86018 some stuff
nireco
parents: 157
diff changeset
   167
    assert(point != prevPoint);
0215ace86018 some stuff
nireco
parents: 157
diff changeset
   168
149
ce4d8f12373a Now the terrain class is basicly ready to be put in action. Not tested :)
saiam
parents: 146
diff changeset
   169
    Vector normal(0,0);
157
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   170
158
0215ace86018 some stuff
nireco
parents: 157
diff changeset
   171
    // These two must be rounded separately
157
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   172
    int dirIdx = getDirectionIndex(prevPoint.roundToInt() - point.roundToInt());
158
0215ace86018 some stuff
nireco
parents: 157
diff changeset
   173
//    dirIdx = (dirIdx+4)%8;
0215ace86018 some stuff
nireco
parents: 157
diff changeset
   174
0215ace86018 some stuff
nireco
parents: 157
diff changeset
   175
    std::cout << (prevPoint.roundToInt()) - (point.roundToInt()) << prevPoint-point << std::endl;
157
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   176
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   177
    normal += DIRECTIONS[dirIdx];
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   178
    for(int i = 1; i <= 2; i++) {
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   179
        if(getType(point + DIRECTIONS[(dirIdx+i+8)%8]) == EMPTY) {
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   180
            normal += DIRECTIONS[(dirIdx+i+8)%8];
149
ce4d8f12373a Now the terrain class is basicly ready to be put in action. Not tested :)
saiam
parents: 146
diff changeset
   181
        }
ce4d8f12373a Now the terrain class is basicly ready to be put in action. Not tested :)
saiam
parents: 146
diff changeset
   182
    }
157
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   183
    for(int i = 1; i <= 2; i++) {
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   184
        if(getType(point + DIRECTIONS[(dirIdx-i+8)%8]) == EMPTY) {
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   185
            normal += DIRECTIONS[(dirIdx-i+8)%8];
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   186
        }
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   187
    }
158
0215ace86018 some stuff
nireco
parents: 157
diff changeset
   188
0215ace86018 some stuff
nireco
parents: 157
diff changeset
   189
    Engine::log(DEBUG, "Physics.getNormal ") << "normal: " << normal << "   dirIdx: " << dirIdx;
0215ace86018 some stuff
nireco
parents: 157
diff changeset
   190
0215ace86018 some stuff
nireco
parents: 157
diff changeset
   191
    if(getType(point) == EMPTY || getType(prevPoint) != EMPTY) {
0215ace86018 some stuff
nireco
parents: 157
diff changeset
   192
        Engine::log(DEBUG, "Physics.getNormal ") << "logic ground error";
0215ace86018 some stuff
nireco
parents: 157
diff changeset
   193
    }
157
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   194
    
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   195
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   196
//    for (int i = 0; i < 8; i++) {
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   197
//        if (getType(p.x+DIRECTIONS[i].x, p.y+DIRECTIONS[i].y) == EMPTY) {
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   198
//            normal += DIRECTIONS[i];
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   199
//        }
8afdd8c449f9 and once again fixed getNormal, also added vector version of getType
nireco
parents: 156
diff changeset
   200
//    }
152
89e2d078817c Updated getNormal
saiam
parents: 151
diff changeset
   201
153
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 152
diff changeset
   202
   
152
89e2d078817c Updated getNormal
saiam
parents: 151
diff changeset
   203
    // Special cases
153
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 152
diff changeset
   204
    /*    Vector tmp = direction(direction(prevPoint-point) + direction(normal));
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 152
diff changeset
   205
    Engine::log(DEBUG, "Terrain.getNormal") << "tmp: " << tmp;
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 152
diff changeset
   206
    if (normal.length() == 0 || (tmp.x != 0 && tmp.y != 0 && getType(tmp.x, tmp.y) != EMPTY)) 
152
89e2d078817c Updated getNormal
saiam
parents: 151
diff changeset
   207
        normal = prevPoint - point; // Direct hit
153
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 152
diff changeset
   208
    */
156
d3e6625cc695 sorted DIRECTIONS
nireco
parents: 153
diff changeset
   209
//    Engine::log(DEBUG, "Terrain.getNormal") << "Normal: " << normal;
149
ce4d8f12373a Now the terrain class is basicly ready to be put in action. Not tested :)
saiam
parents: 146
diff changeset
   210
    return normal;
153
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 152
diff changeset
   211
    return Vector(0,-1);
149
ce4d8f12373a Now the terrain class is basicly ready to be put in action. Not tested :)
saiam
parents: 146
diff changeset
   212
}
146
329953407fdc Updated header Terrain.hh
saiam
parents: 145
diff changeset
   213
152
89e2d078817c Updated getNormal
saiam
parents: 151
diff changeset
   214
Vector direction(const Vector &v) {
89e2d078817c Updated getNormal
saiam
parents: 151
diff changeset
   215
    Vector tmp(v);
153
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 152
diff changeset
   216
    if (tmp.length() > 0) tmp /= tmp.length();
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 152
diff changeset
   217
    tmp.x = round(tmp.x);
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 152
diff changeset
   218
    tmp.y = round(tmp.y);
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 152
diff changeset
   219
    return tmp;
152
89e2d078817c Updated getNormal
saiam
parents: 151
diff changeset
   220
}
89e2d078817c Updated getNormal
saiam
parents: 151
diff changeset
   221
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   222
// TODO: This could better :)
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   223
// TODO: And this need some cleaning :)
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   224
void Terrain::generateTerrain(int seed) {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   225
    srand(seed); // Set random number generator seed.
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
   226
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   227
    // Some constants to control random generation
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   228
    const int min_range = 25;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   229
    const int max_range = 80;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   230
    const int num = 50;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   231
    const int rock_rarity = 4;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   232
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   233
    // Generate circles (or whatever)
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   234
    for (int i = 0; i < num; i++) {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   235
        // Random generate circle attributes
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   236
        int midx = rand()%MAP_WIDTH;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   237
        int midy = rand()%MAP_HEIGHT;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   238
        int range = rand()%(max_range-min_range)+min_range;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   239
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   240
        // Make sure that there's a circle in the midle of the cave
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   241
        if (i == 0) {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   242
            midx = MAP_WIDTH/2;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   243
            midy = MAP_WIDTH/2;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   244
            range = 150;
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
   245
        }
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   246
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   247
        TerrainType type = EMPTY;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   248
        if (rand()%rock_rarity == 0) {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   249
            type = ROCK;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   250
        }
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   251
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   252
        // Loops for every pixel of the cirlcle (or square as it seems
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   253
        // now)
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   254
        for (int x = std::max(0, midx-range); 
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   255
             x < std::min((int32_t)MAP_WIDTH, midx+range); 
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   256
             x++) {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   257
            for (int y = std::max(0, midy-range);
159
109c7612ae2d hyppy vinoon
ekku
parents: 158
diff changeset
   258
                    y < std::min((int32_t)MAP_HEIGHT, midy+range);
109c7612ae2d hyppy vinoon
ekku
parents: 158
diff changeset
   259
                    y++) {
109c7612ae2d hyppy vinoon
ekku
parents: 158
diff changeset
   260
                
160
ba0b6f421a3c not anything big
nireco
parents: 159
diff changeset
   261
                //terrain[x][y] = type;
ba0b6f421a3c not anything big
nireco
parents: 159
diff changeset
   262
ba0b6f421a3c not anything big
nireco
parents: 159
diff changeset
   263
                if ((x-midx)*(x-midx)+(y-midy)*(y-midy) < range*range) {
ba0b6f421a3c not anything big
nireco
parents: 159
diff changeset
   264
                    terrain[x][y] = type;
ba0b6f421a3c not anything big
nireco
parents: 159
diff changeset
   265
                }
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   266
            }
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   267
            
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   268
        } 
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   269
        
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
   270
    }
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   271
    
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   272
    this->generatePixelBuffer();
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
   273
}
139
77220921ae7d Toteutin viel? yhen funktion Terrainiin. Jos joku ehtii huomenna ennen mua toteuttamaan loputkin (l?hinn? noi collidet)
saiam
parents: 137
diff changeset
   274
77220921ae7d Toteutin viel? yhen funktion Terrainiin. Jos joku ehtii huomenna ennen mua toteuttamaan loputkin (l?hinn? noi collidet)
saiam
parents: 137
diff changeset
   275
void Terrain::draw(CL_GraphicContext *gc) {
77220921ae7d Toteutin viel? yhen funktion Terrainiin. Jos joku ehtii huomenna ennen mua toteuttamaan loputkin (l?hinn? noi collidet)
saiam
parents: 137
diff changeset
   276
    CL_Surface surf(this->pixbuf);
77220921ae7d Toteutin viel? yhen funktion Terrainiin. Jos joku ehtii huomenna ennen mua toteuttamaan loputkin (l?hinn? noi collidet)
saiam
parents: 137
diff changeset
   277
    surf.draw(0,0,gc);
77220921ae7d Toteutin viel? yhen funktion Terrainiin. Jos joku ehtii huomenna ennen mua toteuttamaan loputkin (l?hinn? noi collidet)
saiam
parents: 137
diff changeset
   278
}
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   279
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   280
std::vector<std::vector<TerrainType> > Terrain::getTerrain() const {
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   281
    return terrain;
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   282
}
152
89e2d078817c Updated getNormal
saiam
parents: 151
diff changeset
   283