the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#pragma once
2#include "..\Minecraft.World\LevelListener.h"
3#include "..\Minecraft.World\Definitions.h"
4#include "OffsettedRenderList.h"
5#include "..\Minecraft.World\JavaIntHash.h"
6#include "..\Minecraft.World\Level.h"
7#include "ResourceLocation.h"
8#include <xmcore.h>
9#ifdef __PS3__
10#include "C4JSpursJob.h"
11#endif
12class MultiPlayerLevel;
13class Textures;
14class Chunk;
15class Minecraft;
16class TileRenderer;
17class Culler;
18class Entity;
19class TileEntity;
20class Mob;
21class Vec3;
22class Particle;
23class BlockDestructionProgress;
24class IconRegister;
25class Tesselator;
26using namespace std;
27
28// AP - this is a system that works out which chunks actually need to be grouped together via the deferral system when doing chunk::rebuild. Doing this will reduce the number
29// of chunks built in a single group and reduce the chance of seeing through the landscape when digging near the edges/corners of a chunk.
30// I've added another chunk flag to mark a chunk critical so it swipes a bit from the reference count value (goes to 3 bits to 2). This works on Vita because it doesn't have
31// split screen reference counting.
32#ifdef __PSVITA__
33#define _CRITICAL_CHUNKS
34#endif
35
36class LevelRenderer : public LevelListener
37{
38 friend class Chunk;
39
40private:
41 static ResourceLocation MOON_LOCATION;
42 static ResourceLocation MOON_PHASES_LOCATION;
43 static ResourceLocation SUN_LOCATION;
44 static ResourceLocation CLOUDS_LOCATION;
45 static ResourceLocation END_SKY_LOCATION;
46
47public:
48 static const int CHUNK_XZSIZE = 16;
49#ifdef _LARGE_WORLDS
50 static const int CHUNK_SIZE = 16;
51#else
52 static const int CHUNK_SIZE = 16;
53#endif
54 static const int CHUNK_Y_COUNT = Level::maxBuildHeight / CHUNK_SIZE;
55#if defined _XBOX_ONE
56 static const int MAX_COMMANDBUFFER_ALLOCATIONS = 2047 * 1024 * 1024; // Changed to 2047. 4J had set to 512.
57#elif defined __ORBIS__
58 static const int MAX_COMMANDBUFFER_ALLOCATIONS = 448 * 1024 * 1024; // 4J - added - hard limit is 512 so giving a lot of headroom here for fragmentation (have seen 16MB lost to fragmentation in multiplayer crash dump before)
59#elif defined __PS3__
60 static const int MAX_COMMANDBUFFER_ALLOCATIONS = 110 * 1024 * 1024; // 4J - added
61#else
62 static const int MAX_COMMANDBUFFER_ALLOCATIONS = 55 * 1024 * 1024; // 4J - added
63#endif
64public:
65 LevelRenderer(Minecraft *mc, Textures *textures);
66private:
67 void renderStars();
68 void createCloudMesh(); // 4J added
69public:
70 void setLevel(int playerIndex, MultiPlayerLevel *level);
71 void allChanged();
72 void allChanged(int playerIndex);
73
74 // 4J-PB added
75 void AddDLCSkinsToMemTextures();
76public:
77 void renderEntities(Vec3 *cam, Culler *culler, float a);
78 wstring gatherStats1();
79 wstring gatherStats2();
80private:
81 void resortChunks(int xc, int yc, int zc);
82public:
83 int render(shared_ptr<LivingEntity> player, int layer, double alpha, bool updateChunks);
84private:
85 int renderChunks(int from, int to, int layer, double alpha);
86public:
87 int activePlayers(); // 4J - added
88public:
89 void renderSameAsLast(int layer, double alpha);
90 void tick();
91 void renderSky(float alpha);
92 void renderHaloRing(float alpha);
93 void renderClouds(float alpha);
94 bool isInCloud(double x, double y, double z, float alpha);
95 void renderAdvancedClouds(float alpha);
96 bool updateDirtyChunks();
97
98public:
99 void renderHit(shared_ptr<Player> player, HitResult *h, int mode, shared_ptr<ItemInstance> inventoryItem, float a);
100 void renderDestroyAnimation(Tesselator *t, shared_ptr<Player> player, float a);
101 void renderHitOutline(shared_ptr<Player> player, HitResult *h, int mode, float a);
102 void render(AABB *b);
103 void setDirty(int x0, int y0, int z0, int x1, int y1, int z1, Level *level); // 4J - added level param
104 void tileChanged(int x, int y, int z);
105 void tileLightChanged(int x, int y, int z);
106 void setTilesDirty(int x0, int y0, int z0, int x1, int y1, int z1, Level *level); // 4J - added level param
107
108#ifdef __PS3__
109 void cull_SPU(int playerIndex, Culler *culler, float a);
110 void waitForCull_SPU();
111 C4JSpursJobQueue::Port* m_jobPort_CullSPU;
112 C4JSpursJobQueue::Port* m_jobPort_FindNearestChunk;
113 bool m_bSPUCullStarted[4];
114#endif // __PS3__
115 void cull(Culler *culler, float a);
116 void playStreamingMusic(const wstring& name, int x, int y, int z);
117 void playSound(int iSound, double x, double y, double z, float volume, float pitch, float fSoundClipDist=16.0f);
118 void playSound(shared_ptr<Entity> entity,int iSound, double x, double y, double z, float volume, float pitch, float fSoundClipDist=16.0f);
119 void playSoundExceptPlayer(shared_ptr<Player> player, int iSound, double x, double y, double z, float volume, float pitch, float fSoundClipDist=16.0f);
120 void addParticle(ePARTICLE_TYPE eParticleType, double x, double y, double z, double xa, double ya, double za); // 4J added
121 shared_ptr<Particle> addParticleInternal(ePARTICLE_TYPE eParticleType, double x, double y, double z, double xa, double ya, double za); // 4J added
122 void entityAdded(shared_ptr<Entity> entity);
123 void entityRemoved(shared_ptr<Entity> entity);
124 void playerRemoved(shared_ptr<Entity> entity) {} // 4J added - for when a player is removed from the level's player array, not just the entity storage
125 void skyColorChanged();
126 void clear();
127 void globalLevelEvent(int type, int sourceX, int sourceY, int sourceZ, int data);
128 void levelEvent(shared_ptr<Player> source, int type, int x, int y, int z, int data);
129 void destroyTileProgress(int id, int x, int y, int z, int progress);
130 void registerTextures(IconRegister *iconRegister);
131
132 typedef unordered_map<int, vector<shared_ptr<TileEntity> >, IntKeyHash, IntKeyEq> rteMap;
133private:
134
135 // debug
136 int m_freezeticks; // used to freeze the clouds
137
138 // 4J - this block of declarations was scattered round the code but have gathered everything into one place
139 rteMap renderableTileEntities; // 4J - changed - was vector<shared_ptr<TileEntity>, now hashed by chunk so we can find them
140 CRITICAL_SECTION m_csRenderableTileEntities;
141 MultiPlayerLevel *level[4]; // 4J - now one per player
142 Textures *textures;
143 // vector<Chunk *> *sortedChunks[4]; // 4J - removed - not sorting our chunks anymore
144 ClipChunkArray chunks[4]; // 4J - now one per player
145 int lastPlayerCount[4]; // 4J - added
146 int xChunks, yChunks, zChunks;
147 int chunkLists;
148 Minecraft *mc;
149 TileRenderer *tileRenderer[4]; // 4J - now one per player
150 int ticks;
151 int starList, skyList, darkList, haloRingList;
152 int cloudList; // 4J added
153 int xMinChunk, yMinChunk, zMinChunk;
154 int xMaxChunk, yMaxChunk, zMaxChunk;
155 int lastViewDistance;
156 int noEntityRenderFrames;
157 int totalEntities;
158 int renderedEntities;
159 int culledEntities;
160 int chunkFixOffs;
161 vector<Chunk *> _renderChunks;
162 int frame;
163 int repeatList;
164 double xOld[4]; // 4J - now one per player
165 double yOld[4]; // 4J - now one per player
166 double zOld[4]; // 4J - now one per player
167
168 int totalChunks, offscreenChunks, occludedChunks, renderedChunks, emptyChunks;
169 static const int RENDERLISTS_LENGTH = 4; // 4J - added
170 OffsettedRenderList renderLists[RENDERLISTS_LENGTH];
171
172 unordered_map<int, BlockDestructionProgress *> destroyingBlocks;
173 Icon **breakingTextures;
174
175public:
176 void fullyFlagRenderableTileEntitiesToBeRemoved(); // 4J added
177
178 CRITICAL_SECTION m_csDirtyChunks;
179 bool m_nearDirtyChunk;
180
181
182 // 4J - Destroyed Tile Management - these things added so we can track tiles which have been recently destroyed, and
183 // provide temporary collision for them until the render data has been updated to reflect this change
184 class DestroyedTileManager
185 {
186 private:
187 class RecentTile
188 {
189 public:
190 int x;
191 int y;
192 int z;
193 Level *level;
194 AABBList boxes;
195 int timeout_ticks;
196 bool rebuilt;
197 RecentTile(int x, int y, int z, Level *level);
198 ~RecentTile();
199 };
200 CRITICAL_SECTION m_csDestroyedTiles;
201 vector<RecentTile *> m_destroyedTiles;
202 public:
203 void destroyingTileAt( Level *level, int x, int y, int z ); // For game to let this manager know that a tile is about to be destroyed (must be called before it actually is)
204 void updatedChunkAt( Level * level, int x, int y, int z, int veryNearCount ); // For chunk rebuilding to inform the manager that a chunk (a 16x16x16 tile render chunk) has been updated
205 void addAABBs( Level *level, AABB *box, AABBList *boxes ); // For game to get any AABBs that the user should be colliding with as render data has not yet been updated
206 void tick();
207 DestroyedTileManager();
208 ~DestroyedTileManager();
209 };
210 DestroyedTileManager *destroyedTileManager;
211
212 float destroyProgress;
213
214 // 4J - added for new render list handling
215 // This defines the maximum size of renderable level, must be big enough to cope with actual size of level + view distance at each side
216 // so that we can render the "infinite" sea at the edges
217 static const int MAX_LEVEL_RENDER_SIZE[3];
218 static const int DIMENSION_OFFSETS[3];
219 // This is the TOTAL area of columns of chunks to be allocated for render round the players. So for one player, it would be a region of
220 // sqrt(PLAYER_RENDER_AREA) x sqrt(PLAYER_RENDER_AREA)
221#ifdef _LARGE_WORLDS
222 static const int PLAYER_VIEW_DISTANCE = 18; // Straight line distance from centre to extent of visible world
223 static const int PLAYER_RENDER_AREA = (PLAYER_VIEW_DISTANCE * PLAYER_VIEW_DISTANCE * 4);
224#else
225 static const int PLAYER_RENDER_AREA = 400;
226#endif
227
228 static int getDimensionIndexFromId(int id);
229 static int getGlobalIndexForChunk(int x, int y, int z, Level *level);
230 static int getGlobalIndexForChunk(int x, int y, int z, int dimensionId);
231 static bool isGlobalIndexInSameDimension( int idx, Level *level);
232 static int getGlobalChunkCount();
233 static int getGlobalChunkCountForOverworld();
234
235 // Get/set/clear individual flags
236 bool getGlobalChunkFlag(int x, int y, int z, Level *level, unsigned char flag, unsigned char shift = 0);
237 void setGlobalChunkFlag(int x, int y, int z, Level *level, unsigned char flag, unsigned char shift = 0);
238 void setGlobalChunkFlag(int index, unsigned char flag, unsigned char shift = 0);
239 void clearGlobalChunkFlag(int x, int y, int z, Level *level, unsigned char flag, unsigned char shift = 0);
240
241 // Get/set whole byte of flags
242 unsigned char getGlobalChunkFlags(int x, int y, int z, Level *level);
243 void setGlobalChunkFlags(int x, int y, int z, Level *level, unsigned char flags);
244
245 // Reference counting
246 unsigned char incGlobalChunkRefCount(int x, int y, int z, Level *level);
247 unsigned char decGlobalChunkRefCount(int x, int y, int z, Level *level);
248
249 // Actual storage for flags
250 unsigned char *globalChunkFlags;
251
252 // The flag definitions
253 static const int CHUNK_FLAG_COMPILED = 0x01;
254 static const int CHUNK_FLAG_DIRTY = 0x02;
255 static const int CHUNK_FLAG_EMPTY0 = 0x04;
256 static const int CHUNK_FLAG_EMPTY1 = 0x08;
257 static const int CHUNK_FLAG_EMPTYBOTH = 0x0c;
258 static const int CHUNK_FLAG_NOTSKYLIT = 0x10;
259#ifdef _CRITICAL_CHUNKS
260 static const int CHUNK_FLAG_CRITICAL = 0x20;
261 static const int CHUNK_FLAG_CUT_OUT = 0x40;
262 static const int CHUNK_FLAG_REF_MASK = 0x01;
263 static const int CHUNK_FLAG_REF_SHIFT = 7;
264#else
265 static const int CHUNK_FLAG_REF_MASK = 0x07;
266 static const int CHUNK_FLAG_REF_SHIFT = 5;
267#endif
268
269 XLockFreeStack<int> dirtyChunksLockFreeStack;
270
271 bool dirtyChunkPresent;
272 __int64 lastDirtyChunkFound;
273 static const int FORCE_DIRTY_CHUNK_CHECK_PERIOD_MS = 250;
274
275#ifdef _LARGE_WORLDS
276 static const int MAX_CONCURRENT_CHUNK_REBUILDS = 4;
277 static const int MAX_CHUNK_REBUILD_THREADS = MAX_CONCURRENT_CHUNK_REBUILDS - 1;
278 static Chunk permaChunk[MAX_CONCURRENT_CHUNK_REBUILDS];
279 static C4JThread *rebuildThreads[MAX_CHUNK_REBUILD_THREADS];
280 static C4JThread::EventArray *s_rebuildCompleteEvents;
281 static C4JThread::Event *s_activationEventA[MAX_CHUNK_REBUILD_THREADS];
282 static void staticCtor();
283 static int rebuildChunkThreadProc(LPVOID lpParam);
284
285 CRITICAL_SECTION m_csChunkFlags;
286#endif
287 void nonStackDirtyChunksAdded();
288
289 int checkAllPresentChunks(bool *faultFound); // 4J - added for testing
290};