<?php

class Dungeon
{
    private $bs;
    private $map = [0 => []]; //êàðòà
    private $cord = ['x' => 0];
    private $sg = [
        1 => [1 => 1, 2 => 2, 3 => 3, 4 => 4],
        2 => [1 => 2, 2 => 3, 3 => 4, 4 => 1],
        3 => [1 => 3, 2 => 4, 3 => 1, 4 => 2],
        4 => [1 => 4, 2 => 1, 3 => 2, 4 => 3],
    ];
    public $info;
    public $error;
    public $gs = 1;
    public $information;
    public $id_dng;
    public $see;

    public function start()
    {
        global $u, $c, $code;
        $this->info = mysql_fetch_assoc(mysql_query('SELECT * FROM `dungeon_now` WHERE `id` = "' . $u->info['dnow'] . '" LIMIT 1'));
        $this->id_dng = $this->info['id2'];
        $this->gs = $u->info['s'];
        if ($this->gs < 1 || $this->gs > 4) {
            $this->gs = 1;
        }

        if ($this->info['bsid'] > 0) {
            $this->bs = mysql_fetch_assoc(mysql_query('SELECT * FROM `bs_turnirs` WHERE `city` = "' . $u->info['city'] . '" AND `id` = "' . $this->info['bsid'] . '" AND `time_start` = "' . $this->info['time_start'] . '" LIMIT 1'));
        }

        if (isset($_GET['itm_luck']) && $this->info['bsid'] == 0) {
            $this->itm_luck((int)$_GET['itm_luck']);
        } elseif (isset($_GET['itm_unluck']) && $this->info['bsid'] == 0) {
            $this->itm_unluck((int)$_GET['itm_unluck']);
        } elseif (isset($_GET['atack'])) {
            $this->atack((int)$_GET['atack']);
        } elseif (isset($_GET['take'])) {
            $this->takeinv((int)$_GET['take']);
        } elseif (isset($_GET['take_obj'])) {
            $this->takeit((int)$_GET['take_obj']);
        } elseif (isset($_GET['look'])) {
            if ((int)$_GET['look'] == 1) {
                if ($this->gs == 1) {
                    $this->gs = 2;
                } elseif ($this->gs == 2) {
                    $this->gs = 3;
                } elseif ($this->gs == 3) {
                    $this->gs = 4;
                } elseif ($this->gs == 4) {
                    $this->gs = 1;
                }
            } elseif ((int)$_GET['look'] == 2) {
                if ($this->gs == 1) {
                    $this->gs = 4;
                } elseif ($this->gs == 2) {
                    $this->gs = 1;
                } elseif ($this->gs == 3) {
                    $this->gs = 2;
                } elseif ($this->gs == 4) {
                    $this->gs = 3;
                }
            }
            mysql_query('UPDATE `stats` SET `s` = "' . ((int)$this->gs) . '" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
            $u->info['s'] = $this->gs;
        } elseif (isset($_GET['go'])) {
            //ïåðåìåùåíèå
            $this->testGo((int)$_GET['go']);
        }

        /* ãåíåðèðóåì âèä ïåðñîíàæà (òîëüêî êàðòà)
			$this->gs = 1; //ñìîòðèì ïðÿìî
						2; //ñìîòðèì ëåâî
						3; //ñìîòðèì âíèç
						4; //ñìîòðèì ïðàâî
						( ( ( `y` >= '.$u->info['y'].' && `y` <= '.($u->info['y']+4).' ) && ( `x` >= '.($u->info['x']-1).' && `x` <= '.($u->info['x']+1).' ) ) || ( (`x` = '.($u->info['x']+2).' || `x` = '.($u->info['x']-2).') && ( `y` = '.($u->info['y']+3).' || `y` = '.($u->info['y']+4).' ) ) )
		*/

        $whr = [1 => ' ((`x` <= ' . ($u->info['x'] + 2) . ' && `x` >= ' . ($u->info['x'] - 2) . ') && (`y` >= ' . $u->info['y'] . ' && `y` <= ' . ($u->info['y'] + 4) . ')) ', //ïðÿìî
            3 => ' ((`x` <= ' . ($u->info['x'] + 2) . ' && `x` >= ' . ($u->info['x'] - 2) . ') && (`y` <= ' . $u->info['y'] . ' && `y` >= ' . ($u->info['y'] - 4) . ')) ', //âíèç
            2 => ' ((`x` <= ' . $u->info['x'] . ' && `x` >= ' . ($u->info['x'] - 4) . ') && (`y` <= ' . ($u->info['y'] + 2) . ' && `y` >= ' . ($u->info['y'] - 2) . ')) ', //ëåâî
            4 => ' ((`x` >= ' . $u->info['x'] . ' && `x` <= ' . ($u->info['x'] + 4) . ') && (`y` <= ' . ($u->info['y'] + 2) . ' && `y` >= ' . ($u->info['y'] - 2) . ')) ' //ïðàâî
        ];

        $i = 1;
        $sp = mysql_query('SELECT * FROM `dungeon_map` WHERE `id_dng` = "' . $this->id_dng . '" AND ' . $whr[$this->gs] . ' ORDER BY `y` ASC , `x` ASC LIMIT 25');
        while ($pl = mysql_fetch_assoc($sp)) {
            $this->map[0][$pl['y'] . '_' . $pl['x']] = $pl;
            $i++;
        }
        $this->map['good'] = $i; //öåëûõ êëåòîê
        $this->map[1] = $this->genMatix();
        $this->lookDungeon();
    }

    public function pickitem($obj, $itm, $for, $data = '', $dn_delete = false, $chat = 0)
    {
        global $u;
        $itm = mysql_fetch_assoc(mysql_query('SELECT `id`,`name` FROM `items_main` WHERE `id` = "' . $itm . '" LIMIT 1'));
        if (isset($itm['id'])) {
            $tou = 0; //êàêîìó þçåðó ïðåäíàçíà÷åíî
            /* âûäåëÿåì ñëó÷àéíîãî þçåðà èç êîìàíäû */
            $itmnm = mysql_fetch_assoc(mysql_query('SELECT `name` FROM `items_main` WHERE `id` = "' . $itmz[0] . '" LIMIT 1'));
            $itmnm = $itmnm['name'];
            $asex = '';
            if ($u->info['sex'] == 1) {
                $asex = 'à';
            }

            if ($for > 0) {
                $tou = $for;
                $rtxt = '<b>' . $u->info['login'] . '</b> îáíàðóæèë' . $asex . ' ïðåäìåò &quot;' . $itm['name'] . '&quot; â &quot;' . $obj['name'] . '&quot;';
            } else {
                $rtxt = '<b>' . $u->info['login'] . '</b> îáíàðóæèë' . $asex . ' ïðåäìåò &quot;' . $itm['name'] . '&quot; â &quot;' . $obj['name'] . '&quot;, è âû ðåøèëè ðàçûãðàòü åãî';
            }
            if ($dn_delete) {
                $dn_delete = 1;
            } else {
                $dn_delete = 0;
            }
            if ($chat == 0) {
                mysql_query("INSERT INTO `chat` (`dn`,`city`,`room`,`login`,`to`,`text`,`time`,`type`,`toChat`,`typeTime`,`new`) VALUES ('" . $u->info['dnow'] . "','" . $u->info['city'] . "','" . $u->info['room'] . "','','','" . $rtxt . "','" . time() . "','6','0','1','1')");
            }
            $ins = mysql_query('INSERT INTO `dungeon_items` (`dn_delete`,`data`,`dn`,`user`,`item_id`,`time`,`x`,`y`) VALUES (
								"' . $dn_delete . '",
								"' . mysql_real_escape_string($data) . '",
								"' . $u->info['dnow'] . '",
								"' . $tou . '",
								"' . $itm['id'] . '",
								"' . time() . '",
								"' . $u->info['x'] . '",
								"' . $u->info['y'] . '")');
            return $ins;
        }
    }

    public function n_lead($who, $lead)
    {
        global $u, $c, $code;

        $to = mysql_fetch_assoc(mysql_query('SELECT `u`.`id`, `u`.`login`, `st`.`id`, `st`.`dnow` FROM `users` AS `u` LEFT JOIN `stats` AS `st` ON (`u`.`id` = `st`.`id`) WHERE `u`.`login` = "' . mysql_real_escape_string($who) . '" LIMIT 1'));

        if ($this->info['uid'] != $lead) {
            $this->error = '<b>Âû íå ëèäåð...</b>';
        } elseif (!isset($to['id'])) {
            $this->error = '<b>Ïåðñîíàæ íå íàéäåí...</b>';
        } elseif ($to['id'] == $this->info['uid']) {
            $this->error = '<b>Âû è òàê ëèäåð...</b>';
        } elseif ($to['dnow'] != $this->info['id']) {
            $this->error = '<b>Ïåðñîíàæ íå íàéäåí â âàøåé êîìàíäå...</b>';
        } else {
            mysql_query('UPDATE `dungeon_now` SET `uid` = "' . $to['id'] . '" WHERE `id` = "' . $this->info['id'] . '" LIMIT 1');
            if ($u->info['sex'] == 0) {
                $this->sys_chat('<b>' . $u->info['login'] . '</b> ïåðåäàë ëèäåðñòâî â ãðóïïå ïåðñîíàæó <b>' . $to['login'] . '</b>');
            } else {
                $this->sys_chat('<b>' . $u->info['login'] . '</b> ïåðåäàëà ëèäåðñòâî â ãðóïïå ïåðñîíàæó <b>' . $to['login'] . '</b>');
            }
            //header('Location: main.php');
            die('<script>top.frames[\'main\'].location = "main.php";</script>');
        }
    }

    public function go_to_hell($who, $lead)
    {
        global $u, $c, $code;

        $to = mysql_fetch_assoc(mysql_query('SELECT `u`.`id`, `u`.`login`, `st`.`id`, `st`.`dnow` FROM `users` AS `u` LEFT JOIN `stats` AS `st` ON (`u`.`id` = `st`.`id`) WHERE `u`.`login` = "' . mysql_real_escape_string($who) . '" LIMIT 1'));

        if ($this->info['uid'] != $lead) {
            $this->error = '<b>Âû íå ëèäåð...</b>';
        } elseif (!isset($to['id'])) {
            $this->error = '<b>Ïåðñîíàæ íå íàéäåí...</b>';
        } elseif ($to['id'] == $this->info['uid']) {
            $this->error = '<b>Ëèäåðà íåëüçÿ âûãíàòü...</b>';
        } elseif ($to['dnow'] != $this->info['id']) {
            $this->error = '<b>Ïåðñîíàæ íå íàéäåí â âàøåé êîìàíäå...</b>';
        } else {
            $rb = 321; // Ìàãè÷åñêèé ïîðòàë
            if ($u->info['room'] == 304) {
                $rb = 209; // Âõîä â ëåäÿíóþ ïåùåðó
            } elseif ($u->info['room'] == 396) {
                $rb = 395; // Êàíàëèçàöèÿ (Àíãåëñ)
            } elseif ($u->info['room'] == 398) {
                $rb = 321; // Âñå ïåùåðû
            } elseif ($this->info['id2'] == 3) {
                $rb = 293; // Âõîä â Êàòàêîìáû
            } elseif ($this->info['id2'] == 1) {
                $rb = 188; // Âõîä â Êàíàëèçàöèþ
            } elseif ($this->info['id2'] == 13) {
                $rb = 275; // Ãîðà Ëåãèîíà
            } elseif ($this->info['id2'] == 12) {
                $rb = 372; // Âõîä â Ïåùåðó Òûñÿ÷è Ïðîêëÿòèé
            } elseif ($this->info['id2'] == 444) {
                $rb = 321; // Âõîä â ïåùåðó èíêâ
            } elseif ($this->info['id2'] == 31) {
                $rb = 321; // Âõîä â ïåùåðó èíêâ
            } elseif ($this->info['id2'] == 101) {
                $rb = 321; // Âõîä â Áåçäíó

            }

            $sp = mysql_query('SELECT * FROM `dungeon_now` WHERE `time_finish` = "0" LIMIT 50');
            while ($pl = mysql_fetch_assoc($sp)) {
                $cn = mysql_fetch_assoc(mysql_query('SELECT `id` FROM `stats` WHERE `dnow` = "' . $pl['id'] . '" LIMIT 1'));
                if (!isset($cn['id'])) {
                    mysql_query('DELETE FROM `dungeon_bots` WHERE `dn` = "' . $pl['id'] . '" AND `for_dn` = "0"');
                    mysql_query('DELETE FROM `dungeon_obj` WHERE `dn` = "' . $pl['id'] . '" AND `for_dn` = "0"');
                    mysql_query('DELETE FROM `dungeon_items` WHERE `dn` = "' . $pl['id'] . '" AND `for_dn` = "0"');
                    mysql_query('DELETE FROM `dungeon_bots` WHERE `dn` = "' . $pl['id'] . '" AND `for_dn` = "0"');
                    mysql_query('DELETE FROM `dungeon_actions` WHERE `dn` = "' . $pl['id'] . '"');
                    mysql_query('UPDATE `dungeon_now` SET `time_finish` = "' . time() . '" WHERE `id` = "' . $pl['id'] . '" LIMIT 1');
                }
            }

            $city = mysql_fetch_assoc(mysql_query('SELECT `id`, `city` FROM `room` WHERE `id` = "' . $rb . '" LIMIT 1'));
            mysql_query('UPDATE `stats` SET `dnow` = "0" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
            mysql_query('UPDATE `users` SET `room` = "' . $rb . '", `city`="' . $city['city'] . '" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
            //óäàëÿåì âñå ïðåäìåòû êîòîðûå ïðîïàäàþò ïîñëå âûõîäà èç ïåùåðû
            mysql_query('UPDATE `items_users` SET `delete` = "' . time() . '" WHERE `uid` = "' . $u->info['id'] . '" AND `dn_delete` = "1" LIMIT 1000');

            mysql_query('UPDATE `items_users` SET `delete` = "' . time() . '" WHERE `uid` = "' . $u->info['id'] . '" AND (`item_id` = "1189" OR `item_id` = "4447" OR `item_id` = "1174") LIMIT 1000');

            if ($u->info['sex'] == 0) {
                $this->sys_chat('<b>' . $u->info['login'] . '</b> âûãíàë èç ïîõîäà ïåðñîíàæà <b>' . $to['login'] . '</b>');
            } else {
                $this->sys_chat('<b>' . $u->info['login'] . '</b> âûãíàëà èç ïîõîäà ïåðñîíàæà <b>' . $to['login'] . '</b>');
            }
            header('Location: /main.php');
        }
    }

    public function usersDng($laba = false)
    {
        global $u, $c;
        $r = '';
        $stt = [];
        if ($laba == false) {
            $sp = mysql_query('SELECT `u`.`id`,`st`.`id` FROM `stats` AS `u` LEFT JOIN `users` AS `st` ON (`st`.`id` = `u`.`id`) WHERE `u`.`dnow` = "' . $this->info['id'] . '" LIMIT 10');
        } else {
            $sp = mysql_query('SELECT `u`.`id` FROM `users` AS `u` LEFT JOIN `stats` AS `st` ON (`st`.`id` = `u`.`id`) WHERE `u`.`room` = 370 AND `st`.`dnow` = "' . $laba . '" AND `st`.`bot` = 0 LIMIT 6');
        }
        while ($pl = mysql_fetch_assoc($sp)) {
            $stt = $u->getStats($pl['id'], 0);
            if ($stt['mpAll'] > 0) {
                $pm = $stt['mpNow'] / $stt['mpAll'] * 100;
            }
            $ph = $stt['hpNow'] / $stt['hpAll'] * 100;
            $r .= '<table border="0" cellspacing="0" cellpadding="0" height="20">
<tr><td valign="middle"> &nbsp; <font color="#990000">' . $u->microLogin($pl['id'], 1) . '</font> &nbsp; </td>
<td valign="middle" width="120" ';
            if ($stt['mpAll'] < 1) {
                $r .= 'style="padding-top:12px"';
            }
            $r .= '>
<div style="position:relative;"><div id="vhp' . ($pl['id'] + 1000000000000) . '" title="Óðîâåíü æèçíè" align="left" class="seehp" style="position:absolute; top:-10px; width:120px; height:10px; z-index:12;"> ' . floor($stt['hpNow']) . '/' . $stt['hpAll'] . '</div>
<div title="Óðîâåíü æèçíè" class="hpborder" style="position:absolute; top:-10px; width:120px; height:9px; z-index:13;"><img src="https://img.new-combats.com/1x1.gif" height="9" width="1"></div>
<div class="hp_3 senohp" style="height:9px; width:' . floor(120 / 100 * $ph) . 'px; position:absolute; top:-10px; z-index:11;" id="lhp' . ($pl['id'] + 1000000000000) . '"><img src="https://img.new-combats.com/1x1.gif" height="9" width="1"></div>
<div title="Óðîâåíü æèçíè" class="hp_none" style="position:absolute; top:-10px; width:120px; height:10px; z-index:10;"><img src="https://img.new-combats.com/1x1.gif" height="10"></div>
';
            if ($stt['mpAll'] > 0) {
                $r .= '<div id="vmp' . ($pl['id'] + 1000000000000) . '" title="Óðîâåíü ìàíû" align="left" class="seemp" style="position:absolute; top:0px; width:120px; height:10px; z-index:12;"> ' . floor($stt['mpNow']) . '/' . $stt['mpAll'] . '</div>
<div title="Óðîâåíü ìàíû" class="hpborder" style="position:absolute; top:0px; width:120px; height:9px; z-index:13;"><img src="https://img.new-combats.com/1x1.gif" height="9" width="1"></div>
<div class="hp_mp senohp" style="height:9px; position:absolute; top:0px; width:' . floor(120 / 100 * $pm) . 'px; z-index:11;" id="lmp' . ($pl['id'] + 1000000000000) . '"><img src="https://img.new-combats.com/1x1.gif" height="9" width="1"></div>
<div title="Óðîâåíü ìàíû" class="hp_none" style="position:absolute; top:0px; width:120px; height:10px; z-index:10;"></div>';
            }
            $r .= '</div></td><td>';
            if ($this->info['uid'] == $pl['id']) {
                $r .= '<img src="https://img.new-combats.com/i/lead1.gif" title="Ëèäåð ãðóïïû" >';
            }
            if ($this->info['uid'] == $u->info['id'] && $pl['id'] == $u->info['id'] && $this->info['id2'] != 15) {
                $r .= '<a href="javascript: void(0);" onclick="top.n_lead();"><img src="https://img.new-combats.com/i/ico/ico_change_leader.gif" title="Íîâûé ëèäåð" /></a> ';
                $r .= '<a href="javascript: void(0);" onclick="top.go_from_psh();"><img src="https://img.new-combats.com/i/ico/ico_kill_member.gif" title="Âûãíàòü ñóïîñòàòà" /></a> ';
            }
            $r .= '</td></tr></table><br>';
            $r .= '<script>top.startHpRegen("main",' . ($pl['id'] + 1000000000000) . ',' . (0 + $stt['hpNow']) . ',' . (0 + $stt['hpAll']) . ',' . (0 + $stt['mpNow']) . ',' . (0 + $stt['mpAll']) . ',0,0,0,0,1);</script>';
        }
        unset($stt, $ph, $pm);
        return $r;
    }

    public function atack($id)
    {
        global $u, $c, $code;
        $bot = mysql_fetch_assoc(mysql_query('SELECT * FROM `dungeon_bots` WHERE `id2` = "' . $id . '" AND `for_dn` = "0" AND `dn` = "' . $this->info['id'] . '" AND `delete` = "0" LIMIT 1'));
        if (isset($bot['id2'])) {
            if (($u->info['x'] != $bot['x'] || $bot['y'] != $u->info['y']) && $this->testLike($u->info['x'], $u->info['y'], $bot['x'], $bot['y']) == 1) {
                //Ñîçäàåì ïîäåèíîê
                $tbtl = mysql_fetch_assoc(mysql_query('SELECT * FROM `battle` WHERE `dn_id` = "' . $this->info['id'] . '" AND `team_win` = "-1" AND `x` = "' . $bot['x'] . '" AND `y` = "' . $bot['y'] . '" LIMIT 1'));
                if (isset($tbtl['id'])) {
                    //âñòóïàåì â ïîåäèíîê
                    mysql_query('UPDATE `users` SET `battle` = "' . $tbtl['id'] . '" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
                    mysql_query('UPDATE `stats` SET `team` = "1" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
                    /*#$this->error = 'Íàïàäàåì ... '; // <script>location="main.php?rnd='.$code.'";</script>*/
                    //header('location: main.php');
                    die('<script>top.frames[\'main\'].location = "main.php";</script>');
                } else {
                    $btl_id = 0;
                    //$expB = -77.77;
                    $expB = 0;
                    $btl = ['players' => '', 'timeout' => 120, 'type' => 0, 'invis' => 0, 'noinc' => 0, 'travmChance' => 0, 'typeBattle' => 0, 'addExp' => $expB, 'money' => 0];

                    $ins = mysql_query('INSERT INTO `battle` (`dungeon`,`dn_id`,`x`,`y`,`city`,`time_start`,`players`,`timeout`,`type`,`invis`,`noinc`,`travmChance`,`typeBattle`,`addExp`,`money`) VALUES (
														"' . $this->info['id2'] . '",
														"' . $this->info['id'] . '",
														"' . $bot['x'] . '",
														"' . $bot['y'] . '",
														"' . $u->info['city'] . '",
														"' . time() . '",
														"' . $btl['players'] . '",
														"' . $btl['timeout'] . '",
														"' . $btl['type'] . '",
														"' . $btl['invis'] . '",
														"' . $btl['noinc'] . '",
														"' . $btl['travmChance'] . '",
														"' . $btl['typeBattle'] . '",
														"' . $btl['addExp'] . '",
														"' . $btl['money'] . '")');
                    $btl_id = mysql_insert_id();

                    if ($btl_id > 0) {
                        //Äîáàâëÿåì áîòîâ
                        $sp = mysql_query('SELECT * FROM `dungeon_bots` WHERE `for_dn` = "0" AND `dn` = "' . $this->info['id'] . '" AND `x` = "' . $bot['x'] . '" AND `y` = "' . $bot['y'] . '" AND `delete` = "0" LIMIT 1001');
                        $j = 0;
                        $logins_bot = [];
                        while ($pl = mysql_fetch_assoc($sp)) {
                            $jui = 1;
                            mysql_query('UPDATE `dungeon_bots` SET `inBattle` = "' . $btl_id . '" WHERE `id2` = "' . $pl['id2'] . '" LIMIT 1');
                            while ($jui <= $pl['colvo']) {
                                $k = $u->addNewbot($pl['id_bot'], null, null, $logins_bot);
                                $logins_bot = $k['logins_bot'];
                                if ($k != false) {
                                    $upd = mysql_query('UPDATE `users` SET `battle` = "' . $btl_id . '" WHERE `id` = "' . $k['id'] . '" LIMIT 1');
                                    if ($upd) {
                                        $upd = mysql_query('UPDATE `stats` SET `x`="' . $bot['x'] . '",`y`="' . $bot['y'] . '",`team` = "2" WHERE `id` = "' . $k['id'] . '" LIMIT 1');
                                        if ($upd) {
                                            $j++;
                                        }
                                    }
                                }
                                $jui++;
                            }
                        }
                        unset($logins_bot);
                        if ($j > 0) {
                            mysql_query('UPDATE `users` SET `battle` = "' . $btl_id . '" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
                            mysql_query('UPDATE `stats` SET `team` = "1" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
                            /*#$this->error = 'Íàïàäàåì ...';// <script>location="main.php?rnd='.$code.'";</script>*/ //
                            //header('location: main.php');
                            die('<script>top.frames[\'main\'].location = "main.php";</script>');
                        } else {
                            $this->error = 'Íå óäàëîñü íàïàñòü, îøèáêà îáüåêòà íàïàäåíèÿ ...';
                        }
                    } else {
                        $this->error = 'Íå óäàëîñü ñîçäàòü ïîåäèíîê ...';
                    }
                }
            } else {
                $this->error = 'Íå óäàëîñü íàïàñòü ...';
            }
        } else {
            if (isset($this->bs['id']) || $this->info['id2'] == 15) {
                $bot = mysql_fetch_assoc(mysql_query('SELECT `u`.*,`st`.* FROM `users` AS `u` LEFT JOIN `stats` AS `st` ON (`u`.`id` = `st`.`id`) WHERE `u`.`id` = "' . mysql_real_escape_string($id) . '" LIMIT 1'));
                if (($u->info['x'] != $bot['x'] || $bot['y'] != $u->info['y']) && $this->testLike($u->info['x'], $u->info['y'], $bot['x'], $bot['y']) == 1) {
                    //Ñîçäàåì ïîäåèíîê
                    $tbtl = mysql_fetch_assoc(mysql_query('SELECT * FROM `battle` WHERE `dn_id` = "' . $this->info['id'] . '" AND `team_win` = "-1" AND `x` = "' . $bot['x'] . '" AND `y` = "' . $bot['y'] . '" LIMIT 1'));
                    //die('Íàïàäåíèÿ âðåìåííî çàïðåùåíû. ['.$tbtl['id'].'] 5-10 ìèí.');
                    if (isset($tbtl['id'])) {
                        //âñòóïàåì â ïîåäèíîê
                        $lstm = mysql_fetch_assoc(mysql_query('SELECT `u`.*,`st`.* FROM `users` AS `u` LEFT JOIN `stats` AS `st` ON (`u`.`id` = `st`.`id`) WHERE `u`.`battle`="' . $tbtl['id'] . '" ORDER BY  `st`.`team` DESC LIMIT 1'));
                        mysql_query('UPDATE `users` SET `battle` = "' . $tbtl['id'] . '" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
                        $nextteam = $lstm['team'] + 1;
                        mysql_query('UPDATE `stats` SET `team` = "' . $nextteam . '" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
                        mysql_query('UPDATE `stats` SET `x` = "' . $bot['x'] . '", `y` = "' . $bot['y'] . '" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
                        //
                        if ($this->info['bsid'] > 0) {

                            $me_real = mysql_fetch_assoc(mysql_query('SELECT `id`,`login`,`align`,`clan`,`battle`,`level` FROM `users` WHERE `inUser` = "' . $u->info['id'] . '" AND `login` = "' . $u->info['login'] . '" LIMIT 1'));
                            $usr_real = mysql_fetch_assoc(mysql_query('SELECT `id`,`login`,`align`,`clan`,`battle`,`level` FROM `users` WHERE `inUser` = "' . $bot['id'] . '" AND `login` = "' . $bot['login'] . '" LIMIT 1'));
                            mysql_query('UPDATE `stats` SET `tactic7`= 20.000 WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
                            //Çàíîñèì â ëîã ÁÑ
                            if ($u->info['sex'] == 0) {
                                $text = '{u1} âìåøàëñÿ â ïîåäèíîê íàïàâ íà {u2} <a target=_blank href=/logs.php?log=' . $tbtl['id'] . ' >»»</a>';
                            } else {
                                $text = '{u1} âìåøàëàñÿ â ïîåäèíîê íàïàâ íà {u2} <a target=_blank href=/logs.php?log=' . $tbtl['id'] . ' >»»</a>';
                            }
                            if (isset($usr_real['id'])) {
                                $usrreal = '';
                                if ($usr_real['align'] > 0) {
                                    $usrreal .= '<img src=https://img.new-combats.com/i/align/align' . $usr_real['align'] . '.gif width=12 height=15 >';
                                }
                                if ($usr_real['clan'] > 0) {
                                    $usrreal .= '<img src=https://img.new-combats.com/i/clan/' . $usr_real['clan'] . '.gif width=24 height=15 >';
                                }
                                $usrreal .= '<b>' . $usr_real['login'] . '</b>[' . $usr_real['level'] . ']<a target=_blank href=https://new-combats.com/info/' . $usr_real['id'] . ' ><img width=12 hiehgt=11 src=https://img.new-combats.com/i/inf_capitalcity.gif ></a>';
                            } else {
                                $mereal = '<i>Íåâèäèìêà</i>[??]';
                            }
                            if (isset($me_real['id'])) {
                                $mereal = '';
                                if ($me_real['align'] > 0) {
                                    $mereal .= '<img src=https://img.new-combats.com/i/align/align' . $me_real['align'] . '.gif width=12 height=15 >';
                                }
                                if ($me_real['clan'] > 0) {
                                    $mereal .= '<img src=https://img.new-combats.com/i/clan/' . $me_real['clan'] . '.gif width=24 height=15 >';
                                }
                                $mereal .= '<b>' . $me_real['login'] . '</b>[' . $me_real['level'] . ']<a target=_blank href=https://new-combats.com/info/' . $me_real['id'] . ' ><img width=12 hiehgt=11 src=https://img.new-combats.com/i/inf_capitalcity.gif ></a>';
                            } else {
                                $mereal = '<i>Íåâèäèìêà</i>[??]';
                            }
                            $text = str_replace('{u1}', $mereal, $text);
                            $text = str_replace('{u2}', $usrreal, $text);
                            //Äîáàâëÿåì â ëîã ÁÑ
                            mysql_query('INSERT INTO `bs_logs` (`type`,`text`,`time`,`id_bs`,`count_bs`,`city`,`m`,`u`) VALUES (
									"1", "' . mysql_real_escape_string($text) . '", "' . time() . '", "' . $this->bs['id'] . '", "' . $this->bs['count'] . '", "' . $this->bs['city'] . '",
									"' . round($this->bs['money'] * 0.85, 2) . '","' . $i . '"
								)');
                            //
                        }
                        //
                        $this->error = 'Íàïàäàåì ... <script>location="main.php?rnd=' . $code . '";</script>';
                    } else {
                        $btl_id = 0;
                        //$expB = -77.77;
                        if ($this->info['id2'] == 1) {
                            $expB = 200;
                        }
                        $btl = ['players' => '', 'timeout' => 40, 'type' => 0, 'invis' => 0, 'noinc' => 0, 'travmChance' => 0, 'typeBattle' => 0, 'addExp' => $expB, 'money' => 0, 'inTurnir' => 0];

                        if ($this->info['bsid'] > 0) {
                            $btl['inTurnir'] = $this->info['bsid'];
                        }

                        $ins = mysql_query('INSERT INTO `battle` (`inTurnir`,`dungeon`,`dn_id`,`x`,`y`,`city`,`time_start`,`players`,`timeout`,`type`,`invis`,`noinc`,`travmChance`,`typeBattle`,`addExp`,`money`) VALUES (
															"' . $btl['inTurnir'] . '",
															"' . $this->info['id2'] . '",
															"' . $this->info['id'] . '",
															"' . $bot['x'] . '",
															"' . $bot['y'] . '",
															"' . $u->info['city'] . '",
															"' . time() . '",
															"' . $btl['players'] . '",
															"' . $btl['timeout'] . '",
															"' . $btl['type'] . '",
															"' . $btl['invis'] . '",
															"' . $btl['noinc'] . '",
															"' . $btl['travmChance'] . '",
															"' . $btl['typeBattle'] . '",
															"' . $btl['addExp'] . '",
															"' . $btl['money'] . '")');
                        $btl_id = mysql_insert_id();
                        mysql_query('UPDATE `stats` SET `x` = "' . $bot['x'] . '", `y` = "' . $bot['y'] . '" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
                        mysql_query('UPDATE `stats` SET `x` = "' . $bot['x'] . '", `y` = "' . $bot['y'] . '" WHERE `id` = "' . $bot['id'] . '" LIMIT 1');

                        if ($btl_id > 0) {
                            //Äîáàâëÿåì áîòîâ
                            mysql_query('UPDATE `users` SET `battle` = "' . $btl_id . '" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
                            mysql_query('UPDATE `stats` SET `team` = "1" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');

                            mysql_query('UPDATE `users` SET `battle` = "' . $btl_id . '" WHERE `id` = "' . $bot['id'] . '" LIMIT 1');
                            mysql_query('UPDATE `stats` SET `team` = "2" WHERE `id` = "' . $bot['id'] . '" LIMIT 1');

                            if ($u->stats['hpNow'] < 1) {
                                $u->stats['hpNow'] = 1;
                                mysql_query('UPDATE `stats` SET `hpNow` = "1" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
                                mysql_query('UPDATE `users` SET `lose` = `lose` + 1 WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
                            }

                            if ($bot['hpNow'] < 1) {
                                $bot['hpNow'] = 1;
                                mysql_query('UPDATE `stats` SET `hpNow` = "1" WHERE `id` = "' . $bot['id'] . '" LIMIT 1');
                                mysql_query('UPDATE `users` SET `lose` = `lose` + 1 WHERE `id` = "' . $bot['id'] . '" LIMIT 1');
                            }

                            if ($this->info['bsid'] > 0) {

                                $me_real = mysql_fetch_assoc(mysql_query('SELECT `id`,`login`,`align`,`clan`,`battle`,`level` FROM `users` WHERE `inUser` = "' . $u->info['id'] . '" AND `login` = "' . $u->info['login'] . '" LIMIT 1'));
                                $usr_real = mysql_fetch_assoc(mysql_query('SELECT `id`,`login`,`align`,`clan`,`battle`,`level` FROM `users` WHERE `inUser` = "' . $bot['id'] . '" AND `login` = "' . $bot['login'] . '" LIMIT 1'));

                                //Çàíîñèì â ëîã ÁÑ
                                if ($u->info['sex'] == 0) {
                                    $text = '{u1} íàïàë íà {u2} çàâÿçàëñÿ áîé <a target=_blank href=/logs.php?log=' . $btl_id . ' >»»</a>';
                                } else {
                                    $text = '{u1} íàïàëà íà {u2} çàâÿçàëñÿ áîé <a target=_blank href=/logs.php?log=' . $btl_id . ' >»»</a>';
                                }
                                if (isset($usr_real['id'])) {
                                    $usrreal = '';
                                    if ($usr_real['align'] > 0) {
                                        $usrreal .= '<img src=https://img.new-combats.com/i/align/align' . $usr_real['align'] . '.gif width=12 height=15 >';
                                    }
                                    if ($usr_real['clan'] > 0) {
                                        $usrreal .= '<img src=https://img.new-combats.com/i/clan/' . $usr_real['clan'] . '.gif width=24 height=15 >';
                                    }
                                    $usrreal .= '<b>' . $usr_real['login'] . '</b>[' . $usr_real['level'] . ']<a target=_blank href=https://new-combats.com/info/' . $usr_real['id'] . ' ><img width=12 hiehgt=11 src=https://img.new-combats.com/i/inf_capitalcity.gif ></a>';
                                } else {
                                    $mereal = '<i>Íåâèäèìêà</i>[??]';
                                }
                                if (isset($me_real['id'])) {
                                    $mereal = '';
                                    if ($me_real['align'] > 0) {
                                        $mereal .= '<img src=https://img.new-combats.com/i/align/align' . $me_real['align'] . '.gif width=12 height=15 >';
                                    }
                                    if ($me_real['clan'] > 0) {
                                        $mereal .= '<img src=https://img.new-combats.com/i/clan/' . $me_real['clan'] . '.gif width=24 height=15 >';
                                    }
                                    $mereal .= '<b>' . $me_real['login'] . '</b>[' . $me_real['level'] . ']<a target=_blank href=https://new-combats.com/info/' . $me_real['id'] . ' ><img width=12 hiehgt=11 src=https://img.new-combats.com/i/inf_capitalcity.gif ></a>';
                                } else {
                                    $mereal = '<i>Íåâèäèìêà</i>[??]';
                                }
                                $text = str_replace('{u1}', $mereal, $text);
                                $text = str_replace('{u2}', $usrreal, $text);
                                //Äîáàâëÿåì â ëîã ÁÑ
                                mysql_query('INSERT INTO `bs_logs` (`type`,`text`,`time`,`id_bs`,`count_bs`,`city`,`m`,`u`) VALUES (
									"1", "' . mysql_real_escape_string($text) . '", "' . time() . '", "' . $this->bs['id'] . '", "' . $this->bs['count'] . '", "' . $this->bs['city'] . '",
									"' . round($this->bs['money'] * 0.85, 2) . '","' . $i . '"
								)');
                                //
                            }

                            $this->error = 'Íàïàäàåì ... <script>location="main.php?rnd=' . $code . '";</script>';
                        } else {
                            $this->error = 'Íå óäàëîñü ñîçäàòü ïîåäèíîê ...';
                        }
                    }
                } else {
                    $this->error = 'Íå óäàëîñü íàïàñòü ...';
                }
            } else {
                $this->error = 'Íå óäàëîñü íàïàñòü, ñëèøêîì äàëåêî (id' . $id . ')	...';
            }
        }
    }

    public function testDie()
    {
        global $u, $c, $code;
        $dies = mysql_fetch_assoc(mysql_query('SELECT COUNT(`id`) FROM `dungeon_actions` WHERE `uid` = "' . $u->info['id'] . '" AND `dn` = "' . $u->info['dnow'] . '" AND `vars` = "die" LIMIT 1'));
        $dies = $dies[0];


        if ($u->stats['hpNow'] < 1 || $dies > 2) {
            if ($dies < 2) {
                mysql_query('INSERT INTO `dungeon_actions` (`dn`,`uid`,`x`,`y`,`time`,`vars`,`vals`) VALUES (
				    "' . $u->info['dnow'] . '","' . $u->info['id'] . '","' . $u->info['x'] . '","' . $u->info['y'] . '","' . time() . '","die",""
			    )');
                //21:50 ßðîïîëê òðàãè÷åñêè ïîãèá è íàõîäèòñÿ â êîìíàòå "Ýòàæ 2 - Âîäîñòîê"
                $dnow = mysql_fetch_assoc(mysql_query('SELECT id2 FROM `dungeon_now` WHERE  `id` = "' . $u->info['dnow'] . '" LIMIT 1'));
                $room = mysql_fetch_assoc(mysql_query('SELECT name FROM `dungeon_map_info` WHERE `x` = "' . $u->info['res_x'] . '" AND  `y` = "' . $u->info['res_y'] . '" AND `id_dng` = "' . $dnow['id2'] . '" LIMIT 1'));

                if ($u->info['sex'] == 0) {
                    $this->sys_chat('<b>' . $u->info['login'] . '</b> òðàãè÷åñêè ïîãèá è íàõîäèòñÿ â êîìíàòå &quot;' . $room['name'] . '&quot;');
                } else {
                    $this->sys_chat('<b>' . $u->info['login'] . '</b> òðàãè÷åñêè ïîãèáëà è íàõîäèòñÿ â êîìíàòå &quot;' . $room['name'] . '&quot;');
                }
                mysql_query('UPDATE `stats` SET `hpNow` = "1",`x` = "' . $u->info['res_x'] . '",`y` = "' . $u->info['res_y'] . '",`s` = "' . $u->info['res_s'] . '" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
                //header('location: main.php');
                die('<script>top.frames[\'main\'].location = "main.php";</script>');
            } else {
                mysql_query('INSERT INTO `dungeon_actions` (`dn`,`uid`,`x`,`y`,`time`,`vars`,`vals`) VALUES (
				    "' . $u->info['dnow'] . '","' . $u->info['id'] . '","' . $u->info['x'] . '","' . $u->info['y'] . '","' . time() . '","die",""
			    )');
                //21:50 ßðîïîëê òðàãè÷åñêè ïîãèá è íàõîäèòñÿ â êîìíàòå "Ýòàæ 2 - Âîäîñòîê"
                if ($u->info['sex'] == 0) {
                    $this->sys_chat('<b>' . $u->info['login'] . '</b> òðàãè÷åñêè ïîãèá áåç ïðàâà íà âîñêðåøåíèå');
                } else {
                    $this->sys_chat('<b>' . $u->info['login'] . '</b> òðàãè÷åñêè ïîãèáëà áåç ïðàâà íà âîñêðåøåíèå');
                }
                $_GET['exitd'] = true;
            }
        }
    }

    //Ïðåäìåòû äëÿ ÁÑ
    public $itbs = [0 => 558, 1 => 722, 2 => 718, 3 => 700, 4 => 291, 5 => 545, 6 => 533, 7 => 536, 8 => 628, 9 => 710, 10 => 654, 11 => 684, 12 => 689, 13 => 552, 14 => 552, 15 => 716, 16 => 320, 17 => 290, 18 => 543, 19 => 528, 20 => 540, 21 => 627, 22 => 709, 23 => 653, 24 => 681, 25 => 687, 26 => 523, 27 => 523, 28 => 712, 29 => 1151, 30 => 544, 31 => 541, 32 => 525, 33 => 539, 34 => 633, 35 => 707, 36 => 647, 37 => 680, 38 => 685, 39 => 567, 40 => 717, 41 => 706, 42 => 527, 43 => 537, 44 => 626, 45 => 708, 46 => 723, 47 => 651, 48 => 683, 49 => 686, 50 => 2862, 51 => 2863, 52 => 2864];

    public function sys_chat($rtxt)
    {
        global $u;
        mysql_query("INSERT INTO `chat` (`dn`,`city`,`room`,`login`,`to`,`text`,`time`,`type`,`toChat`,`typeTime`,`new`) VALUES ('" . $u->info['dnow'] . "','" . $u->info['city'] . "','" . $u->info['room'] . "','','','" . $rtxt . "','" . time() . "','6','0','1','1')");
    }

    public function takeit($id)
    {
        global $u, $c, $code, $magic;
        $obj = mysql_fetch_assoc(mysql_query('SELECT * FROM `dungeon_obj` WHERE `id` = "' . $id . '" AND `for_dn` = "0" AND `dn` = "' . $this->info['id'] . '" LIMIT 1'));
        if (isset($obj['id'])) {
            $tbot = mysql_fetch_assoc(mysql_query('SELECT * FROM `dungeon_bots` WHERE `x` = "' . $obj['x'] . '" AND `y` = "' . $obj['y'] . '" AND `dn` = "' . $this->info['id'] . '" AND `for_dn` = "0" AND `delete` = "0" LIMIT 1'));


            $i = 0;
            # Ñîçäàåì ÌÀÑÑÈÂ { $act_sl['save_pos'] = "save_pos" }èëè { $act_sl['port'] = "10:20" }
            $act_sl = [];
            $act_sm = explode('|', $obj['action']);
            while ($i < count($act_sm)) {
                $s = explode(':', $act_sm[$i]);
                if (isset($s[1]) && $s[1] != '') {
                    $act_sl[$s[0]] = $s[1];
                } else {
                    $act_sl[$s[0]] = $s[0];
                }
                $i++;
            }

            if (isset($tbot['id2'])) {
                $this->error = 'Íå óäàëîñü, ÷òî-òî èëè êòî-òî ìåøàåò ...';
            } elseif ($this->testLike($u->info['x'], $u->info['y'], $obj['x'], $obj['y']) != 1) {
                $this->error = 'Íå óäàëîñü, ñëèøêîì äàëåêî ...!';
            } else {
                $a = explode('|', $obj['action']);
                $r = '';
                $i = 0;
                while ($i < count($a)) {
                    $s = explode(':', $a[$i]);

                    if ($s[0] == 'kill_bot') {
                        //Òðåáóåòñÿ óáèòü áîòîâ
                        $t = explode(',', $s[1]);
                        $tr_gd = 0;

                        //Ïðîâåðÿåì êîãî íóæíî óáèòü è óáèëè-ëè
                        $j = 1;
                        $jn = 0;
                        while ($j < count($t)) {
                            $itm = explode('.', $t[$j]);
                            //[0] - x , [1] - y
                            $bot_itm = mysql_fetch_assoc(mysql_query('SELECT `u`.`id2`,`st`.`login` FROM `dungeon_bots` AS `u` LEFT JOIN `test_bot` AS `st` ON (`u`.`id_bot` = `st`.`id`) WHERE `u`.`dn` = "' . $u->info['dnow'] . '" AND `u`.`x` = "' . $itm[0] . '" AND `u`.`y` = "' . $itm[1] . '" AND `u`.`delete` = "0" LIMIT 10'));
                            if (isset($bot_itm['id2'])) {
                                $jn++;
                            }
                            $j++;
                        }

                        if ($jn == 0) {
                            $tr_gd = 1;
                        }

                        unset($itm, $bot_itm, $jn);
                        if ($tr_gd == 0) {
                            if ($t[0] == '0') {
                                $r .= 'Íå óäàëîñü, ÷òî-òî èëè êòî-òî ìåøàåò ...';
                            } else {
                                $r .= $t[0];
                            }
                            $i = count($a);
                        }
                    } elseif ($s[0] == 'kill_bot_d') {
                        //Òðåáóåòñÿ óáèòü áîòîâ (âñå áîòû íàïàäàþò , åñëè ÷òî-òî íå òàê )
                        $t = explode(',', $s[1]);
                        $tr_gd = 0;

                        //Ïðîâåðÿåì êîãî íóæíî óáèòü è óáèëè-ëè
                        $j = 1;
                        $jn = 0;
                        $tuz = mysql_fetch_assoc(mysql_query('SELECT `x`,`y`,`id`,`hpNow` FROM `stats` WHERE `dnow` = "' . $this->info['id'] . '" AND ( (`x` = ' . ($pl['x'] + 1) . ' AND `y` = ' . ($pl['y']) . ') OR (`x` = ' . ($pl['x'] - 1) . ' AND `y` = ' . ($pl['y']) . ') OR (`x` = ' . ($pl['x']) . ' AND `y` = ' . ($pl['y'] + 1) . ') OR (`x` = ' . ($pl['x']) . ' AND `y` = ' . ($pl['y'] - 1) . ') ) LIMIT 1'));
                        while ($j < count($t)) {
                            $itm = explode('.', $t[$j]);
                            //[0] - x , [1] - y
                            $bot_itm_sp = mysql_query('SELECT `u`.*,`st`.* FROM `dungeon_bots` AS `u` LEFT JOIN `test_bot` AS `st` ON (`u`.`id_bot` = `st`.`id`) WHERE `u`.`dn` = "' . $u->info['dnow'] . '" AND `u`.`x` = "' . $itm[0] . '" AND `u`.`y` = "' . $itm[1] . '" AND `u`.`delete` = "0" LIMIT 50');
                            while ($bot_itm = mysql_fetch_assoc($bot_itm_sp)) {
                                $jn++;
                                $this->botAtack($bot_itm, $u->info, 1);
                            }
                            $j++;
                        }

                        if ($jn == 0) {
                            $tr_gd = 1;
                        }

                        unset($itm, $bot_itm, $jn);
                        if ($tr_gd == 0) {
                            if ($t[0] == '0') {
                                $r .= 'Íå óäàëîñü, ÷òî-òî èëè êòî-òî ìåøàåò ...';
                            } else {
                                $r .= $t[0];
                            }
                            $r .= ' Ìîíñòðû íà÷àëè àòàêîâàòü âàñ!';
                            $i = count($a);
                        }
                    } elseif ($s[0] == 'ditm') {
                        //òðåáóåò ïðåäìåò äëÿ äåéñòâèÿ
                        $j = 0;
                        $t = explode(',', $s[1]);
                        $tr_gd = 1;
                        while ($j < count($t)) {
                            $itm = explode('=', $t[$j]);
                            $uitm = mysql_fetch_array(mysql_query('SELECT `name` FROM `items_main` WHERE `id` = "' . $itm[0] . '" LIMIT 1'));
                            mysql_query('UPDATE `items_users` SET `delete` = "' . time() . '" WHERE `item_id` = "' . $itm[0] . '" AND `uid` = "' . $u->info['id'] . '" AND (`delete` = 0 OR `delete` = 100) AND `inShop` = 0 AND `inTransfer` = 0 AND `inOdet` = 0 LIMIT ' . $itm[1]);
                            $r .= 'Ïðåäìåò &quot;<b>' . $uitm['name'] . '</b>&quot; (x' . $itm[1] . ') áûë óòðà÷åí...<br>';
                            $j++;
                        }
                    } elseif ($s[0] == 'tritm') {
                        //òðåáóåò ïðåäìåò äëÿ äåéñòâèÿ
                        $j = 0;
                        $t = explode(',', $s[1]);
                        $j = 0;
                        $tr_gd = 1;
                        while ($j < count($t)) {
                            $itm = explode('=', $t[$j]);
                            $uitm = mysql_fetch_array(mysql_query('SELECT COUNT(`id`) FROM `items_users` WHERE `item_id` = "' . $itm[0] . '" AND `uid` = "' . $u->info['id'] . '" AND (`delete` = 0 OR `delete` = 100) AND `inShop` = 0 AND `inTransfer` = 0 AND `inOdet` = 0 LIMIT ' . $itm[1]));
                            $uitm = $uitm[0];
                            if ($uitm < $itm[1]) {
                                $tr_gd = 0;
                                $uitm = mysql_fetch_assoc(mysql_query('SELECT `name` FROM `items_main` WHERE `id` = "' . $itm[0] . '" LIMIT 1'));
                                $r .= 'Òðåáóåòñÿ ïðåäìåò &quot;<b>' . $uitm['name'] . '</b>&quot; (x' . $itm[1] . ')<br>';
                            }
                            $j++;
                        }
                        if (rand(0, 10000) > $itm[2] * 100) {
                            $tr_gd = 0;
                            $r .= 'Ñòðàííî, íî íè÷åãî íå ïðîèçîøëî...<br>';
                        }
                        if ($tr_gd == 1) {
                            //âñå îòëè÷íî
                        } else {
                            $i = count($a);
                        }
                    } elseif ($s[0] == 'repl_ptp') {
                        require_once('dnaction/_dungeon_replace.php');
                        die();
                        //header('Location: ../../modules_data/location/_dungeon_replace.php');
                    } elseif ($s[0] == 'add_eff') {
                        //Êàñòóåì ýôôåêò
                        $t = explode(',', $s[1]);
                        $j = 0;
                        while ($j < count($t)) {
                            $itm = explode('=', $t[$j]);
                            $ch = $u->testAction('`vars` = "add_eff_' . $this->info['id'] . '_' . $obj['id'] . '" AND `uid` = "' . $u->info['id'] . '" LIMIT ' . (1 + (int)$itm[2]) . '', 2); //êîë-âî ïðîøëûõ ïîïûòîê
                            $ch = $ch[0];
                            $ch2 = $u->testAction(' `vars` = "add_eff_' . $this->info['id'] . '_' . $obj['id'] . '" LIMIT ' . (1 + (int)$itm[4]) . '', 2); //êîë-âî ïðîøëûõ ïîïûòîê (âñå þçåðû)
                            $ch2 = $ch2[0];
                            if (($ch2 < $itm[4] || $itm[4] == 0) && $ch < $itm[2]) {
                                if ($itm[1] * 100 >= rand(0, 10000)) {
                                    //óäà÷íî
                                    $eff_d = mysql_fetch_assoc(mysql_query('SELECT `id2`,`mname` FROM `eff_main` WHERE `id2` = "' . $itm[0] . '" LIMIT 1'));
                                    if (isset($eff_d['id2'])) {
                                        //äîáàâëÿåì ýôôåêò
                                        $us = $magic->add_eff($u->info['id'], $itm[0], 1);
                                        if ($us[0] == 1) {
                                            $r .= '<div>Íà Âàñ íàëîæèëè çàêëÿòèå &quot;' . $eff_d['mname'] . '&quot;.</div>';
                                        } else {
                                            $r .= '<div>×òî-òî ïîøëî íå òàê... Îùóùàåòñÿ ÷üå-òî ïðèñóòñòâèå...</div>';
                                        }
                                    } else {
                                        $r .= '<div>×òî-òî ïîøëî íå òàê... Ñëûøåí ÷åé-òî âîé...</div>';
                                    }
                                    unset($eff_d, $us);
                                } else {
                                    //íå óäà÷íî
                                    $r .= '<div>Íå óäàëîñü...</div>';
                                }
                                $u->addAction(time(), 'add_eff_' . $this->info['id'] . '_' . $obj['id'], $u->info['city']);
                            } else {
                                //óæå íåëüçÿ þçàòü
                                $r .= '<div>×òî-òî ïîøëî íå òàê...</div>';
                            }

                            unset($ch, $ch2);
                            $j++;
                        }
                    } elseif ($s[0] == 'mfast') {
                        //Äîáàâëÿåì áàô //Ëîâóøêè è áàôû
                        $j = 0;
                        $t = explode(',', $s[1]);
                        while ($j < count($t)) {
                            $itm = explode('=', $t[$j]);
                            $ch = $u->testAction('`vars` = "bafit_' . $this->info['id'] . '_' . $obj['id'] . '" LIMIT ' . (1 + (int)$itm[2]) . '', 2); //êîë-âî ïðîøëûõ ïîïûòîê
                            $ch = $ch[0];
                            if ($ch < $itm[3]) {
                                if ($itm[2] * 1000 >= rand(1, 100000)) {
                                    if ($itm[0] == 'hpNow') {
                                        $mm = explode('r', $itm[1]);
                                        if ($mm[1] != 0) {
                                            $itm[1] = rand($mm[0], $mm[1]);
                                        }
                                        if ($itm[1] < 0) {
                                            $r .= '<div>Âû ïîïàëè â ëîâóøêó... Çäîðîâüå: <b>' . $itm[1] . ' HP</b></div>';
                                        } elseif ($itm[1] > 0) {
                                            $r .= '<div>Âû ïðèêîñíóëèñü ê ìàãèè... Çäîðîâüå: <b>+' . $itm[1] . ' HP</b></div>';
                                        }
                                        $u->info['hpNow'] += $itm[1];
                                        if ($u->info['hpNow'] < 1) {
                                            $r .= '<div>Âû ïåðåìåùåíû â òî÷êó âîçðîæäåíèÿ...</div>';
                                            mysql_query('UPDATE `stats` SET `hpNow` = "' . $u->info['hpNow'] . '",`x` = "' . $u->info['res_x'] . '",`y` = "' . $u->info['res_y'] . '",`s` = "' . $u->info['res_s'] . '" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
                                        } else {
                                            mysql_query('UPDATE `stats` SET `hpNow` = "' . $u->info['hpNow'] . '" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
                                        }
                                    }
                                }
                                $u->addAction(time(), 'bafit_' . $this->info['id'] . '_' . $obj['id'], $u->info['city']);
                            }
                            //çàïèñûâàåì ïîïûòêó
                            $j++;
                        }
                    } elseif ($s[0] == 'save_pos') {
                        if (isset($act_sl['port'])) {
                            $itm = explode('=', $act_sl['port']);
                            $obj['x'] = $itm[0];
                            $obj['y'] = $itm[1];
                        }
                        #$r .= 'Ïîçèöèÿ ñîõðàíåíà. Òåïåðü ïîñëå ñìåðòè âû îæèâåòå çäåñü.';
                        mysql_query('UPDATE `stats` SET `res_x` = "' . $obj['x'] . '",`res_y` = "' . $obj['y'] . '",`res_s` = "' . $u->info['s'] . '" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
                    } elseif ($s[0] == 'look_text') {
                        $itm = explode('=', $s[1]);
                        $r .= $itm[rand(0, count($itm) - 1)];
                    } elseif ($s[0] == 'save_pos_xy') {
                        $itm = explode('=', $s[1]);
                        $u->info['res_x'] = $itm[0];
                        $u->info['res_y'] = $itm[1];
                        $upd = mysql_query('UPDATE `stats` SET `res_x` = "' . $u->info['x'] . '",`res_y` = "' . $u->info['y'] . '" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
                        if ($upd) {
                            $r .= 'Âû êóäà-òî ïåðåìåñòèëèñü... íà ýòîò ðàç óäà÷íî...<br>';
                        } else {
                            $r .= '×òî-òî çäåñü íå òàê ...';
                        }
                    } elseif ($s[0] == 'port') {
                        //òåëåïîðòèðóåò ïîëüçîâàòåëÿ
                        $itm = explode('=', $s[1]);
                        $u->info['x'] = $itm[0];
                        $u->info['y'] = $itm[1];
                        $upd = mysql_query('UPDATE `stats` SET						
						`x` = "' . $u->info['x'] . '",`y` = "' . $u->info['y'] . '"				
						WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
                        if ($upd) {
                            $r .= 'Âû êóäà-òî ïåðåìåñòèëèñü... íà ýòîò ðàç óäà÷íî...<br>';
                        } else {
                            $r .= '×òî-òî çäåñü íå òàê ...';
                        }
                    } elseif ($s[0] == 'save_port') {
                        $itm = explode('=', $s[1]);
                        $u->info['res_x'] = $itm[0];
                        $u->info['res_y'] = $itm[1];
                        $upd = mysql_query('UPDATE `stats` SET `res_x` = "' . $u->info['res_x'] . '", `res_y` = "' . $u->info['res_y'] . '" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
                    } elseif ($s[0] == 'itm') {
                        //Äîáàâëÿåì ïðåäìåò
                        $j = 0;
                        $t = explode(',', $s[1]);
                        while ($j < count($t)) {
                            $itm = explode('=', $t[$j]);
                            $ch = $u->testAction('`uid` = "' . $u->info['id'] . '" AND `vars` = "takeit_' . $this->info['id'] . '_' . $obj['id'] . '" LIMIT ' . (1 + (int)$itm[2]) . '', 2); //êîë-âî ïðîøëûõ ïîïûòîê
                            $ch = $ch[0];
                            if ($ch >= $itm[2]) {
                                //çàêîí÷èëèñü ïîïûòêè
                                $r = 'íè÷åãî íå ïðîèçîøëî...<br>';
                            } else {
                                if ($itm[1] * 1000 >= rand(1, 100000)) {

                                    //Ñëó÷àéíûé ïðåäìåò (Áàøíÿ ñìåðòè)
                                    if ($itm[0] == 'random1') {
                                        $itm[0] = $this->itbs[rand(0, count($this->itbs))];
                                    }

                                    //óäà÷íàÿ ïîïûòêà
                                    $it = mysql_fetch_assoc(mysql_query('SELECT * FROM `items_main` WHERE `id`="' . ((int)$itm[0]) . '" LIMIT 1'));
                                    if (isset($it['id'])) {
                                        $r .= 'Âû îáíàðóæèëè ïðåäìåò &quot;<b>' . $it['name'] . '</b>&quot;.<br>';
                                        $this->addItem(['uid' => $u->info['id'], 'iid' => $it['id'], 'time' => time(), 'x' => $u->info['x'], 'y' => $u->info['y'], 'bid' => 0, 'del' => (int)$itm[4]]);
                                    }
                                } else {
                                    //íåóäà÷íàÿ ïîïûòêà
                                    $r .= 'Âû íè÷åãî íå íàøëè ...<br>';
                                }
                                $u->addAction(time(), 'takeit_' . $this->info['id'] . '_' . $obj['id'], $u->info['city']);
                            }
                            //çàïèñûâàåì ïîïûòêó
                            $j++;
                        }
                    } elseif ($s[0] == 'itm1') {
                        //Äîáàâëÿåì ïðåäìåò , òîëüêî 1 ïðåäìåò èç âñåõ è âñå þçåðû ìîãóò òîæå
                        $nj = 0;
                        $t = explode(',', $s[1]);
                        $j = rand(0, count($t));
                        if ($nj == 0) {
                            $itm = explode('=', $t[$j]);
                            $ch = $u->testAction('`uid` = "' . $u->info['id'] . '" AND `vars` = "takeit_' . $this->info['id'] . '_' . $obj['id'] . '" LIMIT ' . (1 + (int)$itm[2]) . '', 2); //êîë-âî ïðîøëûõ ïîïûòîê
                            $ch = $ch[0];
                            if ($ch >= $itm[2]) {
                                //çàêîí÷èëèñü ïîïûòêè
                                $r .= 'Ñòðàííî, íî íè÷åãî íå ïðîèçîøëî... <br>';
                            } else {
                                if ($itm[1] * 1000 >= rand(1, 100000)) {

                                    //Ñëó÷àéíûé ïðåäìåò (Áàøíÿ ñìåðòè)
                                    if ($itm[0] == 'random1') {
                                        $itm[0] = $this->itbs[rand(0, count($this->itbs))];
                                    }

                                    //óäà÷íàÿ ïîïûòêà
                                    $it = mysql_fetch_assoc(mysql_query('SELECT * FROM `items_main` WHERE `id`="' . ((int)$itm[0]) . '" LIMIT 1'));
                                    if (isset($it['id'])) {
                                        $r .= 'Âû îáíàðóæèëè ïðåäìåò &quot;<b>' . $it['name'] . '</b>&quot;.<br>';
                                        $u->addAction(time(), 'takeit_' . $this->info['id'] . '_' . $obj['id'], $u->info['city']);
                                        $this->addItem(['uid' => $u->info['id'], 'iid' => $it['id'], 'time' => time(), 'x' => $u->info['x'], 'y' => $u->info['y'], 'bid' => 0, 'del' => (int)$itm[4]]);
                                        $nj++;
                                    }
                                } else {
                                    //íåóäà÷íàÿ ïîïûòêà
                                    $u->addAction(time(), 'takeit_' . $this->info['id'] . '_' . $obj['id'], $u->info['city']);
                                    $r .= 'Âû íè÷åãî íå íàøëè ... <br>';
                                }
                            }
                        }
                    } elseif ($s[0] == 'itm2') {
                        //Äîáàâëÿåì ïðåäìåò , òîëüêî 1 ïðåäìåò èç âñåõ è òîëüêî 1 þçåð ìîæåò ïîäíÿòü
                        $nj = 0;
                        $t = explode(',', $s[1]);
                        $j = rand(0, count($t) - 1);
                        if ($nj == 0) {
                            $itm = explode('=', $t[$j]);
                            $ch = $u->testAction('`vars` = "takeit_' . $this->info['id'] . '_' . $obj['id'] . '" LIMIT ' . (1 + (int)$itm[2]) . '', 2); //êîë-âî ïðîøëûõ ïîïûòîê
                            $ch = $ch[0];
                            if ($ch >= $itm[2]) {
                                //çàêîí÷èëèñü ïîïûòêè
                                $r .= 'Íè÷åãî íå ïðîèçîøëî... <br>';
                            } else {
                                if ($itm[1] * 1000 >= rand(1, 100000)) {

                                    //Ñëó÷àéíûé ïðåäìåò (Áàøíÿ ñìåðòè)
                                    if ($itm[0] == 'random1') {
                                        $itm[0] = $this->itbs[rand(0, count($this->itbs))];
                                    }

                                    //óäà÷íàÿ ïîïûòêà
                                    $it = mysql_fetch_assoc(mysql_query('SELECT * FROM `items_main` WHERE `id`="' . ((int)$itm[0]) . '" LIMIT 1'));
                                    if (isset($it['id'])) {
                                        $r .= 'Âû îáíàðóæèëè ïðåäìåò &quot;<b>' . $it['name'] . '</b>&quot;.<br>';
                                        $u->addAction(time(), 'takeit_' . $this->info['id'] . '_' . $obj['id'], $u->info['city']);
                                        $this->addItem(['uid' => $u->info['id'], 'iid' => $it['id'], 'time' => time(), 'x' => $u->info['x'], 'y' => $u->info['y'], 'bid' => 0, 'del' => (int)$itm[4]]);
                                        $nj++;
                                    }
                                } else {
                                    //íåóäà÷íàÿ ïîïûòêà
                                    $u->addAction(time(), 'takeit_' . $this->info['id'] . '_' . $obj['id'], $u->info['city']);
                                    $r .= 'Âû íè÷åãî íå íàøëè ... <br>';
                                }
                            }
                        }
                    } elseif ($s[0] == 'fileact') {
                        require('dnaction/' . $s[1] . '.php');
                    }
                    $i++;
                }
                $r = rtrim($r, '\<br\>');
                if ($r == '') {
                    $r = 'Â ýòîò ðàç íå óäàëîñü íàéòè ÷òî-ëèáî åùå';
                }
                $this->error = $r;
            }
        } else {
            $this->error = 'Ïðåäìåò íå íàéäåí ...';
        }
    }

    public function addItem($i)
    {
        //äîáàâëÿåì ïðåäìåò â ïåùåðó (âîçìîæíî âûïàë èç áîòà èëè èç ñóíäóêà)
        return mysql_query('INSERT INTO `dungeon_items` (`dn`,`user`,`item_id`,`time`,`x`,`y`,`bot`,`del`) VALUES ("' . $this->info['id'] . '","' . $i['uid'] . '","' . $i['iid'] . '","' . $i['time'] . '","' . $i['x'] . '","' . $i['y'] . '","' . $i['bid'] . '","' . ((int)$i['del']) . '")');
    }

    public function takeinv($id)
    {
        global $u, $c, $code;
        $obj = mysql_fetch_assoc(mysql_query('SELECT * FROM `dungeon_items` WHERE `id` = "' . $id . '" AND `for_dn` = "0" AND `dn` = "' . $this->info['id'] . '" LIMIT 1'));
        if (isset($obj['id'])) {
            $this->test_luck($id);
            $fxv = ['luck_count' => mysql_fetch_assoc(mysql_query('SELECT COUNT(*) FROM `dungeon_actions` WHERE `dn` = "' . $this->info['id'] . '" AND `vars` = "luck_itm' . mysql_real_escape_string($id) . '" LIMIT 1')), 'user_count' => mysql_fetch_assoc(mysql_query('SELECT COUNT(*) FROM `stats` WHERE `dnow` = "' . $this->info['id'] . '" LIMIT 1'))];
            if ($obj['user'] == 0 && $fxv['user_count'][0] > $fxv['luck_count'][0] && $fxv['user_count'][0] > 1 && $this->info['bsid'] == 0) {
                $this->error = 'Âû íå ìîæåòå ñåé÷àñ ïîäíÿòü ýòîò ïðåäìåò, äîæäèòåñü çàâåðøåíèÿ ðîçûãðûøà. Îñòàëîñü ' . $u->timeOut($obj['time'] - time() + 300);
            } elseif ($u->info['x'] != $obj['x'] || $obj['y'] != $u->info['y']) {
                $this->error = 'Ïðåäìåò íå íàéäåí ...';
            } else {
                if ($obj['take'] > 0) {
                    $this->error = 'Êòî-òî îïåðåäèë âàñ ...';
                } else {
                    if ($obj['user'] > 0 && $obj['user'] != $u->info['id'] && $obj['time'] > time() - 300) {
                        $uo = mysql_fetch_assoc(mysql_query('SELECT `id`,`login`,`sex` FROM `users` WHERE `id` = "' . $obj['user'] . '" LIMIT 1'));
                    }
                    if (isset($uo['id'])) {
                        $this->error = 'Ïðåäìåò ïðåäíàçíà÷åí äëÿ &quot;' . $uo['login'] . '&quot;. Âû ñìîæåòå çàáðàòü ýòîò ïðåäìåò, åñëè ';
                        if ($uo['sex'] == 1) {
                            $this->error .= 'îíà ';
                        } else {
                            $this->error .= 'îí ';
                        }
                        $this->error .= ' íå ïîäíèìåò åãî â òå÷åíèè ' . ceil(5 - (time() - $obj['time']) / 60) . ' ìèí.';
                        unset($uo);
                    } else {
                        $upd = mysql_query('UPDATE `dungeon_items` SET `take` = "' . $u->info['id'] . '" WHERE `id` = "' . $obj['id'] . '" LIMIT 1');
                        if ($upd) {
                            $it = mysql_fetch_assoc(mysql_query('SELECT * FROM `items_main` WHERE `id` = "' . $obj['item_id'] . '" LIMIT 1'));
                            if (isset($it['id'])) {
                                $data = '|noremont=1|frompisher=' . $this->info['id2'];
                                if ($it['ts'] != 0) {
                                    $data .= '|sudba=' . $u->info['login'];
                                }
                                if ($obj['data'] != '') {
                                    $data .= $obj['data'];
                                }
                                $data = str_replace('|sudba=-1', '|sudba=' . $u->info['login'] . '', $data);

                                $idl = $u->addItem($obj['item_id'], $u->info['id'], $data, $obj);
                                if ($obj['del'] > 0) {
                                    mysql_query('UPDATE `items_users` SET `dn_delete` = 1 WHERE `id` = "' . $idl . '" LIMIT 1');
                                }
                                unset($idl);

                                if ($this->info['bsid'] == 0) {
                                    $rtxt = '<b>' . $u->info['login'] . '</b> ïîäíÿë ïðåäìåò &quot;' . $it['name'] . '&quot;';
                                    if ($obj['quest'] > 0) {
                                        $rtxt .= ' (Êâåñò)';
                                    }
                                    mysql_query("INSERT INTO `chat` (`dn`,`city`,`room`,`login`,`to`,`text`,`time`,`type`,`toChat`,`typeTime`,`new`) VALUES ('" . $u->info['dnow'] . "','" . $u->info['city'] . "','" . $u->info['room'] . "','','','" . $rtxt . "','" . time() . "','6','0','1','1')");
                                }

                                $this->error = 'Âû ïîäíÿëè &quot;' . $it['name'] . '&quot;';
                            } else {
                                $this->error = 'Íå óäàëîñü íàéòè ïðåäìåò ...';
                            }
                        } else {
                            $this->error = 'Íå óäàëîñü äîáàâèòü ïðåäìåò â èíâåíòàðü ...';
                        }
                    }
                }
            }
        }
    }


    //Ðîçûãðûø ïðåäìåòà
    public function test_luck($id)
    {
        global $u;
        $fxv = ['itm' => mysql_fetch_assoc(mysql_query('SELECT `im`.*,`ish`.* FROM `dungeon_items` AS `ish` LEFT JOIN `items_main` AS `im` ON (`im`.`id` = `ish`.`item_id`) WHERE `ish`.`dn` = "' . $this->info['id'] . '" AND `ish`.`id` = "' . mysql_real_escape_string($id) . '" AND `ish`.`take` = "0" AND `ish`.`delete` = "0" AND `ish`.`x` = "' . $u->info['x'] . '" AND `ish`.`y` = "' . $u->info['y'] . '" LIMIT 1')), 'luck_count' => mysql_fetch_assoc(mysql_query('SELECT COUNT(*) FROM `dungeon_actions` WHERE `dn` = "' . $this->info['id'] . '" AND `vars` = "luck_itm' . mysql_real_escape_string($id) . '" LIMIT 1')), 'user_count' => mysql_fetch_assoc(mysql_query('SELECT COUNT(*) FROM `stats` WHERE `dnow` = "' . $this->info['id'] . '" LIMIT 1'))];
        $fxv['luck_count'] = $fxv['luck_count'][0];
        $fxv['user_count'] = $fxv['user_count'][0];

        if (($fxv['luck_count'] >= $fxv['user_count'] || $fxv['itm']['time'] + 300 < time()) && !empty($fxv['itm']['user'])) {
            $fxv['sp'] = mysql_query('SELECT * FROM `dungeon_actions` WHERE `dn` = "' . $this->info['id'] . '" AND `vars` = "luck_itm' . mysql_real_escape_string($id) . '" ORDER BY `vals` DESC LIMIT ' . $fxv['luck_count'] . '');
            $fxv['winner'] = [];
            $fxv['win_val'] = 0;
            unset($fxv['pl'], $fxv['sp']);
            if (count($fxv['winner']) > 1) {
                //Ðîçûãðûø åùå ðàç ìåæäó ïîáåäèòåëÿìè
                $this->error .= '<div>Ðîçûãðûø çàâåðøåí!</div>';
            } elseif (count($fxv['winner']) == 1) {
                $fxv['user_win'] = mysql_fetch_assoc(mysql_query('SELECT `id`,`login`,`sex` FROM `users` WHERE `id` = "' . $fxv['winner'][0] . '" LIMIT 1'));
                $fxv['text'] = '<b>' . $fxv['user_win']['login'] . '</b> âûèãðûâàåò â ñïîðå çà ïðåäìåò &quot;' . $fxv['itm']['name'] . '&quot;';
                $this->sys_chat($fxv['text']);
                mysql_query('UPDATE `dungeon_items` SET `time` = "' . time() . '",`user` = "' . $fxv['user_win']['id'] . '" WHERE `id` = "' . $fxv['itm']['id'] . '" LIMIT 1');
                $this->error .= '<div>Ðîçûãðûø çàâåðøåí! Ïîáåäèòåëü <b>' . $fxv['user_win']['login'] . '</b>!</div>';
            }
        } else {
            $this->error .= '<div>Ó îñòàëüíûõ ó÷àñòíèêîâ îñòàëîñü ' . $u->timeOut($fxv['itm']['time'] + 300 - time()) . ' ìèí. äî êîíöà ðîçûãðûøà</div>';
        }
        unset($fxv);
    }

    public function itm_luck($id)
    {
        global $u;
        $fxv = ['itm' => mysql_fetch_assoc(mysql_query('SELECT `im`.*,`ish`.* FROM `dungeon_items` AS `ish` LEFT JOIN `items_main` AS `im` ON (`im`.`id` = `ish`.`item_id`) WHERE `ish`.`dn` = "' . $this->info['id'] . '" AND `ish`.`id` = "' . mysql_real_escape_string($id) . '" AND `ish`.`take` = "0" AND `ish`.`delete` = "0" AND `ish`.`x` = "' . $u->info['x'] . '" AND `ish`.`y` = "' . $u->info['y'] . '" LIMIT 1')), 'luck' => mysql_fetch_assoc(mysql_query('SELECT * FROM `dungeon_actions` WHERE `uid` = "' . $u->info['id'] . '" AND `dn` = "' . $this->info['id'] . '" AND `vars` = "luck_itm' . mysql_real_escape_string($id) . '" LIMIT 1')), 'luck2' => mysql_fetch_assoc(mysql_query('SELECT COUNT(*) FROM `dungeon_actions` WHERE `uid` = "' . $u->info['id'] . '" AND `dn` = "' . $this->info['id'] . '" AND `vars` = "luck_itm' . mysql_real_escape_string($id) . '" LIMIT 1'))];
        if ($u->stats['silver'] >= 3) { //äîïîëíèòåëüíûé áðîñîê
            $fxv['luck2'][0]--;
        }
        if ($fxv['itm']['user'] > 0) {
            $this->error = 'Ðîçûãðûø ïðåäìåò óæå çàâåðøèëñÿ...';
        } elseif (!isset($fxv['itm']['id'])) {
            $this->error .= '<div>Ïðåäìåò íå íàéäåí</div>';
        } elseif ($fxv['luck2'][0] > 0) {
            $this->error .= '<div>Âû óæå ó÷àâñòâóåòå â ðîçûãðûøå &quot;' . $fxv['itm']['name'] . '&quot;, îæèäàåì äðóãèõ ó÷àñòíèêîâ åùå ' . $u->timeOut($fxv['itm']['time'] - time() + 300) . '</div>';
        } else {
            $luck_users = mysql_fetch_assoc(mysql_query('SELECT COUNT(`id`) FROM `dungeon_actions` WHERE `uid` = "' . $u->info['id'] . '" AND `dn` = "' . $this->info['id'] . '" AND `vars` = "luck_itm' . mysql_real_escape_string($id) . '" LIMIT 1'));
            $luck_users = $luck_users[0];
            if ($luck_users < 1) {
                mysql_query('UPDATE `dungeon_items` SET `time` = "' . time() . '" WHERE `id` = "' . $fxv['itm']['id'] . '" LIMIT 1');
            }
            $rndl = rand(1, 100);
            mysql_query('INSERT INTO `dungeon_actions` (`uid`,`dn`,`x`,`y`,`time`,`vars`,`vals`) VALUES (
				"' . $u->info['id'] . '","' . $u->info['dnow'] . '","' . $u->info['x'] . '","' . $u->info['y'] . '","' . time() . '",
				"luck_itm' . mysql_real_escape_string($id) . '","' . $rndl . '"
			)');
            if ($u->info['sex'] == 0) {
                $fxv['text'] = '<b>' . $u->info['login'] . '</b> âûáðîñèë *' . $rndl . '* â ñïîðå çà ïðåäìåò &quot;' . $fxv['itm']['name'] . '&quot;';
            } else {
                $fxv['text'] = '<b>' . $u->info['login'] . '</b> âûáðîñèëà *' . $rndl . '* â ñïîðå çà ïðåäìåò &quot;' . $fxv['itm']['name'] . '&quot;';
            }
            $this->sys_chat($fxv['text']);
            $this->error .= '<div>Âû âûáðîñèëè <b>' . $rndl . '</b> â ñïîðå çà &quot;' . $fxv['itm']['name'] . '&quot;</div>';
        }
        $this->test_luck($id);
        unset($fxv);
    }

    public function itm_unluck($id)
    {
        global $u;
        $fxv = ['itm' => mysql_fetch_assoc(mysql_query('SELECT `im`.*,`ish`.* FROM `dungeon_items` AS `ish` LEFT JOIN `items_main` AS `im` ON (`im`.`id` = `ish`.`item_id`) WHERE `ish`.`dn` = "' . $this->info['id'] . '" AND `ish`.`id` = "' . mysql_real_escape_string($id) . '" AND `ish`.`take` = "0" AND `ish`.`delete` = "0" AND `ish`.`x` = "' . $u->info['x'] . '" AND `ish`.`y` = "' . $u->info['y'] . '" LIMIT 1')), 'luck' => mysql_fetch_assoc(mysql_query('SELECT * FROM `dungeon_actions` WHERE `uid` = "' . $u->info['id'] . '" AND `dn` = "' . $this->info['id'] . '" AND `vars` = "luck_itm' . mysql_real_escape_string($id) . '" LIMIT 1'))];
        if ($fxv['itm']['user'] > 0) {
            $this->error = 'Ðîçûãðûø ïðåäìåò óæå çàâåðøèëñÿ...';
        } elseif (!isset($fxv['itm']['id'])) {
            $this->error .= '<div>Ïðåäìåò íå íàéäåí</div>';
        } elseif (isset($fxv['luck']['id'])) {
            if ($fxv['luck']['vals'] == 0) {
                $this->error .= '<div>Âû óæå îòêàçàëèñü îò ó÷àñòèÿ â ðîçûãðûøå &quot;' . $fxv['itm']['name'] . '&quot;</div>';
            } else {
                $this->error .= '<div>Âû óæå ó÷àâñòâóåòå â ðîçûãðûøå &quot;' . $fxv['itm']['name'] . '&quot;, îæèäàåì äðóãèõ ó÷àñòíèêîâ åùå ' . $u->timeOut($fxv['itm']['time'] - time() + 300) . '</div>';
            }
        } else {
            $luck_users = mysql_fetch_assoc(mysql_query('SELECT COUNT(`id`) FROM `dungeon_actions` WHERE `uid` = "' . $u->info['id'] . '" AND `dn` = "' . $this->info['id'] . '" AND `vars` = "luck_itm' . mysql_real_escape_string($id) . '" LIMIT 1'));
            $luck_users = $luck_users[0];
            if ($luck_users < 1) {
                mysql_query('UPDATE `dungeon_items` SET `time` = "' . time() . '" WHERE `id` = "' . $fxv['itm']['id'] . '" LIMIT 1');
            }
            if ($u->info['sex'] == 0) {
                $fxv['text'] = '<b>' . $u->info['login'] . '</b> îòêàçàëñÿ îò ñïîðà çà ïðåäìåò &quot;' . $fxv['itm']['name'] . '&quot;';
            } else {
                $fxv['text'] = '<b>' . $u->info['login'] . '</b> îòêàçàëàñü îò ñïîðà çà ïðåäìåò &quot;' . $fxv['itm']['name'] . '&quot;';
            }
            $this->sys_chat($fxv['text']);
            mysql_query('INSERT INTO `dungeon_actions` (`uid`,`dn`,`x`,`y`,`time`,`vars`,`vals`) VALUES (
				"' . $u->info['id'] . '","' . $u->info['dnow'] . '","' . $u->info['x'] . '","' . $u->info['y'] . '","' . time() . '",
				"luck_itm' . mysql_real_escape_string($id) . '","0"
			)');
            $this->error .= '<div>Âû îòêàçàëèñü îò ó÷àñòèÿ â ðîçûãðûøå &quot;' . $fxv['itm']['name'] . '&quot;</div>';
        }
        unset($fxv);
    }

    public function itemsMap()
    {
        global $u, $c, $code;
        $r = '';
        $live_users = mysql_fetch_assoc(mysql_query('SELECT COUNT(`id`) FROM `stats` WHERE `dnow` = "' . $this->info['id'] . '" LIMIT 1'));
        $live_users = $live_users[0];
        $sp = mysql_query('SELECT `im`.*,`ish`.* FROM `dungeon_items` AS `ish` LEFT JOIN `items_main` AS `im` ON (`im`.`id` = `ish`.`item_id`) WHERE `ish`.`dn` = "' . $this->info['id'] . '" AND `ish`.`take` = "0" AND `ish`.`delete` = "0" AND ( `ish`.`onlyfor` = "0" OR `ish`.`onlyfor` = "' . $u->info['id'] . '" ) AND `ish`.`x` = "' . $u->info['x'] . '" AND `ish`.`y` = "' . $u->info['y'] . '" LIMIT 100');
        while ($pl = mysql_fetch_assoc($sp)) {
            $action = 'main.php?take=' . $pl['id'] . '';
            $tk = 0;
            $luck_users = mysql_fetch_assoc(mysql_query('SELECT COUNT(`id`) FROM `dungeon_actions` WHERE `uid` = "' . $u->info['id'] . '" AND `dn` = "' . $this->info['id'] . '" AND `vars` = "luck_itm' . mysql_real_escape_string($pl['id']) . '" LIMIT 1'));
            $luck_users = $luck_users[0];
            if ($pl['user'] == 0 && $live_users > 1 && ($pl['time'] + 300 > time() || $luck_users < 1)) {
                $fxv = mysql_fetch_assoc(mysql_query('SELECT * FROM `dungeon_actions` WHERE `uid` = "' . $u->info['id'] . '" AND `dn` = "' . $this->info['id'] . '" AND `vars` = "luck_itm' . $pl['id'] . '" LIMIT 1'));
                if (!isset($fxv['id'])) {
                    //Ðîçûãðûø
                    if ($this->info['bsid'] == 0) {
                        $action = 'javascript: top.fartgame(\'' . $pl['id'] . '\',\'' . $pl['img'] . '\',\'' . $pl['name'] . '\',1,\'\');';
                        $tk = 1;
                    }
                }
            }
            if ($tk == 0) {
                $r .= '<a onclick="takeItem(' . $pl['id'] . ')"><img style="padding: 5px; cursor: pointer;" title="Âçÿòü &quot;' . $pl['name'] . '&quot;" src="https://img.new-combats.com/i/items/' . $pl['img'] . '" /></a>';
            } elseif ($tk == 1) {
                $r .= '<a href="' . $action . '"><img style="padding: 5px; cursor: pointer;" title="Âçÿòü &quot;' . $pl['name'] . '&quot;" src="https://img.new-combats.com/i/items/' . $pl['img'] . '" /></a>';
            }
        }
        if ($r != '') {
            $r = '<H4>Â êîìíàòå ðàçáðîñàíû âåùè:</H4>' . $r; //<br> <a onclick="takeItemAll">Ïîäîáðàòü âñå</a><br>
        }
        return $r;
    }

    public function testLike($x1, $y1, $x2, $y2)
    {
        //èç $x1,$y1 â $x2,$y2
        //äîñòóïíà-ëè ýòà êëåòêà äëÿ äåéñòâèé
        $r = 0;
        $c1 = mysql_fetch_assoc(mysql_query('SELECT * FROM `dungeon_map` WHERE `x` = "' . $x1 . '" AND `y` = "' . $y1 . '" AND `id_dng` = "' . $this->info['id2'] . '" LIMIT 1'));
        $c2 = mysql_fetch_assoc(mysql_query('SELECT * FROM `dungeon_map` WHERE `x` = "' . $x2 . '" AND `y` = "' . $y2 . '" AND `id_dng` = "' . $this->info['id2'] . '" LIMIT 1'));
        if (isset($c1['id']) && isset($c2['id'])) {
            if ($x1 == $x2 && $y1 == $y2) {
                $r = 1;
            } elseif ($x1 == $x2 - 1 && $c1['go_1'] == 1) //ïðàâî
            {
                $r = 1;
            } elseif ($x1 == $x2 + 1 && $c1['go_2'] == 1) //ëåâî
            {
                $r = 1;
            } elseif ($y1 == $y2 - 1 && $c1['go_3'] == 1) //âåðõ
            {
                $r = 1;
            } elseif ($y1 == $y2 + 1 && $c1['go_4'] == 1) //íèç
            {
                $r = 1;
            }
        }
        return $r;
    }

    public function genObjects()
    {
        global $u, $c, $code;
        ////i:{id,name,mapPoint,action,img,type},
        //'count':1,0:{0:1234,1:'Ñóíäóê',2:5,3:'',4:'test.gif',5:0,6:position,7:width,8:heigh,9:left,10:top},
        //psition 0 - ïî öåíòðó , 1- ñâåðõó, 2- ñëåâà, 3- ñíèçó, 4- ñïðàâà
        $r = '';
        $whr = [1 => ' (((`u`.`x` <= ' . ($u->info['x'] + 2) . ' && `u`.`x` >= ' . ($u->info['x'] - 2) . ') && (`u`.`y` >= ' . ($u->info['y'] + 1) . ' && `u`.`y` <= ' . ($u->info['y'] + 4) . ')) OR (`u`.`y` = ' . $u->info['y'] . ' && `u`.`x` = ' . $u->info['x'] . ')) ', //ïðÿìî
            3 => ' (((`u`.`x` <= ' . ($u->info['x'] + 2) . ' && `u`.`x` >= ' . ($u->info['x'] - 2) . ') && (`u`.`y` <= ' . ($u->info['y'] - 1) . ' && `u`.`y` >= ' . ($u->info['y'] - 4) . ')) OR (`u`.`y` = ' . $u->info['y'] . ' && `u`.`x` = ' . $u->info['x'] . ')) ', //âíèç
            2 => ' (((`u`.`x` <= ' . ($u->info['x'] - 1) . ' && `u`.`x` >= ' . ($u->info['x'] - 4) . ') && (`u`.`y` <= ' . ($u->info['y'] + 2) . ' && `u`.`y` >= ' . ($u->info['y'] - 2) . '))OR (`u`.`y` = ' . $u->info['y'] . ' && `u`.`x` = ' . $u->info['x'] . ')) ', //ëåâî
            4 => ' (((`u`.`x` >= ' . ($u->info['x'] + 1) . ' && `u`.`x` <= ' . ($u->info['x'] + 4) . ') && (`u`.`y` <= ' . ($u->info['y'] + 2) . ' && `u`.`y` >= ' . ($u->info['y'] - 2) . ')) OR (`u`.`y` = ' . $u->info['y'] . ' && `u`.`x` = ' . $u->info['x'] . ')) ' //ïðàâî
        ];
        $sp = mysql_query('SELECT `u`.* FROM `dungeon_obj` AS `u` WHERE `u`.`dn` = "' . $u->info['dnow'] . '" AND `u`.`for_dn` = "0" AND ((`u`.`s` = "0" OR `u`.`s` = "' . $this->gs . '") OR `u`.`s2` = "' . $this->gs . '") AND ' . $whr[$this->gs] . ' LIMIT 150');
        $i = 0;
        $pos = [];
        while ($pl = mysql_fetch_assoc($sp)) {
            if ($pl['fix_x_y'] == 0 || ($pl['fix_x_y'] == 1 && $pl['x'] == $u->info['x']) || ($pl['fix_x_y'] == 2 && $pl['y'] == $u->info['y']) || ($pl['fix_x_y'] == 3 && $pl['x'] == $u->info['x'] && $pl['y'] == $u->info['y'])) {
                if (($pl['os1'] == 0 && $pl['os2'] == 0 && $pl['os3'] == 0 && $pl['os4'] == 0) || ($this->cord[$pl['y'] . '_' . $pl['x']] == $pl['os1'] || $this->cord[$pl['y'] . '_' . $pl['x']] == $pl['os2'] || $this->cord[$pl['y'] . '_' . $pl['x']] == $pl['os3'] || $this->cord[$pl['y'] . '_' . $pl['x']] == $pl['os4'])) {
                    $i++;
                    if (!isset($pos[$this->cord[$pl['y'] . '_' . $pl['x']]])) {
                        $pos[$this->cord[$pl['y'] . '_' . $pl['x']]] = 0;
                    }
                    $pos[$this->cord[$pl['y'] . '_' . $pl['x']]]++;
                    $r .= ',' . ($i - 1) . ':{0:' . $pl['id'] . ',1:\'' . $pl['name'] . '\',2:' . (0 + $this->cord[$pl['y'] . '_' . $pl['x']]) . ',3:\'action\',4:\'' . $pl['img'] . '\',5:' . $pl['type'] . ',6:0,7:' . $pl['w'] . ',8:' . $pl['h'] . ',9:' . $pl['left'] . ',10:' . $pl['top'] . ',11:' . $pl['date'] . '}';
                } elseif ($this->cord[$pl['y'] . '_' . $pl['x']] == $pl['os1'] - 1 || $this->cord[$pl['y'] . '_' . $pl['x']] == $pl['os2'] - 1 || $this->cord[$pl['y'] . '_' . $pl['x']] == $pl['os3'] - 1 || $this->cord[$pl['y'] . '_' . $pl['x']] == $pl['os4'] - 1) {
                    $dt2 = explode(',', ltrim(rtrim($pl['date'], '\}'), '\{'));
                    $da = [];
                    $is = 0;
                    while ($is < count($dt2)) {
                        $dt2[$is] = explode(':', $dt2[$is]);
                        $da[$dt2[$is][0]] = $dt2[$is][1];
                        $is++;
                    }
                    if (isset($da['use'])) unset($da['use']); // Ñïðàâà
                    if (isset($da['rl2'])) $da['rl2'] = -round((int)$da['rl2'] * 1.55); // Ñëåâà
                    if (isset($da['rl3'])) $da['rl3'] = round((int)$da['rl3'] + 160);
                    if (isset($da['rl4'])) $da['rl4'] = round((int)$da['rl4'] - 120);
                    $pl['date'] = str_replace('"', '', json_encode($da));


                    $i++;
                    if (!isset($pos[$this->cord[$pl['y'] . '_' . $pl['x']]])) {
                        $pos[$this->cord[$pl['y'] . '_' . $pl['x']]] = 0;
                    }
                    $pos[$this->cord[$pl['y'] . '_' . $pl['x']]]++;
                    $r .= ',' . ($i - 1) . ':{0:' . $pl['id'] . ',1:\'' . $pl['name'] . '\',2:' . (0 + $this->cord[$pl['y'] . '_' . $pl['x']]) . ',3:\'\',4:\'' . $pl['img'] . '\',5:' . $pl['type'] . ',6:0,7:' . $pl['w'] . ',8:' . $pl['h'] . ',9:' . $pl['left'] . ',10:' . $pl['top'] . ',11:' . $pl['date'] . '}';
                } elseif ($this->cord[$pl['y'] . '_' . $pl['x']] == $pl['os1'] + 1 || $this->cord[$pl['y'] . '_' . $pl['x']] == $pl['os2'] + 1 || $this->cord[$pl['y'] . '_' . $pl['x']] == $pl['os3'] + 1 || $this->cord[$pl['y'] . '_' . $pl['x']] == $pl['os4'] + 1) {

                    $dt2 = explode(',', ltrim(rtrim($pl['date'], '\}'), '\{'));
                    $da = [];
                    $is = 0;
                    while ($is < count($dt2)) {
                        $dt2[$is] = explode(':', $dt2[$is]);
                        $da[$dt2[$is][0]] = $dt2[$is][1];
                        $is++;
                    }
                    if (isset($da['use'])) unset($da['use']); // Ñïðàâà
                    if (isset($da['rl2'])) $da['rl2'] = 355 - round((int)$da['rl2'] * 0.30); // Ñïðàâà
                    if (isset($da['rl3'])) $da['rl3'] = round((int)$da['rl3'] - 160);
                    if (isset($da['rl4'])) $da['rl4'] = round((int)$da['rl4'] + 120);
                    $pl['date'] = str_replace('"', '', json_encode($da));
                    $i++;
                    if (!isset($pos[$this->cord[$pl['y'] . '_' . $pl['x']]])) {
                        $pos[$this->cord[$pl['y'] . '_' . $pl['x']]] = 0;
                    }
                    $pos[$this->cord[$pl['y'] . '_' . $pl['x']]]++;
                    $r .= ',' . ($i - 1) . ':{0:' . $pl['id'] . ',1:\'' . $pl['name'] . '\',2:' . (0 + $this->cord[$pl['y'] . '_' . $pl['x']]) . ',3:\'\',4:\'' . $pl['img'] . '\',5:' . $pl['type'] . ',6:0,7:' . $pl['w'] . ',8:' . $pl['h'] . ',9:' . $pl['left'] . ',10:' . $pl['top'] . ',11:' . $pl['date'] . '}';
                }
            }
        }
        return 'count:' . $i . $r;
    }

    public function botAtack($bot, $uid, $bs)
    {
        global $u, $c, $code;
        $user = mysql_fetch_assoc(mysql_query('SELECT `id`,`battle` FROM `users` WHERE `id` = "' . $uid['id'] . '" LIMIT 1'));

        if ($user['battle'] > 0) {
            $btli = mysql_fetch_assoc(mysql_query('SELECT `id` FROM `battle` WHERE `id` = "' . $user['battle'] . '" AND `team_win` = "-1" LIMIT 1'));
        }

        if (!isset($btli['id'])) { //Ñîçäàåì ïîåäèíîê
            $btl_id = 0;
            $expB = 0;
            $btl = ['players' => '', 'timeout' => 120, 'type' => 0, 'invis' => 0, 'noinc' => 0, 'travmChance' => 0, 'typeBattle' => 0, 'addExp' => $expB, 'money' => 0];

            mysql_query('INSERT INTO `battle` (`dungeon`,`dn_id`,`x`,`y`,`city`,`time_start`,`players`,`timeout`,`type`,`invis`,`noinc`,`travmChance`,`typeBattle`,`addExp`,`money`) VALUES ("' . $this->info['id2'] . '", "' . $this->info['id'] . '", "' . $bot['x'] . '", "' . $bot['y'] . '", "' . $u->info['city'] . '", "' . time() . '", "' . $btl['players'] . '", "' . $btl['timeout'] . '", "' . $btl['type'] . '", "' . $btl['invis'] . '", "' . $btl['noinc'] . '", "' . $btl['travmChance'] . '", "' . $btl['typeBattle'] . '", "' . $btl['addExp'] . '", "' . $btl['money'] . '")');

            $btl_id = mysql_insert_id();

            if ($btl_id > 0) { //Äîáàâëÿåì áîòîâ
                $sp = mysql_query('SELECT * FROM `dungeon_bots` WHERE `for_dn` = "0" AND `delete` = "0" AND `dn` = "' . $this->info['id'] . '" AND `id2` = "' . $bot['id2'] . '" LIMIT 1'); // Òîëüêî òîò, êîòîðûé íàïàë è æèâ ëè îí?
                $j = 0;
                $logins_bot = [];
                while ($pl = mysql_fetch_assoc($sp)) {
                    mysql_query('UPDATE `dungeon_bots` SET `inBattle` = "' . $btl_id . '" WHERE `id2` = "' . $bot['id2'] . '" LIMIT 1');
                    $jui = 1;
                    while ($jui <= $pl['colvo']) {
                        $k = $u->addNewbot($pl['id_bot'], null, null, $logins_bot);
                        $logins_bot = $k['logins_bot'];
                        if ($k) {
                            $upd = mysql_query('UPDATE `users` SET `battle` = "' . $btl_id . '" WHERE `id` = "' . $k['id'] . '" LIMIT 1');
                            if ($upd) {
                                $upd = mysql_query('UPDATE `stats` SET `team` = "2" WHERE `id` = "' . $k['id'] . '" LIMIT 1');
                                if ($upd) {
                                    $j++;
                                }
                            }
                        }
                        $jui++;
                    }
                }
                unset($logins_bot);
                if ($j > 0) {
                    mysql_query('UPDATE `users` SET `battle` = "' . $btl_id . '" WHERE `id` = "' . $user['id'] . '" LIMIT 1');
                    mysql_query('UPDATE `stats` SET `team` = "1" WHERE `id` = "' . $user['id'] . '" LIMIT 1');
                }
            }
        } else {
            $btl_id = $btli['id'];
            //Äîáàâëÿåì áîòîâ
            $sp = mysql_query('SELECT * FROM `dungeon_bots` WHERE `for_dn` = "0" AND `delete` = "0" AND `dn` = "' . $this->info['id'] . '" AND `id2` = "' . $bot['id2'] . '" LIMIT 1');
            $j = 0;
            $logins_bot = [];
            $logins_bot_text = [];
            $logins_bot_vars = ['time1=' . time() . ''];
            while ($pl = mysql_fetch_assoc($sp)) {
                mysql_query('UPDATE `dungeon_bots` SET `inBattle` = "' . $btl_id . '" WHERE `id2` = "' . $bot['id2'] . '" LIMIT 1');
                $jui = 1;
                while ($jui <= $pl['colvo']) {
                    $k = $u->addNewbot($pl['id_bot'], null, null, $logins_bot);
                    $logins_bot = $k['logins_bot'];
                    $logins_bot_text[] = ' <strong>' . $k['login'] . '</strong>';
                    if ($k != false) {
                        $upd = mysql_query('UPDATE `users` SET `battle` = "' . $btl_id . '" WHERE `id` = "' . $k['id'] . '" LIMIT 1');
                        if ($upd) {
                            $upd = mysql_query('UPDATE `stats` SET `team` = "2" WHERE `id` = "' . $k['id'] . '" LIMIT 1');
                            if ($upd) {
                                $j++;
                            }
                        }
                    }
                    $jui++;
                }
                if ($j > 0) {
                    $logins_bot_text = '{tm1} Â ïîåäèíîê âìåøàëèñü: ' . implode(', ', $logins_bot_text) . '.';
                    $logins_bot_vars = implode('||', $logins_bot_vars);
                    $battle_log = mysql_fetch_assoc(mysql_query('SELECT * FROM `battle_logs` WHERE `battle`=' . $btl_id . ' ORDER BY `id_hod` DESC LIMIT 1'));
                    if ($battle_log['id_hod'] > 0) {
                        mysql_query('INSERT INTO `battle_logs` (`time`,`battle`,`id_hod`,`text`,`vars`,`zona1`,`zonb1`,`zona2`,`zonb2`,`type`) VALUES ("' . time() . '","' . $btl_id . '","' . ($battle_log['id_hod'] + 1) . '","' . $logins_bot_text . '","' . $logins_bot_vars . '","","","","",1)');
                    }
                }
            }
            unset($logins_bot);
            if ($j > 0) {
                mysql_query('UPDATE `users` SET `battle` = "' . $btl_id . '" WHERE `id` = "' . $user['id'] . '" LIMIT 1');
                mysql_query('UPDATE `stats` SET `team` = "1" WHERE `id` = "' . $user['id'] . '" LIMIT 1');
            }
        }
    }

    public function genUsers()
    {
        global $u, $c, $code;
        ////i:{id,login,mapPoint,sex,obraz,type,users_p},
        //'count':1,0:{0:1015,1:'Test1',2:5,3:0,4:'1',5:'user',6:1},
        $r = '';
        $whr = [1 => ' ((`u`.`x` <= ' . ($u->info['x'] + 2) . ' && `u`.`x` >= ' . ($u->info['x'] - 2) . ') && (`u`.`y` >= ' . $u->info['y'] . ' && `u`.`y` <= ' . ($u->info['y'] + 4) . ')) ', //ïðÿìî
            3 => ' ((`u`.`x` <= ' . ($u->info['x'] + 2) . ' && `u`.`x` >= ' . ($u->info['x'] - 2) . ') && (`u`.`y` <= ' . $u->info['y'] . ' && `u`.`y` >= ' . ($u->info['y'] - 4) . ')) ', //âíèç
            2 => ' ((`u`.`x` <= ' . $u->info['x'] . ' && `u`.`x` >= ' . ($u->info['x'] - 4) . ') && (`u`.`y` <= ' . ($u->info['y'] + 2) . ' && `u`.`y` >= ' . ($u->info['y'] - 2) . ')) ', //ëåâî
            4 => ' ((`u`.`x` >= ' . $u->info['x'] . ' && `u`.`x` <= ' . ($u->info['x'] + 4) . ') && (`u`.`y` <= ' . ($u->info['y'] + 2) . ' && `u`.`y` >= ' . ($u->info['y'] - 2) . ')) ' //ïðàâî
        ];
        //
        $tmsu = mysql_fetch_assoc(mysql_query('SELECT * FROM `katok_now` WHERE `clone` = "' . $u->info['id'] . '" LIMIT 1'));
        //
        $sp = mysql_query('SELECT `u`.*,`st`.* FROM `stats` AS `u` LEFT JOIN `users` AS `st` ON (`u`.`id` = `st`.`id`) WHERE `u`.`dnow` = "' . $u->info['dnow'] . '" AND ' . $whr[$this->gs] . ' AND `u`.`id` != "' . $u->info['id'] . '" LIMIT 100');
        $i = 0;
        $pos = [];
        while ($pl = mysql_fetch_assoc($sp)) {
            $i++;
            if (!isset($pos[$this->cord[$pl['y'] . '_' . $pl['x']]])) {
                $pos[$this->cord[$pl['y'] . '_' . $pl['x']]] = 0;
            }
            $pos[$this->cord[$pl['y'] . '_' . $pl['x']]]++;
            $noat = 0;
            if ($this->info['bsid'] > 0) {
                $noat = 1;
            }
            if ($this->info['id2'] == 15) {
                //Õîêêåé
                $r .= ',' . ($i - 1) . ':{8:' . $noat . ',0:' . $pl['id'] . ',1:\'' . $pl['login'] . '\',2:' . (0 + $this->cord[$pl['y'] . '_' . $pl['x']]) . ',3:' . $pl['sex'] . ',4:\'' . str_replace('.gif', '', $pl['obraz']) . '\',5:\'user\',6:' . $pos[$this->cord[$pl['y'] . '_' . $pl['x']]] . '';
                //
                $tms = mysql_fetch_assoc(mysql_query('SELECT * FROM `katok_now` WHERE `clone` = "' . $pl['id'] . '" LIMIT 1'));
                if ($tms['team'] == $tmsu['team']) {
                    $r .= ',9:1';
                } else {
                    $r .= ',9:2';
                }
                $r .= '';
                //
                $r .= '}';
            } else {
                $r .= ',' . ($i - 1) . ':{9:0,8:' . $noat . ',0:' . $pl['id'] . ',1:\'' . $pl['login'] . '\',2:' . (0 + $this->cord[$pl['y'] . '_' . $pl['x']]) . ',3:' . $pl['sex'] . ',4:\'' . str_replace('.gif', '', $pl['obraz']) . '\',5:\'user\',6:' . $pos[$this->cord[$pl['y'] . '_' . $pl['x']]] . '}';
            }
        }

        //îòîáðàæàåì áîòîâ
        //
        //$sp = mysql_query('SELECT `u`.*,`st`.*  FROM `dungeon_bots` AS `u` LEFT JOIN `test_bot` AS `st` ON (`u`.`id_bot` = `st`.`id`) WHERE `u`.`dn` = "'.$u->info['dnow'].'" AND `u`.`atack` = "0" AND `u`.`delete` = "0" AND `u`.`inBattle` = "0" AND `u`.`go_bot` > 0 ORDER BY `u`.`go_bot` ASC LIMIT 35');

        // Âûáèðàåì Áîòîâ â ïîäçåìåëüè, êîòîðûå Íå â áîþ, Æèâûå, è íå äàëüøå ÷åì -\+30 ïî X è -\+35 ïî Y (äàáû íå ãîíÿòü âñåõ áîòîâ, ìåíüøå âûáîðêà).

        $sp = mysql_query('SELECT `db`.*, `tb`.*  FROM `dungeon_bots` AS `db` LEFT JOIN `test_bot` AS `tb` ON (`db`.`id_bot` = `tb`.`id`) LEFT JOIN `stats` as `st` ON (`st`.`dnow` = `db`.`dn`) WHERE `db`.`dn` = "' . $u->info['dnow'] . '" AND `db`.`x` > `st`.`x`-30 AND `db`.`x` < `st`.`x`+30 AND `db`.`y` > `st`.`y`-35 AND `db`.`y` < `st`.`y`+35 AND `db`.`atack` = "0" AND `db`.`delete` = "0" AND `db`.`inBattle` = "0" AND `db`.`go_bot` > 0 GROUP BY `db`.`id2` ORDER BY `db`.`go_bot` ASC LIMIT 100');
        while ($pl = mysql_fetch_assoc($sp)) {
            //ïåðåìåùåíèå áîòà, êàæäûå 3-10 ñåê.
            if ($pl['go_bot'] > 0 && $pl['go_bot'] <= time()) {
                $tgx = rand(-1, 1);
                $tgy = rand(-1, 1);
                if ($tgx != 0 && $tgy != 0) {
                    if (rand(0, 1) == 1) {
                        $tgy = 0;
                    } else {
                        $tgx = 0;
                    }
                }
                $vlb = $this->testLike($pl['x'], $pl['y'], $pl['x'] + $tgx, $pl['y'] + $tgy);
                //Êòî-òî ðÿäîì
                $tuz = mysql_fetch_assoc(mysql_query('SELECT `x`,`y`,`id`,`hpNow` FROM `stats` WHERE `dnow` = "' . $this->info['id'] . '" AND ( (`x` = ' . ($pl['x'] + 1) . ' AND `y` = ' . ($pl['y']) . ') OR (`x` = ' . ($pl['x'] - 1) . ' AND `y` = ' . ($pl['y']) . ') OR (`x` = ' . ($pl['x']) . ' AND `y` = ' . ($pl['y'] + 1) . ') OR (`x` = ' . ($pl['x']) . ' AND `y` = ' . ($pl['y'] - 1) . ') ) LIMIT 1'));

                if (isset($tuz['id']) && $this->testLike($pl['x'], $pl['y'], $tuz['x'], $tuz['y']) == 1) {
                    $tuz['x'] = $pl['x'];
                    $tuz['y'] = $pl['y'];
                    mysql_query('UPDATE `stats` SET `x` = "' . $tuz['x'] . '" , `y` = "' . $tuz['y'] . '" WHERE `id` = "' . $tuz['id'] . '" LIMIT 1');
                    $this->botAtack($pl, $tuz, 1);//Òóò
                    $sps = mysql_query('SELECT `db`.*, `tb`.*  FROM `dungeon_bots` AS `db` LEFT JOIN `test_bot` AS `tb` ON (`db`.`id_bot` = `tb`.`id`) LEFT JOIN `stats` as `st` ON (`st`.`dnow` = `db`.`dn`) WHERE `db`.`dn` = "' . $u->info['dnow'] . '" AND `db`.`x` ="' . $tuz['x'] . '" AND `db`.`y` ="' . $tuz['y'] . '" AND `db`.`atack` = "0" AND `db`.`delete` = "0" AND `db`.`inBattle` = "0" GROUP BY `db`.`id2` ORDER BY `db`.`go_bot` ASC LIMIT 50');
                    while ($pll = mysql_fetch_assoc($sps)) {
                        $this->botAtack($pll, $tuz, 1);
                    }

                } elseif ($vlb == 1) { // Ïåðåäâèæåíèå áîòîâ.
                    $pl['go_bot'] = time() + 10 + rand(1, 5);
                    $pl['x'] += $tgx;
                    $pl['y'] += $tgy;
                    mysql_query('UPDATE `dungeon_bots` SET `x` = "' . $pl['x'] . '",`y` = "' . $pl['y'] . '",`go_bot` = "' . $pl['go_bot'] . '" WHERE `id2` = "' . $pl['id2'] . '" LIMIT 1');
                }
                unset($tgx, $tgy, $vlb, $tuz);
            }
        }

        $sp = mysql_query('SELECT `u`.*,`st`.* FROM `dungeon_bots` AS `u` LEFT JOIN `test_bot` AS `st` ON (`u`.`id_bot` = `st`.`id`) WHERE `u`.`dn` = "' . $u->info['dnow'] . '" AND ' . $whr[$this->gs] . ' AND `u`.`delete` = "0" LIMIT 50');
        while ($pl = mysql_fetch_assoc($sp)) {
            $i++;
            if (!isset($pos[$this->cord[$pl['y'] . '_' . $pl['x']]])) {
                $pos[$this->cord[$pl['y'] . '_' . $pl['x']]] = 0;
            }
            $pos[$this->cord[$pl['y'] . '_' . $pl['x']]]++;
            $dlg = 0;
            if ($pl['dialog'] > 0) {
                $dlg = $pl['dialog'];
            }
            $noat = 1;
            if ($pl['noatack'] == 1 && $this->info['bsid'] == 0) {
                $noat = 0;
            }
            $r .= ',' . ($i - 1) . ':{9:0,0:' . $pl['id2'] . ',1:\'' . $pl['login'] . '\',2:' . (0 + $this->cord[$pl['y'] . '_' . $pl['x']]) . ',3:' . $pl['sex'] . ',4:\'' . str_replace('.gif', '', $pl['obraz']) . '\',5:\'bot\',6:' . $pos[$this->cord[$pl['y'] . '_' . $pl['x']]] . ',7:' . $dlg . ',8:' . $noat . '}';
        }

        return 'count:' . $i . $r;
    }

    public function testGo($id)
    {
        global $u, $c, $code;
        $go = 0;
        if ($id == 1) {
            //âïåðåä
            $go = $this->sg[$this->gs][1];
        } elseif ($id == 2) {
            //íàçàä
            $go = $this->sg[$this->gs][3];
        } elseif ($id == 3) {
            //íà ïðàâî
            $go = $this->sg[$this->gs][4];
        } elseif ($id == 4) {
            //íà ëåâî
            $go = $this->sg[$this->gs][2];
        }
        $thp = mysql_fetch_assoc(mysql_query('SELECT * FROM `dungeon_map` WHERE `x` = "' . $u->info['x'] . '" AND `y` = "' . $u->info['y'] . '" AND `id_dng` = "' . $this->info['id2'] . '" LIMIT 1'));
        $ng = [4 => 1, 2 => 2, 1 => 3, 3 => 4];
        if (isset($thp['id']) && $thp['go_' . $ng[$go]] == 0) {
            $go = 0;
        }
        $tgo = [0 => 0, 1 => 0];
        if ($go == 1) {
            $tgo[1] += 1;
        } elseif ($go == 2) {
            $tgo[0] -= 1;
        } elseif ($go == 3) {
            $tgo[1] -= 1;
        } elseif ($go == 4) {
            $tgo[0] += 1;
        }

        $tbot = mysql_fetch_assoc(mysql_query('SELECT * FROM `dungeon_bots` WHERE `x` = "' . ($u->info['x'] + (int)$tgo[0]) . '" AND `y` = "' . ($u->info['y'] + (int)$tgo[1]) . '" AND `dn` = "' . $this->info['id'] . '" AND `for_dn` = "0" AND `delete` = "0" LIMIT 1'));
        if (isset($tbot['id2']) && $u->info['admin'] == 0) {
            $go = 0;
        }

        $tmap = mysql_fetch_assoc(mysql_query('SELECT * FROM `dungeon_map` WHERE `x` = "' . $u->info['x'] . '" AND `y` = "' . $u->info['y'] . '" AND `id_dng` = "' . $this->info['id2'] . '" LIMIT 1'));
        //íàëè÷èå ïðåäìåòà
        if ($tmap['tr_items'] != '') {
            $ti = explode(',', $tmap['tr_items']);
            $i = 0;
            $trnit = '';
            while ($i < count($ti)) {
                $ti2 = explode('=', $ti[$i]);
                if ($ti2[0] > 0 && $ti2[1] > 0) {
                    $num_rows = mysql_num_rows(mysql_query('SELECT * FROM `items_users` WHERE  `uid` = "' . $u->info['id'] . '" AND `delete` = "0" AND `inShop` = "0" AND `item_id` = "' . ((int)$ti2[0]) . '" LIMIT ' . ((int)$ti2[1]) . ''));
                    if ($num_rows < (int)$ti2[1]) {
                        $tgo = $ti2[2];
                        if ($tgo != '0000' && $tgo[$ng[$go] - 1] == 1) {
                            $go = 0;
                            $trm = mysql_fetch_assoc(mysql_query('SELECT * FROM `items_main` WHERE `id` = "' . ((int)$ti2[0]) . '" LIMIT 1'));
                            $trnit .= '&quot;' . $trm['name'] . '&quot;, ';
                        }
                    }
                }
                $i++;
            }
            if ($trnit != '') {
                $trnit = rtrim($trnit, ', ');
                $this->error = 'Ó âàñ íåò ïîäõîäÿùåãî ïðåäìåòà. Òðåáóåòñÿ ' . $trnit;
            }
        }

        $tmGo = $u->info['timeGo'] - time(); //ñêîëüêî ñåêóíä îñòàëîñü
        if ($tmGo > 0) {
            $go = 0;
            $this->error = 'Íå òàê áûñòðî...';
        }

        if ($u->aves['now'] >= $u->aves['max']) {
            $go = 0;
            $this->error = 'Âû íå ìîæåòå ïåðåìåùàòüñÿ, ðþêçàê ïåðåïîëíåí ...';
        }

        if ($go > 0) {
            if ($go == 1) {
                $u->info['y'] += 1;
            } elseif ($go == 2) {
                $u->info['x'] -= 1;
            } elseif ($go == 3) {
                $u->info['y'] -= 1;
            } elseif ($go == 4) {
                $u->info['x'] += 1;
            }
            if ($u->stats['speed_dungeon'] > 0) {
                $tmap['timeGO'] = round($tmap['timeGO'] / 100 * (100 - $u->stats['speed_dungeon']));
                if ($tmap['timeGO'] < 2) {
                    $tmap['timeGO'] = 1;
                }
            }
            $u->info['timeGo'] = time() + $tmap['timeGO'];
            $u->info['timeGoL'] = time();
            $tmap0 = mysql_fetch_assoc(mysql_query('SELECT `id`,`teleport` FROM `dungeon_map` WHERE `id_dng` = "' . $tmap['id_dng'] . '" AND `x` = "' . $u->info['x'] . '" AND `y` = "' . $u->info['y'] . '" LIMIT 1'));
            if ($tmap0['teleport'] > 0) {
                $tmap1 = mysql_fetch_assoc(mysql_query('SELECT `id`,`x`,`y` FROM `dungeon_map` WHERE `id` = "' . $tmap0['teleport'] . '" LIMIT 1'));
                if (isset($tmap1['id'])) {
                    $u->info['x'] = $tmap1['x'];
                    $u->info['y'] = $tmap1['y'];
                    $this->error = 'Âû ïåðåìåñòèëèñü â äðóãóþ êîìíàòó...';
                }
            }
            mysql_query('UPDATE `stats` SET `x` = "' . $u->info['x'] . '",`y` = "' . $u->info['y'] . '",`timeGo` = "' . $u->info['timeGo'] . '",`timeGoL` = "' . $u->info['timeGoL'] . '" WHERE `id` = "' . $u->info['id'] . '" LIMIT 1');
            unset($id);
        }
    }

    public function testGone($id)
    {
        global $u, $c, $code;
        $go = 0;
        if ($id == 1) {
            //âïåðåä
            $go = $this->sg[$this->gs][1];
        } elseif ($id == 2) {
            //íàçàä
            $go = $this->sg[$this->gs][3];
        } elseif ($id == 3) {
            //íà ïðàâî
            $go = $this->sg[$this->gs][4];
        } elseif ($id == 4) {
            //íà ëåâî
            $go = $this->sg[$this->gs][2];
        }
        $thp = mysql_fetch_assoc(mysql_query('SELECT * FROM `dungeon_map` WHERE `x` = "' . $u->info['x'] . '" AND `y` = "' . $u->info['y'] . '" AND `id_dng` = "' . $this->info['id2'] . '" LIMIT 1'));
        $ng = [4 => 1, 2 => 2, 1 => 3, 3 => 4];
        if (isset($thp['id']) && $thp['go_' . $ng[$go]] == 0) {
            $go = 0;
        }
        $tgo = [0 => 0, 1 => 0];
        if ($go == 1) {
            $tgo[1] += 1;
        } elseif ($go == 2) {
            $tgo[0] -= 1;
        } elseif ($go == 3) {
            $tgo[1] -= 1;
        } elseif ($go == 4) {
            $tgo[0] += 1;
        }

        $tbot = mysql_fetch_assoc(mysql_query('SELECT * FROM `dungeon_bots` WHERE `x` = "' . ($u->info['x'] + (int)$tgo[0]) . '" AND `y` = "' . ($u->info['y'] + (int)$tgo[1]) . '" AND `dn` = "' . $this->info['id'] . '" AND `for_dn` = "0" AND `delete` = "0" LIMIT 1'));
        if (isset($tbot['id2']) && $u->info['admin'] == 0) {
            $go = 0;
        }

        return $go;
    }

    public function testSt($id, $s)
    {
        $r = 0;
        //çàìåíÿåì îòîáðàæåíèå ñòåí â çàâèñèìîñòè îò óãëà îáçîðà
        $s = $this->sg[$this->gs][$s];
        if (isset($this->map[1][$id]['id'])) {
            $r = $this->map[1][$id]['st'][($s - 1)];
        }
        return $r;
    }

    public function lookDungeon()
    {
        global $u, $c, $code, $pd;
        /* Ãåíåðèðóåì èçîáðàæåíèå êàðòû */ /* LEVEL 1 */

        // Èñïðàâëåíèÿ îò 29/10/2014 îòíîñèòåëüíî D5, 2 ýòàæ, ÏÒÏ. Ïîêðóòèòüñÿ è âñå ãóä.  è îò 22/11/2014 äëÿ îáðàòíûõ ñòåí. Åñëè ñòåíà ê íàì çàäíèöåé, ìû å¸ íå âèäèì.
        if ($this->testSt(2, 4) > 0 /* || $this->testSt(3,2)>0 */) {
            $pd[28] = 1;
        }
        if (/*$this->testSt(1,4)>0 ||*/ $this->testSt(2, 2) > 0) {
            $pd[27] = 1;
        }
        if ($this->testSt(2, 1) > 0 /*|| $this->testSt(5,3)>0*/) {
            $pd[26] = 1;
        }
        if ($this->testSt(3, 1) > 0/* || $this->testSt(6,3)>0*/) {
            $pd[25] = 1;
        }
        if ($this->testSt(1, 1) > 0 /*|| $this->testSt(4,3)>0*/) {
            $pd[24] = 1;
        }
        /* LEVEL 2 */
        if ($this->testSt(5, 4) > 0 /*|| $this->testSt(6,2)>0*/) {
            $pd[23] = 1;
        }
        if (/*$this->testSt(4,4)>0 || */ $this->testSt(5, 2) > 0) {
            $pd[22] = 1;
        }
        if ($this->testSt(5, 1) > 0 /*|| $this->testSt(8,3)>0*/) {
            $pd[21] = 1;
        }
        if ($this->testSt(6, 1) > 0 /*|| $this->testSt(7,3)>0*/) {
            $pd[20] = 1;
        }
        if ($this->testSt(4, 1) > 0/* || $this->testSt(9,3)>0*/) {
            $pd[19] = 1;
        }

        /* LEVEL 3 */
        if ($this->testSt(8, 4) > 0 /*|| $this->testSt(7,2)>0*/) {
            $pd[18] = 1;
        }
        if (/*$this->testSt(9,4)>0 || */ $this->testSt(8, 2) > 0) {
            $pd[17] = 1;
        }
        if ($this->testSt(8, 1) > 0 /* || $this->testSt(12,3)>0*/) {
            $pd[16] = 1;
        }
        if ($this->testSt(7, 1) > 0 /* || $this->testSt(13,3)>0*/) {
            $pd[15] = 1;
        }
        if ($this->testSt(9, 1) > 0 /*|| $this->testSt(11,3)>0*/) {
            $pd[14] = 1;
        }

        /* LEVEL 4 */

        if ($this->testSt(12, 4) > 0 || $this->testSt(13, 2) > 0) {
            $pd[13] = 1;
        }
        if ($this->testSt(12, 2) > 0 || $this->testSt(11, 4) > 0) {
            $pd[12] = 1;
        }
        if ($this->testSt(13, 1) > 0 || $this->testSt(17, 3) > 0) {
            $pd[11] = 1;
        } //8
        if ($this->testSt(11, 1) > 0 || $this->testSt(16, 3) > 0) {
            $pd[10] = 1;
        } //7
        if ($this->testSt(12, 1) > 0/* || $this->testSt(15,3)>0*/) {
            $pd[9] = 1;
        }
        if ($this->testSt(14, 1) > 0 || $this->testSt(18, 3) > 0) {
            $pd[6] = 1;
        } //2
        if ($this->testSt(10, 1) > 0 || $this->testSt(19, 3) > 0) {
            $pd[5] = 1;
        } //1
        if ($this->testSt(16, 4) > 0 || $this->testSt(15, 2) > 0) {
            $pd[4] = 1;
        }
        if ($this->testSt(15, 4) > 0 || $this->testSt(17, 2) > 0) {
            $pd[3] = 1;
        }

        /* Ãåíåðèðóåì ïðåäìåòû íà êàðòå */

        /* Ãåíåðèðóåì ïåðñîíàæåé è áîòîâ íà êàðòå */

    }

    public function getMatrix($y, $x)
    {
        global $u;
        $this->cord['x']++;
        $this->cord[($u->info['y'] + $y) . '_' . ($u->info['x'] + $x)] = $this->cord['x'];
        return $this->map[0][($u->info['y'] + $y) . '_' . ($u->info['x'] + $x)];
    }

    public function genMatix()
    {
        $r = [];
        if ($this->gs == 1) {
            //1; //ñìîòðèì ïðÿìî
            $r[1] = $this->getMatrix(0, -1); # ñëåâà îò ìåíÿ
            $r[2] = $this->getMatrix(0, 0); # ïîäîìíîé
            $r[3] = $this->getMatrix(0, 1); # ñïðàâà îò ìåíÿ
            $r[4] = $this->getMatrix(1, -1); # ñëåâà +1 âïåðåä
            $r[5] = $this->getMatrix(1, 0); # ïåðåäîìíîé +1
            $r[6] = $this->getMatrix(1, 1); # ñïðàâà +1 âïåðåä
            $r[7] = $this->getMatrix(2, 1); # ñïðàâà +2 âïåðåä
            $r[8] = $this->getMatrix(2, 0); # ïåðåäîìíîé +2
            $r[9] = $this->getMatrix(2, -1); # ñëåâà +2 âïåðåä
            $r[10] = $this->getMatrix(3, -2); # ñëåâà ÷åðåç îäíó, +3 âïåðåä
            $r[11] = $this->getMatrix(3, -1); # ñëåâà +3 âïåðåä
            $r[12] = $this->getMatrix(3, 0); # ïåðåäîìíîé +3
            $r[13] = $this->getMatrix(3, 1); # ñïðàâà +3 âïåðåä
            $r[14] = $this->getMatrix(3, 2); # ñïðàâà ÷åðåç îäíó, +3 âïåðåä
            $r[15] = $this->getMatrix(4, 0); # ïåðåäîìíîé +4
            $r[16] = $this->getMatrix(4, -1); # ñëåâà +4 âïåðåä
            $r[17] = $this->getMatrix(4, 1); # ñïðàâà +4 âïåðåä
            $r[18] = $this->getMatrix(4, 2); # ñïðàâà ÷åðåç îäíó, +4 âïåðåä
            $r[19] = $this->getMatrix(4, -2); # ñëåâà ÷åðåç îäíó, +4 âïåðåä

        } elseif ($this->gs == 2) {
            //2; //ñìîòðèì ëåâî
            $r[1] = $this->getMatrix(-1, 0);
            $r[2] = $this->getMatrix(0, 0);
            $r[3] = $this->getMatrix(1, 0);
            $r[4] = $this->getMatrix(-1, -1);
            $r[5] = $this->getMatrix(0, -1);
            $r[6] = $this->getMatrix(1, -1);
            $r[7] = $this->getMatrix(1, -2);
            $r[8] = $this->getMatrix(0, -2);
            $r[9] = $this->getMatrix(-1, -2);
            $r[10] = $this->getMatrix(-2, -3);
            $r[11] = $this->getMatrix(-1, -3);
            $r[12] = $this->getMatrix(0, -3);
            $r[13] = $this->getMatrix(1, -3);
            $r[14] = $this->getMatrix(2, -3);
            $r[15] = $this->getMatrix(0, -4);
            $r[16] = $this->getMatrix(-1, -4);
            $r[17] = $this->getMatrix(1, -4);
            $r[18] = $this->getMatrix(2, -4);
            $r[19] = $this->getMatrix(-2, -4);
        } elseif ($this->gs == 3) {
            //3; //ñìîòðèì âíèç
            $r[1] = $this->getMatrix(0, 1);
            $r[2] = $this->getMatrix(0, 0);
            $r[3] = $this->getMatrix(0, -1);
            $r[4] = $this->getMatrix(-1, 1);
            $r[5] = $this->getMatrix(-1, 0);
            $r[6] = $this->getMatrix(-1, -1);
            $r[7] = $this->getMatrix(-2, -1);
            $r[8] = $this->getMatrix(-2, 0);
            $r[9] = $this->getMatrix(-2, 1);
            $r[10] = $this->getMatrix(-3, 2);
            $r[11] = $this->getMatrix(-3, 1);
            $r[12] = $this->getMatrix(-3, 0);
            $r[13] = $this->getMatrix(-3, -1);
            $r[14] = $this->getMatrix(-3, -2);
            $r[15] = $this->getMatrix(-4, 0);
            $r[16] = $this->getMatrix(-4, 1);
            $r[17] = $this->getMatrix(-4, -1);
            $r[18] = $this->getMatrix(-4, -2);
            $r[19] = $this->getMatrix(-4, 2);
        } elseif ($this->gs == 4) {
            //4; //ñìîòðèì ïðàâî
            $r[1] = $this->getMatrix(1, 0);
            $r[2] = $this->getMatrix(0, 0);
            $r[3] = $this->getMatrix(-1, 0);
            $r[4] = $this->getMatrix(1, 1);
            $r[5] = $this->getMatrix(0, 1);
            $r[6] = $this->getMatrix(-1, 1);
            $r[7] = $this->getMatrix(-1, 2);
            $r[8] = $this->getMatrix(0, 2);
            $r[9] = $this->getMatrix(1, 2);
            $r[10] = $this->getMatrix(2, 3);
            $r[11] = $this->getMatrix(1, 3);
            $r[12] = $this->getMatrix(0, 3);
            $r[13] = $this->getMatrix(-1, 3);
            $r[14] = $this->getMatrix(-2, 3);
            $r[15] = $this->getMatrix(0, 4);
            $r[16] = $this->getMatrix(1, 4);
            $r[17] = $this->getMatrix(-1, 4);
            $r[18] = $this->getMatrix(-2, 4);
            $r[19] = $this->getMatrix(2, 4);
        }
        return $r;
    }
}