Transport assist widget

Transport assist widget

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
DZHIBRISH
Posts: 357
Joined: 16 Mar 2007, 22:28

Transport assist widget

Post 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?
Tim-the-maniac
Posts: 250
Joined: 22 Jul 2006, 19:58

Post 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.
DZHIBRISH
Posts: 357
Joined: 16 Mar 2007, 22:28

Post by DZHIBRISH »

You heard the man.YOU exist...so go make it good..
MetalSkin
Posts: 77
Joined: 31 Aug 2007, 02:42

Post 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?
User avatar
TechnoTone
Posts: 165
Joined: 23 Aug 2005, 22:02

Post 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
Tim-the-maniac
Posts: 250
Joined: 22 Jul 2006, 19:58

Post 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.
User avatar
Licho
Zero-K Developer
Posts: 3803
Joined: 19 May 2006, 19:13

Post 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
DZHIBRISH
Posts: 357
Joined: 16 Mar 2007, 22:28

Post by DZHIBRISH »

licho..awsome..jsut one thing.. maybe make it enabled by default..
DZHIBRISH
Posts: 357
Joined: 16 Mar 2007, 22:28

Post 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.
User avatar
[XIII]Roxas
Posts: 182
Joined: 20 Jun 2007, 23:44

Post by [XIII]Roxas »

Enabling it by default is a simple changing of enabled=false to true in the LUA.
DZHIBRISH
Posts: 357
Joined: 16 Mar 2007, 22:28

Post by DZHIBRISH »

ya ik now. i did that..but people who have even less idea about lua than me..
User avatar
REVENGE
Posts: 2382
Joined: 24 Aug 2006, 06:13

Post by REVENGE »

Why did I not get this memo. :evil:
DZHIBRISH
Posts: 357
Joined: 16 Mar 2007, 22:28

Post by DZHIBRISH »

by the way sometimes it doesnt command thae transports to area unload
which bcomes ver unconvinient.
Post Reply

Return to “Lua Scripts”