mirror of
https://github.com/slawkens/myaac.git
synced 2025-04-27 09:49:22 +02:00
Automatically load towns from .OTBM file
Takes up to 10 seconds for otservbr on my PC Taken from old Smart AAC - class SpawnsReader.php
This commit is contained in:
parent
ae7350e3a0
commit
a52396008d
@ -165,7 +165,9 @@ $config = array(
|
|||||||
'character_name_max_length' => 21,
|
'character_name_max_length' => 21,
|
||||||
|
|
||||||
// list of towns
|
// list of towns
|
||||||
// if you use TFS 1.3 with support for 'towns' table in database, then you can ignore this - it will be configured automatically (generated from your .OTBM map)
|
// if you use TFS 1.3 with support for 'towns' table in database, then you can ignore this - it will be configured automatically (from MySQL database - Table - towns)
|
||||||
|
// otherwise it will try to load from your .OTBM map file
|
||||||
|
// if you don't see towns on website, then you need to fill this out
|
||||||
'towns' => array(
|
'towns' => array(
|
||||||
0 => 'No town',
|
0 => 'No town',
|
||||||
1 => 'Sample town'
|
1 => 'Sample town'
|
||||||
|
@ -159,28 +159,9 @@ else {
|
|||||||
}
|
}
|
||||||
unset($tmp, $id, $vocation);
|
unset($tmp, $id, $vocation);
|
||||||
|
|
||||||
// load towns
|
//////////////////////////////////////
|
||||||
/* TODO: doesnt work
|
// load towns from database or OTBM //
|
||||||
ini_set('memory_limit', '-1');
|
//////////////////////////////////////
|
||||||
$tmp = '';
|
|
||||||
|
|
||||||
if($cache->enabled() && $cache->fetch('towns', $tmp)) {
|
|
||||||
$config['towns'] = unserialize($tmp);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$towns = new OTS_OTBMFile();
|
|
||||||
$towns->loadFile('D:/Projekty/opentibia/wodzislawski/data/world/wodzislawski.otbm');
|
|
||||||
|
|
||||||
$config['towns'] = $towns->getTownsList();
|
|
||||||
if($cache->enabled()) {
|
|
||||||
$cache->set('towns', serialize($config['towns']), 120);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
////////////////////////////////////////
|
|
||||||
// load towns from database (TFS 1.3) //
|
|
||||||
////////////////////////////////////////
|
|
||||||
|
|
||||||
$tmp = '';
|
$tmp = '';
|
||||||
$towns = [];
|
$towns = [];
|
||||||
@ -197,9 +178,34 @@ else {
|
|||||||
|
|
||||||
unset($query);
|
unset($query);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
$mapName = configLua('mapName');
|
||||||
|
if (!isset($mapName)) {
|
||||||
|
$mapName = configLua('map');
|
||||||
|
$mapFile = $config['server_path'] . $mapName;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strpos($mapName, '.otbm') === false) {
|
||||||
|
$mapName .= '.otbm';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($mapFile)) {
|
||||||
|
$mapFile = config('data_path') . 'world/' . $mapName;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file_exists($mapFile)) {
|
||||||
|
ini_set('memory_limit', '-1');
|
||||||
|
|
||||||
|
require LIBS . 'TownsReader.php';
|
||||||
|
$townsReader = new TownsReader($mapFile);
|
||||||
|
$townsReader->load();
|
||||||
|
|
||||||
|
$towns = $townsReader->get();
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
$towns = config('towns');
|
$towns = config('towns');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if($cache->enabled()) {
|
if($cache->enabled()) {
|
||||||
$cache->set('towns', serialize($towns), 600);
|
$cache->set('towns', serialize($towns), 600);
|
||||||
@ -207,6 +213,6 @@ else {
|
|||||||
}
|
}
|
||||||
|
|
||||||
config(['towns', $towns]);
|
config(['towns', $towns]);
|
||||||
//////////////////////////////////////////////
|
////////////////////////////////////////////
|
||||||
// END - load towns from database (TFS 1.3) //
|
// END - load towns from database or OTBM //
|
||||||
//////////////////////////////////////////////
|
////////////////////////////////////////////
|
||||||
|
82
system/libs/TownsReader.php
Normal file
82
system/libs/TownsReader.php
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
This file is part of OTSCMS (http://www.otscms.com/) project.
|
||||||
|
|
||||||
|
Copyright (C) 2005 - 2007 Wrzasq (wrzasq@gmail.com)
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation; either version 2 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
This code bases on oryginal OTServ code for .otbm files - file iomapotbm.cpp rev.2141
|
||||||
|
*/
|
||||||
|
class TownsReader
|
||||||
|
{
|
||||||
|
// node bytes
|
||||||
|
const ESCAPE_CHAR = 0xFD;
|
||||||
|
const NODE_START = 0xFE;
|
||||||
|
|
||||||
|
// map node types
|
||||||
|
const OTBM_TOWN = 13;
|
||||||
|
|
||||||
|
// file handler
|
||||||
|
protected $file;
|
||||||
|
|
||||||
|
// towns
|
||||||
|
private $towns = [];
|
||||||
|
|
||||||
|
// loads map .otbm file
|
||||||
|
public function __construct($file)
|
||||||
|
{
|
||||||
|
// opens file for reading
|
||||||
|
$this->file = fopen($file, 'rb');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function load()
|
||||||
|
{
|
||||||
|
// checks if file is opened correctly
|
||||||
|
if ($this->file) {
|
||||||
|
// skips version
|
||||||
|
fseek($this->file, 4);
|
||||||
|
|
||||||
|
// reads nodes chain
|
||||||
|
while (!feof($this->file)) {
|
||||||
|
// reads byte
|
||||||
|
switch (ord(fgetc($this->file))) {
|
||||||
|
// maybe a town node
|
||||||
|
case self::NODE_START:
|
||||||
|
// reads node type
|
||||||
|
if (ord(fgetc($this->file)) == self::OTBM_TOWN) {
|
||||||
|
$id = unpack('L', fread($this->file, 4));
|
||||||
|
$length = unpack('S', fread($this->file, 2));
|
||||||
|
|
||||||
|
// reads town name
|
||||||
|
$this->towns[$id[1]] = fread($this->file, $length[1]);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
// escape next character - it might be NODE_START character which is in fact not
|
||||||
|
case self::ESCAPE_CHAR:
|
||||||
|
fgetc($this->file);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get() {
|
||||||
|
return $this->towns;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user