Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Yay controllable trons. There is no AI tron right now, as this completely replaces the normal update function, and lua would lag too much.
- tron_rx = {-1, 0, 1, 0}
- tron_ry = { 0,-1, 0, 1}
- activetron = -1
- function newtronhead(x,y,i,direction,tmp)
- if (x<4 or y<4 or x>= 608 or y>=380) then
- return -1
- end
- np = tpt.create(x,y,"tron")
- if (np==-1) then
- return -1
- end
- --increase tail length
- if (tpt.get_property("life",i)>=100 and band(tmp,2)==0) then
- tpt.set_property("tmp2",tpt.get_property("tmp2",i)+1,i)
- tpt.set_property("life",5,i)
- end
- --give new head our properties
- if (np > i) then
- tpt.set_property("tmp",tmp+4,np)
- else
- tpt.set_property("tmp",tmp,np)
- end
- tpt.set_property("ctype",tpt.get_property("ctype",i),np)
- tpt.set_property("tmp2",tpt.get_property("tmp2",i),np)
- tpt.set_property("life",tpt.get_property("life",i)+2,np)
- activetron = np
- return 1
- end
- function updatetron(i,x,y,surround_space,nt)
- tmp = tpt.get_property("tmp",i)
- --TRON_WAIT check
- if (band(tmp,4) == 4) then
- tpt.set_property("tmp", tmp-4,i)
- return false
- end
- --Head check
- if (band(tmp,1)==1) then
- direction = band(lsr(tmp,5),3)+1
- if ( newtronhead(x+tron_rx[direction],y+tron_ry[direction],i,direction,tmp) == -1 ) then
- --oh god crash
- tpt.set_property("tmp",tmp+16,i)
- activetron = -1
- end
- tpt.set_property("life",tpt.get_property("tmp2",i),i)
- tpt.set_property("tmp",band(tmp,63512),i)
- end
- return false
- end
- function mouseclicky(mousex,mousey,button,event)
- if (tpt.selectedl ~= 143 or mousey > 384 or mousex > 612) then
- return true
- end
- if (event~=1 or activetron >= 0) then
- return false
- end
- activetron = tpt.create(mousex,mousey,"tron")
- return false
- end
- function keyclicky(key,nkey,modifier,event)
- if (activetron == -1) then return true end
- if (event == 2) then return false end
- tmp = tpt.get_property("tmp",activetron)
- direction = band(lsr(tmp,5),3)
- --up
- if (nkey == 273) then
- if (direction == 3) then return false end
- tpt.set_property("tmp",band(tmp,63515)+lsl(1,5),activetron)
- return false
- end
- --down
- if (nkey == 274) then
- if (direction == 1) then return false end
- tpt.set_property("tmp",band(tmp,63515)+lsl(3,5),activetron)
- return false
- end
- --right
- if (nkey == 275) then
- if (direction == 0) then return false end
- tpt.set_property("tmp",band(tmp,63515)+lsl(2,5),activetron)
- return false
- end
- --left
- if (nkey == 276) then
- if (direction == 2) then return false end
- tpt.set_property("tmp",band(tmp,63515),activetron)
- return false
- end
- return true
- end
- function status()
- if (activetron == -1) then
- tpt.drawtext(260,5,"You have no tron, place one")
- else
- tpt.drawtext(260,5,"Active tron!")
- end
- end
- tpt.register_step(status)
- tpt.register_keypress(keyclicky)
- tpt.register_mouseclick(mouseclicky)
- tpt.element_func(updatetron,tpt.element("tron"),1)
- --[[ Tables and constants for bitwise operator functions ]]--
- local ff = 2^32 - 1
- local maxsize = 2^32
- local floor = math.floor
- local powtbl = { 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536,
- 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216,
- 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648 }
- local xortbl = {
- { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, },
- { 1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14, },
- { 2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9, 14, 15, 12, 13, },
- { 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12, },
- { 4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11, },
- { 5, 4, 7, 6, 1, 0, 3, 2, 13, 12, 15, 14, 9, 8, 11, 10, },
- { 6, 7, 4, 5, 2, 3, 0, 1, 14, 15, 12, 13, 10, 11, 8, 9, },
- { 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8, },
- { 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, },
- { 9, 8, 11, 10, 13, 12, 15, 14, 1, 0, 3, 2, 5, 4, 7, 6, },
- {10, 11, 8, 9, 14, 15, 12, 13, 2, 3, 0, 1, 6, 7, 4, 5, },
- {11, 10, 9, 8, 15, 14, 13, 12, 3, 2, 1, 0, 7, 6, 5, 4, },
- {12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3, },
- {13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0, 3, 2, },
- {14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1, },
- {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, },
- }
- --[[ Perform a bitwise Exclusive OR (XOR) ]]--
- function bxor(a,b)
- local res, mult = 0, 1
- while a > 0 and b > 0 do
- local a2, b2 = a % 16, b % 16
- res = res + xortbl[a2+1][b2+1] * mult
- a = (a-a2) / 16
- b = (b-b2) / 16
- mult = mult * 16
- end
- res = res + (a+b) * mult
- return res
- end
- --[[ Perform a bitwise negation (NOT) ]]--
- function bnot(a)
- return ff - a
- end
- --[[ Perform a bitwise AND ]]--
- function band(a,b)
- return ((a+b) - bxor(a,b))/2
- end
- --[[ Perform a bitwise OR ]]--
- function bor(a,b)
- return ff - band(ff - a, ff - b)
- end
- --[[ Perform a logical shift left ]]--
- function lsl(a,n)
- if n < 1 then
- return a
- else
- return (n > 31) and 0 or (a * powtbl[n]) % maxsize
- end
- end
- --[[ Perform a logical shift right ]]--
- function lsr(a,n)
- if n < 1 then
- return a -- 0 shifting does nothing, negative shifts are ignored
- else
- return (n > 31) and 0 or (floor(a / powtbl[n]) % maxsize)
- end
- end
Add Comment
Please, Sign In to add comment