QJ.NET | Videos | Forums | iPhone | MMORPG | Nintendo DS | Wii | PlayStation 3 | PSP | Xbox 360 | PC | Downloads | Contact Us
Forums | Gaming News | Videos | Downloads | Today's Posts | Mark Forums Read | Chat | FAQ | Members List | Contact

QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides

Go Back   QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides > Developers Corner > PSP Development, Hacks, and Homebrew > PSP Development Forum
The above video goes away if you are a member and logged in, so log in now!

Some hints for AI Enemy

This is a discussion on Some hints for AI Enemy within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; LuA Hi all I am working on my game for a while and I need some help with this: Everytime ...

Reply
 
LinkBack Thread Tools
Old 06-11-2009, 05:39 PM   #1
 
Join Date: May 2008
Posts: 93
Trader Feedback: 0
Default Some hints for AI Enemy

LuA
Hi all
I am working on my game for a while and I need some help with this:
Everytime the player presses circle a new enemy spawns
soo
enemyindex=0
enemy[enemyindex]={x=0,y=0,hp=100,image="e nemywalksright.png"

if pad:circe() and enemyindex <= 10 then
enemyindex=enemyidex+1
end

Now I want to make a for loop i think where i can place all enemy actions and all enemies must act not at the same time, Because else the enemy images will overlay eachother if they are walking to the player so all enemies must have different time when they act..
What I am asking for is a nice for loop with the things that i mentioned..

Last edited by Predricted; 06-11-2009 at 07:26 PM..
Predricted is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-11-2009, 05:56 PM   #2

Developer
 
slicer4ever's Avatar
 
Join Date: Jul 2005
Location: everywhere
Posts: 3,349
Trader Feedback: 0
Default

your idea is correct on what to do, simple expand it to add a variable named action, than in the loop you can do:

Code:
for(int i=0;i<maxenemys;i++){
   if(enemy[i].action == 0){
      //don't do anything
   }else if(enemy[i].action==1){
      //attack
   }else if(enemy[i].action==2){
     //look for enemy
   }
}
etc, theirs your basic concept of what to do
__________________
1. Failed....again...
2. http://slicer.gibbocool.com/ stay updated on all my projects
slicer4ever is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-11-2009, 06:57 PM   #3
 
Join Date: May 2008
Posts: 93
Trader Feedback: 0
Default

Quote:
Originally Posted by slicer4ever View Post
your idea is correct on what to do, simple expand it to add a variable named action, than in the loop you can do:

Code:
for(int i=0;i<maxenemys;i++){
   if(enemy[i].action == 0){
      //don't do anything
   }else if(enemy[i].action==1){
      //attack
   }else if(enemy[i].action==2){
     //look for enemy
   }
}
etc, theirs your basic concept of what to do
Thanks that helps but 1 thing more at the end of the for loop I must add
Code:
if i>=maxenemys then 
i=0 
end
true??
PS. your code is in C i was talking about lua but no big deal haha
Predricted is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-11-2009, 07:38 PM   #4

Developer
 
slicer4ever's Avatar
 
Join Date: Jul 2005
Location: everywhere
Posts: 3,349
Trader Feedback: 0
Default

i understand, however it's been such a long time that i've used lua i don't fully remember how to do a for loop in it properly, however i kept my code as psuedo as possible, as to your question, no, here's what you do:
Code:
while true do
   for i = 0 i < maxenemys; i++ //like i said, don't remeber
   //code to handle enemys
   end
   //code to handle player like normal(doesn't neccissarly have to be below the loop, up to you
end
now the code well be repeated
__________________
1. Failed....again...
2. http://slicer.gibbocool.com/ stay updated on all my projects
slicer4ever is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-11-2009, 07:58 PM   #5
 
Join Date: May 2008
Posts: 93
Trader Feedback: 0
Default

Okay I made this code but I get this error that i can't get rid off
Code:
enemy={}
enimg="images/enemy/imp_left.png"
enemy[1]={x=25,y=23,hp=100,image="images/enemy/imp_up.png",imageload=Image.load(enimg)}
enemies=1


while true do
screenwaitvblankstart
--movement enemy
if enemies > 1 then
for i=0, enemies do
i=i+1
if enemy[i].x > player1[1].x then
enemy[i].x=enemy[i].x-1
enemy[i].image="images/enemy/imp_left.png"
end
if enemy[i].x < player1[1].x then
enemy[i].x=enemy[i].x+1
enemy[i].image="images/enemy/imp_right.png"
end
if enemy[i].y > player1[1].y then
enemy[i].y=enemy[i].y-1
enemy[i].image="images/enemy/imp_up.png"
end
if enemy[i].y < player1[1].y then
enemy[i].y=enemy[i].y+1 
enemy[i].image="images/enemy/imp_down.png"
end
enemy[i].imageload = Image.load(enemy[i].image)
screen.startDraw()
Image.blit(enemy[i].x,enemy[i].y,enemy[i].imageload)
screen.endDraw()
if i == enemies then
i=0
end
end
end

if pad:circle() and oldpad:circle() ~= pad:circle() then
enemies=enemies+1
enemy[enemies]={x=25,y=23,hp=100,image="images/enemy/imp_up.png",imageload=Image.load(enimg)}
end
error :
script.lua:109:
attempt to index value "?"(a nil value)


it happens when i press circle
-=Double Post Merge =-
Code:
enemy={}
enimg="images/enemy/imp_left.png"
enemy[1]={x=25,y=23,hp=100,image=enemy_up.png",imageload=Image.load(enimg)}
enemies=1

while true do
screenwaitvblankstart

enemy movement begins here

--movement enemy

if enemies > 1 then
while true do
for i=0, enemies do
i=i+1
THE LINE BELOW THIS LINE IS THE LINE WHERE THE ERROR TAKES PLACE
if enemy[i].x > player1[1].x then
enemy[i].x=enemy[i].x-1
enemy[i].image="enemy_left.png"
end
if enemy[i].x < player1[1].x then
enemy[i].x=enemy[i].x+1
enemy[i].image="enemy_right.png"
end
if enemy[i].y > player1[1].y then
enemy[i].y=enemy[i].y-1
enemy[i].image="enemy_up.png"
end
if enemy[i].y < player1[1].y then
enemy[i].y=enemy[i].y+1 
enemy[i].image="enemy_down.png"
end
enemy[i].imageload = Image.load(enemy[i].image)
screen.startDraw()
Image.blit(enemy[i].x,enemy[i].y,enemy[i].imageload)
screen.endDraw()
if i >= enemies then
i=0
end
end
end
end

now when you press circle enemies variable increases by 1

if pad:circle() and oldpad:circle() ~= pad:circle() then
enemies=enemies+1
enemy[enemies]={x=25,y=23,hp=100,image="enemy_up.png",imageload=Image.load(enimg)}
end
Okay this error shows up:
attempt to index field '?'(a nil value)

Last edited by Predricted; 06-11-2009 at 08:12 PM.. Reason: Automerged Doublepost
Predricted is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-11-2009, 11:33 PM   #6
Enter Custom Title
 
dan369's Avatar
 
Join Date: Jan 2008
Real First Name: Dan
Location: Wales, cardiff
Just Played: Overlord 2
Posts: 1,306
Blog Entries: 1
Trader Feedback: 0
Default

for- loops in Lua start at 1 not 0, that's C. Also a better way to stop re-swaning enemies would be to use a bool.
Code:
 
maxNumOfEnemies = 5 
maxEnemiesReached = false

--Note this doesn't need to be in a for-loop
if #enemies >= maxNumOfEnemies then
      maxEnemiesReached = true;
else
      maxEnemiesReached = false;
end

--then just add this
if pad:circle() and oldpad:circle() ~= pad:circle() and not maxEnemiesReached then
Your method of creating new enemies wouldn't work, use table.insert()
dan369 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-12-2009, 02:50 AM   #7
 
Join Date: May 2008
Posts: 93
Trader Feedback: 0
Default

Quote:
Originally Posted by dan369 View Post
for- loops in Lua start at 1 not 0, that's C. Also a better way to stop re-swaning enemies would be to use a bool.
Code:
 
maxNumOfEnemies = 5 
maxEnemiesReached = false

--Note this doesn't need to be in a for-loop
if #enemies >= maxNumOfEnemies then
      maxEnemiesReached = true;
else
      maxEnemiesReached = false;
end

--then just add this
if pad:circle() and oldpad:circle() ~= pad:circle() and not maxEnemiesReached then
Your method of creating new enemies wouldn't work, use table.insert()
Your way is easier of making not more then xx amount of enemies
Okay now can you tell me how to use table.insert()
I've tried this
Code:
if pad:circle() and oldpad:circle() ~= pad:circle() then
enemies=enemies+1
table.insert(enemy,x)
table.insert(enemy,y)
table.insert(enemy,hp)
table.insert(enemy,image)
table.insert(enemy,imageload)
end
and still no succes the problem is
that x,y,hp,image,imageload needs a variable because when a new enemy is created it will directly follow the enemy so it will use x and y in if statements but they don't have a variable..
Predricted is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-12-2009, 03:45 AM   #8

Developer
 
Join Date: Mar 2006
Posts: 1,026
Trader Feedback: 0
Default

You need to insert a table into the enemy table.
__________________

Check out my homebrew & C tutorials at http://insomniac.0x89.org/
Coder formerly known as Insomniac197

Quote:
tshirtz: what is irshell ??
Atarian_: it's where people who work for the IRS go when they die
Insert_Witty_Name is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-12-2009, 04:33 AM   #9
Enter Custom Title
 
dan369's Avatar
 
Join Date: Jan 2008
Real First Name: Dan
Location: Wales, cardiff
Just Played: Overlord 2
Posts: 1,306
Blog Entries: 1
Trader Feedback: 0
Default

http://lmgtfy.com/?q=table.insert()+lua
dan369 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-12-2009, 05:43 AM   #10
 
_df_'s Avatar
 
Join Date: May 2007
Posts: 120
Trader Feedback: 0
Default

Quote:
Originally Posted by Predricted View Post
Code:
if enemies > 1 then
	for i=0, enemies do
		i=i+1
		
		......
		
		if i == enemies then
			i=0
		end
	end
end
your lua code is godawful.

just use iteration over the table then you wont need all those hacks in your code.

Code:
local key, enemy
for key,enemy in pairs(enemies) do
	if enemy.y < player.y 
		....
	elseif
	else
	end
end
__________________
-- Code Monkey : Sarien, Fishguts, Cracks and Crevices --

"Did IQ's just drop sharply while I was away?" (Ripley)
_df_ is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-12-2009, 11:55 AM   #11
 
Join Date: May 2008
Posts: 93
Trader Feedback: 0
Default

Quote:
Originally Posted by Insert_Witty_Name View Post
You need to insert a table into the enemy table.
Can you tell me how to do that??
I found a few things like this
table.insert(table, [pos,] value)
but i still don't undersatand??
Predricted is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-12-2009, 12:10 PM   #12
Enter Custom Title
 
dan369's Avatar
 
Join Date: Jan 2008
Real First Name: Dan
Location: Wales, cardiff
Just Played: Overlord 2
Posts: 1,306
Blog Entries: 1
Trader Feedback: 0
Default

table.insert( table, {x = 0, y = 0, img = etc});

pos - postiton to insert into
table - the table to insert too
value - the values you want to be inserted
dan369 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-13-2009, 02:42 AM   #13

Developer
 
Nielkie's Avatar
 
Join Date: Jul 2006
Posts: 549
Trader Feedback: 0
Default

Quote:
Originally Posted by _df_ View Post
Code:
local key, enemy
for key,enemy in pairs(enemies) do
    if enemy.y < player.y 
        ....
    elseif
    else
    end
end
Code:
for i,enemy in ipairs(enemies) do
    if enemy.y < player.y 
        ....
    elseif
    else
    end
end
__________________


Nielkie is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-13-2009, 01:48 PM   #14
 
Join Date: May 2008
Posts: 93
Trader Feedback: 0
Default

Quote:
Originally Posted by dan369 View Post
table.insert( table, {x = 0, y = 0, img = etc});

pos - postiton to insert into
table - the table to insert too
value - the values you want to be inserted
LOL the funny thang is that this is all I needed to fix the code
Thanks
Predricted is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
enemy , hints

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -8. The time now is 01:52 AM.



Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY
Copyright © 2009, QJ.NET. All Rights Reserved.
Contact Us