Skip to content

Commit

Permalink
Merge branch 'release/1.2.32'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bam4d committed Feb 9, 2022
2 parents 1d3613e + 5de3497 commit 14eefad
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. mac/linux/windows]
- Version [e.g. 1.2.31]
- Version [e.g. 1.2.32]

**Additional context**
Add any other context about the problem here.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.10.0)
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.9" CACHE STRING "Minimum OS X deployment version")
project(Griddly VERSION 1.2.31)
project(Griddly VERSION 1.2.32)

set(BINARY ${CMAKE_PROJECT_NAME})

Expand Down
2 changes: 1 addition & 1 deletion bindings/python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace griddly {

PYBIND11_MODULE(python_griddly, m) {
m.doc() = "Griddly python bindings";
m.attr("version") = "1.2.31";
m.attr("version") = "1.2.32";

#ifndef NDEBUG
spdlog::set_level(spdlog::level::debug);
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
author = 'Chris Bamford'

# The full version, including alpha/beta/rc tags
release = '1.2.31'
release = '1.2.32'


# -- General configuration ---------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def griddly_package_data(config='Debug'):

setup(
name='griddly',
version="1.2.31",
version="1.2.32",
author_email="[email protected]",
description="Griddly Python Libraries",
long_description=long_description,
Expand Down
65 changes: 53 additions & 12 deletions src/Griddly/Core/LevelGenerators/MapGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void MapGenerator::reset(std::shared_ptr<Grid> grid) {

spdlog::debug("Adding object {0} to environment at location ({1},{2})", objectName, location[0], location[1]);
auto object = objectGenerator_->newInstance(objectName, playerId, grid);
grid->addObject(location, object);
grid->addObject(location, object, true, nullptr, DiscreteOrientation(objectData.initialDirection));
}
}
}
Expand All @@ -81,14 +81,16 @@ void MapGenerator::parseFromStream(std::istream& stream) {

char currentPlayerId[3];
int playerIdIdx = 0;
Direction currentDirection = Direction::NONE;

char prevChar;

while (auto ch = stream.get()) {
switch (ch) {
case EOF:
if (state == MapReaderState::READ_PLAYERID) {
addObject(currentObjectName, currentPlayerId, playerIdIdx, colCount, rowCount);
if (state == MapReaderState::READ_PLAYERID || state == MapReaderState::READ_INITIAL_ORIENTATION) {
addObject(currentObjectName, currentPlayerId, playerIdIdx, colCount, rowCount, currentDirection);
currentDirection = Direction::NONE;
state = MapReaderState::READ_NORMAL;
}
width_ = firstColCount;
Expand All @@ -103,8 +105,9 @@ void MapGenerator::parseFromStream(std::istream& stream) {

case '\n':
if (state == MapReaderState::READ_PLAYERID) {
addObject(currentObjectName, currentPlayerId, playerIdIdx, colCount, rowCount);
addObject(currentObjectName, currentPlayerId, playerIdIdx, colCount, rowCount, currentDirection);
state = MapReaderState::READ_NORMAL;
currentDirection = Direction::NONE;
colCount++;
}

Expand All @@ -122,27 +125,44 @@ void MapGenerator::parseFromStream(std::istream& stream) {
// Do nothing on whitespace
case ' ':
case '\t':
if (state == MapReaderState::READ_PLAYERID) {
addObject(currentObjectName, currentPlayerId, playerIdIdx, colCount, rowCount);
if (state == MapReaderState::READ_PLAYERID || state == MapReaderState::READ_INITIAL_ORIENTATION) {
addObject(currentObjectName, currentPlayerId, playerIdIdx, colCount, rowCount, currentDirection);
state = MapReaderState::READ_NORMAL;
currentDirection = Direction::NONE;
colCount++;
}
break;

case '.': // dots just signify an empty space
if (state == MapReaderState::READ_PLAYERID) {
addObject(currentObjectName, currentPlayerId, playerIdIdx, colCount, rowCount);
if (state == MapReaderState::READ_PLAYERID || state == MapReaderState::READ_INITIAL_ORIENTATION) {
addObject(currentObjectName, currentPlayerId, playerIdIdx, colCount, rowCount, currentDirection);
state = MapReaderState::READ_NORMAL;
currentDirection = Direction::NONE;
colCount++;
}
colCount++;
prevChar = ch;
break;

case '/':
if (state == MapReaderState::READ_PLAYERID) {
addObject(currentObjectName, currentPlayerId, playerIdIdx, colCount, rowCount);
if (state == MapReaderState::READ_PLAYERID || state == MapReaderState::READ_INITIAL_ORIENTATION) {
addObject(currentObjectName, currentPlayerId, playerIdIdx, colCount, rowCount, currentDirection);
state = MapReaderState::READ_NORMAL;
currentDirection = Direction::NONE;
}
prevChar = ch;
break;

case '[':
if (state == MapReaderState::READ_PLAYERID) {
state = MapReaderState::READ_INITIAL_ORIENTATION;
}
prevChar = ch;
break;

case ']':
if (state != MapReaderState::READ_INITIAL_ORIENTATION) {
throw std::invalid_argument(fmt::format("Invalid closing bracket ']' for initial orientation in map row={0}", rowCount));
}
prevChar = ch;
break;
Expand All @@ -160,13 +180,33 @@ void MapGenerator::parseFromStream(std::istream& stream) {
currentPlayerId[playerIdIdx] = ch;
playerIdIdx++;
} else {
addObject(currentObjectName, currentPlayerId, playerIdIdx, colCount, rowCount);
addObject(currentObjectName, currentPlayerId, playerIdIdx, colCount, rowCount, currentDirection);
currentObjectName = objectGenerator_->getObjectNameFromMapChar(ch);
playerIdIdx = 0;
currentDirection = Direction::NONE;
memset(currentPlayerId, 0x00, 3);
colCount++;
}
} break;
case MapReaderState::READ_INITIAL_ORIENTATION: {
switch (ch) {
case 'U':
currentDirection = Direction::UP;
break;
case 'D':
currentDirection = Direction::DOWN;
break;
case 'L':
currentDirection = Direction::LEFT;
break;
case 'R':
currentDirection = Direction::RIGHT;
break;
default:
throw std::invalid_argument(fmt::format("Unknown direction character {0} at in map row={1}", ch, rowCount));
break;
}
} break;
}
prevChar = ch;
break;
Expand All @@ -175,11 +215,12 @@ void MapGenerator::parseFromStream(std::istream& stream) {
}
}

void MapGenerator::addObject(std::string objectName, char* playerIdString, int playerIdStringLength, uint32_t x, uint32_t y) {
void MapGenerator::addObject(std::string& objectName, char* playerIdString, int playerIdStringLength, uint32_t x, uint32_t y, Direction direction) {
auto playerId = playerIdStringLength > 0 ? atoi(playerIdString) : 0;
GridInitInfo gridInitInfo;
gridInitInfo.objectName = objectName;
gridInitInfo.playerId = playerId;
gridInitInfo.initialDirection = direction;
spdlog::debug("Adding object={0} with playerId={1} to location [{2}, {3}]", objectName, playerId, x, y);

auto location = glm::ivec2(x, y);
Expand Down
6 changes: 4 additions & 2 deletions src/Griddly/Core/LevelGenerators/MapGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ struct GridInitInfo {
std::string objectName;
int32_t playerId;
int32_t zIdx;
Direction initialDirection;
};

enum class MapReaderState {
READ_NORMAL,
READ_PLAYERID
READ_PLAYERID,
READ_INITIAL_ORIENTATION,
};

class MapGenerator : public LevelGenerator {
Expand All @@ -39,6 +41,6 @@ class MapGenerator : public LevelGenerator {

const std::shared_ptr<ObjectGenerator> objectGenerator_;

void addObject(std::string objectName, char* playerIdString, int playerIdStringLength, uint32_t x, uint32_t y);
void addObject(std::string& objectName, char* playerIdString, int playerIdStringLength, uint32_t x, uint32_t y, Direction direction);
};
} // namespace griddly
70 changes: 70 additions & 0 deletions tests/src/Griddly/Core/LevelGenerator/MapReaderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,74 @@ TEST(MapGeneratorTest, testLoadStringMultipleOccupants) {
EXPECT_TRUE(Mock::VerifyAndClearExpectations(mockObjectGeneratorPtr.get()));
}

TEST(MapGeneratorTest, testLoadStringInitialOrientation) {
auto mockObjectGeneratorPtr = std::shared_ptr<MockObjectGenerator>(new MockObjectGenerator());
auto mockGridPtr = std::shared_ptr<MockGrid>(new MockGrid());
auto mockWallObject = std::shared_ptr<MockObject>(new MockObject());
auto mockAvatarObject = std::shared_ptr<MockObject>(new MockObject());
auto mockDefaultObject = std::shared_ptr<MockObject>(new MockObject());

std::shared_ptr<MapGenerator> mapReader(new MapGenerator(1, mockObjectGeneratorPtr));

std::string wallObjectName = "wall";
std::string avatarObjectName = "avatar";

auto objectDefinitions = mockObjectDefinitions({wallObjectName, avatarObjectName});

EXPECT_CALL(*mockObjectGeneratorPtr, getObjectDefinitions())
.WillRepeatedly(Return(objectDefinitions));

EXPECT_CALL(*mockGridPtr, initObject(Eq(wallObjectName), Eq(std::vector<std::string>{})))
.Times(1);

EXPECT_CALL(*mockGridPtr, initObject(Eq(avatarObjectName), Eq(std::vector<std::string>{})))
.Times(1);

std::map<std::string, std::unordered_map<uint32_t, std::shared_ptr<int32_t>>> globalVariables{};
EXPECT_CALL(*mockGridPtr, getGlobalVariables)
.WillRepeatedly(ReturnRef(globalVariables));

EXPECT_CALL(*mockObjectGeneratorPtr, getObjectNameFromMapChar(Eq('W')))
.Times(12)
.WillRepeatedly(ReturnRef(wallObjectName));

EXPECT_CALL(*mockObjectGeneratorPtr, getObjectNameFromMapChar(Eq('P')))
.Times(1)
.WillRepeatedly(ReturnRef(avatarObjectName));

EXPECT_CALL(*mockObjectGeneratorPtr, newInstance(Eq("_empty"), Eq(0), Eq(mockGridPtr)))
.Times(1)
.WillRepeatedly(Return(mockDefaultObject));

EXPECT_CALL(*mockObjectGeneratorPtr, newInstance(Eq("_empty"), Eq(1), Eq(mockGridPtr)))
.Times(1)
.WillRepeatedly(Return(mockDefaultObject));

EXPECT_CALL(*mockObjectGeneratorPtr, newInstance(Eq(wallObjectName), Eq(0), Eq(mockGridPtr)))
.Times(12)
.WillRepeatedly(Return(mockWallObject));

EXPECT_CALL(*mockObjectGeneratorPtr, newInstance(Eq(avatarObjectName), Eq(1), Eq(mockGridPtr)))
.Times(1)
.WillRepeatedly(Return(mockAvatarObject));

EXPECT_CALL(*mockGridPtr, resetMap(Eq(5), Eq(3)))
.Times(1);

EXPECT_CALL(*mockGridPtr, addObject(_, Eq(mockWallObject), Eq(true), Eq(nullptr), Eq(DiscreteOrientation())))
.Times(12);

EXPECT_CALL(*mockGridPtr, addObject(_, Eq(mockAvatarObject), Eq(true), Eq(nullptr), Eq(DiscreteOrientation(Direction::DOWN))))
.Times(1);

std::string levelString = "W W W W W\nW . P1[D] . W\n W W W W W";
auto levelStringStream = std::stringstream(levelString);

mapReader->parseFromStream(levelStringStream);
mapReader->reset(mockGridPtr);

EXPECT_TRUE(Mock::VerifyAndClearExpectations(mockGridPtr.get()));
EXPECT_TRUE(Mock::VerifyAndClearExpectations(mockObjectGeneratorPtr.get()));
}

} // namespace griddly

0 comments on commit 14eefad

Please sign in to comment.