-2

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.

Jay
  • 21
  • 1
  • 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. – House Oct 18 '12 at 02: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 02: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 02: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 02:57
  • Jay, just do a search: http://gamedev.stackexchange.com/search?q=combat+system I don't know if anyone wants to read through all your code and tell you how to improve your system. Look into how combat systems are generally implemented. Find a design you like and adapt your code to fit. I think your question is either going to be too broad or too localized. – House Oct 18 '12 at 03:07
  • My code is largely irrelevant. I've come to the conclusion the logic in it is flawed for the outlined reasons, and I was asking how it could be implemented alternatively. Whether broadly or specifically; this is a game design question. Like I said I don't know what would make this a valid question for gamedev. I will continue to search on my own for ideas, I only came here to see if anyone else had some ideas, if not, so be it. – Jay Oct 18 '12 at 03:11
  • 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 09:33

1 Answers1

1

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 */
  }
 }

 }
}
Chris
  • 11
  • 1