Help with converting laser equipment to use ammo

Place to get help with not working mods / modding interface.
Post Reply
AutoMcD
Fast Inserter
Fast Inserter
Posts: 214
Joined: Wed Apr 27, 2016 5:53 pm
Contact:

Help with converting laser equipment to use ammo

Post by AutoMcD »

I'm making some updated defense weapons for the modular armor.
thread about the idea:
viewtopic.php?f=16&t=25630

Just followed the tutorial, was able to copy the basic personal laser defense and modify it for the advanced version. Simple enough. Just have some graphics work to do.

The other thing I want to make is a version of the automatic defense gun which uses ammo instead of energy. I'm not sure how this will work with the GUI though. Is there any mechanism to drop ammo into equipment? I could settle for it just sucking ammo out of inventory, but it should prefer the piercing ammo if doing this automatically. I need a little bit of lua config help for this.

AutoMcD
Fast Inserter
Fast Inserter
Posts: 214
Joined: Wed Apr 27, 2016 5:53 pm
Contact:

Re: Help with converting laser equipment to use ammo

Post by AutoMcD »

This is what I have so far:
https://www.dropbox.com/s/yoy1zy9parif5 ... 1.zip?dl=0

rev expected will be 0.1.0, i changed the zip filename because this is preliminary.
Everything I've tried with converting the gun ammo resulted in errors so right now it's just the base damage with high ROF.

Image
Image

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Help with converting laser equipment to use ammo

Post by Adil »

The personal defense is hardcoded to draw electric energy. In my mod for combat trains I did use dummy defense equipments on dummy chars, chars didn't have any power source on them, so I could alter the energy stored in equipment to simulate ammo loading, and unloading. If your equipment is used on player this venue is closed.

If your equipment is fast-firing, substracting ammo during the trigger-created-entity event would probably murder the fps.

Maybe invisible non-colliding turret teleporting to player will be solution.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

AutoMcD
Fast Inserter
Fast Inserter
Posts: 214
Joined: Wed Apr 27, 2016 5:53 pm
Contact:

Re: Help with converting laser equipment to use ammo

Post by AutoMcD »

That's an interesting idea with the turret, but pulling something like that off is WAY over my head.

I have the graphics and all just sitting there ready to go.. want to take a crack at it? ;)

As for the energy draw, could give it a large reserve and insignificantly small drain per shot and just leave it if that's how it's going to be. Chalk it up to the ammo feeder.
I'm hoping if it can sound like a gun and use ammo, then the rest is easy to look past. Did you know tracer ammo looks just like lasers? :lol:

Also it would only have to subtract ammo once for every 10 shots, not sure how bad of an FPS hit that would really be.. I doubt it would be noticeable.

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Help with converting laser equipment to use ammo

Post by Adil »

I kind of forgot you could limit the intake.
With that, it's quite an easy code.
Couldn't resist the urge. Assembled it from other my mods. It got a bit more complicated though.

Locale for all the different bullets from bob's, yoki and such is on you.
Attachments
Advanced_Personal_Defense_0.1.0.zip
(69.38 KiB) Downloaded 105 times
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

AutoMcD
Fast Inserter
Fast Inserter
Posts: 214
Joined: Wed Apr 27, 2016 5:53 pm
Contact:

Re: Help with converting laser equipment to use ammo

Post by AutoMcD »

oh wow, thanks!
Fixed a few minor burps.. working well. So it "recharges" from the ammo in inventory, can hold a full stack. Very clever.

I can't seem to find the damage tweaker, how does that work? Is it calculated from ammo somehow? If it's constant then maybe piercing ammo should "refill" it more efficiently than normal ammo.

Also I see that it becomes unloaded if put into inventory! something I don't think we can get around with this technique.

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Help with converting laser equipment to use ammo

Post by Adil »

Yes, damage is defined by ammo during creation in data.lua.
I've written functions to generate item and equipment for each bullet type, the equipment generated takes ammo_type field of bullet.
Then in control.lua it swaps equipments related to same category if only other ammo is present.
You can alter the damage_modifier in attack_parameters passed to generation functions or edit generated prototypes before putting them in data.raw.

Thinking of which, this generation should probably be moved to data-updates.lua rather than data.lua.

Yes, the gun loses its energy when you move it in grid just as normal laser defense does. Surely it is possible to slap together some verification function even now. But I'd thought it'll wait till the june_the_1st, there's new event out there which will make the matter trivial to handle. Simplest way will be reading the energy of equipment during this event and dump the corresponding amount of ammo into inventory.

That said, the 0.13 has other events, which will allow for a more efficient on_tick updates than there are now. With on_player_armor_inventory_changed and on_player_placed_equipment it'll be possible to maintain a per-player table of equipments that need ammo, instead of current iteration over all equipments placed in armor.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

AutoMcD
Fast Inserter
Fast Inserter
Posts: 214
Joined: Wed Apr 27, 2016 5:53 pm
Contact:

Re: Help with converting laser equipment to use ammo

Post by AutoMcD »

I see. I'll wait for the big update, sounds like this will get easier.
It works ok enough for now. :)

AutoMcD
Fast Inserter
Fast Inserter
Posts: 214
Joined: Wed Apr 27, 2016 5:53 pm
Contact:

Re: Help with converting laser equipment to use ammo

Post by AutoMcD »

is there a way to check what tech is researched and modify values based upon that? such as damage, ROF to reflect turret upgrades.

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Help with converting laser equipment to use ammo

Post by Adil »

It inherits category from ammo. Or is it type?
Whatever.
Something from generation makes it affected by some upgrades and I believe it's bullet upgrades.
Yes, I believe it's
ammo_category = "bullet"
If you're determined to have them influenced by gun-turret upgrades, you can rewrite the generation, so that they use different ammo_category, and then add
{
type = "ammo-damage",
ammo_category = "bullet",--you category here
modifier = "0.1"
}
to any research you deem relevant.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

AutoMcD
Fast Inserter
Fast Inserter
Posts: 214
Joined: Wed Apr 27, 2016 5:53 pm
Contact:

Re: Help with converting laser equipment to use ammo

Post by AutoMcD »

bump.

So I'm trying to get this to work with the .13 update.
Aside from the obvious of adding the version line in info.json and deleting the "require defines" line, there's the slightly less obvious renaming of the basic-laser-defense-equipment to personal-laser-defense-equipment.
After that I started getting some odd errors in the control.lua:

Code: Select all

magazine_size=game.get_item_prototype(ammo).magazine_size,
I changed it to =10 just to get past it, next error was line 39, didn't show until the game map was started. Got a null value error..

Code: Select all

local armor=p.get_inventory(defines.inventory.player_armor)[1]
This area is a bit over my head. According to the mod changes thread I believe the relevant line is this:
- Changed game.players, game.surfaces, game.entity_prototypes, game.item_prototypes, game.fluid_prototypes, force.recipes, force,technologies
to use custom access + iterator objects for improved performance.
Would someone be able to explain this better? Adil's code is just a little too complex for me to understand how to fix it.
What Adil has done here is over my head and I'm afraid I'm unable to update it without help.

Here is the full file:

Code: Select all

energy_per_shot=1000 --constant related to amount of energy used in a single shot
check_interval=30 --interval between checks of players

--temporary workaround, every bullet type should be listed here, to provide interchangeability
--of items in same category
ammo_category={--cannot get ammocategory from anywhere
    ['basic-bullet-magazine']='bullet',
    ['piercing-bullet-magazine']='bullet',
}
category_ammo={}
for k,v in pairs(ammo_category) do
    category_ammo[v]=category_ammo[v] or {}
    table.insert(category_ammo[v],k)
end

function init()
    --creation of information tables about generated entities
    global.equip_data={}
    global.ammo_equip={}
    for k,v in pairs(game.item_prototypes) do
        if string.find(k,"automated%-equipment%-#") then
        --equipment is named as 'automated-equipment-#' ..ammo.name.. '#'
            local ammo
            _,_,ammo=string.find(k,"#(.+)#")
            global.equip_data[k]={
                ammo=ammo,
                magazine_size=game.get_item_prototype(ammo).magazine_size,
                }
            global.ammo_equip[ammo]=k
        end
    end
    --value for ontick counter
    global.next=1
end

function check_player(p)
    if not p.valid then return end
    local armor=p.get_inventory(defines.inventory.player_armor)[1]
    --check whether player has armorgrid
    if not (armor and armor.valid ) then print('fug') return end
    if not (armor and armor.valid_for_read and armor.has_grid) then return end
    local inv=p.get_inventory(defines.inventory.player_main)
    local equips=global.equip_data
    local per_shot=energy_per_shot
    local grid=armor.grid
    --search for relevant equipment in grid
    for _,e in pairs(grid.equipment) do 
        if equips[e.name] then
            local eq=equips[e.name]
            local menergy = eq.magazine_size*per_shot
            --is reload required
            if e.max_energy-e.energy> menergy then
                --is there ammo
                if inv.get_item_count(eq.ammo)>10 then
                    inv.remove{name=eq.ammo,count=1}
                    e.energy=e.energy+ menergy
                --is ammo critically low
                elseif e.energy<per_shot then
                    --is there other ammo of same category
                    for _,ammo in pairs(category_ammo[ammo_category[eq.ammo]]) do
                        if inv.get_item_count(ammo)>10 then
                            --swap to equivalent equipment
                            local pos=e.position
                            grid.take{equipment=e}
                            local e=grid.put{name=global.ammo_equip[ammo],position=pos}
                            inv.remove{name=ammo,count=1}
                            e.energy=e.energy+ equips[e.name].magazine_size*per_shot
                            break
                        end
                    end
                end
            end
        end
    end
end

script.on_init(function() init() end)
script.on_configuration_changed(function() init() end)

script.on_event(defines.events.on_tick, function(e)
    local int=check_interval
    local pls=game.players
    for i=global.next,#pls,int do
        check_player(pls[i])
    end
    global.next= (global.next % int) +1
end)
--script.on_event(defines.events.on_player_created,function(e)
    --local p=game.players[e.player_index]
    --p.insert( 'automated-equipment-#' ..'basic-bullet-magazine'.. '#')
    --p.insert('power-armor')
    --p.insert("fusion-reactor-equipment")
--end)

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Help with converting laser equipment to use ammo

Post by Adil »

I've finished a mod updater here. I've seen it work on this mod. It is here.
AutoMcD wrote:This area is a bit over my head. According to the mod changes thread I believe the relevant line is this:
- Changed game.players, game.surfaces, game.entity_prototypes, game.item_prototypes, game.fluid_prototypes, force.recipes, force,technologies
to use custom access + iterator objects for improved performance.
Would someone be able to explain this better?
It meant that where `game.get_item_prototype(ammo)` was used now `game.item_prototypes[ammo]` must be. This happens pretty much for every name that begins with 'set_' or 'get_'.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

AutoMcD
Fast Inserter
Fast Inserter
Posts: 214
Joined: Wed Apr 27, 2016 5:53 pm
Contact:

Re: Help with converting laser equipment to use ammo

Post by AutoMcD »

That is excellent!

..but I still get error on line 40 after using it.
Image

It doesn't happen when game first loads, you have to start a new map.

The offending function:
Image

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Help with converting laser equipment to use ammo

Post by Adil »

You are launching it in sandbox, where no player character exists.
Line 39 should probably be changed to:

Code: Select all

if not p.valid or not p.character then return end
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

AutoMcD
Fast Inserter
Fast Inserter
Posts: 214
Joined: Wed Apr 27, 2016 5:53 pm
Contact:

Re: Help with converting laser equipment to use ammo

Post by AutoMcD »

OMG you are totally right. And good fix, thanks!

AutoMcD
Fast Inserter
Fast Inserter
Posts: 214
Joined: Wed Apr 27, 2016 5:53 pm
Contact:

Re: Help with converting laser equipment to use ammo

Post by AutoMcD »

loading multiplayer save game ends with crash

126.735 Error MainLoop.cpp:373: Exception at tick 899582: Error while running the event handler: Advanced_Personal_Defense/control.lua:39: User isn't connected; can't read character


Any ideas?

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Help with converting laser equipment to use ammo

Post by Adil »

Code: Select all

if not p.valid or not p.connected or not p.character then return end
This is getting uglier and uglier, I had expected them to handle the absent players characters more gracefully.

I don't have much free and productive time as of recently, if rewrite of mod to use new event is going to happen, it'll happen by someone else's doing.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

AutoMcD
Fast Inserter
Fast Inserter
Posts: 214
Joined: Wed Apr 27, 2016 5:53 pm
Contact:

Re: Help with converting laser equipment to use ammo

Post by AutoMcD »

Alright. I do appreciate the time you've invested, thank you.

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Help with converting laser equipment to use ammo

Post by Adil »

Luckily, I've finally had some free time.
I've done the new version of code, now it returns ammo on equipment shuffle.
It now works with characters' references instead of players'. Hopefully, in multiplayer players will get same character reference for multiple session, I hadn't the opportunity to test it.

Unfortunately, the I'm bad with factoring out separate simple functions, when they're only ever run together, so the code might be not the best read.
Attachments
Advanced_Personal_Defense_0.1.1.zip
(69.87 KiB) Downloaded 67 times
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

AutoMcD
Fast Inserter
Fast Inserter
Posts: 214
Joined: Wed Apr 27, 2016 5:53 pm
Contact:

Re: Help with converting laser equipment to use ammo

Post by AutoMcD »

Excellent! Tomorrow i should have time to test it out and get the update pushed. Thanks!

Post Reply

Return to “Modding help”