Unique items in MP

Place to get help with not working mods / modding interface.
YoxFox
Manual Inserter
Manual Inserter
Posts: 4
Joined: Sun Jun 20, 2021 11:54 am
Contact:

Unique items in MP

Post by YoxFox »

Hello guys!

I'm trying to implement my idea in special economic mode and I met an issue when I decided to create unique items based on player name or index.

Idea: I want to create fiat money for MP. This is just an item (a coin) which can be created by one player only. And every player in MP can create his/her own unique money. Nobody elese can't reproduce others coins, but can keep different coins, give they to others (trading) and etc. As an exmaple of money recipe, we can consider simple production: 1 wood -> 100 unique player's coins (remember, this is fiat money!)

Of course I know that it's possible to create virtual money mode with something like virtual bank account. Everybody will have an access to own account and virtual money. But in this case we loose very intersting mechanics like theft, money trains and it's just nice to see how your money lie in a chest or is moving on a belt.

So, my general question: can I create unique items based on player's name / index? I've created my own money (coin) item. I tried to modify my item name in runtime, create unique item property in runtime (example: in control.lua, on_player_crafted_item event). But, I can't do that! The game doesn't allow me to modify/add item properties to make an item (item stack) unique...

Is it possible to create unique items and make my dream (own fiat money in MP) come true? :)

PS: After fiat money, I'm planning to add also "gold" money. This is just very rare item (which can be crafted from rare meneral). If this is very rare and useful in some craft chains, it can be valuable item (like gold, platinum, diamonds in real life). And we can use it in trading operations!
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Unique items in MP

Post by eradicator »

You can't change prototypes at runtime, so the short answer is: No.

The long answer is: Maybe. I see two one possibilities:

1) Generate 100 different coin prototypes in data stage.... Hm, nope, this doesn't work after all.
You'd also need 100 variations of each recipe. Also recipes are owned by forces, not players, so team-play wouldn't work. And in the odd case that more than 100 players join a game (not nessecarily simultaenously) it wouldn't work.


2) Use item-with-tags which can have arbitrary data attached to it (like a player name). And other players will be able to see the name in the item tooltip. But item-with-tags can't be crafted automatically with a specific tag, you have to implement your own fake assembling machine for those recipes (practical upshot of scripted machine is that you could theoretically even change the recipes during a running game. Inflation anyone?). It's not too complicated, but might be a bit slow if the player builds thousands of those machines. If done properly it should still be fast enough for normal-sized factories. Also you have to prevent other players of the same force ("team") from manipulating that machine I guess?
YoxFox wrote: Sun Jun 20, 2021 12:24 pm PS: After fiat money, I'm planning to add also "gold" money.
If you're new to factorio modding I'd start with that. It's much easier as there's a pre-made "market" structure that can handle trading. That way you get a better feeling of how modding works and implementing the fiat thing should be easier and cleaner if you do it with some experience.
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.
YoxFox
Manual Inserter
Manual Inserter
Posts: 4
Joined: Sun Jun 20, 2021 11:54 am
Contact:

Re: Unique items in MP

Post by YoxFox »

eradicator wrote: Sun Jun 20, 2021 1:01 pm If you're new to factorio modding I'd start with that. It's much easier as there's a pre-made "market" structure that can handle trading. That way you get a better feeling of how modding works and implementing the fiat thing should be easier and cleaner if you do it with some experience.
Yes, I'm new man in factorio modding, but I already have small experience. And I see that "golden" money is not a problem (just add new ore, new item + new reciept). After taht I can take some ideas from others trading mods :)
eradicator wrote: Sun Jun 20, 2021 1:01 pm Also you have to prevent other players of the same force ("team") from manipulating that machine I guess?
Probably, it'll be great if not only user will be responsible for money emission. For example, some player crates a team and they control the economic together!
eradicator wrote: Sun Jun 20, 2021 1:01 pm 2) Use item-with-tags which can have arbitrary data attached to it (like a player name). And other players will be able to see the name in the item tooltip. But item-with-tags can't be crafted automatically with a specific tag, you have to implement your own fake assembling machine for those recipes
If I understand correctly, you mean that I can create an item of "item-with-tags" type, and then I can create some assembling machine that can produce such type of items? If two players create these items in different own assembling machines, are they produce different logic items? Will items not be mixed in one chest/inventory? I'll try to investigate some usage examples in other mods...
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Unique items in MP

Post by eradicator »

YoxFox wrote: Sun Jun 20, 2021 1:25 pm If I understand correctly, you mean that I can create an item of "item-with-tags" type,
Also have a look here https://wiki.factorio.com/Prototype_definitions
There's several types of "item-with-*". Not sure which is best in your case. Also I have no idea if and how these can stack at all.
YoxFox wrote: Sun Jun 20, 2021 1:25 pm and then I can create some assembling machine that can produce such type of items?
Except it's not a real assembling machine. It's just something that you make yourself that looks and behaves the same as an assembling machine.
YoxFox wrote: Sun Jun 20, 2021 1:25 pm If two players create these items in different own assembling machines, are they produce different logic items?
That's the part you have to program yourself.
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
Silari
Filter Inserter
Filter Inserter
Posts: 581
Joined: Sat Jan 27, 2018 10:04 pm
Contact:

Re: Unique items in MP

Post by Silari »

eradicator wrote: Sun Jun 20, 2021 1:01 pm 1) Generate 100 different coin prototypes in data stage.... Hm, nope, this doesn't work after all.
You'd also need 100 variations of each recipe. Also recipes are owned by forces, not players, so team-play wouldn't work. And in the odd case that more than 100 players join a game (not nessecarily simultaenously) it wouldn't work.
This could work if you don't want each player's recipe to have a different cost, and you restricted it to handcrafting only. Then you could make a single recipe to make money that everyone shares, but catch the on_player_crafted_item event to replace it with that player's specific money. Would still be limited to a number of players per the prototypes you make.

Obviously, the downside of handcrafting only is it's hard to make a lot of them, but it might be fine if the idea isn't for players to have tons of their coins around.

Combine it with the 'item-with-tags' type idea and you don't need to make the prototypes and avoid that limit - you can make your item with the proper tag in the event.


Alternatively, you could always make the coin creation part of a GUI. Then it would be scripting to handle everything including cost.
YoxFox
Manual Inserter
Manual Inserter
Posts: 4
Joined: Sun Jun 20, 2021 11:54 am
Contact:

Re: Unique items in MP

Post by YoxFox »

After some investigation and atemptions, I've created the solution for my request:

Code: Select all


-- INIT

global = global or {}

script.on_init(function()
	mod_init()
end)

function mod_init()
	global.money_machines = {}
end

-- END INIT

function on_money_machine_placed(event)

	local entity = event.created_entity
	local player = game.get_player(event.player_index)
	local money_machine_destroy_id = script.register_on_entity_destroyed(entity)

	global.money_machines[money_machine_destroy_id] = {entity=entity, player_name=player.name}

end

function on_money_machine_destroyed(event)
	
	local money_machine_destroy_id = event.registration_number
	global.money_machines[money_machine_destroy_id] = nil

end

function update_money_machines(event)
	
	for _, money_machine_data in pairs(global.money_machines) do
		local crafted_fiat_money = money_machine_data.entity.get_output_inventory().find_item_stack("money-with-tags")
		if not (crafted_fiat_money == nil) then
			crafted_fiat_money.set_tag("money_id", money_machine_data.player_name)
		end
	end

end

-- HOOK EVENTS

local money_machine_filters = {{filter="name", name="money-machine"}}
script.on_event(defines.events.on_built_entity, on_money_machine_placed, money_machine_filters)
script.on_event(defines.events.on_entity_destroyed, on_money_machine_destroyed)
script.on_event(defines.events.on_tick, update_money_machines)

General idea: we have fiat money recipe which produces an item of item-with-tags type. We can use this recipe in special money machines only. When you place your money machine, the mod registers the machine for this player. We set special tag to crafted money on every game tick. It makes the money unique for every player who placed own money machines.

PS: after teams logic implementation, we can set "money_id" tag as team name. It'll allow us to produce fiat money and control the economic by teams.
YoxFox
Manual Inserter
Manual Inserter
Posts: 4
Joined: Sun Jun 20, 2021 11:54 am
Contact:

Re: Unique items in MP

Post by YoxFox »

Thx @eradicator and @Silari for your help.
andrei
Burner Inserter
Burner Inserter
Posts: 14
Joined: Tue Aug 27, 2019 8:32 pm
Contact:

Re: Unique items in MP

Post by andrei »

I see that you went a different way, but in case you decide to reconsider, I'd like point out that hand-crafting is also a viable option. I think Silari's solution should work well, and as for
Silari wrote: Sun Jun 20, 2021 6:34 pm Obviously, the downside of handcrafting only is it's hard to make a lot of them, but it might be fine if the idea isn't for players to have tons of their coins around.
this is easy to fix by having multiple recipes: “make 1 coin”, “make 100 coins”, “make over 9000 10000 coins”.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Unique items in MP

Post by eradicator »

Yea, generally looks good. Has potential for performance optimization, but that's a story for later. Just one thing:

Code: Select all

global = global or {}
Remove that. Now. Right now. Can't stress enough how wrong that is. It's a typical mistake that too many people make. It's a desync waiting to happen. The engine does that for you. Doing it manually just breaks things. If you ever encounter a "trying to index a nil value" regarding "global", then it's because you're trying to read from it too early, not because you forget to create it. It's one of the things in factorio modding that's a bit unintuitive. Best to read Data Lifecycle once a week until you fully understand all the nuances.
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 “Modding help”