Advertisement
jacob614

sim.updateParticles script

Jan 11th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.88 KB | None | 0 0
  1. local lastparticle = -1
  2. local overlay = false
  3. local lastkey = {}
  4. local dokeypress
  5.  
  6. local function step()
  7.     if tpt.set_pause() == 0 then
  8.         lastparticle = -1
  9.     end
  10.     if overlay then
  11.         for i in sim.parts() do
  12.             if i <= lastparticle then
  13.                 tpt.drawpixel(math.floor(tpt.get_property("x", i)+.5), math.floor(tpt.get_property("y", i)+.5), 0, 0, 0, 150)
  14.             else
  15.                 break
  16.             end
  17.         end
  18.     end
  19.     if lastkey["time"] and lastkey["time"] < socket.gettime() then
  20.         dokeypress(lastkey["key"], lastkey["nkey"], lastkey["modifier"], lastkey["event"])
  21.         lastkey["time"] = socket.gettime()+.1
  22.     end
  23. end
  24. tpt.register_step(step)
  25.  
  26. --replicate TPT's life decreasing
  27. local function decreaselife()
  28.     for i in sim.parts() do
  29.         local life = tpt.get_property("life", i)
  30.         local properties = elem.property(tpt.get_property("type", i), "Properties")
  31.         if life > 0 and bit.band(properties, elem.PROP_LIFE_DEC) ~= 0 then
  32.             if life == 1 and bit.band(properties, bit.bor(elem.PROP_LIFE_KILL_DEC,elem.PROP_LIFE_KILL)) ~= 0 then
  33.                 sim.partKill(i)
  34.             else
  35.                 tpt.set_property("life", life-1, i)
  36.             end
  37.         elseif life <= 0 and bit.band(properties, elem.PROP_LIFE_KILL) ~= 0 then
  38.             sim.partKill(i)
  39.         end
  40.     end
  41. end
  42.  
  43. function dokeypress(key, nkey, modifier, event)
  44.     if event == 1 and key == 'f' then
  45.         if bit.band(modifier, 0x300) ~= 0 then
  46.             overlay = not overlay
  47.             return false
  48.         elseif bit.band(modifier, 0xC0) ~= 0 then --ctrl is pressed
  49.             lastparticle = lastparticle + 1
  50.             if lastparticle >= sim.XRES*sim.YRES then
  51.                 print("reached the end of the particles")
  52.                 lastparticle = -1
  53.             else
  54.                 if lastparticle == -1 then
  55.                     print("decreasing particle life")
  56.                     decreaselife()
  57.                 end
  58.                 while lastparticle < sim.XRES*sim.YRES and tpt.get_property("type", lastparticle) == 0 do
  59.                     lastparticle = lastparticle + 1
  60.                 end
  61.                 sim.updateParticles(lastparticle, lastparticle)
  62.             end
  63.             return false
  64.         elseif bit.band(modifier, 0x3) ~= 0 then --shift is pressed
  65.             local mousex, mousey = sim.adjustCoords(tpt.mousex, tpt.mousey)
  66.             local i = sim.pmap(mousex, mousey)
  67.             if i then
  68.                 if i > lastparticle then
  69.                     print("advancing from particle #"..(lastparticle+1).." to particle #"..i)
  70.                     sim.updateParticles(lastparticle+1, i)
  71.                     lastparticle = i
  72.                 else
  73.                     print("#"..lastparticle.." has already been updated")
  74.                 end
  75.             else
  76.                 print("advancing to end (particles #"..(lastparticle+1).." to #"..(sim.XRES*sim.YRES-1)..")")
  77.                 sim.updateParticles(lastparticle+1, sim.XRES*sim.YRES-1)
  78.                 lastparticle = -1
  79.             end
  80.             return false
  81.         else
  82.             lastparticle = -1
  83.         end
  84.     end
  85. end
  86.  
  87. local function keypress(key, nkey, modifier, event)
  88.     if event == 1 then
  89.         lastkey = {["key"] = key, ["nkey"] = nkey, ["modifier"] = modifier, ["event"] = event, ["time"] = socket.gettime()+.5}
  90.     else
  91.         lastkey = {}
  92.     end
  93.     return dokeypress(key, nkey, modifier, event)
  94. end
  95. tpt.register_keypress(keypress)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement