[Klonan] Neutral force simple-entity-with-owner ignores force_visibility.

This subforum contains all the issues which we already resolved.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

[Klonan] Neutral force simple-entity-with-owner ignores force_visibility.

Post by eradicator »

What

force_visibility = 'friend' should make a simple-entity-with-owner invisible to non-friend forces. However if the entity is on the neutral force it remains visible.

Reproduction

1) Apply force_visibility to built-in SEWO

Code: Select all

data.raw['simple-entity-with-owner']['simple-entity-with-owner'].force_visibility = "friend"
2) Spawn a bunch of chests and make them blink with force.set_friend:

Code: Select all

/c

local function f (entity, friend)
  for _, force in pairs(game.forces) do
    --[[ make 110% sure that diplomacy is setup correctly ]]
    entity.force.set_cease_fire(force, false)
    force.set_cease_fire(entity.force, false)
    entity.force.set_friend(force, friend)
    force.set_friend(entity.force, friend)
    end
  end
  
local x, friend = {}, false

for _, force in pairs(game.forces) do 
  table.insert(x, game.player.surface.create_entity{
    name = 'simple-entity-with-owner',
    position = {game.player.position.x + force.index*2, game.player.position.y},
    force = force,
    })  
  end  
  
script.on_nth_tick(30, function()
  friend = not friend
  for _, entity in pairs(x) do f(entity, friend) end
  print(friend)
  end)
Expected behavior

The neutral force is by default in cease_fire/ally mode, so friend-only entities should not be visible to other forces.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5221
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Neutral force simple-entity-with-owner ignores force_visibility.

Post by Klonan »

Neutral force is hardcoded to always return true for 'is_friend' checks, this isn't a bug

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Neutral force simple-entity-with-owner ignores force_visibility.

Post by eradicator »

Klonan wrote:
Mon Apr 19, 2021 9:38 pm
Neutral force is hardcoded to always return true for 'is_friend' checks, this isn't a bug
Then why does the API not report that correctly?

Code: Select all

/c game.print(game.forces.neutral.get_friend('player'))
prints false (If it hadn't I wouldn't have bothered writing a bug report about it)
Last edited by eradicator on Mon Apr 19, 2021 9:47 pm, edited 1 time in total.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5221
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Neutral force simple-entity-with-owner ignores force_visibility.

Post by Klonan »

eradicator wrote:
Mon Apr 19, 2021 9:42 pm
Klonan wrote:
Mon Apr 19, 2021 9:38 pm
Neutral force is hardcoded to always return true for 'is_friend' checks, this isn't a bug
Then why does the API not report that correctly?

Code: Select all

/c game.print(game.forces.neutral.get_friend('player'))
prints false
Because internally it calls the wrong functions 8-)

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Neutral force simple-entity-with-owner ignores force_visibility.

Post by eradicator »

Klonan wrote:
Mon Apr 19, 2021 9:47 pm
Because internally it calls the wrong functions 8-)
Hrng. Looking forward to the fix which hopefully includes other stuff like... game.forces.player.get_friend('player') and probably cease_fire related stuff too? :twisted:
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5221
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: [Klonan] Neutral force simple-entity-with-owner ignores force_visibility.

Post by Klonan »

I have thought about it, for now I don't want to change how these functions internally work

If you care, you can use the 'set friend' and 'set ceasefire' on the neutral and player forces so the API will respond correctly, or make an exception in your code for neutral as there is in the engine

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: [Klonan] Neutral force simple-entity-with-owner ignores force_visibility.

Post by eradicator »

Klonan wrote:
Tue Jun 15, 2021 4:21 pm
If you care
Just yesterday it tripped me up again (in a completely unrelated mod) and I had to search the forum just to find my own bugreport still pending. And now this.
Klonan wrote:
Tue Jun 15, 2021 4:21 pm
you can use the 'set friend' and 'set ceasefire' on the neutral and player forces so the API will respond correctly
That's not reliable, any other mod can break that.
Klonan wrote:
Tue Jun 15, 2021 4:21 pm
, or make an exception in your code for neutral as there is in the engine
Yea, now that I (and anybody who's lucky enough to find this thread) at least know that there's undocumented hardcoded behavior involved I can write another wrapper that's only gonna fix my own mods...

Thanks for at least looking I guess? I'm not sure I'd even want to know what kind of code mess in the backend prompted that decision. I truely hope it's not a political decision.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5221
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: [Klonan] Neutral force simple-entity-with-owner ignores force_visibility.

Post by Klonan »

Okay I will think about it some more

I am just worried that it might be a breaking mod change just out of the blue,
That neutral will start replying 'true' to get friend when nobody added it to the friend list,

I was not sure how seriously of a pain you took it as to work with this issue

Internally it is a simple check, there is the friend list, and the cease fire list,
And the Lua API check just checks to see if the force is in that list,

But other engine check, use a different function, which also starts to check other things,
Like if neutral force is checking for friend, its always true,
If you're checking your own force, friends is always true, etc.

As well, the ceasefire is implemented in a different way,
There are many different and subtly different methods, such as 'isEnemy', 'wantsAttack', getTargetMask', etc,
It isn't exactly a 'big mess', but it start to feel a bit more fragile to me, especially as there are many different places in the code, attack logic, AI, triggers, drawing, which all use different variations,

So the change has potentially a lot of side effects,
As well I am not super happy with the API anyway, I think it would be better to somehow use the Force condition: https://lua-api.factorio.com/latest/Con ... eCondition
Also methods for instance, to retrieve the whole list of friends, whole list of enemies, etc.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: [Klonan] Neutral force simple-entity-with-owner ignores force_visibility.

Post by eradicator »

Klonan wrote:
Tue Jun 15, 2021 9:03 pm
Okay I will think about it some more
Thanks. And please excuse my bitter tone.
Klonan wrote:
Tue Jun 15, 2021 9:03 pm
I am just worried that it might be a breaking mod change just out of the blue,
That neutral will start replying 'true' to get friend when nobody added it to the friend list,
Well, as the internal behavior is different anyway I'd assume that mods making assumptions in that regard are already broken.
Klonan wrote:
Tue Jun 15, 2021 9:03 pm
breaking mod change just out of the blue,
[...]
So the change has potentially a lot of side effects,
I can understand that, and I'd be totally fine if you were to say that it'll be fixed with the addon (whenever that is). But having a function that (in my point of view) lies into my face, not fixed at all is a bitter pill to shallow. As a modder I have to rely on the API giving correct information. If i can't trust the API that is a huge problem for me. It's not just "oh, but all the other functions don't lie!", because once the trust is gone I don't know that anymore. Maybe I just haven't yet tripped the particular edge-case where they do lie? Maybe the result is so subtly different that I simply haven't noticed it yet? None of this is_friend hardcoding was documented anywhere, so I don't know what other hardcoded tripwires to expect.
eradicator wrote:
Tue Jun 15, 2021 5:30 pm
Klonan wrote:
Tue Jun 15, 2021 4:21 pm
you can use the 'set friend' and 'set ceasefire' on the neutral and player forces so the API will respond correctly
That's not reliable, any other mod can break that.
Thinking about this some more: If neutral/enemy forces are supposed to have some sort of fixed behavior, I'm not so sure what the usecase is for allowing to change them at all? If set_friend/cease_fire simply had no effect and the forces were set up correctly on an engine level, that would still be a better "fix" than anything I can do in lua.

The lua-side workarounds I see are both ugly:
  1. a custom wrapper (slow) that checks for force.name == 'neutral' / 'enemy', for which I hopefully guess all the hidden behavior right, and which I always have to remember to call instead of the build-in function and which in the end fixes only my own mods.
  2. Abusing on_force_cease_fire_changed (etc.) to enforce the "correct" diplomacy for every mod, again making guesses about the "correct" setup, potentially causing infinite loops when another mod tries to do something similar, etcpp.
Klonan wrote:
Tue Jun 15, 2021 9:03 pm
It isn't exactly a 'big mess', but it start to feel a bit more fragile to me, especially as there are many different places in the code, attack logic, AI, triggers, drawing, which all use different variations,
I see. That sounds at least more messy than your initial response (from which I assumed it would be only a few lines of change). But on the lua side it is at least as messy to try to fix it even if I knew the correct conditions. And I don't because there's no api that returns them. So you have the distinct advantage of being able to see the real conditions.
Klonan wrote:
Tue Jun 15, 2021 9:03 pm
I think it would be better to somehow use the Force condition: https://lua-api.factorio.com/latest/Con ... eCondition
While this (on a first glance) seems to work for the SEWO I mentioned in the opening post this doesn't work for the problem I had yesterday: I need to know if a player is theoretically allowed to "operate" LuaPlayer.selected (range independant). I have no control over either the player's nor the entity's force. So get_friend seemed to be my only option. I had to postpone the feature after noticing the api problem again.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5221
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: [Klonan] Neutral force simple-entity-with-owner ignores force_visibility.

Post by Klonan »

So for the next release I have added LuaForce::is_friend() and is_enemy().

They use the same internal functions as the force condition check of 'friend' and 'enemy', so they work correctly for neutral force, and checking your own force.

I have also tweaked the docs about get_friend and get_ceasefire, to note that it only check if the other force is in the 'friend' or 'ceasefire' list.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: [Klonan] Neutral force simple-entity-with-owner ignores force_visibility.

Post by eradicator »

Klonan wrote:
Tue Aug 17, 2021 5:08 pm
So for the next release I have added LuaForce::is_friend() and is_enemy().
Great! Thank you very much.

So am I assuming correct...

Code: Select all

local is_ceasefire = (not is_friend()) and (not is_enemy())
...that this would then be the third state where the force doesn't shoot at me but I also can't open their entities?
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5221
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: [Klonan] Neutral force simple-entity-with-owner ignores force_visibility.

Post by Klonan »

eradicator wrote:
Tue Aug 17, 2021 5:42 pm
So am I assuming correct...

Code: Select all

local is_ceasefire = (not is_friend()) and (not is_enemy())
...that this would then be the third state where the force doesn't shoot at me but I also can't open their entities?
Yes

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: [Klonan] Neutral force simple-entity-with-owner ignores force_visibility.

Post by eradicator »

Klonan wrote:
Tue Aug 17, 2021 6:46 pm
Yes
Superb.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Post Reply

Return to “Resolved Problems and Bugs”