Alerting enemies from afar

Place to get help with not working mods / modding interface.
Post Reply
User avatar
TaxiService
Inserter
Inserter
Posts: 37
Joined: Fri Apr 06, 2018 3:56 pm
Contact:

Alerting enemies from afar

Post by TaxiService »

Hello. o/
I have assembled a mod that makes cars honk by pressing a button. Right now the honks have no effect on the world other than playing a sound that can be heard by players.

I am trying to add a function to it though: enemies in range of honking would stop what they are doing and start attacking you instead.

here are the excerpts of the mod files responsible for honking which i am currently using: (there's no code for enemy attraction yet)
entities.lua
control.lua
The wiki page about the explosion prototype is missing. So by looking at other mods I have attempted adding an area of damage to the explosion, something like this...
horn that should damage
...but it seemed to have no effect. Can explosions even do damage? :roll: Or are they only graphical effects + sounds?

and even then, I wanted to try and see if biters would react to being hit by a 0-damage area of effect. (so far i haven't been able to create such aoe at all)
Would that be the case? Am I approaching this the right way? I'm looking at the list of Lua classes but... maybe i'm not looking for the right things?

Is this even possible to do? If you have anything to say to contribute it would be really appreciated. : ) Thanks in advance and good day in any case!
my mods and 'prints

betrok
Fast Inserter
Fast Inserter
Posts: 101
Joined: Wed Feb 28, 2018 12:08 pm
Contact:

Re: Alerting enemies from afar

Post by betrok »

I'm not sure, why damage did not work(and whether it should), but what you actually need to do is just to command bitters to attack car in your event handler.

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Alerting enemies from afar

Post by Nexela »

betrok wrote:I'm not sure, why damage did not work(and whether it should), but what you actually need to do is just to command bitters to attack car in your event handler.
0 damage is 0 damage is 0 damage

However using the command stuff on units should work
in your honk event, search for nearest_enemies make em a group and order them to attack!

User avatar
TaxiService
Inserter
Inserter
Posts: 37
Joined: Fri Apr 06, 2018 3:56 pm
Contact:

Re: Alerting enemies from afar

Post by TaxiService »

Thanks for the tips guys! After sifting the forums and various resources here is what i came up with:

Code: Select all

function attractEnemies()
  for _, enemy in pairs(game.player.surface.find_enemy_units(game.player.position, 50)) do
    enemy.set_command({type=defines.command.attack, target=game.player.character})
  end
end
This function is called inside the main honking function.
But... it doesn't work. When honking, the game errors when computing the for loop line, saying: attempt to index field "player" (a nil value)
I have tried many things but as of now I wasn't able to make it go past the line starting with "for". What am I doing wrong?
search for nearest_enemies make em a group and order them to attack!
How would I go about setting that up..? :shock:
my mods and 'prints

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Alerting enemies from afar

Post by Nexela »

TaxiService wrote:Thanks for the tips guys! After sifting the forums and various resources here is what i came up with:

Code: Select all

function attractEnemies()
  for _, enemy in pairs(game.player.surface.find_enemy_units(game.player.position, 50)) do
    enemy.set_command({type=defines.command.attack, target=game.player.character})
  end
end
This function is called inside the main honking function.
But... it doesn't work. When honking, the game errors when computing the for loop line, saying: attempt to index field "player" (a nil value)
I have tried many things but as of now I wasn't able to make it go past the line starting with "for". What am I doing wrong?
search for nearest_enemies make em a group and order them to attack!
How would I go about setting that up..? :shock:
game.player is only valid for commands at the console

you want to pass the vehicle (because the vehicle is making the noise to anger them!) (or player) doing the honking into your function attract_enemies function and do pairs(object.surface blahhhhhh)

User avatar
TaxiService
Inserter
Inserter
Posts: 37
Joined: Fri Apr 06, 2018 3:56 pm
Contact:

Re: Alerting enemies from afar

Post by TaxiService »

Holy shit it works!!!!

Code: Select all

function attractEnemies(unit, range)
  for _, enemy in pairs(unit.surface.find_enemy_units(unit.position, range)) do
    enemy.set_command({type=defines.command.attack, target=unit.character})
  end
end

script.on_event("horn", function(event)
  local player = game.players[event.player_index]
  if player.vehicle then
    if player.vehicle.type == "car" then
      attractEnemies(player, 80)
      if player.vehicle.name == "tank" then
        playSoundAtEntity(TANK_HORN, player)
      else
        playSoundAtEntity(CAR_HORN, player)
      end
    end
  end
end)

script.on_event("hornlong", function(event)
  local player = game.players[event.player_index]
  if player.vehicle then
    if player.vehicle.type == "car" then
      attractEnemies(player, 120)
      if player.vehicle.name == "tank" then
        playSoundAtEntity(TANK_HORN_LONG, player)
      else
        playSoundAtEntity(CAR_HORN_LONG, player)
      end
    end
  end
end)


although sometimes they seem to stun in place... now i gotta tweak the ranges a bit i guess. I need to develop a sense for tiles as a distance. :lol:

EDIT: by the way THANK YOU SO MUCH BRO \o/ or sis
my mods and 'prints

User avatar
NIronwolf
Inserter
Inserter
Posts: 42
Joined: Sat Jul 07, 2018 6:44 am
Contact:

Re: Alerting enemies from afar

Post by NIronwolf »

I'm not a modder, but I'm thinking the repeated honks are restarting the pathfinding. Is there a status you can test to see if a bitter is already in attack mode? Could skip that one then. If there's a way to see what the target is, you could skip it if it's the same. Then you could play bitter tennis in multiplayer getting them to run between honking players. :)

Post Reply

Return to “Modding help”