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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
#include "stdafx.h"
#include "net.minecraft.network.packet.h"
#include "net.minecraft.world.entity.h"
#include "net.minecraft.world.entity.player.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.chunk.h"
#include "net.minecraft.world.level.dimension.h"
#include "net.minecraft.world.level.material.h"
#include "net.minecraft.world.level.saveddata.h"
#include "net.minecraft.world.level.storage.h"
#include "net.minecraft.world.level.tile.h"
#include "net.minecraft.world.item.h"
#include "MapItem.h"
#include "net.minecraft.world.inventory.h"
#include "JavaMath.h"
MapItem::MapItem(int id) : ComplexItem(id)
{
setStackedByData(true);
}
shared_ptr<MapItemSavedData> MapItem::getSavedData(short idNum, Level *level)
{
std::wstring id = wstring( L"map_" ) + std::to_wstring(idNum);
shared_ptr<MapItemSavedData> mapItemSavedData = dynamic_pointer_cast<MapItemSavedData>(level->getSavedData(typeid(MapItemSavedData), id));
if (mapItemSavedData == nullptr)
{
// 4J Stu - This call comes from ClientConnection, but i don't see why we should be trying to work out
// the id again when it's passed as a param. In any case that won't work with the new map setup
//int aux = level->getFreeAuxValueFor(L"map");
int aux = idNum;
id = wstring( L"map_" ) + std::to_wstring(aux);
mapItemSavedData = std::make_shared<MapItemSavedData>(id);
level->setSavedData(id, (shared_ptr<SavedData> ) mapItemSavedData);
}
return mapItemSavedData;
}
shared_ptr<MapItemSavedData> MapItem::getSavedData(shared_ptr<ItemInstance> itemInstance, Level *level)
{
MemSect(31);
std::wstring id = wstring( L"map_" ) + std::to_wstring(itemInstance->getAuxValue() );
MemSect(0);
shared_ptr<MapItemSavedData> mapItemSavedData = dynamic_pointer_cast<MapItemSavedData>( level->getSavedData(typeid(MapItemSavedData), id ) );
bool newData = false;
if (mapItemSavedData == nullptr)
{
// 4J Stu - I don't see why we should be trying to work out the id again when it's passed as a param.
// In any case that won't work with the new map setup
//itemInstance->setAuxValue(level->getFreeAuxValueFor(L"map"));
id = wstring( L"map_" ) + std::to_wstring(itemInstance->getAuxValue() );
mapItemSavedData = std::make_shared<MapItemSavedData>(id);
newData = true;
}
mapItemSavedData->scale = 3;
#ifndef _LARGE_WORLDS
// 4J-PB - for Xbox maps, we'll centre them on the origin of the world, since we can fit the whole world in our map
mapItemSavedData->x = 0;
mapItemSavedData->z = 0;
#endif
if( newData )
{
#ifdef _LARGE_WORLDS
int scale = MapItemSavedData::MAP_SIZE * 2 * (1 << mapItemSavedData->scale);
mapItemSavedData->x = Math::round(static_cast<float>(level->getLevelData()->getXSpawn()) / scale) * scale;
mapItemSavedData->z = Math::round(level->getLevelData()->getZSpawn() / scale) * scale;
#endif
mapItemSavedData->dimension = static_cast<byte>(level->dimension->id);
mapItemSavedData->setDirty();
level->setSavedData(id, (shared_ptr<SavedData> ) mapItemSavedData);
}
return mapItemSavedData;
}
void MapItem::update(Level *level, shared_ptr<Entity> player, shared_ptr<MapItemSavedData> data)
{
if ( (level->dimension->id != data->dimension) || !player->instanceof(eTYPE_PLAYER) )
{
// Wrong dimension, abort
return;
}
int w = MapItem::IMAGE_WIDTH;
int h = MapItem::IMAGE_HEIGHT;
int scale = 1 << data->scale;
int xo = data->x;
int zo = data->z;
int xp = Mth::floor(player->x - xo) / scale + w / 2;
int zp = Mth::floor(player->z - zo) / scale + h / 2;
int rad = 128 / scale;
if (level->dimension->hasCeiling)
{
rad /= 2;
}
shared_ptr<MapItemSavedData::HoldingPlayer> hp = data->getHoldingPlayer(dynamic_pointer_cast<Player>(player));
hp->step++;
for (int x = xp - rad + 1; x < xp + rad; x++)
{
if ((x & 15) != (hp->step & 15)) continue;
int yd0 = 255;
int yd1 = 0;
double ho = 0;
for (int z = zp - rad - 1; z < zp + rad; z++)
{
if (x < 0 || z < -1 || x >= w || z >= h) continue;
int xd = x - xp;
int zd = z - zp;
bool ditherBlack = xd * xd + zd * zd > (rad - 2) * (rad - 2);
int xx = (xo / scale + x - w / 2) * scale;
int zz = (zo / scale + z - h / 2) * scale;
int count[256];
memset( count,0,sizeof(int)*256);
LevelChunk *lc = level->getChunkAt(xx, zz);
if(lc->isEmpty()) continue;
int xso = ((xx)) & 15;
int zso = ((zz)) & 15;
int liquidDepth = 0;
double hh = 0;
if (level->dimension->hasCeiling)
{
int ss = xx + zz * 231871;
ss = ss * ss * 31287121 + ss * 11;
if (((ss >> 20) & 1) == 0) count[Tile::dirt_Id] += 10;
else count[Tile::stone_Id] += 10;
hh = 100;
}
else
{
for (int xs = 0; xs < scale; xs++)
{
for (int zs = 0; zs < scale; zs++)
{
int yy = lc->getHeightmap(xs + xso, zs + zso) + 1;
int t = 0;
if (yy > 1)
{
bool ok = false;
do
{
ok = true;
t = lc->getTile(xs + xso, yy - 1, zs + zso);
if (t == 0) ok = false;
else if (yy > 0 && t > 0 && Tile::tiles[t]->material->color == MaterialColor::none)
{
ok = false;
}
if (!ok)
{
yy--;
if (yy <= 0) break;
t = lc->getTile(xs + xso, yy - 1, zs + zso);
}
} while (yy > 0 && !ok);
if (yy > 0 && t != 0 && Tile::tiles[t]->material->isLiquid())
{
int y = yy - 1;
int below = 0;
do
{
below = lc->getTile(xs + xso, y--, zs + zso);
liquidDepth++;
} while (y > 0 && below != 0 && Tile::tiles[below]->material->isLiquid());
}
}
hh += yy / static_cast<double>(scale * scale);
count[t]++;
}
}
}
liquidDepth /= scale * scale;
int best = 0;
int tBest = 0;
for (int j = 0; j < 256; j++)
{
if (count[j] > best)
{
tBest = j;
best = count[j];
}
}
double diff = ((hh - ho) * 4 / (scale + 4)) + (((x + z) & 1) - 0.5) * 0.4;
int br = 1;
if (diff > +0.6) br = 2;
if (diff < -0.6) br = 0;
int col = 0;
if (tBest > 0)
{
MaterialColor *mc = Tile::tiles[tBest]->material->color;
if (mc == MaterialColor::water)
{
diff = (liquidDepth * 0.1) + ((x + z) & 1) * 0.2;
br = 1;
if (diff < 0.5) br = 2;
if (diff > 0.9) br = 0;
}
col = mc->id;
}
ho = hh;
if (z < 0) continue;
if (xd * xd + zd * zd >= rad * rad) continue;
if (ditherBlack && ((x + z) & 1) == 0)
{
continue;
}
byte oldColor = data->colors[x + z * w];
byte newColor = static_cast<byte>(col * 4 + br);
if (oldColor != newColor)
{
if (yd0 > z) yd0 = z;
if (yd1 < z) yd1 = z;
data->colors[x + z * w] = newColor;
}
}
if (yd0 <= yd1)
{
data->setDirty(x, yd0, yd1);
}
}
}
void MapItem::inventoryTick(shared_ptr<ItemInstance> itemInstance, Level *level, shared_ptr<Entity> owner, int slot, bool selected)
{
if (level->isClientSide) return;
shared_ptr<MapItemSavedData> data = getSavedData(itemInstance, level);
if ( owner->instanceof(eTYPE_PLAYER) )
{
shared_ptr<Player> player = dynamic_pointer_cast<Player>(owner);
// 4J Stu - If the player has a map that belongs to another player, then merge the data over and change this map id to the owners id
int ownersAuxValue = level->getAuxValueForMap(player->getXuid(), data->dimension, data->x, data->z, data->scale);
if(ownersAuxValue != itemInstance->getAuxValue() )
{
shared_ptr<MapItemSavedData> ownersData = getSavedData(ownersAuxValue,level);
ownersData->x = data->x;
ownersData->z = data->z;
ownersData->scale = data->scale;
ownersData->dimension = data->dimension;
itemInstance->setAuxValue( ownersAuxValue );
ownersData->tickCarriedBy(player, itemInstance );
ownersData->mergeInMapData(data);
player->inventoryMenu->broadcastChanges();
data = ownersData;
}
else
{
data->tickCarriedBy(player, itemInstance);
}
}
if (selected)
{
update(level, owner, data);
}
}
shared_ptr<Packet> MapItem::getUpdatePacket(shared_ptr<ItemInstance> itemInstance, Level *level, shared_ptr<Player> player)
{
charArray data = MapItem::getSavedData(itemInstance, level)->getUpdatePacket(itemInstance, level, player);
if (data.data == nullptr || data.length == 0) return nullptr;
shared_ptr<Packet> retval = std::make_shared<ComplexItemDataPacket>(static_cast<short>(Item::map->id), static_cast<short>(itemInstance->getAuxValue()), data);
delete data.data;
return retval;
}
void MapItem::onCraftedBy(shared_ptr<ItemInstance> itemInstance, Level *level, shared_ptr<Player> player)
{
wchar_t buf[64];
int mapScale = 3;
#ifdef _LARGE_WORLDS
int scale = MapItemSavedData::MAP_SIZE * 2 * (1 << mapScale);
int centreXC = static_cast<int>(Math::round(player->x / scale) * scale);
int centreZC = static_cast<int>(Math::round(player->z / scale) * scale);
#else
// 4J-PB - for Xbox maps, we'll centre them on the origin of the world, since we can fit the whole world in our map
int centreXC = 0;
int centreZC = 0;
#endif
itemInstance->setAuxValue(level->getAuxValueForMap(player->getXuid(), player->dimension, centreXC, centreZC, mapScale));
swprintf(buf,64,L"map_%d", itemInstance->getAuxValue());
std::wstring id = wstring(buf);
shared_ptr<MapItemSavedData> data = getSavedData(itemInstance->getAuxValue(), level);
// 4J Stu - We only have one map per player per dimension, so don't reset the one that they have
// when a new one is created
if( data == nullptr )
{
data = std::make_shared<MapItemSavedData>(id);
}
level->setSavedData(id, (shared_ptr<SavedData> ) data);
data->scale = mapScale;
// 4J-PB - for Xbox maps, we'll centre them on the origin of the world, since we can fit the whole world in our map
data->x = centreXC;
data->z = centreZC;
data->dimension = static_cast<byte>(level->dimension->id);
data->setDirty();
}
// 4J - Don't want
/*
void appendHoverText(ItemInstance itemInstance, Player player, List<String> lines, boolean advanced) {
MapItemSavedData data = getSavedData(itemInstance, player.level);
if (advanced) {
if (data == null) {
lines.add("Unknown map");
} else {
lines.add("Scaling at 1:" + (1 << data.scale));
lines.add("(Level " + data.scale + "/" + MapItemSavedData.MAX_SCALE + ")");
}
}
}
*/
|