Re: PTPM mode bugs
01 September 2009, 00:29:38
last edit: 01 September 2009, 01:24:37 by Remp
had a look at vehicle z coords, its a lot more difficult than i thought it would be
getGroundPosition() needs the area to be streamed in so you have to warp your player to the vehicles before you can calculate the new position
i wrote the following code (does sf in about 1 minute) but some of them return rediculous values (one pcj drops 50 units) and ive no idea why:
clientside:
addCommandHandler("modz",function()
zmod_new_dist = {}
zmod_vehicles = getElementsByType("vehicle")
zmod_index = 1
-- leviathan, seasparrow, skimmer, vortex
zmod_ignore = {[417] = true, [447] = true, [460] = true, [539] = true}
local function LoopVehicles()
-- havent loaded the vehicle before
if not zmod_new_dist[zmod_vehicles[zmod_index]] and zmod_vehicles[zmod_index] and getVehicleType(zmod_vehicles[zmod_index]) ~= "Boat" and not zmod_ignore[getElementModel(zmod_vehicles[zmod_index])] then
local x,y,z = getElementPosition(zmod_vehicles[zmod_index])
setElementPosition(getLocalPlayer(),x,y,z+1)
setTimer(function(x,y,z)
outputChatBox("Modding vehicle "..zmod_index)
zmod_new_dist[zmod_vehicles[zmod_index]] = CalculateNewZ(zmod_vehicles[zmod_index])
zmod_index = zmod_index + 1
for i,v in ipairs(zmod_vehicles) do
-- if its streamed in and we havent yet modded it
if isElementStreamedIn(v) and not zmod_new_dist[v] and getVehicleType(zmod_vehicles[i]) ~= "Boat" and not zmod_ignore[getElementModel(zmod_vehicles[i])] then
-- if getDistanceBetweenPoints3D(x,y,z,getElementPosition(zmod_vehicles[i])) < 100 then
outputChatBox("Modding streamed vehicle "..i)
zmod_new_dist[zmod_vehicles[i]] = CalculateNewZ(zmod_vehicles[i])
-- end
end
end
LoopVehicles()
end,3000,1,x,y,z)
else
outputChatBox("Already modded vehicle "..zmod_index)
if zmod_index > 500 then outputChatBox("index 500, breaking") TellServerNewZ() return end
if not zmod_vehicles[zmod_index] then outputChatBox("finished all vehicles") TellServerNewZ() return end
zmod_index = zmod_index + 1
LoopVehicles()
end
return
end
if zmod_index == 1 then
LoopVehicles()
end
end)
function TellServerNewZ()
outputChatBox("Modded "..zmod_index.." vehicles")
triggerServerEvent("NewVehicleZDist",getLocalPlayer(),zmod_new_dist)
end
function CalculateNewZ(vehicle)
local x,y,z = getElementData(vehicle,"posX"), getElementData(vehicle,"posY"), getElementData(vehicle,"posZ")
local gz = getGroundPosition(x,y,z+0.5)
local dist = getElementDistanceFromCentreOfMassToBaseOfModel(vehicle)
return gz + dist
end
serverside:
addEvent("NewVehicleZDist",true)
addEventHandler("NewVehicleZDist",getRootElement(),function(new_dist)
outputChatBox("Setting new positions")
for vehicle,dist in pairs(new_dist) do
local x,y = getElementData(vehicle,"posX"), getElementData(vehicle,"posY")
setElementPosition(vehicle,x,y,dist)
end
local rootnode = xmlLoadFile(":"..runningMapName.."/"..runningMapName..".map")
local nodeindex = 1
local vehicles = getElementsByType("vehicle")
for _,node in ipairs(xmlNodeGetChildren(rootnode)) do
if xmlNodeGetName(node) == "vehicle" then
local oldz = xmlNodeGetAttribute(node,"posZ")
local difference = tonumber(oldz)-tonumber(new_dist[vehicles[nodeindex]])
if oldz and vehicles[nodeindex] and new_dist[vehicles[nodeindex]] then
-- for some reason some vehicles return differences in the +/- 30 range, this is obviously wrong and i have no idea why they react so badly
-- filter them out and fix them by hand if necessary
if math.abs(difference) < 5 then
xmlNodeSetAttribute(node,"posZ",string.format("%.4f",new_dist[vehicles[nodeindex]]))
outputChatBox("Changed "..getVehicleName(vehicles[nodeindex]).." "..oldz.." to "..getVehicleName(vehicles[nodeindex]).." "..string.format("%.4f",new_dist[vehicles[nodeindex]]).." ("..string.format("%.4f",difference)..") ["..nodeindex.."]")
nodeindex = nodeindex + 1
else
outputChatBox("Ignored "..getVehicleName(vehicles[nodeindex]).." ("..difference..")")
nodeindex = nodeindex + 1
end
end
end
end
outputChatBox("Saving new positions to :"..runningMapName.."/"..runningMapName..".map")
xmlSaveFile(rootnode)
xmlUnloadFile(rootnode)
end)
unless any of you have got any ideas why, we can use this and filter out any obviously fucked results then do them by hand later
this will also mess up the map file formatting (removes newlines, puts comments on their own line) which is pretty annoying but i dont think theres anything we can do (unless someone can think of a better way to do all this, possibly just dump the new z coords in an xml and write a seperate program to parse them and replace in the maps)