Jump to content


Photo

Battle Code I need some help


  • This topic is locked This topic is locked
2 replies to this topic

#1 Silverton

Silverton

    GMC Member

  • New Member
  • 2 posts
  • Version:GM8

Posted 03 March 2012 - 09:18 PM

Hey Guys,
Its me Silverton from yoyoforums, I am taking my problem here cause so far, nobody there as been able to help me.
Here is my code:

global.selected=id;
option1=0
option2=0
option3=0
option4=0

if global.lastselected.id.attacks=0 { show_message("No Attacks Left!") }

if global.lastselected!=0
if global.lastselected.id.attacks>0

{
option = show_message_ext("Do you wish to attack this ship?","Attack!","Cancel","");

if (option = 2) exit

if (option = 1) 
global.lastselected.id.attacks-=1;
{ if global.shipattacking=1  if (distance_to_object(global.selected.id)<600) if global.lastselected.id.attacks>0 { if irandom_range(0,100) < 80 { show_message("Attack was successful"); global.selected.id.hp-=30   }}} // 1 = Prometheus Attacking
{ if global.shipattacking=2  if (distance_to_object(global.selected.id)<600) if global.lastselected.id.attacks>0 option2 = show_message_ext("How will you attack?","Earth","Asgard",""); if (option2 = 1)  { if irandom_range(0,100) < 80 { show_message("Attack was successful"); global.selected.id.hp-=40 }}   if (option2 = 2) { if irandom_range(0,100) < 60 { show_message("Attack was successful"); global.selected.id.hp-=40  }}} // 2 = Daedalus Attacking
{ if global.shipattacking=3  if (distance_to_object(global.selected.id)<600) if global.lastselected.id.attacks>0 option3 = show_message_ext("How will you attack?","Earth","Asgard",""); if (option3 = 1){ if irandom_range(0,100) < 80 { show_message("Attack was successful"); global.selected.id.hp-=50 }}   if (option2 = 2) { if irandom_range(0,100) < 70 { show_message("Attack was successful"); global.selected.id.hp-=70    }}} // 3 = Odyssey Attacking
{ if global.shipattacking=4  if (distance_to_object(global.selected.id)<600) if global.lastselected.id.attacks>0 { if irandom_range(0,100) < 60 { show_message("Attack was successful") global.selected.id.hp-=70   }}} // 4 = Hive Ship Attacking
{ if global.shipattacking=5  if (distance_to_object(global.selected.id)<600) if global.lastselected.id.attacks>0 { if irandom_range(0,100) < 60 { show_message("Attack was successful") global.selected.id.hp-=80   }}} // 5 = O'Neil Attacking
{ if global.shipattacking=6  if (distance_to_object(global.selected.id)<600) if global.lastselected.id.attacks>0 { if irandom_range(0,100) < 60 { show_message("Attack was successful") global.selected.id.hp-=60   }}} // 6 = Jackson Attacking
{ if global.shipattacking=7  if (distance_to_object(global.selected.id)<600) if global.lastselected.id.attacks>0 { if irandom_range(0,100) < 70 { show_message("Attack was successful") global.selected.id.hp-=70   }}} // 7 = Hammond Attacking
{ if global.shipattacking=8  if (distance_to_object(global.selected.id)<600) if global.lastselected.id.attacks>0 { if irandom_range(0,100) < 50 { show_message("Attack was successful") global.selected.id.hp-=130   }}} // 8 = Ori Attacking
{ if global.shipattacking=9  if (distance_to_object(global.selected.id)<600) if global.lastselected.id.attacks>0 { if irandom_range(0,100) < 60 { show_message("Attack was successful") global.selected.id.hp-=30   }}} // 9 = Gou'ld Attacking
{ if global.shipattacking=10 if(distance_to_object(global.selected.id)<600)  if global.lastselected.id.attacks>0 { if irandom_range(0,100) < 60 { show_message("Attack was successful") global.selected.id.hp-=70   }}} // 10 = Boss Attacking
{ if global.shipattacking=11 if (distance_to_object(global.selected.id)<600) if global.lastselected.id.attacks>0 if global.lastselected.id.drones>0 { if irandom_range(0,100) < 100 { show_message("Attack was successful") global.selected.id.hp-=200   }}} // 11 = Ancient War Ship Attacking
{ if global.shipattacking=12 if (distance_to_object(global.selected.id)<600) if global.lastselected.id.attacks>0 if global.lastselected.id.drones>0 { if irandom_range(0,100) < 100 { show_message("Attack was successful") global.selected.id.hp-=200   }}} // 12 = Ancient Cruiser Attacking
{ if global.shipattacking=13 if (distance_to_object(global.selected.id)<600) if global.lastselected.id.attacks>0 { if irandom_range(0,100) < 60 { show_message("Attack was successful") global.selected.id.hp-=70   }}} // 13 = Destiny Attacking
{ if global.shipattacking=14 if (distance_to_object(global.selected.id)<600) if global.lastselected.id.attacks>0 { if irandom_range(0,100) < 100 { show_message("Attack was successful") global.selected.id.hp-=200   }}} // 14 = Atlantis Attacking
{ if global.shipattacking=15 if (distance_to_object(global.selected.id)<600) if global.lastselected.id.attacks>0 if global.lastselected.id.drones>0 option4 = show_message_ext("How will you attack?","Drill","Drones",""); if (option4 = 1)  global.lastselected.id.hp-=global.selected.id.hp   global.lastselected.id.hp-=100  if (option4 = 2) { if irandom_range(0,100) < 100 { show_message("Attack was successful"); global.selected.id.hp-=200  }}} // 15 = TowerShip Attacking
{ if global.shipattacking=16 if (distance_to_object(global.selected.id)<600) if global.lastselected.id.attacks>0 { if irandom_range(0,100) < 60 { show_message("Attack was successful") global.selected.id.hp-=70   }}} // 16 = Wraith Super Hive Attacking
{ if global.shipattacking=17 if (distance_to_object(global.selected.id)<600) if global.lastselected.id.attacks>0 { if irandom_range(0,100) < 80 { show_message("Attack was successful") global.selected.id.hp-=400   }}} // 17 = SAD Attacking
{ if global.shipattacking=18 if (distance_to_object(global.selected.id)<600) if global.lastselected.id.attacks>0 if global.lastselected.id.drones>0 { if irandom_range(0,100) < 100 { show_message("Attack was successful") global.selected.id.hp-=200   }}} // 17 = Homeworld Attacking


}  
As you can see, this is a lot!
For starters, what this code is SUPPOSED TO DO, is attack a ship. In my game, you select a ship you want to use to attack. global.lastselected= the ship you click on for attacking. In this turn based game, there are lots of the same ship objects, and lots of the same enemy ship objects, and lots of different ships.
In this code, the ship that is being attacked = global.selected.
global.shipattacking is the way I am using for identifying the type of ship that is attacking. As you can see by looking at this code, different ships do different damage.
A couple of ships, have different types of attacks which you can choose from.

CODE PROBLEMS: Ok, when i go into my test room, buy a attack ship and attack the enemy ship that has this code several things happen.
1# The message "Do you wish to attack" Does pop up, I hit yes, but the "attack was hit" message doesn't pop up. I know I have a random chance code here, but no mater how many times I hit it, number 2 happens.
2# The ship attacking loses EXACTLY 100 hp.

Please help! I have been over the code again and again and I can't find anything wrong! Thank you!
-Silverton
  • 0

#2 @Alex@

@Alex@

    GMC Member

  • GMC Member
  • 4307 posts
  • Version:Unknown

Posted 04 March 2012 - 02:14 PM

I'm not even going attempt to debug something that poorly formatted, there are a lot of styles people code in that I can deal with but that just isn't one of them. You'd be better looking into switch statements and moving your distance check since you check the same distance in all of them you can check it before shipattacking check.

#1 : You'll need to check if each one of your if statements is executing, place a show_message() in each block and see where it gets to.
#2: Looking at your code I can't see any where where you take off exactly 100 but I do see a few sequential removals that would add up to 100. Better formatting would allow us to see if this would execute a lot easier.
  • 0

#3 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 22277 posts
  • Version:GM:Studio

Posted 04 March 2012 - 07:25 PM

Wow... That code is terrible! No offense meant, but if you have to write out the same code more than once, you are doing it wrong. So lets optimise that code first and then see if we can find the error (or even fix it straight away!)



if global.lastselected.id.attacks=0
{
show_message("No Attacks Left!");
}
else
{
if global.lastselected!=0 && global.lastselected.id.attacks>0
    {
    var option;
    option = show_message_ext("Do you wish to attack this ship?", "Attack!", "Cancel", "");
    if option=1
        {
        global.lastselected.id.attacks-=1;
        if distance_to_object(global.selected.id)<600
            {
            switch (global.shipattacking)
                {
                case 1: //Prometheus Attacking
                if irandom_range(0,100) < 80
                    {
                    show_message("Attack was successful");
                    global.selected.id.hp-=30
                    }
                break;
                case 2: // 2 = Daedalus Attacking
                if show_message_ext("How will you attack?", "Earth", "Asgard", "")=1
                    {
                    if irandom_range(0,100) < 80
                        {
                        show_message("Attack was successful");
                        global.selected.id.hp-=40
                        }
                    }
                else
                    {
                    if irandom_range(0,100) < 60
                        {
                        show_message("Attack was successful");
                        global.selected.id.hp-=40
                        }
                    }
                break;
                case 3: // 3 = Odyssey Attacking
                if show_message_ext("How will you attack?", "Earth", "Asgard", "")=1
                    {
                    if irandom_range(0,100) < 80
                        {
                        show_message("Attack was successful");
                        global.selected.id.hp-=50
                        }
                    }
                else
                    {
                    if irandom_range(0,100) < 70
                        {
                        show_message("Attack was successful");
                        global.selected.id.hp-=70
                        }
                    }
                break;
                case 4: // 4 = Hive Ship Attacking
                if irandom_range(0,100) < 60 show_message("Attack was successful") global.selected.id.hp-=70;
                break;
                case 5 : // 5 = O'Neil Attacking
                if irandom_range(0,100) < 60 show_message("Attack was successful") global.selected.id.hp-=80;
                break;
                //etc...
                //etc...
                //etc...
                }
            }
        }
    }
}



Now, see what I've done? All those repeat distance calls and ifs were redundant, so I´ve placed them once at the beginning because they are the same for all ships. Also, just one variable for "options" is more than enough as you can re-use it too! So reformat your code like that and test it again then let us know what happens.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users