Add more commands

This commit is contained in:
Novatux 2013-12-07 16:46:01 +01:00
parent 8968e5d875
commit 3f5aaf3d75
6 changed files with 666 additions and 156 deletions

206
api.lua Normal file
View File

@ -0,0 +1,206 @@
local FUEL_EFFICIENCY = 3 -- How many moves can the turtle do with a second of fuel
tl = {}
local function getv(dir)
if dir==0 then return {x=0,y=0,z=1}
elseif dir==1 then return {x=1,y=0,z=0}
elseif dir==2 then return {x=0,y=0,z=-1}
elseif dir==3 then return {x=-1,y=0,z=0} end
end
local function turtle_can_go(nname)
return nname == "air" or minetest.registered_nodes[nname].liquidtype ~= "none"
end
function tl.forward(turtle, cptr)
local info = get_turtle_info(turtle)
if info.fuel == 0 then
cptr.X = 0
return
end
local spos = info.spos
local dir = info.dir
local npos = vector.add(spos, getv(dir))
if turtle_can_go(minetest.get_node(npos).name) then
info.npos = npos
info.moving = true
info.fuel = info.fuel - 1
cptr.X = u16(-1)
cptr.paused = true
else
cptr.X = 0
end
end
function tl.backward(turtle, cptr)
local info = get_turtle_info(turtle)
if info.fuel == 0 then
cptr.X = 0
return
end
local spos = info.spos
local dir = info.dir
local npos = vector.add(spos, getv((dir+2)%4))
if turtle_can_go(minetest.get_node(npos).name) then
info.npos = npos
info.moving = true
info.fuel = info.fuel - 1
cptr.X = u16(-1)
cptr.paused = true
else
cptr.X = 0
end
end
function tl.up(turtle, cptr)
local info = get_turtle_info(turtle)
if info.fuel == 0 then
cptr.X = 0
return
end
local spos = info.spos
local npos = vector.add(spos, {x=0, y=1, z=0})
if turtle_can_go(minetest.get_node(npos).name) then
info.npos = npos
info.moving = true
info.fuel = info.fuel - 1
cptr.X = u16(-1)
cptr.paused = true
else
cptr.X = 0
end
end
function tl.down(turtle, cptr)
local info = get_turtle_info(turtle)
if info.fuel == 0 then
cptr.X = 0
return
end
local spos = info.spos
local npos = vector.add(spos, {x=0, y=-1, z=0})
if turtle_can_go(minetest.get_node(npos).name) then
info.npos = npos
info.moving = true
info.fuel = info.fuel - 1
cptr.X = u16(-1)
cptr.paused = true
else
cptr.X = 0
end
end
function tl.turnleft(turtle, cptr)
local info = get_turtle_info(turtle)
info.ndir = (info.dir+3)%4
info.rotate = math.pi/2
info.moving = true
cptr.paused = true
end
function tl.turnright(turtle, cptr)
local info = get_turtle_info(turtle)
info.ndir = (info.dir+1)%4
info.rotate = -math.pi/2
info.moving = true
cptr.paused = true
end
local function write_string_at(cptr, addr, str)
for i=1, string.len(str) do
cptr[u16(addr-1+i)] = string.byte(str, i)
end
cptr.X = string.len(str)
end
local function turtle_detect(turtle, cptr, dir)
local info = get_turtle_info(turtle)
local pos = vector.add(info.spos, dir)
local name = minetest.get_node(pos).name
write_string_at(cptr, cptr.X, name)
end
function tl.detect(turtle, cptr)
local info = get_turtle_info(turtle)
turtle_detect(turtle, cptr, getv(info.dir))
end
function tl.detectup(turtle, cptr)
turtle_detect(turtle, cptr, {x = 0, y = 1, z = 0})
end
function tl.detectdown(turtle, cptr)
turtle_detect(turtle, cptr, {x = 0, y = -1, z = 0})
end
local function turtle_dig(turtle, cptr, dir)
local info = get_turtle_info(turtle)
local dpos = vector.add(info.spos, dir)
local dnode = minetest.env:get_node(dpos)
if turtle_can_go(dnode.name) or dnode.name == "ignore" then
cptr.X = 0
return
end
local drops = minetest.get_node_drops(dnode.name, "default:pick_mese")
local _, dropped_item
for _, dropped_item in ipairs(drops) do
local leftover = turtle_invs:add_item(turtle,dropped_item)
minetest.add_item(info.spos,leftover)
end
minetest.remove_node(dpos)
cptr.X = u16(-1)
cptr.paused = true
end
function tl.dig(turtle, cptr)
local info = get_turtle_info(turtle)
turtle_dig(turtle, cptr, getv(info.dir))
end
function tl.digup(turtle, cptr)
turtle_dig(turtle, cptr, {x = 0, y = 1, z = 0})
end
function tl.digdown(turtle, cptr)
turtle_dig(turtle, cptr, {x = 0, y = -1, z = 0})
end
local function stack_set_count(stack, count)
stack = stack:to_table()
if stack==nil then return nil end
stack.count=count
return ItemStack(stack)
end
function tl.refuel(turtle, cptr, slot, nmax)
local info = get_turtle_info(turtle)
local stack = turtle_invs:get_stack(turtle, slot)
local fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = {stack}})
if fuel.time <= 0 then
cptr.X = 0
return
end
local count = math.min(stack:get_count(), nmax)
local fs = stack:to_table()
fs.count = 1
local fstack = ItemStack(fs)
local fuel, afterfuel
fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = {fstack}})
stack:take_item(count)
if afterfuel ~= nil then
afterfuel = afterfuel.items[1]
end
if afterfuel ~= nil then
afterfuel = stack_set_count(afterfuel, afterfuel:get_count()*count)
end
if afterfuel ~= nil then
local leftover = stack:add_item(ItemStack(afterfuel))
turtle_invs:set_stack(turtle, slot, stack)
local leftover2 = turtle_invs:add_item(turtle, leftover)
minetest.add_item(info.spos,leftover2)
else
turtle_invs:set_stack(turtle, slot, stack)
end
info.fuel = info.fuel+FUEL_EFFICIENCY*count*fuel.time
cptr.X = u16(FUEL_EFFICIENCY*count*fuel.time)
end

166
cptr.lua
View File

@ -1,6 +1,5 @@
local CYCLES_PER_STEP = 1000
local MAX_CYCLES = 100000
local FUEL_EFFICIENCY = 3 -- How many moves can the turtle do with a second fuel
local function file_exists(name)
local f = io.open(name, "r")
@ -51,25 +50,25 @@ if bit32 == nil then
end
end
local function s16(x)
function s16(x)
if bit32.band(x, 0x8000)~=0 then
return bit32.band(x, 0xffff)-0x10000
end
return bit32.band(x, 0xffff)
end
local function u16(x)
function u16(x)
return bit32.band(x, 0xffff)
end
local function s32(x)
function s32(x)
if bit32.band(x, 0x80000000)~=0 then
return bit32.band(x, 0xffffffff)-0x100000000
end
return bit32.band(x, 0xffffffff)
end
local function u32(x)
function u32(x)
return bit32.band(x, 0xffffffff)
end
@ -154,152 +153,7 @@ local function send_message(turtle, cptr, maddr, mlen)
turtle_receptor_send(turtle, cptr.channel, msg)
end
local function getv(dir)
if dir==0 then return {x=0,y=0,z=1}
elseif dir==1 then return {x=1,y=0,z=0}
elseif dir==2 then return {x=0,y=0,z=-1}
elseif dir==3 then return {x=-1,y=0,z=0} end
end
local function turtle_can_go(nname)
return nname=="air" or minetest.registered_nodes[nname].liquidtype~="none"
end
local tl = {}
function tl.forward(turtle, cptr)
local info = get_turtle_info(turtle)
if info.fuel == 0 then
cptr.X = 0
return
end
local spos = info.spos
local dir = info.dir
local npos = vector.add(spos, getv(dir))
if turtle_can_go(minetest.get_node(npos).name) then
info.npos = npos
info.moving = true
info.fuel = info.fuel - 1
cptr.X = u16(-1)
cptr.paused = true
else
cptr.X = 0
end
end
function tl.backward(turtle, cptr)
local info = get_turtle_info(turtle)
if info.fuel == 0 then
cptr.X = 0
return
end
local spos = info.spos
local dir = info.dir
local npos = vector.add(spos, getv((dir+2)%4))
if turtle_can_go(minetest.get_node(npos).name) then
info.npos = npos
info.moving = true
info.fuel = info.fuel - 1
cptr.X = u16(-1)
cptr.paused = true
else
cptr.X = 0
end
end
function tl.up(turtle, cptr)
local info = get_turtle_info(turtle)
if info.fuel == 0 then
cptr.X = 0
return
end
local spos = info.spos
local npos = vector.add(spos, {x=0, y=1, z=0})
if turtle_can_go(minetest.get_node(npos).name) then
info.npos = npos
info.moving = true
info.fuel = info.fuel - 1
cptr.X = u16(-1)
cptr.paused = true
else
cptr.X = 0
end
end
function tl.down(turtle, cptr)
local info = get_turtle_info(turtle)
if info.fuel == 0 then
cptr.X = 0
return
end
local spos = info.spos
local npos = vector.add(spos, {x=0, y=-1, z=0})
if turtle_can_go(minetest.get_node(npos).name) then
info.npos = npos
info.moving = true
info.fuel = info.fuel - 1
cptr.X = u16(-1)
cptr.paused = true
else
cptr.X = 0
end
end
function tl.turnleft(turtle, cptr)
local info = get_turtle_info(turtle)
info.ndir = (info.dir+3)%4
info.rotate = math.pi/2
info.moving = true
cptr.paused = true
end
function tl.turnright(turtle, cptr)
local info = get_turtle_info(turtle)
info.ndir = (info.dir+1)%4
info.rotate = -math.pi/2
info.moving = true
cptr.paused = true
end
local function stack_set_count(stack, count)
stack = stack:to_table()
if stack==nil then return nil end
stack.count=count
return ItemStack(stack)
end
function tl.refuel(turtle, cptr, slot, nmax)
local info = get_turtle_info(turtle)
local stack = turtle_invs:get_stack(turtle, slot)
local fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = {stack}})
if fuel.time <= 0 then
cptr.X = 0
return
end
local count = math.min(stack:get_count(), nmax)
local fs = stack:to_table()
fs.count = 1
local fstack = ItemStack(fs)
local fuel, afterfuel
fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = {fstack}})
stack:take_item(count)
if afterfuel ~= nil then
afterfuel = afterfuel.items[1]
end
if afterfuel ~= nil then
afterfuel = stack_set_count(afterfuel, afterfuel:get_count()*count)
end
if afterfuel ~= nil then
local leftover = stack:add_item(ItemStack(afterfuel))
turtle_invs:set_stack(turtle, slot, stack)
local leftover2 = turtle_invs:add_item(turtle, leftover)
minetest.add_item(info.spos,leftover2)
else
turtle_invs:set_stack(turtle, slot, stack)
end
info.fuel = info.fuel+FUEL_EFFICIENCY*count*fuel.time
cptr.X = u16(FUEL_EFFICIENCY*count*fuel.time)
end
dofile(modpath.."/api.lua")
function run_computer(turtle, cptr)
if cptr.stopped then return end
@ -440,7 +294,15 @@ ITABLE_RAW = {
[0x64] = "tl.turnleft(turtle, cptr)",
[0x65] = "tl.turnright(turtle, cptr)",
[0x70] = "tl.refuel(turtle, cptr, cptr.X, cptr.Y)",
[0x68] = "tl.detect(turtle, cptr)",
[0x69] = "tl.detectup(turtle, cptr)",
[0x6a] = "tl.detectdown(turtle, cptr)",
[0x70] = "tl.dig(turtle, cptr)",
[0x71] = "tl.digup(turtle, cptr)",
[0x72] = "tl.digdown(turtle, cptr)",
[0x80] = "tl.refuel(turtle, cptr, cptr.X, cptr.Y)",
}
ITABLE = {}

422
f.py Normal file
View File

@ -0,0 +1,422 @@
f = open('forth.fth','r')
lf = f.readlines()
f.close()
df = {}
def setmemory(addr, value):
memory[addr] = value&0xff
memory[addr+1] = (value>>8)&0xff
def getmemory(addr):
return (memory[addr+1]<<8)+memory[addr]
def to_int(x):
if x[:2]=="0x":
try:
a=int(x[2:],16)
return a
except:
return None
try:
a=int(x)
return a
except:
return None
def header(name, lt=None):
global here
global latest
memory[here] = 0
here += 1
for c in name.strip():
memory[here] = ord(c)
here += 1
if lt!=None:
setmemory(here, lt)
else:
setmemory(here, latest)
here += 2
memory[here] = len(name)
here += 1
if lt==None:
latest = here
df[name] = here
def compile_constant(name, value):
value=to_int(value)
global here
header(name)
memory[here] = 77
here += 1
setmemory(here, value)
here += 2
memory[here] = 33
here += 1
memory[here] = 41
here += 1
def env_compile_2constant(name, value):
value=to_int(value)
global here
global env_latest
header(name, env_latest)
env_latest = here
memory[here] = 77
here += 1
setmemory(here, value&0xffff)
here += 2
memory[here] = 33
here += 1
memory[here] = 77
here += 1
setmemory(here, value>>16)
here += 2
memory[here] = 33
here += 1
memory[here] = 41
here += 1
def env_compile_constant(name, value):
value=to_int(value)
global here
global env_latest
header(name, env_latest)
env_latest = here
memory[here] = 77
here += 1
setmemory(here, value)
here += 2
memory[here] = 33
here += 1
memory[here] = 41
here += 1
ITABLE = {
"IPOP":0x28,
"NXT":0x29,
"CALL":0x2a,
"CRX":0x2b,
"TSX":0x08,
"TRX":0x09,
"TPX":0x0a,
"TIX":0x0b,
"BRK":0x00,
"RPHX":0x01,
"RPHY":0x02,
"RPHZ":0x03,
"RPX":0x10,
"RPLX":0x11,
"RPLY":0x12,
"RPLZ":0x13,
"PSX":0x20,
"PHX":0x21,
"PHY":0x22,
"PHZ":0x23,
"SPX":0x30,
"PLX":0x31,
"PLY":0x32,
"PLZ":0x33,
"RXX":0x04,
"RXY":0x05,
"RYX":0x06,
"RYY":0x07,
"CRXX":0x14,
"CRXY":0x15,
"CRYX":0x16,
"CRYY":0x17,
"WXY":0x25,
"WYX":0x26,
"CWXY":0x35,
"CWYX":0x36,
"ADD":0x0c,
"SUB":0x0d,
"MUL":0x0e,
"SMUL":0x0f,
"DIV":0x1e,
"SDIV":0x1f,
"AND":0x2c,
"OR": 0x2d,
"XOR":0x2e,
"NOT":0x2f,
"RSH":0x3c,
"ASH":0x3d,
"LSH":0x3e,
"SIGN":0x3f,
"JMP":0x38,
"XJMP":0x39,
"YJMP":0x3a,
"ZJMP":0x3b,
"TXS":0x18,
"TXR":0x19,
"TXP":0x1a,
"TXI":0x1b,
"TXZ":0x40,
"TYZ":0x41,
"TZX":0x42,
"TZY":0x43,
"TYX":0x44,
"TXY":0x45,
"DECX":0x46,
"DECY":0x47,
"DECZ":0x48,
"INCX":0x49,
"INCY":0x4a,
"INCZ":0x4b,
"ENTX":0x4d,
"ENTY":0x4e,
"ENTZ":0x4f,
"RCV":0x52,
"DMSG":0x53,
"SEND":0x54,
"CHAN":0x55,
}
def compile_assembly(name, l):
global here
header(name)
for inst in l:
#memory[here] = to_int(inst)
#memory[here] = ITABLE[inst]
memory[here] = ITABLE.get(inst, to_int(inst))&0xff
here += 1
squit = []
def compile_def(name, l, immed=False, save_in=None):
global here
global squit
if name == "":
setmemory(save_in, here)
else:
header(name)
if immed:
memory[here-1] |= 128
memory[here] = 42
here += 1
i = 0
stack = []
#print(name)
while i<len(l):
#print(stack)
word = l[i]
i += 1
if to_int(word)!=None:
setmemory(here, df["(lit)"])
here += 2
setmemory(here, to_int(word))
here += 2
elif word == "POSTPONE":
nw = l[i]
i += 1
setmemory(here, df[nw])
here += 2
elif word == "LITERAL":
setmemory(here, df["(lit)"])
here += 2
setmemory(here, stack.pop())
here += 2
elif word == "[']":
setmemory(here, df["(lit)"])
here += 2
nw = l[i]
i += 1
setmemory(here, df[nw])
here += 2
elif word == "[COMPILE]":
setmemory(here, df["(lit)"])
here += 2
nw = l[i]
i += 1
setmemory(here, df[nw])
here += 2
setmemory(here, df[","])
here += 2
elif word == "IF":
setmemory(here, df["(0branch)"])
here += 2
stack.append(here)
setmemory(here, 0)
here += 2
elif word == "ELSE":
setmemory(here, df["(branch)"])
here += 2
n = stack.pop()
stack.append(here)
setmemory(here, 0)
here += 2
setmemory(n, here)
elif word == "THEN":
setmemory(stack.pop(), here)
elif word == "BEGIN":
stack.append(here)
elif word == "UNTIL":
setmemory(here, df["(0branch)"])
here += 2
setmemory(here, stack.pop())
here += 2
elif word == "REPEAT":
setmemory(here, df["(branch)"])
here += 2
setmemory(here, stack.pop())
here += 2
setmemory(stack.pop(), here)
elif word == "AGAIN":
setmemory(here, df["(branch)"])
here += 2
setmemory(here, stack.pop())
here += 2
elif word == "WHILE":
setmemory(here, df["(0branch)"])
here += 2
n = stack.pop()
stack.append(here)
stack.append(n)
setmemory(here, 0)
here += 2
elif word == "DO":
setmemory(here, df["(do)"])
here += 2
stack.append(here)
here += 2
stack.append(here)
elif word == "?DO":
setmemory(here, df["(?do)"])
here += 2
stack.append(here)
here += 2
stack.append(here)
elif word == "LOOP":
setmemory(here, df["(loop)"])
here += 2
setmemory(here, stack.pop())
here += 2
setmemory(stack.pop(), here)
elif word == "+LOOP":
setmemory(here, df["(+loop)"])
here += 2
setmemory(here, stack.pop())
here += 2
setmemory(stack.pop(), here)
elif word == "QUIT":
squit.append(here)
here += 2
elif word == 'S"':
nw = ""
while l[i][-1] != '"':
nw += l[i]
nw += " "
i += 1
nw += l[i][:-1] #Remove trailing "
i += 1
setmemory(here, df["(slit)"])
here += 2
setmemory(here, len(nw))
here += 2
for c in nw:
memory[here] = ord(c)
here += 1
else:
setmemory(here, df[word])
here += 2
setmemory(here, df["EXIT"])
here += 2
memory=[0]*0x10000
here=0x40c
latest=0
env_latest = 0
state="forth"
for i in lf:
k = i.split()
if len(k)>=1 and k[0] == "ASSEMBLER":
state="assembler"
elif len(k)>=1 and k[0] == "ENVIRONMENT":
state="env"
elif len(k)>=1 and k[0] == "FORTH":
state="forth"
elif len(k)>=3 and k[1] == "CONSTANT" and k[0]!=":":
if state == "env":
env_compile_constant(k[2], k[0])
else:
compile_constant(k[2],k[0])
elif len(k)>=3 and k[1] == "2CONSTANT" and k[0]!=":":
if state == "env":
env_compile_2constant(k[2], k[0])
elif len(k)>=3:
#print(k[0])
if k[0][0] == "\\":
continue
if state=="forth":
if k[0] == ":NONAME":
#print(getmemory(df[k[-2]]+1))
compile_def("",k[1:-3],False, getmemory(df[k[-2]]+1))
elif k[-1] == "IMMEDIATE":
compile_def(k[1],k[2:-2],True)
else:
compile_def(k[1],k[2:-1])
else:
compile_assembly(k[1],k[2:-1])
for i in squit:
setmemory(i, df["QUIT"])
memory[0x108]=10
memory[0x400]=0x4d
memory[0x401]=0x00
memory[0x402]=0x02
memory[0x403]=0x18
memory[0x404]=0x4d
memory[0x405]=0x00
memory[0x406]=0x03
memory[0x407]=0x19
memory[0x408]=0x4d
setmemory(0x409, df["COLD"])
memory[0x40b]=0x1a
setmemory(0x10c, latest)
setmemory(0x112, here)
setmemory(0x1a6, 1)
setmemory(0x1b0, latest)
setmemory(0x1b2, env_latest)
setmemory(0x1a4, 0x1b0)
setmemory(0x1d0, 0x1b0)
setmemory(0x1a2, 0x1b4)
memory[0xff00]=0x4d
memory[0xff01]=0x00
memory[0xff02]=0x04
memory[0xff03]=0x1a
def getc(i):
for key,k in df.items():
if k==i:
return key
#f = open('computer_memory.lua','w')
#f.write("function create_cptr_memory()\n\treturn {\n")
#for i in range(len(memory)):
# f.write("\t\t["+str(i)+"] = "+str(memory[i])+",\n")
#f.write("\t}\nend")
#f.close()
f = open('forth_floppy.lua','w')
f.write("function create_forth_floppy()\n\treturn \"")
for i in range(2**14):
f.write("\\%03d"%memory[i])
f.write("\"\nend")
f.close()

View File

@ -84,6 +84,22 @@ ASSEMBLER
: SET-CHANNEL PLY PLX CHAN NXT ;
: SEND PLY PLX SEND NXT ;
: FW 0x60 PHX NXT ;
: BW 0x61 PHX NXT ;
: UP 0x62 PHX NXT ;
: DN 0x63 PHX NXT ;
: TL 0x64 NXT ;
: TR 0x65 NXT ;
: DT PLX 0x68 PHX NXT ;
: DT-UP PLX 0x69 PHX NXT ;
: DT-DN PLX 0x6a PHX NXT ;
: DIG 0x70 NXT ;
: DIG-UP 0x71 NXT ;
: DIG-DN 0x72 NXT ;
: REFUEL PLY PLX 0x80 PHX NXT ;
ENVIRONMENT
256 CONSTANT /COUNTED-STRING
34 CONSTANT /HOLD

File diff suppressed because one or more lines are too long

10
t.lua
View File

@ -1,5 +1,5 @@
local MAX_LINE_LENGHT = 28
local DEBUG = true
local serialize_inv = function(l)
local l2={}
@ -309,6 +309,7 @@ end
local function done_move(pos, spos, npos)
local dir = vector.subtract(npos, spos)
local move = vector.subtract(npos, pos)
s = dot(dir, move)
return dot(dir, move) <= 0
end
@ -320,6 +321,7 @@ minetest.register_entity("turtle:turtle", {
physical = true,
force_load = TURTLES_FORCE_LOAD,
collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
collides_with_objects = false,
visual = "wielditem",
visual_size = {x = 2/3, y = 2/3},
textures = {"default:wood"},
@ -383,8 +385,10 @@ minetest.register_entity("turtle:turtle", {
end
if info.formspec_changed then
for playername, _ in pairs(info.playernames) do
print(info.text)
print("------------------------------------")
if DEBUG then
print(info.text)
print("------------------------------------")
end
minetest.show_formspec(playername, self.n, info.formspec)
end
info.formspec_changed = nil