From 60b89174c6b6300079344267bae60d74347e1fa5 Mon Sep 17 00:00:00 2001 From: fusion32 Date: Wed, 11 Jun 2025 03:30:51 -0300 Subject: some announce functions --- src/connection.hh | 1 + src/cr.hh | 7 ++ src/cract.cc | 6 +- src/crmain.cc | 5 +- src/crskill.cc | 21 +++--- src/magic.cc | 5 +- src/operate.cc | 196 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/operate.hh | 15 +++++ src/stubs.hh | 15 +++++ 9 files changed, 247 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/connection.hh b/src/connection.hh index d3a53df..ec79a6e 100644 --- a/src/connection.hh +++ b/src/connection.hh @@ -15,6 +15,7 @@ struct TKnownCreature { struct TConnection { void Logout(int Delay, bool StopFight); + bool IsVisible(int x, int y, int z); // DATA // ================= diff --git a/src/cr.hh b/src/cr.hh index 92521bd..162cb68 100644 --- a/src/cr.hh +++ b/src/cr.hh @@ -403,6 +403,13 @@ struct TCombat{ // TFindCreatures // ============================================================================= +enum : int { + FIND_PLAYERS = 0x01, + FIND_NPCS = 0x02, + FIND_MONSTERS = 0x04, + FIND_ALL = FIND_PLAYERS | FIND_NPCS | FIND_MONSTERS, +}; + struct TFindCreatures { TFindCreatures(int RadiusX, int RadiusY, int CenterX, int CenterY, int Mask); TFindCreatures(int RadiusX, int RadiusY, uint32 CreatureID, int Mask); diff --git a/src/cract.cc b/src/cract.cc index d6fd2ee..325fbdc 100644 --- a/src/cract.cc +++ b/src/cract.cc @@ -395,7 +395,7 @@ void TCreature::Go(int DestX, int DestY, int DestZ){ void TCreature::Rotate(int Direction){ this->Direction = Direction; - AnnounceChangedObject(this->CrObject, 2); // OBJECT_CHANGED ? + AnnounceChangedObject(this->CrObject, OBJECT_CHANGED); } void TCreature::Rotate(TCreature *Target){ @@ -1561,7 +1561,7 @@ void TCreature::NotifyChangeInventory(void){ SkillsChanged = true; Skill->DAct = NewDelta[SkillNr]; if(SkillNr == SKILL_GO_STRENGTH){ - AnnounceChangedCreature(this->ID, 4); // CREATURE_GO_STRENGTH_CHANGED ? + AnnounceChangedCreature(this->ID, CREATURE_SPEED_CHANGED); }else if(SkillNr == SKILL_ILLUSION){ if(NewDelta[SKILL_ILLUSION] > 0){ if(this->Outfit.OutfitID != 0 || this->Outfit.ObjectType != 0){ @@ -1574,7 +1574,7 @@ void TCreature::NotifyChangeInventory(void){ }else if(Skill->TimerValue() == 0){ this->Outfit = this->OrgOutfit; } - AnnounceChangedCreature(this->ID, 3); // CREATURE_OUTFIT_CHANGED ? + AnnounceChangedCreature(this->ID, CREATURE_OUTFIT_CHANGED); } } } diff --git a/src/crmain.cc b/src/crmain.cc index 00cd3e2..52a7c13 100644 --- a/src/crmain.cc +++ b/src/crmain.cc @@ -2,6 +2,7 @@ #include "config.hh" #include "enums.hh" #include "info.hh" +#include "operate.hh" #include "stubs.hh" @@ -590,7 +591,7 @@ int TCreature::Damage(TCreature *Attacker, int Damage, int DamageType){ && this->Outfit.ObjectType == 0){ this->SetTimer(SKILL_ILLUSION, 0, 0, 0, -1); this->Outfit = this->OrgOutfit; - AnnounceChangedCreature(this->ID, 3); // CREATURE_OUTFIT_CHANGED ? + AnnounceChangedCreature(this->ID, CREATURE_OUTFIT_CHANGED); NotifyAllCreatures(this->CrObject, 2, NONE); // CREATURE_APPEAR ? } @@ -834,7 +835,7 @@ int TCreature::Damage(TCreature *Attacker, int Damage, int DamageType){ } } - AnnounceChangedCreature(this->ID, 1); // CREATURE_HITPOINTS_CHANGED ? + AnnounceChangedCreature(this->ID, CREATURE_HEALTH_CHANGED); return Damage; } diff --git a/src/crskill.cc b/src/crskill.cc index b1a9d42..f400dc9 100644 --- a/src/crskill.cc +++ b/src/crskill.cc @@ -1,4 +1,5 @@ #include "cr.hh" +#include "operate.hh" #include "stubs.hh" @@ -350,7 +351,7 @@ bool TSkillLevel::Jump(int Range){ this->Master->Skills[SKILL_GO_STRENGTH ]->Advance(Range); this->Master->Skills[SKILL_CARRY_STRENGTH]->Advance(Range); - AnnounceChangedCreature(this->Master->ID, 4); + AnnounceChangedCreature(this->Master->ID, CREATURE_SPEED_CHANGED); this->Master->Combat.CheckCombatValues(); if(this->Master->Type == PLAYER){ @@ -691,7 +692,7 @@ void TSkillHitpoints::Set(int Value){ if(Master->Type == PLAYER){ SendPlayerData(Master->Connection); } - AnnounceChangedCreature(Master->ID, 1); + AnnounceChangedCreature(Master->ID, CREATURE_HEALTH_CHANGED); } } @@ -734,7 +735,7 @@ bool TSkillGoStrength::SetTimer(int Cycle, int Count, int MaxCount, int Addition if(Master->Type == PLAYER){ ((TPlayer*)Master)->CheckState(); } - AnnounceChangedCreature(Master->ID, 4); + AnnounceChangedCreature(Master->ID, CREATURE_SPEED_CHANGED); return true; } @@ -754,7 +755,7 @@ void TSkillGoStrength::Event(int Range){ if(this->SkNr == SKILL_GO_STRENGTH && Master->Type == PLAYER){ ((TPlayer*)Master)->CheckState(); } - AnnounceChangedCreature(Master->ID, 4); + AnnounceChangedCreature(Master->ID, CREATURE_SPEED_CHANGED); } // TSkillCarryStrength @@ -903,9 +904,9 @@ bool TSkillLight::SetTimer(int Cycle, int Count, int MaxCount, int AdditionalVal // TODO(fusion): The decompiled function had this logic, but WTF. // if(GetCreature(Master->ID) != NULL){ - // AnnounceChangedCreature(Master->ID, 2); + // AnnounceChangedCreature(Master->ID, CREATURE_LIGHT_CHANGED); // } - AnnounceChangedCreature(Master->ID, 2); + AnnounceChangedCreature(Master->ID, CREATURE_LIGHT_CHANGED); return true; } @@ -916,7 +917,7 @@ void TSkillLight::Event(int Range){ return; } - AnnounceChangedCreature(Master->ID, 2); + AnnounceChangedCreature(Master->ID, CREATURE_LIGHT_CHANGED); } // TSkillIllusion @@ -938,9 +939,9 @@ bool TSkillIllusion::SetTimer(int Cycle, int Count, int MaxCount, int Additional // TODO(fusion): The decompiled function had this logic, but WTF. // if(GetCreature(Master->ID) != NULL){ - // AnnounceChangedCreature(Master->ID, 3); + // AnnounceChangedCreature(Master->ID, CREATURE_OUTFIT_CHANGED); // } - AnnounceChangedCreature(Master->ID, 3); + AnnounceChangedCreature(Master->ID, CREATURE_OUTFIT_CHANGED); return true; } @@ -954,7 +955,7 @@ void TSkillIllusion::Event(int Range){ if(this->Get() <= 0){ Master->Outfit = Master->OrgOutfit; - AnnounceChangedCreature(Master->ID, 3); + AnnounceChangedCreature(Master->ID, CREATURE_OUTFIT_CHANGED); NotifyAllCreatures(Master->CrObject, 2, NONE); } } diff --git a/src/magic.cc b/src/magic.cc index 728b681..6cff8bc 100644 --- a/src/magic.cc +++ b/src/magic.cc @@ -1,6 +1,7 @@ #include "magic.hh" #include "config.hh" #include "info.hh" +#include "operate.hh" #include "stubs.hh" @@ -1503,7 +1504,7 @@ void Teleport(TCreature *Actor, const char *Param){ GraphicalEffect(DestX, DestY, DestZ, EFFECT_ENERGY); }else if(MDGoStrength != Actor->Skills[SKILL_GO_STRENGTH]->MDAct){ Actor->Skills[SKILL_GO_STRENGTH]->SetMDAct(MDGoStrength); - AnnounceChangedCreature(Actor->ID, 4); // CREATURE_GO_STRENGTH_CHANGED ? + AnnounceChangedCreature(Actor->ID, CREATURE_SPEED_CHANGED); GraphicalEffect(DestX, DestY, DestZ, EFFECT_MAGIC_BLUE); } } @@ -2399,7 +2400,7 @@ void CancelInvisibility(TCreature *Actor, Object Target, int ManaPoints, int Sou Victim->SetTimer(SKILL_ILLUSION, 0, 0, 0, -1); if(Victim->Skills[SKILL_ILLUSION]->Get() == 0){ Victim->Outfit = Victim->OrgOutfit; - AnnounceChangedCreature(Victim->ID, 3); // CREATURE_OUTFIT_CHANGED ? + AnnounceChangedCreature(Victim->ID, CREATURE_OUTFIT_CHANGED); NotifyAllCreatures(Victim->CrObject, 2, NONE); // CREATURE_APPEAR ? }else{ Effect = EFFECT_BLOCK_HIT; diff --git a/src/operate.cc b/src/operate.cc index 041d663..a76e723 100644 --- a/src/operate.cc +++ b/src/operate.cc @@ -4,7 +4,6 @@ #include "stubs.hh" // operate.cc -void AnnounceMovingCreature(uint32 CreatureID, Object Con); void CheckTopMoveObject(uint32 CreatureID, Object Obj, Object Ignore); void CheckMoveObject(uint32 CreatureID, Object Obj, bool Take); void CheckMapDestination(uint32 CreatureID, Object Obj, Object MapCon); @@ -23,6 +22,189 @@ void CollisionEvent(Object Obj, Object Dest); void SeparationEvent(Object Obj, Object Start); +//================================================================================================== +//================================================================================================== +//================================================================================================== +//================================================================================================== + +void AnnounceMovingCreature(uint32 CreatureID, Object Con){ + TCreature *Creature = GetCreature(CreatureID); + if(Creature == NULL){ + error("AnnounceMovingCreature: Kreatur %d existiert nicht.\n", CreatureID); + return; + } + + int ConX, ConY, ConZ; + GetObjectCoordinates(Con, &ConX, &ConY, &ConZ); + + // TODO(fusion): 17 and 15 are probably related to the client's viewport dimensions. + int SearchWidth = 17 + std::abs(Creature->posx - ConX) / 2; + int SearchHeight = 15 + std::abs(Creature->posy - ConY) / 2; + int SearchCenterX = (Creature->posx + ConX) / 2; + int SearchCenterY = (Creature->posy + ConY) / 2; + TFindCreatures Search(SearchWidth, SearchHeight, SearchCenterX, SearchCenterY, FIND_PLAYERS); + while(true){ + uint32 CharacterID = Search.getNext(); + if(CharacterID == 0){ + break; + } + + TPlayer *Player = GetPlayer(CharacterID); + if(Player != NULL && Player->Connection != NULL){ + SendMoveCreature(Player->Connection, CreatureID, ConX, ConY, ConZ); + } + } +} + +void AnnounceChangedCreature(uint32 CreatureID, int Type){ + TCreature *Creature = GetCreature(CreatureID); + if(Creature == NULL){ + error("AnnounceChangedCreature: Kreatur %d existiert nicht (Type=%d).\n", CreatureID, Type); + return; + } + + for(TKnownCreature *KnownCreature = Creature->FirstKnowingConnection; + KnownCreature != NULL; + KnownCreature = KnownCreature->Next){ + TConnection *KnowingConnection = KnownCreature->Connection; + if(KnowingConnection->State != CONNECTION_GAME + || KnownCreature->State == KNOWNCREATURE_OUTDATED){ + continue; + } + + if(KnowingConnection->IsVisible(Creature->posx, Creature->posy, Creature->posz)){ + switch(Type){ + case CREATURE_HEALTH_CHANGED: SendCreatureHealth(KnowingConnection, CreatureID); break; + case CREATURE_LIGHT_CHANGED: SendCreatureLight(KnowingConnection, CreatureID); break; + case CREATURE_OUTFIT_CHANGED: SendCreatureOutfit(KnowingConnection, CreatureID); break; + case CREATURE_SPEED_CHANGED: SendCreatureSpeed(KnowingConnection, CreatureID); break; + case CREATURE_SKULL_CHANGED: SendCreatureSkull(KnowingConnection, CreatureID); break; + case CREATURE_PARTY_CHANGED: SendCreatureParty(KnowingConnection, CreatureID); break; + } + }else{ + KnownCreature->State = KNOWNCREATURE_OUTDATED; + } + } +} + +void AnnounceChangedField(Object Obj, int Type){ + if(!Obj.exists()){ + error("AnnounceChangedField: Übergebenes Objekt existiert nicht.\n"); + return; + } + + int ObjX, ObjY, ObjZ; + GetObjectCoordinates(Obj, &ObjX, &ObjY, &ObjZ); + TFindCreatures Search(16, 14, ObjX, ObjY, FIND_PLAYERS); + while(true){ + uint32 CharacterID = Search.getNext(); + if(CharacterID == 0){ + break; + } + + TPlayer *Player = GetPlayer(CharacterID); + if(Player == NULL || Player->Connection == NULL + || !Player->Connection->IsVisible(ObjX, ObjY, ObjZ)){ + continue; + } + + switch(Type){ + case OBJECT_DELETED: SendDeleteField(Player->Connection, ObjX, ObjY, ObjZ, Obj); break; + case OBJECT_CREATED: SendAddField(Player->Connection, ObjX, ObjY, ObjZ, Obj); break; + case OBJECT_CHANGED: SendChangeField(Player->Connection, ObjX, ObjY, ObjZ, Obj); break; + default:{ + error("AnnounceChangedField: Ungültiger Typ %d.\n", Type); + return; + } + } + } +} + +void AnnounceChangedContainer(Object Obj, int Type){ + if(!Obj.exists()){ + error("AnnounceChangedContainer: Übergebenes Objekt existiert nicht.\n"); + return; + } + + Object Con = Obj.getContainer(); + TFindCreatures Search(1, 1, Obj, FIND_PLAYERS); + while(true){ + uint32 CharacterID = Search.getNext(); + if(CharacterID == 0){ + break; + } + + TPlayer *Player = GetPlayer(CharacterID); + if(Player == NULL || Player->Connection == NULL){ + continue; + } + + for(int ContainerNr = 0; + ContainerNr <= NARRAY(Player->OpenContainer); + ContainerNr += 1){ + if(Player->GetOpenContainer(ContainerNr) != Con){ + continue; + } + + switch(Type){ + case OBJECT_DELETED: SendDeleteInContainer(Player->Connection, ContainerNr, Obj); break; + case OBJECT_CREATED: SendCreateInContainer(Player->Connection, ContainerNr, Obj); break; + case OBJECT_CHANGED: SendChangeInContainer(Player->Connection, ContainerNr, Obj); break; + default:{ + error("AnnounceChangedContainer: Ungültiger Typ %d.\n", Type); + return; + } + } + } + } +} + +void AnnounceChangedInventory(Object Obj, int Type){ + if(!Obj.exists()){ + error("AnnounceChangedInventory: Übergebenes Objekt existiert nicht.\n"); + return; + } + + uint32 OwnerID = GetObjectCreatureID(Obj); + TCreature *Creature = GetCreature(OwnerID); + if(Creature == NULL){ + error("AnnounceChangedInventory: Objekt liegt in keinem Inventory.\n"); + return; + } + + if(Creature->Type != PLAYER){ + return; + } + + int Position = GetObjectBodyPosition(Obj); + switch(Type){ + case OBJECT_DELETED: SendDeleteInventory(Creature->Connection, Position); break; + case OBJECT_CREATED: SendSetInventory(Creature->Connection, Position, Obj); break; + case OBJECT_CHANGED: SendSetInventory(Creature->Connection, Position, Obj); break; + default:{ + error("AnnounceChangedInventory: Ungültiger Typ %d.\n", Type); + return; + } + } +} + +void AnnounceChangedObject(Object Obj, int Type){ + if(!Obj.exists()){ + error("AnnounceChangedObject: Übergebenes Objekt existiert nicht.\n"); + return; + } + + Object Con = Obj.getContainer(); + ObjectType ConType = Con.getObjectType(); + if(ConType.isMapContainer()){ + AnnounceChangedField(Obj, Type); + }else if(ConType.isBodyContainer()){ + AnnounceChangedInventory(Obj, Type); + }else{ + AnnounceChangedContainer(Obj, Type); + } +} + // TODO(fusion): This could have been a simple return value. void CheckContainerDestination(Object Obj, Object Con){ Object Help = Con; @@ -212,7 +394,7 @@ void Move(uint32 CreatureID, Object Obj, Object Con, int Count, bool NoMerge, Ob // easier to read. if(!ObjType.isCreatureContainer() && Remainder == NONE){ - AnnounceChangedObject(Obj, 0); // OBJECT_DISAPPEAR ? + AnnounceChangedObject(Obj, OBJECT_DELETED); NotifyTrades(Obj); NotifyDepot(CreatureID, Obj, CountObjects(Obj)); } @@ -237,13 +419,13 @@ void Move(uint32 CreatureID, Object Obj, Object Con, int Count, bool NoMerge, Ob MoveObject(Obj, Con); if(!ObjType.isCreatureContainer()){ - AnnounceChangedObject(Obj, 1); // OBJECT_APPEAR + AnnounceChangedObject(Obj, OBJECT_CREATED); NotifyTrades(Obj); NotifyDepot(CreatureID, Obj, -CountObjects(Obj)); } if(Remainder != NONE){ - AnnounceChangedObject(Remainder, 2); // OBJECT_CHANGED + AnnounceChangedObject(Remainder, OBJECT_CHANGED); NotifyTrades(Remainder); } @@ -336,17 +518,17 @@ void Merge(uint32 CreatureID, Object Obj, Object Dest, int Count, Object Ignore) if(Count < ObjCount){ ChangeObject(Obj, AMOUNT, ObjCount - Count); - AnnounceChangedObject(Obj, 2); // OBJECT_CHANGED ? + AnnounceChangedObject(Obj, OBJECT_CHANGED); NotifyTrades(Obj); ChangeObject(Dest, AMOUNT, DestCount + Count); }else{ - AnnounceChangedObject(Obj, 0); // OBJECT_DISAPPEAR ? + AnnounceChangedObject(Obj, OBJECT_DELETED); NotifyTrades(Obj); NotifyDepot(CreatureID, Obj, 1); MergeObjects(Obj, Dest); } - AnnounceChangedObject(Dest, 2); // OBJECT_CHANGED ? + AnnounceChangedObject(Dest, OBJECT_CHANGED); NotifyTrades(Dest); NotifyCreature(ObjOwnerID, Dest, ObjCon.getObjectType().isBodyContainer()); NotifyCreature(DestOwnerID, Dest, DestCon.getObjectType().isBodyContainer()); diff --git a/src/operate.hh b/src/operate.hh index 6270ec7..de7b54d 100644 --- a/src/operate.hh +++ b/src/operate.hh @@ -4,6 +4,21 @@ #include "common.hh" #include "map.hh" +enum : int { + CREATURE_HEALTH_CHANGED = 1, + CREATURE_LIGHT_CHANGED = 2, + CREATURE_OUTFIT_CHANGED = 3, + CREATURE_SPEED_CHANGED = 4, + CREATURE_SKULL_CHANGED = 5, + CREATURE_PARTY_CHANGED = 6, +}; + +enum : int { + OBJECT_DELETED = 0, + OBJECT_CREATED = 1, + OBJECT_CHANGED = 2, +}; + void CheckContainerDestination(Object Obj, Object Con); void CheckInventoryDestination(Object Obj, Object Con, bool Split); diff --git a/src/stubs.hh b/src/stubs.hh index 4c1b0cb..a2c5f25 100644 --- a/src/stubs.hh +++ b/src/stubs.hh @@ -69,6 +69,20 @@ extern void SendAmbiente(TConnection *Connection); extern void SendClearTarget(TConnection *Connection); extern void SendCloseContainer(TConnection *Connection, int ContainerNr); extern void SendCloseTrade(TConnection *Connection); +extern void SendCreatureHealth(TConnection *Connection, uint32 CreatureID); +extern void SendCreatureLight(TConnection *Connection, uint32 CreatureID); +extern void SendCreatureOutfit(TConnection *Connection, uint32 CreatureID); +extern void SendCreatureSpeed(TConnection *Connection, uint32 CreatureID); +extern void SendCreatureSkull(TConnection *Connection, uint32 CreatureID); +extern void SendCreatureParty(TConnection *Connection, uint32 CreatureID); +extern void SendDeleteField(TConnection *Connection, int x, int y, int z, Object Obj); +extern void SendAddField(TConnection *Connection, int x, int y, int z, Object Obj); +extern void SendChangeField(TConnection *Connection, int x, int y, int z, Object Obj); +extern void SendDeleteInContainer(TConnection *Connection, int ContainerNr, Object Obj); +extern void SendCreateInContainer(TConnection *Connection, int ContainerNr, Object Obj); +extern void SendChangeInContainer(TConnection *Connection, int ContainerNr, Object Obj); +extern void SendDeleteInventory(TConnection *Connection, int Position); +extern void SendSetInventory(TConnection *Connection, int Position, Object Obj); extern void SendEditList(TConnection *Connection, uint8 ListType, uint32 ID, const char *Text); extern void SendFullScreen(TConnection *Connection); extern void SendFloors(TConnection *Connection, bool Up); @@ -76,6 +90,7 @@ extern void SendRow(TConnection *Connection, int Direction); extern void SendMails(TPlayerData *PlayerData); extern void SendMarkCreature(TConnection *Connection, uint32 CreatureID, int Color); extern void SendMessage(TConnection *Connection, int Mode, const char *Text, ...) ATTR_PRINTF(3, 4); +void SendMoveCreature(TConnection *Connection, uint32 CreatureID, int x, int y, int z); extern void SendPlayerData(TConnection *Connection); extern void SendPlayerSkills(TConnection *Connection); extern void SendPlayerState(TConnection *Connection, uint8 State); -- cgit v1.2.3