src/proto2/Terrain.cc
author saiam
Sun, 30 Nov 2008 17:22:57 +0000
changeset 151 9fc900fbfa79
parent 150 5e032b540af3
child 152 89e2d078817c
permissions -rw-r--r--
Now getNormal might work a little better
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
     1
#include "Terrain.hh"
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
     2
#include "Engine.hh"
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
     3
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
     4
#include <cmath>
151
9fc900fbfa79 Now getNormal might work a little better
saiam
parents: 150
diff changeset
     5
#include <cassert>
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
     6
#include <algorithm>
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
     7
#include <ClanLib/display.h>
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
     8
144
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
     9
Terrain::Terrain() {}
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
    10
Terrain::Terrain(const int &seed) 
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
    11
    : terrain(MAP_WIDTH, std::vector<TerrainType>(MAP_HEIGHT, DIRT)){
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    12
    this->generateTerrain(seed);
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    13
}
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    14
Terrain::Terrain(const Terrain &t) {
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    15
    this->terrain = t.getTerrain();
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    16
    this->generatePixelBuffer();
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    17
}
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    18
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    19
void Terrain::generatePixelBuffer() {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    20
    this->pixbuf = CL_PixelBuffer(MAP_WIDTH, MAP_HEIGHT, 4*MAP_WIDTH, 
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    21
                                  CL_PixelFormat::rgba8888);
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    22
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    23
    CL_Color color;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    24
    for (uint16_t i = 0; i < MAP_WIDTH; i++) {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    25
        for (uint16_t j = 0; j < MAP_HEIGHT; j++) {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    26
            switch(terrain[i][j]) {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    27
            case EMPTY:
144
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
    28
                color = COLOR_EMPTY;
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    29
                break;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    30
            case DIRT:
144
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
    31
                color = COLOR_DIRT;
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    32
                break;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    33
            case ROCK:
144
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
    34
                color = COLOR_ROCK;
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    35
                break;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    36
            default: // TODO: Shouldn't be here.
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    37
                break; 
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    38
            }
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    39
            this->pixbuf.draw_pixel(i, j, color);
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    40
        }
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    41
    }
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    42
}
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    43
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
    44
Vector Terrain::getPixelLocation(Vector point) const{
144
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
    45
    return Vector(scale(point.x), 
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
    46
                  scale(point.y));
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
    47
}
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
    48
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
    49
uint16_t Terrain::scale(float x) const {
144
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
    50
    return (uint16_t)round(x/MAP_SCALE);
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    51
}
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    52
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
    53
TerrainType Terrain::getType(int32_t x, int32_t y) const {
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    54
    if ((x < 0) || (y < 0) ||(x >= MAP_WIDTH) || (y >= MAP_HEIGHT)) {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    55
        return ROCK;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    56
    }
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    57
    return terrain[x][y];
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    58
}
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
    59
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    60
bool Terrain::collides(const Vector &point) const {
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    61
    Vector coor = getPixelLocation(point);
142
00672d0682ac Fixed some obvious bugs.
saiam
parents: 141
diff changeset
    62
    return (getType(coor.x, coor.y) != EMPTY);
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    63
}
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
    64
139
77220921ae7d Toteutin viel? yhen funktion Terrainiin. Jos joku ehtii huomenna ennen mua toteuttamaan loputkin (l?hinn? noi collidet)
saiam
parents: 137
diff changeset
    65
bool Terrain::collides(const Vector &begin, const Vector &end) const {
140
b264b39224e5 Added some notes
saiam
parents: 139
diff changeset
    66
    // TODO: Maybe we should have another function prototype that also
b264b39224e5 Added some notes
saiam
parents: 139
diff changeset
    67
    // returns the point where we collided.
b264b39224e5 Added some notes
saiam
parents: 139
diff changeset
    68
141
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    69
    // We'll use Bresenhams line algorithm to go trough all the
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    70
    // "pixels" of the line.
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    71
    Vector b = getPixelLocation(begin);
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    72
    Vector e = getPixelLocation(end);
140
b264b39224e5 Added some notes
saiam
parents: 139
diff changeset
    73
141
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    74
    bool steep = (abs(e.y - b.y) > abs(e.x - b.x)); // k > 1
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    75
    if (steep) { // Line is steep -> swap x and y coordinates
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    76
        std::swap(b.x, b.y);
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    77
        std::swap(e.x, e.y);
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    78
    }
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    79
    if (b.x > e.x) { // Line goes down -> make it go up
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    80
        std::swap(b, e);
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    81
    }
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    82
    uint16_t dx = e.x - b.x;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    83
    uint16_t dy = abs(e.y - b.y);
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
    84
    int32_t err = dx/2;
141
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    85
    uint16_t ystep;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    86
    uint16_t y = b.y;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    87
    // Is the line ascending or descending
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    88
    if (b.y < e.y) ystep = 1;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    89
    else ystep = -1;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    90
    // Go trough the line
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    91
    for (uint16_t x =  b.x; x <= e.x; x++) {
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    92
        if (steep) { // X and Y coordinates must be switched if steep
142
00672d0682ac Fixed some obvious bugs.
saiam
parents: 141
diff changeset
    93
            if (getType(y,x) != EMPTY) { // Collision!
141
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    94
                return true;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    95
            }
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    96
        } else {
142
00672d0682ac Fixed some obvious bugs.
saiam
parents: 141
diff changeset
    97
            if (getType(x,y) != EMPTY) { // Collision!
141
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    98
                return true;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
    99
            }
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
   100
        }
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
   101
        err = err - dy;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
   102
        if (err < 0) { // Check if we want to make an ystep
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
   103
            y = y + ystep;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
   104
            err = err + dx;
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
   105
        }
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
   106
    }
73109c5652d3 Implementend Terrain::collides for line. It still hasn't a way to return collision point. Has not been tested.
saiam
parents: 140
diff changeset
   107
    return false; // No Collision
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
   108
}
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
   109
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   110
void Terrain::removeGround(const Vector &pos, const float &radius) {
144
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
   111
    // TODO: Implement. Some circle algoritmh should be usefull here,
d02f642625ec Removed unnessessary bloat and added terrain scale
saiam
parents: 143
diff changeset
   112
    // though the current impelementation doesn't seem too bad either.
140
b264b39224e5 Added some notes
saiam
parents: 139
diff changeset
   113
145
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   114
    Vector mid = getPixelLocation(pos);
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   115
    uint16_t r = scale(radius);
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   116
    for (uint16_t i = mid.x-r; i < mid.x+r; i++) {
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   117
        for (uint16_t j = mid.y-r; j < mid.y+r; j++) {
145
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   118
            if (getType(i, j) != ROCK) { // getType returns ROCK if
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   119
                                         // out of bounds
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   120
                if ((i-mid.x)*(i-mid.x)+(j-mid.y)*(j-mid.y) < r*r) {
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   121
                    terrain[i][j] = EMPTY;
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   122
                    pixbuf.draw_pixel(i, j, COLOR_EMPTY);
145
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   123
                }
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   124
            }
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   125
        }
fc572ad7326a Terrain::getTangent and Terrain::getNormal must be implemented. Everything else should be there. Still: Not tested, not
saiam
parents: 144
diff changeset
   126
    }
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
   127
}
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
   128
151
9fc900fbfa79 Now getNormal might work a little better
saiam
parents: 150
diff changeset
   129
Vector Terrain::getNormal(Vector point, Vector prevPoint) const {
149
ce4d8f12373a Now the terrain class is basicly ready to be put in action. Not tested :)
saiam
parents: 146
diff changeset
   130
    Vector p = getPixelLocation(point);
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
   131
149
ce4d8f12373a Now the terrain class is basicly ready to be put in action. Not tested :)
saiam
parents: 146
diff changeset
   132
    Vector normal(0,0);
ce4d8f12373a Now the terrain class is basicly ready to be put in action. Not tested :)
saiam
parents: 146
diff changeset
   133
    for (int i = 0; i < 8; i++) {
ce4d8f12373a Now the terrain class is basicly ready to be put in action. Not tested :)
saiam
parents: 146
diff changeset
   134
        if (getType(p.x+DIRECTIONS[i].x, p.y+DIRECTIONS[i].y) == EMPTY) {
ce4d8f12373a Now the terrain class is basicly ready to be put in action. Not tested :)
saiam
parents: 146
diff changeset
   135
            normal += DIRECTIONS[i];
ce4d8f12373a Now the terrain class is basicly ready to be put in action. Not tested :)
saiam
parents: 146
diff changeset
   136
        }
ce4d8f12373a Now the terrain class is basicly ready to be put in action. Not tested :)
saiam
parents: 146
diff changeset
   137
    }
151
9fc900fbfa79 Now getNormal might work a little better
saiam
parents: 150
diff changeset
   138
    if (normal.length() == 0) normal = prevPoint - point;
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   139
    Engine::log(DEBUG, "Terrain.getNormal") << "Normal: " << normal;
149
ce4d8f12373a Now the terrain class is basicly ready to be put in action. Not tested :)
saiam
parents: 146
diff changeset
   140
    return normal;
ce4d8f12373a Now the terrain class is basicly ready to be put in action. Not tested :)
saiam
parents: 146
diff changeset
   141
}
146
329953407fdc Updated header Terrain.hh
saiam
parents: 145
diff changeset
   142
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   143
// TODO: This could better :)
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   144
// TODO: And this need some cleaning :)
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   145
void Terrain::generateTerrain(int seed) {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   146
    srand(seed); // Set random number generator seed.
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
   147
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   148
    // Some constants to control random generation
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   149
    const int min_range = 25;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   150
    const int max_range = 80;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   151
    const int num = 50;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   152
    const int rock_rarity = 4;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   153
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   154
    // Generate circles (or whatever)
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   155
    for (int i = 0; i < num; i++) {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   156
        // Random generate circle attributes
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   157
        int midx = rand()%MAP_WIDTH;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   158
        int midy = rand()%MAP_HEIGHT;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   159
        int range = rand()%(max_range-min_range)+min_range;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   160
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   161
        // Make sure that there's a circle in the midle of the cave
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   162
        if (i == 0) {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   163
            midx = MAP_WIDTH/2;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   164
            midy = MAP_WIDTH/2;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   165
            range = 150;
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
   166
        }
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   167
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   168
        TerrainType type = EMPTY;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   169
        if (rand()%rock_rarity == 0) {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   170
            type = ROCK;
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   171
        }
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   172
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   173
        // Loops for every pixel of the cirlcle (or square as it seems
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   174
        // now)
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   175
        for (int x = std::max(0, midx-range); 
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   176
             x < std::min((int32_t)MAP_WIDTH, midx+range); 
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   177
             x++) {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   178
            for (int y = std::max(0, midy-range);
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   179
                 y < std::min((int32_t)MAP_HEIGHT, midy+range);
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   180
                 y++) {
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   181
                terrain[x][y] = type;
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   182
            }
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   183
            
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   184
        } 
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   185
        
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
   186
    }
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   187
    
143
45565385d972 Added Terrain::generateTerrain, basicly a straight copy from the old one.
saiam
parents: 142
diff changeset
   188
    this->generatePixelBuffer();
137
8736fdd12197 Drafted somekind of Terrain class. Still unfinished and not uses.
saiam
parents:
diff changeset
   189
}
139
77220921ae7d Toteutin viel? yhen funktion Terrainiin. Jos joku ehtii huomenna ennen mua toteuttamaan loputkin (l?hinn? noi collidet)
saiam
parents: 137
diff changeset
   190
77220921ae7d Toteutin viel? yhen funktion Terrainiin. Jos joku ehtii huomenna ennen mua toteuttamaan loputkin (l?hinn? noi collidet)
saiam
parents: 137
diff changeset
   191
void Terrain::draw(CL_GraphicContext *gc) {
77220921ae7d Toteutin viel? yhen funktion Terrainiin. Jos joku ehtii huomenna ennen mua toteuttamaan loputkin (l?hinn? noi collidet)
saiam
parents: 137
diff changeset
   192
    CL_Surface surf(this->pixbuf);
77220921ae7d Toteutin viel? yhen funktion Terrainiin. Jos joku ehtii huomenna ennen mua toteuttamaan loputkin (l?hinn? noi collidet)
saiam
parents: 137
diff changeset
   193
    surf.draw(0,0,gc);
77220921ae7d Toteutin viel? yhen funktion Terrainiin. Jos joku ehtii huomenna ennen mua toteuttamaan loputkin (l?hinn? noi collidet)
saiam
parents: 137
diff changeset
   194
}
150
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   195
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   196
std::vector<std::vector<TerrainType> > Terrain::getTerrain() const {
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   197
    return terrain;
5e032b540af3 Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents: 149
diff changeset
   198
}