Hi,
I am a little lazy at the moment and I read a lot of threads:"Can I have AI vs AI in GG now?" The most common answer is, it is on the vote board.
So I decided to stop being lazy and make a new project. This project is a AI which fights against Ai and the player and in this thread I will show my small success on the AI road.
Of course everybody is invented to modify the script and upload it. A communtiy AI would be cool.
Attention heavy scripting and advanced math knowledge needed
So what I thought how it could work:
1) Select AI out of a List
2) Rotate to this target
3) Make him damage
There should be some more things, but they are the most important and they work, even when there is a lot of pottential to optimize it. Please do not bash me with information lecture. I have them and I hope I can implement some things
So first I create a AI_List and a target List:
--AI Arrays
ai_list_place=1
AI_List={} --Complete List of all AI character
AI_Chosen_Enemy={} -- List of AI characters who are chosen from other AI characters as target
EntityDist = {} -- Distance to taget
ai_soldier_target={} -- a string which shows if an AI or the player is the target
ai_soldier_target_list_position={} -- Safes the position of the target in the AI_List array for deleting it if it is destroyed
and fill this in the _init function(You could read this later in the full script)
Then I search for a target:
-- Choose an enemy out of the AI List
-- A lot optimization possible
function ChooseEnemy(e)
if AI_Chosen_Enemy[e] == nil then
for i=1, #AI_List do
if AI_List[i]~=e and AI_List[i]~=nil and calcEntityDistance(e,AI_Chosen_Enemy[e])>calcEntityDistance(e,AI_List[i]) then
if GetPlayerDistance(e) > calcEntityDistance(e,AI_List[i]) then
AI_Chosen_Enemy[e]=AI_List[i]
ai_soldier_target[e]="AI"
ai_soldier_target_list_position[e]=i
else
ai_soldier_target[e]="Player"
RotateToPlayer(e)
end
end
end
end
end
if the player is nearer than any enemy AI, the AI chose the player as target and it works like the standart AI_soldier.lua
Ok now we have a target, then we must rotate to him:
--Angle calculation between two entities, so you can rotate them with setrotation(e,x,y,z)
function calRotationAngleToEntity(e1,e2)
if AI_Chosen_Enemy[e1] ~= nil then
side_opposite_an_angle[e1]=g_Entity[e1]['x']-g_Entity[e2]['x']
side_adjacent_an_angle[e1]=g_Entity[e1]['z']-g_Entity[e2]['z']
Hypotenuse[e1]=calcEntityDistance(e1,e2)
AI_Rotation_Angle[e1]=math.floor(math.deg(math.asin(math.abs(side_opposite_an_angle[e1])/Hypotenuse[e1]))+0.5)
if side_opposite_an_angle[e1] > 0 then
if side_adjacent_an_angle[e1] > 0 then
AI_Rotation_Angle[e1]=AI_Rotation_Angle[e1]+180
elseif side_adjacent_an_angle[e1] < 0 then
AI_Rotation_Angle[e1]=-AI_Rotation_Angle[e1]
else
AI_Rotation_Angle[e1]=270
end
elseif side_opposite_an_angle[e1] < 0 then
if side_adjacent_an_angle[e1] > 0 then
AI_Rotation_Angle[e1]=180-AI_Rotation_Angle[e1]
elseif side_adjacent_an_angle[e1] < 0 then
AI_Rotation_Angle[e1]=AI_Rotation_Angle[e1]
else
AI_Rotation_Angle[e1]=90
end
else
if side_adjacent_an_angle[e1] > 0 then
AI_Rotation_Angle[e1]=180
elseif side_adjacent_an_angle[e1] < 0 then
AI_Rotation_Angle[e1]=0
else
AI_Rotation_Angle[e1]=0
end
end
end
end
It is very simple if you undertstand that
sinus=side_opposite_an_angle/Hypotenuse
If you calculate this you have an angle "alpha"
Why you must add 90,180 degree show this picture.(I know I can not paint very good. Forgive me)
Now we have a target.
Then we want that the AI is in attack position:
if ai_soldier_state[e] ~= "attackposition" then
CharacterControlLimbo(e)
SetAnimationFrames(100,205)
LoopAnimation(e)
ModulateSpeed(e,1.0)
SetAnimationSpeed(e,1.0)
StartTimer(e)
ai_soldier_state[e] = "attackposition"
end
and of course he should damage the target:
if GetTimer(e) > 250 then
--Make the target a random damage between 1 and 6
SetEntityHealth(AI_Chosen_Enemy[e],g_Entity[AI_Chosen_Enemy[e]]['health']-math.random(1,6))
StartTimer(e)
end
Of course this are just the ground basics, but I will develope more.
Sorry for the big post
Attach files:
2 pictures
Ai script(I called it darkai_soldier according to the old FPSC Classic AI which is called Dark AI
)
Have a nice day and thanks for reading
Corno_1
My dream is to develope games, which makes fun when I create it and fun when other people play it.