Page 1 of 2

[MOD 0.12.16] Displaying range of turrets [v1.1.2]

Posted: Wed Aug 12, 2015 12:06 pm
by kds71
I finally updated my turret range probe mod to 0.12.x - sorry for the delay :)

Another mod inspired by a reddit post.

This mod adds an item that allows you to check range of a turret.
Since version 1.1.1 it works with any turret in a game (including modded turrets). Config file is not needed anymore and it was removed (thanks to Adil for the tip!).

Features:
  • adds a new technology called "turrets range probing" (requires electronics and turrets)
  • adds a new item, "turret probe" with recipe unlocked by this technology
How to use:
  • click on a turret with a turret probe in your hand to see a range of this turret
  • click elsewhere (still with a turret probe in your hand) to remove displayed range from the screen
  • you may click on several turrets in a row to see their range together (to check for overlapping etc.)
Screenshot
LIcense: Public Domain
Changelog
Downloads:
For Factorio 0.12.16 download v1.1.2 of this mod:
turrets-range_1.1.2.zip
(9.82 KiB) Downloaded 2978 times
For older versions of Factorio download v1.1.1:
turrets-range_1.1.1.zip
(9.82 KiB) Downloaded 1048 times

Re: [MOD 0.12.1] Displaying range of turrets [v1.1.0]

Posted: Wed Aug 12, 2015 12:52 pm
by DOSorDIE
Great Mod ... but how can i disable/enabe ingame, but i dont need this all the time.

Re: [MOD 0.12.1] Displaying range of turrets [v1.1.0]

Posted: Thu Aug 13, 2015 5:31 am
by waduk
So glad you update this, saw you update one of your mod in reddit post.

Re: [MOD 0.12.1] Displaying range of turrets [v1.1.0]

Posted: Thu Aug 13, 2015 5:52 am
by toudi
Awesome, this should be in base game!

Re: [MOD 0.12.1] Displaying range of turrets [v1.1.0]

Posted: Thu Aug 13, 2015 8:23 am
by Adil
I humbly suggest the following code to make it work with any turret.

Code: Select all

game.onload(function()
    turret_ranges={};
    for name,p in pairs(game.entity_prototypes) do 
        if p.type=='electric-turret' or p.type=='ammo-turret' or p.type=='turret' then 
            turret_ranges[name]= p.turret_range
        end; 
    end
end)

Re: [MOD 0.12.1] Displaying range of turrets [v1.1.0]

Posted: Thu Aug 13, 2015 8:42 am
by kds71
Wait, so there is a turret_range property that allows you to read the range of a turret? I couldn't find it anywhere, I have to check it out! Thanks for the tip.

Edit: Awesome, it worked! Uploading new version of the mod right now. I removed "turrets_range" variable, now my code looks like that:

Code: Select all

            if entity[1].type == "electric-turret" or entity[1].type == "ammo-turret" or entity[1].type == "turret" then
                found = true
                if entity[1].type == "electric-turret" then
                    turret_type = "laser"
                else
                    turret_type = "gun"
                end
                draw_turret_range(turret_type, entity[1].position.x, entity[1].position.y, entity[1].prototype.turret_range)
            end

Re: [MOD 0.12.1] Displaying range of turrets [v1.1.1]

Posted: Thu Aug 13, 2015 5:34 pm
by orzelek
The turret_range is a new addition in 0.12. And it's great for mod like this :D

Re: [MOD 0.12.1] Displaying range of turrets [v1.1.1]

Posted: Mon Aug 24, 2015 4:48 pm
by Talguy
Why not program it like this?

Code: Select all

        entity = game.get_surface(1).find_entities{{ pos.x, pos.y }, { pos.x, pos.y }}
        --if #entity > 0 then
            --if entity[1].type == "electric-turret" or entity[1].type == "ammo-turret" or entity[1].type == "turret" then
            if  #entity > 0 and entity[1].prototype.turret_range and entity[1].prototype.turret_range > 0 then
                --found = true
                if entity[1].type == "electric-turret" then
                    turret_type = "laser"
                else
                    turret_type = "gun"
                end
                draw_turret_range(turret_type, entity[1].position.x, entity[1].position.y, entity[1].prototype.turret_range)
            --end
        --end
		else
        --if not found then
            for i = 1, #global.turret_range_entities do
                global.turret_range_entities[i].destroy()
            end
            global.turret_range_entities = {}
        end
That way, other mods like Bobs mod (sniper turret) will be supported, because I think checking turret_range should be sufficient. Turret range should be a debug tool anyway.

Not sure if prototype.turret_range is auto set to 0 through a metatable __index, if so then prototype.turret_range > 0 is sufficient (otherwise attempt to compare number with nil)

Re: [MOD 0.12.1] Displaying range of turrets [v1.1.1]

Posted: Mon Aug 24, 2015 5:36 pm
by Peter34
Thanks for this mod!

Re: [MOD 0.12.1] Displaying range of turrets [v1.1.1]

Posted: Tue Sep 22, 2015 3:42 am
by WhiteHat101
Hi, I love your mod concept, but I wasn't thrilled with the activation mechanism, so I've been trying different methods. I'm pretty happy with my mods to your mod, but I'll often get some client lag when it renders or un-renders a turret's range (It's creating/destroying ~100 entities per turret, I can't really blame the game for some stutter). My code renders nearby turrets; the lag becomes noticeable when I run down a wall of turrets, or past a cluster of turrets (easily, thousands of entities are created and then destroyed in a handful of seconds).

Might there be a way to render the range indicator with a single entity per turret?

Maybe a large custom sprite? Maybe something with the range indicator from power poles? Maybe we could tap into the range display used by grenades and bot capsules?
wip control.lua

Re: [MOD 0.12.1] Displaying range of turrets [v1.1.1]

Posted: Tue Sep 22, 2015 11:36 am
by Peter34
One thing I'm very concerned about is multiplayer compatability. It's all well and good to have a mod that works in SP, but coop MP is ultimately a thousand times more fun, and if a mod creates too much lag, it'll make MP impossible.

Re: [MOD 0.12.1] Displaying range of turrets [v1.1.1]

Posted: Sun Nov 01, 2015 1:46 am
by kds71
I just uploaded v1.1.2 of this mod, it is now compatible with Factorio v0.12.16. Sorry for the delay.
WhiteHat101 wrote:Might there be a way to render the range indicator with a single entity per turret?
The only thing that comes to my mind is to create multiple entity types - one for each range. The problem with this approach is that I want to support modded turrets as well, so basically it would require to create hundreds of entity types to make sure that all possible ranges are covered. I'll think about it.

Re: [MOD 0.12.16] Displaying range of turrets [v1.1.2]

Posted: Sun Jan 31, 2016 4:21 am
by Ranakastrasz
Would it be possible to make the item a modified grenade type item, such thar you can use it any range?

Re: [MOD 0.12.16] Displaying range of turrets [v1.1.2]

Posted: Mon Feb 01, 2016 5:53 pm
by kds71
Ranakastrasz wrote:Would it be possible to make the item a modified grenade type item, such thar you can use it any range?
I will have to look into it, but I think it is possible. I will try to do something about that and report back :)

Re: [MOD 0.12.1] Displaying range of turrets [v1.1.1]

Posted: Mon Feb 01, 2016 9:12 pm
by starholme
WhiteHat101 wrote:Hi, I love your mod concept, but I wasn't thrilled with the activation mechanism, so I've been trying different methods. I'm pretty happy with my mods to your mod, but I'll often get some client lag when it renders or un-renders a turret's range (It's creating/destroying ~100 entities per turret, I can't really blame the game for some stutter). My code renders nearby turrets; the lag becomes noticeable when I run down a wall of turrets, or past a cluster of turrets (easily, thousands of entities are created and then destroyed in a handful of seconds).

Might there be a way to render the range indicator with a single entity per turret?

Maybe a large custom sprite? Maybe something with the range indicator from power poles? Maybe we could tap into the range display used by grenades and bot capsules?
wip control.lua
If you are concerned with the performance impact, best to limit the number of entities changed per tick. You'll have to play around to decide, but either one turret per tick, or x entities per tick.
I've not had time to look at the code, but with a few assumptions:
Entity creation queue - contains all entities that need to be added
Current entity queue - contains all entities already added
Entity destruction queue - contains all entities that need to be destroyed
Active turrets queue - all the turrets that 'should' be showing a range

Each tick:
Add turrets to turret queue that are now in range (Maybe only every 10 ticks or something, probably not required 'every' tick)
- Add entities to creation queue for these turrets
Remove turrets from turret queue that are out of range (Maybe only every 10 ticks or something, probably not required 'every' tick)
- Remove entities from creation queue for these turrets
- Move entities from current queue to destruction queue for these turrets
Starting with the oldest entities in the Destruction queue, remove X (Where X is some fine tuned number) entities, decrementing X
If X is not yet zero, starting with the newest entities in the creation queue, create X entities, move from the creation queue to the current queue

Essentially, newest turrets in range have priority on drawing ranges, oldest turrets have highest priority for removing ranges.

Re: [MOD 0.12.16] Displaying range of turrets [v1.1.2]

Posted: Mon Feb 01, 2016 9:52 pm
by Zeblote
Any way to make this a proper circle (like when you have a grenade equipped) and show only when hovering mouse over a turret?

Re: [MOD 0.12.16] Displaying range of turrets [v1.1.2]

Posted: Tue Feb 02, 2016 8:55 pm
by kds71
Zeblote wrote:Any way to make this a proper circle (like when you have a grenade equipped) ...
Yes, there is a way to make it a proper circle - but, as I said earlier in this thread that would require me to create hundreds of circle sprites with different sizes, because I can't just draw any circle on the screen - it must be stored in the sprite sheet. Anyway, I think I will try to just do that - these simple sprites shouldn't take too much disk space even if there are hundreds of them.
Zeblote wrote:... and show only when hovering mouse over a turret?
This is, unfortunatelly, not possible, since there is no event fired when you hover over turret (or any other entity in the game).

Re: [MOD 0.12.16] Displaying range of turrets [v1.1.2]

Posted: Tue Feb 02, 2016 9:21 pm
by keyboardhack
kds71 wrote:
Zeblote wrote:Any way to make this a proper circle (like when you have a grenade equipped) ...
Yes, there is a way to make it a proper circle - but, as I said earlier in this thread that would require me to create hundreds of circle sprites with different sizes, because I can't just draw any circle on the screen - it must be stored in the sprite sheet.
Not sure if this is what you are refering to or not but you can scale a sprite. Example

Code: Select all

{
	filename = "__modname__/path/to/image",
   width = 100,
	height = 100,
	scale = 2, --twice the size so it will be shown on screen as a 200x200 sprite
}
You could use this to scale up circles to match the size of a turrets range.

Re: [MOD 0.12.16] Displaying range of turrets [v1.1.2]

Posted: Tue Feb 02, 2016 10:33 pm
by Ranakastrasz
keyboardhack wrote:
kds71 wrote:
Zeblote wrote:Any way to make this a proper circle (like when you have a grenade equipped) ...
Yes, there is a way to make it a proper circle - but, as I said earlier in this thread that would require me to create hundreds of circle sprites with different sizes, because I can't just draw any circle on the screen - it must be stored in the sprite sheet.
Not sure if this is what you are refering to or not but you can scale a sprite. Example

Code: Select all

{
	filename = "__modname__/path/to/image",
   width = 100,
	height = 100,
	scale = 2, --twice the size so it will be shown on screen as a 200x200 sprite
}
You could use this to scale up circles to match the size of a turrets range.
Unless you have one for each potential range, it won't work out so well for mods. It works great as is for Bob's mod Sniper turrets. You would want to have one for, like, 1 through 100 or something, which would be sufficient for any range you might reasonably expect to show up.

Re: [MOD 0.12.16] Displaying range of turrets [v1.1.2]

Posted: Tue Feb 02, 2016 11:05 pm
by Zeblote
Can you iterate over all turret prototypes in data-final-fixes and make sprites for the ranges that are used?