Skip to content

Commit

Permalink
feat(bumping): overhaul bumping code
Browse files Browse the repository at this point in the history
fixes #29
  • Loading branch information
Insineer committed Sep 11, 2019
1 parent b28d15b commit dc9537f
Show file tree
Hide file tree
Showing 20 changed files with 72 additions and 62 deletions.
4 changes: 4 additions & 0 deletions GameLogic/Object.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ def Update(self, timeElapsed):

def InteractedBy(self, object):
return False

def BumpedTo(self, object):
print(self.name + " bumped to " + object.name)
return False
3 changes: 1 addition & 2 deletions GameLogic/Objects/Creature.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def __init__(self):

self.layer = 75
self.name = "Creature"
self.density = True

self.__seeInvisibleAbility = False

Expand All @@ -20,8 +21,6 @@ def __init__(self):
self.control = self.GetComponent("Control")
self.DefineUI(self.control.ui)

self.AddVerb("drop", lambda player: self.Drop())

@property
def seeInvisibleAbility(self):
return self.__seeInvisibleAbility
Expand Down
2 changes: 1 addition & 1 deletion GameLogic/Objects/Creatures/Human.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def __init__(self):
super().__init__()
self.name = "Python Human"
self.sprite = "human"
self.density = True
self.PutOn(CreateObject("Objects.Items.Clothes.Uniform", None))
self.__updateActiveHandIcon()
self.AddVerb("drop", lambda player: self.Drop())

# IHasOrgans methods
def CreateOrgans(self):
Expand Down
21 changes: 8 additions & 13 deletions GameLogic/Objects/Projectile.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from Engine_Geometry import DirectionSet, Direction

from Object import Object
from Objects.Creature import Creature

Expand All @@ -7,27 +9,20 @@ def __init__(self):
self.name = "Stun Orb"
self.layer = 100
self.sprite = "stunorb"
self.density = False
self.density = True
self.startTile = None
self.__absoluteSpeed = 7.0
self.__direction = None

def Update(self, timeElapsed):
super().Update(timeElapsed)

if self.startTile is None:
self.startTile = self.tile
else:
if self.startTile is not self.tile:
if self.tile.IsDense():
self.Hit(self.tile.GetDenseObject())
def BumpedTo(self, object):
super().BumpedTo(object)
self.__hit(object)
return True

def SetShotDirection(self, direction):
direction = direction.Normalize()
self.speed = direction * self.__absoluteSpeed
self.__direction = direction

def Hit(self, hittedObject):
def __hit(self, hittedObject):
if isinstance(hittedObject, Creature):
hittedObject.Stun()
self.Delete()
8 changes: 5 additions & 3 deletions GameLogic/Objects/Turfs/Airlock.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from Engine_Geometry import Direction
from Objects.Turf import Turf

class Airlock(Turf):
def __init__(self):
super().__init__()
self.name = "Airlock"
self.sprite = "airlock"
self.density = True
self.__closedSolidity = [Direction.CENTER]

self.solidity.Add(self.__closedSolidity)
self.opened = False
self.locked = False

Expand All @@ -27,7 +29,7 @@ def Activate(self):
return
self.sprite = "airlock"
self.opened = False
self.density = True
self.solidity.Add(self.__closedSolidity)
else:
if not self.PlayAnimation("airlock_opening", lambda: self.__animationOpeningCallback()):
return
Expand All @@ -41,7 +43,7 @@ def Unlock(self):

def __animationOpeningCallback(self):
self.opened = True
self.density = False
self.solidity.Reset()

def __autocloseCallback(self):
if self.opened:
Expand Down
3 changes: 2 additions & 1 deletion GameLogic/Objects/Turfs/Wall.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from Engine_Geometry import Direction
from Objects.Turf import Turf

class Wall(Turf):
Expand All @@ -6,4 +7,4 @@ def __init__(self):
self.isWall = True
self.name = "Wall"
self.sprite = "wall"
self.density = True
self.solidity.Add([Direction.CENTER])
2 changes: 1 addition & 1 deletion OSS13 Client/Sources/Graphics/TileGrid/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,5 @@ bool Object::PixelTransparent(uf::vec2i pixel) const {
Tile *Object::GetTile() { return tile; }
sf::Vector2f Object::GetShift() const { return shift; }
sf::Vector2i Object::GetMoveIntent() const { return moveIntent; }
bool Object::IsDense() const { return solidity.IsExistsOne({uf::Direction::CENTER}); }
bool Object::IsDense() const { return density; }
uf::DirectionSet Object::GetSolidity() const { return solidity; }
1 change: 1 addition & 0 deletions OSS13 Client/Sources/Graphics/TileGrid/Object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Object {
uf::Direction direction;
uint layer;

bool density;
uf::DirectionSet solidity;
uf::DirectionSetFractional opacity;

Expand Down
3 changes: 1 addition & 2 deletions OSS13 Client/Sources/Graphics/TileGrid/Tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ bool Tile::IsBlocked() const {
return false;
}

bool Tile::IsBlocked(const std::initializer_list<uf::Direction> &directions) const {
bool Tile::IsBlocked(uf::DirectionSet directions) const {
for (auto &obj : content)
if (obj->GetSolidity().IsExistsOne(directions)) return true;
return false;
}

2 changes: 1 addition & 1 deletion OSS13 Client/Sources/Graphics/TileGrid/Tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Tile : public INonCopyable {
Object *GetObject(uint id);
TileGrid *GetTileGrid();
bool IsBlocked() const;
bool IsBlocked(const std::initializer_list<uf::Direction> &directions) const;
bool IsBlocked(uf::DirectionSet directions) const;

friend sf::Packet &operator>>(sf::Packet &packet, Tile &tile);
friend std::unique_ptr<Tile> CreateTileWithInfo(TileGrid *tileGrid, const network::protocol::TileInfo &tileInfo);
Expand Down
8 changes: 4 additions & 4 deletions OSS13 Client/Sources/Graphics/TileGrid/TileGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,14 @@ void MovementPrediction(Object *controllable, uf::vec2i moveCommand) {
if (controllable->IsDense()) {
auto moveDirection = uf::VectToDirection(moveIntent);

if (lastTile->IsBlocked({ moveDirection })) { // exit from current tile
if (lastTile->IsBlocked(uf::DirectionSet({ moveDirection }))) { // exit from current tile
moveIntent = controllable->GetMoveIntent();
} else {
if (!newTileDiag || newTileDiag->IsBlocked({ uf::InvertDirection(moveDirection), uf::Direction::CENTER })) {
if (!newTileDiag || newTileDiag->IsBlocked(uf::DirectionSet({ uf::InvertDirection(moveDirection), uf::Direction::CENTER }))) {
return;
} else {
if (!newTileX || newTileX != lastTile && newTileX->IsBlocked({ uf::InvertDirection(xDirection), yDirection, uf::Direction::CENTER })) return;
if (!newTileY || newTileY != lastTile && newTileY->IsBlocked({ uf::InvertDirection(yDirection), xDirection, uf::Direction::CENTER })) return;
if (!newTileX || newTileX != lastTile && newTileX->IsBlocked(uf::DirectionSet({ uf::InvertDirection(xDirection), yDirection, uf::Direction::CENTER }))) return;
if (!newTileY || newTileY != lastTile && newTileY->IsBlocked(uf::DirectionSet({ uf::InvertDirection(yDirection), xDirection, uf::Direction::CENTER }))) return;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions OSS13 Client/Sources/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ std::unique_ptr<Object> CreateObjectWithInfo(const network::protocol::ObjectInfo
object->name = objectInfo.name;
object->layer = objectInfo.layer;
object->direction = objectInfo.direction;
object->density = objectInfo.density;
object->solidity = objectInfo.solidity;
object->opacity = objectInfo.opacity;
object->moveSpeed = objectInfo.moveSpeed;
Expand Down
2 changes: 1 addition & 1 deletion OSS13 Server/Sources/ScriptEngine/Module/WorldModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ PYBIND11_EMBEDDED_MODULE(Engine_World, m) {
.def_property_readonly("z", &Tile::Z)
.def_property_readonly("pos", &Tile::GetPos)
.def_property_readonly("map", &Tile::GetMap)
.def("IsDense", py::overload_cast<>(&Tile::IsDense, py::const_))
.def("IsDense", &Tile::IsDense)
.def("IsSpace", &Tile::IsSpace)
.def("GetDenseObject", &Tile::GetDenseObject, py::return_value_policy::reference);

Expand Down
4 changes: 4 additions & 0 deletions OSS13 Server/Sources/ScriptEngine/Trampoline/PyObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class PyObject : public Object, public std::enable_shared_from_this<PyObject> {
PYBIND11_OVERLOAD_PURE_NAME(bool, Object, "InteractedBy", InteractedBy, obj);
}

bool BumpedTo(Object *obj) override {
PYBIND11_OVERLOAD_PURE_NAME(bool, Object, "BumpedTo", BumpedTo, obj);
}

bool RemoveObject(Object *obj) override {
PYBIND11_OVERLOAD_NAME(bool, Object, "RemoveObject", RemoveObject, obj);
}
Expand Down
27 changes: 15 additions & 12 deletions OSS13 Server/Sources/World/Objects/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ void Object::Update(std::chrono::microseconds timeElapsed) {

if (iconsOutdated) {
updateIcons();
auto diff = std::make_shared<network::protocol::UpdateIconsDiff>();
diff->objId = ID();
for (auto &iconInfo : icons)
diff->iconsIds.push_back(iconInfo.id + static_cast<uint32_t>(iconInfo.state));
GetTile()->AddDiff(diff, this);
iconsOutdated = false;
if (GetTile()) {
auto diff = std::make_shared<network::protocol::UpdateIconsDiff>();
diff->objId = ID();
for (auto &iconInfo : icons)
diff->iconsIds.push_back(iconInfo.id + static_cast<uint32_t>(iconInfo.state));
GetTile()->AddDiff(diff, this);
iconsOutdated = false;
}
}

animationTimer.Update(timeElapsed);
Expand Down Expand Up @@ -97,15 +99,15 @@ void Object::Move(uf::vec2i order) {
if (GetDensity()) {
auto moveDirection = uf::VectToDirection(moveIntent);

if (tile->IsDense({moveDirection})) { // exit from current tile
if (tile->IsDense(DirectionSet({moveDirection}))) { // exit from current tile
moveIntent = GetMoveIntent();
} else {
if (!newTileDiag || newTileDiag->IsDense({uf::InvertDirection(moveDirection), uf::Direction::CENTER})) {
if (!newTileDiag || newTileDiag->IsDense((DirectionSet({uf::InvertDirection(moveDirection), uf::Direction::CENTER})))) {
return;
}
else {
if (!newTileX || newTileX != tile && newTileX->IsDense({ uf::InvertDirection(xDirection), yDirection, uf::Direction::CENTER })) return;
if (!newTileY || newTileY != tile && newTileY->IsDense({ uf::InvertDirection(yDirection), xDirection, uf::Direction::CENTER })) return;
if (!newTileX || newTileX != tile && newTileX->IsDense((DirectionSet({ uf::InvertDirection(xDirection), yDirection, uf::Direction::CENTER })))) return;
if (!newTileY || newTileY != tile && newTileY->IsDense((DirectionSet({ uf::InvertDirection(yDirection), xDirection, uf::Direction::CENTER })))) return;
}
}
}
Expand Down Expand Up @@ -204,8 +206,8 @@ bool Object::PlayAnimation(const std::string &animation, std::function<void()> c
return true;
}

bool Object::GetDensity() const { return solidity.IsExistsOne({Direction::CENTER}); };
void Object::SetDensity(bool density) { density ? solidity.Add({Direction::CENTER}) : solidity.Remove({Direction::CENTER}); }
bool Object::GetDensity() const { return density; };
void Object::SetDensity(bool density) { this->density = density; }

void Object::SetSolidity(uf::DirectionSet directions) { solidity = directions; }
const uf::DirectionSet &Object::GetSolidity() const { return solidity; }
Expand Down Expand Up @@ -316,6 +318,7 @@ network::protocol::ObjectInfo Object::GetObjectInfo() const {
objectInfo.name = name;
objectInfo.layer = layer;
objectInfo.direction = direction;
objectInfo.density = density;
objectInfo.solidity = solidity;
objectInfo.opacity = opacity;
objectInfo.moveSpeed = moveSpeed;
Expand Down
2 changes: 2 additions & 0 deletions OSS13 Server/Sources/World/Objects/Object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Object : public VerbsHolder, public INonCopyable {
virtual void Update(std::chrono::microseconds timeElapsed);

virtual bool InteractedBy(Object *) = 0;
virtual bool BumpedTo(Object *) = 0;

virtual void Move(uf::vec2i order);
virtual void MoveZ(int order) {};
Expand Down Expand Up @@ -135,6 +136,7 @@ class Object : public VerbsHolder, public INonCopyable {
uint layer;
uf::Direction direction;

bool density{false}; // object can't pass through solid objects
uf::DirectionSet solidity;
uf::DirectionSetFractional opacity;
uf::DirectionSetFractional airtightness;
Expand Down
32 changes: 15 additions & 17 deletions OSS13 Server/Sources/World/Tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,24 @@ bool Tile::MoveTo(Object *obj) {
return false;
}

if (obj->GetDensity())
for (auto &object : content)
if (object)
if (object->GetDensity()) {
return false;
}

Tile *lastTile = obj->GetTile();
rpos delta = GetPos() - lastTile->GetPos();
uf::Direction direction = uf::VectToDirection(delta);

if (obj->GetDensity()) {
auto bumpedTo = lastTile->GetDenseObject(DirectionSet({ direction }));
if (!bumpedTo)
bumpedTo = GetDenseObject(DirectionSet({uf::InvertDirection(direction), uf::Direction::CENTER}));
if (bumpedTo) {
obj->BumpedTo(bumpedTo);
return false;
}
}

if (abs(delta.x) > 1 || abs(delta.y) > 1)
LOGW << "Warning! Moving more than a one tile. (Tile::MoveTo)";
if (delta.z)
LOGW << "Warning! Moving between Z-levels. (Tile::MoveTo)";
const uf::Direction direction = uf::VectToDirection(delta);

auto relocateAwayDiff = std::make_shared<network::protocol::RelocateAwayDiff>(); // TODO: MoveAway???
relocateAwayDiff->objId = obj->ID();
Expand Down Expand Up @@ -196,10 +200,10 @@ const std::list<Object *> &Tile::Content() const {
return content;
}

Object *Tile::GetDenseObject() const
Object *Tile::GetDenseObject(DirectionSet directions) const
{
for (auto &obj : content)
if (obj->GetDensity()) return obj;
if (obj->GetSolidity().IsExistsOne(directions)) return obj;
return nullptr;
}

Expand All @@ -209,13 +213,7 @@ uf::vec3i Tile::GetPos() const {

Map *Tile::GetMap() const { return map; }

bool Tile::IsDense() const {
for (auto &obj : content)
if (obj->GetDensity()) return true;
return false;
}

bool Tile::IsDense(const std::initializer_list<uf::Direction> &directions) const {
bool Tile::IsDense(uf::DirectionSet directions) const {
for (auto &obj : content)
if (obj->GetSolidity().IsExistsOne(directions)) return true;
return false;
Expand Down
5 changes: 2 additions & 3 deletions OSS13 Server/Sources/World/Tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ class Tile {
void PlaceTo(Object *);

const std::list<Object *> &Content() const;
Object *GetDenseObject() const;
Object *GetDenseObject(uf::DirectionSet directions) const;

uf::vec3i GetPos() const;
Map *GetMap() const;
bool IsDense() const;
bool IsDense(const std::initializer_list<uf::Direction> &directions) const;
bool IsDense(uf::DirectionSet directions) const;
bool IsSpace() const;
Locale *GetLocale() const;

Expand Down
2 changes: 1 addition & 1 deletion SharedLibrary/Sources/Shared/Geometry/DirectionSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace uf {
class DirectionSet {
public:
DirectionSet() = default;
explicit DirectionSet(std::list<Direction> directions);
DirectionSet(std::list<Direction> directions);

DirectionSet(const DirectionSet &) = default;
DirectionSet(DirectionSet &&) = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ DEFINE_SERIALIZABLE(ObjectInfo, uf::ISerializable)
std::vector<uint32_t> spriteIds;
uint32_t layer;
uf::Direction direction;
bool density;
uf::DirectionSet solidity;
uf::DirectionSetFractional opacity;

Expand All @@ -28,6 +29,7 @@ DEFINE_SERIALIZABLE(ObjectInfo, uf::ISerializable)
ar & spriteIds;
ar & layer;
ar & direction;
ar & density;
ar & solidity;
ar & opacity;
ar & moveSpeed;
Expand Down

0 comments on commit dc9537f

Please sign in to comment.