src/Terrain.cc
author ekku
Mon, 08 Dec 2008 15:59:33 +0000
changeset 287 f59c8dee7f91
parent 285 c080c8c70333
child 296 4d3ebaa29430
permissions -rw-r--r--
getType added for physics object
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     1
#include "Terrain.hh"
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
     2
#include "Graphics.hh"
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     3
#include "Engine.hh"
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     4
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     5
#include <cmath>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     6
#include <cassert>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     7
#include <algorithm>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     8
#include <ClanLib/display.h>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     9
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    10
Terrain::Terrain (void) :
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    11
    map_width(0), map_height(0)
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    12
{
185
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
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    15
Terrain::Terrain (PixelDimension map_width, PixelDimension map_height, int seed) :
282
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 255
diff changeset
    16
    terrain(map_width, std::vector<TerrainType>(map_height, TERRAIN_DIRT)),
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 255
diff changeset
    17
    map_width(map_width), 
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 255
diff changeset
    18
    map_height(map_height)
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    19
{
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    20
    generateTerrain(seed);
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    21
}
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    22
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    23
Terrain::Terrain (const Terrain &t) 
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    24
{
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    25
    map_width = t.map_width;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    26
    map_height = t.map_height;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    27
    terrain = t.terrain;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    28
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    29
    generatePixelBuffer();
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    30
}
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    31
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    32
/*
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    33
 * Texture generation util functions
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    34
 */
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    35
static void fractal_step(std::vector<double>& land, int size, double str, int dist) {
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    36
    for(int i = 0; i < size; i += dist*2) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    37
        for(int j = dist; j < size; j += dist*2) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    38
            double sum = 0;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    39
            sum += land[((i+size-dist)%size)+(j*size)];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    40
            sum += land[i+((j+size-dist)%size)*size];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    41
            sum += land[((i+dist)%size)+j*size];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    42
            sum += land[i+((j+dist)%size)*size];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    43
            land[i+j*size] = sum/4 + (rand()%10000-5000)*str;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    44
        }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    45
    }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    46
    for(int i = dist; i < size; i += dist*2) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    47
        for(int j = 0; j < size; j += dist*2) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    48
            double sum = 0;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    49
            sum += land[((i+size-dist)%size)+(j*size)];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    50
            sum += land[i+((j+size-dist)%size)*size];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    51
            sum += land[((i+dist)%size)+j*size];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    52
            sum += land[i+((j+dist)%size)*size];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    53
            land[i+j*size] = sum/4 + (rand()%10000-5000)*str;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    54
        }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    55
    }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    56
}
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    57
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    58
static void fractal_diamond(std::vector<double>& land, int size, double str, int dist) {
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    59
    for(int i = dist; i < size; i += dist*2) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    60
        for(int j = dist; j < size; j += dist*2) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    61
            double sum = 0;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    62
            sum += land[((i+size-dist)%size)+(((j+size-dist)%size)*size)];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    63
            sum += land[((i+dist)%size)+((j+size-dist)%size)*size];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    64
            sum += land[(i+size-dist)%size+((j+dist)%size)*size];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    65
            sum += land[(i+dist)%size+((j+dist)%size)*size];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    66
            land[i+j*size] = sum/4 + (rand()%10000-5000)*str;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    67
        }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    68
    }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    69
}
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    70
244
80a818ac288b fixed H value of generate_texture
nireco
parents: 243
diff changeset
    71
/**
80a818ac288b fixed H value of generate_texture
nireco
parents: 243
diff changeset
    72
 * Algorithm read from http://www.gameprogrammer.com/fractal.html
80a818ac288b fixed H value of generate_texture
nireco
parents: 243
diff changeset
    73
 */
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    74
void Terrain::generate_texture (void) {
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    75
    int texturesize = 128;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    76
    texture = std::vector<std::vector<int> >(texturesize, std::vector<int>(texturesize));
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    77
    std::vector<double> land(texture.size()*texture.size());
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    78
    double str = 0.8;
244
80a818ac288b fixed H value of generate_texture
nireco
parents: 243
diff changeset
    79
    double H = 0.8;
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    80
    for(int i = 512; i >= 1; i /= 2) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    81
        fractal_diamond(land, texturesize, str, i);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    82
        fractal_step(land, texturesize, str, i);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    83
        str *= H;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    84
    }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    85
    double min = 100000;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    86
    double max = -100000;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    87
    for(int i = 0; i < texturesize*texturesize; i++) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    88
        if(land[i] < min) min = land[i];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    89
        if(land[i] > max) max = land[i];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    90
    }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    91
    max -= min;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    92
    for(int i = 0; i < texturesize*texturesize; i++) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    93
        land[i] -= min;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    94
    }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    95
    for(int i = 0; i < texturesize*texturesize; i++) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    96
        land[i] = land[i]*255/max;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    97
        texture[i%texturesize][i/texturesize] = (int)(land[i]);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    98
    }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    99
}
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   100
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   101
/**
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   102
 * Changes color depending on x and y values
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   103
 * x and y should be valid coordinates (not outside)
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   104
 */
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   105
void Terrain::noisifyPixel(CL_Color& color, PixelCoordinate pc) {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   106
    int tx = pc.x % texture.size();
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   107
    int ty = pc.y % texture[0].size();
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   108
    int red = color.get_red();
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   109
    int green = color.get_green();
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   110
    int blue = color.get_blue();
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   111
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   112
    red += texture[tx][ty] / 8 - 16;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   113
    green += texture[tx][ty] / 8 - 16;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   114
    blue += texture[tx][ty] / 8 - 16;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   115
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   116
    if (red < 0)
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   117
        red = 0;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   118
    else if (red >= 256)
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   119
        red = 255;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   120
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   121
    if (green < 0)
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   122
        green = 0;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   123
    else if (green >= 256)
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   124
        green = 255;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   125
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   126
    if (blue < 0)
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   127
        blue = 0;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   128
    else if (blue >= 256)
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   129
        blue = 255;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   130
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   131
    color = CL_Color(red, green, blue);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   132
}
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   133
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   134
/**
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   135
 * Sets to color the correct color of pixel in (x,y)
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   136
 */
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   137
void Terrain::loadPixelColor(CL_Color& color, PixelCoordinate pc) {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   138
    if ((pc.x < 0) || (pc.y < 0) || (pc.x >= map_width) || (pc.y >= map_height)) {
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   139
        color = CL_Color(0, 0, 0);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   140
        return;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   141
    }
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   142
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   143
    switch (terrain[pc.x][pc.y]) {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   144
    case TERRAIN_EMPTY:
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   145
        color = COLOR_EMPTY;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   146
        break;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   147
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   148
    case TERRAIN_DIRT:
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   149
        color = COLOR_DIRT;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   150
        break;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   151
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   152
    case TERRAIN_ROCK:
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   153
        color = COLOR_ROCK;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   154
        break;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   155
    }
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   156
        
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   157
    noisifyPixel(color, pc);
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   158
}
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   159
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   160
void Terrain::generatePixelBuffer (void) {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   161
    // initialze texture
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   162
    generate_texture();
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   163
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   164
    // create pixel buffer
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   165
    pixbuf = CL_PixelBuffer(map_width, map_height, 4 * map_width, CL_PixelFormat::rgba8888);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   166
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   167
    CL_Color color;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   168
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   169
    for (PixelDimension x = 0; x < map_width; x++) {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   170
        for (PixelDimension y = 0; y < map_height; y++) {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   171
            PixelCoordinate pc(x, y);
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   172
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   173
            loadPixelColor(color, pc);
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   174
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   175
            pixbuf.draw_pixel(pc.x, pc.y, color);
185
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
    }
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
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   180
PixelCoordinate Terrain::getPixelCoordinate (Vector point) const {
282
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 255
diff changeset
   181
    // XXX: assume 1:1
285
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
   182
    return PixelCoordinate((int) point.x, (int) point.y);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   183
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   184
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   185
PixelCoordinate Terrain::getDimensions (void) const {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   186
    return PixelCoordinate(map_width, map_height);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   187
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   188
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   189
TerrainType Terrain::getType (PixelDimension px, PixelDimension py) const {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   190
    if ((px < 0) || (py < 0) ||(px >= map_width) || (py >= map_height))
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   191
        return TERRAIN_ROCK;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   192
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   193
    return terrain[px][py];
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   194
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   195
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   196
TerrainType Terrain::getType (PixelCoordinate pc) const {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   197
    return getType(pc.x, pc.y);
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   198
}
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   199
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   200
TerrainType Terrain::getType (Vector point) const {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   201
    return getType((PixelDimension) point.x, (PixelDimension) point.y);
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   202
}
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   203
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   204
bool Terrain::collides (const Vector &point) const {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   205
    return (getType(point) != TERRAIN_EMPTY);
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   206
}
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   207
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   208
bool Terrain::collides (const Vector &begin, const Vector &end) const {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   209
    // 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
   210
    // returns the point where we collided.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   211
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   212
    // 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
   213
    // "pixels" of the line.
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   214
    PixelCoordinate b = getPixelCoordinate(begin);
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   215
    PixelCoordinate e = getPixelCoordinate(end);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   216
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   217
    bool steep = (abs(e.y - b.y) > abs(e.x - b.x)); // k > 1
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   218
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   219
    if (steep) { 
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   220
        // Line is steep -> swap x and y coordinates
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   221
        std::swap(b.x, b.y);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   222
        std::swap(e.x, e.y);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   223
    }
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   224
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   225
    if (b.x > e.x) { 
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   226
        // Line goes down -> make it go up
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   227
        std::swap(b, e);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   228
    }
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   229
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   230
    PixelDimension dx = e.x - b.x, dy = abs(e.y - b.y);
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   231
    PixelDimension err = dx / 2;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   232
    PixelDimension ystep, y = b.y;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   233
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   234
    // Is the line ascending or descending
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   235
    if (b.y < e.y) 
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   236
        ystep = 1;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   237
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   238
    else 
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   239
        ystep = -1;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   240
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   241
    // Go trough the line
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   242
    for (PixelDimension x =  b.x; x <= e.x; x++) {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   243
        if (steep) { 
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   244
            // X and Y coordinates must be switched if steep
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   245
            if (getType(y, x) != TERRAIN_EMPTY) { 
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   246
                // Collision!
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   247
                return true;
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
        } else {
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   250
            if (getType(x, y) != TERRAIN_EMPTY) { 
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   251
                // Collision!
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   252
                return true;
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
        }
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   255
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   256
        err = err - dy;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   257
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   258
        if (err < 0) { 
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   259
            // Check if we want to make an ystep
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   260
            y = y + ystep;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   261
            err = err + dx;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   262
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   263
    }
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   264
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   265
    return false; // No Collision
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   266
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   267
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   268
void Terrain::removeGround (const Vector &pos, float radius) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   269
    // 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
   270
    // 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
   271
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   272
    PixelCoordinate mid = getPixelCoordinate(pos);
282
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 255
diff changeset
   273
    PixelDimension r = (unsigned int) radius; // XXX: scale
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   274
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   275
    for (PixelDimension i = mid.x - r; i < mid.x + r; i++) {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   276
        for (PixelDimension j = mid.y-r; j < mid.y+r; j++) {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   277
            PixelCoordinate pc(i, j);
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   278
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   279
            if (getType(pc) != TERRAIN_ROCK) { 
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   280
                // getType returns ROCK if out of bounds
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   281
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   282
                if ((i - mid.x) * (i - mid.x) + (j - mid.y) * (j - mid.y) < r * r) {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   283
                    terrain[i][j] = TERRAIN_EMPTY;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   284
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   285
                    CL_Color color;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   286
                    loadPixelColor(color, pc);
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   287
                    pixbuf.draw_pixel(pc.x, pc.y, color);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   288
                }
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
        }
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
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   293
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   294
/**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   295
 * 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
   296
 * 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
   297
 */
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   298
static int getDirectionIndex (Vector direction) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   299
    Vector dir = direction.roundToInt();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   300
    if(dir.x == 0 && dir.y == -1) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   301
        return 0;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   302
    } else if (dir.x == 1 && dir.y == -1) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   303
        return 1;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   304
    } else if (dir.x == 1 && dir.y == 0) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   305
        return 2;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   306
    } else if (dir.x == 1 && dir.y == 1) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   307
        return 3;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   308
    } else if (dir.x == 0 && dir.y == 1) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   309
        return 4;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   310
    } else if (dir.x == -1 && dir.y == 1) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   311
        return 5;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   312
    } else if (dir.x == -1 && dir.y == 0) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   313
        return 6;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   314
    } else if (dir.x == -1 && dir.y == -1) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   315
        return 7;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   316
    }
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   317
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   318
    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
   319
    return 0;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   320
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   321
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   322
/**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   323
 * 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
   324
 * @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
   325
 * @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
   326
 */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   327
Vector Terrain::getNormal(Vector point, Vector prevPoint) const {
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   328
    PixelCoordinate p = getPixelCoordinate(point);
185
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
    assert(point != prevPoint);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   331
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   332
    Vector normal(0,0);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   333
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   334
    // These two must be rounded separately
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   335
    int dirIdx = getDirectionIndex(prevPoint.roundToInt() - point.roundToInt());
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   336
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   337
    normal += DIRECTIONS[dirIdx];
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   338
223
2fcaf54ed37b basic network-projectiles
terom
parents: 204
diff changeset
   339
    for (int i = 1; i <= 2; i++) {
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   340
        if (getType(point + DIRECTIONS[(dirIdx+i+8)%8]) == TERRAIN_EMPTY) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   341
            normal += DIRECTIONS[(dirIdx+i+8)%8];
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   342
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   343
    }
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   344
223
2fcaf54ed37b basic network-projectiles
terom
parents: 204
diff changeset
   345
    for (int i = 1; i <= 2; i++) {
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   346
        if (getType(point + DIRECTIONS[(dirIdx-i+8)%8]) == TERRAIN_EMPTY) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   347
            normal += DIRECTIONS[(dirIdx-i+8)%8];
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   348
        }
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
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   351
    if (getType(point) == TERRAIN_EMPTY || getType(prevPoint) != TERRAIN_EMPTY) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   352
        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
   353
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   354
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   355
    return normal;
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
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   358
// XXX: weird vectors
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   359
Vector direction (const Vector &v) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   360
    Vector tmp(v);
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   361
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   362
    if (tmp.length() > 0) 
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   363
        tmp /= tmp.length();
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   364
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   365
    tmp.x = (uint16_t)(tmp.x);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   366
    tmp.y = (uint16_t)(tmp.y);
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   367
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   368
    return tmp;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   369
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   370
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   371
// TODO: This could better :)
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   372
// TODO: And this need some cleaning :)
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   373
void Terrain::generateTerrain (int seed) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   374
    srand(seed); // Set random number generator seed.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   375
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   376
    // Some constants to control random generation
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   377
    const int min_range = 25;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   378
    const int max_range = 80;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   379
    const int num = 50;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   380
    const int rock_rarity = 4;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   381
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   382
    // Generate circles (or whatever)
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   383
    for (int i = 0; i < num; i++) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   384
        // Random generate circle attributes
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   385
        PixelCoordinate mid(rand() % map_width, rand() % map_width);
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   386
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   387
        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
   388
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   389
        // 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
   390
        if (i == 0) {
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   391
            mid.x = map_width / 2;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   392
            mid.y = map_height / 2;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   393
            range = 150;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   394
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   395
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   396
        TerrainType type = TERRAIN_EMPTY;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   397
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   398
        if (rand() % rock_rarity == 0) {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   399
            type = TERRAIN_ROCK;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   400
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   401
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   402
        // 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
   403
        // now)
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   404
        for (
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   405
            PixelDimension x = std::max((PixelDimension) 0, mid.x - range); 
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   406
            x < std::min(map_width, mid.x + range); 
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   407
            x++
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   408
        ) {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   409
            for (
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   410
                PixelDimension y = std::max((PixelDimension) 0, mid.y - range);
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   411
                y < std::min(map_height, mid.y + range);
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   412
                y++
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   413
            ) {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   414
                if ((x - mid.x) * (x - mid.x) + (y - mid.y) * (y - mid.y) < range * range) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   415
                    terrain[x][y] = type;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   416
                }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   417
            }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   418
        } 
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   419
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   420
    
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   421
    // regenerate pixel buffer
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   422
    this->generatePixelBuffer();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   423
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   424
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   425
void Terrain::draw (Graphics *g, PixelCoordinate camera) {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   426
    CL_Surface surf (pixbuf);
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   427
    surf.draw(-camera.x, -camera.y, g->get_gc());
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   428
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   429
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   430
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
   431
    return terrain;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   432
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   433