summaryrefslogtreecommitdiffstats
path: root/src/old/terrain.h
blob: 38e72874b32e0e66dc0c9968f6f14864c40764fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// * This file is part of the COLOBOT source code
// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
// *
// * This program is free software: you can redistribute it and/or modify
// * it under the terms of the GNU General Public License as published by
// * the Free Software Foundation, either version 3 of the License, or
// * (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU General Public License for more details.
// *
// * You should have received a copy of the GNU General Public License
// * along with this program. If not, see  http://www.gnu.org/licenses/.

// terrain.h

#pragma once


#include "common/struct.h"
#include "math/point.h"
#include "old/d3dengine.h"


class CInstanceManager;
class CD3DEngine;
class CWater;



const float FLATLIMIT = (5.0f*Math::PI/180.0f);


enum TerrainRes
{
    TR_NULL     = 0,
    TR_STONE    = 1,
    TR_URANIUM  = 2,
    TR_POWER    = 3,
    TR_KEYa     = 4,
    TR_KEYb     = 5,
    TR_KEYc     = 6,
    TR_KEYd     = 7,
};


const int MAXBUILDINGLEVEL = 100;

struct BuildingLevel
{
    Math::Vector    center;
    float       factor;
    float       min;
    float       max;
    float       level;
    float       height;
    float       bboxMinX;
    float       bboxMaxX;
    float       bboxMinZ;
    float       bboxMaxZ;
};


const int MAXMATTERRAIN = 100;

struct TerrainMaterial
{
    short       id;
    char        texName[20];
    float       u,v;
    float       hardness;
    char        mat[4];     // up, right, down, left
};

struct DotLevel
{
    short       id;
    char        mat[4];     // up, right, down, left
};


const int MAXFLYINGLIMIT = 10;

struct FlyingLimit
{
    Math::Vector    center;
    float       extRadius;
    float       intRadius;
    float       maxHeight;
};



class CTerrain
{
public:
    CTerrain(CInstanceManager* iMan);
    ~CTerrain();

    bool        Generate(int mosaic, int brickP2, float size, float vision, int depth, float hardness);
    bool        InitTextures(char* baseName, int* table, int dx, int dy);
    void        LevelFlush();
    bool        LevelMaterial(int id, char* baseName, float u, float v, int up, int right, int down, int left, float hardness);
    bool        LevelInit(int id);
    bool        LevelGenerate(int *id, float min, float max, float slope, float freq, Math::Vector center, float radius);
    void        FlushRelief();
    bool        ReliefFromBMP(const char* filename, float scaleRelief, bool adjustBorder);
    bool        ReliefFromDXF(const char* filename, float scaleRelief);
    bool        ResFromBMP(const char* filename);
    bool        CreateObjects(bool bMultiRes);
    bool        Terraform(const Math::Vector &p1, const Math::Vector &p2, float height);

    void        SetWind(Math::Vector speed);
    Math::Vector    RetWind();

    float       RetFineSlope(const Math::Vector &pos);
    float       RetCoarseSlope(const Math::Vector &pos);
    bool        GetNormal(Math::Vector &n, const Math::Vector &p);
    float       RetFloorLevel(const Math::Vector &p, bool bBrut=false, bool bWater=false);
    float       RetFloorHeight(const Math::Vector &p, bool bBrut=false, bool bWater=false);
    bool        MoveOnFloor(Math::Vector &p, bool bBrut=false, bool bWater=false);
    bool        ValidPosition(Math::Vector &p, float marging);
    TerrainRes  RetResource(const Math::Vector &p);
    void        LimitPos(Math::Vector &pos);

    void        FlushBuildingLevel();
    bool        AddBuildingLevel(Math::Vector center, float min, float max, float height, float factor);
    bool        UpdateBuildingLevel(Math::Vector center);
    bool        DeleteBuildingLevel(Math::Vector center);
    float       RetBuildingFactor(const Math::Vector &p);
    float       RetHardness(const Math::Vector &p);

    int         RetMosaic();
    int         RetBrick();
    float       RetSize();
    float       RetScaleRelief();

    void        GroundFlat(Math::Vector pos);
    float       RetFlatZoneRadius(Math::Vector center, float max);

    void        SetFlyingMaxHeight(float height);
    float       RetFlyingMaxHeight();
    void        FlushFlyingLimit();
    bool        AddFlyingLimit(Math::Vector center, float extRadius, float intRadius, float maxHeight);
    float       RetFlyingLimit(Math::Vector pos, bool bNoLimit);

protected:
    bool        ReliefAddDot(Math::Vector pos, float scaleRelief);
    void        AdjustRelief();
    Math::Vector    RetVector(int x, int y);
    D3DVERTEX2  RetVertex(int x, int y, int step);
    bool        CreateMosaic(int ox, int oy, int step, int objRank, const D3DMATERIAL7 &mat, float min, float max);
    bool        CreateSquare(bool bMultiRes, int x, int y);

    TerrainMaterial* LevelSearchMat(int id);
    void        LevelTextureName(int x, int y, char *name, Math::Point &uv);
    float       LevelRetHeight(int x, int y);
    bool        LevelGetDot(int x, int y, float min, float max, float slope);
    int         LevelTestMat(char *mat);
    void        LevelSetDot(int x, int y, int id, char *mat);
    bool        LevelIfDot(int x, int y, int id, char *mat);
    bool        LevelPutDot(int x, int y, int id);
    void        LevelOpenTable();
    void        LevelCloseTable();

    void        AdjustBuildingLevel(Math::Vector &p);

protected:
    CInstanceManager* m_iMan;
    CD3DEngine*     m_engine;
    CWater*         m_water;

    int         m_mosaic;       // number of mosaics
    int         m_brick;        // number of bricks per mosaics
    float           m_size;         // size of an item in an brick
    float           m_vision;       // vision before a change of resolution
    float*          m_relief;       // table of the relief
    int*            m_texture;      // table of textures
    int*            m_objRank;      // table of rows of objects
    bool            m_bMultiText;
    bool            m_bLevelText;
    float           m_scaleMapping;     // scale of the mapping
    float           m_scaleRelief;
    int         m_subdivMapping;
    int         m_depth;        // number of different resolutions (1,2,3,4)
    char            m_texBaseName[20];
    char            m_texBaseExt[10];
    float           m_defHardness;
    
    TerrainMaterial m_levelMat[MAXMATTERRAIN+1];
    int             m_levelMatTotal;
    int             m_levelMatMax;
    int             m_levelDotSize;
    DotLevel*       m_levelDot;
    int             m_levelID;

    int             m_buildingUsed;
    BuildingLevel   m_buildingTable[MAXBUILDINGLEVEL];

    unsigned char*  m_resources;
    Math::Vector        m_wind;         // wind speed

    float           m_flyingMaxHeight;
    int             m_flyingLimitTotal;
    FlyingLimit     m_flyingLimit[MAXFLYINGLIMIT];
};