Random outputs?

Place to get help with not working mods / modding interface.
User avatar
Dysoch
Filter Inserter
Filter Inserter
Posts: 445
Joined: Fri Oct 18, 2013 2:27 pm
Contact:

Random outputs?

Post by Dysoch »

Is it possible to input an item in the furnace, and have 1 of 5 products come out?
Something like this (this is probably a very wrongly written line of code )

Code: Select all

Function
1="ruby-1"
2="topaz-1"
3="sapphire-1"
4="diamond-1"
5="emerald-1"
End

Recipe:
Result = function.math.random(1,5)
Result_count = math.random(1,3)
I know, badly written, but you get my point. (I hope)
Creator of:
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Random outputs?

Post by FreeER »

so something like receiving a 'mystery ore'? Hmm...Not that I can think of. Well, of course it's possible with generic container entity and scripting etc., but not that I can think of otherwise if you want it to have a random chance upon each smelting. You could do it in the prototypes but that would choose one at load time and not change until Factorio is closed and reopened (maybe with resetrecipes?).
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net
User avatar
Dysoch
Filter Inserter
Filter Inserter
Posts: 445
Joined: Fri Oct 18, 2013 2:27 pm
Contact:

Re: Random outputs?

Post by Dysoch »

FreeER wrote:so something like receiving a 'mystery ore'? Hmm...Not that I can think of. Well, of course it's possible with generic container entity and scripting etc., but not that I can think of otherwise if you want it to have a random chance upon each smelting. You could do it in the prototypes but that would choose one at load time and not change until Factorio is closed and reopened (maybe with resetrecipes?).
if possible with resetrecipes, i could just simply add an ontick event with game.force.player.resetrecipes() that runs every minute or so and be done with it :P
but i figure the devs might know better, lets just hope they say its possible. :P
Creator of:
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
User avatar
Dysoch
Filter Inserter
Filter Inserter
Posts: 445
Joined: Fri Oct 18, 2013 2:27 pm
Contact:

Re: Random outputs?

Post by Dysoch »

Code: Select all

function randomresult()
	if math.random(1,5)==1 then "ruby-1",
	else math.random(1,5)==2 then "emerald-1",
	else math.random(1,5)==3 then "topaz-1",
	else math.random(1,5)==4 then "diamond-1",
	else math.random(1,5)==5 then "sspphire-1",
	end
end
this is the function, it gives an unexpected symbol error around "ruby-1",

Code: Select all

{
    type = "recipe",
    name = "sand-110",
	category = "gem-grinding",
	energy_required = 50,
    ingredients =
    {
	  {"stone", 1},
    },
    result = randomresult()
  },
the recipe, its just for testing, but it will be changed when its working
Creator of:
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Random outputs?

Post by FreeER »

that is because it doesn't know what to do with a string :)
I'd use a table gem_results={"ruby-1", "emerald-1", "topaz-1", "diamond-1", "sapphire-1"}
then in the recipe use result=gem_results[math.random(#gem_results)]. That would allow you to add more to the table quite easily. Of course if you really wanted a function you'd use something like

Code: Select all

function random_gem()
  local gem = math.random(5)
  if gem == 1 then return "ruby-1"
  elseif gem == 2 then return "emerald-1"
  ...
  end
end
But then you have more to write for each new gem you might add later and you have to remember to update math.random's range (or that new gem wouldn't be used)
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net
User avatar
Dysoch
Filter Inserter
Filter Inserter
Posts: 445
Joined: Fri Oct 18, 2013 2:27 pm
Contact:

Re: Random outputs?

Post by Dysoch »

Code: Select all

{
    type = "recipe",
    name = "sandid",
	category = "smelting",
	energy_required = 50,
    ingredients =
    {
	  {"sand", 1},
    },
    result = gem_results[math.random(#gem_results)]
  },
and

Code: Select all

gem_results={"ruby-1", "emerald-1", "topaz-1", "diamond-1", "sapphire-1"}
result in no errors, but the recipe isnt showing up anymore (assembling wise) and not activated when smelting
ill give it a try with the function

edit, nope function isnt working either. the recipe is not activated
Creator of:
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
SilverWarior
Filter Inserter
Filter Inserter
Posts: 559
Joined: Mon Mar 04, 2013 9:23 am
Contact:

Re: Random outputs?

Post by SilverWarior »

I don't think you can achieve this only by changing item recipie. Why? As far as I know recipies are just record informations and not classes. This means that they simply store constant data in more organized manner.

To be able to achieve this developers would have to change the furnace class first as it is curently build in a way to only support only one item output per recipie.
User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Random outputs?

Post by FreeER »

SilverWarior wrote:I don't think you can achieve this only by changing item recipie. Why? As far as I know recipies are just record informations and not classes. This means that they simply store constant data in more organized manner.

To be able to achieve this developers would have to change the furnace class first as it is curently build in a way to only support only one item output per recipie.
Well he's not (from what I read) trying to output more than one item type at a time, he wants a random one to be chosen (rather like the enemy spawner only spawns one unit at a time, but it is 'random' as to which, obviously not best example because spawner is affected by pollution and evolution factor but). The problem is that the recipe is only checked when Factorio loads, thus it picks one result item and doesn't change until Factorio is restarted. If resetrecipes could be used to force Factorio to reload that (and rerun math.random) then you'd effectively get a random output, but still only a single type at any one smelting.

assuming a function worked:

Code: Select all

function RandomResult(resources)
  return resources[math.random(#resources)]
end
Would probably be the easiest function, simply pass it a table of item names and it'll randomly return one.

But if this ever works it'll probably be because the devs modified it to work similar to the spawner, aka give it a table of {name, chance}s with chance adding up to 1. Multiple tables would give multiple outputs, aka result = {{{ruby-poor, 0.8},{"ruby-high", 0.2}}, {"stone", 1}} which gives 80% chance for poor quality ruby, 20% for high chance and 100% chance of stone. Then technology modifiers could exist to improve the ruby-high chance upon research. just a guess as to a possibility
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net
SilverWarior
Filter Inserter
Filter Inserter
Posts: 559
Joined: Mon Mar 04, 2013 9:23 am
Contact:

Re: Random outputs?

Post by SilverWarior »

FreeER wrote:Well he's not (from what I read) trying to output more than one item type at a time, he wants a random one to be chosen (rather like the enemy spawner only spawns one unit at a time
Yes I know that. But the problem with current implementation is that only one posible product type is read from the item recipie.
So yes I advise developers to change this so that recipie can specify several posible product types for single recipie (posible outproducts are stored in table).
And then you can easily controll wheter you want the furnace/some other machine to randomy produce one of posible product types or multiple product types at once.
Ability to get multiple products with single recipie has already been requested some time ago.
User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Random outputs?

Post by FreeER »

SilverWarior wrote:But the problem with current implementation is that only one posible product type is read from the item recipie.
Yes that is the problem, I replied to your initial one because you said
SilverWarior wrote: To be able to achieve this developers would have to change the furnace class first as it is curently build in a way to only support only one item output per recipie.
Slight difference but important. The furnace is not limited to a single output, it can have multiple input and output slots (if changed in the prototype). It's the actual recipe mechanic that is limited. Of course I've no idea exactly how much would have to be changed for this to work without issues, they'd first have to allow multiple results (and random results), then they'd have to decide how to insert those items into the furnace (if it has one slot is it even able to use a recipe with multiple outputs, or does it place the first in the output slot and the rest in a buffer?). Of course they'd also have to modify assembling machines as well (though in a similar manner). and what about mining results, could you mine ores and also receive stone (or stone fragments)? If so you'd also have to modify the mining drill. It's not straight forward as I'm sure you know, but there is a bit more to it than some others (with less experience or hadn't thought it through) may believe.

And since this is in Modding Help rather than Interface Requests (at the moment), I attempted to give a possible method to achieve it with the current state of mechanics. It didn't work unfortunately but, I tried :)
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net
User avatar
Dysoch
Filter Inserter
Filter Inserter
Posts: 445
Joined: Fri Oct 18, 2013 2:27 pm
Contact:

Re: Random outputs?

Post by Dysoch »

while thinking about the new biters i added and their spawn rates, something came to me.

The game already has a random output. granted, it has a factor of evolution to spawn biters, but it could be easily modified for items.
something like:

Code: Select all

    result = (function()
                     local res = {}
                     res[1] = {"ruby-1", 0.30}
                     res[2] = {"emerald-1", 0.20}
                     res[3] = {"sapphire-1", 0.20}
		               res[4] = {"topaz-1", 0.20}
		               res[5] = {"diamond-1", 0.10}
                     return res
                   end)(),
and it could be modified to have a random result_count:
                     res[1] = {"ruby-1", result_count = math.random(3), 0.30}
havent tested it, family just came xD ill test tomorrow
Creator of:
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
User avatar
Dysoch
Filter Inserter
Filter Inserter
Posts: 445
Joined: Fri Oct 18, 2013 2:27 pm
Contact:

Re: Random outputs?

Post by Dysoch »

Tested it, and it doesnt work :(
Hoping the devs can fix this so it would work without going throu a script and chest.
Creator of:
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
SilverWarior
Filter Inserter
Filter Inserter
Posts: 559
Joined: Mon Mar 04, 2013 9:23 am
Contact:

Re: Random outputs?

Post by SilverWarior »

It doesn't work becouse Furnace code expects to be able to read constant value out of item recipie and doesn't expect that there is a method which returns such value instead.
kovarex
Factorio Staff
Factorio Staff
Posts: 8207
Joined: Wed Feb 06, 2013 12:00 am
Contact:

Re: Random outputs?

Post by kovarex »

If we made recipes with multiple/random results (similar to loot definition probably) there would have to be special rules about these kind of recipes:
  • The recipe wouldn't be used for intermediate crafting automatically
  • The recipe would have to have custom icon name and description specified
  • I thought there were going to be more problems :)
User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Random outputs?

Post by FreeER »

kovarex wrote:If we made recipes with multiple/random results (similar to loot definition probably) there would have to be special rules about these kind of recipes:
  • [*] The recipe wouldn't be used for intermediate crafting automatically
    Makes sense
    [*] The recipe would have to have custom icon name and description specified
    Hm, not sure what you mean by custom icon name...Would that make the icon any different? If all results were given using a table the code should be able to easily say, if table.length > 1 then multi, else regular. You could also, I would think, make the results also show in the pop where the ingredients do now. That should prevent having to create recipes differently if they are multi vs singular result, though it would obviously require more changes...
    [*] I thought there were going to be more problems :)
    Me too :)
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net
User avatar
Dysoch
Filter Inserter
Filter Inserter
Posts: 445
Joined: Fri Oct 18, 2013 2:27 pm
Contact:

Re: Random outputs?

Post by Dysoch »

Well im trying to get a recycler working, that works with scrap and scrap metal. It works just fine, but it could use random smeltings result :)

I dont want intermediates with random rsults. You could restrict it only to machines (eg. Recipe categories other then "crafting")
Creator of:
- DyTech
- DyWorld
- DyWorld-Dynamics
- DyWorld-Dynamics 2
Active since Factorio 0.6
SilverWarior
Filter Inserter
Filter Inserter
Posts: 559
Joined: Mon Mar 04, 2013 9:23 am
Contact:

Re: Random outputs?

Post by SilverWarior »

kovarex wrote:The recipe wouldn't be used for intermediate crafting automatically
You already have such recipies. All smelting recipies work only in furnaces.
kovarex wrote:The recipe would have to have custom icon name and description specified[/qote]

Same would go to all smelting recipies right now. I can still remember how hard was for me to figure out hoow to make steel for the first time.
kovarex wrote:I thought there were going to be more problems :)[/list]
Another posible problem that me and FreeER came out when further discusing about this idea through PM would be on how to determine output slots. You can't just use one as each item slot can only contain one item type.
As posible solution you would have to make furnace/asembly machine in a way that it generates output slots based on posible output results.
That would be easily done for assembly machines and multiple item recipies but not so easy for furnaces becouse the furnace chose apropriate recipie based on input resources. But I'm still sure it can be done.
Post Reply

Return to “Modding help”