Page 1 of 1

[.18.34] Recipe categories scroll reset

Posted: Sat Jul 18, 2020 4:12 pm
by micromario
I am still getting this issue:
viewtopic.php?t=82492

but only when I call
player.cancel_crafting() from a mod

this bug was reported for my better crafting queue mod here
https://mods.factorio.com/mod/better-cr ... b6195d4897

Re: [.18.34] Recipe categories scroll reset

Posted: Tue Jul 21, 2020 3:43 pm
by Rseding91
I can't reproduce. When I use cancel_crafting from the console it doesn't reset the scroll bar for me.

What else are you doing API wise? From what I can see, the only time when it will scroll up is if a recipe is enabled/disabled or the active group tab is changed.

Re: [.18.34] Recipe categories scroll reset

Posted: Tue Jul 21, 2020 4:34 pm
by micromario

Code: Select all

local function score(recipe)
	local recipe_prototype = game.recipe_prototypes[recipe.recipe]
	local result = game.item_prototypes[recipe_prototype.products[1].name]

	local score = 10

	if result.attack_parameters then -- guns
		score = 13000
	elseif result.magazine_size then -- bullets
		score = 10000
	elseif result.resistances then -- armor
		score = 7000
	elseif result.speed then -- repair kit and mining tools
		score = 3000
	--[[
	elseif result.fuel_category then -- solid fuel
		score = 1800
	--]]
	elseif result.capsule_action then -- capsules
		score = 1000
	elseif result.place_result then -- buildings
		score = 500
	elseif result.type == 'tool' then -- science packs
		score = 320
	elseif result.place_as_tile_result then -- tiles
		score = 80
	elseif result.module_effects then -- modules
		score = 1
	end
	
	return score / math.sqrt(recipe_prototype.energy) / recipe.count
end

script.on_event(defines.events.on_pre_player_crafted_item, function(event)
	global.busy = global.busy or {}
	local busy = global.busy
	local pid = event.player_index
	
	if busy[pid] then return end
	local player = game.players[pid]
	if player.crafting_queue_size == 1 then return end
	
	local inventory_size = player.character_inventory_slots_bonus
	player.character_inventory_slots_bonus = 10 * inventory_size + 2000

	local current = player.crafting_queue[#player.crafting_queue]
	local current_score = score(current)
	
	player.cancel_crafting({index=current.index, count=current.count})
	
	local cache = {}
	while player.crafting_queue do
		local this = player.crafting_queue[#player.crafting_queue]
		if this.recipe == current.recipe or score(this) > current_score then break end
		cache[#cache + 1] = this
		player.cancel_crafting({index=this.index, count=this.count})
	end
	
	busy[pid] = true
	
	player.begin_crafting{count=current.count, recipe=current.recipe}

	for i = #cache, 1, -1 do
		local v = cache[i]
		player.begin_crafting({count = v.count, recipe = v.recipe})
	end

	player.character_inventory_slots_bonus = inventory_size

	busy[pid] = nil
end)
I have attached the full source code for the mod
The API functions I'm using are
game.players[]
game.recipe_prototypes[]
game.item_prototypes[]
LuaPlayer.cancel_crafting
LuaPlayer.begin_crafting
LuaPlayer.character_inventory_slots_bonus =
LuaPlayer.crafting_queue[]

Now that I think about it its very possible that setting inventory size would reset scroll

Re: [.18.34] Recipe categories scroll reset

Posted: Tue Jul 21, 2020 4:55 pm
by Rseding91
Yes, changing the inventory size reloads the entire GUI which as a side effect resets the scroll location. That's just how it has to work.

Re: [.18.34] Recipe categories scroll reset

Posted: Tue Jul 21, 2020 4:57 pm
by micromario
Thank you for letting me know. I'll try to find a solution that does not involve resizing the inventory