Jump to content

[script/server-side] Ir al hospital más cercano al morir


Recommended Posts

Introducción

Es un script muy básico de lado del server que consiste en enviar al jugador al hospital más cercano al morir, la función busca en una tabla de datos flotantes la posición del hospital más cercano del jugador cuando éste muere.

El script busca el ID y posteriormente utiliza otra función para obtener las coordenadas de dicho hospital ID.


Codigo

-- <script name="Near hospital spawn">

hospitalsTable = { 
	{-318.8799, 1049.2433, 20.3403, 0.0000},
	{1172.8372, -1325.3186, 15.4000, 270.0000},
	{-2655.1240, 638.6232, 14.4531, 180.0000},
	{1241.6802, 326.4038, 19.7555, 335.0000},
	{1607.4869, 1816.0693, 10.8203, 0.0000},
	{2034.0670, -1402.6815, 17.2938, 180.0000},
} 

function spawnPlayerInHospital(player)
	local id = getHospitalCLosestID(player);
	local x, y, z, a = getHospitalCoords(id);

	fadeCamera(player, true, 0.5);
	spawnPlayer(player, x, y, z, a);
end

function getHospitalCLosestID(player)
	local s = -1;
   	local temp1 = 99999.0;
   	local temp2 = 0.0
   	local x,y,z = getElementPosition(player);
    for key,val in pairs(hospitalsTable) do     
        temp2 = getDistanceBetweenPoints2D(x, y, val[1], val[2]);
        if(temp2 == false) then
        	s = 1
        	break
        end  
        if(temp1 > temp2) then
        	temp1 = temp2
        	s = key
        end
    end 
    return s
end 

function getHospitalCoords(id)
	if (id == -1 or id <= table.getn(hospitalsTable)-1 or id >= table.getn(hospitalsTable)+1) then
		id = 1
	end
	return hospitalsTable[id][1], hospitalsTable[id][2], hospitalsTable[id][3], hospitalsTable[id][4]
end
-- </script>  

Una vez añadido el script solamente faltaría agregarlo al evento deseado:

addEventHandler("onPlayerWasted", root, function()
    fadeCamera(source, false, 1.0);
	setTimer(spawnPlayerInHospital, 2*1000, 1, source)
end)  

 

Edited by Chainsaw
Link to comment

Sí, lo hice yo. Conozco otros lenguajes de programación y soy totalmente nuevo en LUA por lo que no sé si el código sea lo más optimo a lo que se puede llegar en LUA, me gustaría saber si el código lo es, a ver si alguien me dice algo que pueda mejorar!

Edited by Chainsaw
Link to comment
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...