Changing a unit's max speed

Changing a unit's max speed

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

Moderator: Moderators

Post Reply
User avatar
thesleepless
Posts: 417
Joined: 24 Oct 2007, 04:49

Changing a unit's max speed

Post 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?
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: Changing a unit's max speed

Post by FLOZi »

Heck, it's even possible using COB.
User avatar
thesleepless
Posts: 417
Joined: 24 Oct 2007, 04:49

Re: Changing a unit's max speed

Post by thesleepless »

example plz?
User avatar
lurker
Posts: 3842
Joined: 08 Jan 2007, 06:13

Re: Changing a unit's max speed

Post 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.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: Changing a unit's max speed

Post by FLOZi »

also relevant is the constant for getting fuel level, of course, i forget what it is.
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: Changing a unit's max speed

Post 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?
User avatar
thesleepless
Posts: 417
Joined: 24 Oct 2007, 04:49

Re: Changing a unit's max speed

Post 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
User avatar
lurker
Posts: 3842
Joined: 08 Jan 2007, 06:13

Re: Changing a unit's max speed

Post 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.
User avatar
thesleepless
Posts: 417
Joined: 24 Oct 2007, 04:49

Re: Changing a unit's max speed

Post 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?
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Changing a unit's max speed

Post 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();
;
Post Reply

Return to “Lua Scripts”