jcvenom Posted August 16, 2015 Posted August 16, 2015 I was contemplating about my work and I realised one mod I never tried making or has never been requested by a client of mine is an attack system. However I have an idea of how to make one but im not sure how i would go about doing it and how the formulas would come into play, lets discuss this :confused: Quote
flixbeat Posted August 16, 2015 Posted August 16, 2015 (edited) I recently finished coding the battle system of my game and I'm not publicizing my work here yet, but the attack works like this. $rand = rand(1,10); # generates a random number from 1-10 $player_atk = 3; # let's assume the player attack is 3, means 3 damage will be inflicted to the enemy if($rand <= $player_atk){ # if random number is less than or equal to player's attack $enemy_hp -= $player_atk; # successful hit } else{ print("attack missed"); # or you can damage the player here. } very simple, no formula has been used or created, well... just to contribute an idea. Edited August 16, 2015 by flixbeat syntax error for random Quote
jcvenom Posted August 16, 2015 Author Posted August 16, 2015 I recently finished coding the battle system of my game and I'm not publicizing my work here yet, but the attack works like this. $rand = rand(1,10); # generates a random number from 1-10 $player_atk = 3; # let's assume the player attack is 3, means 3 damage will be inflicted to the enemy if($rand <= $player_atk){ # if random number is less than or equal to player's attack $enemy_hp -= $player_atk; # successful hit } else{ print("attack missed"); # or you can damage the player here. } very simple, no formula has been used or created, well... just to contribute an idea. Thanks but this seems to be different to a text based attack system Quote
jcvenom Posted August 16, 2015 Author Posted August 16, 2015 I recently finished coding the battle system of my game and I'm not publicizing my work here yet, but the attack works like this. $rand = rand(1,10); # generates a random number from 1-10 $player_atk = 3; # let's assume the player attack is 3, means 3 damage will be inflicted to the enemy if($rand <= $player_atk){ # if random number is less than or equal to player's attack $enemy_hp -= $player_atk; # successful hit } else{ print("attack missed"); # or you can damage the player here. } very simple, no formula has been used or created, well... just to contribute an idea. Also id like to base the damage inflicted by what stats the player has Quote
Coly010 Posted August 17, 2015 Posted August 17, 2015 Thanks but this seems to be different to a text based attack system Not at all. It depends on the game and how their attack system works? This could be a perfectly viable attack system. You have 3 attack so you do 3 damage each time you land a successful hit. Now i personally like to have a few different stats that will contribute to the attack formula. I'm gonna attempt to do a quick example here on my phone. Ok so every user has three stats relating to the attack system. Strength - determines how much hp to take Agility - determines who attacks first and also determines how high a chance you have of avoiding an attack Defence - made up from your armour, reduces damage received Let's put a cap on the stats at 80. I like OOP so im going to style it using that. I'll leave out some functions code as they should be easy to work out class Attack { var $attackFirst, $opponent, $user; var $attacking; var $statCap = 80; // create the attack public function __construct($oppid, $userid){ $this->user = $this->getUserStats($userid); $this->opponent = $this->getUserStats($oppid); $this->determineAttackFirst(); while($this->attacking){ $this->attack(); } } // use a conditional statement to determine who strikes first private function determineAttackFirst(){ $this->attackFirst = $this->user['agility'] > $this->opponent['agility'] ? $this->user['id'] : $this->opponent['id']; } private function avoidAttack($agi){ // to make it simple, create a random between 1 and the stat cap. If the random is below or equals it, success, they avoid the attack (mt_rand(1, $this->statCap) <= $agu ? return true : return false); } // simple enough damage calculator private function calculateDamage($str, $def){ $dmg = ($str * mt_rand(1, 5)) - $def; if( $dmg < 0) { $dmg = 0; } return $dmg; } private function userAttack(){ $dmg = $this->calculateDamage($this->user['strength'], $this->opponent['defence']); $avoided = $this->avoidAttack( $this->opponent['agility']); $log = $this->user['name']." attempts to strike ". $this->opponent['name'] ."! <br />"; switch($avoided){ case true: $log .= "But ".$this->opponent['name']." avoids the attack!<br /><br />"; break; case false: $hpleft = $this->opponent['hp']-$dmg; $hpleft = ($hpleft < 0 ? 0 : $hpleft); $log .= "You deal ".$dmg." leaving them with ".$hpleft; } $this->attacking = (isset($hpleft) && $hpleft <= 0 ? false : true); } private function attack(){ if($this->attackFirst == $this->user['id']){ $this->userAttack(); $this->opponentAttack(); } else { // vice versa of above } } } Hopefully that's understandable if not ask questions and I'll answer. It's not a working script either, there are functions missing and it can be optimised quite a bit. Quote
jcvenom Posted August 17, 2015 Author Posted August 17, 2015 Not at all. It depends on the game and how their attack system works? This could be a perfectly viable attack system. You have 3 attack so you do 3 damage each time you land a successful hit. Now i personally like to have a few different stats that will contribute to the attack formula. I'm gonna attempt to do a quick example here on my phone. Ok so every user has three stats relating to the attack system. Strength - determines how much hp to take Agility - determines who attacks first and also determines how high a chance you have of avoiding an attack Defence - made up from your armour, reduces damage received Let's put a cap on the stats at 80. I like OOP so im going to style it using that. I'll leave out some functions code as they should be easy to work out class Attack { var $attackFirst, $opponent, $user; var $attacking; var $statCap = 80; // create the attack public function __construct($oppid, $userid){ $this->user = $this->getUserStats($userid); $this->opponent = $this->getUserStats($oppid); $this->determineAttackFirst(); while($this->attacking){ $this->attack(); } } // use a conditional statement to determine who strikes first private function determineAttackFirst(){ $this->attackFirst = $this->user['agility'] > $this->opponent['agility'] ? $this->user['id'] : $this->opponent['id']; } private function avoidAttack($agi){ // to make it simple, create a random between 1 and the stat cap. If the random is below or equals it, success, they avoid the attack (mt_rand(1, $this->statCap) <= $agu ? return true : return false); } // simple enough damage calculator private function calculateDamage($str, $def){ $dmg = ($str * mt_rand(1, 5)) - $def; if( $dmg < 0) { $dmg = 0; } return $dmg; } private function userAttack(){ $dmg = $this->calculateDamage($this->user['strength'], $this->opponent['defence']); $avoided = $this->avoidAttack( $this->opponent['agility']); $log = $this->user['name']." attempts to strike ". $this->opponent['name'] ."! <br />"; switch($avoided){ case true: $log .= "But ".$this->opponent['name']." avoids the attack!<br /><br />"; break; case false: $hpleft = $this->opponent['hp']-$dmg; $hpleft = ($hpleft < 0 ? 0 : $hpleft); $log .= "You deal ".$dmg." leaving them with ".$hpleft; } $this->attacking = (isset($hpleft) && $hpleft <= 0 ? false : true); } private function attack(){ if($this->attackFirst == $this->user['id']){ $this->userAttack(); $this->opponentAttack(); } else { // vice versa of above } } } Hopefully that's understandable if not ask questions and I'll answer. It's not a working script either, there are functions missing and it can be optimised quite a bit. Only question is hope did you determine what formula needs to be used to preform the damage hit? Quote
Coly010 Posted August 17, 2015 Posted August 17, 2015 Only question is hope did you determine what formula needs to be used to preform the damage hit? You mean this? $dmg = ($str * mt_rand(1, 5)) - $def; I thought it up as I typed the code.. There isn't any specific damage calculation formula. For example if you have weapons that add to the damage then it may look something like this: $dmg = ($str + $wepdmg) - $def; I add the mt_rand so that it provides a bit of variety into the damage being dealt. Quote
jcvenom Posted August 17, 2015 Author Posted August 17, 2015 You mean this? $dmg = ($str * mt_rand(1, 5)) - $def; I thought it up as I typed the code.. There isn't any specific damage calculation formula. For example if you have weapons that add to the damage then it may look something like this: $dmg = ($str + $wepdmg) - $def; I add the mt_rand so that it provides a bit of variety into the damage being dealt. Oh i think i have an idea but i would like to base something like this on strength, defence and agility Quote
Coly010 Posted August 17, 2015 Posted August 17, 2015 Oh i think i have an idea but i would like to base something like this on strength, defence and agility it is based on strength, defence and agility? it uses strength and defence to work out the damage, and it uses agility to see if the other person avoids the attack or not? Quote
jcvenom Posted August 17, 2015 Author Posted August 17, 2015 it is based on strength, defence and agility? it uses strength and defence to work out the damage, and it uses agility to see if the other person avoids the attack or not? Yeh sorry im just confused, but yes thats what i mean Quote
KyleMassacre Posted August 17, 2015 Posted August 17, 2015 With something like this you can add little multipliers quite easily. I won't go into math because I'm lazy and whatnot but for example you can add an array k/v pair with something like head, body, arm, leg shot with a damage multiplier. $multi = array('head' => .2, 'arm' => .05, 'leg' => .05, 'body' => .1); Quote
jcvenom Posted August 17, 2015 Author Posted August 17, 2015 With something like this you can add little multipliers quite easily. I won't go into math because I'm lazy and whatnot but for example you can add an array k/v pair with something like head, body, arm, leg shot with a damage multiplier. $multi = array('head' => .2, 'arm' => .05, 'leg' => .05, 'body' => .1); The math side of things is my only issue and how the strength and defence would aid the damage caused Quote
KyleMassacre Posted August 17, 2015 Posted August 17, 2015 $multi = array('head' => 1.2, 'arm' => 1.05, 'leg' => 1.05, 'body' => 1.1); $rand_key = array_rand($multi,1); $dmg = ($str * mt_rand(1, 5)) - $def * $multi[$rand_key]; Str: 30 Def: 28 Random Number: 5 Muliplier: .5 Would come out to 134.2 damage. Whereas without the random multiplier it would be 122. I did change my percentages to make it easier to multiply by Quote
Coly010 Posted August 17, 2015 Posted August 17, 2015 The math side of things is my only issue and how the strength and defence would aid the damage caused its somewhat basic math? GCSE standard I'd think. [MENTION=68711]KyleMassacre[/MENTION] - I didn't even think about dictating a place to hit on the other place lol. but yeah, the script is easily scalable which is partially why i'm quite proud of it haha. Quote
~Rob0t Posted August 18, 2015 Posted August 18, 2015 Not at all. It depends on the game and how their attack system works? This could be a perfectly viable attack system. You have 3 attack so you do 3 damage each time you land a successful hit. Now i personally like to have a few different stats that will contribute to the attack formula. I'm gonna attempt to do a quick example here on my phone. Ok so every user has three stats relating to the attack system. Strength - determines how much hp to take Agility - determines who attacks first and also determines how high a chance you have of avoiding an attack Defence - made up from your armour, reduces damage received Let's put a cap on the stats at 80. I like OOP so im going to style it using that. I'll leave out some functions code as they should be easy to work out class Attack { var $attackFirst, $opponent, $user; var $attacking; var $statCap = 80; // create the attack public function __construct($oppid, $userid){ $this->user = $this->getUserStats($userid); $this->opponent = $this->getUserStats($oppid); $this->determineAttackFirst(); while($this->attacking){ $this->attack(); } } // use a conditional statement to determine who strikes first private function determineAttackFirst(){ $this->attackFirst = $this->user['agility'] > $this->opponent['agility'] ? $this->user['id'] : $this->opponent['id']; } private function avoidAttack($agi){ // to make it simple, create a random between 1 and the stat cap. If the random is below or equals it, success, they avoid the attack (mt_rand(1, $this->statCap) <= $agu ? return true : return false); } // simple enough damage calculator private function calculateDamage($str, $def){ $dmg = ($str * mt_rand(1, 5)) - $def; if( $dmg < 0) { $dmg = 0; } return $dmg; } private function userAttack(){ $dmg = $this->calculateDamage($this->user['strength'], $this->opponent['defence']); $avoided = $this->avoidAttack( $this->opponent['agility']); $log = $this->user['name']." attempts to strike ". $this->opponent['name'] ."! <br />"; switch($avoided){ case true: $log .= "But ".$this->opponent['name']." avoids the attack!<br /><br />"; break; case false: $hpleft = $this->opponent['hp']-$dmg; $hpleft = ($hpleft < 0 ? 0 : $hpleft); $log .= "You deal ".$dmg." leaving them with ".$hpleft; } $this->attacking = (isset($hpleft) && $hpleft <= 0 ? false : true); } private function attack(){ if($this->attackFirst == $this->user['id']){ $this->userAttack(); $this->opponentAttack(); } else { // vice versa of above } } } Hopefully that's understandable if not ask questions and I'll answer. It's not a working script either, there are functions missing and it can be optimised quite a bit. All your properties have public scope by declaring them with var. You should read up on visibility You have a parse error on line 23 You have inconsistent indent style I think you meant to put your $log variable into a property so it can be read later. Also, it would be better to make this an array and format the results with implode You're assigning a lot of variables when you don't need to, albeit not a huge factor, it's bad practice. For example, line 35 can be in line 37. Apart from that, nice. Quote
Coly010 Posted August 18, 2015 Posted August 18, 2015 All your properties have public scope by declaring them with var. You should read up on visibility You have a parse error on line 23 You have inconsistent indent style I think you meant to put your $log variable into a property so it can be read later. Also, it would be better to make this an array and format the results with implode You're assigning a lot of variables when you don't need to, albeit not a huge factor, it's bad practice. For example, line 35 can be in line 37. Apart from that, nice. What part of "quick example on my phone" and "it's not a working script either, there are functions missing and it can be optimised do you not get? Wow that question makes me sound angry and bad tempered lol. I know all about visibility, keeping consistent indent is difficult when typing on a phone and you're trying to deal with autocorrect. I'm well aware of the parse error; and the unneeded variabs, hence why I said it can be optimised. Quote
jcvenom Posted August 18, 2015 Author Posted August 18, 2015 All your properties have public scope by declaring them with var. You should read up on visibility You have a parse error on line 23 You have inconsistent indent style I think you meant to put your $log variable into a property so it can be read later. Also, it would be better to make this an array and format the results with implode You're assigning a lot of variables when you don't need to, albeit not a huge factor, it's bad practice. For example, line 35 can be in line 37. Apart from that, nice. You can't really judge this, he did state clearly this isnt a working script it's unfinished btw that's why there is unneeded variables which can later can be optimized for use, it's an example and I understood it clearly, Tbh you have time to judge someone's work and your opinion is rather vague. As for keeping consistent with an indent style I wouldn't be arsed XD if I was typing on a phone and he did explain it quite nicely although he used a phone and one last point as for the line 35 and line 37 I too would have placed line 35 in a variable and used it on line 37 it makes code look neat rather than clustery(If that's a word), I'm just defending a member who is doing me good so please respect his efforts :) thanks you Quote
~Rob0t Posted August 18, 2015 Posted August 18, 2015 (edited) You can't really judge this, he did state clearly this isnt a working script it's unfinished btw that's why there is unneeded variables which can later can be optimized for use, it's an example and I understood it clearly, Tbh you have time to judge someone's work and your opinion is rather vague. As for keeping consistent with an indent style I wouldn't be arsed XD if I was typing on a phone and he did explain it quite nicely although he used a phone and one last point as for the line 35 and line 37 I too would have placed line 35 in a variable and used it on line 37 it makes code look neat rather than clustery(If that's a word), I'm just defending a member who is doing me good so please respect his efforts :) thanks you > your opinion is rather vague No, it really isn't vague. Vague would be "It doesn't work" > As for keeping consistent with an indent style I wouldn't be arsed XD if I was typing on a phone > [...] it makes code look neat [...] You have conflicting views. > I'm just defending a member who is doing me good so please respect his efforts Never did I say I didn't respect this efforts... What part of "quick example on my phone" and "it's not a working script either, there are functions missing and it can be optimised do you not get? [...] Ah, you took it as I was bashing you, I wasn't. I never said I didn't understand the "quick example on my phone" and "it's not a working script either, there are functions missing and it can be optimised" - I just dismissed them for the fact people would copy&paste this then complain it doesn't work. Edited August 18, 2015 by ~Rob0t Quote
jcvenom Posted August 18, 2015 Author Posted August 18, 2015 > your opinion is rather vague No, it really isn't vague. Vague would be "It doesn't work" > As for keeping consistent with an indent style I wouldn't be arsed XD if I was typing on a phone > [...] it makes code look neat [...] You have conflicting views. > I'm just defending a member who is doing me good so please respect his efforts Never did I say I didn't respect this efforts... I have different views Quote
Coly010 Posted August 18, 2015 Posted August 18, 2015 > your opinion is rather vague No, it really isn't vague. Vague would be "It doesn't work" > As for keeping consistent with an indent style I wouldn't be arsed XD if I was typing on a phone > [...] it makes code look neat [...] You have conflicting views. > I'm just defending a member who is doing me good so please respect his efforts Never did I say I didn't respect this efforts... Ah, you took it as I was bashing you, I wasn't. I never said I didn't understand the "quick example on my phone" and "it's not a working script either, there are functions missing and it can be optimised" - I just dismissed them for the fact people would copy&paste this then complain it doesn't work. despite me writing that its not a working script? whats the difference in posting its not working in the same post as the code, and you saying its not working? however, I do appreciate your input :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.