Data Realms Fan Forums
http://45.55.195.193/

Lua tricks Topic
http://45.55.195.193/viewtopic.php?f=73&t=16260
Page 1 of 2

Author:  Areku [ Tue Aug 18, 2009 1:06 am ]
Post subject:  Lua tricks Topic

So, basically, this is a topic for all those useful snippets of code I occasionally find while playing with Sparkle MagicTM. Just thought I'd post 'em here for easier access, and 'cause sometimes you're having a lot of trouble with a complex script when a small bit of code would solve the problem. Feel free to post your other Lua tricks here, along with a brief description of their purpose.


tl;dr: Post your cool Lua stuff (not console commands, we already have a thread for that) here.


To get the topic started, a tiny bit of code that I found today, pretty useful stuff:
Code:
function Update(self)


   if self:GetAltitude(5,2) < 3 then
      self.ToDelete = true;
   end
end


This code , designed to be used on MOPixels, makes the bullet harmless on terrain, while leaving its interactions with other particles unchanged. Good for when you wanna make a powerful sniper, but don't want it to rape too much terrain.

Author:  mail2345 [ Tue Aug 18, 2009 5:38 am ]
Post subject:  Re: Lua tricks Topic

Eh, I need a place to centralize my insane hacksuseful tricks.

viewtopic.php?f=73&t=16213
viewtopic.php?f=73&t=15692
viewtopic.php?f=73&t=15359

Gonna post some neat armour piercing code later.

Author:  Grif [ Sat Aug 22, 2009 3:29 pm ]
Post subject:  Re: Lua tricks Topic

Code:
function KeyPressLength()
   if self.init == 0 then
      self.init = 1;
      self.presses = 0;
      self.pressed = 0;
      self.presstimer = Timer();
   end
   --replace BODY_JUMP with whichever
   if self:GetController():IsState(Controller.BODY_JUMP) == true then
      self.pressed = 1;
   else
      if self.pressed == 1 then
         self.pressed = 0;
         self.presstime = self.presstimer.ElapsedSimTimeMS;
         --put any code to execute when you stop holding the button here
      elseif self.pressed == 0 then
         self.presstimer:Reset();
      end
   end
end


Get the duration of a keypress and execute code only when it's released.

Author:  xerxys [ Mon Aug 24, 2009 7:51 pm ]
Post subject:  Re: Lua tricks Topic

TLB's conveyor area check:
Code:
if (item.Pos.X >= self.Pos.X - 15) and (item.Pos.X <= self.Pos.X + 15) and (item.Pos.Y >= self.Pos.Y - 16) and (item.Pos.Y <= self.Pos.Y + 16) then


Another big proximity check:
Code:
local curdist = 600;
for actor in MovableMan.Actors do
      local avgx = actor.Pos.X - self.Pos.X;
      local avgy = actor.Pos.Y - self.Pos.Y;
      local dist = math.sqrt(avgx ^ 2 + avgy ^ 2);
      if dist < curdist then
          curdist = dist;


Kyred's proximity check:
Code:
if math.abs(actor.Pos.X - self.Pos.X) < 40 and math.abs(actor.Pos.Y - self.Pos.Y) < 40 then


The Magnitude() thing:
Code:
if (thing1.Pos - thing2.Pos).Magnitude < desiredproximity then

Author:  Grif [ Mon Aug 24, 2009 11:59 pm ]
Post subject:  Re: Lua tricks Topic

First check (the one for conveyors) can be done more efficiently with boxes. SceneMan:ShortestDistance is the most efficient for circular checks, but it isn't quite at the same scale (Pythagorean theorem gets pixel-accurate dimensions, but vector magnitudes aren't the same)

Author:  piipu [ Tue Aug 25, 2009 5:34 am ]
Post subject:  Re: Lua tricks Topic

Vector magnitude is exactly the same as the dist calculated with that pythagoras stuff. Vector.Magnitude is just shorter and likely a lot faster. And SceneMan:ShortestDistance() just returns a vector to the target, not actual distance, so you have to use Vector.Magnitude with that too.

Author:  Grif [ Tue Aug 25, 2009 5:53 am ]
Post subject:  Re: Lua tricks Topic

Yeah, I meant SceneMan:ShortestDistance.Magnitude. But uh, in my experience it's slightly skewed.

Author:  Kyred [ Wed Aug 26, 2009 9:38 pm ]
Post subject:  Re: Lua tricks Topic

Grif wrote:
(Pythagorean theorem gets pixel-accurate dimensions, but vector magnitudes aren't the same)
Distance in CC is measured in pixels from the target. Finding the magnitude of a vector stretched between two objects should return the number of pixels between the two.

The Pythagorean theorem is often used to find the magnitude of a vector. To use the Pythagorean theorem to find the distance between the dummy and the soldier in the picture below, you would do:
Code:
math.sqrt( (588-190)^2 + (495-250)^2 )
= math.sqrt(398^2 + 245^2)

The magnitude of vector V3 can be found by using the Pythagorean theorem on the V3's components:
Code:
math.sqrt(398^2 + 245^2)

These both come out to the same value. So, unless if Vector.Magnitude uses a different, less accurate, calculation method, Vector.Magnitude and the Pythagorean theorem methods should come out to the same value.

@Grif:
By skewed do you mean shorter or longer?
Image
Anything in the picture with a P (like P1 or P2) stands for a point. V's stand for vectors.

Author:  Grif [ Wed Aug 26, 2009 11:45 pm ]
Post subject:  Re: Lua tricks Topic

I know that they should be the same but I've had experiences where it doesn't line up with the pixel-width of a glow. This could have to do with Data's method of drawing glows or something, but it's distinctly not the actual diameter of the glow in question.

Author:  DrLuke [ Sat Oct 03, 2009 10:56 am ]
Post subject:  Re: Lua tricks Topic

Build your own timer:
Code:
function Create(self)
   self.timer = 0;
end

function Update(self)
   self.timer = self.timer + 1;
end

Author:  Roon3 [ Sat Oct 03, 2009 12:16 pm ]
Post subject:  Re: Lua tricks Topic

http://datarealms.com/wiki/index.php/LuaDocs/Timer

Author:  CrazyMLC [ Sat Oct 03, 2009 2:27 pm ]
Post subject:  Re: Lua tricks Topic

Areku wrote:
Code:
function Update(self)


   if self:GetAltitude(5,2) < 3 then
      self.ToDelete = true;
   end
end


This code , designed to be used on MOPixels, makes the bullet harmless on terrain, while leaving its interactions with other particles unchanged. Good for when you wanna make a powerful sniper, but don't want it to rape too much terrain.

:shock:
I think you just made my day.

Also, you might, maybe, want to enforce a rule where you cannot post if you don't have a code snippet.

Author:  findude [ Mon Oct 12, 2009 10:14 am ]
Post subject:  Re: Lua tricks Topic

Just so happens that the default gravity acceleration is nullified by substracting
~0.3 from the objects Y velocity each update call. Zerogravity!

Author:  Grif [ Mon Oct 12, 2009 3:08 pm ]
Post subject:  Re: Lua tricks Topic

findude wrote:
Just so happens that the default gravity acceleration is nullified by substracting
~0.3 from the objects Y velocity each update call. Zerogravity!


self.Vel.Y = self.Vel.Y - SceneMan.GlobalAccSalar * TimerMan.DeltaTimeSecs

Salar is mispelled in the actual function and idk exactly what deltatime is referenced as, but that's your ideal zero gravity.

Author:  Kyred [ Mon Oct 12, 2009 6:13 pm ]
Post subject:  Re: Lua tricks Topic

Grif wrote:
findude wrote:
Just so happens that the default gravity acceleration is nullified by substracting
~0.3 from the objects Y velocity each update call. Zerogravity!


self.Vel.Y = self.Vel.Y - SceneMan.GlobalAccSalar * TimerMan.DeltaTimeSecs

Salar is mispelled in the actual function and idk exactly what deltatime is referenced as, but that's your ideal zero gravity.
For anyone who is wonder how Grif got this equation:

Velocity = Initial Velocity + Acceleration * Change In Time (ie. V = V0 + A*t)

We want to apply a velocity upward. In CC, -Y means up, so the equation changes to: V = V0 - A*t

TimerMan.DeltaTimeSecs gives the ratio of Real Time vs. Simulation Time. Or to put it simply, the time in seconds between each Sim Update (usually 0.003 something. Check your settings.ini to see what it is by default).

Also remember, that different objects have a different GlobalAcc value set by their .ini information. You'll need to account for that as well.

Page 1 of 2 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/