Here's a new version of the teams demo that shows a basic implementation of multiple teams. I added a third yellow team that will attack both green and red. I created them as enemies because they are hostile to the player - if they weren't I'd create them as allies.
While Curtis's idea probably works I think this method is better because it allows the AI system to prioritize the targets like it normally would. So if it's hostile to the player, it won't temporarily become an ally and therefore ignore you, but still attack you if it finds it neccassary. It also doesn't require all that much extra code.
So for those of you who wanted to have zombies that hate EVERYBODY...here ya go
remstart
*
* Teams
*
* -Creates 3 teams (friendly(green), enemy(red) and neutral(white))
* -Neutrals will run away from combat, friendlies will defend you, enemies will attack you
* -Sounds are enabled in this demo when an entity fires and will trigger nearby entities
*
* -Compiled with Version 6.1
*
remend
sync on : sync rate 0 : autocam off
set text font "Verdana" : set text size 14
rem creates the AI system and sets everything to default.
AI Start
rem sets the global radius, all entities will assumed to be this size
AI Set Radius 2.5
MakeBackground()
MakeLevel()
dim shootTimer(30) as float
rem make a patrol path
AI Make Path 1
AI Path Add Point 1,40,40
AI Path Add Point 1,-40,40
rem entity template
make object cone 1,5
xrotate object 1,90
fix object pivot 1
make mesh from object 1,1
delete object 1
rem attack template
make object plain 1,0.3,1,1
xrotate object 1,90
fix object pivot 1
make mesh from object 2,1
delete object 1
rem single color images
MakeColorImage(11,1,rgb(255,0,0))
MakeColorImage(12,1,rgb(0,255,0))
MakeColorImage(55,1,RGB(255,228,79))
dim team_ref(30)
rem make 3 enemies
for i = 2 to 4
make object i,1,0
position object i,i*5 - 30,2.5,40
texture object i,11
make object i+1000,2,11
set object light i+1000,0
hide object i+1000
rem add them to the enemy team and set their speed (in units per second)
AI Add Enemy i
AI Set Entity Speed i,10.0
AI Set Entity Aggressive i
AI Set Entity View Arc i,90,180
AI Set Entity View Range i,60
AI Set Entity Hearing Range i,100
team_ref(i) = 2
next i
rem assign entities their patrol paths (entityNum, pathNum)
AI Entity Assign Patrol Path 2,1
AI Entity Assign Patrol Path 3,1
AI Entity Assign Patrol Path 4,1
rem make 3 cilivians
for i = 11 to 13
make object i,1,0
position object i,rnd(50)-25,2.5,rnd(50)-25
rem add them to the enemy team and set their speed (in units per second)
AI Add Neutral i
AI Set Entity Speed i,10.0
AI Set Entity View Arc i,90,180
AI Set Entity Hearing Range i,100
team_ref(i) = 0
next i
rem make 2 friendlies
for i = 21 to 22
make object i,1,0
position object i,i*5 - 120,2.5,-40
texture object i,12
make object i+1000,2,12
set object light i+1000,0
hide object i+1000
rem add them to the enemy team and set their speed (in units per second)
AI Add Friendly i
AI Set Entity Speed i,10.0
AI Set Entity View Arc i,90,180
AI Set Entity View Range i,60
AI Set Entity Hearing Range i,80
team_ref(i) = 1
next i
rem make 3 of another team :P
for i = 23 to 25
make object i,1,0
position object i,i*5 - 30,2.5,40
texture object i,55
make object i+1000,2,11
set object light i+1000,0
hide object i+1000
rem add them to the enemy team and set their speed (in units per second)
AI Add Enemy i
AI Set Entity Speed i,10.0
AI Set Entity Aggressive i
AI Set Entity View Arc i,90,180
AI Set Entity View Range i,60
AI Set Entity Hearing Range i,100
team_ref(i) = 3
next i
rem make player
make object sphere 1,5
position object 1,20,2.5,-40
AI Add Player 1
position camera 0,100,-30 : point camera 0,0,-5
currtime# = timer()
lasttime# = currtime#
do
currtime# = timer()
speed# = 0.06*(currtime# - lasttime#)
lasttime# = currtime#
if upkey()=1 then move object 1,speed#
if downkey()=1 then move object 1,-speed#
if leftkey()=1 then move object left 1,speed#
if rightkey()=1 then move object right 1,speed#
rem handle debugging output
if keystate(2)=1 and ptimer<timer()
ptimer = timer()+300
pMode = 1-pMode
if ( pMode=0 ) then AI Debug Hide Paths else AI Debug Show Paths 2.5
endif
if keystate(3)=1 and btimer<timer()
btimer = timer()+300
bMode = 1-bMode
if ( bMode=0 ) then AI Debug Hide Obstacle Bounds 0 else AI Debug Show Obstacle Bounds 0,2.5
endif
if keystate(33)=1 and ftimer<timer()
ftimer = timer()+300
AI Team Follow Player 20
endif
if keystate(31)=1 and stimer<timer()
stimer = timer()+300
AI Team Separate
endif
rem handle F1 key
if ( keystate(59)=1 and f1Timer<timer() )
f1Timer = timer()+300
F1Pressed=1-F1Pressed
endif
if ( F1Pressed )
rem display info
set cursor 0,0
print "Enemies Are Red, Friendlies Are Green, Neutral Is White"
print
print "Controls: "
print "[ArrowKeys] Move Player"
print "[F] Make Friendlies Follow Player"
print "[S] Stop Friendlies Following Player"
print
print "Debug Controls: "
print "[1] Toggle Entity Path"
print "[2] Toggle Obstacle Bounds"
print
print "FPS: ";screen fps()
else
center text screen width()/2, 30, "-- Press F1 For Help --"
endif
rem hide attack lines
for i=1001 to 1025
if object exist(i) and shootTimer(i-1000)<70 then hide object i
next i
rem Call this command every sync to update entity AI state
AI Update
for i = 2 to 30
if AI Entity Exist(i)=1
my_team = team_ref(i)
if my_team <> 0 `Civilians still don't figh
for k = 2 to 30
if Ai Entity Exist(k)=1 and k <> i
if my_team <> team_ref(k) and team_ref(k) <> 0 `Don't attack civilians! Of course you could make them extra evil if you want :P
if Ai Get Entity Can See(i,object position x(k),object position y(k),1)
AI Entity Add Target i,k
endif
endif
endif
next k
endif
if AI Get Entity Can Fire(i) and shootTimer(i)<=0
tx# = AI Get Entity Target X(i)
tz# = AI Get Entity Target Z(i)
x# = object position x(i)
z# = object position z(i)
dx# = ( x# + tx# ) / 2.0
dz# = ( z# + tz# ) / 2.0
dist# = sqrt ( (tx#-x#)*(tx#-x#) + (tz#-z#)*(tz#-z#) )
ang# = acos ( (tz#-z#) / dist# )
if ( (tx#-x#) < 0 ) then ang# = 360 - ang#
position object i+1000,dx#,2.5,dz#
yrotate object i+1000,ang#+0.1
scale object i+1000,100,100,100*dist#
show object i+1000
shootTimer(i)=100
AI Create Sound x#,z#,11,100
endif
endif
if shootTimer(i)>0 then shootTimer(i) = shootTimer(i)-(speed#*3)
next i
sync
loop
function MakeBackground()
load image "stripe5.png",1
load image "stripe6.png",2
rem create the background object
make object sphere 99,400,48,48
texture object 99,1
scale object texture 99,6,6
set object cull 99,0
set object light 99,0
rotate object 99,0,0,90
rem add the logo across the bottom
load image "logo.png", 100000
sprite 1, 0, screen height() - 60, 100000
endfunction
function MakeLevel()
rem make floor
make object box 100,100,1,100
position object 100,0,-0.5,0
texture object 100,2
i = 111
for x = -1 to 1
for z = -1 to 1
if ( x<>0 or z<>0 )
y# = rnd(5)+5
make object box i,12,y#,12
position object i,x*30,y#/2,z*30
color object i,rgb(rnd(180),rnd(180),rnd(180))
AI Add Static Obstacle i
inc i
endif
next y
next x
rem make boundary obstacle, encloses the space to keep the entity in
AI Start New Obstacle
rem add points in a anti-clockwise direction to create a boundary (use clockwise to create an obstacle)
AI Add Obstacle Vertex -50,-50
AI Add Obstacle Vertex 50,-50
AI Add Obstacle Vertex 50,50
AI Add Obstacle Vertex -50,50
rem finish creating our boundary
AI End New Obstacle 0,1
AI Complete Obstacles
endfunction
function MakeColorImage(image,mem,color)
make memblock mem,16
write memblock dword mem,0,1
write memblock dword mem,4,1
write memblock dword mem,8,32
write memblock dword mem,12,color
make image from memblock image,mem
delete memblock mem
endfunction