[mod-making] Target marker for turrets

Place to get help with not working mods / modding interface.
Post Reply
Aphlax
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sat Dec 04, 2021 10:05 am

[mod-making] Target marker for turrets

Post by Aphlax »

Hey, I'm trying to write a mod and want to create a turret that has an indicator for where it's going to shoot, see the sketch below. Is something like this possible with factorio modding?

If I can not do it with the attack_parameters, maybe I can create the target marker entity it during on_tick (Turrets do not fire an event when they shoot afaik). Is there some better documentation on attack_parameters and projectiles than the wiki? (since I have a hard time understanding it)

Also, unrelated question: Am I right to ask mod-making questions here or is there a better place?

Image

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5148
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: [mod-making] Target marker for turrets

Post by Klonan »

Aphlax wrote:
Sat Dec 04, 2021 10:22 am
Also, unrelated question: Am I right to ask mod-making questions here or is there a better place?
Moved to modding help :)
Aphlax wrote:
Sat Dec 04, 2021 10:22 am
If I can not do it with the attack_parameters
It should be possible with the attack parameters and trigger system, it can create entities and things.
Aphlax wrote:
Sat Dec 04, 2021 10:22 am
maybe I can create the target marker entity it during on_tick (Turrets do not fire an event when they shoot afaik).
You can add a script-trigger trigger effect to the turret, that way it will fire an event when it shoots

Aphlax
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sat Dec 04, 2021 10:05 am

Re: [mod-making] Target marker for turrets

Post by Aphlax »

Hi Klonan, thank you for your answer! I played around with the attack parameters and actions and managed to get half-way there:

Currently (see 1) I am able to spawn a target indicator at the predicted target position. The predicted target position is computed the usual way - taking in account the projectile speed and the target movement. What currently does not work:
  • The indicator spawns at the same time that the projectile is shot. I tried setting a warmup time, but the indicator does not spawn before that is completed.
  • The projectile speed is currently slow, but eventually I want to have it fast. With the current implementation this would make it impossible to evade.
  • The indicator should spawn exactly 1 second before the projectile arrives at the predicted target location (currently it spawns too fast to evade if you are close to the tower and too easy to evade if you are far away).
So now I think I have to do some scripting to get to the desired result. I looked into script-triggers and here is what I'm thinking to do:
  • Instead of firing a projectile, the tower just fires a script trigger. I get the source_entity and target_entity in on_script_trigger_effect.
  • I can not use the target prediction of the tower since there is no way of telling it that I want the target at exactly 1s from now, so I'll have to compute it myself, which is easier since I assume the projectile speed is infinite.
  • I spawn an indicator right away and then wait for 1s to spawn the projectile. (How do I wait for 1s?)
  • There is no way with this method to animate the tower shooting (I want to add a powering-up animation for each shot).
Do you know if there is an easier solution for this, or one that allows me to animate the tower shooting?

Thank you for your help - Aphlax


(1)

Code: Select all

local tower = {
    type = "turret",
    name = "tower",
    ...,
    attack_parameters = {
      type = "projectile",
      range = 16,
      cooldown = 120,
      lead_target_for_projectile_speed = 0.5,
      ammo_type = {
        category = "laser",
        target_type = "position",
        clamp_position = true,
        action = {
          type = "direct",
          action_delivery = {
            type = "projectile",
            projectile = "tower-projectile",
            starting_speed = 0.5,
            target_effects = {
              type = "create-entity",
              entity_name = "tower-target-indicator"
            }
          },
        }
      }
    }
  }

local projectile = {
    type = "projectile",
    name = "tower-projectile",
    ...,
    action = {
      {
        type = "direct",
        action_delivery = {
          type = "instant",
          target_effects = {
            {
              type = "create-entity",
              entity_name = "explosion"
            }
          }
        }
      },
      {
        type = "area",
        radius = 2,
        perimeter = 2,
        action_delivery = {
          type = "instant",
          target_effects = {
            {
              type = "damage",
              damage = {amount = 1, type = "explosion"}
            }
          }
        }
      }
    }
  }

local indicator = {
    type = "corpse",
    name = "tower-target-indicator",
    time_before_removed = 120,
    time_before_shading_off = 60,
    ...
  }

data:extend({tower, projectile, indicator})

Aphlax
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sat Dec 04, 2021 10:05 am

Re: [mod-making] Target marker for turrets

Post by Aphlax »

I moved now to a solution using script-triggers. I also changed to projectile to a beam, since create_entity with a projectile was not able to target a position (only entities with health are possible). Here is the final code if someone is interested:

data.lua

Code: Select all

local tower = {
    type = "turret",
    name = "tower",
    ...,
    attack_parameters = {
      type = "beam",
      range = 16,
      cooldown = 120,
      ammo_type = {
        category = "laser",
        target_type = "entity",
        action = {
          type = "direct",
          action_delivery = {
            type = "instant",
            target_effects = {
              type = "script",
              effect_id = "tower-shot"
            }
          },
        }
      }
    }
  }

local beam = {
    type = "beam",
    name = "tower-beam",
    ...,
    action = {
      {
        type = "direct",
        action_delivery = {
          type = "instant",
          target_effects = {
            {
              type = "create-entity",
              entity_name = "explosion"
            }
          }
        }
      },
      {
        type = "area",
        radius = 2,
        perimeter = 2,
        action_delivery = {
          type = "instant",
          target_effects = {
            {
              type = "damage",
              damage = {amount = 1, type = "explosion"}
            }
          }
        }
      }
    }
  }

local indicator = {
    type = "corpse",
    name = "tower-target-indicator",
    ...,
    time_before_removed = 120,
    time_before_shading_off = 60
  }

data:extend({tower, beam, indicator})
control.lua

Code: Select all

script.on_event(defines.events.on_script_trigger_effect,
  function (event)
    if event.effect_id == "tower-shot" and event.target_entity ~= nil then
        local first_position = event.target_entity.position
        onTick(event.tick + 2, function(new_tick)
            local second_position = event.target_entity.position
            local target = v_add(first_position, v_scale(v_sub(second_position, first_position), 60))

            game.get_surface(event.surface_index).create_entity({
                name = "tower-target-indicator",
                force = 'neutral',
                position = target
            })
            
            onTick(new_tick + 60, function()
                game.get_surface(event.surface_index).create_entity({
                    name = "tower-beam",
                    force = event.source_entity.force,
                    source = event.source_entity,
                    position = event.source_entity.position,
                    target_position = target,
                    source_offset = { x = 0, y = -3 },
                    duration = 10,
                    max_length = 16
                })
            end)
        end)
    end
  end)

Post Reply

Return to “Modding help”