I made a script that randomly generates items after fights and then prints out their data so far. But I got this error
When item is generated it is working corectly, but when item isnt generated it throws out this error
Chance: 9399
Hello LVL 100 adventurer!
(test for qlvl - 0)
Itemtype - 0
Warning: Cannot use a scalar value as an array in C:\wamp\www\Game\itemgen.php on line 81
You searched monsters body and found 0 0!
Dmg: -
-
Heres my script
<?php
include ('config.php');
$playerlvl = 100; //Rand (2, 100)-1;
$chance = Rand (0,10000)-1;
$itemstats = array( "strenght", "dexterity", "inteligence", "stamina", "health points", "energy points", "armor rating");
$data = 0;
$quality = 0;
echo "Chance: $chance </br>";
echo "Hello LVL $playerlvl adventurer! </br>";
//Function that generates stats,$playerlvl, $qmulti, $itemstats, $itemtype are stated out of the function
function statgen($playerlvl, $qmulti, $itemstats, $itemtype) {
$data = array();
if ($itemtype == "weapon") {
$data['itemlvl'] = $playerlvl;
$data['minwdmg'] = round(10 + 3 * $playerlvl * $qmulti/2);
$data['maxwdmg'] = round(20 + 3 * $playerlvl * $qmulti/2);
$data['statt1'] = $itemstats[rand(0,6)];
$data['statt2'] = $itemstats[rand(0,6)];
$data['stat1'] = round($playerlvl * ($qmulti/2));
$data['stat2'] = round($playerlvl * ($qmulti/2));
}
else {
$data['itemlvl'] = $playerlvl;
$data['statAR'] = round($playerlvl * 3/2 + rand(0, 10));
$data['statt1'] = $itemstats[rand(0,6)];
$data['statt2'] = $itemstats[rand(0,6)];
$data['stat1'] = round($playerlvl * ($qmulti/2));
$data['stat2'] = round($playerlvl * ($qmulti/2));
}
return $data;
}
// Gets itemqlvl, if its=0 than does nothing, else creates item
if ($chance <= 10){
$itemqlvl = 3;
}
elseif ($chance <= 100){
$itemqlvl = 2;
}
elseif ($chance <= 9000) {
$itemqlvl = 1;
}
else {
$itemqlvl = 0;
}
echo "</br> (test for qlvl - $itemqlvl) </br>";
switch($itemqlvl){
case "0":
break;
case "1":
$quality = "common";
$qmulti = 2;
break;
case "2":
$quality = "enchanted";
$qmulti = 3;
break;
case "3":
$quality = "elite";
$qmulti = 4;
break;
}
if ($itemqlvl >= 1) {
$itemtypes = array( "weapon", "body", "legs", "feet", "hands", "head");
$itemtype = $itemtypes[rand(0, 5)];
$data = statgen($playerlvl, $qmulti, $itemstats, $itemtype);
}
else {
$itemtype = 0;
}
echo "Itemtype - $itemtype <br>";
//Prints out results/tests.
function wprop($data, $quality, $itemtype) {
$litemtype = $itemtype;
if ($litemtype == "weapon") {
if ($data['statt1'] == $data['statt2']) {
$data['stat'] = $data['stat1'] + $data['stat2'];
$wprop['stats'] = "Dmg:" .$data['minwdmg']." - " .$data['maxwdmg']." </br>".$data['statt1']." - ".$data['stat']." ";
}
else {
$wprop['stats'] = "Dmg:" .$data['minwdmg']." - " .$data['maxwdmg']." </br>".$data['statt1']." - ".$data['stat1']." </br> ".$data['statt2']." - ".$data['stat2'].".";
}
$wprop['type'] = "You searched monsters body and found $quality $itemtype!</br>";
return $wprop;
}
elseif ($litemtype != "weapon") {
if ($data['statt1'] == $data['statt2']) {
if ($data['statt1'] == "armor rating") {
$data['stat'] = $data['statAR'] + $data['stat1'] + $data['stat2'];
$wprop['stats'] = "Armor rating: ".$data['stat']."";
}
else {
$data['stat'] = $data['stat1'] + $data['stat2'];
$wprop['stats'] ="Armor: ".$data['statAR']." </br> ".$data['statt1']." - ".$data['stat']."";
}
}
elseif ($data['statt1'] == "armor rating") {
$data['stat'] = $data['statAR'] + $data['stat1'];
$wprop['stats'] = "Armor: ".$data['stat']." </br> ".$data['statt2']." - ".$data['stat2']."";
}
elseif ($data['statt2'] == "armor rating") {
$data['stat'] = $data['statAR'] + $data['stat2'];
$wprop['stats'] = "Armor: ".$data['stat']." </br> ".$data['statt1']." - ".$data['stat1']."";
}
else {
$wprop['stats'] = "Armor: ".$data['statAR']." </br> ".$data['statt1']." - ".$data['stat1']." </br> ".$data['statt2']." - ".$data['stat2']."";
}
$wprop['type'] = "You searched monsters body and found $quality $itemtype!</br>";
return $wprop;
}
elseif ($litemtype == 0) {
$wprop['stats'] = "Monster didn't drop anything";
$wprop['type'] = "";
return $wprop;
}
}
$wprop = wprop($data, $quality, $itemtype);
echo "".$wprop['type']." ".$wprop['stats']."";
?>
I figured that line that isnt working is this one
elseif ($litemtype == 0) {
$wprop['stats'] = "Monster didn't drop anything";
$wprop['type'] = "";
return $wprop;
}
But the weard part is that I have defined that $litemtype == 0, it even prints it out on the echo part that i made for testing purpose. Any ideas why this happens?
Edit: I also tried to change $itemtype to "nothing" and NULL in case of nothing beeing dropped, but it didnt fix the problem eather