gametechmods

Robot Arena => Discussion => Topic started by: Gauche Suede on March 10, 2012, 10:09:16 PM

Title: From Text to Phyton
Post by: Gauche Suede on March 10, 2012, 10:09:16 PM
i've just finished my very own AI code(RS Hybrid), but i still have it on text document. how do i turn it into a phyton file? ;)
Title: Re: From Text to Phyton
Post by: Mr. AS on March 10, 2012, 10:11:33 PM
make a copy of an already existing .py file and edit that with notepad
Title: Re: From Text to Phyton
Post by: Gauche Suede on March 10, 2012, 11:17:19 PM
okay, i've did that, but now, after i tested the AI on Ninja Asassin, when i tried to battle it, the game won't load. what is missing?
Title: Re: From Text to Phyton
Post by: Gauche Suede on March 11, 2012, 12:07:01 AM
@Larrain
i use Windows XP..
Title: Re: From Text to Phyton
Post by: martymidget on March 11, 2012, 03:18:21 AM
You need notepad++ to save pythons, I believe.

Title: Re: From Text to Phyton
Post by: cephalopod on March 11, 2012, 10:32:43 AM
I doubt it's that - Notepad++ is just a fancier, nicer-for-editing Notepad. May be a little issue with the code, but I'll let someone more experienced in that business take it further, 'cos I have no idea :3
Title: Re: From Text to Phyton
Post by: ty4er on March 11, 2012, 10:35:12 AM
i'm sure it is notepad c++, you might want to ask clickbeetle or trovaner or someone like that
Title: Re: From Text to Phyton
Post by: Trovaner on March 11, 2012, 01:16:24 PM
A python file is basically a text file that has the .py extension (as far as you need to concern yourself). Python files are read by an interpreter which in RA2's case I believe is version 2.3. If your game crashes during startup, you usually have a syntax error. If this is the case, IDLE or some unix based terminal can tell you whats wrong with the "grammer."

Notepad++ is in no way required to program especially in python but its my favorite text editor (scite is also pretty good). 
Title: Re: From Text to Phyton
Post by: Gauche Suede on March 12, 2012, 06:29:44 AM
Guys, here's my RS Hybrid AI:
Code: [Select]
from __future__ import generators
import plus
import AI
from AI import vector3
import Arenas
import Gooey
import math
import Tactics

class RS Hybrid(AI.SuperAI):
    "Spins and Rams!"
    name = "RS Hybrid"

    def __init__(self, **args):
        AI.SuperAI.__init__(self, **args)

   self.spin_range = 40.0
   
   if 'range' in args:
            self.spin_range = args.get('range')

   self.tactics.append(Tactics.Ram(self))

    def Activate(self, active):
        if active:
            if AI.SuperAI.debugging:
                self.debug = Gooey.Plain("watch", 0, 75, 100, 75)
                tbox = self.debug.addText("line0", 0, 0, 100, 15)
                tbox.setText("Throttle")
                tbox = self.debug.addText("line1", 0, 15, 100, 15)
                tbox.setText("Turning")
                tbox = self.debug.addText("line2", 0, 30, 100, 15)
                tbox.setText("")
                tbox = self.debug.addText("line3", 0, 45, 100, 15)
                tbox.setText("")

   return AI.SuperAI.Activate(self, active)

    def Tick(self):
        if self.weapons:
            # spin up depending on enemy's range
            enemy, range = self.GetNearestEnemy()
           
            if enemy is not None and range < self.spin_range:
                self.Input("Spin", 0, 1)
            elif self.GetInputStatus("Spin", 0) != 0:
                self.Input("Spin", 0, 0)

   return AI.SuperAI.Tick(self)

    def RobotInRange(self, robot_id):
        "Return tuple of (part-of-robot-in-range, chassis-in-range)"
        # GetLastDamage returns:  component damaged, amount, at time, by player, by component
        range = self.GetDistanceToID(robot_id)
        if range < self.spin_range:
            damage = self.GetLastDamageReceived()
            if damage[3] == robot_id and (plus.getTimeElapsed() - damage[2] < 1.0):
                return (True, True)

        return (False, False)
       
    def LostComponent(self, id):
        #print "Lost Component!"
        return AI.SuperAI.LostComponent(self, id)

    def LostComponent(self, id):
        # if we lose all our weapons, stop using the Ram tactic and switch to Shove
        if id in self.weapons: self.weapons.remove(id)
       
        if not self.weapons:
            tactic = [x for x in self.tactics if x.name == "Engage"]
            if len(tactic) > 0:
                self.tactics.remove(tactic[0])
               
                self.tactics.append(Tactics.Shove(self))
                self.tactics.append(Tactics.Charge(self))

        return AI.SuperAI.LostComponent(self, id)

    def DebugString(self, id, string):
        if self.debug:
            if id == 0: self.debug.get("line0").setText(string)
            elif id == 1: self.debug.get("line1").setText(string)
            elif id == 2: self.debug.get("line2").setText(string)
            elif id == 3: self.debug.get("line3").setText(string)

AI.register(RS Hybrid)
Is this good? what should i change from it?
Title: Re: From Text to Phyton
Post by: Trovaner on March 12, 2012, 11:42:57 AM
TTYTT, it doesn't look like you changed that much. From what I can tell, you changed the default spin_range (which was already adjustable via the bindings) and made Ram the main tactic (which has already been done inside OmniTrueRam.py). Though by building upon Spinner.py, you have a specialized version of RobotInRange code that isn't found inside of any of the Omni variants (IIRC).

Issues:
1. Remove the first LostComponent method. I am aware that it was in Spinner.py but it is overridden anyways so its just wasting space.
2. Fix your __init__ method so that everything is indented by 8 spaces.
3. Fix your LostComponent method so that it properly switches tactics when all the weapons fall off (replace "Engage" with "Ram").
4. Due to an issue with how RA2's AI uses switches, you'll want to change the self.Input("Spin", 0, 1) to self.Input("Spin", 0, 100). This makes it compatible with Analog controls while still allowing it to work with buttons.

For anyone interested in using this, ram is not preinstalled in stock or DSL so you'll need to download Clickbeetle's upgraded tactics.py to use it (it may be found in BBEANS AI). You may also want to wait until he fixes the above issues.