This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Mr_Pyro's profile picture

[1.12.2] Reduce Lag?

Started by Mr_Pyro, 31 December 2017 - 03:09 AM
Mr_Pyro #1
Posted 31 December 2017 - 04:09 AM
Hi, I created the program below to place glowstone blocks to light up a room. You give it [slot 1] block to place [slot 2] fuel [slot 3 & 4] blocks its allowed to break (torches or other temporary lighting solutions) [slot 14-16] extra glowstone or blocks that match what was placed in slot 1. It works well with only 1 or 2 small bugs, but it causes a lot of lag. Are there any ways to make it more efficient and cause less lag?


local turn = 'left'
local lightBlock
function moveSpace(n)
print("Log:moveSpace: Called " .. tostring(n))
local i = 0
while i < n do
  if turtle.detect() then
   print("Log:moveSpace: Block In Front of Turtle")
   if canForward() then
    print("Log:moveSpace: Block is a Breakable")
    turtle.dig()
    turtle.forward()
    i = i + 1
   end
  else
   turtle.forward()
   i = i + 1
  end
end
print("Log:moveSpace: Done")
end
function logLightBlock()
if turtle.getItemCount(1) ~= 0 then
  lightBlock = turtle.getItemDetail(1).name
elseif turtle.getItemCount(14) ~= 0 then
  lightBlock = turtle.getItemDetail(14).name
elseif turtle.getItemCount(15) ~= 0 then
  lightBlock = turtle.getItemDetail(15).name
elseif turtle.getItemCount(16) ~= 0 then
  lightBlock = turtle.getItemDetail(16).name
else
  print("------------")
  print("Error: Unable to properly log the light block!! Program may not work as intended.")
  print("------------")
  sleep(4)
end
end
function restockMainSlot()
if turtle.getItemCount(1) == 0 and lightBlock ~= nil then
  if turtle.getItemCount(14) ~= 0 then
   if turtle.getItemDetail(14).name == lightBlock then
    turtle.select(14)
    turtle.transferTo(1)
    turtle.select(1)
  
   elseif turtle.getItemCount(15) ~= 0 then
    if turtle.getItemDetail(15).name == lightBlock then
	 turtle.select(15)
	 turtle.transferTo(1)
	 turtle.select(1)
  
    elseif turtle.getItemCount(16) ~= 0 then
	 if turtle.getItemDetail(16).name == lightBlock then
	  turtle.select(16)
	  turtle.transferTo(1)
	  turtle.select(1)
	 end
    end
   end
  end
end
end
function canForward()
if not turtle.detect() then
  return true
end
turtle.select(3)
if turtle.compare() == true then
  turtle.select(1)
  return true
else
  turtle.select(4)
  if turtle.compare() == true then
   turtle.select(1)
   return true
  else
   turtle.select(1)
   return false
  end
end
end
function checkStock()
if turtle.getItemCount(1) == 0 then
  restockMainSlot()
  while turtle.getItemCount(1) == 0 do
   sleep(5)
   print("Log:replace: Resources Needed")
  end
end
end
function replace()
turtle.select(5)
if turtle.compareDown() or not turtle.detectDown() then
  print("Log:replace: Replacing Block Below")
  turtle.digDown()
  turtle.select(1)
  checkStock()
  turtle.placeDown()
else
  print("Log:replace: Unable to Replace Block Below")
  checkStock()
  turtle.select(1)
end
end
function checkFuel()
if turtle.getFuelLevel() < 100 then
  turtle.select(2)
  print("Log:checkFuel: Fuel Low, Refueling")
  turtle.refuel()
  turtle.select(1)
end
end
function makeTurn(dir)
if not canForward() then
  if dir == 'left' then
   turtle.turnLeft()
  elseif dir == 'right' then
   turtle.turnRight()
  end
  if not canForward() then
   if dir == 'right' then
    turtle.turnLeft()
   elseif dir == 'left' then
    turtle.turnRight()
   end
   if dir == 'right' then
    turtle.turnLeft()
   elseif dir == 'left' then
    turtle.turnRight()
   end
   if not canForward() then
    return '0'
   else
    moveSpace(5)
    if dir == 'right' then
	 turtle.turnLeft()
    elseif dir == 'left' then
	 turtle.turnRight()
    end
    replace()
    return oppDir(dir)
   end
  else
   moveSpace(5)
   if dir == 'left' then
    turtle.turnLeft()
   elseif dir == 'right' then
    turtle.turnRight()
   end
   replace()
   return oppDir(dir)
  end
else
  return '0'
end
end
function oppDir(dir)
if dir == 'left' then
  return 'right'
elseif dir == 'right' then
  return 'left'
end
end
logLightBlock()
replace()
while true do
if canForward() == true then
  moveSpace(5)
  replace()
  checkFuel()
else
  turn = makeTurn(turn)
  if turn == '0' then
   return false
  end
end
end
CLNinja #2
Posted 31 December 2017 - 03:13 PM
replace all sleeps with sleep(0) it'll make it as fast as possible. Only sleep where you're in a loop (as thats where you really need the yield)
SquidDev #3
Posted 31 December 2017 - 03:43 PM
replace all sleeps with sleep(0) it'll make it as fast as possible. Only sleep where you're in a loop (as thats where you really need the yield)
The only sleeps in the above code occur when waiting for user input: changing that shouldn't have any effect on how fast the code runs.

If you're talking about in-game lag (rather than in-computer Lag), there doesn't seem to be anything in your code which should cause issues. Minecraft's lighting engine isn't super efficient, but I doubt you're placing lights fast enough for it to become an issue. If it's framerate lag, it might be worth using Shift+F3's graph to see where the issue is. If it's TPS lag (blocks take a while to respond to right clicks, etc…) I'd recommend using a profiling mod to gather more information.
Mr_Pyro #4
Posted 01 January 2018 - 12:37 AM
image (computer not running) 90fps stable (fps lock)

image (computer running) ~60fps not stable keeps dipping and fluctuating due to "unspecified"

i will look into profiling mods
Mr_Pyro #5
Posted 01 January 2018 - 01:02 AM
image
Saldor010 #6
Posted 01 January 2018 - 05:05 AM
Just gonna point out real quick, if you're running it on a server, it's gonna be a lot laggier than on single player.
Mr_Pyro #7
Posted 02 January 2018 - 03:07 AM
its single player
Lupus590 #8
Posted 02 January 2018 - 11:10 AM
Do you still get lag if the turtle doesn't replace the lights but otherwise is running the program?
Bomb Bloke #9
Posted 02 January 2018 - 12:54 PM
… and what if you simply have the turtle roam around using a simple loop (forward, turn left, forward, turn left, forward, turn left…)? Odds are you can at least narrow this down to the use of a specific function.

In terms of tweaking the above script, while I doubt it'll affect your frame rate at all, a few optimisations come to mind. In particular, I don't like your canForward() function - if you have access to turtle.getItemDetail() then you have access to turtle.inspect(), so there's no need for all this slot-changing and block-comparing every single time the turtle wants to move anywhere. Let's reduce it:

local diggableBlocks = {["minecraft:dirt"] = true, ["minecraft:stone"] = true, ["minecraft:gravel"] = true} -- Add whatever blocks you like to the list.

function canForward()
	local found, block = turtle.inspect()
	
	if not found then return true end
	
	return diggableBlocks[block.name]  -- Returns either true or nil, depending on whether the name exists in the table or not.
end
Mr_Pyro #10
Posted 11 January 2018 - 11:11 PM
Do you still get lag if the turtle doesn't replace the lights but otherwise is running the program?

yes

… and what if you simply have the turtle roam around using a simple loop (forward, turn left, forward, turn left, forward, turn left…)? Odds are you can at least narrow this down to the use of a specific function.

In terms of tweaking the above script, while I doubt it'll affect your frame rate at all, a few optimisations come to mind. In particular, I don't like your canForward() function - if you have access to turtle.getItemDetail() then you have access to turtle.inspect(), so there's no need for all this slot-changing and block-comparing every single time the turtle wants to move anywhere. Let's reduce it:

local diggableBlocks = {["minecraft:dirt"] = true, ["minecraft:stone"] = true, ["minecraft:gravel"] = true} -- Add whatever blocks you like to the list.

function canForward()
	local found, block = turtle.inspect()
	
	if not found then return true end
	
	return diggableBlocks[block.name]  -- Returns either true or nil, depending on whether the name exists in the table or not.
end


while true do
	 turtle.turnleft()
end

drops my fps down to 70 and anything with computercraft is laggy temp reduced frames while in computercraft ui, any turtle action reduced frames …

yeah that was a temp solution to allow the program to be adapted to work in a variety of situations (ver 0.0.1) the plan was to rewrite the program to go through a setup process (have the user place blocks in front and log them instead of using inv slots)

-sorry for taking so long to reply
Edited on 11 January 2018 - 10:12 PM
Lupus590 #11
Posted 12 January 2018 - 11:48 AM
What if the turtle breaks and replaces one light without moving?

Given that you had lag when the turtle didn't replace the light, I think it might be the turtle moving. The above request is to double check.
Mr_Pyro #12
Posted 12 January 2018 - 09:47 PM
What if the turtle breaks and replaces one light without moving?

Given that you had lag when the turtle didn't replace the light, I think it might be the turtle moving. The above request is to double check.


while true do
	 turtle.digDown()
	 turtle.placeDown()
end

fps drops down to 70 fps
Lupus590 #13
Posted 14 January 2018 - 10:59 PM
Is the issue still present without other mods? Especially mods which effect rendering ect. such as Optifine, Fastcraft, FPSBoost, ect.
Mr_Pyro #14
Posted 14 January 2018 - 11:47 PM
Is the issue still present without other mods? Especially mods which effect rendering ect. such as Optifine, Fastcraft, FPSBoost, ect.

I updated all the mods to the current versions. Optifine was not updated but ctm was. The blocks that I used as the floor is a connected texture block from chisel. After the mods were updated it does not lag as much as before while the turtle is running. I have also noticed something else. No mater what my fps is locked at it always only drops about 10-15 fps (locked 120 -> 110-105, locked 180 -> 170-165 etc.)
Lupus590 #15
Posted 15 January 2018 - 11:44 AM
So you have not tested this without other mods yet? I would recomend that you do, it may not be computercraft which is the root cause of the issue.
Mr_Pyro #16
Posted 15 January 2018 - 10:48 PM
So you have not tested this without other mods yet? I would recomend that you do, it may not be computercraft which is the root cause of the issue.

yes i tested it without ctm and optifine no change
Lupus590 #17
Posted 16 January 2018 - 11:00 AM
Uninstall every mod other than CC.
SquidDev #18
Posted 16 January 2018 - 11:03 AM
Uninstall every mod other than CC.
The one and only way to play Minecraft.

In all seriousness, do you think you could upload your mod pack and world somewhere? I'd be happy to run a profiler over it and see what's at fault. I don't think it's CC, but you never know with modded…
Mr_Pyro #19
Posted 17 January 2018 - 03:07 AM
Uninstall every mod other than CC.
The one and only way to play Minecraft.
In all seriousness, do you think you could upload your mod pack and world somewhere? I'd be happy to run a profiler over it and see what's at fault. I don't think it's CC, but you never know with modded…
um ok
[twitch profile version]
http://www.mediafire.com/file/d3y5afehvvbaduz/Direwolf20_Tech_Only_1.12-1.4.0.zip
[non-twitch version]
http://www.mediafire.com/folder/4khaq1spf6fhg/Minecraft_Mod_Pack_1.12_(DireWolf)
Edited on 17 January 2018 - 02:09 AM