The above video goes away if you are a member and logged in, so log in now!

 Would you like to get all the newest Gaming News fromQJ.NET in your email each day? Want to learn more about the team who brings you the QJ news?

This is a discussion on Lua Programming Help Thread within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; I've got a box in the middle and other stuff, but thats pretty much just a single model then theres ...

 Tweet
1. I've got a box in the middle and other stuff, but thats pretty much just a single model then theres the world. thanks

2. Originally Posted by TurtlesPwn
dan - state = math.random(0,4)

combina - all you modified on that was locking it on the number pad. for a simple # input, any type of OSK is overkill.
Also photoshopped the space into a decimal point, and changed the function.
It's a nooby change

anyway this OSK is perfect for me, I want to keep it.
Just wanna get rid of those bugs (after that it's perfect)

3. Just wondering about how to do a score check function with multiple balls in a Pong game. Here is how I currently do it for one ball.

Code:
```function checkScore ()
if Ball[1].x < 0 - Ball.img:width () then
gamestate = "scored"
Player[1].score = Player[1].score - 10

if Player[2].score < 100 then
Player[2].score = Player[2].score + 10
end

whoScored = "CPU"
Ball.x = 240
Ball.y = math.random (80,120)
Ball.xspeed = 6.2
end

if Ball.x > Resolution.width then
gamestate = "scored"
Player[2].score = Player[2].score - 10

if Player[1].score < 100 then
Player[1].score = Player[1].score + 10
end

whoScored = "You"
Ball.x = 240
Ball.y = math.random (80,120)
Ball.xspeed = -6
end
end```
If I had three balls would I do something like this(I'll just use the first line as an example)?

Code:
`if Ball[1].x or Ball[2].x or Ball[3].x < 0 - Ball.img:width () then`
Does this work or is there a different way to do this?

4. Use a for loop.

5. Oh, that makes sense. Alright, I shall try that. Thanks yaustar.

7. Originally Posted by GP3X
what do you mean? ... in what way?.

8. The for loop is very helpful for when you have multiple objects or variables you need the program to refer to but would it be wise to use it in ever single instance where you need to refer to more than 1 item from the same table? For example:

Code:
```screen:blit (Ball[1].x,Ball[1].y,Ball[1].img)
screen:blit (Ball[2].x,Ball[2].y,Ball[2].img)
screen:blit (Ball[3].x,Ball[3].y,Ball[3].img)```
I have a lot of things like this(not necessarily just blitting images) that could probably be pruned off a bit if I used a for loop but is it good practice to use the for loop for every single example of this kind of thing?

EDIT: I guess there is also the issue of speed. Would it be just as fast as simply saying:

Code:
```if catfood == 1 then
dofile "yummy.lua"

elseif catfood == 2 then
dofile "yummy.lua"

elseif catfood == 3 then
dofile "yummy.lua"```
?

9. I have a lot of things like this(not necessarily just blitting images) that could probably be pruned off a bit if I used a for loop but is it good practice to use the for loop for every single example of this kind of thing?
Yes, what makes you think otherwise? As long as you write the for loop properly, you can easily change the number of objects in the table without ever needing to change the for loop code.

EDIT: I guess there is also the issue of speed. Would it be just as fast as simply saying:
Code:
```if catfood == 1 then
dofile "yummy.lua"

elseif catfood == 2 then
dofile "yummy.lua"

elseif catfood == 3 then
dofile "yummy.lua"```
A for loop isn't applicable here. Regardless, I would write it like:
Code:
```someRandomName = { "yummyone.lua", "yummytwo.lua", "yummythree.lua" }

-- some code later
dofile( someRandomName[catfood] )```

10. Thanks, yaustar. I am just not too confident in my understanding of Lua so thanks for helping me out. :)

Does anyone know how to make proper A.I. that can handle blocking multiple balls in a Pong game? Currently I am using the A.I. code taught by Evilmana.com which isn't very great for handling more than one ball. It isn't horrible but one common problem I find is that when two balls come at it and they split directions, because of the way it is programmed to follow the balls, it gets stuck(doesn't move).

So, exactly, how would I program the A.I. so it can handle multiple balls coming at it? I was thinking of making it sort of prioritize the balls based on which is closest to it but I am not sure if that will completely solve the problem. Here is my current A.I. code if you could please take a quick look at it.

Code:
```function cpuAI ()
for n = 1,3 do
if Ball[n].xspeed == 5.5 or Ball[n].xspeed == 5 or Ball[n].xspeed == 3.8 then
if Player[2].y + paddleSpot < Ball[n].y and Player[2].y < (Resolution.height - Player[2].img:height ()) then
Player[2].y = Player[2].y + Player[2].speed
end

if Player[2].y + paddleSpot > Ball[n].y and Player[2].y > 0 then
Player[2].y = Player[2].y - Player[2].speed
end
end
end
end```

11. Make the AI choose one ball via a criteria (E.g distance, direction, speed), and move to block it.

12. Okay, here is how I will do it then: First I will compare the distances of the balls. If one is closer then I will make a variable equal to that Ball's number and the variable will be used in the AI in place of n in the code I showed. So basically I will wrap an if statement around the previous AI code. Is that good?

13. First let me say that very good very complete multiball pong AI that takes all possible variables into effect requires a good bit of work/thinking. However, for easier AI that still works well, I would follow this logic pattern:
1-Find out which balls are moving toward the AI player
2-Calculate an estimated frames to arrival for each by taking the distance between it and the AI player and dividing that by the X movement speed of the ball
3-Starting with the ball with the lowest EFA, find its EYA - estimated Y position at arrival. Make sure to account for bouncing off walls.
4-Check if it is possible for the AI player to move from its current Y to a Y where the paddle will block the ball in a number of frames equal to or less than the ball's EFA
5-If yes, this is the ball you want to attempt to block - save it's table index as the ball being blocked for the movement part to move toward
6-If no, repeat from step 3 for the ball with the next lowest EFA
7-Once the ball to block has been identified, move the AI player to the place it needs to be to block the ball
8-When that ball bounces off the AI player, repeat steps 1-7

This will account for and ignore balls that will arrive too soon to be blocked and will also attempt to block the ball that will arrive soonest rather than the ball that is closest in distance.

14. Ah, TurtlesPwn, that is great. In my game the balls all have different speeds so instead of having the AI block the one closest to it it is good that it would block the one arriving the soonest.

EDIT: Nevermind, I know why you need the EYA now.

15. Originally Posted by blah
what do you mean? ... in what way?.
I have a set of primitives in the scene, 2 spheres, and one light source, and I'm photon tracing using pure lua NOT GU FUNCTIONS that defeats the purpose of creating a photon tracer.
The reason I am spliting it into sections is for performance, I was considering an octree but they are really optimized for outdoor, realtime graphics, also I was going to use a BSP tree to start off with then make a kd tree, but I've decided to go straight to a kd tree instead.
If you don't know what photon tracing is, then consider it doing a whole lot of equations to get the colour of a pixel, not unlike real time graphics, but photon trace looks much better.
Now back to my question, I can chose to split the X plane in relation to the Y plane and then in relation to Z using one of a few forumlae.

A good heuristic is the SAH – or surface area heuristic. The basic idea is this: The chance that a ray hits a voxel is related to it's surface area. The area of a voxel is:

area = 2 * width * height * depth

The cost of traversing a voxel can be approximated by the following formula:

cost = Ctraversal + area * prims * Cintersect

Since splitting a voxel always produces two new voxels, the cost of a split at a particular position can be calculated by summing the cost of both sides.

I have made a photon tracer in C# on PC, and calculation where to split the nodes by volume was faster than by primitives in each node. But I don't know which would be faster in PSP lua, I am currently traversing the nodes using Havran's TABrec algorithm (Havran tried a lot of spatial subdivisions and traversal algorithms; TA stands for 'traversal algorithm', rec means recursive and B is the version – His code is a modification of an existing algorithm A). The code is not overly complex, but instead of copying a huge amount of text from his thesis I rather point you to the original document.

So simple enough volume, or primitives? and why?

16. Man, I just keep getting problem after problem. I amr eally sorry but this one is a little of the normal so at least it isn't a typical n00b question. How do I write a System.message() function that has the option to resume the game and the option to return to the menu? The functions readme in LuaPlayer HM doesn't really explain very well how to get it working. I've tried several different ways but non of them really worked properly except for this

Code:
```function menuReturn ()
button = System.buttonPressed (1)

if button == 1 then

else
gamestate = "playing"
end
end```
But even this doesn't work well. When I press YES it just resumes the game and then after I open it up again, any button I press will return to the menu. And sometimes after returning from the menu and then opening it up again it will go back to he menu again no matter what button you press. Is this a LuaPlayer HM problem or a problem with my coding?

EDIT: Also, is there a way to do this differently?

Code:
```function paused ()
button = System.buttonPressed (0)

System.message ("Paused",0)

if button == button then

gamestate = "playing"
end
end```

EDIT: Ah man! Another problem. It works in the menu somehow. After exiting to the menu you can still press select and the System message will come up. I have no idea how that happened.

17. :rtfm:

18. Originally Posted by GP3X
:rtfm:
Well, assuming the manual is the Functions.txt file that comes with LuaPlayer HM I just stated that I read it.

19. There will likely be some information concerning system function calls here as well http://www.lua.org/pil/index.html

20. Originally Posted by GP3X
There will likely be some information concerning system function calls here as well http://www.lua.org/pil/index.html
But isn't this a LuaPlayer/LuaPlayer HM specific problem? At the very least it has to be a problem only the PSP has right?

EDIT: TurtlesPwn I've done msot of the code already but how do I find the EYA? I've been trying to think and think but I can't figure it out.

21. If you know some C or C++ you can probably do most of it yourself.

22. Ha, actually Lua is my first language and the only other language I am learning right now is Stackless Python so I don't think any of that will help. But anyway if I am coding in Lua how would C/C++ help unless I am somehow integrating C/C++ into my code.

23. Ahh if lua is your first language thats alright, was mine too, just looking though the manuels usually answers a lot of questions, find out what parameters they system functions take (if any) and then it should be pretty easy from there.

24. I did the base for you, now you change it to work for your game and finish it up:
Code:
```function getEYA(ballNum,frames)
local y=ball[ballNum].y
local ym=ball[ballNum].ym
for i = 1, frames do
y = y + ym
--put stuff for checking if it hits a side and changing YM to make it bounce off but use the local y and ym in it instead, ignore stuff for X
end
return y
end
--use like this:
eya = getEYA(numberOfBall,EFA)```

25. Thanks TurtlesPwn! That's great! Ha, I was just using the EFA while waiting for an answer and it already was working significantly better. I can't wait to see it work with EYA and the rest of the AI finished. Really, thanks, alot!

26. Yup you're welcome. Post up your code when you get your AI all finished, I'd like to see how it looks. Or if you're releasing rather soon (next day or two) dont worry about it Ill just look there

27. Yeah, this is just one of the last game modes I'm implementing then I just have to clean up everything and then I will release it. Should only take a couple of days to finish up if no problems occur. Really, thanks a lot! (I know I repeated the sentence over from last post but seriously, THANKS!)

EDIT: Oh yeah, I forgot, does anyone have an answer to my System.message() problem?

28. 2 questions:
1. Instead of "press x to do something", how do I make it "press x twice to do something"?

2. The Lua question from here:
Creating PARAM.SFO

29. Originally Posted by CoMbiNa
2 questions:
1. Instead of "press x to do something", how do I make it "press x twice to do something"?
Put in a counter that increments everytime you press X.

30. Originally Posted by yaustar
Put in a counter that increments everytime you press X.
kk, haven't thought of that.
(I thought there is a way to simply make it like:
if pad:cross() twice then bla bla bla end
just an example...)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

All times are GMT -8. The time now is 02:57 AM.

Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY