From 4bfa5b213257416f997d01b087c9e8bbb91cb3b9 Mon Sep 17 00:00:00 2001 From: Philipp Sehmisch Date: Wed, 27 Dec 2006 20:33:16 +0000 Subject: Clients are now notified when other clients near them perform attacks. --- src/defines.h | 2 ++ src/gamehandler.cpp | 9 +++++++++ src/object.h | 7 +++++-- src/player.cpp | 16 ++++++++++++++++ src/player.h | 9 +++++++++ src/state.cpp | 27 ++++++++++++++++++++++++--- 6 files changed, 65 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/defines.h b/src/defines.h index 86367813..dc594311 100644 --- a/src/defines.h +++ b/src/defines.h @@ -153,6 +153,8 @@ enum { GPMSG_BEINGS_MOVE = 0x0280, // { W being id, B flags [, C position] [, W*2 destination] }* PGMSG_SAY = 0x02A0, // S text GPMSG_SAY = 0x02A1, // W being id, S text + PGMSG_ATTACK = 0x0290, // - + GPMSG_BEING_ATTACK = 0x0291, // W being id PGMSG_USE_ITEM = 0x0300, // L item id GPMSG_USE_RESPONSE = 0x0301, // B error PGMSG_EQUIP = 0x0302, // L item id, B slot diff --git a/src/gamehandler.cpp b/src/gamehandler.cpp index 968325a7..e2a37e32 100644 --- a/src/gamehandler.cpp +++ b/src/gamehandler.cpp @@ -219,6 +219,15 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) ERRMSG_OK : ERRMSG_FAILURE); } break; + case PGMSG_ATTACK: + { + LOG_DEBUG ( "Player " << + computer.getCharacter()->getPublicID() << + " attacks", + 0); + computer.getCharacter()->setAttacking(true); + } break; + default: LOG_WARN("Invalid message type", 0); result.writeShort(XXMSG_INVALID); diff --git a/src/object.h b/src/object.h index 8c771aa2..63d6c115 100644 --- a/src/object.h +++ b/src/object.h @@ -33,7 +33,8 @@ enum { NEW_ON_MAP = 1, - NEW_DESTINATION = 2 + NEW_DESTINATION = 2, + ATTACK = 4 }; /** @@ -183,12 +184,14 @@ class MovingObject: public Object void setPublicID(int id) { mPublicID = id; } + protected: + unsigned short mActionTime; /**< delay until next action */ + private: unsigned short mPublicID; /**< Object ID sent to clients (unique with respect to the map) */ Point mDst; /**< target coordinates */ Point mOld; /**< old coordinates */ unsigned short mSpeed; /**< speed */ - unsigned short mActionTime; /**< delay until next action */ }; /** diff --git a/src/player.cpp b/src/player.cpp index 40650157..14882712 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -22,6 +22,8 @@ #include "player.h" +#include "defines.h" + #include void Player::setDatabaseID(int id) @@ -42,6 +44,20 @@ void Player::update() setStat(STAT_MAGIC, 10 + mRawStats.stats[STAT_INTELLIGENCE]); setStat(STAT_ACCURACY, 50 + mRawStats.stats[STAT_DEXTERITY]); setStat(STAT_SPEED, mRawStats.stats[STAT_DEXTERITY]); + + // attacking + if (mIsAttacking) + { + // plausibility check of attack command + if (mActionTime <= 0) + { + // perform attack + mActionTime = 1000; + mIsAttacking = false; + raiseUpdateFlags (ATTACK); + //TODO: attack mechanics + } + } } void Player::setInventory(const Inventory &inven) diff --git a/src/player.h b/src/player.h index 83f5fee1..f3d1c35e 100644 --- a/src/player.h +++ b/src/player.h @@ -42,6 +42,7 @@ class Player : public Being : Being(OBJECT_PLAYER, 65535), mDatabaseID(id), mName(name), + mIsAttacking(false), mClient(NULL) {} @@ -202,6 +203,12 @@ class Player : public Being bool unequip(unsigned char slot); + /** + * Set attacking state + **/ + void setAttacking(bool isAttacking) + { mIsAttacking = isAttacking; } + /** * Gets database ID. * @@ -238,6 +245,8 @@ class Player : public Being Inventory inventory; /**< Player inventory and Equipment */ + bool mIsAttacking; /**< attacking state */ + friend class GameClient; }; diff --git a/src/state.cpp b/src/state.cpp index 9da71adc..b4853f68 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -88,18 +88,39 @@ State::update() for (MovingObjectIterator o(map->getAroundPlayerIterator(*p)); o; ++o) { + Point os = (*o)->getOldPosition(); Point on = (*o)->getPosition(); + int flags = 0; + + // Handle attacking + if ( (*o)->getUpdateFlags() & ATTACK + && (*o)->getPublicID() != (*p)->getPublicID() + && (*p)->getPosition().inRangeOf(on) + ) + { + MessageOut AttackMsg (GPMSG_BEING_ATTACK); + AttackMsg.writeShort((*o)->getPublicID()); + + LOG_DEBUG( "Sending attack packet from " << + (*o)->getPublicID() << + " to " << + (*p)->getPublicID(), + 0 + ); + + gameHandler->sendTo(*p, AttackMsg); + } + + // Handle moving + /* Check whether this player and this moving object were around * the last time and whether they will be around the next time. */ bool wereInRange = (*p)->getOldPosition().inRangeOf(os) && !(((*p)->getUpdateFlags() | (*o)->getUpdateFlags()) & NEW_ON_MAP); bool willBeInRange = (*p)->getPosition().inRangeOf(on); - - int flags = 0; - if (!wereInRange) { // o was outside p's range. -- cgit v1.2.3-60-g2f50