src/Terrain.cc
author terom
Mon, 15 Dec 2008 14:24:38 +0000
changeset 370 39e59dd36b6e
parent 327 09a3b5055862
child 377 01d3c340b372
permissions -rw-r--r--
clean up Vector a bit, remove unused Terrain -> direction function
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
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    82
void Terrain::generate_texture (void) {
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    83
    int texturesize = 128;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    84
    texture = std::vector<std::vector<int> >(texturesize, std::vector<int>(texturesize));
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    85
    std::vector<double> land(texture.size()*texture.size());
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    86
    double str = 0.8;
244
80a818ac288b fixed H value of generate_texture
nireco
parents: 243
diff changeset
    87
    double H = 0.8;
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    88
    for(int i = 512; i >= 1; i /= 2) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    89
        fractal_diamond(land, texturesize, str, i);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    90
        fractal_step(land, texturesize, str, i);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    91
        str *= H;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    92
    }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    93
    double min = 100000;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    94
    double max = -100000;
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
        if(land[i] < min) min = land[i];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    97
        if(land[i] > max) max = land[i];
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    98
    }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
    99
    max -= min;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   100
    for(int i = 0; i < texturesize*texturesize; i++) {
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   101
        land[i] -= min;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   102
    }
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] = land[i]*255/max;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   105
        texture[i%texturesize][i/texturesize] = (int)(land[i]);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   106
    }
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   107
}
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   108
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   109
/**
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   110
 * Changes color depending on x and y values
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   111
 * x and y should be valid coordinates (not outside)
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   112
 */
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   113
void Terrain::noisifyPixel(CL_Color& color, PixelCoordinate pc) {
327
09a3b5055862 stronger texture
nireco
parents: 323
diff changeset
   114
    int texture_fade = 8;
09a3b5055862 stronger texture
nireco
parents: 323
diff changeset
   115
    switch (terrain[pc.x][pc.y]) {
09a3b5055862 stronger texture
nireco
parents: 323
diff changeset
   116
    case TERRAIN_EMPTY:
09a3b5055862 stronger texture
nireco
parents: 323
diff changeset
   117
        break;
09a3b5055862 stronger texture
nireco
parents: 323
diff changeset
   118
    case TERRAIN_DIRT:
09a3b5055862 stronger texture
nireco
parents: 323
diff changeset
   119
        texture_fade = 4;
09a3b5055862 stronger texture
nireco
parents: 323
diff changeset
   120
        break;
09a3b5055862 stronger texture
nireco
parents: 323
diff changeset
   121
    case TERRAIN_ROCK:
09a3b5055862 stronger texture
nireco
parents: 323
diff changeset
   122
        texture_fade = 2;
09a3b5055862 stronger texture
nireco
parents: 323
diff changeset
   123
        break;
09a3b5055862 stronger texture
nireco
parents: 323
diff changeset
   124
    }
09a3b5055862 stronger texture
nireco
parents: 323
diff changeset
   125
    int tx = (pc.x + texture_fade * 37) % texture.size();
09a3b5055862 stronger texture
nireco
parents: 323
diff changeset
   126
    int ty = (pc.y + texture_fade * 37) % texture[0].size();
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   127
    int red = color.get_red();
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   128
    int green = color.get_green();
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   129
    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
   130
327
09a3b5055862 stronger texture
nireco
parents: 323
diff changeset
   131
    red += (texture[tx][ty] - 128) / texture_fade;
09a3b5055862 stronger texture
nireco
parents: 323
diff changeset
   132
    green += (texture[tx][ty] - 128) / texture_fade;
09a3b5055862 stronger texture
nireco
parents: 323
diff changeset
   133
    blue += (texture[tx][ty] - 128) / texture_fade;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   134
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   135
    if (red < 0)
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   136
        red = 0;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   137
    else if (red >= 256)
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   138
        red = 255;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   139
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   140
    if (green < 0)
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   141
        green = 0;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   142
    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
   143
        green = 255;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   144
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   145
    if (blue < 0)
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   146
        blue = 0;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   147
    else if (blue >= 256)
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   148
        blue = 255;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   149
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   150
    color = CL_Color(red, green, blue);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   151
}
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   152
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   153
/**
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   154
 * Sets to color the correct color of pixel in (x,y)
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
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
   157
    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
   158
        color = CL_Color(0, 0, 0);
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   159
        return;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   160
    }
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   161
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   162
    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
   163
    case TERRAIN_EMPTY:
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   164
        color = COLOR_EMPTY;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   165
        break;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   166
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   167
    case TERRAIN_DIRT:
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   168
        color = COLOR_DIRT;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   169
        break;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   170
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   171
    case TERRAIN_ROCK:
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   172
        color = COLOR_ROCK;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   173
        break;
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   174
    }
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   175
        
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   176
    noisifyPixel(color, pc);
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   177
}
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   178
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   179
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
   180
    // initialze texture
243
25d9a0090397 some generated texture
nireco
parents: 223
diff changeset
   181
    generate_texture();
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   182
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   183
    // create pixel buffer
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   184
    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
   185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   186
    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
   187
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   188
    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
   189
        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
   190
            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
   191
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   192
            loadPixelColor(color, pc);
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   193
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   194
            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
   195
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   196
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   197
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   198
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   199
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
   200
    // XXX: assume 1:1
285
c080c8c70333 Previous position added, and unsigned int bug fixed
ekku
parents: 282
diff changeset
   201
    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
   202
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   203
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   204
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
   205
    return PixelCoordinate(map_width, map_height);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   206
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   207
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   208
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
   209
    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
   210
        return TERRAIN_ROCK;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   211
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   212
    return terrain[px][py];
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   213
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   214
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   215
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
   216
    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
   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
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
   220
    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
   221
}
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   222
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   223
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
   224
    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
   225
}
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   226
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   227
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
   228
    // 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
   229
    // returns the point where we collided.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   230
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   231
    // 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
   232
    // "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
   233
    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
   234
    PixelCoordinate e = getPixelCoordinate(end);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   235
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   236
    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
   237
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   238
    if (steep) { 
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   239
        // 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
   240
        std::swap(b.x, b.y);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   241
        std::swap(e.x, e.y);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   242
    }
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   243
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   244
    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
   245
        // 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
   246
        std::swap(b, e);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   247
    }
255
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
    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
   250
    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
   251
    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
   252
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   253
    // 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
   254
    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
   255
        ystep = 1;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   256
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   257
    else 
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   258
        ystep = -1;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   259
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   260
    // 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
   261
    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
   262
        if (steep) { 
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   263
            // 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
   264
            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
   265
                // Collision!
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   266
                return true;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   267
            }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   268
        } else {
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   269
            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
   270
                // Collision!
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   271
                return true;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   272
            }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   273
        }
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   274
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   275
        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
   276
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   277
        if (err < 0) { 
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   278
            // 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
   279
            y = y + ystep;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   280
            err = err + dx;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   281
        }
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
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   284
    return false; // No Collision
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   285
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   286
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   287
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
   288
    // 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
   289
    // 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
   290
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   291
    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
   292
    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
   293
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   294
    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
   295
        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
   296
            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
   297
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   298
            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
   299
                // 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
   300
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   301
                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
   302
                    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
   303
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   304
                    CL_Color color;
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   305
                    loadPixelColor(color, pc);
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   306
                    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
   307
                }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   308
            }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   309
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   310
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   311
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   312
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   313
/**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   314
 * 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
   315
 * 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
   316
 */
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   317
static int getDirectionIndex (Vector direction) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   318
    Vector dir = direction.roundToInt();
370
39e59dd36b6e clean up Vector a bit, remove unused Terrain -> direction function
terom
parents: 327
diff changeset
   319
39e59dd36b6e clean up Vector a bit, remove unused Terrain -> direction function
terom
parents: 327
diff changeset
   320
    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
   321
        return 0;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   322
    } 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
   323
        return 1;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   324
    } 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
   325
        return 2;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   326
    } 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
   327
        return 3;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   328
    } 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
   329
        return 4;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   330
    } 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
   331
        return 5;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   332
    } 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
   333
        return 6;
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   334
    } 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
   335
        return 7;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   336
    }
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   337
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   338
    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
   339
    return 0;
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
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
 * 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
   344
 * @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
   345
 * @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
   346
 */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   347
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
   348
    PixelCoordinate p = getPixelCoordinate(point);
185
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
    assert(point != prevPoint);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   351
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   352
    Vector normal(0,0);
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
    // These two must be rounded separately
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   355
    int dirIdx = getDirectionIndex(prevPoint.roundToInt() - point.roundToInt());
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
    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
   358
223
2fcaf54ed37b basic network-projectiles
terom
parents: 204
diff changeset
   359
    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
   360
        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
   361
            normal += DIRECTIONS[(dirIdx+i+8)%8];
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   362
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   363
    }
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   364
223
2fcaf54ed37b basic network-projectiles
terom
parents: 204
diff changeset
   365
    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
   366
        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
   367
            normal += DIRECTIONS[(dirIdx-i+8)%8];
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   368
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   369
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   370
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   371
    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
   372
        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
   373
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   374
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   375
    return normal;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   376
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   377
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   378
// TODO: This could better :)
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   379
// 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
   380
void Terrain::generateTerrain (int seed) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   381
    srand(seed); // Set random number generator seed.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   382
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   383
    // Some constants to control random generation
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   384
    const int min_range = 25;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   385
    const int max_range = 80;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   386
    const int num = 50;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   387
    const int rock_rarity = 4;
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
    // Generate circles (or whatever)
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   390
    for (int i = 0; i < num; i++) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   391
        // 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
   392
        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
   393
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   394
        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
   395
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   396
        // 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
   397
        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
   398
            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
   399
            mid.y = map_height / 2;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   400
            range = 150;
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
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   403
        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
   404
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   405
        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
   406
            type = TERRAIN_ROCK;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   407
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   408
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   409
        // 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
   410
        // now)
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   411
        for (
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   412
            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
   413
            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
   414
            x++
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   415
        ) {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   416
            for (
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   417
                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
   418
                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
   419
                y++
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   420
            ) {
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   421
                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
   422
                    terrain[x][y] = type;
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
            }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   425
        } 
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   426
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   427
    
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
   428
    // regenerate pixel buffer
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   429
    this->generatePixelBuffer();
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
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
   433
    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
   434
    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
   435
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   436
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   437
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
   438
    return terrain;
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