View unanswered posts | View active topics It is currently Thu Dec 26, 2024 4:11 am



Reply to topic  [ 8 posts ] 
 Custom AI? 
Author Message

Joined: Fri Nov 23, 2012 5:42 am
Posts: 143
Reply with quote
Post Custom AI?
Melee weapons don't really work in cortex command mainly because the AI is too stupid to use them. They usually just stand still trying to hit the enemy with a sword from half a screen away.
So I modified the default AI, making any actor with a weapon in the "melee weapons" group go into the handtohand attack mode, so that they actually close the distance.

It works pretty well, however when I try and make my custom actors use my modified AI, I get a bunch of error messages in the console:

Code:
ERROR: knights.rte/Actors/AI/HumanAI.lua:3: module 'knights.rte/Actors/AI/NativeHumanAI' not found:
   no field package.preload['knights.rte/Actors/AI/NativeHumanAI']
   no file '.\knights\rte/Actors/AI/NativeHumanAI.lua'
   no file 'C:\Users\Thomas\Desktop\Cortex Command\lua\knights\rte/Actors/AI/NativeHumanAI.lua'
   no file 'C:\Users\Thomas\Desktop\Cortex Command\lua\knights\rte/Actors/AI/NativeHumanAI\init.lua'
   no file 'C:\Users\Thomas\Desktop\Cortex Command\knights\rte/Actors/AI/NativeHumanAI.lua'
   no file 'C:\Users\Thomas\Desktop\Cortex Command\knights\rte/Actors/AI/NativeHumanAI\init.lua'
   no file 'Base.rte/knights\rte/Actors/AI/NativeHumanAI.lua'
   no file '.\knights\rte/Actors/AI/NativeHumanAI.dll'
   no file 'C:\Users\Thomas\Desktop\Cortex Command\knights\rte/Actors/AI/NativeHumanAI.dll'
   no file 'C:\Users\Thomas\Desktop\Cortex Command\loadall.dll'
   no file '.\knights.dll'
   no file 'C:\Users\Thomas\Desktop\Cortex Command\knights.dll'
   no file 'C:\Users\Thomas\Desktop\Cortex Command\loadall.dll'


It loads the first AI file, the one defined in the actors ini, but it doesn't seem to want to load the second file. I checked the pathing and that all seems to be ok, so I don't know what the problem is. Overwriting the base AI files works fine, but I'd rather not do that. Is overwriting the base AI files the only way to play with my modified AI?

My HumanAI file in case it helps:

Code:
dofile("knights.rte/Constants.lua")
require("knights.rte/Actors/AI/NativeHumanAI")   --dofile("Base.rte/Actors/AI/NativeHumanAI.lua")

function Create(self)
   self.AI = NativeHumanAI:Create(self)
end

function UpdateAI(self)
   self.AI:Update(self)
end


Mon May 13, 2013 8:24 pm
Profile
User avatar

Joined: Wed Jan 24, 2007 11:53 pm
Posts: 589
Location: Silicon Valley, California
Reply with quote
Post Re: Custom AI?
As far as I could tell, even if the paths are correct you can only load one lua file per actor. I encountered this when I tried to add the base human AI to a soldier that switches teams using another script, it would only load the first lua file. However, from this post, I think you can just paste one lua file into the other. In your case, you could try copying what's found in the base human AI lua file, and pasting it into your melee script before your new stuff.

I'd really like to see how it works out for you; the zombies in our DayZ mod have the same problem with melee. Forcing the zombies to walk by making their idle path a copy of their walkpath was the only alternative I could come up with, but that really limits them otherwise (especially if you tried to play as one of them).


Mon May 13, 2013 8:48 pm
Profile

Joined: Fri Sep 10, 2010 1:48 am
Posts: 666
Location: Halifax, Canada
Reply with quote
Post Re: Custom AI?
What uber said is probably a problem. However your problem here is with the use of require. I'm not sure if its path can be changed out of base.rte but for now, use dofile instead. It's slightly slower apparently, but you can change the path.


Mon May 13, 2013 9:16 pm
Profile

Joined: Fri Nov 23, 2012 5:42 am
Posts: 143
Reply with quote
Post Re: Custom AI?
Bad Boy wrote:
What uber said is probably a problem. However your problem here is with the use of require. I'm not sure if its path can be changed out of base.rte but for now, use dofile instead. It's slightly slower apparently, but you can change the path.


Thanks, that did it.

uberhen wrote:
I'd really like to see how it works out for you; the zombies in our DayZ mod have the same problem with melee. Forcing the zombies to walk by making their idle path a copy of their walkpath was the only alternative I could come up with, but that really limits them otherwise (especially if you tried to play as one of them).


I don't know much about scripting so I used a rather crude method, I just edited the base AI files so that the start of the CreateAttackBehavior function in NativeHumanAI.lua looks like this:

Code:
function NativeHumanAI:CreateAttackBehavior(Owner)
   self.ReloadTimer:Reset()
   self.TargetLostTimer:Reset()

   if Owner:EquipFirearm(true) then
      if Owner.EquippedItem:HasObjectInGroup("Melee Weapons") then   
         self.NextBehavior = coroutine.create(HumanBehaviors.AttackTarget)
         self.NextBehaviorName = "AttackTarget"
      else
         self.NextBehavior = coroutine.create(HumanBehaviors.ShootTarget)
         self.NextBehaviorName = "ShootTarget"
      end


that function seems to get called when an actor spots a target, I just added an if statement to check if the equipped item belongs to the "melee weapons" group. The "self.NextBehaviorName = "AttackTarget" sets the actors attack mode to unarmed attack, which is much beter at closing distance than the ShootTarget behaviour.

Then in HumanBehavoirs.lua, where the AttackTarget behavior is defined, I did this:

Code:
   if Owner.EquippedItem then
      HD = ToHeldDevice(Owner.EquippedItem)
      if HD:IsTool() then   -- attack with digger
         local Dist = SceneMan:ShortestDistance(HD.MuzzlePos, AI.Target.Pos, false)
         if Dist.Magnitude < 40 then
            AI.Ctrl.AnalogAim = SceneMan:ShortestDistance(Owner.EyePos, AI.Target.Pos, false).Normalized
            AI.fire = true
         else
            AI.fire = false
         end
      elseif HD:HasObjectInGroup("Melee Weapons") then
         local Dist = SceneMan:ShortestDistance(HD.MuzzlePos, AI.Target.Pos, false)
         if Dist.Magnitude < 50 then
            AI.Ctrl.AnalogAim = SceneMan:ShortestDistance(Owner.EyePos, AI.Target.Pos, false).Normalized
            AI.fire = true
         else
            AI.fire = false
         end         
      else
         break
      end
   -- else TODO: periodically look for weapons?
   end


Which makes the actor "fire" his melee weapon when in range.

It's not perfect, the actors don't aim properly and still like to just crush enemies if they can, but it's a lot beter than watching them freeze as soon as they spot an enemy.


Mon May 13, 2013 10:36 pm
Profile
User avatar

Joined: Wed Jan 24, 2007 11:53 pm
Posts: 589
Location: Silicon Valley, California
Reply with quote
Post Re: Custom AI?
Heh, guess I should just stick to helping with .ini and leave the lua stuff to actual programmers. :P

Glad that you got it working, and thanks a lot for showing how you did it. One question: in your AttackTarget behavior, I saw the melee weapons have a distance magnitude of 50; does that mean they will walk to within 50 pixels of the enemy and start swinging?


Mon May 13, 2013 10:55 pm
Profile

Joined: Fri Nov 23, 2012 5:42 am
Posts: 143
Reply with quote
Post Re: Custom AI?
Yes, I think. They do get closer than 50 pixels, they just starting swinging at 50 pixels.

And bad Boy's fix doesn't work after all, I just forgot to change back to the normal AI files in base.rte. Doh.

But the error has gotten smaller:

Code:
ERROR: cannot open knights.rte/Actors/AI/NativeHumanAI: No such file or directory


Am I just stupid for trying to use essentially modified copies of the base AI files? I was under the impression cortex command could handle 2 version of (virtually) the same lua file without any problems. Would changing the function names and maybe even the variables to unique versions work?


Tue May 14, 2013 12:11 am
Profile

Joined: Fri Sep 10, 2010 1:48 am
Posts: 666
Location: Halifax, Canada
Reply with quote
Post Re: Custom AI?
That suggests and error with your filepath. That said, are you using the base behaviours file or did you make your own? If the latter, you may want to rename the global table and all the functions since it's possibly it screwed things up (though if you didn't modify anything it should be fine).
To go into more detail, (and I may make mistakes here, it's been a while since I've touched the ai and it was in a slightly older version) HumanBehaviours.lua creates a global table of human behaviours, presumably so they can be referenced in nativehumanai. You probably won't be touching that so you can just use it as is and keep the line saying require(...HumanBehaviours.lua). However, if you do plan to change them, you'll want to change the name of your global behaviour table, to something like HumanKnightBehaviours. You'd then have to change all the behaviour function names to reflect that, since they're all elements of that table.

If you can't get it working, feel free to post up the .rte, I'll take a look at it and see if I can get it running for you.


Tue May 14, 2013 2:18 am
Profile

Joined: Fri Nov 23, 2012 5:42 am
Posts: 143
Reply with quote
Post Re: Custom AI?
Sorry, real life got in the way, but I tried renaming the tables and functions. Didn't work. I then tried renaming my custom ai files and moved them back into base and changed the dofile back into require and that does actually work. So I'm guessing uber was right and cortex command just doesn't like to load multiple lua files per actor, unless those files are in base. It's not a perfect solution, but it's beter than overwriting the base files, so I think I'll just leave it like this.


Thu May 23, 2013 1:30 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 8 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.040s | 14 Queries | GZIP : Off ]