-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmap.php
executable file
·233 lines (216 loc) · 6.31 KB
/
map.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
require_once('properties.php');
require_once('tileset.php');
require_once('tilelayer.php');
require_once('objectlayer.php');
require_once('imagelayer.php');
class MapBase {
//attributes
public $version='';
public $orientation='';
public $width=0;
public $height=0;
public $tilewidth=0;
public $tileheight=0;
public $backgroundcolor='';
public $tilesets=array();
public $layers=array();
public $filename='';
private $xml=NULL;
public $ref='';
private static $local_refs=array(
'',
'file',
'LOCAL',
);
private static $urls=array(
'tmw'=>'https://github.com/themanaworld/tmwa-client-data/raw/master/',
'evol'=>'https://github.com/EvolOnline/clientdata-beta/raw/master/',
'tales'=>'https://github.com/tales/sourceoftales/raw/master/',
'elmlor'=>'https://github.com/KaneHart/Elmlor-Client-Data/raw/master/',
'lof'=>'https://github.com/landoffire/lof-tmwa-client-data/raw/master/',
'lof-newworld'=>'https://github.com/landoffire/lof-newworld/raw/master/',
'stendhal'=>'http://arianne.cvs.sourceforge.net/viewvc/arianne/stendhal/tiled/',
);
//constructors
//static methods
public static function load_xml_from_file($filename, $ref='') {
if(!file_exists($filename)) {
throw new Exception('File \''.htmlentities($filename).'\' not found with ref \''.$ref.'\'.');
}
return simplexml_load_file($filename);
}
public static function load_xml_from_url($url, $ref='') {
//return simplexml_load_file($url);
return simplexml_load_string(get_url($url));
}
public static function load_xml($filename, $ref='') {
if(in_array($ref, MapBase::$local_refs)) {
return MapBase::load_xml_from_file($filename);
//return self::load_xml_from_file($filename);
}
//else if(array_key_exists($ref, self::$urls)) {
else if(array_key_exists($ref, MapBase::$urls)) {
//var_dump(self::$urls[$ref].$filename);
//return self::load_xml_from_url(self::$urls[$ref].$filename);
return MapBase::load_xml_from_url(MapBase::$urls[$ref].$filename);
}
else {
throw new Exception('Incorrect Map ref.');
}
}
//methods
private function load_map() {
$this->version = (string)$this->xml['version'];
$this->orientation = (string)$this->xml['orientation'];
$this->width =(int)$this->xml['width' ];
$this->height=(int)$this->xml['height'];
$this->tilewidth =(int)$this->xml['tilewidth' ];
$this->tileheight=(int)$this->xml['tileheight'];
$this->backgroundcolor=(string)$this->xml['backgroundcolor'];
}
private function load_tilesets($recur=true) {
$i=0;
foreach($this->xml->tileset as $ts) {
$this->tilesets[$i]=new Tileset();
$this->tilesets[$i]->ref=$this->ref;
$this->tilesets[$i]->setMap($this);
$this->tilesets[$i]->load_from_element($ts, $this->ref, $recur);
++$i;
}
return $i;
}
private function load_layers($recur=true) {
$i=0;
foreach($this->xml->children() as $ly) {
if( $ly->getName() === 'properties' || $ly->getName() === 'tileset' ) {
continue;
}
if($ly->getName() === 'layer') {
$this->load_tilelayer($i, $ly, $recur);
}
elseif($ly->getName() === 'objectgroup') {
$this->load_objectlayer($i, $ly, $recur);
}
elseif($ly->getName() === 'imagelayer') {
$this->load_imagelayer($i, $ly, $recur);
}
else {
throw new Exception('unknown element in xml file (from '.__FILE__.':'.__LINE__.')');
return false;
}
++$i;
}
return $i;
}
private function load_tilelayer($i, $tl, $recur=true) {
$this->layers[$i]=new TileLayer();
$this->layers[$i]->setMap($this);
$this->layers[$i]->ref=$this->ref;
$this->layers[$i]->load_from_element($tl, $this->ref, $recur);
}
private function load_imagelayer($i, $il, $recur=true) {
$this->layers[$i]=new ImageLayer();
$this->layers[$i]->setMap($this);
$this->layers[$i]->ref=$this->ref;
$this->layers[$i]->load_from_element($il, $this->ref, $recur);
}
private function load_objectlayer($i, $ol, $recur=true) {
$this->layers[$i]=new ObjectLayer();
$this->layers[$i]->setMap($this);
$this->layers[$i]->ref=$this->ref;
$this->layers[$i]->load_from_element($ol, $this->ref, $recur);
}
public function load($filename, $ref='', $recur=true) {
$this->ref=$ref;
$this->filename=$filename;
$this->xml=self::load_xml($filename, $ref);
if($this->xml===false) {
if($ref==='') {
throw new Exception('File \''.$filename.'\' not found.');
}
else {
throw new Exception('File \''.$filename.'\' not found or inaccessible with ref \''.$ref.'\'.');
}
}
$this->load_map();
if((bool)$this->xml->properties!==false) {
$this->loadProperties_from_element($this->xml->properties, $ref);
}
if($recur) {
$this->load_tilesets($recur);
$this->load_layers($recur);
}
return $this->xml;
}
public function get_tileset_index($gid) {
$index=-1;
foreach($this->tilesets as $i=>$ts) {
if($gid>=$ts->firstgid) $index=$i;
}
//unset($i,$ts);
return $index;
}
public function isValid() {
if($this->version!=='1.0') {
throw new Exception('Incorrect map version.');
return false;
}
if(!in_array($this->orientation, array('orthogonal', 'isometric', 'stagerred'))) {
throw new Exception('Incorrect map orientation.');
return false;
}
if(!is_int($this->width ) || $this->width <0) {
throw new Exception('Incorrect map width .');
return false;
}
if(!is_int($this->height) || $this->height<0) {
throw new Exception('Incorrect map height.');
return false;
}
if(!is_int($this->tilewidth ) || $this->tilewidth <0) {
throw new Exception('Incorrect map tilewidth .');
return false;
}
if(!is_int($this->tileheight) || $this->tileheight<0) {
throw new Exception('Incorrect map tileheight.');
return false;
}
if(!is_string($this->backgroundcolor)) {
throw new Exception('Incorrect map backgroundcolor.');
return false;
}
return true;
}
public function isValidR() {
$this->isValid();
foreach($this->tilesets as $i=>$ts) {
if(!($ts instanceof Tileset)) {
throw new Exception('Incorrect map tileset.');
return false;
}
try {
$ts->isValid();
}
catch(Exception $ex) {
print('Tileset n°'.$i."\n");
throw $ex;
}
}
foreach($this->layers as $i->$ly) {
if(!($ly instanceof Layer)) {
throw new Exception('Incorrect map element.');
return false;
}
try {
$ly->isValid();
}
catch(Exception $ex) {
print('*Layer n°'.$i."\n");
throw $ex;
}
}
return true;
}
};
?>