File: TerrainMod_unittest.cpp

package info (click to toggle)
eris 1.3.14-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,016 kB
  • ctags: 1,819
  • sloc: cpp: 13,153; sh: 9,106; perl: 287; makefile: 201; ansic: 165
file content (253 lines) | stat: -rw-r--r-- 8,911 bytes parent folder | download
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Eris Online RPG Protocol Library
// Copyright (C) 2008 Erik Hjortsberg <erik@worldforge.org>
//
// 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 2 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, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

// $Id$

#include <Eris/Avatar.h>

#include <Eris/Connection.h>
#include <Eris/Account.h>
#include <Eris/Entity.h>
#include <Eris/View.h>
#include <Eris/Log.h>
#include <Eris/TerrainMod.h>
#include <Eris/Exceptions.h>

#include <Atlas/Objects/Anonymous.h>
#include <Atlas/Objects/Entity.h>

#include <wfmath/atlasconv.h>

#include <iostream>

static void writeLog(Eris::LogLevel, const std::string & msg)
{       
    std::cerr << msg << std::endl << std::flush;
}


class TestConnection : public Eris::Connection {
  public:
    TestConnection(const std::string & name, const std::string & host,
                   short port, bool debug) :
                   Eris::Connection(name, host, port, debug) { }

    virtual void send(const Atlas::Objects::Root &obj) {
        std::cout << "Sending " << obj->getParents().front()
                  << std::endl << std::flush;
    }
};

class TestAccount : public Eris::Account {
  public:
    TestAccount(Eris::Connection * con) : Eris::Account(con) { }

    void setup_insertActiveCharacters(Eris::Avatar * ea) {
        m_activeCharacters.insert(std::make_pair(ea->getId(), ea));
    }
};

class TestAvatar : public Eris::Avatar {
  public:
    TestAvatar(Eris::Account * ac, const std::string & ent_id) :
               Eris::Avatar(ac, ent_id) { }

    void setup_setEntity(Eris::Entity * ent) {
        m_entity = ent;
        m_entityId = ent->getId();
    }
};

class TestEntity : public Eris::Entity {
  public:
    TestEntity(const std::string& id, Eris::TypeInfo* ty, Eris::View* vw) :
               Eris::Entity(id, ty, vw) { }

    void setup_setLocation(Eris::Entity * e) {
        setLocation(e);
    }
    void setup_setAttr(const std::string &p, const Atlas::Message::Element &v)
    {
        setAttr(p, v);
    }
    
};


int main()
{
    Eris::Logged.connect(sigc::ptr_fun(writeLog));
    Eris::setLogLevel(Eris::LOG_DEBUG);
    {
        TestConnection con("name", "localhost",
                                                    6767, true);

        TestAccount acc(&con);
        
        std::string fake_char_id("1");
        TestAvatar ea(&acc, fake_char_id);
        acc.setup_insertActiveCharacters(&ea);
        TestEntity* char_ent = new TestEntity(fake_char_id, 0, ea.getView());
        ea.setup_setEntity(char_ent);
        
        typedef std::map<std::string, Atlas::Message::Element> ElementStore;
        ElementStore modTypes;
        ElementStore shapes;
        
        ElementStore levelMods;
        Atlas::Message::MapType levelMod1;
        levelMod1["type"] = Atlas::Message::Element("levelmod");
        levelMods["1"] = levelMod1;
        Atlas::Message::MapType levelMod2(levelMod1);
        levelMod2["height"] = 20;
        levelMods["2"] = levelMod2;
        Atlas::Message::MapType levelMod3(levelMod1);
        levelMod2["heightoffset"] = 30;
        levelMods["3"] = levelMod3;
        
        
        ElementStore adjustMods;
        Atlas::Message::MapType adjustMod1;
        adjustMod1["type"] = Atlas::Message::Element("adjustmod");
        adjustMods["1"] = levelMod1;
        Atlas::Message::MapType adjustMod2(adjustMod1);
        adjustMod2["height"] = 20;
        adjustMods["2"] = adjustMod2;
        Atlas::Message::MapType adjustMod3(adjustMod1);
        adjustMod2["heightoffset"] = 30;
        adjustMods["3"] = adjustMod3;
        
        Atlas::Message::MapType craterMod1;
        craterMod1["type"] = Atlas::Message::Element("cratermod");
        
        ElementStore slopeMods;
        Atlas::Message::MapType slopeMod1;
        slopeMod1["type"] = Atlas::Message::Element("slopemod");
        Atlas::Message::ListType slopes;
        slopes.push_back(10);
        slopes.push_back(20);
        slopeMod1["slopes"] = slopes;
        slopeMods["1"] = slopeMod1;
        
        
        Atlas::Message::MapType shapeBall;
        shapeBall["radius"] = 15;
        shapeBall["type"] = "ball";
        shapes["ball"] = shapeBall;
        
        Atlas::Message::MapType shapePolygon;
        Atlas::Message::ListType points;
        points.push_back(WFMath::Point<2>(0,0).toAtlas());
        points.push_back(WFMath::Point<2>(10,0).toAtlas());
        points.push_back(WFMath::Point<2>(10,10).toAtlas());
        points.push_back(WFMath::Point<2>(0,10).toAtlas());
        shapePolygon["points"] = points;
        shapePolygon["type"] = "polygon";
        shapes["polygon"] = shapePolygon;
        
        shapes["empty"] = Atlas::Message::MapType();
        
        //no terrain mod info
        {
            Atlas::Message::MapType emptyElement;
            TestEntity* mod_ent = new TestEntity("2", 0, ea.getView());
            mod_ent->setup_setAttr("terrainmod", emptyElement);
            
            Eris::TerrainMod mod(mod_ent);
            assert(!mod.init());
            
        }
        
        //no shape
        {
            Atlas::Message::MapType modElement = levelMod1;
            TestEntity* mod_ent = new TestEntity("2", 0, ea.getView());
            mod_ent->setup_setAttr("terrainmod", modElement);
            
            Eris::TerrainMod mod(mod_ent);
            assert(!mod.init());
            
        }       
         
        //test level mod
        for (ElementStore::iterator I = levelMods.begin(); I != levelMods.end(); ++I) {
            for (ElementStore::iterator J = shapes.begin(); J != shapes.end(); ++J) {
                std::cout << "Testing level mod " << I->first << " with " << J->first << std::endl;
                Atlas::Message::Element modElement = I->second;
                modElement.asMap()["shape"] = J->second;
                TestEntity* mod_ent = new TestEntity("2", 0, ea.getView());
                mod_ent->setup_setAttr("terrainmod", modElement);
                
                Eris::TerrainMod mod(mod_ent);
                if (J->first == "empty") {
                    assert(!mod.init());
                } else {
                    assert(mod.init());
                }
            }
        }        
        
        //test adjust mod
        for (ElementStore::iterator I = adjustMods.begin(); I != adjustMods.end(); ++I) {
            for (ElementStore::iterator J = shapes.begin(); J != shapes.end(); ++J) {
                std::cout << "Testing adjust mod " << I->first << " with " << J->first << std::endl;
                Atlas::Message::Element modElement = I->second;
                modElement.asMap()["shape"] = J->second;
                TestEntity* mod_ent = new TestEntity("2", 0, ea.getView());
                mod_ent->setup_setAttr("terrainmod", modElement);
                
                Eris::TerrainMod mod(mod_ent);
                if (J->first == "empty") {
                    assert(!mod.init());
                } else {
                    assert(mod.init());
                }
            }
        }
                
        //test slope mod
        for (ElementStore::iterator I = slopeMods.begin(); I != slopeMods.end(); ++I) {
            for (ElementStore::iterator J = shapes.begin(); J != shapes.end(); ++J) {
                std::cout << "Testing slope mod " << I->first << " with " << J->first << std::endl;
                Atlas::Message::Element modElement = I->second;
                modElement.asMap()["shape"] = J->second;
                TestEntity* mod_ent = new TestEntity("2", 0, ea.getView());
                mod_ent->setup_setAttr("terrainmod", modElement);
                
                Eris::TerrainMod mod(mod_ent);
                if (J->first == "empty") {
                    assert(!mod.init());
                } else {
                    assert(mod.init());
                }
            }
        }
        
        //test crater mod
        {
            Atlas::Message::MapType modElement = craterMod1;
            modElement["shape"] = shapeBall;
            TestEntity* mod_ent = new TestEntity("2", 0, ea.getView());
            mod_ent->setup_setAttr("terrainmod", modElement);
            
            Eris::TerrainMod mod(mod_ent);
            assert(mod.init());
            
        }
    }
    return 0;
}