forked from calref/cboe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtown_write.cpp
156 lines (151 loc) · 5.45 KB
/
town_write.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
//
// town_write.cpp
// BoE
//
// Created by Celtic Minstrel on 15-09-02.
//
//
#include <fstream>
#include "catch.hpp"
#include "tinyprint.h"
#include "scenario.hpp"
#include "regtown.hpp"
using namespace std;
using namespace ticpp;
extern Document xmlDocFromStream(istream& stream, string name);
extern void readTownFromXml(Document&& data, cTown*& town, cScenario& scen);
extern void writeTownToXml(ticpp::Printer&& data, cTown& town);
static void in_and_out(string name, cTown*& town, cScenario& scen) {
string fpath = "junk/town_";
fpath += name;
fpath += ".xml";
ofstream fout;
fout.exceptions(ios::badbit);
fout.open(fpath);
writeTownToXml(Printer(name, fout), *town);
fout.close();
// Reconstruct the town, to ensure that it doesn't just pass due to old data still being around
delete town;
ifstream fin;
fin.exceptions(ios::badbit);
fin.open(fpath);
readTownFromXml(xmlDocFromStream(fin, name), town, scen);
}
TEST_CASE("Saving a town") {
cScenario scen;
cTown* town = new cTinyTown(scen);
town->town_name = "Test Town";
town->in_town_rect = {2,3,30,29};
town->difficulty = 1;
town->lighting_type = LIGHT_NONE;
SECTION("With the minimal required information") {
in_and_out("basic", town, scen);
CHECK(town->max_dim() == 32);
CHECK(town->town_name == "Test Town");
CHECK(town->in_town_rect == rect(2,3,30,29));
CHECK(town->difficulty == 1);
CHECK(town->lighting_type == LIGHT_NONE);
CHECK_FALSE(town->has_tavern);
CHECK_FALSE(town->defy_scrying);
}
SECTION("With lots of optional information") {
town->comment[2] = "Try a comment!";
town->spec_on_entry = 42;
town->spec_on_entry_if_dead = 19;
town->spec_on_hostile = 47;
town->exit_specs[0] = 16;
town->exit_locs[0] = {24,2};
town->town_chop_time = 25;
town->town_chop_key = 6;
town->max_num_monst = 100000;
town->is_hidden = town->has_tavern = true;
town->defy_scrying = town->defy_mapping = true;
town->strong_barriers = true;
REQUIRE(town->timers.size() >= 1);
town->timers[0].node = 12;
town->timers[0].time = 2500;
REQUIRE(town->wandering.size() >= 1);
town->wandering[0].monst = {7,8,9,10};
town->sign_locs.emplace_back(0,0,"Sign #4279816");
town->spec_strs.emplace_back("Something! With extra spaces!");
town->room_rect.emplace_back(0,0,1,1,"Unknown Area . . .");
in_and_out("optional", town, scen);
CHECK(town->comment[2] == "Try a comment!");
CHECK(town->spec_on_entry == 42);
CHECK(town->spec_on_entry_if_dead == 19);
CHECK(town->spec_on_hostile == 47);
CHECK(town->exit_specs[0] == 16);
CHECK(town->exit_locs[0] == loc(24,2));
CHECK(town->town_chop_time == 25);
CHECK(town->town_chop_key == 6);
CHECK(town->max_num_monst == 100000);
CHECK(town->is_hidden);
CHECK(town->strong_barriers);
CHECK(town->has_tavern);
CHECK(town->defy_mapping);
CHECK(town->defy_scrying);
REQUIRE(town->timers.size() >= 1);
CHECK(town->timers[0].node_type == 2);
CHECK(town->timers[0].node == 12);
CHECK(town->timers[0].time == 2500);
REQUIRE(town->wandering.size() >= 1);
CHECK(town->wandering[0].monst[0] == 7);
CHECK(town->wandering[0].monst[1] == 8);
CHECK(town->wandering[0].monst[2] == 9);
CHECK(town->wandering[0].monst[3] == 10);
REQUIRE(town->sign_locs.size() >= 1);
CHECK(town->sign_locs[0].text == "Sign #4279816");
REQUIRE(town->spec_strs.size() >= 1);
CHECK(town->spec_strs[0] == "Something! With extra spaces!");
REQUIRE(town->room_rect.size() >= 1);
CHECK(town->room_rect[0].descr == "Unknown Area . . .");
}
SECTION("With a preset item") {
town->preset_items.emplace_back();
town->preset_items.back().code = 52;
town->preset_items.back().ability = 9;
town->preset_items.back().charges = 102;
town->preset_items.back().always_there = true;
town->preset_items.back().property = true;
town->preset_items.back().contained = true;
in_and_out("item", town, scen);
REQUIRE(town->preset_items.size() >= 1);
CHECK(town->preset_items[0].code == 52);
CHECK(town->preset_items[0].ability == 9);
CHECK(town->preset_items[0].charges == 102);
CHECK(town->preset_items[0].always_there);
CHECK(town->preset_items[0].property);
CHECK(town->preset_items[0].contained);
}
SECTION("With a townsperson") {
town->creatures.emplace_back();
town->creatures.back().number = 60;
town->creatures.back().start_attitude = eAttitude::DOCILE;
town->creatures.back().mobility = 1;
town->creatures.back().spec1 = 17;
town->creatures.back().spec2 = 4;
town->creatures.back().spec_enc_code = 3;
town->creatures.back().time_flag = eMonstTime::DISAPPEAR_WHEN_EVENT;
town->creatures.back().time_code = 5;
town->creatures.back().monster_time = 16;
town->creatures.back().facial_pic = 56;
town->creatures.back().personality = 8;
town->creatures.back().special_on_kill = 9;
town->creatures.back().special_on_talk = 12;
in_and_out("townsperson", town, scen);
REQUIRE(town->creatures.size() >= 1);
CHECK(town->creatures[0].number == 60);
CHECK(town->creatures[0].start_attitude == eAttitude::DOCILE);
CHECK(town->creatures[0].mobility == 1);
CHECK(town->creatures[0].spec1 == 17);
CHECK(town->creatures[0].spec2 == 4);
CHECK(town->creatures[0].spec_enc_code == 3);
CHECK(town->creatures[0].time_flag == eMonstTime::DISAPPEAR_WHEN_EVENT);
CHECK(town->creatures[0].monster_time == 16);
CHECK(town->creatures[0].time_code == 5);
CHECK(town->creatures[0].facial_pic == 56);
CHECK(town->creatures[0].personality == 8);
CHECK(town->creatures[0].special_on_kill == 9);
CHECK(town->creatures[0].special_on_talk == 12);
}
}