-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatedungeon.cpp
220 lines (193 loc) · 6.51 KB
/
createdungeon.cpp
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
/* createdungeon
* =============
*
* DEPENDENCIES
* dungeonmaker 2.05+ ( http://dungeonmaker.sourceforge.net )
*
* COMPILE WITH
* g++ -o createdungeon createdungeon.cpp -lDungeonMaker
*
* CREATED AT
* 9. Apr 2009
*
* AUTHOR
* wolfger@spearwolf.de
*/
#include <iostream>
#include <sstream>
#include <cstdio>
//#include <cstdlib>
#include "./dungeonmaker/DungeonMaker.h"
using namespace std;
using namespace alifegames;
char toChar(SquareData sd) // {{{
{
char c;
switch (sd) {
case OPEN: c = ' '; break;
case CLOSED: c = '#'; break;
case G_CLOSED: c = '#'; break; // GUARANTEED-OPEN AND GUARANTEED-CLOSED
case G_OPEN: c = ' '; break;
case NJ_OPEN: c = '.'; break; // NV = non-join, these cannot be joined br Builders
case NJ_G_OPEN: c = '_'; break; // with others of their own kind
case NJ_CLOSED: c = 'x'; break;
case NJ_G_CLOSED: c = 'X'; break;
case IR_OPEN: c = '\''; break; // inside-room, open
case IT_OPEN: c = ','; break; // inside-tunnel, open
case IA_OPEN: c = '^'; break; // inside anteroom, open
case H_DOOR: c = '='; break; // horizontal door
case V_DOOR: c = '|'; break; // vertical door
case MOB1: c = '1'; break; // MOBs of different level - higher is better
case MOB2: c = '2'; break;
case MOB3: c = '3'; break;
case TREAS1: c = '4'; break; // treasure of different level
case TREAS2: c = '5'; break;
case TREAS3: c = '6'; break;
case COLUMN: c = '@'; break;
default: c = '?';
}
return c;
}
// }}}
int toInt(SquareData sd) // {{{
{
int i;
switch (sd) {
case OPEN: i = 0; break;
case CLOSED: i = 1; break;
case G_CLOSED: i = 2; break; // GUARANTEED-OPEN AND GUARANTEED-CLOSED
case G_OPEN: i = 3; break;
case NJ_OPEN: i = 4; break; // NV = non-join, these cannot be joined br Builders
case NJ_G_OPEN: i = 5; break; // with others of their own kind
case NJ_CLOSED: i = 6; break;
case NJ_G_CLOSED: i = 7; break;
case IR_OPEN: i = 8; break; // inside-room, open
case IT_OPEN: i = 9; break; // inside-tunnel, open
case IA_OPEN: i = 10; break; // inside anteroom, open
case H_DOOR: i = 11; break; // horizontal door
case V_DOOR: i = 12; break; // vertical door
case MOB1: i = 13; break; // MOBs of different level - higher is better
case MOB2: i = 14; break;
case MOB3: i = 15; break;
case TREAS1: i = 16; break; // treasure of different level
case TREAS2: i = 17; break;
case TREAS3: i = 18; break;
case COLUMN: i = 19; break;
default: i = -1;
}
return i;
}
// }}}
void printMapToStdout(DungeonMaker* theDungeonMaker) // {{{
{
for (int x = 0; x < theDungeonMaker->GetDimX() ; ++x) {
ostringstream row;
for (int y = 0; y < theDungeonMaker->GetDimY() ; ++y) {
row << toChar(theDungeonMaker->GetMap(x, y));
}
cout << row.str() << endl;
}
}
// }}}
const char* convertMapToString(DungeonMaker* theDungeonMaker) // {{{
{
ostringstream out;
for (int x = 0; x < theDungeonMaker->GetDimX() ; ++x) {
for (int y = 0; y < theDungeonMaker->GetDimY() ; ++y) {
out << toChar(theDungeonMaker->GetMap(x, y));
}
out << endl;
}
return out.str().c_str();
}
// }}}
void convertMapToDataArray(DungeonMaker* theDungeonMaker, int* arr) // {{{
{
int width = theDungeonMaker->GetDimX();
int height = theDungeonMaker->GetDimY();
arr[0] = width;
arr[1] = height;
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
arr[2 + (y*width) + x] = toInt(theDungeonMaker->GetMap(x, y));
}
}
}
// }}}
const char* convertMapToJsonStr(DungeonMaker* theDungeonMaker) // {{{
{
ostringstream out;
char buf_w[128];
//char buf_h[128];
int width = theDungeonMaker->GetDimX();
int height = theDungeonMaker->GetDimY();
//if (height < 0 || height > 10000) {
//height = width;
//}
out << "{\"width\":";
std::snprintf(buf_w, 128, "%i", width);
out << buf_w;
//cout << "W" << width << ", " << buf_w << endl;
//out << "," << endl << "\"height\":";
//std::snprintf(buf_h, 128, "%i", height);
//out << buf_h;
//cout << "H" << height << ", " << buf_h << endl;
out << "," << endl << "\"data\":[" << endl;
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
out << toInt(theDungeonMaker->GetMap(x, y));
if (x == width - 1 && y == height - 1) {
out << "]}";
} else {
out << ",";
}
out << endl;
}
}
out << endl;
//cout << out.str().c_str() << endl;
return out.str().c_str();
}
// }}}
/*
extern "C" const char* createDungeon(char* designFile, unsigned int seed)
{
//unsigned int seed = 666; //rand();
DungeonMaker theDungeonMaker;
theDungeonMaker.Init_fromFile(designFile, seed);
theDungeonMaker.generate();
//return convertMapToString(&theDungeonMaker);
return convertMapToJsonStr(&theDungeonMaker);
}
*/
int* dungeonArray = 0;
extern "C" int* createDungeonData(char* designFile, unsigned int seed)
{
DungeonMaker theDungeonMaker;
theDungeonMaker.Init_fromFile(designFile, seed);
theDungeonMaker.generate();
if (dungeonArray) {
delete[] dungeonArray;
}
dungeonArray = new int[2 + (theDungeonMaker.GetDimX()*theDungeonMaker.GetDimY())];
convertMapToDataArray(&theDungeonMaker, dungeonArray);
return dungeonArray;
}
int main(int argc, char** argv)
{
/*
unsigned int seed = 0;
DungeonMaker theDungeonMaker;
if (argc != 2) {
cerr << "simple usage: " << argv[0] << " " << "<dungeon-config-file>" << endl;
exit(1);
}
theDungeonMaker.Init_fromFile(argv[1], seed);
//theDungeonMaker.Init_fromFile("design2", seed);
theDungeonMaker.generate();
printMapToStdout(&theDungeonMaker);
//cout << convertMapToJsonStr(&theDungeonMaker) << endl;
cerr << "Thank you and have a nice day." << endl;
*/
return 0;
}