-
Notifications
You must be signed in to change notification settings - Fork 1
/
incarnam.php
158 lines (113 loc) · 3.79 KB
/
incarnam.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
<?php
set_time_limit(-1);
use Arakne\MapParser\Renderer\MapRenderer;
use Swf\Cli\Jar;
use Swf\SwfLoader;
require_once __DIR__.'/vendor/autoload.php';
$pdo = new PDO('mysql:host=127.0.0.1;dbname=araknemu', 'araknemu');
$areas = $pdo->query('select SUBAREA_ID FROM SUBAREA WHERE AREA_ID = 45')->fetchAll();
$areas = array_map(function ($a) { return $a['SUBAREA_ID']; }, $areas);
$cacheDir = __DIR__.'/cache/incarnam';
$dofusClipsDir = __DIR__.'/gfx';
$swfLoader = new SwfLoader(new Jar(__DIR__.'/ffdec_15.1.1/ffdec.jar'));
$mapRenderer = new MapRenderer(
$swfLoader->bulk(glob($dofusClipsDir.'/g*.swf'))->setResultDirectory($cacheDir.'/grounds'),
$swfLoader->bulk(glob($dofusClipsDir.'/o*.swf'))->setResultDirectory($cacheDir.'/objects')
);
if (!is_dir($cacheDir)) {
mkdir($cacheDir, 0777, true);
}
const TILE_SIZE = 256;
const WH_RATE = MapRenderer::DISPLAY_WIDTH / MapRenderer::DISPLAY_HEIGHT;
$Xmin = 0;
$Xmax = 0;
$Ymin = 0;
$Ymax = 0;
$stmt = $pdo->prepare('select mappos from maps where mappos like ?');
foreach ($areas as $area) {
$stmt->execute(['%,%,'.$area]);
while ($map = $stmt->fetch()) {
$pos = array_map('intval', explode(',', $map['mappos']));
if ($pos[0] < $Xmin) {
$Xmin = $pos[0];
}
if ($pos[0] > $Xmax) {
$Xmax = $pos[0];
}
if ($pos[1] < $Ymin) {
$Ymin = $pos[1];
}
if ($pos[1] > $Ymax) {
$Ymax = $pos[1];
}
}
}
function mapByCoords($x, $y) {
global $areas, $pdo, $cacheDir, $mapRenderer;
$query = 'SELECT * FROM maps WHERE mappos IN (';
$pos = [];
foreach ($areas as $area) {
$pos[] = '"'.$x.','.$y.','.$area.'"';
}
$map = $pdo->query('SELECT * FROM maps WHERE mappos IN ('.implode(',', $pos).')')->fetch();
if (!$map) {
return null;
}
if (!file_exists($cacheDir.'/'.$map['id'].'.png')) {
$img = $mapRenderer->render($map['mapData'], 15, 17);
imagepng($img, $cacheDir.'/'.$map['id'].'.png');
imagedestroy($img);
}
return $cacheDir.'/'.$map['id'].'.png';
}
$zoom = $_GET['z'] ?? 0;
$x = $_GET['x'] ?? 0;
$y = $_GET['y'] ?? 0;
if (!is_dir($cacheDir.'/area')) {
mkdir($cacheDir.'/area', 0777, true);
}
$areaFile = $cacheDir.'/area/'.$x.'_'.$y.'_'.$zoom.'.png';
if (file_exists($areaFile)) {
header('Content-Type: image/png');
readfile($areaFile);
exit;
}
$tileRenderer = new \Arakne\MapParser\Renderer\TileRenderer(
$mapRenderer,
function (\Arakne\MapParser\Renderer\MapCoordinates $coordinates) use($areas, $pdo) {
$query = 'SELECT * FROM maps WHERE mappos IN (';
$pos = [];
foreach ($areas as $area) {
$pos[] = '"'.$coordinates->x().','.$coordinates->y().','.$area.'"';
}
return $pdo->query('SELECT * FROM maps WHERE mappos IN ('.implode(',', $pos).')')->fetch();
},
$Xmin,
$Ymin,
$cacheDir
);
$width = $Xmax - $Xmin + 1;
$height = $Ymax - $Ymin + 1;
$realWidth = ($Xmax - $Xmin) * MapRenderer::DISPLAY_WIDTH;
$realHeight = ($Ymax - $Ymin) * MapRenderer::DISPLAY_HEIGHT;
$size = max($realWidth, $realHeight);
// @todo 2^n
$tileCount = ceil($size / TILE_SIZE);
$tileCount /= pow(2, $zoom);
$startX = $x * $tileCount;
$startY = $y * $tileCount;
$img = imagecreatetruecolor(TILE_SIZE, TILE_SIZE);
$subtileSize = TILE_SIZE / $tileCount;
for ($x = 0; $x <= $tileCount; ++$x) {
for ($y = 0; $y <= $tileCount; ++$y) {
$mapTile = $tileRenderer->render($startX + $x, $startY + $y);
$gd = $mapTile->getCore();
imagecopyresampled($img, $gd, $x * $subtileSize, $y * $subtileSize, 0, 0, $subtileSize, $subtileSize, 256, 256);
//imagedestroy($mapTile);
}
}
// Save cache
imagepng($img, $areaFile);
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);