Testing Complete.
[asgard.git] / src / Database.cpp
blob14bf9d5202f4d7b93ffebcfc67c24589fb7eb780
1 /*****************************************************************************
2 * Copyright (c) 2007 Russ Adams, Sean Eubanks, Asgard Contributors
3 * This file is part of Asgard.
4 *
5 * Asgard is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * Asgard is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Asgard; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 ****************************************************************************/
20 #include <cassert>
21 #include "Database.h"
22 #include "MapObjectFactory.h"
23 #include "MapObjectType.h"
24 #include "QueryGenerator.h"
25 #include "DatabaseColumnMap.h"
26 #include "GameEngine.h"
27 #include "RowSet.h"
29 Database* Database::instance = NULL;
31 Database::Database()
33 int status = sqlite3_open(ASGARD_DATABASE, &this->asgardDb);
35 if(status != SQLITE_OK)
37 sqlite3_close(this->asgardDb);
40 // TODO: Add some type of logging for status
43 Database::~Database()
45 sqlite3_close(this->asgardDb);
48 Database* Database::getInstance()
50 if(instance == NULL) instance = new Database();
51 return instance;
54 void Database::determineVisibleBoxes(Coordinate currentPosition, int *visibleBoxes, int numVisibleBoxes)
56 // Map as of 0.3.0 is 1 boundbox, 0.
57 visibleBoxes[0] = 1;
60 bool Database::loadBoundingBox(int boxId)
62 char* query;
63 MapObjectType type;
64 char *sqliteErrorCode;
66 // Invalid Bounding Box
67 if (boxId <= 0)
68 return false;
70 // NonPlayerCharacter
71 query = QueryGenerator::nonPlayerCharacter(boxId);
72 type = MAP_OBJECT_TYPE_NON_PLAYER_CHARACTER;
73 sqlite3_exec(this->asgardDb, query, MapObjectFactory::processRow, (void*)&type, &sqliteErrorCode);
74 delete query;
76 // Container
77 query = QueryGenerator::container(boxId);
78 type = MAP_OBJECT_TYPE_CONTAINER;
79 sqlite3_exec(this->asgardDb, query, MapObjectFactory::processRow, (void*)&type, &sqliteErrorCode);
80 delete query;
82 // StaticMapObject
83 query = QueryGenerator::staticMapObject(boxId);
84 type = MAP_OBJECT_TYPE_STATIC_MAP_OBJECT;
85 sqlite3_exec(this->asgardDb, query, MapObjectFactory::processRow, (void*)&type, &sqliteErrorCode);
86 delete query;
88 // Tile
89 query = QueryGenerator::tile(boxId);
90 type = MAP_OBJECT_TYPE_TILE;
91 sqlite3_exec(this->asgardDb, query, MapObjectFactory::processRow, (void*)&type, &sqliteErrorCode);
92 delete query;
94 sqlite3_free(sqliteErrorCode);
96 return true;
99 RowSet* Database::loadHardpoints(int smoId)
101 RowSet* rs = new RowSet();
102 char* query;
103 int rc;
105 query = QueryGenerator::hardpoint(smoId);
106 rc = rs->select(this->asgardDb, query);
107 delete query;
109 if (rc == SQLITE_OK)
111 assert(rs->getColCount() == HARDPOINT_COLUMN_COUNT);
114 return rs;
117 RowSet* Database::loadNonPlayerCharacterPath(int npcId)
119 RowSet* rs = new RowSet();
120 char* query;
121 int rc;
123 query = QueryGenerator::nonPlayerCharacterPath(npcId);
124 rc = rs->select(this->asgardDb, query);
125 delete query;
127 if (rc == SQLITE_OK)
129 assert(rs->getColCount() == NON_PLAYER_CHARACTER_PATH_COLUMN_COUNT);
132 return rs;