-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtileset.php
executable file
·289 lines (276 loc) · 9.11 KB
/
tileset.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
require_once('properties.php');
require_once('idproperties.php');
require_once('map.php');
class TilesetBase {
//attributes
public $firstgid=0;
public $name='';
public $tilewidth=0;
public $tileheight=0;
public $tileoffsetx=0;
public $tileoffsety=0;
public $margin=0;
public $spacing=0;
public $source='';
public $sourceTSX='';
public $trans='';
public $width=0;
public $height=0;
public $terrains=array();
public $tiles=array();
//internal
public $ref=NULL;
public $map=NULL;
private $filename='';
//private $xml=NULL;
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) {
if(!file_exists($filename)) {
throw new Exception('File \''.htmlentities($filename).'\' not found.');
}
return simplexml_load_file($filename);
}
public static function load_xml_from_url($url) {
//return simplexml_load_file($url);
return simplexml_load_string(get_url($url));
}
public static function load_xml($filename, $ref='') {
if(in_array($ref, TilesetBase::$local_refs)) {
return TilesetBase::load_xml_from_file($filename);
//return self::load_xml_from_file($filename);
}
else if(array_key_exists($ref, TilesetBase::$urls)) {
//else if(array_key_exists($ref, self::$urls)) {
return TilesetBase::load_xml_from_url(TilesetBase::$urls[$ref].$filename);
//var_dump(self::$urls[$ref].$filename);
//return self::load_xml_from_url(self::$urls[$ref].$filename);
}
else {
throw new Exception('Incorrect Tileset ref.');
}
}
//methods
public function setMap(Map $map) {
$this->map=$map;
}
public function getMap() {
return $this->map;
}
public function load_from_tsx($filename, $ref='') {
$this->ref=$ref;
$this->filename=$filename;
//$xml=Tileset::load_xml($filename, $ref);
$xml=self::load_xml($filename, $ref);
if($xml===false) {
if($ref==='') {
throw new Exception('File \''.$filename.'\' not found.');
}
else {
throw new Exception('File \''.$filename.'\' not found or inaccessible with ref \''.$ref.'\'.');
}
}
return $this->load_from_element($xml);
}
public function load_from_element(SimpleXMLElement $xml, $ref='', $recur=true) {
$this->ref=$ref;
if((bool)$xml['source']!=FALSE) {
$this->sourceTSX=(string)$xml['source'];
$this->firstgid=(int)$xml['firstgid'];
if($recur) {
$this->load_from_tsx(dirname($this->map->filename).'/'.$this->sourceTSX, $ref);
}
return;
}
if(isset($xml['firstgid'])) $this->firstgid=(int)$xml['firstgid'];
if((bool)$xml['firstgid']!=FALSE) $this->firstgid=(int)$xml['firstgid'];
$this->name=(string)$xml['name'];
$this->tilewidth =(int)$xml['tilewidth' ];
$this->tileheight=(int)$xml['tileheight'];
$this->tileoffsetx=(int)$xml->tileoffset['x'];
$this->tileoffsety=(int)$xml->tileoffset['y'];
$this->margin =(int)$xml['margin'];
$this->spacing=(int)$xml['spacing'];
$this->source=(string)$xml->image['source'];//
$this->trans=(string)$xml->image['trans'];
$this->width =(int)$xml->image['width' ];
$this->height=(int)$xml->image['height'];
if( ($this->width ==0 || $this->height==0) && $recur ) {
if($this->ref=='') {
if(file_exists(dirname($this->filename).'/'.$this->source)) {
$ar=getimagesize(dirname($this->filename).'/'.$this->source);
$this->width =$ar[0];
$this->height=$ar[1];
unset($ar);
}
else {
trigger_error('Image \''.$this->source.'\' not found', E_USER_NOTICE);
}
}
else {
$ar=getimagesize(TilesetBase::$urls[$ref].dirname($this->map->filename).'/'.dirname($this->filename).'/'.$this->source);
$this->width =$ar[0];
$this->height=$ar[1];
unset($ar);
}
}
$j=0;
if((bool)$xml->terraintypes!=FALSE) {
foreach($xml->terraintypes->terrain as $terrain) {
$this->terrains[$j]=array();
$this->terrains[$j]['name']=(string)$terrain['name'];
$this->terrains[$j]['tile']=(int)$terrain['tile'];
$this->terrains[$j]['distances']=(string)$terrain['distances'];
++$j;
}
}
unset($j);
if((bool)$xml->tile!=FALSE) {
$this->tiles=array();
foreach($xml->tile as $tile) {
assert(isset($tile['id']));
$this->tiles[(string)$tile['id']]=array();
if((bool)$tile->image!=false) {
if(isset($tile->image['format'])) {
$this->tiles[(string)$tile['id']]['imageformat']=(string)$tile->image['format'];
}
if((bool)$tile->image->data!=false && isset($tile->image->data['encoding'])) {
$this->tiles[(string)$tile['id']]['imageencoding']=(string)$tile->image->data['encoding'];
}
if((bool)$tile->image->data!=false && isset($tile->image->data['compression'])) {
$this->tiles[(string)$tile['id']]['imagecompression']=(string)$tile->image->data['compression'];
}
if((bool)$tile->image->data!=false && isset($tile->image->data[0])) {
$this->tiles[(string)$tile['id']]['imagecontent']=trim((string)$tile->image->data[0]);
}
if(strlen($this->tiles[(string)$tile['id']]['imagecontent'])>0) {
if( strlen($this->tiles[(string)$tile['id']]['imageencoding'])>0 && strlen($this->tiles[(string)$tile['id']]['imagecompression'])>0 ) {
$this->tiles[(string)$tile['id']]['imagecontent']=parse_data($this->tiles[(string)$tile['id']]['imagecontent'], $this->tiles[(string)$tile['id']]['imageencoding'], $this->tiles[(string)$tile['id']]['imagecompression']);
}
}
if(isset($tile->image['source'])) {
$this->tiles[(string)$tile['id']]['imagesource']=(string)$tile->image['source'];
}
}
//var_dump((string)$tile['id']);//die();
//var_dump((string)$tile['terrain']);die();
if(isset($tile['terrain'])) {
$this->tiles[(string)$tile['id']]['terrain']=(string)$tile['terrain'];
}
if(isset($tile['probability'])) {
$this->tiles[(string)$tile['id']]['probability']=(double)$tile['probability'];
}
/*if(count($this->tiles[(string)$tile['id']])==0) {
unset($this->tiles[(string)$tile['id']]);
}//*/
}
}
if((bool)$xml->properties!==false) {
$this->loadProperties_from_element($xml->properties, $ref);
}
if((bool)$xml->tile!==false) {
$this->loadIdProperties_from_element($xml->xpath('tile'), $ref);
}
return $xml;
}
public function get_tile_from_terrain($id) {
if(is_int($id)) {
return $this->terrains[$id]['tile'];
}
else if(is_string($id)) {
foreach($this->terrains as $terrain) {
if($terrain['name']===$id) return $terrain['tile'];
}
throw new Exception('terrain not found');
}
else {
throw new Exception('bad terrain identifiant.');
}
}
public function get_tile_from_terrains($tl, $tr, $bl, $br) {
$value=$tl.','.$tr.','.$bl.','.$br;
foreach($this->tiles as $id=>$tile) {
if($tile['terrain']===$value) return $id;
}
throw new Exception('terrain tile not found');
}
public function isValid() {
if(!is_string($this->sourceTSX)) {
throw new Exception('Incorrect tileset source.');
return false;
}
if(!is_int($this->firstgid ) || $this->firstgid<1) {
throw new Exception('Incorrect tileset firstgid.');
return false;
}
if(!is_string($this->name)) {
throw new Exception('Incorrect tileset name.');
return false;
}
if(!is_int($this->tilewidth ) || $this->tilewidth <0) {
throw new Exception('Incorrect tileset width .');
return false;
}
if(!is_int($this->tileheight) || $this->tileheight<0) {
throw new Exception('Incorrect tileset height.');
return false;
}
if(!is_int($this->tileoffsetx )) {
throw new Exception('Incorrect tileset tileoffsetx.');
return false;
}
if(!is_int($this->tileoffsety )) {
throw new Exception('Incorrect tileset tileoffsety .');
return false;
}
if(!is_int($this->margin) || $this->margin<0) {
throw new Exception('Incorrect tileset margin.');
return false;
}
if(!is_int($this->spacing) || $this->spacing<0) {
throw new Exception('Incorrect tileset spacing.');
return false;
}
if(!is_string($this->source)) {
throw new Exception('Incorrect tileset source.');
return false;
}
if(!is_string($this->trans) || strlen($this->trans)!=6 || strcspn($this->trans, '0123456789abcdefABCDEF')!=0 ) {
throw new Exception('Incorrect tileset trans.');
return false;
}
if(!is_int($this->width ) || $this->width <0) {
throw new Exception('Incorrect tileset width .');
return false;
}
if(!is_int($this->height) || $this->height<0) {
throw new Exception('Incorrect tileset height.');
return false;
}
if(!is_array($this->terrains)) {
throw new Exception('Incorrect tileset terrains.');
return false;
}
if(!is_array($this->tiles)) {
throw new Exception('Incorrect tileset tiles.');
return false;
}
return true;
}
};
?>