forked from multitheftauto/mtasa-blue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial server awareness for custom train tracks (multitheftauto#…
…1715) This **fully backwards compatible** pull request contains initial support for the train track element and the ability to change between default train tracks. This is the first in a series of pull requests that intends to pick apart the `feature/custom-train-tracks` branch in multitheftauto#250, so that it can be confidently reviewed and merged without worrying about bugs too much. You can get default track elements using the below documentation. This documentation is currently only accurate server-side. Client-side it will just return the same number you provide, as clients do not yet have a concept of track elements. ```lua -- OOP: TrainTrack.getDefaultTrack(trackID) train-track getDefaultTrack(number trackID) -- trackID: the ID of the default track, one of `0`, `1`, `2`, `3`. ``` And then set (or get) the track of a train using the following functions: ```lua -- OOP: v:setTrack(t) bool setTrainTrack(vehicle v, train-track t) -- OOP: v:getTrack(v) train-track getTrainTrack(vehicle v) ``` I have tested sync with one and two players, and track and position _syncing_ seems to work great. The only client changes this PR brings is exposing existing client to the Lua API (a partial revert of 89b5620 / multitheftauto#1713). The client-side `setTrainTrack` function is not protected, and can run on server trains. This is consistent with the behaviour of `setTrainPosition`, `setTrainDirection` and `setTrainSpeed`. Finally, since this code is based on the original `feature/custom-train-tracks` branch: Co-authored-by: Jusonex <[email protected]> Co-authored-by: Cazomino05 <[email protected]> Co-authored-by: "[email protected]" <[email protected]>
- Loading branch information
1 parent
fced208
commit c610ff3
Showing
21 changed files
with
843 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,7 @@ class CElement | |
WATER, | ||
WEAPON, | ||
DATABASE_CONNECTION, | ||
TRAIN_TRACK, | ||
ROOT, | ||
UNKNOWN, | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/***************************************************************************** | ||
* | ||
* PROJECT: Multi Theft Auto | ||
* LICENSE: See LICENSE in the top level directory | ||
* | ||
* Multi Theft Auto is available from http://www.multitheftauto.com/ | ||
* | ||
*****************************************************************************/ | ||
|
||
#include "StdInc.h" | ||
|
||
CTrainTrack::CTrainTrack(CTrainTrackManager* pManager, const std::vector<STrackNode>& nodes, bool linkLastNodes, CElement* pParent, uchar defaultTrackId) | ||
: CElement(pParent), m_pManager(pManager) | ||
{ | ||
m_iType = CElement::TRAIN_TRACK; | ||
SetTypeName("train-track"); | ||
|
||
m_LinkLastNodes = linkLastNodes; | ||
m_Nodes = nodes; | ||
m_DefaultTrackId = defaultTrackId; | ||
} | ||
|
||
CTrainTrack::~CTrainTrack() | ||
{ | ||
// Remove all vehicles from the track | ||
CVehicleManager* pVehicleManager = g_pGame->GetVehicleManager(); | ||
for (auto pVehicle : pVehicleManager->GetVehicles()) | ||
{ | ||
if (pVehicle->GetTrainTrack() != this) | ||
continue; | ||
|
||
pVehicle->SetTrainTrack(nullptr); | ||
pVehicle->SetDerailed(true); | ||
} | ||
|
||
// Unreference train track | ||
m_pManager->DestroyTrainTrack(this); | ||
} | ||
|
||
bool CTrainTrack::SetTrackNodePosition(uint nodeIndex, const CVector& position) | ||
{ | ||
if (nodeIndex >= m_Nodes.size()) | ||
return false; | ||
|
||
auto& node = m_Nodes[nodeIndex]; | ||
node.position = position; | ||
return true; | ||
} | ||
|
||
bool CTrainTrack::GetTrackNodePosition(uint nodeIndex, CVector& position) | ||
{ | ||
if (nodeIndex >= m_Nodes.size()) | ||
return false; | ||
|
||
auto& node = m_Nodes[nodeIndex]; | ||
position = node.position; | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/***************************************************************************** | ||
* | ||
* PROJECT: Multi Theft Auto | ||
* LICENSE: See LICENSE in the top level directory | ||
* | ||
* Multi Theft Auto is available from http://www.multitheftauto.com/ | ||
* | ||
*****************************************************************************/ | ||
|
||
#pragma once | ||
class CTrainTrackManager; | ||
|
||
struct STrackNode | ||
{ | ||
CVector position; | ||
float railDistance; | ||
|
||
STrackNode(std::int16_t x, std::int16_t y, std::int16_t z, std::uint16_t distance) | ||
{ | ||
position.fX = 8.0f * x; | ||
position.fY = 8.0f * y; | ||
position.fZ = 8.0f * z; | ||
railDistance = 3.0f * distance; // TODO(Jusonex, feature/custom-train-tracks/9063a3dc080): Remove distance | ||
} | ||
|
||
STrackNode(const CVector& pos) : position(pos) {} | ||
STrackNode() {} | ||
}; | ||
|
||
class CTrainTrack : public CElement | ||
{ | ||
public: | ||
CTrainTrack(CTrainTrackManager* pManager, const std::vector<STrackNode>& nodes, bool linkLastNodes, CElement* pParent, uchar defaultTrackId = 0xFF); | ||
// TODO: Add move constructor | ||
virtual ~CTrainTrack(); | ||
|
||
bool SetTrackNodePosition(uint nodeIndex, const CVector& position); | ||
bool GetTrackNodePosition(uint nodeIndex, CVector& position); | ||
|
||
inline const std::vector<STrackNode>& GetNodes() const { return m_Nodes; } | ||
inline std::size_t GetNumberOfNodes() const { return m_Nodes.size(); } | ||
|
||
inline void SetLastNodesLinked(bool link) { m_LinkLastNodes = link; } | ||
inline bool GetLastNodesLinked() { return m_LinkLastNodes; } | ||
|
||
inline bool IsDefault() { return m_DefaultTrackId != 0xFF; } | ||
inline uchar GetDefaultTrackId() { return m_DefaultTrackId; } | ||
|
||
virtual void Unlink() override {} | ||
bool ReadSpecialData(const int iLine) override { return false; } | ||
|
||
private: | ||
CTrainTrackManager* m_pManager; | ||
|
||
std::vector<STrackNode> m_Nodes; | ||
bool m_LinkLastNodes; | ||
bool m_Default; | ||
uchar m_DefaultTrackId; | ||
}; |
Oops, something went wrong.