Forged Alliance Forever Forged Alliance Forever Forums 2020-05-02T18:57:40+02:00 /feed.php?f=53&t=19181 2020-05-02T18:57:40+02:00 2020-05-02T18:57:40+02:00 /viewtopic.php?t=19181&p=183762#p183762 <![CDATA[Change unit movement speed by terrain type]]>
Once you have made your map select the terrain type tab and use Sand01 and paint the areas you want the effect to happen on, you can change the terrain type to another one if you don't like the dust effect sand01 uses just make sure you change the references in the script to match.

With this script you need to make your map unranked because it affects the units in a way that is not always obvious, and might affect the balance.

You should put an explanation in the description on what terrain and where is effected by this script to avoid player confusion.

Standard map (not Adaptive)

open your script.lua and replace everything with this
Code:
local ScenarioUtils = import('/lua/sim/ScenarioUtilities.lua')
local ScenarioFramework = import('/lua/ScenarioFramework.lua')
local simUnit = import('/lua/sim/Unit.lua')
local oldUnit = simUnit.Unit

local TERRAIN_SAND_NAME = 'Sand01'

------ GENERAL

function OnPopulate ()
    ScenarioUtils.InitializeArmies()
    ScenarioFramework.SetPlayableArea('AREA_1' , false)
end

function OnStart (scenario)
   ForkThread(showmessage)
   ForkThread(gatherFeedback)
end

------ Startmessage

function gatherFeedback()
    WaitSeconds(10)
    BroadcastMSG('The -- INSERT TERRAIN OR AREA HERE -- will slow your GROUND units down -- HOVER units are affected less',  -- message
                 40,                                                             -- fontsize
                 'd0d0d0',                                                       -- color
                 10,                                                             -- duration
                 'center')                                                       -- position
end

-- Change message colour to stand out against terrain colour codes are; Yellow - ffff00,  Orange - ffa500, Red - ff0000, Purple - 9370db Blue - 0000ff,
-- Light Blue - 00bfff, Green - 7cfc00, Black - 000000, White - d0d0d0

function showmessage()
    local message = ''
    local sendmessage = false
end

function BroadcastMSG(message, fontsize, RGBColor, duration, location)

-- broadcast a text message to players
-- possible locations = lefttop, leftcenter, leftbottom,  righttop, rightcenter, rightbottom, rightbottom, centertop, center, centerbottom

    PrintText(message, fontsize, 'ff' .. RGBColor, duration , location) ;
end

----- SLOW ON SAND

simUnit.Unit = Class(oldUnit) {
   OnTerrainTypeChange = function(self, new, old)
      oldUnit.OnTerrainTypeChange(self, new, old)
      
      if (new and new.Name == TERRAIN_SAND_NAME) then
         if (isLandUnit(self)) then
            if (isCommanderUnit(self)) then
               self:SetSpeedMult(0.9)
            elseif (isHoveringUnit(self)) then
               self:SetSpeedMult(0.8)
            else
               self:SetSpeedMult(0.6)
            end
         end
      elseif (old and old.Name == TERRAIN_SAND_NAME) then
         if (isLandUnit(self)) then
            self:SetSpeedMult(1)
         end
      end
   end,
}

function isLandUnit (unit)
   if (EntityCategoryContains(categories.MOBILE * categories.LAND, unit)) then
      return true
   else
      return false
   end
end

function isHoveringUnit (unit)
   if (EntityCategoryContains(categories.HOVER, unit)) then
      return true
   else
      return false
   end
end

function isCommanderUnit (unit)
   if (EntityCategoryContains(categories.COMMAND + categories.SUBCOMMANDER, unit)) then
      return true
   else
      return false
   end
end
Now on line 24 is the start message replace INSERT TERRAIN OR AREA HERE with either the terrain type or area the effect will take place i.e Sand, Mud, Darker grass, Then you are ready to test your map, if you want to adjust the effected speed skip to the last part.

Adaptive Maps

With adaptive maps it's a bit more involved but not to hard, first you need to add this, where the script is calling for all the other local references
Code:
local simUnit = import('/lua/sim/Unit.lua')
local oldUnit = simUnit.Unit
local TERRAIN_SAND_NAME = 'Sand01'
Then add this underneath, after the end of --OnStart and before the next function
Code:
----- SLOW ON SAND

simUnit.Unit = Class(oldUnit) {
   OnTerrainTypeChange = function(self, new, old)
      oldUnit.OnTerrainTypeChange(self, new, old)
      
      if (new and new.Name == TERRAIN_SAND_NAME) then
         if (isLandUnit(self)) then
            if (isCommanderUnit(self)) then
               self:SetSpeedMult(0.9)
            elseif (isHoveringUnit(self)) then
               self:SetSpeedMult(0.8)
            else
               self:SetSpeedMult(0.6)
            end
         end
      elseif (old and old.Name == TERRAIN_SAND_NAME) then
         if (isLandUnit(self)) then
            self:SetSpeedMult(1)
         end
      end
   end,
}

function isLandUnit (unit)
   if (EntityCategoryContains(categories.MOBILE * categories.LAND, unit)) then
      return true
   else
      return false
   end
end

function isHoveringUnit (unit)
   if (EntityCategoryContains(categories.HOVER, unit)) then
      return true
   else
      return false
   end
end

function isCommanderUnit (unit)
   if (EntityCategoryContains(categories.COMMAND + categories.SUBCOMMANDER, unit)) then
      return true
   else
      return false
   end
end

--Startmessageslow
function gatherFeedback()
    WaitSeconds(0)
    BroadcastMSG('The -- INSERT TERRAIN OR AREA HERE -- will slow your GROUND units down -- HOVER units are affected less',  -- message
                 40,                                                             -- fontsize
                 'ff0000',                                                       -- color
                 10,                                                             -- duration
                 'center')                                                       -- position
end

-- Change message colour to stand out against terrain colour codes are; Yellow - ffff00,  Orange - ffa500, Red - ff0000, Purple - 9370db Blue - 0000ff,
-- Light Blue - 00bfff, Green - 7cfc00, Black - 000000, White - d0d0d0   
Now find the --startmessageslow part and replace INSERT TERRAIN OR AREA HERE with either the terrain type or area the effect will take place i.e Sand, Mud, Darker grass, Then you are ready to test your map.

Changing the speed

To change how much units are effected you need to change unit:SetSpeedMult(0.9)
with 1 being default speed 0.5 being half speed and anything over 1 will speed units up
there are 3 categories;
Code:
if (new and new.Name == TERRAIN_SAND_NAME) then
         if (isLandUnit(self)) then
            if (isCommanderUnit(unit)) then
               unit:SetSpeedMult(0.9)
            elseif (isHoveringUnit(unit)) then
               unit:SetSpeedMult(0.8)
            else
               unit:SetSpeedMult(0.6)
            end
Commander and Sacu
Code:
if (isCommanderUnit(unit)) then
               unit:SetSpeedMult(0.9)
Hover units
Code:
elseif (isHoveringUnit(unit)) then
               unit:SetSpeedMult(0.8)
everything else
Code:
else
               unit:SetSpeedMult(0.6)

And credit the author in description

Statistics: Posted by MadMax — 02 May 2020, 18:57


]]>