Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Atelier on Fri 04/06/2010 10:22:08

Title: Battle System - am I close? [SOLVED]
Post by: Atelier on Fri 04/06/2010 10:22:08
Sorry to ask for help again. I've come up with a crude battle system, but every time I call it one of three things happens:

- It only runs once.
- The health lost isn't calculated properly.
- The game freezes.

This is how I call it:

if (Parser.Said("kill monk")) {
    OpponentName = cMonk.Name;    //opponentname & victim are global variables.
    victim = cMonk.ID;
    StartFight();
}



function StartFight()
{
    cinfo battlearray[150]; //cinfo is a struct and I'm combining it with an array
    battlearray[victim].health = 100; //Set the opponents health at 100.
    int chancetohit = Random(10); //There's a 1/5 chance of not hitting.
   
   
    while (battlearray[victim].health > 0) { //While the opponent's health is more than 0 the battle carries on:

    if (chancetohit < 3) { //The player misses and instead gets struck by opponent.
         Add(String.Format("%s lands a cruel strike on you!", OpponentName), 15);
         battlearray[player.ID].health =- Random(battlearray[victim].maxdamage); //The opponent's max damage is subtracted from the player's health. (at random)
         }
         
    else {  //(chancetohit was not 1/5 so player hits)  
         Add(String.Format("You brutally crush %s!", OpponentName.LowerCase()), 15);
         battlearray[victim].health =- Random(maxdamage); //Your maxdamage is subtracted from opponent's health (at random)
         }
   
    }

    //The while loop has finished so opponent must be out of health.    
    Add(String.Format("Hurray! %s is dead!", OpponentName), 2, 1);
    ch_status[victim] = eStatusDead; //The opponent is then declared dead.

}


Frankly, where am I going wrong? Most of the time the victim is killed after only one hit - despite "maxdamage" being only 30, and the opponent's health is 100. I think it has something to do with the Random(maxdamage) declarations. Please, don't go out of your way to help me; I'll be satisfied with pointers :=

Thanks,
Atelier
Title: Re: Battle System - am I close?
Post by: Wonkyth on Fri 04/06/2010 10:36:08
Is it this bit of

battlearray[player.ID].health/* [THIS BIT ]*/ =- /* [THIS BIT] *//Random(battlearray[victim].maxdamage);

?
Title: Re: Battle System - am I close?
Post by: Wyz on Fri 04/06/2010 10:54:05
'=-' should be '-=' I guess.

It is both correct code, but I think it does not do what you expect it does:


A -= B // Otherwise: A = A - B
A =- B // Otherwise: A = 0 - B
Title: Re: Battle System - am I close?
Post by: Atelier on Fri 04/06/2010 11:21:43
I tried both:


battlearray[player.ID] -= Random(battlearray[victim].maxdamage);
battlearray[player.ID] -= battlearray[player.ID] - Random(battlearray[victim].maxdamage);


But now the game freezes whenever I call the function :-\

However, swapping around -= and -= definitely seems more logical, as before the opponent's health went into minus numbers. Which would also explain why it was only being called once :D

But like I said now it freezes.

EDIT: Aha. Found the offending lines.


battlearray[player.ID].health -= Random(battlearray[victim].maxdamage);
battlearray[victim].health -= Random(maxdamage);


When I replace Random(maxdamage) with Random(30), for example, it works perfectly. It seems using an int instead of an actual number causes freezing.

What could I do?
Title: Re: Battle System - am I close?
Post by: Wyz on Fri 04/06/2010 12:06:27
You could print out the values of 'maxdamage' real quick in that spot, then you know for sure they're set to something logical. :)
Title: Re: Battle System - am I close?
Post by: Atelier on Fri 04/06/2010 12:24:27

if (Parser.Said("wield [onyx] dagger")) maxdamage = 34;


Everything works fine now. Excellent, thanks for your help. Problem solved :=
Title: Re: Battle System - am I close?
Post by: Khris on Fri 04/06/2010 12:27:51
You didn't include the struct code so it may be the source of the errors. My guess is you declared the struct variables in the header or something, or didn't initialize the values correctly.

This:
battlearray[player.ID] -= battlearray[player.ID] - Random(battlearray[victim].maxdamage);
will set battlearray to the random value instead of substracting it.

A -= B; is the same as A = A - B;
A -= A - B is the same as A = B; (and aside from being not what you want, removes the point of having the short version -=).

Also, if I'm not mistaken, the fight will continue even with the player having zero or negative health.
Plus, chancetohit isn't randomized every turn, which will create pretty onesided fights.
Thus, if chancetohit is < 3, the while loop will hang due to the enemy's health never reaching 0.

Also, Random(10) returns a random number from 0-10 while <3 is 0-2 which makes the chances of the player missing not 1 in 5 (20%) but 3 in 11 (27,27%).
Title: Re: Battle System - am I close? [SOLVED]
Post by: Wonkyth on Sat 05/06/2010 09:56:56
Quote from: Khris on Fri 04/06/2010 12:27:51
This:
battlearray[player.ID] -= battlearray[player.ID] - Random(battlearray[victim].maxdamage);
will set battlearray to the random value instead of substracting it.
That's what I meant.
I guess I wasn't clear enough on the matter.
Title: Re: Battle System - am I close? [SOLVED]
Post by: Atelier on Sat 05/06/2010 13:39:45
It's ok, I also noticed these problems and solved them duly. Although quite crude it's very fun to play :D