[ Solved ]An item fuel that uses energy stored on equipment grid to power vehicles

This is the place to request new mods or give ideas about what could be done.
N0TZ3R0
Inserter
Inserter
Posts: 22
Joined: Tue Dec 29, 2020 1:36 am
Contact:

[ Solved ]An item fuel that uses energy stored on equipment grid to power vehicles

Post by N0TZ3R0 »

Hello guys, I'm veeery new to modding and I wanna suggest for u guys or get any help with a project, basically I'll tell y'all a little bit of what I'm thinking....

An item ( maybe called electric transformer ) that has a fuel type of chemical ( like coal etc ) and when put on a vehicle's fuel slot, it will check if the vehicle has in it equipment grid ( thanks to other mods such as vehicle grid by optera ) energy stored, then it'll "convert" it to make the vehicle working, it's similar to the mod electric vehicles reborn, but the big difference is that u can put the electric transformer ( the item ) in any vehicle ( even modded ) that has an equipment grid and needs fuel to run, any ideas or anyone that can make this ?
Last edited by N0TZ3R0 on Sun Jan 10, 2021 2:17 am, edited 1 time in total.

sherlockholmes221b
Burner Inserter
Burner Inserter
Posts: 10
Joined: Tue Dec 31, 2019 6:02 pm
Contact:

Re: An item fuel that uses energy stored on equipment grid to power vehicles

Post by sherlockholmes221b »

This would be possible (I think) if the item was in vehicle inventory. Not the burning slot.

N0TZ3R0
Inserter
Inserter
Posts: 22
Joined: Tue Dec 29, 2020 1:36 am
Contact:

Re: An item fuel that uses energy stored on equipment grid to power vehicles

Post by N0TZ3R0 »

Yep, if it works, no problem where the item will be, do u have any code idea ?

User avatar
ptx0
Smart Inserter
Smart Inserter
Posts: 1507
Joined: Wed Jan 01, 2020 7:16 pm
Contact:

Re: An item fuel that uses energy stored on equipment grid to power vehicles

Post by ptx0 »

N0TZ3R0 wrote:
Tue Dec 29, 2020 1:45 am
Hello guys, I'm veeery new to modding and I wanna suggest for u guys or get any help with a project, basically I'll tell y'all a little bit of what I'm thinking....
...
sounds like what the electric train mods do already, sorta.

N0TZ3R0
Inserter
Inserter
Posts: 22
Joined: Tue Dec 29, 2020 1:36 am
Contact:

Re: An item fuel that uses energy stored on equipment grid to power vehicles

Post by N0TZ3R0 »

I didn't test it but as I said it's exactly what "Electric vehicles reborn" do but they do only for vanilla vehicles, not modded one as I'm thinking

PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: An item fuel that uses energy stored on equipment grid to power vehicles

Post by PFQNiet »

Your transformer would be better as an equipment grid item itself. Maybe a battery-type equipment that periodically deletes its energy to replenish the fuel.

As for the fuel, you can either just use solid-fuel (or any built-in fuel item) or create your own (to ensure there aren't mod incompatibilities). Set the vehicle's burner's currently_burning to the chosen fuel and the remaining fuel to some amount, draining the "battery" equipment accordingly.

N0TZ3R0
Inserter
Inserter
Posts: 22
Joined: Tue Dec 29, 2020 1:36 am
Contact:

Re: An item fuel that uses energy stored on equipment grid to power vehicles

Post by N0TZ3R0 »

That's a perfect ideia, but since I'm very new to Lua and modding, sounds complicated for me.

N0TZ3R0
Inserter
Inserter
Posts: 22
Joined: Tue Dec 29, 2020 1:36 am
Contact:

Re: An item fuel that uses energy stored on equipment grid to power vehicles

Post by N0TZ3R0 »

Also another fuel sounds great, it can be named something like " Transformer Output" and have a custom icon so it makes more sense

PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: An item fuel that uses energy stored on equipment grid to power vehicles

Post by PFQNiet »

The general idea will be to have a global list of vehicles. When equipment is placed, add the vehicle to the list. When removed, check there's not a second one placed and unregister the vehicle.

Then on nth tick you can loop through registered vehicles and "refuel" them using the battery energy.

If you go with a custom fuel item, make sure to set it to hidden.

N0TZ3R0
Inserter
Inserter
Posts: 22
Joined: Tue Dec 29, 2020 1:36 am
Contact:

Re: An item fuel that uses energy stored on equipment grid to power vehicles

Post by N0TZ3R0 »

Thanks a lot for the tips, I'll try to search and make it work

sherlockholmes221b
Burner Inserter
Burner Inserter
Posts: 10
Joined: Tue Dec 31, 2019 6:02 pm
Contact:

Re: An item fuel that uses energy stored on equipment grid to power vehicles

Post by sherlockholmes221b »

The following code (in control.lua) does pretty much what you said. Note, that this will work only if there is a player inside and it will be overpowered. You'd need to play a little more with the code to make things what you want them to be.

Code: Select all

-- We'll use this to store the driven entity
local driven_entity

-- When player gets inside or outside the vehicle, we acknowledge that
script.on_event(defines.events.on_player_driving_changed_state, function(e)
    if driven_entity then
        driven_entity = nil
    else
        driven_entity = e.entity
    end
end)

-- on every tick
script.on_event(defines.events.on_tick, function(e)
    -- First check if driven entity is not nil and if it has eq grid and fuel inventory
    if driven_entity and driven_entity.grid and driven_entity.get_fuel_inventory() then
        -- Then check if we can actually insert the fuel and if the energy available in grids is even there
        if driven_entity.get_fuel_inventory().can_insert({name="solid-fuel", count=1}) and driven_entity.grid.available_in_batteries > 0 then
            -- For every piece of equipment in grid we check if this is the right one...
            for _, eq in ipairs(driven_entity.grid.equipment) do
                if eq.name == "energy-shield-equipment" then
                    -- ... And if so, we insert the fuel
                    driven_entity.get_fuel_inventory().insert({name="solid-fuel", count=1})
                end
            end
        end
    end
end)
As I've said before, this is overpowered. I suggest you to play a little with it, for example create a fuel only when the right equipment is activated.

N0TZ3R0
Inserter
Inserter
Posts: 22
Joined: Tue Dec 29, 2020 1:36 am
Contact:

Re: An item fuel that uses energy stored on equipment grid to power vehicles

Post by N0TZ3R0 »

Thanks a lot sir, i'll try to improve it and and change it according to my mind, again, thanks buddy.

PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: An item fuel that uses energy stored on equipment grid to power vehicles

Post by PFQNiet »

That code may work in single player but will instantly fail in any multiplayer setup. It will also fail after save/load.

N0TZ3R0
Inserter
Inserter
Posts: 22
Joined: Tue Dec 29, 2020 1:36 am
Contact:

Re: An item fuel that uses energy stored on equipment grid to power vehicles

Post by N0TZ3R0 »

After a few tests it works "reverse" add the fuel when I leave the vehicle, also one thing that's missing and I can't add, I searched a lot but no lucky, is to drain energy from grid everytime the mod add fuel.

PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: An item fuel that uses energy stored on equipment grid to power vehicles

Post by PFQNiet »

I'm on holiday and my laptop can't run Factorio, so I can't test this, but the following should either work, or be easy enough to debug!

data.lua:

Code: Select all

data:extend{
  {
    type = "battery-equipment",
    name = "electric-transformer",
    categories = {"armor"}, -- adjust as appropriate so that it gets placed in vehicles only
    energy_source = {
      type = "electric",
      buffer_capacity = "1MJ",
      usage_priority = "secondary-input"
    },
    shape = {
      type = "full",
      width = 1,
      height = 2
    }, -- adjust size as desired
    sprite = table.deepcopy(data.raw['battery-equipment']['battery-equipment'].sprite) -- replace this with your own graphic
  },
  {
    type = "item",
    name = "electric-transformer",
    icon = "__base__/graphics/icons/battery-equipment.png", -- replace this with your own graphic
    icon_size = 64,
    icon_mipmaps = 4,
    subgroup = "equipment",
    order = "b[battery]-d[electric-transformer]",
    stack_size = 10,
    placed_as_equipment_result = "electric-transformer"
  },
  {
    type = "item",
    name = "electric-transformer-power",
    fuel_value = "1MJ",
    fuel_category = "chemical",
    flags = {"hidden", "hide-from-fuel-tooltip"}
  }
}
This should create the transformer equipment as a 1MJ "battery" that only takes power in, doesn't give it back out (a real battery would have usage_priority = "tertiary" to allow input/output). It creates an item that can be placed in equipment grids as the transformer, and also a custom "fuel" item that will be used by the transformer. You should add in a recipe for the transformer yourself. Otherwise you can "hack" one in with:

Code: Select all

/c game.player.insert("electric-transformer")
Aside from adding in your own graphics and/or tweaking the equipment size (personally I would make it 4x4 but you may want it to be a 1x1 add-on instead, that's entirely up to you) you should also take a look at what "categories" are offered by vehicle equipment grids in general, and change the one in my code to fit with that. After all, it wouldn't make sense to put it in player armour!

Once that's done, here's what your control.lua might look like:

Code: Select all

local event_handler = require("event_handler")
local script_data = {
  cars = {}
}
local MODULO = 60 -- update each vehicle once per 60 ticks
for i=0,MODULO-1 do script_data.cars[i] = {} end

local EQUIPMENT = "electric-transformer"
local FUEL = "electric-transformer-power"
local VEHICLE_TYPES = {"car", "artillery-wagon", "cargo-wagon", "fluid-wagon", "locomotive", "spider-vehicle"}
local function isVehicle(entity)
  for _,type in pairs(VEHICLE_TYPES) do
    if entity.type == type then return true end
  end
  return false
end

local function onBuilt(event)
  local entity = event.entity or event.created_entity
  if not (entity and entity.valid and isVehicle(entity)) then return end  
  script_data.cars[entity.unit_number % MODULO][entity.unit_number] = entity
end
local function onMined(event)
  local entity = event.entity
  if not (entity and entity.valid and isVehicle(entity)) then return end
  script_data.cars[entity.unit_number % MODULO][entity.unit_number] = nil
end

local function onTick(event)
  local process = script_data.cars[event.tick % MODULO]
  local CAPACITY = game.item_prototypes[FUEL].fuel_value
  for n,car in pairs(process) do
    if not (car and car.valid) then
      -- car was destroyed but somehow not unregistered, so clean it up here
      process[n] = nil
    elseif not car.grid then
      -- vehicle doesn't have a grid, skip it
    else
      -- check if car has electric-transformer in its grid and isn't burning "real" fuel
      if not car.burner.currently_burning then
        car.burner.currently_burning = FUEL
      end
      if car.burner.currently_burning.name == FUEL then
        local missing = CAPACITY - car.burner.remaining_burning_fuel
        -- loop through equipment in the car's grid, and transfer energy from transformers to the burner
        for _,equipment in pairs(car.grid.equipment) do
          if equipment.name == EQUIPMENT then
            local drain = math.min(equipment.energy, missing)
            equipment.energy = equipment.energy - drain
            car.burner.remaining_burning_fuel = car.burner.remaining_burning_fuel + drain
            missing = missing - drain
            if missing <= 0 then break end
          end
        end
      end
    end
  end
end

event_handler.add_lib{
  on_init = function()
    global.vehicles = global.vehicles or script_data.cars
    -- scan for any existing vehicles on any surfaces and start tracking them
    for _,surface in pairs(game.surfaces) do
      local vehicles = surface.find_entities_filtered{type = VEHICLE_TYPES}
      for _,entity in pairs(vehicles) do
        onBuilt{entity=entity}
      end
    end
  end,
  on_load = function()
    script_data.cars = global.vehicles or script_data.cars
  end,
  events = {
    [defines.events.on_tick] = onTick,
    [defines.events.on_built_entity] = onBuilt,
    [defines.events.on_robot_built_entity] = onBuilt,
    [defines.events.script_raised_built] = onBuilt,
    [defines.events.script_raised_revive] = onBuilt,
    [defines.events.on_player_mined_entity] = onMined,
    [defines.events.on_robot_mined_entity] = onMined,
    [defines.events.on_entity_died] = onMined,
    [defines.events.script_raised_destroy] = onMined
  }
}
Assuming I haven't messed anything up, this should save references to placed vehicles in the global table when they are built, and removes them when the vehicle is mined or destroyed.

These vehicles are spread out into "buckets" which are then iterated over and updated accordingly. If a vehicle isn't burning conventional fuel, it will check for a transformer in its grid and pull energy from that.

It should work with existing saves, as it will scan for and start tracking any vehicles with grids. It should even work if the vehicle didn't have a grid when placed but a mod was later added to give it a grid!

Give that a try and let me know how many errors you get! (I have long since lost all faith in any code I write and - as I said at the start - I have no means of testing this right now!)
Last edited by PFQNiet on Sun Jan 03, 2021 12:49 pm, edited 2 times in total.

N0TZ3R0
Inserter
Inserter
Posts: 22
Joined: Tue Dec 29, 2020 1:36 am
Contact:

Re: An item fuel that uses energy stored on equipment grid to power vehicles

Post by N0TZ3R0 »

So, I'm trying your code, put some icon ( was missing so game wont start ) but there's a new error, in control.lua line 10:

__ZElectricEngine__/control.lua:10: attempt to index global 'game' (a nil value)
stack traceback:
__ZElectricEngine__/control.lua:10: in main chunk

Line 10:

local CAPACITY = game.item_prototypes[FUEL].fuel_value

N0TZ3R0
Inserter
Inserter
Posts: 22
Joined: Tue Dec 29, 2020 1:36 am
Contact:

Re: An item fuel that uses energy stored on equipment grid to power vehicles

Post by N0TZ3R0 »

Also, another one:

O mod ZElectricEngine (0.0.1) causou um erro não recuperável.
Por favor, reporte este erro ao autor do mod.

Error while running event ZElectricEngine::on_tick (ID 0)
__ZElectricEngine__/control.lua:32: attempt to index field 'cars' (a nil value)
stack traceback:
__ZElectricEngine__/control.lua:32: in function 'handler'
__core__/lualib/event_handler.lua:47: in function <__core__/lualib/event_handler.lua:45>

PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: An item fuel that uses energy stored on equipment grid to power vehicles

Post by PFQNiet »

As expected XD

I have edited my post above with what should hopefully be fixes for both those errors. I thought you could access game.item_prototypes in global code but apparently not. And I seem to have made a mistake in how to save/load global data. Whoops!

N0TZ3R0
Inserter
Inserter
Posts: 22
Joined: Tue Dec 29, 2020 1:36 am
Contact:

Re: An item fuel that uses energy stored on equipment grid to power vehicles

Post by N0TZ3R0 »

So, conclusions, with the updated code, no error appeared, but the mechanic ( what should happen ) is not working, I equipped the car with the transformer + battery + Fusion reactor and nothing, I remove the flags from the fuel to test it and it's working fine( manual input of the fuel ). Any ideas ?

PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: An item fuel that uses energy stored on equipment grid to power vehicles

Post by PFQNiet »

That's gonna need some more debugging, but can you at least confirm that the transformer's energy buffer is filling as it should?

Post Reply

Return to “Ideas and Requests For Mods”