<?php

require "_trinity.php";

MySTART( DBnameFromArgv() );

$iname = "";
$res_item = MyOP("SELECT * FROM item_template WHERE ".EntryConditionFromArgv($iname));
$nitems = 0;
$global_refloots = 0;

while($item = mysql_fetch_object($res_item)) {

if ($nitems > 0)  echo "\n";

PrintItem($item, "Item ");

unset($loot);
unset($lootids);

$refloots = 0;
$res = MyOP("SELECT * from reference_loot_template WHERE item=$item->entry AND mincountOrRef>=0");
$lootids['RE'] = "";
while($r = mysql_fetch_object($res)) {
  $lootids['RE'] .= ($lootids['RE'] == "" ? "" : ",") . $r->entry;
  $loot["$r->entry RE"] = $r;
  ++$refloots;
  ++$global_refloots;
}

$check_ref_loot = ($lootids['RE'] == "") ? "FALSE" : "mincountOrRef<0 AND (-mincountOrRef) IN (${lootids['RE']})";

if ($lootids['RE'] != "") {

$res = MyOP("SELECT * from reference_loot_template WHERE $check_ref_loot");
while($r = mysql_fetch_object($res)) {
  $lootids['RE'] = @$lootids['RE']. (@$lootids['RE'] == "" ? "" : ",") . $r->entry;
  $loot["$r->entry RE"] = $loot[(- $r->mincountOrRef)." RE"];
  $loot["$r->entry RE"]->mincountOrRef *= $r->maxcount;
  $loot["$r->entry RE"]->maxcount *= $r->maxcount;
}
$check_ref_loot = "mincountOrRef<0 AND (-mincountOrRef) IN (${lootids['RE']})";

}

$res = MyOP(
"(SELECT *, 'DR' AS tableID from creature_loot_template WHERE item=$item->entry AND mincountOrRef>=0 OR $check_ref_loot)"
." UNION ".
"(SELECT *, 'SK' AS tableID from skinning_loot_template WHERE item=$item->entry AND mincountOrRef>=0 OR $check_ref_loot)"
." UNION ".
"(SELECT *, 'PP' AS tableID from pickpocketing_loot_template WHERE item=$item->entry AND mincountOrRef>=0 OR $check_ref_loot)"
);
while($r = mysql_fetch_object($res)) {
  $lootids[$r->tableID] = @$lootids[$r->tableID]. (@$lootids[$r->tableID] == "" ? "" : ",") . $r->entry;
  if ($r->mincountOrRef >= 0) $loot["$r->entry $r->tableID"] = $r;
  else {
    $loot["$r->entry $r->tableID"] = $loot[(- $r->mincountOrRef)." RE"];
    $loot["$r->entry $r->tableID"]->mincountOrRef *= $r->maxcount;
    $loot["$r->entry $r->tableID"]->maxcount *= $r->maxcount;
  }
}


++$nitems;
$whereloot = "";
$sep = "";
if (isset($lootids['DR']))  { $whereloot .= "${sep}lootid IN (${lootids['DR']})"; $sep = " OR "; }
if (isset($lootids['SK']))  { $whereloot .= "${sep}skinloot IN (${lootids['SK']})"; $sep = " OR "; }
if (isset($lootids['PP']))  { $whereloot .= "${sep}pickpocketloot IN (${lootids['PP']})"; $sep = " OR "; }

if ($whereloot == "") {
    echo "  === No creature drop/skinning/pickpocket entries found for this item ===\n";
    continue;
}

$nloots = 0;
$ncreatures = 0;
unset($loot_used);

$r = MyROWobj("SELECT * FROM creature_template");
$res = MyOP("SELECT * FROM creature_template WHERE $whereloot ORDER BY minlevel".
  (isset($r->minhealth) ? ", minhealth ASC":
   (isset($r->Health_mod) ? ", Health_mod ASC" :
    (isset($r->HealthMultiplier) ? ", HealthMultiplier ASC" :
     ""
  )))
);
while($r = mysql_fetch_object($res)) {
  // вероятности и среднее количество показывает только для DR и SK, но не для PP
  $a1 = @$loot["$r->lootid DR"]->ChanceOrQuestChance;
  $a2 = @$loot["$r->skinloot SK"]->ChanceOrQuestChance;
  $prob = 100 - (100-abs($a1)) * (100-abs($a2))/100; // интерференция вероятностей от простого loot и от skinning
  if ($a1 < 0 or $a2 < 0)  $prob = -$prob;
  $a1 = 0; $a2 = 0;
  if (@$loot["$r->lootid DR"]->maxcount > 1)  $a1 = (max(1,@$loot["$r->lootid DR"]->mincountOrRef) + @$loot["$r->lootid DR"]->maxcount)/2.0;
  if (@$loot["$r->skinloot SK"]->maxcount > 1)  $a2 = (max(1,@$loot["$r->skinloot SK"]->mincountOrRef) + @$loot["$r->skinloot SK"]->maxcount)/2.0;
  $average = max($a1,$a2);
  printf("  %8s%s %s%s%s %5s %-26s L%d..%d  %s %d%s",
    sprintf("%.3f", $prob),
    (@$loot["$r->lootid DR"]->groupid > 0 or @$loot["$r->skinloot SK"]->groupid > 0) ? "@":"%",
    isset($loot["$r->lootid DR"]) ? "d" : " ",
    isset($loot["$r->skinloot SK"]) ? "s" : " ",
    isset($loot["$r->pickpocketloot PP"]) ? "p" : " ",
    $r->entry, $r->name,
    $r->minlevel, $r->maxlevel,
    (isset($r->minhealth) ? ($r->minhealth == $r->maxhealth ? $r->maxhealth : "$r->minhealth..$r->maxhealth") :
     (isset($r->Health_mod) ? sprintf("*%.2f", $r->Health_mod) :
      (isset($r->HealthMultiplier) ? sprintf("*%.2f", $r->HealthMultiplier) :
       ""
    ))),
    round(($r->mindmg + $r->maxdmg)/2 * (isset($r->dmg_multiplier) ? $r->dmg_multiplier : 1)),
    $average > 0 ? sprintf("  x%.2f", $average) : ""
  );
  echo "\n";
  ++$ncreatures;
  if (isset($loot["$r->lootid DR"]) and !isset($loot_used["$r->lootid DR"])) { $loot_used["$r->lootid DR"] = true; ++$nloots; }
  if (isset($loot["$r->skinloot SK"]) and !isset($loot_used["$r->skinloot SK"])) { $loot_used["$r->skinloot SK"] = true; ++$nloots; }
}

printf("  === %d refs, %d creatures, %d DR+SK loots ===\n", $refloots, $ncreatures, $nloots);
}

if ($nitems == 0) die("Item `$iname` not found in item_template!\n");
printf("=== %d items (%d refs) === \n", $nitems, $global_refloots);

?>
