# SPDX-FileCopyrightText: 2023 DrSmugleaf <DrSmugleaf@users.noreply.github.com>
# SPDX-FileCopyrightText: 2023 Morb <14136326+Morb0@users.noreply.github.com>
# SPDX-FileCopyrightText: 2023 chromiumboy <50505512+chromiumboy@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 12rabbits <53499656+12rabbits@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 Alzore <140123969+Blackern5000@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 Brandon Hu <103440971+Brandon-Huu@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 Cojoke <83733158+Cojoke-dot@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 Dimastra <65184747+Dimastra@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 Dimastra <dimastra@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 Ed <96445749+TheShuEd@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 Emisse <99158783+Emisse@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 Eoin Mcloughlin <helloworld@eoinrul.es>
# SPDX-FileCopyrightText: 2024 Flareguy <78941145+Flareguy@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 IProduceWidgets <107586145+IProduceWidgets@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 JIPDawg <51352440+JIPDawg@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 JIPDawg <JIPDawg93@gmail.com>
# SPDX-FileCopyrightText: 2024 Julian Giebel <juliangiebel@live.de>
# SPDX-FileCopyrightText: 2024 Kara <lunarautomaton6@gmail.com>
# SPDX-FileCopyrightText: 2024 Mervill <mervills.email@gmail.com>
# SPDX-FileCopyrightText: 2024 Moomoobeef <62638182+Moomoobeef@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 Nemanja <98561806+EmoGarbage404@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 PJBot <pieterjan.briers+bot@gmail.com>
# SPDX-FileCopyrightText: 2024 Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
# SPDX-FileCopyrightText: 2024 Pieter-Jan Briers <pieterjan.briers@gmail.com>
# SPDX-FileCopyrightText: 2024 Piras314 <p1r4s@proton.me>
# SPDX-FileCopyrightText: 2024 PopGamer46 <yt1popgamer@gmail.com>
# SPDX-FileCopyrightText: 2024 PursuitInAshes <pursuitinashes@gmail.com>
# SPDX-FileCopyrightText: 2024 QueerNB <176353696+QueerNB@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 Saphire Lattice <lattice@saphi.re>
# SPDX-FileCopyrightText: 2024 ShadowCommander <10494922+ShadowCommander@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 Simon <63975668+Simyon264@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 SpeltIncorrectyl <66873282+SpeltIncorrectyl@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 Spessmann <156740760+Spessmann@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 Tayrtahn <tayrtahn@gmail.com>
# SPDX-FileCopyrightText: 2024 Thomas <87614336+Aeshus@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 Winkarst <74284083+Winkarst-cpu@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 deltanedas <39013340+deltanedas@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 deltanedas <@deltanedas:kde.org>
# SPDX-FileCopyrightText: 2024 eoineoineoin <github@eoinrul.es>
# SPDX-FileCopyrightText: 2024 github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 lzk <124214523+lzk228@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 slarticodefast <161409025+slarticodefast@users.noreply.github.com>
# SPDX-FileCopyrightText: 2024 stellar-novas <stellar_novas@riseup.net>
# SPDX-FileCopyrightText: 2024 themias <89101928+themias@users.noreply.github.com>
# SPDX-FileCopyrightText: 2025 Aiden <28298836+Aidenkrz@users.noreply.github.com>
# SPDX-FileCopyrightText: 2025 ArtisticRoomba <145879011+ArtisticRoomba@users.noreply.github.com>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# You can use this Desmos sheet to calculate fuel burn rate values:
# https://www.desmos.com/calculator/qcektq5dqs
#

- type: entity
  abstract: true
  id: PortableGeneratorBase
  parent: [ BaseMachine, SmallConstructibleMachine, StructureWheeled]
  components:
    # Basic properties
    - type: Transform
      anchored: False
    - type: Physics
      bodyType: Dynamic
    - type: StaticPrice
      price: 500
    - type: AmbientSound
      range: 5
      volume: -5
      sound:
        path: /Audio/Ambience/Objects/engine_hum.ogg
      enabled: false
    - type: Fixtures
      fixtures:
        fix1:
          shape:
            !type:PhysShapeAabb
            bounds: "-0.40,-0.40,0.40,0.40"
          # Despite the heavy weight, it has wheels, so it's still fairly portable.
          density: 155
          mask:
            - MachineMask
          layer:
            - MachineLayer

    # Visuals
    - type: Appearance
    - type: Sprite
      sprite: Structures/Power/Generation/portable_generator.rsi
      noRot: true

    # Construction, interaction
    - type: WiresPanel
    - type: UserInterface
      interfaces:
        enum.GeneratorComponentUiKey.Key:
          type: PortableGeneratorBoundUserInterface
    - type: ActivatableUI
      key: enum.GeneratorComponentUiKey.Key
    - type: Electrified
      onHandInteract: false
      onInteractUsing: false
      onBump: false
      requirePower: true
      highVoltageNode: output

    - type: Destructible
      thresholds:
        - trigger:
            !type:DamageTrigger
            damage: 200
          behaviors:
            - !type:DoActsBehavior
              acts: [ "Destruction" ]
        - trigger:
            !type:DamageTrigger
            damage: 100
          behaviors:
            - !type:PlaySoundBehavior
              sound:
                collection: MetalBreak
            - !type:ChangeConstructionNodeBehavior
              node: machineFrame
            - !type:DoActsBehavior
              acts: ["Destruction"]
    - type: GuideHelp
      guides:
      - PortableGenerator
      - Power

    # Core functionality
    - type: PortableGenerator
      startSoundEmpty: { collection: GeneratorTugEmpty }
      startSound: { collection: GeneratorTug }
    - type: FuelGenerator
    - type: PowerSupplier
      supplyRate: 3000
      supplyRampRate: 500
      supplyRampTolerance: 500
      enabled: false
    - type: DeviceLinkSink
      ports:
      - On
      - Off
      - Toggle
    - type: GeneratorSignalControl
    - type: DeviceNetwork
      deviceNetId: Wireless
      receiveFrequencyId: BasicDevice
    - type: WirelessNetworkConnection
      range: 200

- type: entity
  abstract: true
  parent: PortableGeneratorBase
  id: PortableGeneratorSwitchableBase
  components:
  - type: PowerSwitchable
    examineText: power-switchable-generator-examine
    switchText: power-switchable-generator-switched
    cables:
    - voltage: HV
      node: output_hv
    - voltage: MV
      node: output_mv
  - type: UseDelay
    delay: 1
  - type: NodeContainer
    examinable: true
    nodes:
      output_hv:
        !type:CableDeviceNode
        nodeGroupID: HVPower
      output_mv:
        !type:CableDeviceNode
        nodeGroupID: MVPower
        enabled: false

- type: entity
  name: P.A.C.M.A.N.-type portable generator
  description: |-
    A flexible backup generator for powering a variety of equipment.
    Runs off solid plasma sheets and is rated for up to 30 kW.
  parent: PortableGeneratorSwitchableBase
  id: PortableGeneratorPacman
  suffix: Plasma, 30 kW
  components:
    - type: Sprite
      layers:
        - state: portgen0
          map: [ "enum.GeneratorVisualLayers.Body" ]
        - state: portgen_on_unlit
          map: [ "enum.GeneratorVisualLayers.Unlit" ]
          shader: unshaded
          visible: false
    - type: GenericVisualizer
      visuals:
        enum.GeneratorVisuals.Running:
          enum.GeneratorVisualLayers.Body:
            True: { state: portgen0on }
            False: { state: portgen0 }
          enum.GeneratorVisualLayers.Unlit:
            True: { visible: true }
            False: { visible: false }

    - type: Machine
      board: PortableGeneratorPacmanMachineCircuitboard
    - type: FuelGenerator
      minTargetPower: 5000
      maxTargetPower: 30000
      targetPower: 30000
      optimalPower: 30000
      # 15 minutes at max output
      optimalBurnRate: 0.0333333
      # a decent curve that goes up to about an hour at 5 kW.
      fuelEfficiencyConstant: 0.75
    - type: SolidFuelGeneratorAdapter
      fuelMaterial: Plasma
      multiplier: 0.01
    - type: MaterialStorage
      storageLimit: 3000
      materialWhiteList: [Plasma]
    - type: PortableGenerator
      startChance: 0.8
    - type: GeneratorExhaustGas
      gasType: CarbonDioxide
      # 2 moles of gas for every sheet of plasma.
      moleRatio: 2
    - type: PowerMonitoringDevice
      group: Generator
      loadNodes:
        - output_hv
        - output_mv
      sprite: Structures/Power/Generation/portable_generator.rsi
      state: portgen0
    - type: PowerSupplier
      supplyRampRate: 5000
      supplyRampTolerance: 1500

- type: entity
  name: S.U.P.E.R.P.A.C.M.A.N.-type portable generator
  description: |-
    An advanced generator for powering departments.
    Runs off uranium sheets and is rated for up to 50 kW.
  parent: PortableGeneratorSwitchableBase
  id: PortableGeneratorSuperPacman
  suffix: Uranium, 50 kW
  components:
    - type: Sprite
      layers:
        - state: portgen1
          map: [ "enum.GeneratorVisualLayers.Body" ]
        - state: portgen_on_unlit
          map: [ "enum.GeneratorVisualLayers.Unlit" ]
          shader: unshaded
          visible: false
    - type: GenericVisualizer
      visuals:
        enum.GeneratorVisuals.Running:
          enum.GeneratorVisualLayers.Body:
            True: { state: portgen1on }
            False: { state: portgen1 }
          enum.GeneratorVisualLayers.Unlit:
            True: { visible: true }
            False: { visible: false }

    - type: Machine
      board: PortableGeneratorSuperPacmanMachineCircuitboard
    - type: FuelGenerator
      minTargetPower: 10000
      maxTargetPower: 50000
      targetPower: 50000
      optimalPower: 50000
      # 30 minutes at full power
      optimalBurnRate: 0.016666666
      # Barely save any fuel from reducing power output
      fuelEfficiencyConstant: 0.1
    - type: SolidFuelGeneratorAdapter
      fuelMaterial: Uranium
      multiplier: 0.01
    - type: MaterialStorage
      storageLimit: 3000
      materialWhiteList: [Uranium]
    - type: PortableGenerator
    - type: PowerMonitoringDevice
      group: Generator
      loadNodes:
        - output_hv
        - output_mv
      sprite: Structures/Power/Generation/portable_generator.rsi
      state: portgen1
    - type: PowerSupplier
      supplyRampRate: 7500
      supplyRampTolerance: 2500

- type: entity
  name: J.R.P.A.C.M.A.N.-type portable generator
  description: |-
    A small generator capable of powering individual rooms, in case of emergencies.
    Runs off welding fuel and is rated for up to 8 kW.
    Rated ages 3 and up.
  parent: PortableGeneratorBase
  id: PortableGeneratorJrPacman
  suffix: Welding Fuel, 8 kW
  components:
    - type: AmbientSound
      range: 4
      volume: -8

    - type: Fixtures
      fixtures:
        fix1:
          shape:
            !type:PhysShapeAabb
            bounds: "-0.30,-0.30,0.30,0.30"
          density: 80
          mask:
            - MachineMask
          layer:
            - MachineLayer

    - type: Sprite
      layers:
        - state: portgen3
          map: [ "enum.GeneratorVisualLayers.Body" ]
        - state: portgen3on_unlit
          map: [ "enum.GeneratorVisualLayers.Unlit" ]
          shader: unshaded
          visible: false
    - type: GenericVisualizer
      visuals:
        enum.GeneratorVisuals.Running:
          enum.GeneratorVisualLayers.Body:
            True: { state: portgen3on }
            False: { state: portgen3 }
          enum.GeneratorVisualLayers.Unlit:
            True: { visible: true }
            False: { visible: false }

    - type: Machine
      board: PortableGeneratorJrPacmanMachineCircuitboard
    - type: FuelGenerator
      targetPower: 5000
      minTargetPower: 4000
      optimalPower: 8000
      maxTargetPower: 8000
      # 7.5 minutes at full tank.
      optimalBurnRate: 0.11111111
      # Shallow curve that allows you to just barely eek out 12 minutes at lowest.
      fuelEfficiencyConstant: 0.3
    - type: ChemicalFuelGeneratorAdapter
      solution: tank
      reagents:
        WeldingFuel: 1
    - type: SolutionContainerManager
      solutions:
        tank:
          maxVol: 50
    - type: RefillableSolution
      solution: tank
    - type: PortableGenerator
      # Unreliable bugger
      startChance: 0.5
    - type: NodeContainer
      examinable: true
      nodes:
        output:
          !type:CableDeviceNode
          nodeGroupID: Apc
    - type: PowerMonitoringDevice
      group: Generator
      loadNode: output
      sprite: Structures/Power/Generation/portable_generator.rsi
      state: portgen3
    - type: PowerSupplier
      # No ramping needed on this bugger.
      voltage: Apc
      supplyRampTolerance: 2000
    - type: GeneratorExhaustGas
      gasType: CarbonDioxide
      # Full tank is 25 moles of gas
      moleRatio: 0.5
    - type: Explosive
      explosionType: Default
      tileBreakScale: 0
    - type: Destructible
      thresholds:
        - trigger:
            !type:DamageTrigger
            damage: 200
          behaviors:
            - !type:DoActsBehavior
              acts: [ "Destruction" ]
        - trigger:
            !type:DamageTrigger
            damage: 100
          behaviors:
            - !type:SpillBehavior
              solution: tank
            - !type:PlaySoundBehavior
              sound:
                collection: MetalBreak
            - !type:ChangeConstructionNodeBehavior
              node: machineFrame
            - !type:DoActsBehavior
              acts: ["Destruction"]
        - trigger:
            !type:DamageTypeTrigger
            damageType: Piercing
            damage: 75
          behaviors:
            - !type:SolutionExplosionBehavior
              solution: tank