Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ A directional move starts from the location named by the entity's location id (l

Entities are matched by `Entity::getId()`, not by address. `Location::isEntityPresent`, `Location::removeEntity`, and `Environment::getEntity` all compare ids, so two distinct `Entity` objects that share an id are treated as the same entity. Removing through such a twin takes the stored entity out of its location but resets only the twin's ids, so the stored entity keeps ids describing a placement it no longer has. Entity ids should therefore be unique within an environment.

A newly constructed entity has an environment id and grid id of `-1` and a location id of `"N/S"`. Placing it through an `Environment` sets all three; `Environment::removeEntity` resets them to those same values.
A newly constructed entity has an environment id and grid id of `-1` and a location id of `"N/S"`. Placing it through an `Environment` sets all three; `Environment::removeEntity` resets them to those same values. The lower layers set only the ids they know: `Grid::addEntity` and `Grid::addEntityToLocation` set the grid id and location id but not the environment id, and `Grid::removeEntity` resets the grid id and location id but leaves the environment id unchanged. `Location::addEntity` and `Location::removeEntity` set and reset only the location id.

`Environment::removeEntity` and `Grid::removeEntity` do nothing when the entity is not present. Calling either on an entity that is not in that environment or grid — including one placed in a different environment — leaves its environment id, grid id, location id, and actual placement unchanged. `Location::removeEntity` likewise leaves the location id of an entity that is not in that location unchanged.

Expand Down
55 changes: 55 additions & 0 deletions src/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,59 @@ void testMovingEntityToItsCurrentLocationLeavesItThere() {
std::cout << " --- " << "Success" << std::endl;
}

void testPlacingThroughGridOrLocationSetsOnlyThatLayersIds() {
std::cout << "Test 84 - Placing and removing through a grid or a location sets only the ids that layer knows";
Grid grid(3, 2);
Entity placedInGrid(0, "Daniel");
grid.addEntity(placedInGrid);
assert(placedInGrid.getGridId() == 3);
assert(placedInGrid.getEnvironmentId() == -1);
assert(grid.getLocation(placedInGrid.getLocationId()).isEntityPresent(&placedInGrid) == true);
assert(grid.getNumEntities() == 1);

// the grid has no knowledge of an environment, so removal leaves the environment id alone
placedInGrid.setEnvironmentId(9);
grid.removeEntity(placedInGrid);
assert(placedInGrid.getGridId() == -1);
assert(placedInGrid.getLocationId() == "N/S");
assert(placedInGrid.getEnvironmentId() == 9);

Entity placedAtLocation(2, "Phil");
assert(grid.addEntityToLocation(placedAtLocation, grid.getLocationByCoordinates(1, 1)) == true);
assert(placedAtLocation.getGridId() == 3);
assert(placedAtLocation.getLocationId() == "3-1-1");
assert(placedAtLocation.getEnvironmentId() == -1);

Location location("standalone", 0, 0);
Entity placedInLocation(1, "Bob");
placedInLocation.setGridId(4);
placedInLocation.setEnvironmentId(5);
location.addEntity(&placedInLocation);
assert(placedInLocation.getLocationId() == "standalone");
assert(placedInLocation.getGridId() == 4);
assert(placedInLocation.getEnvironmentId() == 5);
location.removeEntity(&placedInLocation);
assert(placedInLocation.getLocationId() == "N/S");
assert(placedInLocation.getGridId() == 4);
assert(placedInLocation.getEnvironmentId() == 5);
std::cout << " --- " << "Success" << std::endl;
}

void testRetrievingRandomLocationReturnsAGridElement() {
std::cout << "Test 85 - Retrieving a random location returns a reference into the grid's own locations";
Grid grid(0, 3);
std::vector<Location>& locations = grid.getLocations();
for (size_t i = 0; i < 100; i++) {
Location* random = &grid.getRandomLocation();
assert(random >= &locations.front() && random <= &locations.back());
}
Entity entity(0, "Daniel");
grid.getRandomLocation().addEntity(&entity);
assert(grid.isEntityPresent(entity) == true);
assert(grid.getNumEntities() == 1);
std::cout << " --- " << "Success" << std::endl;
}

void seedRandomNumberGenerator() {
srand (time (NULL));
}
Expand Down Expand Up @@ -1528,5 +1581,7 @@ int main() {
testMovingEntityWhoseLocationIdNamesNoGridLocationThrows();
testEnvironmentOfSizeZeroHasNoLocationsAndLookupsThrow();
testMovingEntityToItsCurrentLocationLeavesItThere();
testPlacingThroughGridOrLocationSetsOnlyThatLayersIds();
testRetrievingRandomLocationReturnsAGridElement();
return 0;
}
Loading