Page 1 of 1

LuaEquipmentGrid reduce battery energy evenly

Posted: Wed May 03, 2017 2:29 am
by sparr
I would like to consume some of the power in the player's batteries. The built-in equipment all draws evenly from the batteries. I can't find a way to do that. I am loathe to loop through every LuaEquipment in the grid and reduce the energy one at a time, for both speed and unevenness reasons.

Re: LuaEquipmentGrid reduce battery energy evenly

Posted: Wed May 03, 2017 3:19 am
by Ranakastrasz
Consuming energy directly? No vanilla equipment does that, it draws from a consumer type equipment.


That said, I think you can get the total power stored from the grid and then loop through all of the modules to remove the proportional fraction



That said, this would be somewhat useful.

Re: LuaEquipmentGrid reduce battery energy evenly

Posted: Wed May 03, 2017 3:25 am
by sparr
Ranakastrasz wrote:Consuming energy directly? No vanilla equipment does that, it draws from a consumer type equipment.
That's the behavior I want to mimic.

Ranakastrasz wrote:That said, I think you can get the total power stored from the grid and then loop through all of the modules to remove the proportional fraction
Yeah, I went with an even lazier option and just removed from the first batteries for now.

Re: LuaEquipmentGrid reduce battery energy evenly

Posted: Wed May 03, 2017 2:05 pm
by Ranakastrasz
If you want to mimic that behavior, use a dummy consumer and get power from it alone.

Re: LuaEquipmentGrid reduce battery energy evenly

Posted: Wed May 03, 2017 3:05 pm
by sparr
Do you have an example of a mod with a dummy consumer? For equipment, not entities.

Re: LuaEquipmentGrid reduce battery energy evenly

Posted: Wed May 03, 2017 5:45 pm
by Ranakastrasz
Well, not directly, but you could use one of several things.

The main issue is that you probably can't make the tooltip show the power consumption.

Laser turret Equipment. Set range to zero, power consumption to *Lots*, cooldown to *Lots*, etc.
Battery Equipment. Set priority to... Uhm, Primary? I think?. Whatever shields and legs use anyway.
Legs. Set speed boost to 0, power consumption to zero. Otherwise it would use energy if you move, or give you +speed.
Discharge Equipment. Actually, given that that requires an action to activate, it might be the best case. Just don't add a trigger item thinggy.
Night vision, might have unwanted behavior, don't use.

Shields probably have unwanted behavior, but I am unsure. You might be able to set capacity to zero, and then still have it display a power consumption amount. That is probably the best choice.

Re: LuaEquipmentGrid reduce battery energy evenly

Posted: Wed May 03, 2017 5:53 pm
by Nexela
Nanobots in the scripts/armormods.lua has some examples that might interest you.

Re: LuaEquipmentGrid reduce battery energy evenly

Posted: Wed May 03, 2017 6:34 pm
by sparr
I don't understand how I would trigger any of those entity types to use power when I want them to.

Re: LuaEquipmentGrid reduce battery energy evenly

Posted: Wed May 03, 2017 7:18 pm
by Ranakastrasz
....

OnTick, get the equipment grid. Loop through till you find equipment of matching type. Check for enough power for that tick. (60 ticks/second).
Remove that much power if it is available, do your effect. Else, do nothing.

Re: LuaEquipmentGrid reduce battery energy evenly

Posted: Wed May 03, 2017 7:51 pm
by sparr
But then the equipment would hold energy when it's not being used, like a roboport or a battery. I don't want that. I want it to just consume electricity directly from the batteries, like the laser defense does.

Re: LuaEquipmentGrid reduce battery energy evenly

Posted: Wed May 03, 2017 10:21 pm
by Ranakastrasz
The laser defence does not consume energy directly from the batteries, it stores energy until it has enough energy and a target, then discharges it all for a laser blast. It holds enough for like 3 shots, or a single second of firing. As such, just leave the capacity very low.

Even the frikkin Night vision goggles store enough power for like 12 seconds of operation.

Code: Select all

    energy_source =
    {
      type = "electric",
      buffer_capacity = "120kJ",
      input_flow_limit = "240kW",
      usage_priority = "primary-input"
    },
    energy_input = "10kW",
Night vision consumes 10 kW, or 10 kJ/seconds, and stores 120 kJ. It fully charges in half a second as well.

Code: Select all

 energy_source =
    {
      type = "electric",
      usage_priority = "secondary-input",
      buffer_capacity = "220kJ"
    },
    attack_parameters =
    {
      type = "projectile",
      ammo_category = "electric",
      cooldown = 20,
      damage_modifier = 15,
      projectile_center = {0, 0},
      projectile_creation_distance = 0.6,
      range = 15,
      sound = make_laser_sounds(),
      ammo_type =
      {
        type = "projectile",
        category = "electric",
        energy_consumption = "200kJ",
        projectile = "laser",
        speed = 1,
        action =
        {
          {
            type = "direct",
            action_delivery =
            {
              {
                type = "projectile",
                projectile = "laser",
                starting_speed = 0.28
              }
            }
          }
        }
      }
    },
The Laser turret consumes 200 kJ per shot, and holds 220 kJ. It charges at a rate of.... Yes.
Apperently it has no throttle on how fast it charges, presumably to support attack speed upgrades.