Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local G = -0.05
- local DRAG = 0.99
- local b = {
- G = G,
- DRAG = DRAG,
- void_level = -128,
- reentry_level = 600
- }
- local function interp(p1,p2,y)
- return p1[1] + (p2[1]-p1[1])/(p2[2]-p1[2])*(y-p1[2])
- end
- function b.tick(p,v)
- v[1],v[2] = v[1]*DRAG,v[2]*DRAG + G
- p[1],p[2] = p[1] + v[1],p[2]+v[2]
- end
- function b.simulate(angleRad,nCannonLength,nPowderCharges,tY)
- local v = {nPowderCharges*2*math.cos(angleRad),nPowderCharges*2*math.sin(angleRad)}
- local p = {nCannonLength*math.cos(angleRad),nCannonLength*math.sin(angleRad)}
- local t = 0
- while v[2] >= 0 or p[2] > tY do
- local last_pos = {p[1],p[2]}
- b.tick(p,v)
- if (last_pos[2] > tY) ~= (p[2] > tY) then
- return interp(last_pos,p,tY),t
- end
- t = t+1
- end
- end
- function b.getAngle(tX,tY,nCannonLength,nPowderCharges,err)
- err = tonumber(err) or 0.1
- local angle = 0
- local hit,tick = b.simulate(angle,nCannonLength,nPowderCharges,tY)
- local lastType
- local increment = 0.1
- for i=1,10 do --70s, might be too long for good calculations
- local res = {pcall(function()
- while true do
- if not hit or hit < tX - err then --undershoot -> increase angle
- if lastType == "overshoot" then increment = increment * 0.5 end
- angle = angle + increment
- lastType = "undershoot"
- elseif hit > tX + err then --overshoot decrease angle
- if lastType == "undershoot" then increment = increment * 0.5 end
- angle = angle - increment
- lastType = "overshoot"
- else
- return true,{pitch=angle,simulatedHit={hit,tY},flightTicks=tick}
- end
- if angle < -math.pi/6 or angle >= math.pi/3 then return false,"No suitable angle found for the landing" end
- hit,tick = b.simulate(angle,nCannonLength,nPowderCharges,tY)
- end
- end)}
- if table.remove(res,1) then return unpack(res) end
- sleep(0)
- end
- return false,"timed out"
- end
- return b
Advertisement
Add Comment
Please, Sign In to add comment