Page 1 of 1

Transport assist widget

Posted: 10 Oct 2007, 22:13
by DZHIBRISH
Hi.Why isnt the transport assist widget working?the one that allows u to set a transport to guard labs and it stops units built and makes the transport cary them to the labs rally point.
Whoever did the widget...its awsome can you fix it please?

Posted: 11 Oct 2007, 21:06
by Tim-the-maniac
I made it tho I cant find the DL link for it anymore (there is still some part of UF serving spring files right?). Also I dont have spring to test it but im sure plenty of people here can fix and make it less crappy at the same time.

Posted: 12 Oct 2007, 02:46
by DZHIBRISH
You heard the man.YOU exist...so go make it good..

Posted: 12 Oct 2007, 04:43
by MetalSkin
If someone can post the gadget I'm happy to have a gander and see what's wrong. I like the concept behind the widget and would love to have it working, so seems reasonable that I should look at it and hopefully get it working.

Can someone post it please? Or give the name of it so I can do a google/uf search for it please?

Posted: 12 Oct 2007, 09:43
by TechnoTone
Here's one I found but I don't know if it's up-to-date:

Code: Select all

--[[
When this widget is enabled, air transports guarding factories will 
automatically take new units built at the factory to the destination of the factory
--]]

function widget:GetInfo()
  return {
    name      = "Transportation Assister",
    desc      = "Transports guarding factories will automatically move new units",
    author    = "Ray",
    date      = "Feb 18, 2007",
    license   = "GNU GPL, v2 or later",
    layer     = 0,
    enabled   = false
  }
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

include("spring.h.lua")

function widget:Initialize()
	local _, _, spec = Spring.GetPlayerInfo(Spring.GetMyPlayerID())
	if spec then
		widgetHandler:RemoveWidget()
		return false
	end
end

function widget:UnitFromFactory(unitID, unitDefID, unitTeam, factID, factDefID, userOrders) 
	if unitTeam ~= Spring.GetMyTeamID() then return end

	local cQueue = Spring.GetCommandQueue(factID)
	local UD = UnitDefs[Spring.GetUnitDefID(unitID)]
	local transID = null
	if not UD.springCategories["notair"] or not UD.springCategories["notship"] then return end
	for _, elem in ipairs(cQueue) do
		if elem.id ~= CMD_MOVE then
			return
		end
	end
	
	transID = IdleTransport(factID)
	if transID ~= null then
		MoveUnit(unitID, factID)
		TransportUnit(unitID, transID, factID)
	end
	
end


function TransportUnit(unitID, transID, factID)
	local transCQueue = Spring.GetCommandQueue(transID)
	local params = {}
	local newParams
	local tries = 0
	if table.getn(transCQueue) > 2 then
		Spring.GiveOrderToUnit(transID, CMD_GUARD, {factID}, {"shift"})
		Spring.GiveOrderToUnit(transID, CMD_LOAD_UNITS, {unitID}, {"shift"})
	else
		Spring.GiveOrderToUnit(transID, CMD_LOAD_UNITS, {unitID}, {""})
	end
	local fCQueue = Spring.GetCommandQueue(factID)
	
	local UD = UnitDefs[Spring.GetUnitDefID(transID)]
		
	for num, elem in ipairs(fCQueue) do
		params = elem.params
		newParams = params
		while OverOldCmd(transID, newParams) and tries < 20 do
			newParams = {params[1] + math.random(1,100), params[2], params[3] + math.random(1,100)}
			tries = tries + 1
		end
		if num ~= table.getn(fCQueue) then
			Spring.GiveOrderToUnit(transID, CMD_MOVE, newParams, {"shift"})
		else
			Spring.GiveOrderToUnit(transID, CMD_UNLOAD_UNIT, newParams, {"shift"})
		end
	end
	Spring.GiveOrderToUnit(transID, CMD_STOP, {}, {"shift"})

	Spring.GiveOrderToUnit(transID, CMD_GUARD, {factID}, {"shift"})
	
end

function OverOldCmd(unitID, params)
	local cQueue = Spring.GetCommandQueue(unitID)
	local nParams
	for _, elem in ipairs(cQueue) do

		if elem.id == CMD_MOVE or elem.id == CMD_UNLOAD_UNIT then 
			nParams = elem.params
			if nParams[1] > params[1] - 30 and nParams[1] < params[1] + 30 then
				if nParams[3] > params[3] - 30 and nParams[3] < params[3] + 30 then
					return true
				end
			end
		end
	end
	return false
end

function MoveUnit(unitID, factID)
	local fX, fY, fZ = Spring.GetUnitPosition(factID)
	local bF = Spring.GetUnitBuildFacing(factID)
	local xAdd
	local yAdd
	if bF == 0 then
		xAdd = 0
		yAdd = 70
	elseif bF == 1 then
		xAdd = 70
		yAdd = 0
	elseif bF == 2 then
		xAdd = 0
		yAdd = -70
	else
		xAdd = -70
		yAdd = 0
	end
	Spring.GiveOrderToUnit(unitID, CMD_MOVE, {fX + xAdd, fY, fZ + yAdd} , {""})

end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function IdleTransport(factID)
	local cQueue
	local leastQCount
	local idleTrans = null
	local lastCmd
	local uList = Spring.GetTeamUnits(Spring.GetMyTeamID())
	local leastDist = null
	for _, ID in ipairs(uList) do -- cycle through every unit
		UD = UnitDefs[Spring.GetUnitDefID(ID)]
		if UD.isTransport then
			cQueue = Spring.GetCommandQueue(ID)
			lastCmd = table.getn(cQueue)
			
			if (lastCmd > 0 and cQueue[lastCmd].id == CMD_GUARD and cQueue[lastCmd].params[1] == factID) or (lastCmd > 1 and cQueue[lastCmd-1].id == CMD_GUARD and cQueue[lastCmd-1].params[1] == factID) then
					
				if idleTrans ~= null then
					if table.getn(Spring.GetCommandQueue(ID)) < table.getn(Spring.GetCommandQueue(idleTrans)) then
						leastQCount = table.getn(Spring.GetCommandQueue(ID))
						idleTrans = ID
					end
				else
					leastQCount = table.getn(Spring.GetCommandQueue(ID))
					idleTrans = ID
				end
			end
			
		end
	end
	
	if idleTrans == null then 
		return null 
	end
	
	for _, ID in ipairs(uList) do -- cycle through every unit
		UD = UnitDefs[Spring.GetUnitDefID(ID)]
		if UD.isTransport then
			cQueue = Spring.GetCommandQueue(ID)
			lastCmd = table.getn(cQueue)
				
			if (lastCmd > 0 and cQueue[lastCmd].id == CMD_GUARD and cQueue[lastCmd].params[1] == factID) or (lastCmd > 1 and (cQueue[lastCmd-1].id == CMD_GUARD and cQueue[lastCmd-1].params[1] == factID)) then
				if lastCmd == leastQCount or lastCmd == leastQCount + 1 then
					if leastDist ~= null then
						if DistFromFac(ID, factID) < leastDist then
							leastDist = DistFromFac(ID, factID)
							idleTrans = ID
						end
					else
						leastDist = DistFromFac(ID, factID)
						idleTrans = ID
					end
				end
			end
		end
	end
	
	return idleTrans
	
end


function DistFromFac(transID, factID)
	
	local uX, uY, uZ = Spring.GetUnitPosition(transID)
	local fX, fY, fZ = Spring.GetUnitPosition(factID)
    return math.sqrt((uX - fX)^2 + (uZ - fZ)^2)

end

function echo(msg)
  Spring.SendCommands({"echo " .. msg})
end

Posted: 13 Oct 2007, 14:08
by Tim-the-maniac
urh please delete that :p. Best to just remake it and also add something which checks the unload command locations to make sure 2 arent placed too near each other to cancel out.

Posted: 13 Oct 2007, 18:43
by Licho
I modified this widget and it works for me (uses area unload and stops unit it tries to pick-up)

http://widgets.licho.eu/unit_transport_assist.lua

Posted: 13 Oct 2007, 20:15
by DZHIBRISH
licho..awsome..jsut one thing.. maybe make it enabled by default..

Posted: 14 Oct 2007, 14:28
by DZHIBRISH
another thingy can you correct it so that a transport wont move if its not completly full..cause otherwise the transports are wasted .the t2 transport for example.plus the t1 transports in my mod can carry more than 1 unit as well.

Posted: 14 Oct 2007, 19:17
by [XIII]Roxas
Enabling it by default is a simple changing of enabled=false to true in the LUA.

Posted: 14 Oct 2007, 19:53
by DZHIBRISH
ya ik now. i did that..but people who have even less idea about lua than me..

Posted: 15 Oct 2007, 00:55
by REVENGE
Why did I not get this memo. :evil:

Posted: 15 Oct 2007, 01:55
by DZHIBRISH
by the way sometimes it doesnt command thae transports to area unload
which bcomes ver unconvinient.