aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/makamys/lodmod/renderer/LODRegion.java
blob: ed13980c07236ea7aa9213c7393cd2db540104d9 (plain)
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
package makamys.lodmod.renderer;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import makamys.lodmod.LODMod;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.common.util.Constants.NBT;

public class LODRegion {
	
	private LODChunk[][] data = new LODChunk[32][32];
	
	int regionX, regionZ;
	
	public LODRegion(int regionX, int regionZ) {
		this.regionX = regionX;
		this.regionZ = regionZ;
		
		for(int i = 0; i < 32; i++) {
			for(int j = 0; j < 32; j++) {
				data[i][j] = new LODChunk(regionX * 32 + i, regionZ * 32 + j);
			}
		}
	}
	
	public LODRegion(int regionX, int regionZ, NBTTagCompound nbt) {
        this.regionX = regionX;
        this.regionZ = regionZ;
        
        NBTTagList list = nbt.getTagList("chunks", NBT.TAG_COMPOUND);
        List<String> stringTable = Arrays.asList(nbt.getString("stringTable").split("\\n"));
        
        int idx = 0;
        for(int i = 0; i < 32; i++) {
            for(int j = 0; j < 32; j++) {
                data[i][j] = new LODChunk(list.getCompoundTagAt(idx++), stringTable);
                if(data[i][j].hasChunkMeshes()) {
                    LODMod.renderer.setVisible(data[i][j], true);
                }
            }
        }        
    }
	
	public static LODRegion load(Path saveDir, int regionX, int regionZ) {
	    if(!LODMod.disableChunkMeshes && LODMod.saveMeshes) {
    	    File saveFile = getSavePath(saveDir, regionX, regionZ).toFile();
    	    if(saveFile.exists()) {
    	        try {
                    NBTTagCompound nbt = CompressedStreamTools.readCompressed(new FileInputStream(saveFile));
                    return new LODRegion(regionX, regionZ, nbt);
                } catch (Exception e) {
                    e.printStackTrace();
                }
    	    }
	    }
	    return new LODRegion(regionX, regionZ);
	}
	
	private static Path getSavePath(Path saveDir, int regionX, int regionZ) {
	    return saveDir.resolve("lod").resolve(regionX + "," + regionZ + ".lod");
	}
	
	public void save(Path saveDir) {
	    if(LODMod.disableChunkMeshes && LODMod.saveMeshes) return;
	    
	    try {
	        File saveFile = getSavePath(saveDir, regionX, regionZ).toFile();
	        saveFile.getParentFile().mkdirs();
	        
	        NBTTagCompound oldNbt = null;
	        NBTTagList oldList = null;
	        List<String> oldStringTable = null;
	        if(saveFile.exists()) {
	           oldNbt = CompressedStreamTools.readCompressed(new FileInputStream(saveFile));
	           oldList = oldNbt.getTagList("chunks", NBT.TAG_COMPOUND);;
	           oldStringTable = Arrays.asList(oldNbt.getString("stringTable").split("\\n"));
	        }
	        
            NBTTagCompound nbt = new NBTTagCompound();
            nbt.setByte("V", (byte)0);
            nbt.setString("stringTable", String.join("\n", (List<String>) ((TextureMap)Minecraft.getMinecraft().getTextureManager().getTexture(TextureMap.locationBlocksTexture)).mapUploadedSprites.keySet().stream().collect(Collectors.toList())));
            
            NBTTagList list = new NBTTagList();
            
            int idx = 0;
            for(int i = 0; i < 32; i++) {
                for(int j = 0; j < 32; j++) {
                    list.appendTag(data[i][j].saveToNBT(oldNbt == null ? null : oldList.getCompoundTagAt(idx++),
                            oldNbt == null? null : oldStringTable));
                }
            }
            nbt.setTag("chunks", list);
            
            new Thread(
                new Runnable() {
                    
                    @Override
                    public void run() {
                        try {
                            CompressedStreamTools.writeCompressed(nbt, new FileOutputStream(saveFile));
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
            }).start();
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	}
	
	public LODChunk getChunkAbsolute(int chunkXAbs, int chunkZAbs) {
		return getChunk(chunkXAbs - regionX * 32, chunkZAbs - regionZ * 32);
	}
	
	public LODChunk getChunk(int x, int z) {
		if(x >= 0 && x < 32 && z >= 0 && z < 32) {
			return data[x][z];
		} else {
			return null;
		}
	}
	
	public LODChunk putChunk(Chunk chunk) {
		int relX = chunk.xPosition - regionX * 32;
		int relZ = chunk.zPosition - regionZ * 32;
		
		if(relX >= 0 && relX < 32 && relZ >= 0 && relZ < 32) {
			data[relX][relZ].receiveChunk(chunk);
			return data[relX][relZ];
		}
		return null;
	}
	
	public boolean tick(Entity player) {
	    int visibleChunks = 0;
		for(int i = 0; i < 32; i++) {
			for(int j = 0; j < 32; j++) {
				LODChunk chunk = data[i][j];
				if(chunk != null) {
					chunk.tick(player);
					if(chunk.visible) {
					    visibleChunks++;
					}
				}
			}
		}
		return visibleChunks > 0;
	}
	
	public void destroy(Path saveDir) {
	    save(saveDir);
	    for(int i = 0; i < 32; i++) {
            for(int j = 0; j < 32; j++) {
                LODChunk chunk = data[i][j];
                if(chunk != null) {
                    chunk.destroy();
                }
            }
        }
	}
	
	public double distanceTaxicab(Entity entity) {
	    double centerX = ((regionX * 32) + 16) * 16;
	    double centerZ = ((regionZ * 32) + 16) * 16;
	    
	    return Math.max(Math.abs(centerX - entity.posX), Math.abs(centerZ - entity.posZ));
	    
	}
	
	@Override
	public String toString() {
	    return "LODRegion(" + regionX + ", " + regionZ + ")";
	}
	
}