jacob614

controllable TRON - cracker64

Sep 20th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.90 KB | None | 0 0
  1. --Yay controllable trons.  There is no AI tron right now, as this completely replaces the normal update function, and lua would lag too much.
  2. tron_rx = {-1, 0, 1, 0}
  3. tron_ry = { 0,-1, 0, 1}
  4. activetron = -1
  5.  
  6. function newtronhead(x,y,i,direction,tmp)
  7.     if (x<4 or y<4 or x>= 608 or y>=380) then
  8.         return -1
  9.     end
  10.     np = tpt.create(x,y,"tron")
  11.     if (np==-1) then
  12.         return -1
  13.     end
  14.     --increase tail length
  15.     if (tpt.get_property("life",i)>=100 and  band(tmp,2)==0)  then
  16.         tpt.set_property("tmp2",tpt.get_property("tmp2",i)+1,i)
  17.         tpt.set_property("life",5,i)
  18.     end
  19.     --give new head our properties
  20.     if (np > i) then
  21.         tpt.set_property("tmp",tmp+4,np)
  22.     else
  23.         tpt.set_property("tmp",tmp,np)
  24.     end
  25.     tpt.set_property("ctype",tpt.get_property("ctype",i),np)
  26.     tpt.set_property("tmp2",tpt.get_property("tmp2",i),np)
  27.     tpt.set_property("life",tpt.get_property("life",i)+2,np)
  28.     activetron = np
  29.     return 1
  30. end
  31.  
  32. function updatetron(i,x,y,surround_space,nt)
  33.     tmp = tpt.get_property("tmp",i)
  34.     --TRON_WAIT check
  35.     if (band(tmp,4) == 4) then
  36.         tpt.set_property("tmp", tmp-4,i)
  37.         return false
  38.     end
  39.     --Head check
  40.     if (band(tmp,1)==1) then
  41.         direction = band(lsr(tmp,5),3)+1
  42.         if ( newtronhead(x+tron_rx[direction],y+tron_ry[direction],i,direction,tmp) == -1 ) then
  43.             --oh god crash
  44.             tpt.set_property("tmp",tmp+16,i)
  45.             activetron = -1
  46.         end
  47.         tpt.set_property("life",tpt.get_property("tmp2",i),i)
  48.         tpt.set_property("tmp",band(tmp,63512),i)
  49.     end
  50.     return false
  51. end
  52.  
  53. function mouseclicky(mousex,mousey,button,event)
  54.     if (tpt.selectedl ~= 143 or mousey > 384 or mousex > 612) then
  55.         return true
  56.     end
  57.     if (event~=1 or activetron >= 0) then
  58.         return false
  59.     end
  60.     activetron = tpt.create(mousex,mousey,"tron")
  61.     return false
  62. end
  63.  
  64. function keyclicky(key,nkey,modifier,event)
  65.     if (activetron == -1) then return true end
  66.     if (event == 2) then return false end
  67.     tmp = tpt.get_property("tmp",activetron)
  68.     direction = band(lsr(tmp,5),3)
  69.     --up
  70.     if (nkey == 273) then
  71.         if (direction == 3) then return false end
  72.         tpt.set_property("tmp",band(tmp,63515)+lsl(1,5),activetron)
  73.         return false
  74.     end
  75.     --down
  76.     if (nkey == 274) then
  77.         if (direction == 1) then return false end
  78.         tpt.set_property("tmp",band(tmp,63515)+lsl(3,5),activetron)
  79.         return false
  80.     end
  81.     --right
  82.     if (nkey == 275) then
  83.         if (direction == 0) then return false end
  84.         tpt.set_property("tmp",band(tmp,63515)+lsl(2,5),activetron)
  85.         return false
  86.     end
  87.     --left
  88.     if (nkey == 276) then
  89.         if (direction == 2) then return false end
  90.         tpt.set_property("tmp",band(tmp,63515),activetron)
  91.         return false
  92.     end
  93.     return true
  94. end
  95.  
  96. function status()
  97.     if (activetron == -1) then
  98.         tpt.drawtext(260,5,"You have no tron, place one")
  99.     else
  100.         tpt.drawtext(260,5,"Active tron!")
  101.     end
  102. end
  103.  
  104. tpt.register_step(status)
  105. tpt.register_keypress(keyclicky)
  106. tpt.register_mouseclick(mouseclicky)
  107. tpt.element_func(updatetron,tpt.element("tron"),1)
  108.  
  109.  
  110.  
  111.  
  112. --[[ Tables and constants for bitwise operator functions ]]--
  113.  
  114. local ff = 2^32 - 1
  115. local maxsize = 2^32
  116. local floor = math.floor
  117. local powtbl = { 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536,
  118.                  131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216,
  119.                  33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648 }
  120. local xortbl = {
  121.         { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, },
  122.         { 1,  0,  3,  2,  5,  4,  7,  6,  9,  8, 11, 10, 13, 12, 15, 14, },
  123.         { 2,  3,  0,  1,  6,  7,  4,  5, 10, 11,  8,  9, 14, 15, 12, 13, },
  124.         { 3,  2,  1,  0,  7,  6,  5,  4, 11, 10,  9,  8, 15, 14, 13, 12, },
  125.         { 4,  5,  6,  7,  0,  1,  2,  3, 12, 13, 14, 15,  8,  9, 10, 11, },
  126.         { 5,  4,  7,  6,  1,  0,  3,  2, 13, 12, 15, 14,  9,  8, 11, 10, },
  127.         { 6,  7,  4,  5,  2,  3,  0,  1, 14, 15, 12, 13, 10, 11,  8,  9, },
  128.         { 7,  6,  5,  4,  3,  2,  1,  0, 15, 14, 13, 12, 11, 10,  9,  8, },
  129.         { 8,  9, 10, 11, 12, 13, 14, 15,  0,  1,  2,  3,  4,  5,  6,  7, },
  130.         { 9,  8, 11, 10, 13, 12, 15, 14,  1,  0,  3,  2,  5,  4,  7,  6, },
  131.         {10, 11,  8,  9, 14, 15, 12, 13,  2,  3,  0,  1,  6,  7,  4,  5, },
  132.         {11, 10,  9,  8, 15, 14, 13, 12,  3,  2,  1,  0,  7,  6,  5,  4, },
  133.         {12, 13, 14, 15,  8,  9, 10, 11,  4,  5,  6,  7,  0,  1,  2,  3, },
  134.         {13, 12, 15, 14,  9,  8, 11, 10,  5,  4,  7,  6,  1,  0,  3,  2, },
  135.         {14, 15, 12, 13, 10, 11,  8,  9,  6,  7,  4,  5,  2,  3,  0,  1, },
  136.         {15, 14, 13, 12, 11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1,  0, },
  137.     }
  138.  
  139. --[[ Perform a bitwise Exclusive OR (XOR) ]]--
  140.  
  141. function bxor(a,b)
  142.     local res, mult = 0, 1
  143.     while a > 0 and b > 0 do
  144.         local a2, b2 = a % 16, b % 16
  145.         res = res + xortbl[a2+1][b2+1] * mult
  146.         a = (a-a2) / 16
  147.         b = (b-b2) / 16
  148.         mult = mult * 16
  149.     end
  150.     res = res + (a+b) * mult
  151.     return res
  152. end
  153.  
  154. --[[ Perform a bitwise negation (NOT) ]]--
  155.  
  156. function bnot(a)
  157.   return ff - a
  158. end
  159.  
  160. --[[ Perform a bitwise AND ]]--
  161.  
  162. function band(a,b)
  163.     return ((a+b) - bxor(a,b))/2
  164. end
  165.  
  166. --[[ Perform a bitwise OR ]]--
  167.  
  168. function bor(a,b)
  169.     return ff - band(ff - a, ff - b)
  170. end
  171.  
  172. --[[ Perform a logical shift left ]]--
  173.  
  174. function lsl(a,n)
  175.     if n < 1 then
  176.         return a
  177.     else
  178.         return (n > 31) and 0 or (a * powtbl[n]) % maxsize
  179.     end
  180. end
  181.  
  182. --[[ Perform a logical shift right ]]--
  183.  
  184. function lsr(a,n)
  185.     if n < 1 then
  186.         return a    -- 0 shifting does nothing, negative shifts are ignored
  187.     else
  188.         return (n > 31) and 0 or (floor(a / powtbl[n]) % maxsize)
  189.     end
  190. end
Add Comment
Please, Sign In to add comment