Jump to content




Save lua state with game saves.


  • You cannot reply to this topic
27 replies to this topic

#21 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 21 March 2013 - 08:45 PM

1) Record your intention to move in the file, and the direction you are moving in.
<-- if interrupted here, coordinates are valid and turtle hasn't moved
2) Request the turtle to move with turtle.native.
<-- if interrupted here, the turtle will fire a success or failure event after it restarts, and coordinates are wrong if it succeeded.
3) Wait 0.05 (or 0.1?) seconds for a failure event. If you get one, go to step 6
<-- if interrupted here, the turtle will fire a success event after it restarts, and coordinates are wrong if it succeeded.
4) Update your coordinates. Record the fact that you haven't received a success event yet.
<-- if interrupted here, the turtle will fire a success event after it restarts, and coordinates are valid.
5) Wait for a success event.
<-- if interrupted here, the turtle will not fire an event after it restarts, and coordinates are valid.
6) Reset the file.


When loading:
if file has "intention to move" flag then
  if file has "updated coordinates but didn't receive success event" flag then:
	wait for success event, failure event or one second
	if failure event then
	   crash - something screwed up
	end
  else
	wait for success event, failure event or one second
	if timed out then
	   crash - something screwed up
	end
	if success event then
	  update coordinates with direction of movement
	end
  end
end

Btw, that's three file writes per block moved.

#22 PonyKuu

  • Members
  • 215 posts

Posted 21 March 2013 - 08:49 PM

View PostTheOriginalBIT, on 21 March 2013 - 08:44 PM, said:

- turtle 1 changes its pos in file and then moves
Are you sure that turtle wouldn't yield between those things?

#23 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 March 2013 - 08:53 PM

View PostPonyKuu, on 21 March 2013 - 08:49 PM, said:

Are you sure that turtle wouldn't yield between those things?
Positive.

Things that yield

Quote

sleep
os.pullEvent
os.pullEventRaw
turtle api, NOT turtle.native
... I feel like there is another one I'm forgetting but cannot remember at this time...

if you don't believe me run this code
while true do local h = fs.open('this', 'w') h.write('this') h.close() end
it will error with the 'too long without yielding' after ~10 seconds.

#24 PonyKuu

  • Members
  • 215 posts

Posted 21 March 2013 - 08:54 PM

Oh, Immibis... That's cool, but a bit... complicated. And I have no Idea what turtle.native is... Wiki doesn't have such page. Is it a some sort of low-level turtle function?

#25 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 March 2013 - 08:56 PM

yes turtle.native is the low level. its what actually tells the Java to move the turtle and such.
the turtle api adds this over the native

local function waitForResponse( _id )
  local event, responseID, success
  while event ~= "turtle_response" or responseID ~= _id do
	event, responseID, success = os.pullEvent( "turtle_response" )
  end
  return success
end


local function wrap( _sCommand )
  return function( ... )
	local id = native[_sCommand]( ... )
	if id == -1 then
	  return false
	end
	return waitForResponse( id )
  end
end


for k,v in pairs( native ) do
  if type( k ) == "string" and type( v ) == "function" then
	if turtle[k] == nil then
	  turtle[k] = wrap( k )
	end
  end
end

wiki doesn't have the page because they don't want you to know about the natives :P (there is more than just turtle.native, like term.native)

#26 PonyKuu

  • Members
  • 215 posts

Posted 21 March 2013 - 08:59 PM

Oh, I see. Thank you for the explanation and stuff.
But all that doesn't mean that Lua state saving on chunk/world reload is a bad idea. ^_^'

#27 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 22 March 2013 - 03:41 AM

It's already planned, it's just not easy, given the current state of things.

#28 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 22 March 2013 - 06:00 AM

And for what I saw in the decompiled sources, they already started work on this (not sure since which version). I don't know if it's even working, but there's some work done to use native lua libraries and the pluto library (used to save the lua state).

(Sorry if it was a secret :P)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users