Added random placement function.

master
Robert Zenz 2015-07-28 19:30:44 +02:00
parent d44b2043bc
commit f7bec59627
2 changed files with 127 additions and 7 deletions

View File

@ -68,7 +68,7 @@
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#spawnusher.activate">spawnusher.activate</a>&nbsp;(required_air_bubble_size, retry_time)</td>
<td class="name" nowrap><a href="#spawnusher.activate">spawnusher.activate</a>&nbsp;(random_placement_radius, required_air_bubble_size, retry_time)</td>
<td class="summary">Activates the spawn usher system.</td>
</tr>
@ -92,6 +92,16 @@
<td class="summary">Move all players that could not be placed so far.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#spawnusher.move_random">spawnusher.move_random</a>&nbsp;(player)</td>
<td class="summary">Moves the player randomly on the x and z plane.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#spawnusher.on_spawn_player">spawnusher.on_spawn_player</a>&nbsp;(player)</td>
<td class="summary">Callback for if a player spawns.</td>
</tr>
</table>
@ -110,7 +120,7 @@
<dt><a name="spawnusher.activate"></a><strong>spawnusher.activate</strong>&nbsp;(required_air_bubble_size, retry_time)</dt>
<dt><a name="spawnusher.activate"></a><strong>spawnusher.activate</strong>&nbsp;(random_placement_radius, required_air_bubble_size, retry_time)</dt>
<dd>
Activates the spawn usher system.
@ -118,6 +128,10 @@ Activates the spawn usher system.
<h3>Parameters</h3>
<ul>
<li>
random_placement_radius: Optional. The player will be respawned in the given radius around the spawn point.
</li>
<li>
required_air_bubble_size: Optional. The size/height of the bubble of air that is required for the player to spawn. Defaults to 2.
</li>
@ -237,6 +251,61 @@ Move all players that could not be placed so far.
</dd>
<dt><a name="spawnusher.move_random"></a><strong>spawnusher.move_random</strong>&nbsp;(player)</dt>
<dd>
Moves the player randomly on the x and z plane.
<h3>Parameters</h3>
<ul>
<li>
player: The Player object to move.
</li>
</ul>
</dd>
<dt><a name="spawnusher.on_spawn_player"></a><strong>spawnusher.on_spawn_player</strong>&nbsp;(player)</dt>
<dd>
Callback for if a player spawns.
<h3>Parameters</h3>
<ul>
<li>
player: The Player that spawned.
</li>
</ul>
<h3>Return value:</h3>
true, to disable default placement.
</dd>

View File

@ -37,6 +37,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-- The only function that should be called from clients is activate.
spawnusher = {
players = List:new(),
random = nil,
random_placement_radius = 40,
required_bubble_size = 2,
retry_time = 0.5,
scheduled = false
@ -45,17 +47,25 @@ spawnusher = {
--- Activates the spawn usher system.
--
-- @param required_air_bubble_size Optional. The size/height of the bubble of air
-- that is required for the player to spawn.
-- @param random_placement_radius Optional. The player will be respawned in
-- the given radius around the spawn point.
-- @param required_air_bubble_size Optional. The size/height of the bubble of
-- air that is required for the player to spawn.
-- Defaults to 2.
-- @param retry_time Optional. This is the time that passes between tries to
-- place to the player.
function spawnusher.activate(required_air_bubble_size, retry_time)
function spawnusher.activate(random_placement_radius, required_air_bubble_size, retry_time)
spawnusher.random_placement_radius = random_placement_radius or 40
spawnusher.required_bubble_size = required_bubble_size or 2
spawnusher.retry_time = retry_time or 0.5
minetest.register_on_newplayer(spawnusher.move_player)
minetest.register_on_respawnplayer(spawnusher.move_player)
-- Initialize the PcgRandom with the current time. Given that placement
-- of the player is not critical or needs to be reproducable in any way,
-- it really does not matter here.
spawnusher.random = PcgRandom(os.time())
minetest.register_on_newplayer(spawnusher.on_spawn_player)
minetest.register_on_respawnplayer(spawnusher.on_spawn_player)
end
--- Tests if the given position is an air bubble big enough.
@ -107,6 +117,21 @@ function spawnusher.move_later(player, current_pos)
end
end
--- Moves the player randomly on the x and z plane.
--
-- @param player The Player object to move.
function spawnusher.move_random(player)
local move_x = spawnusher.random:next(-spawnusher.random_placement_radius, spawnusher.random_placement_radius)
local move_z = spawnusher.random:next(-spawnusher.random_placement_radius, spawnusher.random_placement_radius)
local pos = player:getpos()
pos.x = pos.x + move_x
pos.z = pos.z + move_z
player:setpos(pos)
end
--- Moves the player to a safe location.
--
-- @param player The player object.
@ -190,3 +215,29 @@ function spawnusher.move_players()
end
end
--- Callback for if a player spawns.
--
-- @param player The Player that spawned.
-- @return true, to disable default placement.
function spawnusher.on_spawn_player(player)
-- Move the player to the set/default spawn point.
local spawn_pos = minetest.setting_get_pos("static_spawnpoint")
if spawn_pos == nil then
spawn_pos = {
x = 0,
y = 0,
z = 0
}
end
player:setpos(spawn_pos)
-- Move the player randomly afterwards.
spawnusher.move_random(player)
-- Now find a nice spawn place for the player.
spawnusher.move_player(player)
return true
end