src/Terrain.cc
author nireco
Sun, 07 Dec 2008 14:13:22 +0000
changeset 244 80a818ac288b
parent 243 25d9a0090397
child 248 e40ef56dc62c
permissions -rw-r--r--
fixed H value of generate_texture
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     1
#include "Terrain.hh"
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     2
#include "Engine.hh"
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     3
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     4
#include <cmath>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     5
#include <cassert>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     6
#include <algorithm>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     7
#include <ClanLib/display.h>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     8
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     9
Terrain::Terrain() {}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    10
Terrain::Terrain(const int &seed) 
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    11
    : terrain(MAP_WIDTH, std::vector<TerrainType>(MAP_HEIGHT, DIRT)){
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    12
    this->generateTerrain(seed);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    13
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    14
Terrain::Terrain(const Terrain &t) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    15
    this->terrain = t.getTerrain();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    16
    this->generatePixelBuffer();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    17
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    18
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    19
void fractal_step(std::vector<double>& land, int size, double str, int dist) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    20
    for(int i = 0; i < size; i += dist*2) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    21
        for(int j = dist; j < size; j += dist*2) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    22
            double sum = 0;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    23
            sum += land[((i+size-dist)%size)+(j*size)];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    24
            sum += land[i+((j+size-dist)%size)*size];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    25
            sum += land[((i+dist)%size)+j*size];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    26
            sum += land[i+((j+dist)%size)*size];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    27
            land[i+j*size] = sum/4 + (rand()%10000-5000)*str;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    28
        }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    29
    }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    30
    for(int i = dist; i < size; i += dist*2) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    31
        for(int j = 0; j < size; j += dist*2) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    32
            double sum = 0;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    33
            sum += land[((i+size-dist)%size)+(j*size)];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    34
            sum += land[i+((j+size-dist)%size)*size];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    35
            sum += land[((i+dist)%size)+j*size];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    36
            sum += land[i+((j+dist)%size)*size];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    37
            land[i+j*size] = sum/4 + (rand()%10000-5000)*str;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    38
        }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    39
    }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    40
}
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    41
void fractal_diamond(std::vector<double>& land, int size, double str, int dist) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    42
    for(int i = dist; i < size; i += dist*2) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    43
        for(int j = dist; j < size; j += dist*2) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    44
            double sum = 0;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    45
            sum += land[((i+size-dist)%size)+(((j+size-dist)%size)*size)];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    46
            sum += land[((i+dist)%size)+((j+size-dist)%size)*size];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    47
            sum += land[(i+size-dist)%size+((j+dist)%size)*size];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    48
            sum += land[(i+dist)%size+((j+dist)%size)*size];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    49
            land[i+j*size] = sum/4 + (rand()%10000-5000)*str;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    50
        }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    51
    }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    52
}
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    53
244
80a818ac288b fixed H value of generate_texture
nireco
parents: 243
diff changeset
    54
/**
80a818ac288b fixed H value of generate_texture
nireco
parents: 243
diff changeset
    55
 * Algorithm read from http://www.gameprogrammer.com/fractal.html
80a818ac288b fixed H value of generate_texture
nireco
parents: 243
diff changeset
    56
 */
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    57
void Terrain::generate_texture() {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    58
    int texturesize = 128;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    59
    texture = std::vector<std::vector<int> >(texturesize, std::vector<int>(texturesize));
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    60
    std::vector<double> land(texture.size()*texture.size());
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    61
    double str = 0.8;
244
80a818ac288b fixed H value of generate_texture
nireco
parents: 243
diff changeset
    62
    double H = 0.8;
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    63
    for(int i = 512; i >= 1; i /= 2) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    64
        fractal_diamond(land, texturesize, str, i);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    65
        fractal_step(land, texturesize, str, i);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    66
        str *= H;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    67
    }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    68
    double min = 100000;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    69
    double max = -100000;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    70
    for(int i = 0; i < texturesize*texturesize; i++) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    71
        if(land[i] < min) min = land[i];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    72
        if(land[i] > max) max = land[i];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    73
    }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    74
    max -= min;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    75
    for(int i = 0; i < texturesize*texturesize; i++) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    76
        land[i] -= min;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    77
    }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    78
    for(int i = 0; i < texturesize*texturesize; i++) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    79
        land[i] = land[i]*255/max;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    80
        texture[i%texturesize][i/texturesize] = (int)(land[i]);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    81
    }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    82
}
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    83
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    84
/**
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    85
 * Changes color depending on x and y values
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    86
 * x and y should be valid coordinates (not outside)
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    87
 */
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    88
void Terrain::noisifyPixel(CL_Color& color, int x, int y) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    89
    int tx = x%texture.size();
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    90
    int ty = y%texture[0].size();
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    91
    int red = color.get_red();
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    92
    int green = color.get_green();
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    93
    int blue = color.get_blue();
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    94
    red += texture[tx][ty]/8-16;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    95
    green += texture[tx][ty]/8-16;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    96
    blue += texture[tx][ty]/8-16;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    97
    if(red < 0)
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    98
        red = 0;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    99
    if(red >= 256)
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   100
        red = 255;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   101
    if(green < 0)
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   102
        green = 0;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   103
    if(blue >= 256)
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   104
        blue = 255;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   105
    if(blue < 0)
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   106
        blue = 0;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   107
    if(green >= 256)
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   108
        green = 255;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   109
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   110
    color = CL_Color(red, green, blue);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   111
}
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   112
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   113
/**
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   114
 * Sets to color the correct color of pixel in (x,y)
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   115
 */
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   116
void Terrain::loadPixelColor(CL_Color& color, int x, int y) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   117
    if ((x < 0) || (y < 0) ||(x >= MAP_WIDTH) || (y >= MAP_HEIGHT)) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   118
        color = CL_Color(0, 0, 0);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   119
        return;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   120
    }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   121
    switch(terrain[x][y]) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   122
    case EMPTY:
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   123
        color = COLOR_EMPTY;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   124
        noisifyPixel(color, x, y);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   125
        break;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   126
    case DIRT:
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   127
        color = COLOR_DIRT;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   128
        noisifyPixel(color, x, y);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   129
        break;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   130
    case ROCK:
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   131
        color = COLOR_ROCK;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   132
        noisifyPixel(color, x, y);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   133
        break;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   134
    }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   135
}
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   136
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   137
void Terrain::generatePixelBuffer() {
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   138
    generate_texture();
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   139
    this->pixbuf = CL_PixelBuffer(MAP_WIDTH, MAP_HEIGHT, 4*MAP_WIDTH, 
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   140
                                  CL_PixelFormat::rgba8888);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   141
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   142
    CL_Color color;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   143
    for (uint16_t i = 0; i < MAP_WIDTH; i++) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   144
        for (uint16_t j = 0; j < MAP_HEIGHT; j++) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   145
            switch(terrain[i][j]) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   146
            case EMPTY:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   147
                color = COLOR_EMPTY;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   148
                break;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   149
            case DIRT:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   150
                color = COLOR_DIRT;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   151
                break;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   152
            case ROCK:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   153
                color = COLOR_ROCK;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   154
                break;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   155
            default: // TODO: Shouldn't be here.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   156
                break; 
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   157
            }
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   158
            loadPixelColor(color, i, j);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   159
            this->pixbuf.draw_pixel(i, j, color);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   160
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   161
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   162
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   163
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   164
Vector Terrain::getPixelLocation(Vector point) const{
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   165
    return Vector(scale(point.x), 
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   166
                  scale(point.y));
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   167
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   168
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   169
uint16_t Terrain::scale(float x) const {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   170
    return (uint16_t)(x/MAP_SCALE);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   171
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   172
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   173
TerrainType Terrain::getType(int32_t x, int32_t y) const {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   174
    if ((x < 0) || (y < 0) ||(x >= MAP_WIDTH) || (y >= MAP_HEIGHT)) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   175
        return ROCK;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   176
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   177
    return terrain[x][y];
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   178
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   179
TerrainType Terrain::getType(Vector point) const {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   180
    return getType((int32_t)point.x, (int32_t)point.y);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   181
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   182
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   183
bool Terrain::collides(const Vector &point) const {
204
4c386e9c950f Removed one memory leak.
saiam
parents: 185
diff changeset
   184
    return (getType(point) != EMPTY);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   185
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   186
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   187
bool Terrain::collides(const Vector &begin, const Vector &end) const {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   188
    // TODO: Maybe we should have another function prototype that also
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   189
    // returns the point where we collided.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   190
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   191
    // We'll use Bresenhams line algorithm to go trough all the
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   192
    // "pixels" of the line.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   193
    Vector b = getPixelLocation(begin);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   194
    Vector e = getPixelLocation(end);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   195
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   196
    bool steep = (abs(e.y - b.y) > abs(e.x - b.x)); // k > 1
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   197
    if (steep) { // Line is steep -> swap x and y coordinates
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   198
        std::swap(b.x, b.y);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   199
        std::swap(e.x, e.y);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   200
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   201
    if (b.x > e.x) { // Line goes down -> make it go up
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   202
        std::swap(b, e);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   203
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   204
    uint16_t dx = e.x - b.x;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   205
    uint16_t dy = abs(e.y - b.y);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   206
    int32_t err = dx/2;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   207
    uint16_t ystep;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   208
    uint16_t y = b.y;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   209
    // Is the line ascending or descending
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   210
    if (b.y < e.y) ystep = 1;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   211
    else ystep = -1;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   212
    // Go trough the line
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   213
    for (uint16_t x =  b.x; x <= e.x; x++) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   214
        if (steep) { // X and Y coordinates must be switched if steep
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   215
            if (getType(y,x) != EMPTY) { // Collision!
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   216
                return true;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   217
            }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   218
        } else {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   219
            if (getType(x,y) != EMPTY) { // Collision!
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   220
                return true;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   221
            }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   222
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   223
        err = err - dy;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   224
        if (err < 0) { // Check if we want to make an ystep
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   225
            y = y + ystep;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   226
            err = err + dx;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   227
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   228
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   229
    return false; // No Collision
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   230
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   231
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   232
void Terrain::removeGround(const Vector &pos, const float &radius) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   233
    // TODO: Implement. Some circle algoritmh should be usefull here,
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   234
    // though the current impelementation doesn't seem too bad either.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   235
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   236
    Vector mid = getPixelLocation(pos);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   237
    uint16_t r = scale(radius);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   238
    for (uint16_t i = mid.x-r; i < mid.x+r; i++) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   239
        for (uint16_t j = mid.y-r; j < mid.y+r; j++) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   240
            if (getType(i, j) != ROCK) { // getType returns ROCK if
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   241
                                         // out of bounds
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   242
                if ((i-mid.x)*(i-mid.x)+(j-mid.y)*(j-mid.y) < r*r) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   243
                    terrain[i][j] = EMPTY;
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   244
                    CL_Color color(0, 0, 0);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   245
                    loadPixelColor(color, i, j);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   246
                    pixbuf.draw_pixel(i, j, color);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   247
                }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   248
            }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   249
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   250
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   251
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   252
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   253
/**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   254
 * Gets the index of the given coordinate direction
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   255
 * referring to the DIRECTIONS table in Physics.hh
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   256
 */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   257
int getDirectionIndex (Vector direction) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   258
    Vector dir = direction.roundToInt();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   259
    if(dir.x == 0 && dir.y == -1) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   260
        return 0;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   261
    } else if(dir.x == 1 && dir.y == -1) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   262
        return 1;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   263
    } else if(dir.x == 1 && dir.y == 0) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   264
        return 2;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   265
    } else if(dir.x == 1 && dir.y == 1) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   266
        return 3;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   267
    } else if(dir.x == 0 && dir.y == 1) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   268
        return 4;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   269
    } else if(dir.x == -1 && dir.y == 1) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   270
        return 5;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   271
    } else if(dir.x == -1 && dir.y == 0) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   272
        return 6;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   273
    } else if(dir.x == -1 && dir.y == -1) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   274
        return 7;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   275
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   276
    Engine::log(DEBUG, "Terrain.getDirectionIndex ") << "invalid direction: " << direction;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   277
    return 0;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   278
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   279
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   280
/**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   281
 * point should be ground and prevPoint air, but it's easy to assure that
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   282
 * @param point - pixel on ground to which was collided
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   283
 * @param prevPoint - pixel where we are when we collide
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   284
 */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   285
Vector Terrain::getNormal(Vector point, Vector prevPoint) const {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   286
    Vector p = getPixelLocation(point);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   287
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   288
    assert(point != prevPoint);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   289
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   290
    Vector normal(0,0);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   291
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   292
    // These two must be rounded separately
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   293
    int dirIdx = getDirectionIndex(prevPoint.roundToInt() - point.roundToInt());
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   294
//    dirIdx = (dirIdx+4)%8;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   295
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   296
    normal += DIRECTIONS[dirIdx];
223
2fcaf54ed37b basic network-projectiles
terom
parents: 204
diff changeset
   297
    for (int i = 1; i <= 2; i++) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   298
        if(getType(point + DIRECTIONS[(dirIdx+i+8)%8]) == EMPTY) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   299
            normal += DIRECTIONS[(dirIdx+i+8)%8];
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   300
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   301
    }
223
2fcaf54ed37b basic network-projectiles
terom
parents: 204
diff changeset
   302
    for (int i = 1; i <= 2; i++) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   303
        if(getType(point + DIRECTIONS[(dirIdx-i+8)%8]) == EMPTY) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   304
            normal += DIRECTIONS[(dirIdx-i+8)%8];
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   305
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   306
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   307
223
2fcaf54ed37b basic network-projectiles
terom
parents: 204
diff changeset
   308
    if (getType(point) == EMPTY || getType(prevPoint) != EMPTY) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   309
        Engine::log(DEBUG, "Physics.getNormal ") << "logic ground error";
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   310
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   311
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   312
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   313
//    for (int i = 0; i < 8; i++) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   314
//        if (getType(p.x+DIRECTIONS[i].x, p.y+DIRECTIONS[i].y) == EMPTY) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   315
//            normal += DIRECTIONS[i];
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   316
//        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   317
//    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   318
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   319
   
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   320
    // Special cases
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   321
    /*    Vector tmp = direction(direction(prevPoint-point) + direction(normal));
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   322
    Engine::log(DEBUG, "Terrain.getNormal") << "tmp: " << tmp;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   323
    if (normal.length() == 0 || (tmp.x != 0 && tmp.y != 0 && getType(tmp.x, tmp.y) != EMPTY)) 
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   324
        normal = prevPoint - point; // Direct hit
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   325
    */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   326
//    Engine::log(DEBUG, "Terrain.getNormal") << "Normal: " << normal;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   327
    return normal;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   328
    return Vector(0,-1);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   329
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   330
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   331
Vector direction(const Vector &v) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   332
    Vector tmp(v);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   333
    if (tmp.length() > 0) tmp /= tmp.length();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   334
    tmp.x = (uint16_t)(tmp.x);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   335
    tmp.y = (uint16_t)(tmp.y);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   336
    return tmp;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   337
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   338
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   339
// TODO: This could better :)
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   340
// TODO: And this need some cleaning :)
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   341
void Terrain::generateTerrain(int seed) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   342
    srand(seed); // Set random number generator seed.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   343
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   344
    // Some constants to control random generation
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   345
    const int min_range = 25;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   346
    const int max_range = 80;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   347
    const int num = 50;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   348
    const int rock_rarity = 4;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   349
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   350
    // Generate circles (or whatever)
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   351
    for (int i = 0; i < num; i++) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   352
        // Random generate circle attributes
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   353
        int midx = rand()%MAP_WIDTH;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   354
        int midy = rand()%MAP_HEIGHT;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   355
        int range = rand()%(max_range-min_range)+min_range;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   356
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   357
        // Make sure that there's a circle in the midle of the cave
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   358
        if (i == 0) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   359
            midx = MAP_WIDTH/2;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   360
            midy = MAP_WIDTH/2;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   361
            range = 150;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   362
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   363
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   364
        TerrainType type = EMPTY;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   365
        if (rand()%rock_rarity == 0) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   366
            type = ROCK;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   367
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   368
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   369
        // Loops for every pixel of the cirlcle (or square as it seems
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   370
        // now)
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   371
        for (int x = std::max(0, midx-range); 
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   372
             x < std::min((int32_t)MAP_WIDTH, midx+range); 
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   373
             x++) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   374
            for (int y = std::max(0, midy-range);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   375
                    y < std::min((int32_t)MAP_HEIGHT, midy+range);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   376
                    y++) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   377
                
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   378
                //terrain[x][y] = type;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   379
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   380
                if ((x-midx)*(x-midx)+(y-midy)*(y-midy) < range*range) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   381
                    terrain[x][y] = type;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   382
                }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   383
            }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   384
            
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   385
        } 
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   386
        
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   387
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   388
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   389
    this->generatePixelBuffer();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   390
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   391
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   392
void Terrain::draw(CL_GraphicContext *gc) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   393
    CL_Surface surf(this->pixbuf);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   394
    surf.draw(0,0,gc);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   395
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   396
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   397
std::vector<std::vector<TerrainType> > Terrain::getTerrain() const {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   398
    return terrain;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   399
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   400