Artillery Cannon help required

Place to get help with not working mods / modding interface.
Post Reply
Buhamut
Inserter
Inserter
Posts: 24
Joined: Sun Apr 26, 2015 1:47 pm
Contact:

Artillery Cannon help required

Post by Buhamut »

What I want to do in psedocode:

Code: Select all

If turret is placed, add turret to THELIST -- have this working, and THELIST is just a psedoname
If turret is removed/destroyed, remove turret from THELIST -- have this working
Cycle through THELIST  -- have this working
  If turret has target 
    if targetRange > 50
      break
    else
      find new target -- I know how to do this, just not coded
    end
  else
    find new target
    if no new target, idle -- I think I can get this to work
  end
My problem is trying to keep track of what the turrets have for a current target, there doesn't seem to be any type of ID tracking. In order to correctly find my turrets in THELIST I search for correct position data, which works because they are buildings and do not move. The turrets will target biters though and they move around. So I wanted to add some ID data to the ones that were targeted, so I could find them again really easy. We already have entity.type, and entity.name. I want to add entity.ID.

However so far all of my attempts to store ID on the biters, or target info on the turrets have failed. I cannot add a .target field to the turrets stored in THELIST, I cannot add an ID field to the biters I want to track. I have tried:

Code: Select all

if not glob.artillerytable[entry].target then 
  glob.artillerytable[entry].target = {}
end
Along with other similar variants, including table.insert(). It seems not a problem with the Lua code, but rather the factorio entity handler that does not like me adding fields it doesn't know about.

I can create and maintain a parallel list for storing the targets and data, but how am I supposed to track my targets easily?! If I want to verify that my turrets current target is still valid, how can I find that same biter again later? The interval between acquiring a target, and checking later means that the position data will be completely different by then, so I can't use that, and I can't add a field to it ...

Any thoughts on this? I'm completely out of ideas right now.

Koub
Global Moderator
Global Moderator
Posts: 7199
Joined: Fri May 30, 2014 8:54 am
Contact:

Re: Artillery Cannon help required

Post by Koub »

I must confess I have never coded in lua, but I have read a pair of manuals, just in case I would get into it.
If I remember correctly, you canindex a list with about anything in LUA.
Why not make a list that associates each cannon to a biter ID ? like :
Cannonlist = (cannon1;ID biter1), (cannon2;id biter2), ...
Like that, you just have to check fir each biter ID in range :
if cannonlist(currentbiterID) exists, get to next biter
else add current cannon in cannonlist with currentbiterID as index, and start over.

I might have totally not understood how LUA works, but if I'm right, it could be an efficient way to ensure each cannon targets a new biter.
I haven't thought on how you would have to cycle once every biter is already targeted, but shouldn't be over complicated.
Koub - Please consider English is not my native language.

orzelek
Smart Inserter
Smart Inserter
Posts: 3911
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Artillery Cannon help required

Post by orzelek »

I'm not sire if this will work - but you should be able to put actual biter entity to target list for given cannon.

One thing you can't do is to modify the entity - thats why I think your plan is not working.

You can index by entity tho - so you can have something like this:

Code: Select all

local targets = glob.targets[turretEntity] or {}

local currentTarget = nil

if #targets > 0 then
for _,target in ipairs(targets) do
    if target.valid then
        currentTarget = target
        break
    end
end
end

if not currentTarget then
-- search for new targets
end

currentTarget -- here you can shoot at it
I'm not 100% about valid field - it's mentioned on wiki for entities but not described.

Buhamut
Inserter
Inserter
Posts: 24
Joined: Sun Apr 26, 2015 1:47 pm
Contact:

Re: Artillery Cannon help required

Post by Buhamut »

In reply to Koub, that's my issue. There is no biter ID. I can get his type, name, current position, all kinds of stuff, but nothing UNIQUE to THAT biter like an ID.
In order to find my own turrets on my list, I have to use the position data, which works fine because they are buildings and do not move.

To everyone else, let me simplify the question:

If I make a list in glob, say named targets, so that I have glob.targets

I find a biter I want to kill, lets call him George.

I add him to the list in glob.targets. Does this just COPY George's current info into the list, or does this entry now point to George himself?

If I let the game run for a while so that George has now moved, and then look on the list at glob.targets[1].position, will it show the values from when I put George into the list (A copy), or will it show me Georges new current location?

If it is the latter, then I have no problem and this will be easy. If it is NOT Georges current position, but just a copy of where he WAS, then how do I find George again?

Koub
Global Moderator
Global Moderator
Posts: 7199
Joined: Fri May 30, 2014 8:54 am
Contact:

Re: Artillery Cannon help required

Post by Koub »

Hmmm I see. You'll have to implant a GPS chip in its neck :mrgreen:.

Ok more seriously. Do you have to always aim at George with the same artillery ? No So at every tick, you can "reassociate" one artillery position to one biter position by scanning all the biters and all the artilleries that can shoot at that tick. Every artillery shoots, and at next tick, you scan to see if there's an artillery ready to shoot, and as soon as you have one, rince and repeat.
Koub - Please consider English is not my native language.

Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

Re: Artillery Cannon help required

Post by Choumiko »

Buhamut wrote:In reply to Koub, that's my issue. There is no biter ID. I can get his type, name, current position, all kinds of stuff, but nothing UNIQUE to THAT biter like an ID.
In order to find my own turrets on my list, I have to use the position data, which works fine because they are buildings and do not move.
the "lua table" where you're getting the name/type, position is what you call the id. I guess you do something like

Code: Select all

    for _, entity in pairs(game.findentitiesfiltered{area = area, type = "biter"}) do
         table.insert(glob.targets, entity)
    end
Say, George is inserted as the second biter, then glob.targets[2] will always be George, and glob.targets[2].position will always be the current position (unless he gets killed, then it will throw an error, thats what entity.valid is for)
You can't simply use the entity as a table index, as they change. So to find a specific one (or to prevent adding the same biter multiple times) you'll have to loop the table and compare them with entity.equals()

Buhamut
Inserter
Inserter
Posts: 24
Joined: Sun Apr 26, 2015 1:47 pm
Contact:

Re: Artillery Cannon help required

Post by Buhamut »

Good news, and bad news.

Thanks to the help I received here (Thank you all by the way! Greatly appreciated)I was able to run some tests. If I add an entity to a list, (such as George from the example above), all of its stats do update in real time to show current values. This made things easy to implement my targeting script which works flawlessly. I can pick a nice target between 50 and 200 units away, and double check periodically whether or not the target is still in that range before the artillery fires.

However, When I first starting writing a script to implement min range I had tested unit.setcommand() by first seeing if unit.hascommand() would return true (which it did), and then giving it a target with unit.setcommand(). Since I did not have a targeting script yet to pass a biter to it, I tested it by telling it to fire on me. This returned an error of "Entity is not an enemy". I thought to myself "Oh well that makes sense, you wouldn't want a turret to be able to fire on friendlies if they got mixed in.".
When I got a valid target however, and attempted the same call with the new, now enemy target, I got the same message and realized it was not referring to the TARGET but the ARTILLERY CANNON. I cannot issue an attack command to my cannon, with that command, as the command only works on enemies. SO MUCH WASTED TIME!

*** How can I either issue an attack (and Idle) order to a turret?
-OR-
*** How can I create the bullet entity (Like in the prototype), and assign a target (Coords, Entity, anything) to IT, and fire the damn thing manually?

Koub
Global Moderator
Global Moderator
Posts: 7199
Joined: Fri May 30, 2014 8:54 am
Contact:

Re: Artillery Cannon help required

Post by Koub »

Hi,

Dunno if it can help, but what's the following line in entity\turrets.lua doing :

Code: Select all

      projectile_creation_distance = 0.6,
I mean if you raise the distance where the projectile is created, won't it work ?
Koub - Please consider English is not my native language.

DOSorDIE
Fast Inserter
Fast Inserter
Posts: 249
Joined: Sat Oct 11, 2014 3:43 pm
Contact:

Re: Artillery Cannon help required

Post by DOSorDIE »

Maybe only a bad idea, but i itry it.

Make a Maker (like a granate) but with a huge radius to throw.
Use a as example a light that is not attackale but visible.
And your artillery fire only at that marked point until u remove the marker.
Make the marker with a countdown (5 min as example) until it explode.
Without a marker the artillery stop fire.

User avatar
rk84
Filter Inserter
Filter Inserter
Posts: 556
Joined: Wed Feb 13, 2013 9:15 am
Contact:

Re: Artillery Cannon help required

Post by rk84 »

Choumiko wrote:You can't simply use the entity as a table index, as they change. So to find a specific one (or to prevent adding the same biter multiple times) you'll have to loop the table and compare them with entity.equals()
This is about to change as they are ready to remove equals-method, because LuaObjects will be compared with == (in 0.12.x?)
Test mode
Searching Flashlight
[WIP]Fluid handling expansion
[WIP]PvP gamescript
[WIP]Rocket Express
Autofill: The torch has been pass to Nexela

Buhamut
Inserter
Inserter
Posts: 24
Joined: Sun Apr 26, 2015 1:47 pm
Contact:

Re: Artillery Cannon help required

Post by Buhamut »

rk84 wrote:
Choumiko wrote:You can't simply use the entity as a table index, as they change. So to find a specific one (or to prevent adding the same biter multiple times) you'll have to loop the table and compare them with entity.equals()
This is about to change as they are ready to remove equals-method, because LuaObjects will be compared with == (in 0.12.x?)
I do not know if they are planning on changing entity.equals() with a == operator, but the point is moot. If you record an entity to a list everything will update in real time to the entity. You can have it display a biters position every second and watch it change as the biter walks around. This is a floating point number that carries out to about 7 or 8 digits making finding the same biter trivially easy.
DOSorDIE wrote:Maybe only a bad idea, but i itry it.

Make a Maker (like a granate) but with a huge radius to throw.
Use a as example a light that is not attackale but visible.

***** And your artillery fire only at that marked point until u remove the marker.

Make the marker with a countdown (5 min as example) until it explode.
Without a marker the artillery stop fire.
The problem I have now is not the targeting, I have a good script written to find and track targets.

*** I cannot tell the turret what to fire at or where to fire. ***

It does not matter if I want to type in coordinates, put down a marker, find a biter to target or even have it target me the player. I cannot tell it where to fire, or what to fire at.
Koub wrote:Hi,

Dunno if it can help, but what's the following line in entity\turrets.lua doing :

Code: Select all

      projectile_creation_distance = 0.6,
I mean if you raise the distance where the projectile is created, won't it work ?
That line tells the game how far away from the center of the turret to make the projectile. You don't want the projectile to appear after a big gap, or even behind the turret or else it would look weird. Unfortunatly this line is already being used in my prototype and does not help me with my problem.

starholme
Fast Inserter
Fast Inserter
Posts: 201
Joined: Tue Oct 21, 2014 7:25 pm
Contact:

Re: Artillery Cannon help required

Post by starholme »

Anyone ever figure out how to create an explosion in a mod? I'm thinking that the turret doesn't actually fire, it just could create an explosion entity at a location (as if you threw a grenade there as a player)

SpeedyBrain
Long Handed Inserter
Long Handed Inserter
Posts: 77
Joined: Wed May 20, 2015 12:08 am
Contact:

Re: Artillery Cannon help required

Post by SpeedyBrain »

starholme wrote:Anyone ever figure out how to create an explosion in a mod? I'm thinking that the turret doesn't actually fire, it just could create an explosion entity at a location (as if you threw a grenade there as a player)
An explosion is just an entity :)

Turrets shoot a projectile at a locked target
The projectile has values like onimpact create explosion.
ImageImageImageImage

Post Reply

Return to “Modding help”