Page 1 of 1

Changing a unit's max speed

Posted: 29 Dec 2008, 11:32
by thesleepless
I'm trying to write a script that will set a unit's max speed to 0 when it runs out of fuel.

i could use paralyze but i still want the unit to be able to attack and do stuff, just not move.

is this possible using lua?

Re: Changing a unit's max speed

Posted: 29 Dec 2008, 12:28
by FLOZi
Heck, it's even possible using COB.

Re: Changing a unit's max speed

Posted: 29 Dec 2008, 14:27
by thesleepless
example plz?

Re: Changing a unit's max speed

Posted: 29 Dec 2008, 17:38
by lurker
Did you look at the wiki? Fine. When the unit is created:
normal_max = get MAX_SPEED

Then you switch between
set MAX_SPEED to 0
set MAX_SPEED to normal_max

If you set turninplace=0 in the fbi then they'll probably also be unable to turn when stuck.

Re: Changing a unit's max speed

Posted: 29 Dec 2008, 18:19
by FLOZi
also relevant is the constant for getting fuel level, of course, i forget what it is.

Re: Changing a unit's max speed

Posted: 29 Dec 2008, 18:31
by smoth
thesleepless wrote:example plz?
kampfer in gundam rts, let it fly over water.
zgok in gundam, moves faster in water.

both can get speed boosts based on conditions.

we have get/set speed for a speed boosts/drops in bos. Take a look at those units, is that what you needed?

Re: Changing a unit's max speed

Posted: 29 Dec 2008, 18:33
by thesleepless
thanks, that helps a lot, this is what i've got so far

Code: Select all

function gadget:GetInfo()
	return {
		name = "supplies",
		desc = "manage fuel and ammo supplies",
		author = "thesleepless",
		date = "Dec 29, 2008",
		license = "Public Domain",
		layer = 0,
		enabled = true
	}
end

if(not gadgetHandler:IsSyncedCode()) then
	return
else
	local ammo = {}
	local fuel = {}
	local outOfFuel = {}
	local outOfAmmo = {}
	local maxSpeeds = {}
	local myTeam = Spring.GetMyTeamID()
end

function gadget:Initialize()
end

function gadget:UnitCreated(unitID, unitDefID, teamID)
	local ud = UnitDefs[unitDefID]
	local unitName = ud.name
	ammo[unitID] = ud.startingAmmo
	fuel[unitID] = ud.startingFuel
end

function gadget:UnitDestroyed(unitID, _, teamID)
	if(ammo[unitID]) then
		ammo[unitID] = nil
	end
	if(fuel[unitID]) then
		fuel[unitID] = nil
	end
end

-- supplies units around the supply unit witn supplies
function UnitSupplyUnits(sUnitID)
	-- get all friendly units within radius
	local posx, posy, posz = Spring.GetUnitPosition(sUnitID)
	local ud = UnitDefs[Spring.GetUnitDefID(unitID)]
	local units = Spring.GetUnitsInCylinder(posx, posz, ud.supplyRadius)
	for unitID,val in pairs(units) do
		if(Spring.GetUnitTeam(unitID) == Spring.GetUnitTeam(sUnitID)) then
			if(ammo[unitID] < ud.fuelCapacity and ammo[sUnitID] >= 1) then
				ammo[unitID] = ammo[unitID] + 1
				ammo[sUnitID] = ammo[sUnitID] - 1
			end
			if(ud.canMove) then
				if(fuel[unitID] < ud.fuelCapacity and fuel[sUnitID] >= 1) then
					if(outOfFuel[unitID]) then
						outOfFuel[unitID] = nil
						Spring.SetUnitCOBValue("MAX_SPEED", maxSpeeds[unitID])
					end
					fuel[unitID] = fuel[unitID] + 1
					fuel[sUnitID] = fuel[sUnitID] - 1
				end
			end
		end
	end
end

function gadet:GameFrame(n)
	if n%30 ~= 0 then
		return
	end
	-- subtract fuel based on movement
	local units = Spring.GetAllUnits()
	for i=1, #units do
		local unitID = units[i]
		local ud = UnitDefs[Spring.GetUnitDefID(unitID)]
		if(ud.canMove) then
			if(not fuel[unitID]) then
				fuel[unitID] = 0
			end
			-- get speed of unit
			local sx, sy, sz = Spring.GetUnitVelocity(unitID)
			local speed = math.sqrt(sx * sx + sy * sy + sz * sz)
			if(fuel[unitID] > 0) then
				fuel[unitID] = fuel[unitID] - (ud.fuelUsage * speed * 30)
			end
			if( not outOfFuel[unitID] and fuel[unitID] <= 0) then
				fuel[unitID] = 0
				outOfFuel[unitID] = true
				maxSpeeds[unitID] = Spring.GetUnitCOBValue(unitID, "MAX_SPEED")
				Spring.SetUnitCOBValue(unitID, "MAX_SPEED", 0)
				if(Spring.GetUnitTeam(unitID) == myTeam) then
					Spring.Echo(ud.name .. " is out of fuel")
				end
			end
		end
	end
end

function gadget:ProjectileCreated(projID, ownerID)
	if(ammo[ownerID]) then
		ammo[ownerID] = ammo[ownerID] - 1
	end
	if(ammo[ownerID] <= 0) then
		outOfAmmo[ownerID] = true
		local ud = UnitDefs[Spring.GetUnitDefID(ownerID)]
		if(Spring.GetUnitTeam(ownerID) == myTeam) then
			Spring.Echo(ud.name .. " is out of ammo")
		end
		-- need to diable the unit from firing somehow
	end
end

Re: Changing a unit's max speed

Posted: 29 Dec 2008, 18:50
by lurker
MAX_SPEED is 75; you don't give that call a string. There is already a fuel system for weapons built into spring, but I'm not sure if anyone ever made it so you could use it on non-planes.

Re: Changing a unit's max speed

Posted: 29 Dec 2008, 19:11
by thesleepless
ok great, i think i'll avoid the inbuilt fuel system for now.

is there a way to set the turn_rate dynamically or is it fixed from when the fbi is loaded?
If you set turninplace=0 in the fbi then they'll probably also be unable to turn when stuck.
but this would stop it from turning in place all the time, correct?
which is undesirable for tanks and i'd like tanks to need fuel to move.

also

Code: Select all

         local sx, sy, sz = Spring.GetUnitVelocity(unitID)
         local speed = math.sqrt(sx * sx + sy * sy + sz * sz)
         if(fuel[unitID] > 0) then
            fuel[unitID] = fuel[unitID] - (ud.fuelUsage * speed * 30)
         end
i'm currently getting the speed from the velocity of the unit, but i'd like to get it from the engine's speed not it's movement speed. eg. if a tank is falling out of a plane then it shouldn't be using fuel, only when it's moving itself. it that data available at all?

Re: Changing a unit's max speed

Posted: 29 Dec 2008, 20:33
by Argh
Yes, you could do that with Lua-->COB, via StartMoving(), as in:

Code: Select all

lua_IamUsingFuelNow(arg) { return(0); }

StartMoving()
call-script lua_IamUsingFuelNow();
;