TheDoctor

05-27-2015, 04:00 PM

Hey had coded last days for the first time with lua, first to just play around. But now i want to try something bigger... and theres the Problem.

So i want to define a area via 4 pairs of coordinates in the DB and give the Zone to a Player.

When the Player is in between of this coordinates, he is in his own area and is able to place Objects...

but my formula seems wrong on Calculation if the player is in his area.

Maybe a Mistake you can see or a better idea you got for me?

function ptInTriangle(player)

local p_x = player:GetX();

local p_y = player:GetY();

local p_name = player:GetName();

local p_id = player:GetGUIDLow();

local p_map = player:GetMapId();

local p_area = player:GetAreaId();

local Qp0_x = WorldDBQuery("SELECT X1, X2, X3, X4, Y1, Y2, Y3, Y4 FROM Area WHERE Map = " ..p_map.. " AND Player= '" ..p_name.. "' AND AreaID= " ..p_area.. " ");

local p0_x = Qp0_x:GetFloat(0);

local p1_x = Qp0_x:GetFloat(1);

local p2_x = Qp0_x:GetFloat(2);

local p3_x = Qp0_x:GetFloat(3);

local p0_y = Qp0_x:GetFloat(4);

local p1_y = Qp0_x:GetFloat(5);

local p2_y = Qp0_x:GetFloat(6);

local p3_y = Qp0_x:GetFloat(7);

local A = 1/2 * (-p1_y * p2_x + p0_y * (-p1_x + p2_x) + p0_x * (p1_y - p2_y) + p1_x * p2_y);

--sign = A < 0 and -1 or 1;

if(A < 0 and A < -1 or A < 1) then

s = (p0_y * p2_x - p0_x * p2_y + (p2_y - p0_y) * p_x + (p0_x - p2_x) * p_y) * A;

t = (p0_x * p1_y - p0_y * p1_x + (p0_y - p1_y) * p_x + (p1_x - p0_x) * p_y) * A;

if (s > 0 and t > 0 and (s + t) < 2 * A * A) then

return true

else

return false

end

else return false

end

end

local function player_in_area(player)

plr_x = player:GetX();

plr_y = player:GetY();

local plr_name = player:GetName();

local plr_map = player:GetMapId();

local area = player:GetAreaId();

CreateLuaEvent(function() ptInTriangle(player) end, 100,1)

if(ptInTriangle(player) == true) then

return true

elseif(ptInTriangle(player) == true) then

return true

else

return false

end

end

local function place(event, player, spell, skipCheck)

local spellid = spell:GetEntry();

local area = player:GetAreaId();

local x, y, z = spell:GetTargetDest();

local plr_o = player:GetO();

local plr_in = player:GetInstanceId();

local plr_map = player:GetMapId();

local plr_p = player:GetPhaseMask();

local plr_x = player:GetX();

local plr_y = player:GetY();

local plr_z = player:GetZ();

local plr_name = player:GetName();

CreateLuaEvent(function() player_in_area(player) end, 100,1)

if(spellid == 96001 and player_in_area(player) == true) then

PerformIngameSpawn(2, 24470, plr_map, plr_in, x, y, z, plr_o, true, 0, plr_p)

end

end

RegisterPlayerEvent(5, place)

So i want to define a area via 4 pairs of coordinates in the DB and give the Zone to a Player.

When the Player is in between of this coordinates, he is in his own area and is able to place Objects...

but my formula seems wrong on Calculation if the player is in his area.

Maybe a Mistake you can see or a better idea you got for me?

function ptInTriangle(player)

local p_x = player:GetX();

local p_y = player:GetY();

local p_name = player:GetName();

local p_id = player:GetGUIDLow();

local p_map = player:GetMapId();

local p_area = player:GetAreaId();

local Qp0_x = WorldDBQuery("SELECT X1, X2, X3, X4, Y1, Y2, Y3, Y4 FROM Area WHERE Map = " ..p_map.. " AND Player= '" ..p_name.. "' AND AreaID= " ..p_area.. " ");

local p0_x = Qp0_x:GetFloat(0);

local p1_x = Qp0_x:GetFloat(1);

local p2_x = Qp0_x:GetFloat(2);

local p3_x = Qp0_x:GetFloat(3);

local p0_y = Qp0_x:GetFloat(4);

local p1_y = Qp0_x:GetFloat(5);

local p2_y = Qp0_x:GetFloat(6);

local p3_y = Qp0_x:GetFloat(7);

local A = 1/2 * (-p1_y * p2_x + p0_y * (-p1_x + p2_x) + p0_x * (p1_y - p2_y) + p1_x * p2_y);

--sign = A < 0 and -1 or 1;

if(A < 0 and A < -1 or A < 1) then

s = (p0_y * p2_x - p0_x * p2_y + (p2_y - p0_y) * p_x + (p0_x - p2_x) * p_y) * A;

t = (p0_x * p1_y - p0_y * p1_x + (p0_y - p1_y) * p_x + (p1_x - p0_x) * p_y) * A;

if (s > 0 and t > 0 and (s + t) < 2 * A * A) then

return true

else

return false

end

else return false

end

end

local function player_in_area(player)

plr_x = player:GetX();

plr_y = player:GetY();

local plr_name = player:GetName();

local plr_map = player:GetMapId();

local area = player:GetAreaId();

CreateLuaEvent(function() ptInTriangle(player) end, 100,1)

if(ptInTriangle(player) == true) then

return true

elseif(ptInTriangle(player) == true) then

return true

else

return false

end

end

local function place(event, player, spell, skipCheck)

local spellid = spell:GetEntry();

local area = player:GetAreaId();

local x, y, z = spell:GetTargetDest();

local plr_o = player:GetO();

local plr_in = player:GetInstanceId();

local plr_map = player:GetMapId();

local plr_p = player:GetPhaseMask();

local plr_x = player:GetX();

local plr_y = player:GetY();

local plr_z = player:GetZ();

local plr_name = player:GetName();

CreateLuaEvent(function() player_in_area(player) end, 100,1)

if(spellid == 96001 and player_in_area(player) == true) then

PerformIngameSpawn(2, 24470, plr_map, plr_in, x, y, z, plr_o, true, 0, plr_p)

end

end

RegisterPlayerEvent(5, place)