### Author Topic: Common coding tasks  (Read 2131 times)

0 Members and 1 Guest are viewing this topic.

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Common coding tasks
« on: May 18, 2012, 11:15:54 AM »
Figured I'd start a thread where I (and others) give examples of program code that is likely to be used often.

Let me know if you guys have any requests!
Also let me know if you spot a mistake, or you tried some of this code and it didn't work, even in an otherwise empty level file.

0. Contents

1. Send seedlings after a delay
2. Perform an arbitary number of different tasks after different amounts of time
3. Determine whether a number is odd or even
4. Calculate the distance between the centers of two asteroids
5. Calculate the distance between the closest edges of two asteroids
6. Calculate how much send distance is required for Asteroid 1 to be able to send seedlings to Asteroid 2
7. Calculate the X and Y coordinates at which the line formed by points: [Lx1, Ly1] [Lx2, Ly2] intersects with the line formed by [Lx3, Ly3] [Lx4, Ly4]
8. Calculate the X, Y and Z coordinates at which a line, denoted by any 2 points with coordinates [Lx1, Ly1, Lz1] [Lx2, Ly2, Lz2] intersects with a plane, denoted by any 3 coordinates on that plane; [Px1, Py1, Pz1] [Px2, Py2, Pz2] [Px3, Py3, Pz3]
9. Convert a set of 2D LevelDraw Coordinates into a set of 2D ScreenDraw Coordinates
10.  Concatenate one or more variables and/or one or more strings of text into a single MessageBox

1. Send seedlings after a delay
Code: [Select]
`function LevelLogic() -- create a "latch" variable, so that the seedlings are only sent once. once = false -- Run this loop continuously while GameRunning() do -- check if enough game time has passed yet if GetGameTime() > 3 and once == false then -- 3 seconds have passed - Asteroid 0 should now send 50 seedlings belonging to Empire 2 to Asteroid 1. GetAsteroid(0):SendSeedlingsToTarget(50,2,GetAsteroid(1)) -- flip the latch once = true end coroutine.yield() endend`

2. Perform an arbitary number of different tasks after different amounts of time
Code: [Select]
`function LevelLogic() -- create an "incident" variable.  This will be used to store a note of which event is due to happen next. incident = 0 -- create a "next" variable, this will record what the Game Time should be when the next incident must be run next = 0 -- Run this loop continuously while GameRunning() do -- check if it's time for the next event if GetGameTime() > next then -- It's time, so run the action function for the current incident Action(incident) -- increment the incident counter by 1 incident = incident + 1 end coroutine.yield() endendfunction Action(incident) if incident == 0 then GetAsteroid(0):AddSeedlings(10) next = GetGameTime() + 10 elseif incident == 1 then GetAsteroid(0):AddSeedlings(20) next = GetGameTime() + 5 elseif incident == 2 then GetAsteroid(0):PlantDysonTree(1) next = GetGameTime() + 25 elseif incident == 3 then GetAsteroid(0):SendSeedlingsToTarget(50,1,GetAsteroid(1)) next = GetGameTime() + 10 elseif incident == 4 then SetBackdropColour(0,0,80) next = GetGameTime() + 15 else -- no further incidents defined endend`

3. Determine whether an integer is odd or even
Code: [Select]
`if integer % 2 == 0 then -- if the integer divided by 2 has a remainder of 0, then --the integer is evenelse -- the integer is oddend`

4. Calculate the distance between the centers of two asteroids
Code: [Select]
`function LevelLogic() -- first lets assign the values to some variables roid1X = GetAsteroid(1).position.X roid1Y = GetAsteroid(1).position.Y roid2X = GetAsteroid(2).position.X roid2Y = GetAsteroid(2).position.Y -- next, calculate the diffence between their X coordinates dx = roid2X - roid1X -- and their Y coordinates dy = roid2Y - roid1Y -- dx and dy are like 2 sides of a right angled triangle. -- If you recall pythagoras, the 3rd side (hypotenuse) can be calculated with a^2 + b^2 = c^2 -- So dx^2 + dy^2 = DistanceBetweenAsteroids^2 distance = math.sqrt((dx * dx) + (dy * dy))end`

5. Calculate the distance between the closest edges of two asteroids
Code: [Select]
`function LevelLogic() -- first lets assign the values to some variables roid1X = GetAsteroid(1).position.X roid1Y = GetAsteroid(1).position.Y roid2X = GetAsteroid(2).position.X roid2Y = GetAsteroid(2).position.Y -- next, calculate the diffence between their X coordinates dx = roid2X - roid1X -- and their Y coordinates dy = roid2Y - roid1Y -- dx and dx are like 2 sides of a right angled triangle. -- If you recall pythagoras, the 3rd side (hypotenuse) can be calculated with a^2 + b^2 = c^2 -- So dx^2 + dy^2 = DistanceBetweenAsteroids^2 dist = math.sqrt((dx * dx) + (dy * dy)) -- dist is the distance between the asteroid centers.  To get the actual distance between their surfaces, we must subtract both radii from dist distance = dist - GetAsteroid(1).radius - GetAsteroid(2).radiusend`

6. Calculate how much send distance is required for Asteroid 1 to be able to send seedlings to Asteroid 2
Code: [Select]
`function LevelLogic() -- first lets assign the values to some variables roid1X = GetAsteroid(1).position.X roid1Y = GetAsteroid(1).position.Y roid2X = GetAsteroid(2).position.X roid2Y = GetAsteroid(2).position.Y -- next, calculate the diffence between their X coordinates dx = roid2X - roid1X -- and their Y coordinates dy = roid2Y - roid1Y -- dx and dx are like 2 sides of a right angled triangle. -- If you recall pythagoras, the 3rd side (hypotenuse) can be calculated with a^2 + b^2 = c^2 -- So dx^2 + dy^2 = DistanceBetweenAsteroids^2 dist = math.sqrt((dx * dx) + (dy * dy)) -- dist is the distance between the asteroid centers.  To get the minimum required SendDistance, we measure from the center of Asteroid 1 (startpoint) to the edge of Asteroid 2 (destination) distance = dist - GetAsteroid(2).radiusend`

7. Calculate the X and Y coordinates at which the line formed by points: [Lx1, Ly1] [Lx2, Ly2] intersects with the line formed by [Lx3, Ly3] [Lx4, Ly4]
Code: [Select]
`-- calculate A, B and C for both lines (standard form)A1 = Ly2-Ly1B1 = Lx1-Lx2C1 = A1*x1+B1*y1A2 = Ly4-Ly3B2 = Lx3-Lx4C2 = A3*x3+B3*y3-- detect if they will ever intersect..det = A1*B2 - A2*B1if det == 0 --Lines are parallel else -- intersection coordinates give as: intersectX = ((B2*C1) - (B1*C2)) / det intersectY = ((A1*C2) - (A2*C1)) / detend`

8. Calculate the X, Y and Z coordinates at which a line, denoted by 2 points with coordinates [Lx1, Ly1, Lz1] [Lx2, Ly2, Lz2] intersects with a plane, denoted by 3 coordinates on that plane; [Px1, Py1, Pz1] [Px2, Py2, Pz2] [Px3, Py3, Pz3]
Code: [Select]
` -- get 2 vectors along the plane -- PV1x stands for Plane Vector #1, X-coordinate PV1x = Px2 - Px1 PV1y = Py2 - Py1 PV1z = Pz2 - Pz1 PV2x = Px3 - Px1 PV2y = Py3 - Py1 PV2z = Pz3 - Pz1 -- now find the cross product, which is a line perpendicular to the plane -- PV1 X PV2 = normal vector! i = (PV1y * PV2z) - (PV1z * PV2y) j = (PV1z * PV2x) - (PV1x * PV2z) k = (PV1x * PV2y) - (PV1y * PV2x) -- for the line, calculate tx, ty, tz tx = Lx2 - Lx1 ty = Ly2 - Ly1 tz = Lz2 - Lz1 -- calculate t num = ((i * (Px1 - Lx1)) + (j * (Py1 - Ly1)) + (k * (Pz1 - Lz1))) denom = ((i * (Lx2 - Lx1)) + (j * (Ly2 - Ly1)) + (k * (Lz2 - Lz1))) t = num / denom -- calculate coordinates of the intersection point intersectX = (t * tx) + Lx1 intersectY = (t * ty) + Ly1 intersectZ = (t * tz) + Lz1`

9. Convert a set of 2D LevelDraw Coordinates into a set of 2D ScreenDraw Coordinates
Code: [Select]
`-- where lvlX is the x-coordinate of the point in Leveldraw that must be converted, and lvlY is the y-coordinate.screenX = (lvlX * GetCameraScale()) - (GetCameraX() * GetCameraScale()) + (GetScreenWidth() - (GetScreenWidth() / 2))screenY = (lvly * GetCameraScale()) - (GetCameraY() * GetCameraScale()) + (GetScreenHeight() - (GetScreenHeight() / 2))`

10.  Concatenate one or more variables and/or one or more strings of text into a single MessageBox
Code: [Select]
`c1 = 5d1 = 0omg1 = "cats"MessageBox(c1 .. d1)-- "05"MessageBox("Hello " .. omg1)-- "Hello cats"MessageBox("You have " .. c1 .. " " .. omg1 .. " and " .. d1 .. " dogs.  Clearly you must prefer " .. omg1 .. ".")-- "You have 5 cats and 0 dogs.  Clearly you must prefer cats."`
« Last Edit: May 26, 2012, 12:33:01 PM by annikk.exe »

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Re: Common coding tasks
« Reply #1 on: May 18, 2012, 11:54:35 AM »
Reserved
« Last Edit: May 26, 2012, 12:27:02 PM by annikk.exe »

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Re: Common coding tasks
« Reply #2 on: May 18, 2012, 12:50:55 PM »
Reserved
« Last Edit: May 26, 2012, 12:27:11 PM by annikk.exe »

#### Tomfloria

• Shrub
• Thank You
• -Given: 0
• Posts: 232
• First iOS modder :D
##### Re: Common coding tasks
« Reply #3 on: May 18, 2012, 02:13:22 PM »
Put in messages and while loops, if that's what it's called

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Re: Common coding tasks
« Reply #4 on: May 18, 2012, 02:40:07 PM »
Hmm, do you need an example of just a while loop?
I would have thought they were covered adequately in the beginner and intermediate guides..

(especially the intermediate guide - there is a whole section that talks about while GameRunning() do at the start of it)

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Re: Common coding tasks
« Reply #5 on: May 18, 2012, 02:42:28 PM »
And messagebox.... isn't MessageBox("Fish") self explanatory?

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Re: Common coding tasks
« Reply #6 on: May 18, 2012, 02:43:52 PM »
I could do one on concatenating variables and text into the same MessageBox I suppose... that's not obvious.

#### Tomfloria

• Shrub
• Thank You
• -Given: 0
• Posts: 232
• First iOS modder :D
##### Re: Common coding tasks
« Reply #7 on: May 18, 2012, 02:46:29 PM »
No I know how to do them XD, this is a common coding thread, I always use while loops

#### Tomfloria

• Shrub
• Thank You
• -Given: 0
• Posts: 232
• First iOS modder :D
##### Re: Common coding tasks
« Reply #8 on: May 18, 2012, 02:50:58 PM »
Such as

Code: [Select]
` while GetEmpire(0).NumSeedlings > 0 do --Checks to see if Empire 0's Number of Seedlings is 0 or less, can't be less than 0 though.                         CheckConditions() --THIS IS SOMETHING I USE PERSONALLY. YOU DON'T NEED IT.                         coroutine.yield() -- Makes this statement check over and over until Empire 0's number of seedlings is at 0 end --To end this statement`

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Re: Common coding tasks
« Reply #9 on: May 18, 2012, 02:53:33 PM »
By "common coding tasks" I mean the ones you are likely to have to do at some point, but excluding anything that is so basic that most people are likely to know about it already.

I'm also aiming to avoid duplicating anything in the beginner or intermediate guides.

I've added a concatenated messagebox guide.  :>

#### Tomfloria

• Shrub
• Thank You
• -Given: 0
• Posts: 232
• First iOS modder :D
##### Re: Common coding tasks
« Reply #10 on: May 18, 2012, 02:55:55 PM »
OH.. ICUP.

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Re: Common coding tasks
« Reply #11 on: May 18, 2012, 02:56:52 PM »
Also, I consider using any type of While loop other than While GameRunning() to be bad programming practice. :P

It's far far more flexible if you just have one loop running all the time, and then use conditionals to trigger code within that loop.  EG, did you see example 2 in this thread? :>

#### Tomfloria

• Shrub
• Thank You
• -Given: 0
• Posts: 232
• First iOS modder :D
##### Re: Common coding tasks
« Reply #12 on: May 18, 2012, 03:02:34 PM »
I see what you mean, but isn't it easier to see your code in 1,2,3,4 (order) rather than somewhere else? At least easier for me

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Re: Common coding tasks
« Reply #13 on: May 18, 2012, 04:52:09 PM »
It's slightly easier when you're doing basic things, but once you get into coding anything above beginner level, using any other kind of While loop other than while GameRunning() is likely to cause problems; you'll want to run multiple mechanics at once, but you won't be able to easily because different sections of code are looping at different times...

Hard to explain, but it's a conclusion I swiftly reached after I made the level "Return to Fluffy Land" which was my 2nd level ever.  I wrote that with lots of While loops that were triggered one after the other.  While 1 would loop for a while, then stop... then While 2 would loop for a while, then stop, then While 3 would loop...and so on.  It ended up being sooo messy, and by the end of the level I found it was restricting what I could do, so for the final loop I believe I just did While GameRunning() do, and then figured out how to run mechanics in parallel within that loop.

• Achiever
• Ent
• Thank You
• -Given: 0