stombaugh Posted November 11, 2008 Share Posted November 11, 2008 Hello, I'm trying to design a Calculator for a MMORPG Modern-War-Militia. For those of you who don't know this game It's obviosuly a war game. I'm trying to Design a Calc that when a User inputs how many of Each unit he has and then Enters the total number of units his enemy has it automatically divides out how much of each unit the Attacker(user) should send to Attack Successfully. I currently got it setup telling the Attacker if he should be able to attack successfully and telling them also if the enemy may be able to Retaliate. Let me show you an Example: Attacker's units 40 Light Infantry 20 Light Machine Gun 20 Light Rangers Defender's Units 10 Light Infantry 10 Light Machine Gun 10 Light Rangers Totals: Attackers Offensive Points = 160 Defenders Defensive Points = 100 ok, the Attacker submits in my calculator, it totals out the Attack and Defense points. But I want the calc to tell the attacker that it'd take atleast 40 Light Infantry, and 14 Light Ranger (Total Offensive Pts = 122) to Successfully Attack the Defensive Player. Anyone Have any ideas? Link to comment https://forums.phpfreaks.com/topic/132249-php-help/ Share on other sites More sharing options...
Alt_F4 Posted November 12, 2008 Share Posted November 12, 2008 hi try something like this - its only basic but hopefully it will give you one direction you could take: <?php $DBConnect = mysql_connect("localhost", "root", "root"); if(!$DBConnect) { die("<p>ERROR1: Database unavailable</p>"); } $DBName = "test"; if(!mysql_select_db($DBName, $DBConnect)) { die("<p>ERROR2: Database unavailable</p>"); } function calcOffense($offense,$number) { $points = $offense * $number; return $points; } function calcDefence($defence,$number) { $points = $defence * $number; return $points; } //set totals $attacker_off_total=0; $attacker_def_total=0; $defender_off_total=0; $defender_def_total=0; $defender_array=array(); $attacker_array=array(); //loop through the attacker types of units //this will depend on where you keep your data //this has been pulled from a database $sql = "SELECT * FROM tbl_Units WHERE team_id=1 ORDER BY unit_offense DESC"; $query = mysql_query($sql); $i=0; while($result=mysql_fetch_array($query)) { //calculate offensive power of each unit $attacker_off_total += calcOffense($result['unit_offense'],$result['unit_number']); //calculate defensive power of each unit $attacker_def_total += calcDefence($result['unit_defence'],$result['unit_number']); //add unit names and nubers to array for later use $attacker_array[$i]["name"]=$result['unit_name']; $attacker_array[$i]["number"]=$result['unit_number']; $attacker_array[$i]["offense"]=$result['unit_offense']; $i++; } //loop through the defender types of units //this will depend on where you keep your data //this has been pulled from a database $sql = "SELECT * FROM tbl_Units WHERE team_id=2 ORDER BY unit_offense DESC"; $query = mysql_query($sql); $i=0; while($result=mysql_fetch_array($query)) { //calculate offensive power of each unit $defender_off_total += calcOffense($result['unit_offense'],$result['unit_number']); //calculate defensive power of each unit $defender_def_total += calcDefence($result['unit_defence'],$result['unit_number']); //add unit names and nubers to array for later use $defender_array[$i]["name"]=$result['unit_name']; $defender_array[$i]["number"]=$result['unit_number']; $defender_array[$i]["offense"]=$result['unit_offense']; $i++; } if($defender_def_total > $attacker_off_total) { echo "cannot win: defender's defense too high"; } if($attacker_off_total > $defender_def_total) { echo "to win you need to send:<br />"; $output=""; $ttlCalcOff=0; $calcDef = $defender_def_total; for($i=0;$i<sizeof($attacker_array);$i++) { if($calcDef > 0) { $units = ceil($calcDef / $attacker_array[$i]["offense"]); //if the number of calculated units exceeds the number of units the attacker has, //adjust defense points accordingly if($units > $attacker_array[$i]["number"]) { $units = $attacker_array[$i]["number"]; $calcOff = $units*$attacker_array[$i]["offense"]; $ttlCalcOff += $calcOff; $calcDef = $calcDef - $calcOff; $output .= $units." ".$attacker_array[$i]["name"]."<br />"; } else { $calcOff = $units*$attacker_array[$i]["offense"]; $ttlCalcOff += $calcOff; $calcDef = $calcDef - $calcOff; $output .= $units." ".$attacker_array[$i]["name"]."<br />"; } } } echo $output."<br />"; echo "total offensive points for attacker: ".$ttlCalcOff."<br />"; echo "total defensive points for defender: ".$defender_def_total; } ?> have fun Link to comment https://forums.phpfreaks.com/topic/132249-php-help/#findComment-688295 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.