summaryrefslogtreecommitdiffstats
path: root/MCServer/Plugins/ChunkWorx/chunkworx_main.lua
blob: 26f9ca62443868409233f887296f87840edb290d (plain) (blame)
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
-- Global variables
PLUGIN = {}	-- Reference to own plugin object
GENERATION_STATE = 0
OPERATION_CODE = 0	-- 0 = generation
CX = 0
CZ = 0
CURRENT = 0
-- AREA Variables
AreaStartX = -10
AreaStartZ = -10
AreaEndX = 10
AreaEndZ = 10
-- RADIAL Variables
RadialX = 0
RadialZ = 0
Radius = 1
-- WORLD
WORK_WORLD = cRoot:Get():GetDefaultWorld():GetName()
WW_instance = cRoot:Get():GetDefaultWorld()
WORLDS = {}

function Initialize(Plugin)
	PLUGIN = Plugin
	
	PLUGIN:SetName("ChunkWorx")
	PLUGIN:SetVersion(4)
	
	PluginManager = cRoot:Get():GetPluginManager()
	PluginManager:AddHook(PLUGIN, cPluginManager.E_PLUGIN_TICK)
	
	Plugin:AddWebTab("(Re)Generation", HandleRequest_Generation)
	
	GENERATION_STATE = 0
	WW_instance = cRoot:Get():GetWorld(WORK_WORLD)
	if (WW_instance == nil) then
		LOG("" .. PLUGIN:GetName() .. " v" .. PLUGIN:GetVersion() .. ": NO WORLD found :(")
	end
	
	PLUGIN.IniFile = cIniFile("ChunkWorx.ini")
	if (PLUGIN.IniFile:ReadFile() == false) then
		PLUGIN.IniFile:HeaderComment("ChunkWorx Save")
		PLUGIN.IniFile:AddKeyName("Area data")
		PLUGIN.IniFile:SetValueI("Area data", "StartX", AreaStartX)
		PLUGIN.IniFile:SetValueI("Area data", "StartZ", AreaStartZ)
		PLUGIN.IniFile:SetValueI("Area data", "EndX", AreaEndX)
		PLUGIN.IniFile:SetValueI("Area data", "EndZ", AreaEndZ)
		PLUGIN.IniFile:AddKeyName("Radial data")
		PLUGIN.IniFile:SetValueI("Radial data", "RadialX", RadialX)
		PLUGIN.IniFile:SetValueI("Radial data", "RadialZ", RadialZ)
		PLUGIN.IniFile:SetValueI("Radial data", "Radius", Radius)
		PLUGIN.IniFile:WriteFile()
	end
	
	AreaStartX = PLUGIN.IniFile:GetValueI("Area data", "StartX")
	AreaStartZ = PLUGIN.IniFile:GetValueI("Area data", "StartZ")
	AreaEndX = PLUGIN.IniFile:GetValueI("Area data", "EndX")
	AreaEndZ = PLUGIN.IniFile:GetValueI("Area data", "EndZ")
	
	RadialX = PLUGIN.IniFile:GetValueI("Radial data", "RadialX")
	RadialZ = PLUGIN.IniFile:GetValueI("Radial data", "RadialZ")
	Radius = PLUGIN.IniFile:GetValueI("Radial data", "Radius")
	
	LOG("Initialized " .. PLUGIN:GetName() .. " v" .. PLUGIN:GetVersion())
	--LOG("Test1: " .. math.fmod(1.5, 1)) - return fractional part!
	return true
end

function OnDisable()
  PLUGIN.IniFile:WriteFile()
  LOG(PLUGIN:GetName() .. " v" .. PLUGIN:GetVersion() .. " is shutting down...")
end

function Tick( DeltaTime )
	if (GENERATION_STATE == 1 or GENERATION_STATE == 3) then
		LOGINFO("" .. PLUGIN:GetName() .. " v" .. PLUGIN:GetVersion() .. ": works STARTED!")
		LOGINFO("" .. PLUGIN:GetName() .. " v" .. PLUGIN:GetVersion() .. ": At world: " .. WORK_WORLD)
		WW_instance = cRoot:Get():GetWorld(WORK_WORLD)
		if (GENERATION_STATE == 1) then GENERATION_STATE = 2 end
		if (GENERATION_STATE == 3) then GENERATION_STATE = 4 end
		
		-- Changing in case coordinates are flipped
		local shifter = AreaEndX
		if (AreaStartX > AreaEndX) then
			AreaEndX = AreaStartX
			AreaStartX = shifter
		end
		shifter = AreaEndZ
		if (AreaStartZ > AreaEndZ) then
			AreaEndZ = AreaStartZ
			AreaStartZ = shifter
		end
		
		CX = AreaStartX
		CZ = AreaStartZ
		CURRENT = 0
		
		if (WW_instance == nil) then
			LOG("" .. PLUGIN:GetName() .. " v" .. PLUGIN:GetVersion() .. ": works ABORTED")
			LOG("" .. PLUGIN:GetName() .. " v" .. PLUGIN:GetVersion() .. ": NO WORLD found :(")
			GENERATION_STATE = 0
		end
	end
	
	
	
	if (GENERATION_STATE == 2 or GENERATION_STATE == 4) then
		if (WW_instance:GetGeneratorQueueLength() < 200
		and WW_instance:GetLightingQueueLength() < 200
		and (WW_instance:GetStorageSaveQueueLength() + WW_instance:GetStorageLoadQueueLength()) < 80) then
			local chunk_sum = (1+ AreaEndX - AreaStartX) * (1+ AreaEndZ - AreaStartZ)
			LOG("" .. PLUGIN:GetName() .. " v" .. PLUGIN:GetVersion() .. ": PROCESSING 100 chunks, (" .. CURRENT .. "/" .. chunk_sum .. ")")
			for C = 1, 100	do
				if (GENERATION_STATE == 2) then WW_instance:GenerateChunk(CX, CZ) end
				if (GENERATION_STATE == 4) then WW_instance:RegenerateChunk(CX, CZ) end
				
				CX = CX + 1
				CURRENT = CURRENT + 1
				if (CX > AreaEndX) then
					CX = AreaStartX
					CZ = CZ + 1
					if (CZ > AreaEndZ) then
						if (GENERATION_STATE == 2) then LOGINFO("" .. PLUGIN:GetName() .. " v" .. PLUGIN:GetVersion() .. " - generation ENDED!") end
						if (GENERATION_STATE == 4) then LOGINFO("" .. PLUGIN:GetName() .. " v" .. PLUGIN:GetVersion() .. " - REgeneration ENDED!") end
						
						GENERATION_STATE = 0
						break
					end
				end
			end
			WW_instance:SaveAllChunks()
			WW_instance:UnloadUnusedChunks()
		end
	end
end