Skip to content

Instantly share code, notes, and snippets.

@FALL1N1
Created October 24, 2012 01:05
Show Gist options
  • Select an option

  • Save FALL1N1/3943081 to your computer and use it in GitHub Desktop.

Select an option

Save FALL1N1/3943081 to your computer and use it in GitHub Desktop.
Transport System @ Gunship
diff --git a/src/server/game/Entities/Transport/Transport.cpp b/src/server/game/Entities/Transport/Transport.cpp
index f3b2d51..142f81b 100644
--- a/src/server/game/Entities/Transport/Transport.cpp
+++ b/src/server/game/Entities/Transport/Transport.cpp
@@ -25,6 +25,7 @@
#include "WorldPacket.h"
#include "DBCStores.h"
#include "World.h"
+#include "Vehicle.h"
#include "GameObjectAI.h"
void MapManager::LoadTransports()
@@ -125,6 +126,81 @@ void MapManager::LoadTransports()
sLog->outString();
}
+Transport* MapManager::LoadTransportInMap(Map* instance, uint32 goEntry, uint32 period)
+{
+ const GameObjectTemplate* goInfo = sObjectMgr->GetGameObjectTemplate(goEntry);
+
+ if (!goInfo)
+ {
+ sLog->outErrorDb("Transport ID:%u, will not be loaded, gameobject_template missing", goEntry);
+ return NULL;
+ }
+
+ if (goInfo->type != GAMEOBJECT_TYPE_MO_TRANSPORT)
+ {
+ sLog->outErrorDb("Transport ID:%u, Name: %s, will not be loaded, gameobject_template type wrong", goEntry, goInfo->name.c_str());
+ return NULL;
+ }
+
+ Transport* t = new Transport(period, goInfo->ScriptId);
+ std::set<uint32> mapsUsed;
+ if (!t->GenerateWaypoints(goInfo->moTransport.taxiPathId, mapsUsed))
+ {
+ sLog->outErrorDb("Transport (path id %u) path size = 0. Transport ignored, check DBC files or the gameobject's data0 field.", goInfo->moTransport.taxiPathId);
+ delete t;
+ return NULL;
+ }
+ uint32 transportLowGuid = sObjectMgr->GenerateLowGuid(HIGHGUID_MO_TRANSPORT);
+
+ if (!t->Create(transportLowGuid, goEntry, t->m_WayPoints[0].mapid, t->m_WayPoints[0].x, t->m_WayPoints[0].y, t->m_WayPoints[0].z-10, 0.0f, 0, 0))
+ {
+ delete t;
+ return NULL;
+ }
+
+ m_Transports.insert(t);
+ m_TransportsByInstanceIdMap[instance->GetInstanceId()].insert(t);
+ t->SetMap(instance);
+ t->AddToWorld();
+ t->BuildStartMovePacket(instance);
+ t->BuildStopMovePacket(instance);
+ // Make transport realy stoppped at server-side. Movement will be handled by scripts
+ t->m_WayPoints.clear();
+
+ return t;
+}
+
+void MapManager::UnLoadTransportFromMap(Transport* t)
+{
+ Map* map = t->GetMap();
+
+ for (Transport::CreatureSet::iterator itr = t->m_NPCPassengerSet.begin(); itr != t->m_NPCPassengerSet.end();)
+ {
+ if (Creature* npc = *itr)
+ {
+ npc->SetTransport(NULL);
+ npc->setActive(false);
+ npc->RemoveFromWorld();
+ }
+ ++itr;
+ }
+
+ UpdateData transData;
+ t->BuildOutOfRangeUpdateBlock(&transData);
+ WorldPacket out_packet;
+ transData.BuildPacket(&out_packet);
+
+ for (Map::PlayerList::const_iterator itr = map->GetPlayers().begin(); itr != map->GetPlayers().end(); ++itr)
+ if (t != itr->getSource()->GetTransport())
+ itr->getSource()->SendDirectMessage(&out_packet);
+
+ t->m_NPCPassengerSet.clear();
+ m_TransportsByInstanceIdMap[t->GetInstanceId()].erase(t);
+ m_Transports.erase(t);
+ t->m_WayPoints.clear();
+ t->RemoveFromWorld();
+}
+
void MapManager::LoadTransportNPCs()
{
uint32 oldMSTime = getMSTime();
@@ -180,8 +256,11 @@ Transport::~Transport()
{
for (CreatureSet::iterator itr = m_NPCPassengerSet.begin(); itr != m_NPCPassengerSet.end(); ++itr)
{
- (*itr)->SetTransport(NULL);
- GetMap()->AddObjectToRemoveList(*itr);
+ Creature* passenger = *itr;
+ Map* map = passenger->GetMap();
+ passenger->SetTransport(NULL);
+ passenger->CleanupsBeforeDelete();
+ map->RemoveFromMap(passenger, true);
}
m_NPCPassengerSet.clear();
@@ -529,6 +608,8 @@ bool Transport::RemovePassenger(Player* passenger)
void Transport::Update(uint32 p_diff)
{
+ UpdateNPCPositions();
+
if (!AI())
{
if (!AIM_Initialize())
@@ -557,7 +638,6 @@ void Transport::Update(uint32 p_diff)
else
{
Relocate(m_curr->second.x, m_curr->second.y, m_curr->second.z, GetAngle(m_next->second.x, m_next->second.y) + float(M_PI));
- UpdateNPCPositions(); // COME BACK MARKER
}
sScriptMgr->OnRelocate(this, m_curr->first, m_curr->second.mapid, m_curr->second.x, m_curr->second.y, m_curr->second.z);
@@ -569,7 +649,6 @@ void Transport::Update(uint32 p_diff)
sLog->outDebug(LOG_FILTER_TRANSPORTS, "%s moved to %d %f %f %f %d", m_name.c_str(), m_curr->second.id, m_curr->second.x, m_curr->second.y, m_curr->second.z, m_curr->second.mapid);
}
-
sScriptMgr->OnTransportUpdate(this, p_diff);
}
@@ -630,56 +709,87 @@ void Transport::BuildStopMovePacket(Map const* targetMap)
UpdateForMap(targetMap);
}
-uint32 Transport::AddNPCPassenger(uint32 tguid, uint32 entry, float x, float y, float z, float o, uint32 anim)
-{
- Map* map = GetMap();
- //make it world object so it will not be unloaded with grid
- Creature* creature = new Creature(true);
-
- if (!creature->Create(sObjectMgr->GenerateLowGuid(HIGHGUID_UNIT), map, GetPhaseMask(), entry, 0, GetGOInfo()->faction, 0, 0, 0, 0))
- {
- delete creature;
- return 0;
+ Creature* Transport::AddNPCPassenger(uint32 tguid, uint32 entry, float x, float y, float z, float o, uint32 anim)
+ {
+ Map* map = GetMap();
+ Creature* pCreature = new Creature;
+
+ if (!pCreature->Create(sObjectMgr->GenerateLowGuid(HIGHGUID_UNIT), map, GetPhaseMask(), entry, 0, GetGOInfo()->faction, 0, 0, 0, 0))
+ {
+ delete pCreature;
+ return 0;
}
+
+ pCreature->SetTransport(this);
+ pCreature->AddUnitMovementFlag(MOVEMENTFLAG_ONTRANSPORT);
+ pCreature->m_movementInfo.guid = GetGUID();
+ pCreature->m_movementInfo.t_pos.Relocate(x, y, z, o);
+ o += GetOrientation();
+ MapManager::NormalizeOrientation(o);
+
+ pCreature->Relocate(
+ GetPositionX() + (x * cos(GetOrientation()) + y * sin(GetOrientation() + float(M_PI))),
+ GetPositionY() + (y * cos(GetOrientation()) + x * sin(GetOrientation())),
+ z + GetPositionZ(),
+ o);
+
+ pCreature->SetHomePosition(pCreature->GetPositionX(), pCreature->GetPositionY(), pCreature->GetPositionZ(), pCreature->GetOrientation());
+
+ if (!pCreature->IsPositionValid())
+ {
+ sLog->outError("Creature (guidlow %d, entry %d) not created. Suggested coordinates isn't valid (X: %f Y: %f)", pCreature->GetGUIDLow(), pCreature->GetEntry(), pCreature->GetPositionX(), pCreature->GetPositionY());
+ delete pCreature;
+ return 0;
+ }
+
+
+ map->AddToMap(pCreature);
+ m_NPCPassengerSet.insert(pCreature);
+
+ pCreature->setActive(true);
+ sScriptMgr->OnAddCreaturePassenger(this, pCreature);
+ return pCreature;
+ }
+
+ Creature* Transport::AddNPCPassengerInInstance(uint32 entry, float x, float y, float z, float o, uint32 anim)
+ {
+ Map* map = GetMap();
+ Creature* pCreature = new Creature;
+
+ if (!pCreature->Create(sObjectMgr->GenerateLowGuid(HIGHGUID_UNIT), map, GetPhaseMask(), entry, 0, GetGOInfo()->faction, 0, 0, 0, 0))
+ {
+ delete pCreature;
+ return NULL;
+ }
- creature->SetTransport(this);
- creature->AddUnitMovementFlag(MOVEMENTFLAG_ONTRANSPORT);
- creature->m_movementInfo.guid = GetGUID();
- creature->m_movementInfo.t_pos.Relocate(x, y, z, o);
-
- if (anim)
- creature->SetUInt32Value(UNIT_NPC_EMOTESTATE, anim);
-
- creature->Relocate(
- GetPositionX() + (x * cos(GetOrientation()) + y * sin(GetOrientation() + float(M_PI))),
- GetPositionY() + (y * cos(GetOrientation()) + x * sin(GetOrientation())),
- z + GetPositionZ(),
- o + GetOrientation());
+ pCreature->SetTransport(this);
+ pCreature->AddUnitMovementFlag(MOVEMENTFLAG_ONTRANSPORT);
+ pCreature->m_movementInfo.guid = GetGUID();
+ pCreature->m_movementInfo.t_pos.Relocate(x, y, z, o);
+ o += GetOrientation();
+ MapManager::NormalizeOrientation(o);
- creature->SetHomePosition(creature->GetPositionX(), creature->GetPositionY(), creature->GetPositionZ(), creature->GetOrientation());
+ pCreature->Relocate(
+ GetPositionX() + (x * cos(GetOrientation()) + y * sin(GetOrientation() + float(M_PI))),
+ GetPositionY() + (y * cos(GetOrientation()) + x * sin(GetOrientation())),
+ z + GetPositionZ() ,
+ o);
- if (!creature->IsPositionValid())
- {
- sLog->outError("Creature (guidlow %d, entry %d) not created. Suggested coordinates isn't valid (X: %f Y: %f)", creature->GetGUIDLow(), creature->GetEntry(), creature->GetPositionX(), creature->GetPositionY());
- delete creature;
- return 0;
- }
+ pCreature->SetHomePosition(pCreature->GetPositionX(), pCreature->GetPositionY(), pCreature->GetPositionZ(), pCreature->GetOrientation());
- map->AddToMap(creature);
- m_NPCPassengerSet.insert(creature);
+ if (!pCreature->IsPositionValid())
+ {
+ sLog->outError("Creature (guidlow %d, entry %d) not created. Suggested coordinates isn't valid (X: %f Y: %f)", pCreature->GetGUIDLow(), pCreature->GetEntry(), pCreature->GetPositionX(), pCreature->GetPositionY());
+ delete pCreature;
+ return NULL;
+ }
- if (tguid == 0)
- {
- ++currenttguid;
- tguid = currenttguid;
- }
- else
- currenttguid = std::max(tguid, currenttguid);
+ map->AddToMap(pCreature);
+ m_NPCPassengerSet.insert(pCreature);
- creature->setActive(true);
- creature->SetGUIDTransport(tguid);
- sScriptMgr->OnAddCreaturePassenger(this, creature);
- return tguid;
+ pCreature->setActive(true);
+ sScriptMgr->OnAddCreaturePassenger(this, pCreature);
+ return pCreature;
}
void Transport::UpdatePosition(MovementInfo* mi)
@@ -701,10 +811,28 @@ void Transport::UpdateNPCPositions()
float x, y, z, o;
o = GetOrientation() + npc->m_movementInfo.t_pos.m_orientation;
+ MapManager::NormalizeOrientation(o);
x = GetPositionX() + (npc->m_movementInfo.t_pos.m_positionX * cos(GetOrientation()) + npc->m_movementInfo.t_pos.m_positionY * sin(GetOrientation() + M_PI));
y = GetPositionY() + (npc->m_movementInfo.t_pos.m_positionY * cos(GetOrientation()) + npc->m_movementInfo.t_pos.m_positionX * sin(GetOrientation()));
z = GetPositionZ() + npc->m_movementInfo.t_pos.m_positionZ;
npc->SetHomePosition(x, y, z, o);
GetMap()->CreatureRelocation(npc, x, y, z, o, false);
}
-}
+
+ for (PlayerSet::iterator itr = m_passengers.begin(); itr != m_passengers.end(); ++itr)
+ {
+ Player* plr = *itr;
+
+ float x, y, z, o;
+ o = GetOrientation() + plr->m_movementInfo.t_pos.m_orientation;
+ MapManager::NormalizeOrientation(o);
+ x = GetPositionX() + (plr->m_movementInfo.t_pos.m_positionX * cos(GetOrientation()) + plr->m_movementInfo.t_pos.m_positionY * sin(GetOrientation() + M_PI));
+ y = GetPositionY() + (plr->m_movementInfo.t_pos.m_positionY * cos(GetOrientation()) + plr->m_movementInfo.t_pos.m_positionX * sin(GetOrientation()));
+ z = GetPositionZ() + plr->m_movementInfo.t_pos.m_positionZ;
+ plr->Relocate(x, y, z, o);
+ UpdateData transData;
+ WorldPacket packet;
+ transData.BuildPacket(&packet);
+ plr->SendDirectMessage(&packet);
+ }
+}
\ No newline at end of file
diff --git a/src/server/game/Entities/Transport/Transport.h b/src/server/game/Entities/Transport/Transport.h
index 518dcf6..381c99a 100755
--- a/src/server/game/Entities/Transport/Transport.h
+++ b/src/server/game/Entities/Transport/Transport.h
@@ -44,7 +44,8 @@ class Transport : public GameObject
typedef std::set<Creature*> CreatureSet;
CreatureSet m_NPCPassengerSet;
- uint32 AddNPCPassenger(uint32 tguid, uint32 entry, float x, float y, float z, float o, uint32 anim=0);
+ Creature* AddNPCPassenger(uint32 tguid, uint32 entry, float x, float y, float z, float o, uint32 anim=0);
+ Creature* AddNPCPassengerInInstance(uint32 entry, float x, float y, float z, float o, uint32 anim=0);
void UpdatePosition(MovementInfo* mi);
void UpdateNPCPositions();
void BuildStartMovePacket(Map const* targetMap);
@@ -92,5 +93,4 @@ class Transport : public GameObject
void DoEventIfAny(WayPointMap::value_type const& node, bool departure);
WayPointMap::const_iterator GetNextWayPoint();
};
-#endif
-
+#endif
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment