Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I posted this a while ago on stackoverflow, they thought it would be better place here, I agree.

Essentially I know what I want to accomplish, and I have something to the effect of what I want but I am not satisfied with it. Here's the problem.

Each user has some states: STR (how hard they hit), DEF (dodging/blocking attacks), SPD (when they can strike), and STAMINA (basically their endurance in game, if this runs out they can no longer fight and lose)

What I need is something like this:

UserA Stats: STR: 1,000 DEF: 2500 SPD: 2000 (HP: 1000/1000)

UserB Stats: STR: 1,500 DEF: 500 SPD: 4000 (HP: 1000/1000)

Because the second user has double the speed, he lands twice the amount of hits on the first user, before he gets hit. Because he has less strength than the first users defence, he will do no, to little damage. This is how the battle would theoretically go:

UserB strikes UserA for 0 damage

UserB strikes UserA for 0 damage

UserA strikes UserB for 500 damage

UserB strikes UserA for 0 damage

UserB strikes UserA for 0 damage

UserA strikes UserB for 500 damage, and sends him to the hospital!

I was using this code, which is buggy, and not efficient, I just need a better way to do this: http://pastebin.com/15LiQQuJ

Oh, and if anyone has some good ideas on how to improve the concept that would be cool too! It's not that elaborate so I'll be thinking of all sorts of things to make it more dynamic.

Thanks.

share|improve this question
1  
Jay. I'm afraid there's not really a correct answer to this question. Essentially you're just asking "How can this be improved", not even "How can I improve it in this way". It's very broad. –  Byte56 Oct 18 '12 at 2:42
    
The question is about the design of the system. Right now it's basically counting the milliseconds before one can attack, it is very inefficient and I was wondering if there was a better way to do this... I'm not exactly sure how else I can ask this. –  Jay Oct 18 '12 at 2:48
1  
Why do you think it's inefficient? What particularly you don't like about it? @Byte56 is right, it's hard to understand what's the question. –  Petr Abdulin Oct 18 '12 at 2:53
    
Ok sorry I should have been more specific.. the first problem is, there is a cap on the speed in the thousands which is arbitrary. I can fix that but then because its being timed in milliseconds if speeds go up very high the loop will go through MANY iterations, and the script could just time out. It's definitely not the best solution. –  Jay Oct 18 '12 at 2:57
2  
@Jay instead of commenting, it's better to modify and improve original question (an 'edit' link below the question). No need to worry. Try to localize the problems, an ask them separately as a single, yet complete question (or at least make a numbered list for questions). –  Petr Abdulin Oct 18 '12 at 9:33
show 2 more comments

1 Answer

If I understand correctly, you're just trying to figure out a better way to figure out who attacks when and how often. Looks like everything is autocalculated, so you don't need to figure out the proper delay between attacks or anything. You really just need to figure out how many attacks the fast guy gets before the slow guy gets one. You should be able to do that by adding code similar to this to resolve_battle:

if( $user_a['SPD'] > $user_b['SPD']){
 $a_is_faster = true; // this variable remembers which of the two is faster (true if UserA, false if UserB)
 $speed_ratio = $user_a['SPD'] / $user_b['SPD']; // this var remembers how many attacks the faster player gets before the slower player attacks
}else{
 $a_is_faster = false;
 $speed_ratio = $user_b['SPD'] / $user_a['SPD'];
}
$speed_counter = $speed_ratio; // this keeps track of how many more attacks the faster player is going to get before the slower player gets one
while($in_progress){

 if($speed_counter>=1){
  $speed_counter--;
  if($a_is_faster){
  /* code to make userA attack */
  }else{
  /* code to make userB attack */
  }
 }else{
  $speed_counter += $speed_ratio; // when the slower player gets their attack, we add more attacks to the faster player's counter
  if($a_is_faster){
  /* code to make userB attack */
  }else{
  /* code to make userA attack */
  }
 }

 }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.