Jump to content


Photo

AI for a strategy game


  • Please log in to reply
5 replies to this topic

#1 SHiLLySiT

SHiLLySiT

    GMC Member

  • New Member
  • 78 posts
  • Version:GM:Studio

Posted 19 July 2010 - 08:22 PM

Okay so basically my game is one of those 'defend' the house type games. Instead of trying to explain it and confuse you, here is a flash game example: http://armorgames.co...616/age-of-war. My theme and game play is different;(1)instead of cash you have action points(AP points) that you gain from each kill (2)you can fire arrows from your base at the cost of 1 AP points or (3)chose to create a unit for its relative cost. The arrows are strictly for killing enemy units and the units are primarily used to attack the enemy wall/castle though a few of them have unit to unit attacks. There are also several abilities amongst the units that you can choose to use AP points on instead.

So I'm not have any problems with game play, in fact, it's already pretty much done. I just recently started on the enemy AI and it's making me pull my hair out. I set up a Google Docs flow chart to help ease my headache with some visual organization: https://docs.google....thkey=CPmj9tQC. (yellow boxes are variables affecting the chance, red boxes are random/chance/choose functions, green and blue are just normal.)

I had a very primitve beginning to this when I was testing the engine with one unit vs firing arrows:
alarm[1]=thinkstep//always set alarm

if ((ceil(irandom(100)))>unitorattack_chance)//decide to create unit or attack
    {
    //chance_repeat=(ceil(irandom(max_actionpoints/peasant_cost)))
    if actionpoints>=peasant_cost//create a peasant
        //repeat(chance_repeat)
        {
        instance_create(x+64,y+88,obj_enemy_peasant)
        actionpoints-=2
        }
    else
        {exit}
    }
else
    {
    if (actionpoints>=1)
        {
        if instance_exists(obj_player_parent)
            {
            tokill=instance_nearest(x,y,obj_player_parent)
            range=distance_to_object(tokill)
            dir=point_direction(x,y,(tokill.x),(tokill.y))
            dir-=(ceil(irandom_range(15,30)))//angle adjust
            arrow=instance_create(x+25,y+45,obj_enemy_arrow)
            arrow.dir=dir
            with (arrow){motion_set(dir,8)}
            actionpoints-=1
            }
        }
    else
        {exit}
    }
Which worked fine, but now it has gotten very complex; 4 units and 4 abilities plus the enemy needs to judge threat levels. For example, if a player unit is very close to the enemy castle, there needs to be more weight (or more of a chance) on shooting an arrow. Also, the enemy should learn to save when it is low on AP and sometimes decide to save for more powerful units, otherwise after reaching low AP, it will always release an arrow or cheap unit because it doesn't have enough for more.

Edited by SHiLLySiT, 19 July 2010 - 08:36 PM.

  • 0

#2 SHiLLySiT

SHiLLySiT

    GMC Member

  • New Member
  • 78 posts
  • Version:GM:Studio

Posted 21 July 2010 - 04:26 AM

This is probably going to need to be moved to the Advanced forum after this since I am now adding some current code. So mods do what you please...


So I was tossing some ideas around and I finally came up with the following in the following in the shower. I've really only structured it but I also added bits of the code here and there, mainly of which I had before in my more primitive code.
alarm[1]=thinkstep//always set alarm

//stepone
step1_chance=50
modifier_s1=0

//varibles
if obj_enemy_castle.hp<=20//low hp, favor ability->healing
    {modifier_s1+=40;modifier_s2+=40;}
if actionpoints<=(ceil(max_actionpoints/2))//low ap, favor doing nothing or saving
    {modifier_s1+=-30;}
if distance_to_object(tokill)<=50//player close, favor shooting arrow
    {modifier_s1+=30;modifier_s2+=40;}

step1_chance+=modifier_s1//adds weight from variables
    
if ((ceil(irandom(100)))>step1_chance)//decide to do -something- or -nothing-
    {
    //something
    step2_chance=50
    modifier_s2=0
    step2_chance+=modifier_s2//adds weight from variables
    
    //step two
    if ((ceil(irandom(100)))>step2_chance)//decide to do -shoot arrow- or -create unit/ability-
        {
        //shoot arrow
            if (actionpoints>=1)
                {
                if instance_exists(obj_player_parent)
                    {
                    tokill=instance_nearest(x,y,obj_player_parent)
                    range=distance_to_object(tokill)
                    dir=point_direction(x,y,(tokill.x),(tokill.y))
                    dir-=(ceil(irandom_range(15,30)))//angle adjust
                    arrow=instance_create(x+25,y+45,obj_enemy_arrow)
                    arrow.dir=dir
                    with (arrow){motion_set(dir,8)}
                    actionpoints-=1
                    }
                }
            else
                {exit}
        }
    else
        {
        //create_unit/ability
        if ((ceil(irandom(100)))>step2_chance)//decide to do -unit- or -ability-
            {
            //unit
                //calculate ap, find units equal to or less ap cost, add to list
                //choose from list randomly
                //release unit
            }
        else
            {
            //ability
                //calculate ap, find abilities equal to or less ap cost, add to list
                //choose from list randomly
                //use ability
            }
        }
    
    }
else
    {
    //nothing
    if ((ceil(irandom(100)))>step2_chance)//decide to do -save- or -do nothing-
        {
        //save
            //choose unit to save for
            //saving=1
            //wait next step
            //if enough ap release unit
            //else repeat wait
        }
    else
        {
        //do nothing
        }
    }

As you can see its quite lengthy and thats not even the complete code. I just wanted to know if I'm on the right track here before I waste all this time writing it.
  • 0

#3 Rani_sputnik

Rani_sputnik

    GMC Member

  • GMC Member
  • 353 posts

Posted 21 July 2010 - 09:35 AM

Na I reckon you need to rethink your strategy.

Simple/great game concept. there is a simple strategy I like called finite state machines. The ai can only be in one state at a time. For each state, you can easilly setup eactly what the ai should be doing. Then every few steps you could re-evaluate which state the ai is in based on input priorities.

There is a great tutorial on FSM's in the tutorial section. If you use these, you wont be able to get confused with what the ai is doing at any point in time. Hope this helps. It wont make very intuitive ai, but it will make a solid, easy to understand, challenging one.
  • 0

#4 SHiLLySiT

SHiLLySiT

    GMC Member

  • New Member
  • 78 posts
  • Version:GM:Studio

Posted 21 July 2010 - 05:33 PM

Na I reckon you need to rethink your strategy.

Simple/great game concept. there is a simple strategy I like called finite state machines. The ai can only be in one state at a time. For each state, you can easilly setup eactly what the ai should be doing. Then every few steps you could re-evaluate which state the ai is in based on input priorities.

There is a great tutorial on FSM's in the tutorial section. If you use these, you wont be able to get confused with what the ai is doing at any point in time. Hope this helps. It wont make very intuitive ai, but it will make a solid, easy to understand, challenging one.


Oh interesting. That had crossed my mind; if i understand correctly, i create multiple states and swap the states depending on the situation? Thats so much easier! I had been browsing for AI tutorials but most I came across were for conventional GM games such as platformers and top downs. Thank you!
  • 0

#5 Rani_sputnik

Rani_sputnik

    GMC Member

  • GMC Member
  • 353 posts

Posted 22 July 2010 - 04:17 AM

Thank you!


No probs. it'll be really easy to make some good ai from there :D If you need any more help pm me.
  • 0

#6 SHiLLySiT

SHiLLySiT

    GMC Member

  • New Member
  • 78 posts
  • Version:GM:Studio

Posted 22 July 2010 - 04:22 AM


Thank you!


No probs. it'll be really easy to make some good ai from there :D If you need any more help pm me.


I'm still new at AI programming so I might take you up on that offer. ;]
Thanks again!

Edited by SHiLLySiT, 22 July 2010 - 04:23 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users