What is the right command to make a unit damage itself?

Interesting mapping tools and mapping help.

What is the right command to make a unit damage itself?

Postby Ghoustaq » 18 Mar 2018, 20:23

Damage(instigator, target, amount, damageType) ?

or

Entity:Kill(instigator,type,excessDamageRatio)?


I am not clear about the exact effects of these two commands.

The below is my case to use "Damage" command in a mapscript. Of course, it's not work yet. I mean no damage happens. :?

Can anybody fix it? I will be thankful :)

Code: Select all
local ScenarioUtils = import('/lua/sim/ScenarioUtilities.lua')
local ScenarioFramework = import('/lua/ScenarioFramework.lua')

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



local oldDamageArea = DamageArea

local self = import('/lua/sim/Entity.lua').Entity()
   for k = 1, 1 do
      local DAMAGEAREAName = 'DAMAGEAREA_' .. k
      local location = ScenarioUtils.MarkerToPosition(DAMAGEAREAName)
           
      ForkThread(Damage, self)
      end
end



Damage = function(instigator, target, amount, damageType)
    local px, py, pz = unpack(location)
    local army = ListArmies()
    local rect = Rect(px-radius, pz-radius, px+radius, pz+radius)
    local units = GetUnitsInRect(rect) or {}
    local amount = 30
    local damageAllies = true
    local damageSelf = true
    local radius = 25
    local damageType = 'Reclaimed'
    local target = units
    for _, u in units do
        if VDist3(u:GetPosition(), location) <= radius then
           continue
        end
        if instigator == u then
            if damageSelf then
                local vector = import('/lua/utilities.lua').GetDirectionVector(location, u:GetPosition())
                -- need this ugliness due to Damage() refuse to damage when instigator == u
                instigator:Damage(instigator, u, 30, vector, 'Reclaimed')
            end
        elseif damageAllies or not IsAlly(army, u:GetArmy()) then
            instigator:Damage(instigator, u, 30, 'Reclaimed')
        end
    end

    local reclaim = GetReclaimablesInRect(rect) or {}
    for _, r in reclaim do
        if IsProp(r) and VDist3(r:GetPosition(), location) <= radius then
            Damage(instigator, location, r, 30, 'Reclaimed')
        end
    end

     -- Get rid of trees
     oldDamageArea(instigator, location, radius, 1, 'Reclaimed', false, false)
    end


function OnStart(self)
end


And the error reported in the Moho log

    WARNING: Error running lua script: ...reme commander forged alliance\maps\11\11_script.lua(24): access to nonexistent global variable "location"
    stack traceback:
    [C]: in function `error'
    ...alliance\gamedata\mohodata.scd\lua\system\config.lua(53): in function <...alliance\gamedata\mohodata.scd\lua\system\config.lua:52>
    ...reme commander forged alliance\maps\11\11_script.lua(24): in function <...reme commander forged alliance\maps\11\11_script.lua:23>
Last edited by Ghoustaq on 19 Mar 2018, 11:29, edited 1 time in total.
User avatar
Ghoustaq
Avatar-of-War
 
Posts: 65
Joined: 25 Dec 2017, 13:54
Has liked: 10 times
Been liked: 0 time
FAF User Name: Ghoustaq

Re: What is the right command to make a unit damage itself?

Postby speed2 » 18 Mar 2018, 22:49

Ghoustaq wrote:Damage(instigator, target, amount, damageType) ?

or

Entity:Kill(instigator,type,excessDamageRatio)?


I am not clear about the exact effects of these two commands.

The below is my case to use "Damage" command in a mapscript. Of course, it's not work yet. I mean no damage happens. :?

Does anybody can fix it? I will be thankful :)

Code: Select all
local ScenarioUtils = import('/lua/sim/ScenarioUtilities.lua')
local ScenarioFramework = import('/lua/ScenarioFramework.lua')

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



local oldDamageArea = DamageArea

local self = import('/lua/sim/Entity.lua').Entity()
   for k = 1, 1 do
      local DAMAGEAREAName = 'DAMAGEAREA_' .. k
      local location = ScenarioUtils.MarkerToPosition(DAMAGEAREAName)
           
      ForkThread(Damage, self)
      end
end



Damage = function(instigator, target, amount, damageType)
    local px, py, pz = unpack(location)
    local army = ListArmies()
    local rect = Rect(px-radius, pz-radius, px+radius, pz+radius)
    local units = GetUnitsInRect(rect) or {}
    local amount = 30
    local damageAllies = true
    local damageSelf = true
    local radius = 25
    local damageType = 'Reclaimed'
    local target = units
    for _, u in units do
        if VDist3(u:GetPosition(), location) <= radius then
           continue
        end
        if instigator == u then
            if damageSelf then
                local vector = import('/lua/utilities.lua').GetDirectionVector(location, u:GetPosition())
                -- need this ugliness due to Damage() refuse to damage when instigator == u
                instigator:Damage(instigator, u, 30, vector, 'Reclaimed')
            end
        elseif damageAllies or not IsAlly(army, u:GetArmy()) then
            instigator:Damage(instigator, u, 30, 'Reclaimed')
        end
    end

    local reclaim = GetReclaimablesInRect(rect) or {}
    for _, r in reclaim do
        if IsProp(r) and VDist3(r:GetPosition(), location) <= radius then
            Damage(instigator, location, r, 30, 'Reclaimed')
        end
    end

     -- Get rid of trees
     oldDamageArea(instigator, location, radius, 1, 'Reclaimed', false, false)
    end


function OnStart(self)
end


And the error reported in the Moho log

    WARNING: Error running lua script: ...reme commander forged alliance\maps\11\11_script.lua(24): access to nonexistent global variable "location"
    stack traceback:
    [C]: in function `error'
    ...alliance\gamedata\mohodata.scd\lua\system\config.lua(53): in function <...alliance\gamedata\mohodata.scd\lua\system\config.lua:52>
    ...reme commander forged alliance\maps\11\11_script.lua(24): in function <...reme commander forged alliance\maps\11\11_script.lua:23>

Well the error says that on line 24 you're trying to use variable that doesnt exist (location). You dont have it defined in that function.

Else Damage(instigator, target, amount, damageType) should damage the unit, what's not clear about it?
Kill will damage it as well but well... kill it in the process, do kinda fatal damage.
User avatar
speed2
Supreme Commander
 
Posts: 2986
Joined: 05 Jan 2013, 15:11
Has liked: 545 times
Been liked: 1004 times
FAF User Name: speed2

Re: What is the right command to make a unit damage itself?

Postby Ghoustaq » 19 Mar 2018, 19:00

@speed2

Thank you, I have defined "location" now. but the new error comes.


WARNING: Error running lua script: ...reme commander forged alliance\maps\11\11_script.lua(27): stack overflow
stack traceback:
...reme commander forged alliance\maps\11\11_script.lua(27): in function `Damage'
...reme commander forged alliance\maps\11\11_script.lua(58): in function `Damage'
...reme commander forged alliance\maps\11\11_script.lua(58): in function `Damage'
...reme commander forged alliance\maps\11\11_script.lua(58): in function `Damage'
...reme commander forged alliance\maps\11\11_script.lua(58): in function `Damage'
...reme commander forged alliance\maps\11\11_script.lua(58): in function `Damage'
...reme commander forged alliance\maps\11\11_script.lua(58): in function `Damage'
...reme commander forged alliance\maps\11\11_script.lua(58): in function `Damage'
...reme commander forged alliance\maps\11\11_script.lua(58): in function `Damage'
...reme commander forged alliance\maps\11\11_script.lua(58): in function `Damage'
...reme commander forged alliance\maps\11\11_script.lua(58): in function `Damage'
...
...reme commander forged alliance\maps\11\11_script.lua(58): in function `Damage'
...reme commander forged alliance\maps\11\11_script.lua(58): in function `Damage'
...reme commander forged alliance\maps\11\11_script.lua(58): in function `Damage'
...reme commander forged alliance\maps\11\11_script.lua(58): in function `Damage'
...reme commander forged alliance\maps\11\11_script.lua(58): in function `Damage'
...reme commander forged alliance\maps\11\11_script.lua(58): in function `Damage'
...reme commander forged alliance\maps\11\11_script.lua(58): in function `Damage'
...reme commander forged alliance\maps\11\11_script.lua(58): in function `Damage'
...reme commander forged alliance\maps\11\11_script.lua(58): in function `Damage'
...reme commander forged alliance\maps\11\11_script.lua(58): in function <...reme commander forged alliance\maps\11\11_script.lua:25>


What does it mean?

line 25: Damage = function(instigator, location, target, amount, damageType)
line 27: local px, py, pz = unpack(location)
line 58: Damage(instigator, location, 25, 1, 'Force', false, false)


the whole codes here

Code: Select all
local ScenarioUtils = import('/lua/sim/ScenarioUtilities.lua')
local ScenarioFramework = import('/lua/ScenarioFramework.lua')

function OnPopulate()
   ScenarioUtils.InitializeArmies()





local self = import('/lua/sim/Entity.lua').Entity()
   for k = 1, 1 do
    local DAMAGEAREAName = 'DAMAGEAREA_' .. k
    local location = ScenarioUtils.MarkerToPosition(DAMAGEAREAName)



 
      ForkThread(Damage, self, location)
      end
end



Damage = function(instigator, location, target, amount, damageType)

    local px, py, pz = unpack(location)
    local army = ListArmies()
    local amount = 30
    local damageAllies = true
    local damageSelf = true
    local damageType = 'Reclaimed'
    local rect = Rect(px-25, pz-25, px+25, pz+25)
    local units = GetUnitsInRect(rect) or {}
    for _, u in units do
        if VDist3(u:GetPosition(), location) <= 25 then
           continue
        end
        if instigator == u then
            if damageSelf then
                local vector = import('/lua/utilities.lua').GetDirectionVector(location, u:GetPosition())
                -- need this ugliness due to Damage() refuse to damage when instigator == u
                instigator:Damage(instigator, u, 30, vector, 'Reclaimed')
            end
        elseif damageAllies or not IsAlly(army, u:GetArmy()) then
            instigator:Damage(instigator, u, 30, 'Reclaimed')
        end
    end

    local reclaim = GetReclaimablesInRect(rect) or {}
    for _, r in reclaim do
        if IsProp(r) and VDist3(r:GetPosition(), location) <= 25 then
            Damage(instigator, location, r, 30, 'Reclaimed')
        end
    end

     -- Get rid of trees
     Damage(instigator, location, 25, 1, 'Force', false, false)
end

function OnStart(self)
end
User avatar
Ghoustaq
Avatar-of-War
 
Posts: 65
Joined: 25 Dec 2017, 13:54
Has liked: 10 times
Been liked: 0 time
FAF User Name: Ghoustaq

Re: What is the right command to make a unit damage itself?

Postby Ghoustaq » 19 Mar 2018, 19:16

@speed2 I deleted the line 58, then no error is reported, but no damage happens still. I feel so confused :?
User avatar
Ghoustaq
Avatar-of-War
 
Posts: 65
Joined: 25 Dec 2017, 13:54
Has liked: 10 times
Been liked: 0 time
FAF User Name: Ghoustaq

Re: What is the right command to make a unit damage itself?

Postby ZLO_RD » 19 Mar 2018, 19:36

from all units in FA i only know nukes do damage to themself. and for quite some time t4 nuke could not do it.

also scatis and trebuchet do minor damage to themself (1-5 dmg per shot or so)

maybe napalm units can burn themself as well... ( but i could not do it so i don't remember)

Edit: also billy nuke
http://www.youtube.com/user/dimatularus
http://www.twitch.tv/zlo_rd
TA4Life: "At the very least we are not slaves to the UI"
User avatar
ZLO_RD
Supreme Commander
 
Posts: 2075
Joined: 27 Oct 2011, 13:57
Location: Russia, Tula
Has liked: 270 times
Been liked: 336 times
FAF User Name: ZLO

Re: What is the right command to make a unit damage itself?

Postby Franck83 » 19 Mar 2018, 23:20

@speed2 I deleted the line 58, then no error is reported, but no damage happens still. I feel so confused :?


in Unit.lua, i'm using this method that impact directly the health. Give it a try.

Code: Select all
instigator:AdjustHealth(instigator, - DamageAmount)
Alliance of Heroes Mod is out ! Try it ! It's in the Mod Vault !
User avatar
Franck83
Priest
 
Posts: 414
Joined: 30 Dec 2016, 11:59
Location: France
Has liked: 71 times
Been liked: 78 times
FAF User Name: Franck

Re: What is the right command to make a unit damage itself?

Postby Ghoustaq » 20 Mar 2018, 17:55

Franck83 wrote:
@speed2 I deleted the line 58, then no error is reported, but no damage happens still. I feel so confused :?


in Unit.lua, i'm using this method that impact directly the health. Give it a try.

Code: Select all
instigator:AdjustHealth(instigator, - DamageAmount)



Great! :lol: this command does work! Thanks for your help!

Code: Select all
local ScenarioUtils = import('/lua/sim/ScenarioUtilities.lua')
local ScenarioFramework = import('/lua/ScenarioFramework.lua')

function OnPopulate()
   ScenarioUtils.InitializeArmies()   
end

function OnStart(self)

   ForkThread(HealthAdjustor)
end

   local DAMAGEAREAName
   local location
   local px, py, pz
   local rect
   local units
   local health
   local preAdjHealth

function HealthAdjustor(self)
    while true do
      for k = 1, 1 do
        DAMAGEAREAName = 'DAMAGEAREA_' .. k
        location = ScenarioUtils.MarkerToPosition(DAMAGEAREAName)
        px, py, pz = unpack(location)
        rect = Rect(px-25, pz-25, px+25, pz+25)
        units = GetUnitsInRect(rect)
      end
      for _, instigator in units do
        if VDist3(instigator:GetPosition(), location) <= 25 then
        health = instigator:GetHealth()
        instigator:AdjustHealth(instigator, -30)
        end
      end

        WaitSeconds(10)
    end
end
User avatar
Ghoustaq
Avatar-of-War
 
Posts: 65
Joined: 25 Dec 2017, 13:54
Has liked: 10 times
Been liked: 0 time
FAF User Name: Ghoustaq

Re: What is the right command to make a unit damage itself?

Postby Franck83 » 20 Mar 2018, 18:33

The method with GetUnitsAroundPoint() is much easier to use and more CPU friendly. But it's in circle from unit position not from rect :

You can specify unit categories (from bp.Categories in unit blueprints) :

Code: Select all
local units = unit:GetAIBrain():GetUnitsAroundPoint(categories.COMMAND + categories.SUBCOMMANDER, unit:GetPosition(), 25, 'Enemy')
for _, instigator in units do
   instigator:AdjustHealth(instigator, -30)
end
Alliance of Heroes Mod is out ! Try it ! It's in the Mod Vault !
User avatar
Franck83
Priest
 
Posts: 414
Joined: 30 Dec 2016, 11:59
Location: France
Has liked: 71 times
Been liked: 78 times
FAF User Name: Franck


Return to Mapping

Who is online

Users browsing this forum: No registered users and 1 guest