<?php
/* Examples:
quest.php The Platinum Discs
quest.php (entry IN (5211, 5742))
quest.php "(LimitTime<=0)"
quest.php @General
quest.php "(8921 IN (NextQuestId,NextQuestInChain,entry,PrevQuestId))"
*/
require "_trinity.php";

MySTART( DBnameFromArgv() );

$verbose = false;
if (@$argv[count($argv)-1] == "/")  { $verbose = true; array_splice($argv,-1); }

$queststarter = MyVAL("SHOW TABLES LIKE 'creature_queststarter'")==null ? 'questrelation' : 'queststarter';
$questender   = MyVAL("SHOW TABLES LIKE 'creature_questender'")==null ? 'involvedrelation' : 'questender';
$questEntry = mysql_query("SELECT Id FROM quest_template LIMIT 1") ? "Id" : "entry";
$questTitle = mysql_query("SELECT Title FROM quest_template LIMIT 1") ? "Title" : "Name";

if (substr($argv[1], 0, 1) == '@') { // показывает задания, связанные с именем NPC
   $npc = substr($argv[1], 1);
   if (intval($npc) > 0)  $npcentry = intval($npc);
   else {
     $res = MyOP("SELECT entry FROM creature_template WHERE INSTR(name,". MySTR($npc) .")");
     $npc = "0";
     while($c = mysql_fetch_object($res))
       $npc = ($npc == "0" ? "$c->entry" : "$npc,$c->entry");
   }
   $argv[1] = "($questEntry IN (SELECT quest FROM creature_$queststarter WHERE id IN ($npc) UNION SELECT quest FROM creature_$questender WHERE id IN ($npc)))";
}


$args = "(SpecialFlags&1 != 0)"; // командная строка по умолчанию

$res_quest = MyOP("
SELECT *,
 IF((@t:=
     (SELECT GROUP_CONCAT(name SEPARATOR ';') FROM creature_template   WHERE entry IN (SELECT id FROM creature_$queststarter   WHERE quest=qt.$questEntry))
    ) IS NULL,
    CONCAT('GO ', (SELECT GROUP_CONCAT(name SEPARATOR ';') FROM gameobject_template WHERE entry IN (SELECT id FROM gameobject_$queststarter WHERE quest=qt.$questEntry)) )
    ,@t
 ) AS questStarter,
 IF((@t:=
     (SELECT GROUP_CONCAT(name SEPARATOR ';') FROM creature_template   WHERE entry IN (SELECT id FROM creature_$questender    WHERE quest=qt.$questEntry))
    ) IS NULL,
    CONCAT('GO ', (SELECT GROUP_CONCAT(name SEPARATOR ';') FROM gameobject_template WHERE entry IN (SELECT id FROM gameobject_$questender   WHERE quest=qt.$questEntry)) )
    ,@t
 ) AS questEnder,
 $questTitle AS Title
FROM quest_template AS qt
WHERE ".EntryConditionFromArgv($args, $questTitle, $questEntry)." ORDER BY Title, minlevel
");

$nquests = 0;

while($q = mysql_fetch_object($res_quest)) {

if (isset($q->NextQuestIdChain)) {
   // Sunwell core
   $q->NextQuestInChain = $q->NextQuestIdChain;
   $q->ReqItemId1 = $q->RequiredItemId1;
   $q->ReqItemId2 = $q->RequiredItemId2;
   $q->ReqItemId3 = $q->RequiredItemId3;
   $q->ReqItemId4 = $q->RequiredItemId4;
   $q->ReqItemCount1 = $q->RequiredItemCount1;
   $q->ReqItemCount2 = $q->RequiredItemCount2;
   $q->ReqItemCount3 = $q->RequiredItemCount3;
   $q->ReqItemCount4 = $q->RequiredItemCount4;
}

printf("%5d %5d %s %5d!  [%2d:%2d]%s %s  / %s => %s\n",
  @$q->entry != null ? $q->entry : $q->Id,
  $q->PrevQuestId,
  $q->NextQuestInChain!=0 && $q->NextQuestId != $q->NextQuestInChain ?
    ($q->NextQuestId!=0 ? "$q->NextQuestId$q->NextQuestInChain"
      : sprintf("%5d>", $q->NextQuestInChain))
    : sprintf("%5d", $q->NextQuestId!=0 ? $q->NextQuestId : 0),
  $q->ExclusiveGroup,
  $q->MinLevel, @$q->QuestLevel != null ? $q->QuestLevel : $q->Level,
  $q->SpecialFlags&1 ? "r" : " ",
  $q->Title, $q->questStarter, $q->questEnder
);

if ($verbose) {
for($i=1; $i <= 4; $i++) {
   eval("\$itemId = \$q->ReqItemId$i; \$itemCount = \$q->ReqItemCount$i;");
   if ($itemId == 0)  continue;
   $item = MyROWobj("SELECT * FROM item_template WHERE entry=$itemId");
   printf("%5dx  %5d  %s\n", $itemCount, $itemId, @$item->name);
}

print_r($q);
}

++$nquests;

}

printf("  === %d quests === \n", $nquests);

?>
