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