// // map_write.cpp // BoE // // Created by Celtic Minstrel on 15-07-09. // // #include #include "catch.hpp" #include "map_parse.hpp" #include "scenario.hpp" #include "regtown.hpp" using namespace std; extern void loadOutMapData(map_data&& data, location which, cScenario& scen); extern void loadTownMapData(map_data&& data, int which, cScenario& scen); extern map_data buildOutMapData(location which, cScenario& scenario); extern map_data buildTownMapData(size_t which, cScenario& scenario); static void in_and_out(string name, map_data& map, bool town) { string fpath = "junk/"; fpath += name; fpath += ".map"; ofstream fout; fout.exceptions(ios::badbit); fout.open(fpath); map.writeTo(fout); fout.close(); // Reconstruct the map, to ensure that it doesn't just pass due to old data still being around map.~map_data(); new(&map) map_data(); ifstream fin; fin.exceptions(ios::badbit); fin.open(fpath); map = load_map(fin, town, name); } static void in_and_out(cScenario& scen, bool town) { map_data map = town ? buildTownMapData(0, scen) : buildOutMapData(loc(0,0), scen); // Reconstruct the scenario, to ensure that it doesn't just pass due to old data still being around scen.~cScenario(); new(&scen) cScenario(); scen.ter_types.resize(50); if(town) { // Reconstruct the town, to ensure that it doesn't just pass due to old data still being around scen.addTown(); loadTownMapData(move(map), 0, scen); } else { // Reconstruct the sector, to ensure that it doesn't just pass due to old data still being around scen.outdoors.resize(1,1); scen.outdoors[0][0] = new cOutdoors(scen); loadOutMapData(move(map), loc(0,0), scen); } } TEST_CASE("Saving map data to file") { map_data map; vector> test; for(int x = 0; x < 5; x++) for(int y = 0; y < 5; y++) map.set(x, y, 1 + y * 5 + x); SECTION("Basic") { in_and_out("basic", map, true); for(int y = 0; y < 5; y++) { for(int x = 0; x < 5; x++) { int ter = 1 + y * 5 + x; CAPTURE(x); CAPTURE(y); CAPTURE(ter); CHECK(map.get(x, y) == ter); } } } SECTION("With vehicles") { map.addFeature(0, 0, eMapFeature::BOAT, 1); map.addFeature(1, 1, eMapFeature::BOAT, -2); map.addFeature(2, 2, eMapFeature::HORSE, 3); map.addFeature(3, 3, eMapFeature::HORSE, -4); in_and_out("vehicles", map, true); test.emplace_back(make_pair(eMapFeature::BOAT, 1)); CHECK(map.getFeatures(0, 0) == test); test[0].second = -2; CHECK(map.getFeatures(1, 1) == test); test[0].first = eMapFeature::HORSE; test[0].second = 3; CHECK(map.getFeatures(2, 2) == test); test[0].second = -4; CHECK(map.getFeatures(3, 3) == test); } } TEST_CASE("Building map data") { ifstream fin; map_data map; fin.exceptions(ios::badbit); cScenario scen; scen.outdoors.resize(1,1); scen.outdoors[0][0] = new cOutdoors(scen); scen.addTown(); scen.ter_types.resize(50); for(int x = 0; x < 5; x++) for(int y = 0; y < 5; y++) { int n = 1 + y * 5 + x; scen.towns[0]->terrain(x, y) = n; scen.outdoors[0][0]->terrain[x][y] = n; } SECTION("Basic") { in_and_out(scen, true); for(int y = 0; y < 5; y++) { for(int x = 0; x < 5; x++) { int ter = 1 + y * 5 + x; CAPTURE(x); CAPTURE(y); CAPTURE(ter); CHECK(scen.towns[0]->terrain(x, y) == ter); } } } SECTION("With vehicles") { scen.boats[0].property = true; scen.boats[0].loc = {1,1}; scen.boats[1].property = false; scen.boats[1].loc = {2,2}; scen.horses[0].property = true; scen.horses[0].loc = {3,3}; scen.horses[1].property = false; scen.horses[1].loc = {4,4}; SECTION("Outdoors") { scen.boats[0].which_town = scen.horses[0].which_town = 200; scen.boats[1].which_town = scen.horses[1].which_town = 200; scen.boats[0].sector = scen.horses[0].sector = {0,0}; scen.boats[1].sector = scen.horses[1].sector = {0,0}; in_and_out(scen, false); CHECK(scen.boats[0].property); CHECK(scen.boats[0].loc == loc(1,1)); CHECK(scen.boats[0].which_town == 200); CHECK(scen.boats[0].sector == loc(0,0)); CHECK_FALSE(scen.boats[1].property); CHECK(scen.boats[1].loc == loc(2,2)); CHECK(scen.boats[1].which_town == 200); CHECK(scen.boats[1].sector == loc(0,0)); CHECK(scen.horses[0].property); CHECK(scen.horses[0].loc == loc(3,3)); CHECK(scen.horses[0].which_town == 200); CHECK(scen.horses[0].sector == loc(0,0)); CHECK_FALSE(scen.horses[1].property); CHECK(scen.horses[1].loc == loc(4,4)); CHECK(scen.horses[1].which_town == 200); CHECK(scen.horses[1].sector == loc(0,0)); } SECTION("In town") { scen.boats[0].which_town = scen.horses[0].which_town = 0; scen.boats[1].which_town = scen.horses[1].which_town = 0; in_and_out(scen, true); CHECK(scen.boats[0].property); CHECK(scen.boats[0].loc == loc(1,1)); CHECK(scen.boats[0].which_town == 0); CHECK_FALSE(scen.boats[1].property); CHECK(scen.boats[1].loc == loc(2,2)); CHECK(scen.boats[1].which_town == 0); CHECK(scen.horses[0].property); CHECK(scen.horses[0].loc == loc(3,3)); CHECK(scen.horses[0].which_town == 0); CHECK_FALSE(scen.horses[1].property); CHECK(scen.horses[1].loc == loc(4,4)); CHECK(scen.horses[1].which_town == 0); } } }