Archived

This topic is now archived and is closed to further replies.

ColdfireV

AI scripting

Recommended Posts

I wanted to post this here, because not many people read the AI board often. Anyway... How is it possible to write an AI scripting language in C++? I''ve been thinking about it for awhile, but no good ideas have come up. Of course, I could do a function that have a million different IF-THEN''s or CASE''s or whatever conditionals there are, but that''s just not good enough. I''m talking about a scripting language that''s truly robust, non-compiled, and fast enough to be run in a game. Here''s an example of the script itself: OBJECT Light //declaration IF (DISTANCE(Player) < 100) TURNON() END IF END OBJECT Yeah, that''s just pretty basic... But let''s say that for every object created in the game, you can have the flexibility to do anything that''s possibly done by the object. Obviously, there would be some hard-coding of the C++ code to do the action, but it must able to be called by the script. How can I possibly start working on something like that? Any ideas would be great. ColdfireV

Share this post


Link to post
Share on other sites
i guess you could make a file parser and compile the script runtime and have like a separate thread manage the AI script.. that''s rather daunting.

You could also set up a sorta half script where there a some predefined modes of AI motion, but the script defines how and when to switch into the different motions.

I''m out of ideas and looking for more too.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You could use a scripting language to do this. Lua interfaces with c/c++ quite easily and is reasonably fast (I haven''t done testing but it works well for my AI scripting needs)

Share this post


Link to post
Share on other sites
I wrote an AI scripter that took logical statements like the one you described and parsed them into logical operator trees attached to functions. It was all done at load time but could be parsed on the fly.

Bascially you''d need all your functions to have tags that were defined in the scripting language and could be translated into function pointers (mine were all void* (function) (void*)) for use in the game. It was based around controlling npc actions more than object but it could be used for both with little change. You''re welcome to the code which is fairly well written and well commented.

I even put in an ability to define your own statements so you could make the script langage as English as you want.

Mike

Share this post


Link to post
Share on other sites
I have written a scripting language just for things like this. It is EXTREMELY flexible (you can add any command you can think of, within reason). I wrote it for use in my MUD, but I am now adapting it to run qbasic/truebasic style programs (aka interpreted, but not part of a bigger project). You should be able to download it off my website in a few days... I haven''t put it up yet. Send me an email and I will let you know when my website is totally up.

--------------------


You are not a real programmer until you end all your sentences with semicolons;

Yanroy@usa.com

Share this post


Link to post
Share on other sites
I personally am a fan of function pointers and DLLs for scripting. You basically have triggers and the triggers would call function pointers that are bound to the object/character/or map section. Besides, no compiled script is going to be as fast as native computer language

Share this post


Link to post
Share on other sites
MikeD, I''d like to take a look at your script engine. I''d just like to see how it''s done. How would you "parse them into logical operator trees attached to functions"? I''m probably not C++ advanced enough to figure that out (I really am advanced at C++, but I just can''t think of how that would be done. A good look at your code would probably help.)

An idea I had was to do a Java-like compile. In the start of the program, all the AI would be compiled into byte-code, and the byte-code could be interpreted. I have absolutely no idea whatsoever how that would be done, but it would be a really good idea I believe.

Lastly, what is Lua? It sounds like it''s somewhat useful, but how is it incorporated with C++, what''s the language like, etc? I might want to look into that, but insight on that would be appreciated. Thanks guys...



ColdfireV

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You should have a look at the scripting engine tutorial on flipcode (http://www.flipcode.com/tutorials/tut_scr01.shtml) by Jan Niestadt. It''s a quite good introduction in scripting languages.

Share this post


Link to post
Share on other sites
Staffan:
Someone wise once said: "If nobody ever reinvented the wheel, all our cars would roll on logs."

Just a thought. Reinventing a better wheel is a great idea.

Share this post


Link to post
Share on other sites
Very true. But in most cases a better option is already avalible (unless you have special demands of course). C++, MSVC and DirectX are all examples off that.

"Paranoia is the belief in a hidden order behind the visible." - Anonymous

Share this post


Link to post
Share on other sites
Okay, by "parse them into logical operator trees attached to functions"? I mean I have a tree system which branches at each binary operator (i.e. an operator that has two arguments such as AND or OR), there''s an associated function which is fired based on the result of the tree and each leaf on the tree is a function which returns TRUE or FALSE.
Each tree and all associateed functions makes up a whole or part of an if-statement.

So...

if(A && B) alpha;
else if(B // C) beta;
else gamma;

...has A, B and C being functions returning TRUE or FALSE, alpha, beta and gamma being functions which are called if the && and // trees return TRUE based on their arguments.

ColdfireV, I''ll bring in the code tomorrow and mail it to you.

Mike

Share this post


Link to post
Share on other sites
Hi !

I would write my own scripting language. Of course people are right to say reinventing the wheel is bad, but on the other side, it makes a lot of fun to create it''s own scritping language and it''s a very advanced topic. If you can manage to write your won scripting language, there is nearly nothing you can''t do )
Ok, I have created my own scripting engine for my 3D Game Engine. It just took me about 8 weeks, and it features everything UnrealScript features, so it''s full object orientated, has it''s own compiler (->bytecode) and I have also written a virtual machine.

At the beginning I had the same problems as you, how the hell is a compiler done ?? Well, then I found a document of Jack Crenshaw, it''s called "Let''s build a compiler" and it started 1988 !! Since compiler-technology does not change just and is something that is solved since the 80th, it''s no problem that it''s quite old. This guy wrote such an incredible tutorial, check it out:

http://www.iecc.com/compilers/crenshaw/

Also this tutorial is done in Turbo Pascal (Source-Code) it''s quite easy to port to C or C++. I created an MFC-Project and startet to do what he does and explain, and after some time I had a working compiler which compiled to bytecode. Get a good book on Java and read everything on bytecode-generation.

And if you start with that tutorial take you some time, and then step by step everything gets very clear, why I need a stack, what is a symbol table.

And if you have questions just mail me : pschuster@ngi.de I think I can help you, if you get stuck.

Phillip Schuster

Share this post


Link to post
Share on other sites