narc0tiq / YARM

Yet another variant of the Resource Monitor for Factorio
https://mods.factorio.com/mod/YARM
MIT License
53 stars 40 forks source link

Code changes to resolve site overwrite due to proximity and to allow custom naming formats #165

Open PiggyWhiskey opened 9 months ago

PiggyWhiskey commented 9 months ago

I don't have permission to submit a pull request, so here it is:

resmon.lua Replace lines 527-530 with

local tmpName = settings.global["YARM-site-autoname-format"].value
--Surface
tmpName = string.gsub(tmpName, "{surface}", string.gsub(" " .. site.surface.name, "%W%l", string.upper):sub(2)) --Camel Case

--X
tmpName = string.gsub(tmpName, "{x}", site.center.x)
--Y
tmpName = string.gsub(tmpName, "{y}", site.center.y)
--Resource
--HACK: Localised name doesn't seem to be substituting
tmpName = string.gsub(tmpName, "{resource}", site.ore_type)
--Cardinal
tmpName = string.gsub(tmpName, "{cardinal}", get_octant_name(site.center))
--Distance
tmpName = string.gsub(tmpName, "{distance}", string.format("%d", util.distance({ x = 0, y = 0 }, site.center)))
site.name = tmpName

settings.lua Add

{
    type = "string-setting",
    name = "YARM-site-autoname-format",
    setting_type = "runtime-global",
    order = "e",
    default_value = "{surface} - {resource} ( {x}, {y} )",
},

locale/en/base.cfg Add

[mod-setting-name]
YARM-site-autoname-format=Format to automatically name new sites

[mod-setting-description]
YARM-site-autoname-format=All tags must be fully lowercase\nOnly applies to new Resource Sites\n\n{surface} = Surface of origin (Nauvis)\n{resource} = Resource name (Iron Ore)\n{x} = X Coordinate of site\n{x} = Y Coordinate of site\n{inc} = Increment if name already exists\n{cardinal} = Compass direction from 0,0\n{distance} = Tiles distance from 0,0

These changes will allow players to add custom naming conventions, and prevent the common issue of sites being removed due to the same automatic name being used

wchristian commented 9 months ago

i'd like to integrate this with proper attribution. mind forking the repo to your account, making a commit there, and starting a pull request from there?

PiggyWhiskey commented 9 months ago

I can do that, I want to fix the lack of localised resource name though for some reason site.ore_name doesn't return any value, although it's used fine in printing to the player. site.ore_type returns the object name iron-ore though, but it's not the best.

It also would need more work to have a site 'ID' to allow an automatic increment, possibly check if current_site.name exists in the data list

The issue with cross-surface resource sites in the same co-ords still breaks the values as well, so the unique names won't fix it.

narc0tiq commented 9 months ago

I want to fix the lack of localised resource name though

Well, it's easy enough to look up the docs on LocalisedString but what you're really running into is that it's an all-or-nothing situation: the site name must be either a raw string or a localized string, and due to Lua, it really has to be a raw string (because it's a table key!).

What I would suggest is that you remove the site name from display completely, internally generate it to be as unique as possible (could be surface + x/y would be enough) and add a separate site.display_name that the player can set freely with any content they like*. You can ask Factorio to give you raw text from a localised string** (using a specific player's locale, so it's deterministic), and you should know which player is adding the site so you can use them for the initial naming.

You can even leave it as a LocalisedString until a player tries to change it, but that might be too confusing for multilingual multiplayer servers ("Why did 'Eisenerz' change to 'Iron ore' when Mr.X renamed the site?").


The issue with cross-surface resource sites in the same co-ords

That one comes from the ore tracker using the x/y coordinates as the table key: https://github.com/narc0tiq/YARM/blob/072291cd134b86a948a8a9eb1ecc5f50b574d734/libs/ore_tracker.lua#L35-L40

You will want to change it so position_to_string also takes a surface parameter and make a migration that updates the old ones -- remember the ore tracker has the entity itself so ask the entity to tell you its surface.

To be perfectly honest, given the changes in Factorio savegames since YARM v0.7.15 (i.e., before Factorio 0.14!), it might be better to remove the ore tracker altogether and go back to storing the actual entities in the site. All this crap (meaning resmon.migrate_ore_sites) is a workaround for the fact that, at the time, it was making loading times increase massively if you had lots of entity references in your global data -- and then, by the time the ore_tracker came around, that was all fine again and we were mitigating other problems, which have probably also been fixed.


* Any content the player likes, including duplicates -- I should be able to have 5 "Base" sites for my 5 resources in the starting area. ** Slight wrinkle in this is that request_translation doesn't directly return anything, but raises on_string_translated instead -- you will have a tick or two where the display name is unavailable and you'll have to have a way to link the translated string back to the site whose name it's supposed to be when the translation does come in.

PiggyWhiskey commented 9 months ago

Seems like a re-write of how it stores the resource fields might be better

The other issue with using center coordinates is an ore patch surrounding another patch will have the same center

Maybe the sites should stored like: UniqueID Surface Resource Type Resource Name Tick Added Entities[]

other calculated fields Total Quantity ETD etc

Then a few functions to find the sites needed get_site_by_id(siteID get_site_by_surface(surface, [resourceType]) get_site_by_location(surface, resourceType, location[x,y])

I saw there was a YARM2 branch, is that happening?

narc0tiq commented 9 months ago

I saw there was a YARM2 branch, is that happening?

I haven't worked on YARM in years, that's why I was happy that @wchristian is willing to take over maintenance.

YARM2 hasn't made it past the basic concept stage; what I did manage to get running is the part where you craft a thing like a fake miner that you put on the ore patch, and it counts the ore for you (using invisible circuit wires to an invisible pole, because that's the only way to get the numbers out).

I really liked that concept since it's basically not having to do its own counting but I hated the invisible pole because it could get left behind somehow (is it actually possible to register a fully dependent entity? That gets destroyed when the main one does?).

The other part I didn't like is this doesn't really help for oil fields, or any other sparse resource, and I have several options I didn't like for that:

But I never got around to implementing any of these and it's just decayed in the 3 years since. Maybe if the Factorio expansion motivates me to get back to modding I'll make another go of YARM2 -- ever since miners got the ability to output an ore patch summary to circuit network I've always wanted to use that somehow.


On a very related note, a different thing I wanted was to make a generic circuit network monitor -- basically YARM, but using a circuit-connected device and thus able to monitor literally anything. I got some progress on https://github.com/narc0tiq/prodmon (maybe should be named circuit-monitor?) but it didn't retain my attention enough to get through to a minimum-releasable.

Part of the concept for YARM2 was to enable some UI goodness like grouping and summaries, as well as local configurability since there would be a proper entity to attach to; prodmon would have had something similar as you'd want to filter what is actually tracked, configure what "full" means, and other options that could make it a really useful tool overall. There is a mod called production monitor (or something similar) but that uses the production statistics whereas prodmon, despite the name, would just be about whatever circuit signals you could craft, and could mean anything.

But my Factorio time has dwindled over the years and everything stalled. I kept hoping someone would pick up the resource monitor idea and make their own (as I did to follow up the 3 similar monitors from ancient Factorio that got discontinued), but apparently YARM is somewhere where it's just good enough for the people who want a monitor and can write mods, so it's just stagnated as-is.

If you want to make a go of it, you're welcome to both my ideas and any of my code that you can use: we're open source here, we love forks.