Jump to content

Starter GlassCutter


Recommended Posts

UnlockRecipe(recipe_name) is what happens what you prototype a recipe, so I guess that's not what you're after.

The "glasscutter" recipe requires you to have moon altar tech proficiency, so if your character does not have that innately, they cannot create it without being near a prototyper that provides that proficiency. So, what do you want to do? Give your character innate moon altar proficiency (which enables him to always be able to craft glasscutters)? That solution is probably not viable, since the moon altar tab would then have to be active all the time. Another thing you can do, is simply make a duplicate recipe for "glasscutters", which only your character can craft.

I'm going to copy and extrapolate on code from an old post I made about duplicating recipes, to make the code more customizable.
 

DUPLICATING RECIPES, INCL. MAKING THEM CHARACTER-SPECIFIC AND OTHER THINGS

The function below can duplicate any recipe. The only required parameters are the name of the original recipe and a unique suffix (just make one up).

The only thing that isn't explained in the comments, is the optional_custom_spacing parameter. Only placeable recipes (e.g., buildings like the Science Machine) use the spacing parameter. It determines how much space is required around them when they're placed. Unless you have a specific plan concerning that, I would just leave it at the same value as the original recipe, which in this case means you don't fill it out.

Spoiler

local function GenerateDuplicateRecipe(original_recipe, unique_suffix, optional_custom_ingredients, optional_custom_tab,
optional_custom_tech_level, optional_custom_number_to_give, optional_custom_builder_tag,
optional_custom_nounlock, optional_custom_spacing)
	-- Fetch the original recipe, and make sure it is actually there.
	local orig_recipe = GLOBAL.AllRecipes[original_recipe]
	if not orig_recipe then
		print("A recipe for "..original_recipe.." does not exist!")
		return
	end
	
	-- Create our new unique prefab name, using the unique_suffix.
	local new_prefab_name = original_recipe.."_"..unique_suffix
	
	-- In the recipe class, the given ingredients are split into three separate lists:
	-- * Character ingredients, e.g., max-health cost or sanity cost.
	-- * Tech ingredients, which are only used for sculpting recipes.
	-- * Any other ingredients, e.g., items.
	-- If we have not provided custom ingredients, we have to merge the original ones.
	-- Note that we just repurpose the optional_custom_ingredients parameter variable.
	if optional_custom_ingredients == nil then
		optional_custom_ingredients = {}
		for k,v in pairs(orig_recipe.character_ingredients) do
			table.insert(optional_custom_ingredients, v)
		end
		for k,v in pairs(orig_recipe.tech_ingredients) do
			table.insert(optional_custom_ingredients, v)
		end
		for k,v in pairs(orig_recipe.ingredients) do
			table.insert(optional_custom_ingredients, v)
		end
	end
	
	-- The parameter list for constructing a recipe, which is mirrored by the AddRecipe function:
	-- AddRecipe(name, ingredients, tab, level, placer, min_spacing, nounlock,
	-- numtogive, builder_tag, atlas, image, testfn, product)

	-- Add a new recipe, reusing everything from the original recipe, except our custom parameters.
	local new_prefab_recipe = AddRecipe(
		new_prefab_name,
		optional_custom_ingredients,
		optional_custom_tab or orig_recipe.tab,
		optional_custom_tech_level or orig_recipe.level,
		orig_recipe.placer,
		optional_custom_spacing or orig_recipe.min_spacing,
		optional_custom_nounlock or orig_recipe.nounlock,
		optional_custom_number_to_give or orig_recipe.numtogive,
		optional_custom_builder_tag or orig_recipe.builder_tag,
		orig_recipe.atlas,
		orig_recipe.imagefn or orig_recipe.image,
		orig_recipe.testfn,
		original_recipe)
	
	-- To make our recipe not end up at the bottom of the crafting tab, we inherit the sortkey,
	-- but add a little so it appears underneath the original recipe.
	new_prefab_recipe.sortkey = orig_recipe.sortkey + 0.5
	
	-- Duplicate the name- and description-strings of the original recipe.
	local upper_new_prefab = string.upper(new_prefab_name)
	local upper_prefab = string.upper(original_recipe)

	-- These three lines do not really have a function, but I will leave them in for good measure.
	-- Their original purpose from the other author, was to fill out the NAMES and RECIPE_DESC
	-- using the data from the original recipe, but the crafting tab pop-up actually reads the name
	-- and description using the "product" parameter, which is the name of the original prefab,
	-- so the duplicate recipes will always have the name and description of the original prefab.
	-- You cannot rename or change the description of the duplicate recipe in this way, unless
	-- you do more intrusive code.
	local STRINGS = GLOBAL.STRINGS
	STRINGS.NAMES[upper_new_prefab] = STRINGS.NAMES[upper_prefab]
	STRINGS.RECIPE_DESC[upper_new_prefab] = STRINGS.RECIPE_DESC[upper_prefab]
end


-- EXAMPLE USAGE

-- You can find the original recipes in recipes.lua.
-- Here we generate the duplicates for the blowdart_pipe prefab. It can only be crafted using feather_robin_winter,
-- but we want to generate duplicate recipes, so we can create the same blowdart_pipe item using the other feathers.
-- We also change the other recipes, using all of our custom parameters.

-- This first one only changes the ingredients, so it uses feather_canary instead of feather_robin_winter.
GenerateDuplicateRecipe(
	"blowdart_pipe", -- original_recipe
	"feather_canary", -- unique_suffix; makes your recipe name unique. Does not show anywhere ingame!
	{Ingredient("cutreeds", 2),Ingredient("houndstooth", 1),Ingredient("feather_canary", 1) } -- ingredients
)

-- This second one, we change the tab to be TOOLS (so the recipe will show up in the tools-tab
-- instead) and change the tech level requirements so it requires an Alchemy Machine (SCIENCE_TWO),
-- instead of only a Science Machine (SCIENCE_ONE).
GenerateDuplicateRecipe(
	"blowdart_pipe", -- original_recipe
	"feather_robin", -- unique_suffix; makes your recipe name unique. Does not show anywhere ingame!
	{Ingredient("cutreeds", 2),Ingredient("houndstooth", 1),Ingredient("feather_robin", 1) }, -- ingredients
	RECIPETABS.TOOLS, -- optional_custom_tab
	TECH.SCIENCE_TWO -- optional_custom_tech_level
)

-- This third one we change so it gives 3 of the product, can only be crafted by (and only shows
-- up for) the character prefab "johnny", and set it so it does not need to be prototyped.
-- Notice that we put "nil" for the tab and tech level, so those are copied from the original.
GenerateDuplicateRecipe(
	"blowdart_pipe", -- original_recipe
	"feather_crow", -- unique_suffix; makes your recipe name unique. Does not show anywhere ingame!
	{Ingredient("cutreeds", 2),Ingredient("houndstooth", 1),Ingredient("feather_crow", 1) }, -- ingredients
	nil, -- optional_custom_tab
	nil, -- optional_custom_tech_level
	3, -- optional_custom_number_to_give
	"johnny", -- optional_custom_builder_tag; only characters with this tag can see and use this recipe.
	true -- optional_custom_nounlock; whether the recipe needs to be prototyped. "true" means no.
)

 


NOTE THAT THIS IS ONLY FOR DST! If you want to know how to make a recipe character-specific in DS, look at this post.

Also, if you want to make a custom item not be able to be picked up by any other character than your custom character, you can also find that in this post.

Edited by Ultroman
Link to comment
Share on other sites

On 2019-09-19 at 1:01 AM, Ultroman said:

The "glasscutter" recipe requires you to have moon altar tech proficiency, so if your character does not have that innately, they cannot create it without being near a prototyper that provides that proficiency. So, what do you want to do? Give your character innate moon altar proficiency (which enables him to always be able to craft glasscutters)? That solution is probably not viable, since the moon altar tab would then have to be active all the time.

I'd like to do that but you aren't elaborating how

Link to comment
Share on other sites

15 hours ago, Well-met said:

I'd like to do that but you aren't elaborating how

That's because it seems like a bad idea, since all other special crafting tabs would be fighting with the special Moon Altar tab about which one should be on top. And also, your character would be able to craft ALL Moon Altar recipes using this method. If you want to try it out, this should do it.

inst:ListenForEvent("techtreechange", function(inst, data)
	data.level.MOON_ALTAR = 2
end)

I am not 100% sure how this will work, but it should work. I'm just not sure if it will even pop out the Moon Altar tab for you. I highly recommend instead making a new recipe for the glasscutter prefab, with a builder_tag unique to your character. That will 100% work, using the code I provided above.

Link to comment
Share on other sites

The last thing I posted, you put in your character's master_postinit function.

The code for duplicating a recipe is really only that long because I wrote a ton of comments in it to help you understand it. You take the whole GenerateDuplicateRecipe function and put it at the bottom of your modmain.lua. Then, below it, you use it, like the examples I included. It's really not as scary as it might seem at first :) Study it. Read my comments. It should all make sense after a while.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...