Difference between revisions of "Development:Python:AI Scripts"

From VsWiki
Jump to: navigation, search
m
(ah, yes, one more)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{NAV_Manual |
+
=Python AI Scripts=
| previous=[[HowTo:Edit_Missions:Python:Missions|Missions]]
 
| up=[[Development:Missions|Edit Missions]]
 
| next=[[HowTo:Edit_Missions:Python:Bindings|Python Bindings]]
 
}}
 
----
 
 
 
 
Let me talk about AI scripts briefly.
 
Let me talk about AI scripts briefly.
  
You'll notice a nice AI script named <code>[http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/vegastrike/data/printhello.py?rev=HEAD&content-type=text/vnd.viewcvs-markup printhello.py]</code>. It is very similar to a mission with a few notable differences.  {{fixme}} ''This is a reference to the old pre 4.0 data''
+
In the <code>modules</code> directory, you'll notice a nice AI script named '''[http://vegastrike.svn.sourceforge.net/viewvc/vegastrike/trunk/data/modules/ai/ai_sitting_duck.py?view=markup ai_sitting_duck.py]'''. It is very similar to a mission with a few notable differences:
 
+
* First of all, the AI class you make must inherit from the class <code>VS.PythonAI</code>.  
* First of all the AI class you make must inherit from the class <code>VS.PythonAI</code>.  
+
* Secondly, instead of having an <code>__init__</code> function it must have an <code>init()</code> function. The reasons for this have something to do with how inheritance works from C++, but this init function takes in self of course and the unit to which the AI script belongs. <code>__init__</code> may start off some AI scripts from the get go, or you may wait until you execute each frame. In this case ''ai_sitting_duck.py'' loads the XML script <code>++turntowards.xml</code> (which is the hard coded C++ equivalent to '''[http://vegastrike.svn.sourceforge.net/viewvc/vegastrike/trunk/data/ai/script/turntowards.xml?view=markup ai/scripts/turntowards.xml]'''). After loading that AI script it replaces its parents order with the last AI script, in this case the XML script. In the Execute function this AI script calls Execute on the higher level (which takes care of responding to communications) and then prints h to the console for no apparent reason.  
* Secondly instead of having an <code>__init__</code> function it must have an <code>init()</code> function. The reasons for this have something to do with how inheritance works from C++, but this init function takes in self of course and the unit to which the AI script belongs. <code>__init__</code> may start off some AI scripts from the get go, or you may wait until you execute each frame. in this case printhello loads the XML script <code>++turntowards.xml</code> (which is the hard coded C++ equivalent to <code>[http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/vegastrike/data4.x/ai/scripts/turntowards.xml?rev=HEAD&content-type=text/vnd.viewcvs-markup ai/scripts/turntowards.xml]</code>). After loading that AI script it replaces its parents order with the last AI script, in this case the XML script. In the Execute function this AI script calls Execute on the higher level (which takes care of responding to communications) and then prints h to the console for no apparent reason.  
+
* Lastly and most importantly, the AI script makes a new class for itself. So when the C++ code executes <code>ai_sitting_duck.py</code> it will call the C++ constructor which will take care of binding it. So when you make a new unit like this:
* Lastly and most importantly, the AI script makes a new class for itself. So when the C++ code executes <code>printhello.py</code> it will call the C++ constructor which will take care of binding it. So when you make a new unit like so:
+
<code><pre>
<pre>  
+
VS.launch (fgname,type,faction,"unit","ai_sitting_duck.py",nr_ships,nr_waves,vec,logo)
VS.launch (fgname,type,faction,"unit","printhello.py",nr_ships,nr_waves,vec,logo)
+
</pre></code>
</pre>
+
it will have this python AI attached to it. However, if you are too lazy to write AI's you can use the most excellent default AI:
it will have this python AI attached to it. However if you are too lazy to write AI's you can use the most excellent default AI  
+
<code><pre>
<pre>
 
 
VS.launch (fgname,type,faction,"unit","default",nr_ships,nr_waves,vec,logo)
 
VS.launch (fgname,type,faction,"unit","default",nr_ships,nr_waves,vec,logo)
</pre>
+
</pre></code>
  
----
+
[[Category:HowTos|Edit Missions:Python:AI scripts]]
{{NAV_Manual |
+
[[Category:Development]]
| previous=[[HowTo:Edit_Missions:Python:Missions|Missions]]
+
[[Category:Development|Edit Missions:Python:AI scripts]]
| up=[[Development:Missions|Edit Missions]]
 
| next=[[HowTo:Edit_Missions:Python:Bindings|Python Bindings]]
 
}}
 
[[Category:HowTos|Edit Missions:Python:AI scripts]] [[Category:Development|Edit Missions:Python:AI scripts]]
 

Latest revision as of 13:09, 15 August 2010

Python AI Scripts

Let me talk about AI scripts briefly.

In the modules directory, you'll notice a nice AI script named ai_sitting_duck.py. It is very similar to a mission with a few notable differences:

  • First of all, the AI class you make must inherit from the class VS.PythonAI.
  • Secondly, instead of having an __init__ function it must have an init() function. The reasons for this have something to do with how inheritance works from C++, but this init function takes in self of course and the unit to which the AI script belongs. __init__ may start off some AI scripts from the get go, or you may wait until you execute each frame. In this case ai_sitting_duck.py loads the XML script ++turntowards.xml (which is the hard coded C++ equivalent to ai/scripts/turntowards.xml). After loading that AI script it replaces its parents order with the last AI script, in this case the XML script. In the Execute function this AI script calls Execute on the higher level (which takes care of responding to communications) and then prints h to the console for no apparent reason.
  • Lastly and most importantly, the AI script makes a new class for itself. So when the C++ code executes ai_sitting_duck.py it will call the C++ constructor which will take care of binding it. So when you make a new unit like this:
VS.launch (fgname,type,faction,"unit","ai_sitting_duck.py",nr_ships,nr_waves,vec,logo)

it will have this python AI attached to it. However, if you are too lazy to write AI's you can use the most excellent default AI:

VS.launch (fgname,type,faction,"unit","default",nr_ships,nr_waves,vec,logo)