Battle System - am I close? [SOLVED]

Started by Atelier, Fri 04/06/2010 10:22:08

Previous topic - Next topic

Atelier

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:
Code: ags

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


Code: ags

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

Wonkyth

Is it this bit of
Code: ags

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

?
"But with a ninja on your face, you live longer!"

Wyz

'=-' should be '-=' I guess.

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

Code: ags

A -= B // Otherwise: A = A - B
A =- B // Otherwise: A = 0 - B
Life is like an adventure without the pixel hunts.

Atelier

#3
I tried both:

Code: ags

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.

Code: ags

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?

Wyz

You could print out the values of 'maxdamage' real quick in that spot, then you know for sure they're set to something logical. :)
Life is like an adventure without the pixel hunts.

Atelier

Code: ags

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


Everything works fine now. Excellent, thanks for your help. Problem solved :=

Khris

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:
Code: ags
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%).

Wonkyth

Quote from: Khris on Fri 04/06/2010 12:27:51
This:
Code: ags
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.
"But with a ninja on your face, you live longer!"

Atelier

It's ok, I also noticed these problems and solved them duly. Although quite crude it's very fun to play :D

SMF spam blocked by CleanTalk