Unable to calculate player speed on concrete and unsure why

Place to get help with not working mods / modding interface.
Utahjarhead
Burner Inserter
Burner Inserter
Posts: 7
Joined: Sat Oct 14, 2023 6:27 pm
Contact:

Unable to calculate player speed on concrete and unsure why

Post by Utahjarhead »

I'm writing a mod to grant a mech suit the ability to fly at the speed as running, even taking into account concrete while simultaneously ignoring the slower speeds of things like Fulgora's oil oceans. It does catch the flying speed properly, but doesn't grant run bonuses based on the underlying tiles such as concrete or refined concrete.

The mod is here: https://mods.factorio.com/mod/mech-suit ... /downloads

But the code is here since it's not all that much. I understand only the basics of modding so far. I would ask that someone point me in the right direction so I can learn from what I've hosed up. Please let me know what I am doing wrong.

Code: Select all

local UPDATE_INTERVAL = 30

local function get_base_flying_speed(character)
  local proto = character.prototype
  if proto and proto.flying_speed then
    return proto.flying_speed
  end
  return nil
end

local function update_character_flying_speed(player)
  if not (player and player.valid) then
    return
  end

  local character = player.character
  if not (character and character.valid) then
    return
  end

  local running_speed = player.character_running_speed
  if not running_speed then
    return
  end

  local tile_modifier = nil
  if character.surface then
    local tile = character.surface.get_tile(character.position)
    if tile and tile.prototype then
      tile_modifier = tile.prototype.walking_speed_modifier
    end
  end

  if tile_modifier then
    if tile_modifier > 1 then
      -- Apply speed bonus (concrete, etc.)
      running_speed = running_speed * tile_modifier
    elseif tile_modifier < 1 then
      -- Remove speed penalty (oil, etc.)
      running_speed = running_speed / tile_modifier
    end
  end

  local base_flying_speed = get_base_flying_speed(character)
  if not base_flying_speed or base_flying_speed <= 0 then
    return
  end

  local desired_modifier = (running_speed / base_flying_speed) - 1

  if character.character_flying_speed_modifier ~= nil then
    character.character_flying_speed_modifier = desired_modifier
  elseif character.flying_speed_modifier ~= nil then
    character.flying_speed_modifier = desired_modifier
  end
end

local function update_player(player)
  if player and player.valid then
    update_character_flying_speed(player)
  end
end

script.on_event(defines.events.on_player_created, function(event)
  update_player(game.players[event.player_index])
end)

script.on_event(defines.events.on_player_respawned, function(event)
  update_player(game.players[event.player_index])
end)

script.on_event(defines.events.on_player_armor_inventory_changed, function(event)
  update_player(game.players[event.player_index])
end)

script.on_event(defines.events.on_tick, function(event)
  if event.tick % UPDATE_INTERVAL ~= 0 then
    return
  end

  for _, player in pairs(game.connected_players) do
    update_player(player)
  end
end)
Post Reply

Return to “Modding help”