2 #include <jumpnrun/levels.h>
3 #include <jumpnrun/items.h>
4 #include <jumpnrun/tiles.h>
5 #include <jumpnrun/enemies.h>
8 #include <boost/spirit/include/qi_symbols.hpp>
24 #define PATH_PREFIX "../badge/jumpnrun/"
30 ("tube_top_left" , JUMPNRUN_TILE_TYPE_TUBE_TOP_LEFT
)
31 ("tube_top_right", JUMPNRUN_TILE_TYPE_TUBE_TOP_RIGHT
)
32 ("tube_left" , JUMPNRUN_TILE_TYPE_TUBE_LEFT
)
33 ("tube_right" , JUMPNRUN_TILE_TYPE_TUBE_RIGHT
)
34 ("brick" , JUMPNRUN_TILE_TYPE_BRICK
)
35 ("square" , JUMPNRUN_TILE_TYPE_SQUARE
)
39 ("doc", JUMPNRUN_ITEM_TYPE_DOCUMENT
)
43 ("cat" , JUMPNRUN_ENEMY_TYPE_CAT
)
44 ("mushroom" , JUMPNRUN_ENEMY_TYPE_MUSHROOM
)
45 ("kaninchen", JUMPNRUN_ENEMY_TYPE_KANINCHEN
)
49 boost::spirit::qi::symbols
<char, unsigned> tiles
;
50 boost::spirit::qi::symbols
<char, unsigned> items
;
51 boost::spirit::qi::symbols
<char, unsigned> enemies
;
54 struct level_name_map
{
56 std::ifstream
in(PATH_PREFIX
"levels.txt");
59 throw std::logic_error(PATH_PREFIX
"levels.txt konnte nicht geƶffnet werden.");
63 while(std::getline(in
, name
)) {
65 names
.push_back(name
);
70 std::vector
<std::string
> names
;
75 level(std::string
const &name
)
77 level_lines(LEVEL_LINE_COUNT
)
79 std::ifstream
in((PATH_PREFIX
+ name
+ ".lv").c_str());
82 throw std::invalid_argument("Could not open file: " + name
+ ".lv");
85 for(std::size_t i
= 0; i
< level_lines
.size(); ++i
) {
86 std::getline(in
, level_lines
[i
]);
90 std::string type_prefix
;
91 std::map
<char, std::string
> *objmap
= 0;
93 while(std::getline(in
, line
)) {
95 if(line
== "[tiles]") {
97 } else if(line
== "[items]") {
99 } else if(line
== "[enemies]") {
100 objmap
= &enemy_types
;
102 throw std::invalid_argument("Unkown type: " + line
);
104 } else if(line
!= "") {
107 std::istringstream
parser(line
);
109 if(parser
>> c
>> tok
) {
114 throw std::invalid_argument("Line not parseable: " + line
);
120 std::size_t length() const {
121 std::size_t level_length
= 0;
123 for(std::size_t i
= 0; i
< level_lines
.size(); ++i
) {
124 level_length
= std::max(level_length
, level_lines
[i
].size());
130 unsigned find_type(boost::spirit::qi::symbols
<char, unsigned> const &types
, std::string
const &type
, std::string
const &error
) const {
131 unsigned const *p
= types
.find(type
);
132 if(!p
) throw std::invalid_argument(error
+ type
);
136 std::size_t count_things(std::map
<char, std::string
> const &objmap
) const {
137 std::size_t len
= length();
140 for(std::size_t x
= 0; x
< len
; ++x
) {
141 for(std::size_t y
= 0; y
< level_lines
.size(); ++y
) {
142 if(x
< level_lines
[y
].size()) {
143 auto iter
= objmap
.find(level_lines
[y
][x
]);
145 if(iter
!= objmap
.end()) {
155 std::size_t count_tiles () const { return count_things(tile_types
); }
156 std::size_t count_items () const { return count_things(item_types
); }
157 std::size_t count_enemies() const { return count_things(enemy_types
); }
159 void dump_things(std::ostream
&dest
, std::map
<char, std::string
> const &objmap
, boost::spirit::qi::symbols
<char, unsigned> const &types
, std::string
const &error
) const {
160 std::size_t len
= length();
162 for(std::size_t x
= 0; x
< len
; ++x
) {
163 for(std::size_t y
= 0; y
< level_lines
.size(); ++y
) {
164 if(level_lines
[y
].size() < x
) continue;
166 auto iter
= objmap
.find(level_lines
[y
][x
]);
168 if(iter
!= objmap
.end()) {
169 unsigned char buf
[3];
170 buf
[0] = static_cast<uint8_t>(y
) << 4 | static_cast<uint8_t>(x
>> 8);
171 buf
[1] = static_cast<uint8_t>(x
);
172 buf
[2] = static_cast<uint8_t>(find_type(types
, iter
->second
, error
));
174 dest
.write(static_cast<char const*>(static_cast<void*>(&buf
[0])), sizeof(buf
));
180 void dump_tiles (std::ostream
&dest
) const { dump_things(dest
, tile_types
, desc
.tiles
, "Unbekannter Tile-Typ: "); }
181 void dump_items (std::ostream
&dest
) const { dump_things(dest
, item_types
, desc
.items
, "Unbekannter Item-Typ: "); }
182 void dump_enemies(std::ostream
&dest
) const { dump_things(dest
, enemy_types
, desc
.enemies
, "Unbekannter Enemy-Typ: "); }
184 std::pair
<std::size_t, std::size_t> starting_position() const {
185 std::pair
<std::size_t, std::size_t> player_pos(0, 0);
187 for(std::size_t y
= 0; y
< level_lines
.size(); ++y
) {
188 std::size_t x
= level_lines
[y
].find('P');
190 if(x
!= std::string::npos
) {
191 player_pos
.first
= x
;
192 player_pos
.second
= y
;
200 void generate_header(jumpnrun_level
*dest
) const {
201 dest
->header
.tile_count
= count_tiles ();
202 dest
->header
.item_count
= count_items ();
203 dest
->header
.enemy_count
= count_enemies();
206 void dump(std::ostream
&dest
) const {
207 jumpnrun_level dummy
;
209 generate_header(&dummy
);
211 static_cast<uint16_t>(dummy
.header
. tile_count
),
212 static_cast<uint16_t>(dummy
.header
. item_count
),
213 static_cast<uint16_t>(dummy
.header
.enemy_count
)
216 std::pair
<std::size_t, std::size_t> player_pos
= starting_position();
219 static_cast<uint16_t>(player_pos
.first
),
220 static_cast<uint16_t>(player_pos
.second
)
223 dest
.write(static_cast<char const *>(static_cast<void const *>(head
)), sizeof(head
));
224 dest
.write(static_cast<char const *>(static_cast<void const *>(spos
)), sizeof(spos
));
233 std::vector
<std::string
> level_lines
;
234 std::map
<char, std::string
> tile_types
;
235 std::map
<char, std::string
> item_types
;
236 std::map
<char, std::string
> enemy_types
;
240 void jumpnrun_level_dump(size_t level
) {
241 std::string name
= jnrcpp::level_names
.names
[level
];
242 jnrcpp::level
lv(name
);
243 std::ofstream
out((PATH_PREFIX
+ name
+ ".lvl").c_str());
248 void jumpnrun_levels_dump(void) {
249 for(size_t i
= 0; i
< jnrcpp::level_names
.names
.size(); ++i
) {
250 jumpnrun_level_dump(i
);
255 jumpnrun_levels_dump();