'.$row['name'].'';
	}
	$result[$index++] = 'Lernpunkte ';
	$result[$index++] = 'Trainingspunkte ';
	$result[$index++] = 'Stärke ';
	$result[$index++] = 'Geschwindigkeit ';
	$result[$index++] = 'Verteidigung ';
	$result[$index++] = 'Ausdauer ';
	$result[$index++] = 'Glück ';
	$result[$index++] = 'HP ';
	$result[$index++] = 'MP ';
	return $result;
}
function istrinstr($source, $insert, $search) {
	$pos = strpos($source, $search);
	$newstr = substr($source, 0, $pos).$insert.substr($source, $pos);
	return $newstr;
}
function getNPCUser() {
	$qry = db_query('SELECT * FROM user WHERE nickname = "'.NPC_USER_NAME.'";');
	return mysql_fetch_assoc($qry);
}
function getNPCAttacks() {
	$qry = db_query('SELECT name,id FROM attacken a WHERE info like "%NPC%";');
	$set = array();
	while ($result = mysql_fetch_assoc($qry)) {
		$set[$result['id']] = $result['name'];
	}
	return $set;
}
function learnNewAttacks($charid, $att_ids) {
	//auslesen der gelernten Attacken
	$sql = 'SELECT at_id, id FROM lernen WHERE besitzer = '.$charid.' AND at_id in ('.implode(',', $att_ids).');';
	$qry = db_query($sql);
	$learned_atts = array();
	while($result = mysql_fetch_assoc($qry)) {
		$learned_atts[$result['at_id']] = $result['id'];
	}
	
	//Namen der Attacken auslesen
	$sql = 'SELECT id,name FROM attacken WHERE id in ('.implode(',', $att_ids).');';
	$qry = db_query($sql);
	$att_names = array();
	while($result = mysql_fetch_assoc($qry)) {
		$att_names[$result['id']] = $result['name'];
	}
	
	$tmp_ids = array();
	foreach ($att_ids as $att) {
		if($learned_atts[$att] == NULL && $tmp_ids[$att] == NULL) { 
			$tmp_ids[$att] = 1;
			/*
			 * Keine Gute Lösung, es wird zwar von der db verhindert, dass doppelte Einträge gespeichert werden
			 * aber ungebrauchte Attacken werden so auch nicht gelöscht (höchstens der NPC wird gelöscht)
			 */
			db_query('INSERT INTO lernen(name,at_id,besitzer,aktiv) VALUES(\''.$att_names[$att].'\','.$att.','.$charid.',1);');
		}
	}
}
function getNPCAttackSet($charid, $att_ids) {
	$sql = 'SELECT at_id, id FROM lernen WHERE besitzer = '.$charid.' AND at_id in ('.implode(',', $att_ids).');';
	$qry = db_query($sql);
	$learned_atts = array();
	while($result = mysql_fetch_assoc($qry)) {
		$learned_atts[$result['at_id']] = $result['id'];
	}
	$attset = array();
	$i = 1;
	foreach ($att_ids as $att) {
		if($learned_atts[$att] != NULL) {
			$attset[$i] = $learned_atts[$att];
		} else {
			$attset[$i] = $att;
		}
		$i++;
	}
	return $attset;
}
function buildAttackSet($charid, $att_ids) {
	learnNewAttacks($charid, $att_ids);
	return getNPCAttackSet($charid, $att_ids);
}
function displayNPCInfo($action, $task, $charid) {
	//Form-Variablen
	echo "\t".' '."\n";
	echo "\t".' '."\n";
	
	$npc_race = getRaceTypeName('NPC'); // do not remove npc (there is a separate setting for this)
	$races = getRacesByType($npc_race); // remove all NPC
	$race_ids = array();
	$race_names = array();
	foreach($races as $race) {
		$race_ids[] = $race['id'];
		$race_names[] = $race['name'];
	}
	
	//initialisierung der Variablen für Inhalt
	$d_select_array = array($race_ids, array(300, 1200, 3600));
	$d_array = array('charakter', 'name', 'type', 'hp', 'mp', 'starke', 'verteidigung', 'speed',
		'ausdauer', 'glueck', 'zeit_rec', 'level', 'lvlmin', 'lvlmax', 'bild');
	$desc_select_array = array($race_names, array('5min', '20min', '60min'));
	$desc_array = array('ID', 'Name', 'Type', 'HP', 'MP', 'Stärke', 'Verteidigung', 'Geschwindigkeit',
			'Ausdauer', 'Glück', 'Kampfdauer in Sekunden', 'Level', 'Min-Level', 'Max-Level', 'Bild');
	$attset = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
	$attset_names = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
	$drop_items = getDropItemsAsOptions();
	//initialisieren falls editiert wird (dort existieren schon Inhalte)
	if($task == TASK_EDIT) {
		$attset = getAttackSet($charid, NORMALKAMPF);
		$attset_names = getAttackNames($attset);
		$sql = 'SELECT kl.*, ni.* FROM chars c inner join npc_item ni on ni.charakter = c.id inner join kampf_list kl on ni.charakter = kl.charakter WHERE c.id ='.$charid;
		//$sql = 'SELECT * FROM chars c inner join kampf_list kl on c.id = kl.charakter WHERE c.id ='.$charid;
		//echo $sql." ";
		$qry = db_query($sql);
		$row = mysql_fetch_assoc($qry);
		// Fetch the char array by getChar method (for backwards compat) with no caching enabled!
		$char_data = getChar($charid, false);
                foreach( $char_data as $key => $value ) {
			if($key != 'item') {
				$row[$key] = $value;
			}
		}
	}
	$attacks = getNPCAttacks();
	$colspan = 2;
	if($task == TASK_EDIT) {
		echo "\t".'
NPC bearbeiten  '."\n";
	} else {
		echo "\t".'NPC erstellen  '."\n";
	}
	echo "\t\t".''.$desc_array[0].' '.$row[$d_array[0]].'  '."\n";
	$j = 0;
	//allgemeine informationen
	for($i = 1;$i'.$desc_array[$i].' ';
			for($k = 0; $k < count($d_select_array[$j]); $k++) {
				echo "".$desc_select_array[$j][$k]." ";
			}
			echo '  '."\n";
			$j++;
		} else {
			echo "\t\t".''.$desc_array[$i].'  '."\n";
		}
	}
	//dropitems
	echo "\t".'Drop  '."\n";
	echo "\t\t".'Item ';
	//zusammenbauen des suchparameters
	if($row['datensatz'] == 'chars') {
		$selected = '\''.$row['feld'].','.$row['datensatz'].'\'';
	} else {
		$selected = '\''.$row['item'].','.$row['datensatz'].'\'';
	}
	foreach($drop_items as $drop) {
		if(strpos($drop, $selected) == true) {
			echo istrinstr($drop, ' selected', '>')." \n";
		} else {
			echo $drop."\n";
		}
	}
	echo '  '."\n";
	echo "\t\t".'Anzahl  '."\n";
	echo "\t\t".'maximale Wiederholungen  '."\n";
	echo "\t\t".'Preis  '."\n";
	//attacken informationen
	echo "\t".'Attacken bearbeiten  '."\n";
	for($i = 1;$i<11;$i++){
		echo "\t\t".'Runde '.$i.' ';
		echo " ".$attset_names[$attset[$i]]." ";
		foreach($attacks as $key => $value) {
			echo " ".$value." ";
		}
		echo '  '."\n";
	}
	//menü
	echo "\t".' '."\n";
	echo "\t".'NPC Übersicht  '."\n";
}
function editNPC($charid, $data, $att_ids) {
	$attset = buildAttackSet($charid, $att_ids);
	$error = validateAttackSet($attset);
	$d_array = array('name', 'type', 'hp', 'mp', 'starke', 'verteidigung', 'speed',
					'ausdauer', 'glueck', 'level', 'bild');
	$sql = 'UPDATE chars c INNER JOIN kampf_list kl ON c.id=kl.charakter INNER JOIN npc_item n ON c.id=n.charakter SET c.';
	$sql .= $d_array[0].' = \''.encodeNoHTMLWithBB($data[$d_array[0]]).'\'';
	for($i=1;$i";
	$u = db_query($sql);
	$log = 0;
	if(mysql_affected_rows() == 0 || $u === FALSE) {
		echo "\t\t\t".'Stats wurden nicht geändert oder es gab Fehler!  ';
	} else{
		echo "\t\t\t".'Änderungen wurden übernommen!  ';
		$log++;
	}
	if($error == NULL) {
		updateAttackSet($charid, NORMALKAMPF, $attset);
		//alter müll
		$qry = 'UPDATE chars SET attacken=\''.implode(',', $attset).'\', '.
				'attacken2=\''.implode(',', $attset).'\' WHERE id='.$charid.' LIMIT 1';
		db_query($qry);
		$log++;
		echo "\t\t\t".'Attacken übernommen!  ';
	} else {
		echo "\t\t\t".''.$error.'  ';
	}
	if($log >= 1) {
		logaction('Ein NPC wurde geändert (id = '.$charid.','.encodeNoHTMLWithBB(join($data,',')).')');
	}
}
function createNPC($data, $att_ids) {
	foreach ($data as $key => $value) {
		if($value == NULL) {
			echo 'fehler dude! bei '.$key;
		} else {
			$value = encodeNoHTMLWithBB($value);
		}
	}
	//Char erstellen, wichtig für die restlichen Sachen
	$success = erstelleChar(getNPCUser(), $data['type'], $data['name'], $data['bild'], $data['level'], 0, $data['hp'], $data['mp'], $data['starke'], $data['verteidigung'], $data['speed'],
	$data['ausdauer'], $data['glueck'], $data['type']);
	if($success) {
		//Char ID holen
		$qry = db_query('SELECT id FROM chars c WHERE name = "'.$data['name'].'";');
		$result = mysql_fetch_assoc($qry);
		$char_id = $result['id'];
		//zur Kampfliste hinzufügen :)
		$sql = 'INSERT INTO kampf_list(kampfname,charakter,rasse,besitzer,zeit,zeit_rec,lvlmax,ip,lvlmin) VALUES("NPC Kampf",'.$char_id.',"NPC","'.NPC_USER_NAME.'",0,'.
		$data['zeit_rec'].','.$data['lvlmax'].',0,'.$data['lvlmin'].');';
		db_query($sql);
		//drops hinzufügen :D
		$sql = 'INSERT INTO npc_item(charakter,item,feld,mal,datensatz,wieviel,preis) VALUES('.$char_id;
		$info = explode(',', $data['drop']);
		if($info[1] == 'chars') {
			$sql .= ',0,\''.$info[0].'\'';
		} else {
			$sql .= ','.$info[0].',\'\'';
		}
		$sql .= ','.$data['drop_max'].',\''.$info[1].'\','.$data['drop_anzahl'].','.$data['preis'].');';
		echo $sql;
		db_query($sql);
		//Attacksets soweit einbinden
		createAttackSet($char_id, NORMALKAMPF);
		$attset = buildAttackSet($char_id, $att_ids);
		$error = validateAttackSet($attset);
		echo "\t".'NPC '.$data['name'].' wurde erfolgreich erstellt!  '."\n";
		if($error != null) {
			echo "\t".'Es gab Probleme beim erstellen des Attacksets!  '."\n";
			echo "\t".''.$error.'  '."\n";
		} else {
			updateAttackSet($char_id, NORMALKAMPF, $attset);
			//alter müll
			$qry = 'UPDATE chars SET attacken=\''.implode(',', $attset).'\', '.
				'attacken2=\''.implode(',', $attset).'\' WHERE id='.$char_id.' LIMIT 1';
			db_query($qry);
		}
	}
	echo "\t".'NPC Übersicht  '."\n";
}
function displayDeleteNPC($charid) {
	$colspan = 1;
	$row = getChar($charid, false);
	echo "\t".'NPC löschen  '."\n";
	echo "\t".'Soll der NPC '.$row['name'].' wirklich gelöscht werden  '."\n";
	echo "\t".'ja , nein  '."\n";
}
function deleteNPC($charid) {
	$colspan = 1;
	$sql = 'DELETE FROM chars WHERE id = '.$charid.';';
	$qry = db_query($sql);
	if($qry === FALSE || mysql_affected_rows() <= 0){
		echo "\t".'Der NPC '.$row['name'].' konnte nicht gelöscht werden!  '."\n";;
	} else {
		echo "\t".'Der NPC '.$row['name'].' wurde gelöscht!  '."\n";;
	}
	echo "\t".'NPC Übersicht  '."\n";
}
function displayNPCOverview($page) {
	$d_array = array('id', 'name', 'level');
	$desc_array = array('id', 'Name', 'Level');
	$npc_race = getRaceTypeName('NPC'); // do not remove npc (there is a separate setting for this)
	$races = getRacesByType($npc_race); // remove all NPC
	foreach($races as $race) {
		$include_race_ids[] = '\''.$race['id'].'\'';
	}
	
	$sql = 'SELECT '.join($d_array,',').' from chars where rasse IN ('.implode(',', $include_race_ids).') ORDER BY level, Name LIMIT '.($page*20).', 20';
	$qry = db_query($sql);
	$colspan = count($d_array);
	echo "\t".'Übersicht  '."\n";;
	echo "\t".''."\n";
	for($i=1;$i'.$desc_array[$i].''."\n";
	}
	echo "\t\t".'Bearbeiten '."\n";
	echo "\t".'  '."\n";
	echo "\t".' '."\n";
	while($row = mysql_fetch_assoc($qry)){
		echo "\t".''."\n";
		for($i=1;$i'.$row[$d_array[$i]].''."\n";
		}
		echo "\t\t".''."\n";
		// Echo the delete and the edit button :)
		echo "\t\t\t".'Edit , '."\n";
		echo "\t\t\t".'Delete '."\n";
		echo "\t\t".' '."\n";
		echo "\t".'  '."\n";
	}
	$total = mysql_num_rows(db_query('SELECT '.join($d_array,',').' from chars where rasse IN ('.implode(',', $include_race_ids).')'));
	$url = '###LABEL### ';
	echo "\t".' '."\n";
	echo "\t".''.displayPagelinksNew(20, $total, $page, $url).'  '."\n";
	echo "\t".' '."\n";
	echo "\t".'Create a new NPC  '."\n";
}
function displayNPC($action, $task, $charid, $table, $page, $data, $att_ids){
	$colspan = 3;
	echo ''."\n";
}
?>