Add API function to invoke player respawn

closes #12272
This commit is contained in:
sfan5 2022-05-25 19:29:11 +02:00
parent da71e86633
commit f195db2d14
5 changed files with 27 additions and 3 deletions

@ -7099,6 +7099,8 @@ object you are working with still exists.
* `intensity` sets the intensity of the shadows from 0 (no shadows, default) to 1 (blackness) * `intensity` sets the intensity of the shadows from 0 (no shadows, default) to 1 (blackness)
* `get_lighting()`: returns the current state of lighting for the player. * `get_lighting()`: returns the current state of lighting for the player.
* Result is a table with the same fields as `light_definition` in `set_lighting`. * Result is a table with the same fields as `light_definition` in `set_lighting`.
* `respawn()`: Respawns the player using the same mechanism as the death screen,
including calling on_respawnplayer callbacks.
`PcgRandom` `PcgRandom`
----------- -----------

@ -2323,6 +2323,21 @@ int ObjectRef::l_get_lighting(lua_State *L)
return 1; return 1;
} }
// respawn(self)
int ObjectRef::l_respawn(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
RemotePlayer *player = getplayer(ref);
if (player == nullptr)
return 0;
getServer(L)->RespawnPlayer(player->getPeerId());
lua_pushboolean(L, true);
return 1;
}
ObjectRef::ObjectRef(ServerActiveObject *object): ObjectRef::ObjectRef(ServerActiveObject *object):
m_object(object) m_object(object)
{} {}
@ -2478,5 +2493,7 @@ luaL_Reg ObjectRef::methods[] = {
luamethod(ObjectRef, set_minimap_modes), luamethod(ObjectRef, set_minimap_modes),
luamethod(ObjectRef, set_lighting), luamethod(ObjectRef, set_lighting),
luamethod(ObjectRef, get_lighting), luamethod(ObjectRef, get_lighting),
luamethod(ObjectRef, respawn),
{0,0} {0,0}
}; };

@ -382,4 +382,7 @@ private:
// get_lighting(self) // get_lighting(self)
static int l_get_lighting(lua_State *L); static int l_get_lighting(lua_State *L);
// respawn(self)
static int l_respawn(lua_State *L);
}; };

@ -2784,9 +2784,10 @@ void Server::RespawnPlayer(session_t peer_id)
<< playersao->getPlayer()->getName() << playersao->getPlayer()->getName()
<< " respawns" << std::endl; << " respawns" << std::endl;
playersao->setHP(playersao->accessObjectProperties()->hp_max, const auto *prop = playersao->accessObjectProperties();
playersao->setHP(prop->hp_max,
PlayerHPChangeReason(PlayerHPChangeReason::RESPAWN)); PlayerHPChangeReason(PlayerHPChangeReason::RESPAWN));
playersao->setBreath(playersao->accessObjectProperties()->breath_max); playersao->setBreath(prop->breath_max);
bool repositioned = m_script->on_respawnplayer(playersao); bool repositioned = m_script->on_respawnplayer(playersao);
if (!repositioned) { if (!repositioned) {

@ -336,6 +336,8 @@ public:
void setLighting(RemotePlayer *player, const Lighting &lighting); void setLighting(RemotePlayer *player, const Lighting &lighting);
void RespawnPlayer(session_t peer_id);
/* con::PeerHandler implementation. */ /* con::PeerHandler implementation. */
void peerAdded(con::Peer *peer); void peerAdded(con::Peer *peer);
void deletingPeer(con::Peer *peer, bool timeout); void deletingPeer(con::Peer *peer, bool timeout);
@ -529,7 +531,6 @@ private:
*/ */
void HandlePlayerDeath(PlayerSAO* sao, const PlayerHPChangeReason &reason); void HandlePlayerDeath(PlayerSAO* sao, const PlayerHPChangeReason &reason);
void RespawnPlayer(session_t peer_id);
void DeleteClient(session_t peer_id, ClientDeletionReason reason); void DeleteClient(session_t peer_id, ClientDeletionReason reason);
void UpdateCrafting(RemotePlayer *player); void UpdateCrafting(RemotePlayer *player);
bool checkInteractDistance(RemotePlayer *player, const f32 d, const std::string &what); bool checkInteractDistance(RemotePlayer *player, const f32 d, const std::string &what);