summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.hgignore19
-rw-r--r--.hgtags7
-rw-r--r--CMakeLists.txt12
-rw-r--r--cmake/Modules/FindIrrlicht.cmake2
-rw-r--r--data/chest_front.pngbin0 -> 167 bytes
-rw-r--r--data/chest_side.pngbin0 -> 151 bytes
-rw-r--r--data/chest_top.pngbin0 -> 142 bytes
-rw-r--r--data/cobble.pngbin0 -> 830 bytes
-rw-r--r--data/furnace_front.pngbin0 -> 246 bytes
-rw-r--r--data/furnace_side.pngbin0 -> 236 bytes
-rw-r--r--data/glass.pngbin0 -> 1309 bytes
-rw-r--r--data/grass.pngbin744 -> 874 bytes
-rw-r--r--data/grass_side.pngbin391 -> 878 bytes
-rw-r--r--data/heart.pngbin0 -> 220 bytes
-rw-r--r--data/item_fence.png (renamed from data/tool_stpick.png)bin990 -> 1264 bytes
-rw-r--r--data/menulogo.pngbin0 -> 498 bytes
-rw-r--r--data/oerkki1.pngbin0 -> 212 bytes
-rw-r--r--data/scripts/objects/test/client.lua137
-rw-r--r--data/scripts/objects/test/server.lua322
-rw-r--r--data/sign_wall.pngbin0 -> 489 bytes
-rw-r--r--data/skybox1.pngbin0 -> 236 bytes
-rw-r--r--data/skybox1_dawn.pngbin0 -> 598 bytes
-rw-r--r--data/skybox1_night.pngbin0 -> 2247 bytes
-rw-r--r--data/skybox2.pngbin0 -> 231 bytes
-rw-r--r--data/skybox2_dawn.pngbin0 -> 199 bytes
-rw-r--r--data/skybox2_night.pngbin0 -> 2583 bytes
-rw-r--r--data/skybox3.pngbin0 -> 190 bytes
-rw-r--r--data/skybox3_dawn.pngbin0 -> 190 bytes
-rw-r--r--data/skybox3_night.pngbin0 -> 190 bytes
-rw-r--r--data/steel_block.pngbin0 -> 219 bytes
-rw-r--r--data/steel_ingot.pngbin0 -> 207 bytes
-rw-r--r--data/stick.pngbin947 -> 182 bytes
-rw-r--r--data/stone.pngbin846 -> 806 bytes
-rw-r--r--data/tool_mesepick.pngbin990 -> 252 bytes
-rw-r--r--data/tool_steelaxe.pngbin0 -> 209 bytes
-rw-r--r--data/tool_steelpick.pngbin0 -> 271 bytes
-rw-r--r--data/tool_steelshovel.pngbin0 -> 216 bytes
-rw-r--r--data/tool_stoneaxe.pngbin0 -> 193 bytes
-rw-r--r--data/tool_stonepick.pngbin0 -> 262 bytes
-rw-r--r--data/tool_stoneshovel.pngbin0 -> 203 bytes
-rw-r--r--data/tool_woodaxe.pngbin0 -> 190 bytes
-rw-r--r--data/tool_woodpick.pngbin0 -> 245 bytes
-rw-r--r--data/tool_woodshovel.pngbin0 -> 203 bytes
-rw-r--r--data/tool_wpick.pngbin989 -> 0 bytes
-rw-r--r--data/water.pngbin1033 -> 518 bytes
-rw-r--r--doc/README.txt13
-rw-r--r--doc/changelog.txt53
-rwxr-xr-xgenmap.py89
-rw-r--r--minetest.conf.example89
-rw-r--r--misc/map.cpp6393
-rw-r--r--misc/noise.cpp410
-rw-r--r--misc/noise.h108
-rw-r--r--pnoise.py102
-rw-r--r--src/CMakeLists.txt24
-rw-r--r--src/activeobject.h4
-rw-r--r--src/auth.cpp264
-rw-r--r--src/auth.h101
-rw-r--r--src/base64.cpp124
-rw-r--r--src/base64.h4
-rw-r--r--src/client.cpp1148
-rw-r--r--src/client.h275
-rw-r--r--src/clientobject.cpp874
-rw-r--r--src/clientobject.h226
-rw-r--r--src/clientserver.h80
-rw-r--r--src/clouds.cpp157
-rw-r--r--src/clouds.h83
-rw-r--r--src/cmake_config.h.in2
-rw-r--r--src/collision.cpp185
-rw-r--r--src/collision.h (renamed from src/iirrlichtwrapper.h)56
-rw-r--r--src/common_irrlicht.h5
-rw-r--r--src/connection.cpp26
-rw-r--r--src/connection.h19
-rw-r--r--src/constants.h3
-rw-r--r--src/debug.cpp1
-rw-r--r--src/debug.h5
-rw-r--r--src/defaultsettings.cpp34
-rw-r--r--src/environment.cpp945
-rw-r--r--src/environment.h192
-rw-r--r--src/filesys.cpp19
-rw-r--r--src/filesys.h3
-rw-r--r--src/game.cpp2153
-rw-r--r--src/game.h77
-rw-r--r--src/guiFurnaceMenu.cpp58
-rw-r--r--src/guiFurnaceMenu.h44
-rw-r--r--src/guiInventoryMenu.cpp121
-rw-r--r--src/guiInventoryMenu.h68
-rw-r--r--src/guiMainMenu.cpp185
-rw-r--r--src/guiMainMenu.h14
-rw-r--r--src/guiPasswordChange.cpp250
-rw-r--r--src/guiPasswordChange.h55
-rw-r--r--src/guiPauseMenu.cpp36
-rw-r--r--src/guiPauseMenu.h1
-rw-r--r--src/inventory.cpp392
-rw-r--r--src/inventory.h204
-rw-r--r--src/irrlichtwrapper.cpp452
-rw-r--r--src/irrlichtwrapper.h187
-rw-r--r--src/keycode.cpp192
-rw-r--r--src/keycode.h31
-rw-r--r--src/light.cpp64
-rw-r--r--src/lua/CMakeLists.txt109
-rw-r--r--src/lua/COPYRIGHT34
-rw-r--r--src/lua/cmake/CustomMacros.cmake14
-rw-r--r--src/lua/src/CMakeLists.txt65
-rw-r--r--src/lua/src/lapi.c1087
-rw-r--r--src/lua/src/lapi.h16
-rw-r--r--src/lua/src/lauxlib.c652
-rw-r--r--src/lua/src/lauxlib.h174
-rw-r--r--src/lua/src/lbaselib.c653
-rw-r--r--src/lua/src/lcode.c839
-rw-r--r--src/lua/src/lcode.h76
-rw-r--r--src/lua/src/ldblib.c397
-rw-r--r--src/lua/src/ldebug.c638
-rw-r--r--src/lua/src/ldebug.h33
-rw-r--r--src/lua/src/ldo.c518
-rw-r--r--src/lua/src/ldo.h57
-rw-r--r--src/lua/src/ldump.c164
-rw-r--r--src/lua/src/lfunc.c174
-rw-r--r--src/lua/src/lfunc.h34
-rw-r--r--src/lua/src/lgc.c711
-rw-r--r--src/lua/src/lgc.h110
-rw-r--r--src/lua/src/linit.c38
-rw-r--r--src/lua/src/liolib.c553
-rw-r--r--src/lua/src/llex.c461
-rw-r--r--src/lua/src/llex.h81
-rw-r--r--src/lua/src/llimits.h128
-rw-r--r--src/lua/src/lmathlib.c263
-rw-r--r--src/lua/src/lmem.c86
-rw-r--r--src/lua/src/lmem.h49
-rw-r--r--src/lua/src/loadlib.c666
-rw-r--r--src/lua/src/lobject.c214
-rw-r--r--src/lua/src/lobject.h381
-rw-r--r--src/lua/src/lopcodes.c102
-rw-r--r--src/lua/src/lopcodes.h268
-rw-r--r--src/lua/src/loslib.c243
-rw-r--r--src/lua/src/lparser.c1339
-rw-r--r--src/lua/src/lparser.h82
-rw-r--r--src/lua/src/lstate.c214
-rw-r--r--src/lua/src/lstate.h169
-rw-r--r--src/lua/src/lstring.c111
-rw-r--r--src/lua/src/lstring.h31
-rw-r--r--src/lua/src/lstrlib.c869
-rw-r--r--src/lua/src/ltable.c588
-rw-r--r--src/lua/src/ltable.h40
-rw-r--r--src/lua/src/ltablib.c287
-rw-r--r--src/lua/src/ltm.c75
-rw-r--r--src/lua/src/ltm.h54
-rw-r--r--src/lua/src/lua.c392
-rw-r--r--src/lua/src/lua.h388
-rw-r--r--src/lua/src/luac.c200
-rw-r--r--src/lua/src/luaconf.h763
-rw-r--r--src/lua/src/lualib.h53
-rw-r--r--src/lua/src/lundump.c227
-rw-r--r--src/lua/src/lundump.h36
-rw-r--r--src/lua/src/lvm.c763
-rw-r--r--src/lua/src/lvm.h36
-rw-r--r--src/lua/src/lzio.c82
-rw-r--r--src/lua/src/lzio.h67
-rw-r--r--src/lua/src/print.c227
-rw-r--r--src/main.cpp2608
-rw-r--r--src/main.h110
-rw-r--r--src/map.cpp1703
-rw-r--r--src/map.h139
-rw-r--r--src/mapblock.cpp1283
-rw-r--r--src/mapblock.h190
-rw-r--r--src/mapblockobject.cpp21
-rw-r--r--src/mapblockobject.h12
-rw-r--r--src/mapchunk.h7
-rw-r--r--src/mapnode.cpp115
-rw-r--r--src/mapnode.h71
-rw-r--r--src/materials.cpp78
-rw-r--r--src/mineral.cpp2
-rw-r--r--src/mineral.h3
-rw-r--r--src/modalMenu.h9
-rw-r--r--src/nodemetadata.cpp512
-rw-r--r--src/nodemetadata.h163
-rw-r--r--src/noise.cpp138
-rw-r--r--src/noise.h30
-rw-r--r--src/player.cpp69
-rw-r--r--src/player.h21
-rw-r--r--src/porting.cpp4
-rw-r--r--src/profiler.h131
-rw-r--r--src/serialization.cpp6
-rw-r--r--src/serialization.h46
-rw-r--r--src/server.cpp2523
-rw-r--r--src/server.h209
-rw-r--r--src/servercommand.cpp236
-rw-r--r--src/servercommand.h60
-rw-r--r--src/servermain.cpp5
-rw-r--r--src/serverobject.cpp1117
-rw-r--r--src/serverobject.h215
-rw-r--r--src/sha1.cpp207
-rw-r--r--src/sha1.h51
-rw-r--r--src/socket.cpp2
-rw-r--r--src/staticobject.h167
-rw-r--r--src/test.cpp24
-rw-r--r--src/texture.h134
-rw-r--r--src/tile.cpp66
-rw-r--r--src/tile.h19
-rw-r--r--src/utility.cpp39
-rw-r--r--src/utility.h299
-rw-r--r--src/voxel.h30
201 files changed, 22687 insertions, 24567 deletions
diff --git a/.hgignore b/.hgignore
new file mode 100644
index 000000000..0870e3c67
--- /dev/null
+++ b/.hgignore
@@ -0,0 +1,19 @@
+map/*
+world/*
+CMakeFiles/*
+src/CMakeFiles/*
+src/Makefile
+src/cmake_config.h
+src/cmake_install.cmake
+src/jthread/CMakeFiles/*
+src/jthread/Makefile
+src/jthread/cmake_config.h
+src/jthread/cmake_install.cmake
+minetest.conf
+bin/
+CMakeCache.txt
+CPackConfig.cmake
+CPackSourceConfig.cmake
+Makefile
+cmake_install.cmake
+src/jthread/libjthread.a
diff --git a/.hgtags b/.hgtags
index 215b1d074..b555b807d 100644
--- a/.hgtags
+++ b/.hgtags
@@ -3,3 +3,10 @@ c37bcfd89dd627fdb131ae3f77fcaab02721bf76 working
e3c3c8e27bbc8c9b61710517a78944deb1c61696 110211211322
23880c78e40c50ad54fcd8844510f7a423b37f2a 110212200513
20c49c98c92a62df457b773c562df41d4167492b 110214175330
+10be2b71f965585af90af96903e83b4ddff52bf9 20110424_0
+9b05d4bfee9312aef4182fa6f63b4237368cec34 0.2.20110529_0
+6fa0a8b40406aa567f8fa84b5e2045a7e3762c1d 0.2.20110529_1
+cf6dd618ef0b7514c81ae87749733b5a328fc763 0.2.20110529_2
+96efc17b4cd92aacbe947b520a6ba91727d42f03 0.2.20110602_0
+0000000000000000000000000000000000000000 0.2.20110602_0
+6f17cd3f6c5481e6abc906fc441980c764632cbc 0.2.20110602_0
diff --git a/CMakeLists.txt b/CMakeLists.txt
index da820dcc1..c1074e6d3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -8,8 +8,8 @@ endif(${CMAKE_VERSION} STREQUAL "2.8.2")
project(minetest)
set(VERSION_MAJOR 0)
-set(VERSION_MINOR 0)
-set(VERSION_PATCH 1)
+set(VERSION_MINOR 2)
+set(VERSION_PATCH 20110602_0)
set(VERSION_STRING "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
# Configuration options
@@ -53,10 +53,10 @@ elseif(APPLE)
set(DOCDIR "share/doc/minetest")
set(EXAMPLE_CONF_DIR ".")
elseif(UNIX) # Linux, BSD etc
- set(DATADIR "usr/share/minetest")
- set(BINDIR "usr/bin")
- set(DOCDIR "usr/share/doc/minetest")
- set(EXAMPLE_CONF_DIR "usr/share/doc/minetest")
+ set(DATADIR "share/minetest")
+ set(BINDIR "bin")
+ set(DOCDIR "share/doc/minetest")
+ set(EXAMPLE_CONF_DIR "share/doc/minetest")
endif()
install(FILES "doc/README.txt" DESTINATION "${DOCDIR}")
diff --git a/cmake/Modules/FindIrrlicht.cmake b/cmake/Modules/FindIrrlicht.cmake
index 63881000f..bd0042262 100644
--- a/cmake/Modules/FindIrrlicht.cmake
+++ b/cmake/Modules/FindIrrlicht.cmake
@@ -23,7 +23,7 @@ if(NOT IRRLICHT_SOURCE_DIR STREQUAL "")
set(IRRLICHT_LIBRARY_NAMES Irrlicht.lib)
else()
set(IRRLICHT_SOURCE_DIR_LIBS "${IRRLICHT_SOURCE_DIR}/lib/Win32-gcc")
- set(IRRLICHT_LIBRARY_NAMES libIrrlicht.a)
+ set(IRRLICHT_LIBRARY_NAMES libIrrlicht.a libIrrlicht.dll.a)
endif()
else()
set(IRRLICHT_SOURCE_DIR_LIBS "${IRRLICHT_SOURCE_DIR}/lib/Linux")
diff --git a/data/chest_front.png b/data/chest_front.png
new file mode 100644
index 000000000..c5628af94
--- /dev/null
+++ b/data/chest_front.png
Binary files differ
diff --git a/data/chest_side.png b/data/chest_side.png
new file mode 100644
index 000000000..916dd784d
--- /dev/null
+++ b/data/chest_side.png
Binary files differ
diff --git a/data/chest_top.png b/data/chest_top.png
new file mode 100644
index 000000000..58c79674d
--- /dev/null
+++ b/data/chest_top.png
Binary files differ
diff --git a/data/cobble.png b/data/cobble.png
new file mode 100644
index 000000000..7d0447419
--- /dev/null
+++ b/data/cobble.png
Binary files differ
diff --git a/data/furnace_front.png b/data/furnace_front.png
new file mode 100644
index 000000000..1620a27c3
--- /dev/null
+++ b/data/furnace_front.png
Binary files differ
diff --git a/data/furnace_side.png b/data/furnace_side.png
new file mode 100644
index 000000000..63cb162fe
--- /dev/null
+++ b/data/furnace_side.png
Binary files differ
diff --git a/data/glass.png b/data/glass.png
new file mode 100644
index 000000000..8598ce670
--- /dev/null
+++ b/data/glass.png
Binary files differ
diff --git a/data/grass.png b/data/grass.png
index a8071b501..3610bb2bc 100644
--- a/data/grass.png
+++ b/data/grass.png
Binary files differ
diff --git a/data/grass_side.png b/data/grass_side.png
index 3971aaf8d..4f4f680be 100644
--- a/data/grass_side.png
+++ b/data/grass_side.png
Binary files differ
diff --git a/data/heart.png b/data/heart.png
new file mode 100644
index 000000000..ddd273dd2
--- /dev/null
+++ b/data/heart.png
Binary files differ
diff --git a/data/tool_stpick.png b/data/item_fence.png
index 9ca3a5e03..76a461878 100644
--- a/data/tool_stpick.png
+++ b/data/item_fence.png
Binary files differ
diff --git a/data/menulogo.png b/data/menulogo.png
new file mode 100644
index 000000000..76595c48d
--- /dev/null
+++ b/data/menulogo.png
Binary files differ
diff --git a/data/oerkki1.png b/data/oerkki1.png
new file mode 100644
index 000000000..c32fb99db
--- /dev/null
+++ b/data/oerkki1.png
Binary files differ
diff --git a/data/scripts/objects/test/client.lua b/data/scripts/objects/test/client.lua
deleted file mode 100644
index a685721a1..000000000
--- a/data/scripts/objects/test/client.lua
+++ /dev/null
@@ -1,137 +0,0 @@
--- Client-side code of the test lua object
-
---
--- Some helper functions and classes
---
-
-function split(str, pat)
- local t = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
-end
-
--- For debugging
-function dump(o)
- if type(o) == 'table' then
- local s = '{ '
- for k,v in pairs(o) do
- if type(k) ~= 'number' then k = '"'..k..'"' end
- s = s .. '['..k..'] = ' .. dump(v) .. ','
- end
- return s .. '} '
- else
- return tostring(o)
- end
-end
-
-function vector_subtract(a, b)
- return {X=a.X-b.X, Y=a.Y-b.Y, Z=a.Z-b.Z}
-end
-
-function vector_add(a, b)
- return {X=a.X+b.X, Y=a.Y+b.Y, Z=a.Z+b.Z}
-end
-
-function vector_multiply(a, d)
- return {X=a.X*d, Y=a.Y*d, Z=a.Z*d}
-end
-
-SmoothTranslator = {}
-SmoothTranslator.__index = SmoothTranslator
-
-function SmoothTranslator.create()
- local obj = {}
- setmetatable(obj, SmoothTranslator)
- obj.vect_old = {X=0, Y=0, Z=0}
- obj.anim_counter = 0
- obj.anim_time = 0
- obj.anim_time_counter = 0
- obj.vect_show = {X=0, Y=0, Z=0}
- obj.vect_aim = {X=0, Y=0, Z=0}
- return obj
-end
-
-function SmoothTranslator:update(vect_new)
- self.vect_old = self.vect_show
- self.vect_aim = vect_new
- if self.anim_time < 0.001 or self.anim_time > 1.0 then
- self.anim_time = self.anim_time_counter
- else
- self.anim_time = self.anim_time * 0.9 + self.anim_time_counter * 0.1
- end
- self.anim_time_counter = 0
- self.anim_counter = 0
-end
-
-function SmoothTranslator:translate(dtime)
- self.anim_time_counter = self.anim_time_counter + dtime
- self.anim_counter = self.anim_counter + dtime
- vect_move = vector_subtract(self.vect_aim, self.vect_old)
- moveratio = 1.0
- if self.anim_time > 0.001 then
- moveratio = self.anim_time_counter / self.anim_time
- end
- -- Move a bit less than should, to avoid oscillation
- moveratio = moveratio * 0.8
- if moveratio > 1.5 then
- moveratio = 1.5
- end
- self.vect_show = vector_add(self.vect_old, vector_multiply(vect_move, moveratio))
-end
-
---
--- Actual code
---
-
-pos_trans = SmoothTranslator.create()
-rot_trans = SmoothTranslator.create()
-
--- Callback functions
-
-function on_step(self, dtime)
- pos_trans:translate(dtime)
- rot_trans:translate(dtime)
- object_set_position(self, pos_trans.vect_show)
- object_set_rotation(self, rot_trans.vect_show)
-end
-
-function on_process_message(self, data)
- --print("client got message: " .. data)
-
- -- Receive our custom messages
-
- sp = split(data, " ")
- if sp[1] == "pos" then
- pos_trans:update({X=sp[2], Y=sp[3], Z=sp[4]})
- end
- if sp[1] == "rot" then
- rot_trans:update({X=sp[2], Y=sp[3], Z=sp[4]})
- end
-end
-
-function on_initialize(self, data)
- print("client object got initialization: " .. data)
-
- corners = {
- {-1/2,-1/2, 0},
- { 1/2,-1/2, 0},
- { 1/2, 0, 0},
- {-1/2, 0, 0},
- }
- object_add_to_mesh(self, "rat.png", corners, false)
-
-end
-
diff --git a/data/scripts/objects/test/server.lua b/data/scripts/objects/test/server.lua
deleted file mode 100644
index 1213e2fe6..000000000
--- a/data/scripts/objects/test/server.lua
+++ /dev/null
@@ -1,322 +0,0 @@
--- Server-side code of the test lua object
-
---
--- Some helper functions and classes
---
-
--- For debugging
-function dump(o)
- if type(o) == 'table' then
- local s = '{ '
- for k,v in pairs(o) do
- if type(k) ~= 'number' then k = '"'..k..'"' end
- s = s .. '['..k..'] = ' .. dump(v) .. ','
- end
- return s .. '} '
- else
- return tostring(o)
- end
-end
-
-function table.copy(t)
- local t2 = {}
- for k,v in pairs(t) do
- t2[k] = v
- end
- return t2
-end
-
-function vector_zero()
- return {X=0,Y=0,Z=0}
-end
-
-function vector_subtract(a, b)
- return {X=a.X-b.X, Y=a.Y-b.Y, Z=a.Z-b.Z}
-end
-
-function vector_add(a, b)
- return {X=a.X+b.X, Y=a.Y+b.Y, Z=a.Z+b.Z}
-end
-
-function vector_multiply(a, d)
- return {X=a.X*d, Y=a.Y*d, Z=a.Z*d}
-end
-
-function vector_copy(a)
- return {X=a.X, Y=a.Y, Z=a.Z}
-end
-
-function vector_length(a)
- return math.sqrt(a.X*a.X + a.Y*a.Y + a.Z*a.Z)
-end
-
-function vector_eq(a, b)
- return (a.X==b.X and a.Y==b.Y and a.Z==b.Z)
-end
-
-function round(num, idp)
- local mult = 10^(idp or 0)
- return math.floor(num * mult + 0.5) / mult
-end
-
-function vector_snap(a)
- return {X=round(a.X, 0), Y=round(a.Y, 0), Z=round(a.Z, 0)}
-end
-
---
--- Actual code
---
-
-CONTENT_STONE = 0
-
-is_digger = false
-counter = 0
-counter2 = 0
-counter3 = 0
-counter4 = 0
-counter_move = 0
-death_counter = 0
--- This is set in on_initialize()
-position = {X=0,Y=0,Z=0}
-starting_position = {X=0,Y=0,Z=0}
-rotation = {X=0, Y=math.random(0,360), Z=0}
-y_dir = 1
-temp1 = 0
-speed = 1.5
-main_dir = {X=0,Y=0,Z=0}
-
-function dir_goodness(env, pos, dir)
- if vector_eq(dir, vector_zero()) then
- return -1
- end
- p = vector_add(pos, dir)
- n = env_get_node(env, p)
- f = get_content_features(n.content)
- if f.walkable then
- p.Y = p.Y + 1
- n = env_get_node(env, p)
- f = get_content_features(n.content)
- if f.walkable then
- -- Too high
- return -1
- end
- -- Hill
- return 2
- end
- p.Y = p.Y - 1
- n = env_get_node(env, p)
- f = get_content_features(n.content)
- if f.walkable then
- -- Flat
- return 1
- end
- -- Drop
- return 0
-end
-
-function on_step(self, dtime)
- -- Limit step to a sane value; it jumps a lot while the map generator
- -- is in action
- if dtime > 0.5 then
- dtime = 0.5
- end
-
- env = object_get_environment(self)
-
- --[[
- -- Returned value has these fields:
- -- * int content
- -- * int param1
- -- * int param2
- p = {X=position.X, Y=position.Y-0.35, Z=position.Z}
- n = env_get_node(env, p)
- f = get_content_features(n.content)
- if f.walkable then
- y_dir = 1
- else
- y_dir = -1
- end
- -- Keep the object approximately at ground level
- position.Y = position.Y + dtime * 2.0 * y_dir
-
- -- Move the object around
- position.X = position.X + math.cos(math.pi+rotation.Y/180*math.pi)
- * dtime * speed
- position.Z = position.Z + math.sin(math.pi+rotation.Y/180*math.pi)
- * dtime * speed
-
- -- Rotate the object if it is too far from the starting point
- counter3 = counter3 - dtime
- if counter3 < 0 then
- counter3 = counter3 + 1
- diff = vector_subtract(position, starting_position)
- d = vector_length(diff)
- --print("pos="..dump(position).." starting="..dump(starting_position))
- --print("diff=" .. dump(diff))
- --print("d=" .. d)
- if d > 3 then
- rotation.Y = rotation.Y + 90
- --rotation.Y = rotation.Y + math.random(-180, 180)
- end
- end
-
- -- This value has to be set; it determines to which player the
- -- object is near to and such
- object_set_base_position(self, position)
-
- counter4 = counter4 - dtime
- if counter4 < 0 then
- --counter4 = counter4 + math.random(0.5,8)
- counter4 = counter4 + 0.6/speed
- -- Mess around with the map
- if is_digger == true then
- np = vector_add(position, {X=0,Y=-0.6,Z=0})
- env_dig_node(env, np)
- else
- np = vector_add(position, {X=0,Y=0,Z=0})
- env_place_node(env, np, {content=0})
- end
- end
- --]]
-
- counter_move = counter_move - dtime
- if counter_move < 0 then
- counter_move = counter_move + 1/speed
- if counter_move < 0 then counter_move = 0 end
-
- old_position = vector_copy(position)
-
- dirs = {
- {X=1, Y=0, Z=0},
- {X=-1, Y=0, Z=0},
- {X=0, Y=0, Z=1},
- {X=0, Y=0, Z=-1}
- }
-
- best_dir = main_dir
- best_goodness = dir_goodness(env, position, main_dir)
-
- for k,v in ipairs(dirs) do
- -- Don't go directly backwards
- if not vector_eq(vector_subtract(vector_zero(), v), main_dir) then
- goodness = dir_goodness(env, position, v)
- if goodness > best_goodness then
- best_dir = v
- goodness = best_goodness
- end
- end
- end
-
- -- Place stone block when dir changed
- if not vector_eq(main_dir, best_dir) then
- np = vector_add(position, {X=0,Y=0,Z=0})
- env_place_node(env, np, {content=CONTENT_STONE})
- end
-
- main_dir = best_dir
-
- position = vector_add(position, main_dir)
-
- pos_diff = vector_subtract(position, old_position)
- rotation.Y = math.atan2(pos_diff.Z, pos_diff.X)/math.pi*180-180
-
- -- Returned value has these fields:
- -- * int content
- -- * int param1
- -- * int param2
- p = {X=position.X, Y=position.Y, Z=position.Z}
- n = env_get_node(env, p)
- f = get_content_features(n.content)
- if f.walkable then
- position.Y = position.Y + 1
- end
- p = {X=position.X, Y=position.Y-1, Z=position.Z}
- n = env_get_node(env, p)
- f = get_content_features(n.content)
- if not f.walkable then
- position.Y = position.Y - 1
- end
-
- -- Center in the middle of the node
- position = vector_snap(position)
- end
-
- -- This value has to be set; it determines to which player the
- -- object is near to and such
- object_set_base_position(self, position)
-
- --[[
- counter4 = counter4 - dtime
- if counter4 < 0 then
- --counter4 = counter4 + math.random(0.5,8)
- counter4 = counter4 + 0.6/speed
- -- Mess around with the map
- if is_digger == true then
- np = vector_add(position, {X=0,Y=-0.6,Z=0})
- env_dig_node(env, np)
- else
- np = vector_add(position, {X=0,Y=0,Z=0})
- env_place_node(env, np, {content=0})
- end
- end
- --]]
-
- ---[[
- -- Send some custom messages at a custom interval
-
- counter = counter - dtime
- if counter < 0 then
- counter = counter + 0.25
- if counter < 0 then
- counter = 0
- end
-
- message = "pos " .. position.X .. " " .. position.Y .. " " .. position.Z
- object_add_message(self, message)
-
- message = "rot " .. rotation.X .. " " .. rotation.Y .. " " .. rotation.Z
- object_add_message(self, message)
- end
- --]]
-
- -- Remove the object after some time
- death_counter = death_counter + dtime
- if death_counter > 40 then
- object_remove(self)
- end
-
-end
-
--- This stuff is passed to a newly created client-side counterpart of this object
-function on_get_client_init_data(self)
- -- Just return some data for testing
- return "result of get_client_init_data"
-end
-
--- This should return some data that mostly saves the state of this object
--- Not completely implemented yet
-function on_get_server_init_data(self)
- -- Just return some data for testing
- return "result of get_server_init_data"
-end
-
--- When the object is loaded from scratch, this is called before the first
--- on_step(). Data is an empty string or the output of an object spawner
--- hook, but such things are not yet implemented.
---
--- At reload time, the last output of get_server_init_data is passed as data.
---
--- This should initialize the position of the object to the value returned
--- by object_get_base_position(self)
---
--- Not completely implemented yet
---
-function on_initialize(self, data)
- print("server object got initialization: " .. data)
- position = object_get_base_position(self)
- starting_position = vector_copy(position);
- if math.random() < 0.5 then
- is_digger = true
- end
-end
-
diff --git a/data/sign_wall.png b/data/sign_wall.png
new file mode 100644
index 000000000..06eac1ee9
--- /dev/null
+++ b/data/sign_wall.png
Binary files differ
diff --git a/data/skybox1.png b/data/skybox1.png
new file mode 100644
index 000000000..9801d5f4f
--- /dev/null
+++ b/data/skybox1.png
Binary files differ
diff --git a/data/skybox1_dawn.png b/data/skybox1_dawn.png
new file mode 100644
index 000000000..9711c4781
--- /dev/null
+++ b/data/skybox1_dawn.png
Binary files differ
diff --git a/data/skybox1_night.png b/data/skybox1_night.png
new file mode 100644
index 000000000..32e43a63e
--- /dev/null
+++ b/data/skybox1_night.png
Binary files differ
diff --git a/data/skybox2.png b/data/skybox2.png
new file mode 100644
index 000000000..a8c94b4d2
--- /dev/null
+++ b/data/skybox2.png
Binary files differ
diff --git a/data/skybox2_dawn.png b/data/skybox2_dawn.png
new file mode 100644
index 000000000..a761dff2d
--- /dev/null
+++ b/data/skybox2_dawn.png
Binary files differ
diff --git a/data/skybox2_night.png b/data/skybox2_night.png
new file mode 100644
index 000000000..beb07a9cf
--- /dev/null
+++ b/data/skybox2_night.png
Binary files differ
diff --git a/data/skybox3.png b/data/skybox3.png
new file mode 100644
index 000000000..2776ec712
--- /dev/null
+++ b/data/skybox3.png
Binary files differ
diff --git a/data/skybox3_dawn.png b/data/skybox3_dawn.png
new file mode 100644
index 000000000..22c8cbef5
--- /dev/null
+++ b/data/skybox3_dawn.png
Binary files differ
diff --git a/data/skybox3_night.png b/data/skybox3_night.png
new file mode 100644
index 000000000..bb509789b
--- /dev/null
+++ b/data/skybox3_night.png
Binary files differ
diff --git a/data/steel_block.png b/data/steel_block.png
new file mode 100644
index 000000000..8e2020081
--- /dev/null
+++ b/data/steel_block.png
Binary files differ
diff --git a/data/steel_ingot.png b/data/steel_ingot.png
new file mode 100644
index 000000000..f6c9414e7
--- /dev/null
+++ b/data/steel_ingot.png
Binary files differ
diff --git a/data/stick.png b/data/stick.png
index 7a4663cc3..2d31797f0 100644
--- a/data/stick.png
+++ b/data/stick.png
Binary files differ
diff --git a/data/stone.png b/data/stone.png
index d085cb8dd..cad0dbe2b 100644
--- a/data/stone.png
+++ b/data/stone.png
Binary files differ
diff --git a/data/tool_mesepick.png b/data/tool_mesepick.png
index 886f4b21c..a1f3812e0 100644
--- a/data/tool_mesepick.png
+++ b/data/tool_mesepick.png
Binary files differ
diff --git a/data/tool_steelaxe.png b/data/tool_steelaxe.png
new file mode 100644
index 000000000..390dbb087
--- /dev/null
+++ b/data/tool_steelaxe.png
Binary files differ
diff --git a/data/tool_steelpick.png b/data/tool_steelpick.png
new file mode 100644
index 000000000..7982dafeb
--- /dev/null
+++ b/data/tool_steelpick.png
Binary files differ
diff --git a/data/tool_steelshovel.png b/data/tool_steelshovel.png
new file mode 100644
index 000000000..ed8413846
--- /dev/null
+++ b/data/tool_steelshovel.png
Binary files differ
diff --git a/data/tool_stoneaxe.png b/data/tool_stoneaxe.png
new file mode 100644
index 000000000..0c5414af5
--- /dev/null
+++ b/data/tool_stoneaxe.png
Binary files differ
diff --git a/data/tool_stonepick.png b/data/tool_stonepick.png
new file mode 100644
index 000000000..b34de6f32
--- /dev/null
+++ b/data/tool_stonepick.png
Binary files differ
diff --git a/data/tool_stoneshovel.png b/data/tool_stoneshovel.png
new file mode 100644
index 000000000..ba5243101
--- /dev/null
+++ b/data/tool_stoneshovel.png
Binary files differ
diff --git a/data/tool_woodaxe.png b/data/tool_woodaxe.png
new file mode 100644
index 000000000..34f54eff9
--- /dev/null
+++ b/data/tool_woodaxe.png
Binary files differ
diff --git a/data/tool_woodpick.png b/data/tool_woodpick.png
new file mode 100644
index 000000000..ea728cca3
--- /dev/null
+++ b/data/tool_woodpick.png
Binary files differ
diff --git a/data/tool_woodshovel.png b/data/tool_woodshovel.png
new file mode 100644
index 000000000..649ab4c38
--- /dev/null
+++ b/data/tool_woodshovel.png
Binary files differ
diff --git a/data/tool_wpick.png b/data/tool_wpick.png
deleted file mode 100644
index 359249515..000000000
--- a/data/tool_wpick.png
+++ /dev/null
Binary files differ
diff --git a/data/water.png b/data/water.png
index a304059e3..e5f8cdc25 100644
--- a/data/water.png
+++ b/data/water.png
Binary files differ
diff --git a/doc/README.txt b/doc/README.txt
index be2c53966..645e2a560 100644
--- a/doc/README.txt
+++ b/doc/README.txt
@@ -3,6 +3,12 @@ Minetest-c55
An InfiniMiner/Minecraft inspired game.
Copyright (c) 2010-2011 Perttu Ahola <celeron55@gmail.com>
+Further documentation:
+----------------------
+- Website: http://celeron.55.lt/~celeron55/minetest/
+- Wiki: http://celeron.55.lt/~celeron55/minetest/wiki/
+- Forum: http://celeron.55.lt/~celeron55/minetest/forum/
+
This is a development version:
------------------------------
- Don't expect it to work as well as a finished game will.
@@ -12,6 +18,7 @@ This is a development version:
Controls:
---------
- See the in-game pause menu
+- Settable in the configuration file, see the section below.
Map directory:
--------------
@@ -46,7 +53,7 @@ Compiling on GNU/Linux:
-----------------------
Install dependencies. Here's an example for Debian/Ubuntu:
-$ apt-get install libirrlicht-dev cmake libbz2-dev libpng12-dev libjpeg8-dev libxxf86vm-dev
+$ apt-get install build-essential libirrlicht-dev cmake libbz2-dev libpng12-dev libjpeg8-dev libxxf86vm-dev libgl1-mesa-dev
Download source, extract (this is the URL to the latest of source repository, which might not work at all times):
$ wget https://bitbucket.org/celeron55/minetest/get/tip.tar.gz
@@ -143,8 +150,7 @@ Compiling on Windows:
If using MSVC:
- Open the generated minetest.sln
- The project defaults to the "Debug" configuration. Make very sure to
- select "Release", unless you like to have a very glitchy gaming
- experience.
+ select "Release", unless you want to debug some stuff (it's slower)
- Build the ALL_BUILD project
- Build the INSTALL project
- You should now have a working game with the executable in
@@ -153,6 +159,7 @@ Compiling on Windows:
project.
If using MinGW:
- Using the command line, browse to the build directory and run 'make'
+ (or mingw32-make or whatever it happens to be)
- You should now have a working game with the executable in
DIR/minetest/bin/minetest.exe
diff --git a/doc/changelog.txt b/doc/changelog.txt
index 6ff1fa52b..5a01b6bc4 100644
--- a/doc/changelog.txt
+++ b/doc/changelog.txt
@@ -3,6 +3,59 @@ Minetest-c55 changelog
This should contain all the major changes.
For minor stuff, refer to the commit log of the repository.
+2011-06-02:
+- Password crash on windows fixed
+- Optimized server CPU usage a lot
+- Furnaces now work also while players are not near to them
+
+2011-05-29:
+- Optimized smooth lighting
+- A number of small fixes
+- Added clouds and simple skyboxes
+- The glass block added
+- Added key configuration to config file
+- Player privileges on server
+- Slightly updated map format
+- Player passwords
+- All textures first searched from texture_path
+- Map directory ("map") has been renamed to "world" (just rename it to load an old world)
+- Mouse inversion (invert_mouse)
+- Grass doesn't grow immediately anymore
+- Fence added
+
+2011-04-24:
+- Smooth lighting with simple ambient occlusion
+- Updated main menu
+
+2011-04-23_0_test:
+- Small bug fixes
+- Item drop multiplication fixed
+- HP added
+- Added A simple monster which spawns to dark places at map generation time
+- Some code refactoring and cleaning (possibly new bugs)
+
+2011-04-11:
+- Fixed crafting a bit
+
+2011-04-10_0:
+- Asynchronous map generation
+- New object system
+
+2011-04-06:
+- Mesh update of node addition/removal is now done asynchronously on client, removing frametime spike
+- Node addition/removal is sent directly only to clients that are closer than 100 nodes to the modification. For the others, the modified blocks are set unsent. (and are re-sent when applicable)
+
+2011-04-05:
+- Made furnace usable
+- Added cobblestone
+- Added wood, stone and steel tools: pickaxes, shovels and axes
+- Incremented to version 0.0.2
+
+2011-04-04:
+- Cleaned client to be completely synchronous, except for the mesh calculation, which is now done with queues in a separate thread.
+- Added node metadata support
+- Added chests
+
2011-02-17:
- Added better handling of textures. Now many file extensions are searched. Also too large textures are not put on the texture atlas, and the construction of the texture atlas is stopped when it is full.
diff --git a/genmap.py b/genmap.py
index 8949888a4..aaa35690f 100755
--- a/genmap.py
+++ b/genmap.py
@@ -1,13 +1,12 @@
-#!/usr/bin/python
+#!/usr/bin/python2
+
+# This is an example script that generates some valid map data.
import struct
import random
-
-def getrand():
- i = random.randrange(0,2)
- if i==0:
- return 0
- return 254
+import os
+import sys
+from pnoise import pnoise
"""
Map format:
@@ -21,18 +20,72 @@ ffff = -1
0001 = 1
"""
-f = open("map/sectors/00000000/ffff", "wb")
+def to4h(i):
+ s = "";
+ s += '{0:1x}'.format((i>>12) & 0x000f)
+ s += '{0:1x}'.format((i>>8) & 0x000f)
+ s += '{0:1x}'.format((i>>4) & 0x000f)
+ s += '{0:1x}'.format((i>>0) & 0x000f)
+ return s
+
+def getrand():
+ i = random.randrange(0,2)
+ if i==0:
+ return 0
+ return 254
+
+def writeblock(mapdir, px,py,pz, version):
+ sectordir = mapdir + "/sectors/" + to4h(px) + to4h(pz)
+
+ try:
+ os.makedirs(sectordir)
+ except OSError:
+ pass
+
+ f = open(sectordir+"/"+to4h(py), "wb")
+
+ if version == 0:
+ # version
+ f.write(struct.pack('B', 0))
+ # is_underground
+ f.write(struct.pack('B', 0))
+ elif version == 2:
+ # version
+ f.write(struct.pack('B', 2))
+ # is_underground
+ f.write(struct.pack('B', 0))
+
+ for z in range(0,16):
+ for y in range(0,16):
+ for x in range(0,16):
+ b = 254
+ r = 20.0*pnoise((px*16+x)/100.,(pz*16+z)/100.,0)
+ r += 5.0*pnoise((px*16+x)/25.,(pz*16+z)/25.,0)
+ #print("r="+str(r))
+ y1 = py*16+y
+ if y1 <= r-3:
+ b = 0 #stone
+ elif y1 <= r:
+ b = 1 #grass
+ elif y1 <= 1:
+ b = 9 #water
+ if version == 0:
+ # Material content
+ f.write(struct.pack('B', b))
+ elif version == 2:
+ # Material content
+ f.write(struct.pack('B', b))
+ # Brightness
+ f.write(struct.pack('B', 15))
-# version
-f.write(struct.pack('B', 2))
-# is_underground
-f.write(struct.pack('B', 0))
+ f.close()
-for i in range(0,16*16*16):
- # Material content
- f.write(struct.pack('B', getrand()))
- # Brightness
- f.write(struct.pack('B', 15))
+mapdir = "map"
-f.close()
+for z in range(-2,3):
+ for y in range(-1,2):
+ for x in range(-2,3):
+ print("generating block "+str(x)+","+str(y)+","+str(z))
+ writeblock(mapdir, x,y,z, 0)
+#END
diff --git a/minetest.conf.example b/minetest.conf.example
index c78266e7b..743186853 100644
--- a/minetest.conf.example
+++ b/minetest.conf.example
@@ -2,56 +2,119 @@
# ../minetest.conf
# ../../minetest.conf
# Any other path can be chosen by passing the path as a parameter
-# to the program, eg. "minetest.exe ../minetest.conf.example"
+# to the program, eg. "minetest.exe --config ../minetest.conf.example"
#
# By default, all the settings are commented and not functional.
# Uncomment settings by removing the preceding #.
+#
+# Further documentation:
+# http://celeron.55.lt/~celeron55/minetest/wiki/doku.php
#
# Client side stuff
#
-#wanted_fps = 30
-#fps_max = 60
-#viewing_range_nodes_max = 300
-#viewing_range_nodes_min = 35
+# Initial window size
#screenW = 800
#screenH = 600
+
+# Port to connect to and to bind a server at
#port = 30000
+
+# Address to connect to (blank = start local server)
#address =
+
+# Name of player. On server, this is the default admin.
#name =
-# Whether to try to fog out the border of the visible area
+# Key configuration.
+# See http://irrlicht.sourceforge.net/docu/namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3
+#keymap_forward = KEY_KEY_W
+#keymap_backward = KEY_KEY_S
+#keymap_left = KEY_KEY_A
+#keymap_right = KEY_KEY_D
+#keymap_jump = KEY_SPACE
+#keymap_sneak = KEY_LSHIFT
+#keymap_inventory = KEY_KEY_I
+#keymap_chat = KEY_KEY_T
+#keymap_rangeselect = KEY_KEY_R
+# Some (temporary) keys for debugging features
+#keymap_special1 = KEY_KEY_E
+#keymap_print_debug_stacks = KEY_KEY_P
+
+#invert_mouse = false
+
+# The desired FPS
+#wanted_fps = 30
+
+# If FPS would go higher than this, limit it by sleeping
+# (to not waste CPU power for no benefit)
+#fps_max = 60
+
+# The allowed adjustment range for the automatic rendering range adjustment
+#viewing_range_nodes_max = 300
+#viewing_range_nodes_min = 35
+
+# Whether to fog out the end of the visible area
#enable_fog = true
+
# Enable a bit lower water surface; disable for speed
#new_style_water = true
+
# Enable nice leaves; disable for speed
#new_style_leaves = true
-# Whether to draw a frametime graph
+
+# Enable smooth lighting with simple ambient occlusion;
+# disable for speed or for different looks.
+#smooth_lighting = true
+
+# Whether to draw a frametime graph (for debugging frametime)
#frametime_graph = false
-# Enable combining mainly used textures to a bigger one
+
+# Enable combining mainly used textures to a bigger one for improved speed
+# disable if it causes graphics glitches.
#enable_texture_atlas = true
-# Path to texture directory
+
+# Path to texture directory. All textures are first searched from here.
#texture_path =
+
+# Video back-end.
# Possible values: null, software, burningsvideo, direct3d8, direct3d9, opengl
#video_driver = opengl
-#
+# Enable random user input, for testing
#random_input = false
+
+# Timeout for client to remove unused map data from memory
#client_delete_unused_sectors_timeout = 1200
#
# Server side stuff
#
-# Set to true to enable experimental features
-#enable_experimental = false
-
+# Map directory (everything in the world is stored here)
#map-dir = /home/palle/custom_map
# Set to true to enable creative mode (unlimited inventory)
#creative_mode = false
+#enable_damage = false
+
+#default_password =
+
+# Available privileges: build, teleport, settime, privs, shout
+#default_privs = build, shout
+
+# Gives some stuff to players at the beginning
+#give_initial_stuff = false
+
+# Set to true to enable experimental features
+# (varies from version to version, see wiki)
+#enable_experimental = false
+
+# Profiler data print interval. 0 = disable.
+#profiler_print_interval = 10
+
# Player and object positions are sent at intervals specified by this
#objectdata_inverval = 0.2
diff --git a/misc/map.cpp b/misc/map.cpp
new file mode 100644
index 000000000..78a849fd5
--- /dev/null
+++ b/misc/map.cpp
@@ -0,0 +1,6393 @@
+/*
+Minetest-c55
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "map.h"
+#include "main.h"
+#include "jmutexautolock.h"
+#include "client.h"
+#include "filesys.h"
+#include "utility.h"
+#include "voxel.h"
+#include "porting.h"
+#include "mineral.h"
+#include "noise.h"
+#include "serverobject.h"
+
+/*
+ Map
+*/
+
+Map::Map(std::ostream &dout):
+ m_dout(dout),
+ m_sector_cache(NULL)
+{
+ /*m_sector_mutex.Init();
+ assert(m_sector_mutex.IsInitialized());*/
+}
+
+Map::~Map()
+{
+ /*
+ Free all MapSectors
+ */
+ core::map<v2s16, MapSector*>::Iterator i = m_sectors.getIterator();
+ for(; i.atEnd() == false; i++)
+ {
+ MapSector *sector = i.getNode()->getValue();
+ delete sector;
+ }
+}
+
+void Map::addEventReceiver(MapEventReceiver *event_receiver)
+{
+ m_event_receivers.insert(event_receiver, false);
+}
+
+void Map::removeEventReceiver(MapEventReceiver *event_receiver)
+{
+ if(m_event_receivers.find(event_receiver) == NULL)
+ return;
+ m_event_receivers.remove(event_receiver);
+}
+
+void Map::dispatchEvent(MapEditEvent *event)
+{
+ for(core::map<MapEventReceiver*, bool>::Iterator
+ i = m_event_receivers.getIterator();
+ i.atEnd()==false; i++)
+ {
+ MapEventReceiver* event_receiver = i.getNode()->getKey();
+ event_receiver->onMapEditEvent(event);
+ }
+}
+
+MapSector * Map::getSectorNoGenerateNoExNoLock(v2s16 p)
+{
+ if(m_sector_cache != NULL && p == m_sector_cache_p){
+ MapSector * sector = m_sector_cache;
+ // Reset inactivity timer
+ sector->usage_timer = 0.0;
+ return sector;
+ }
+
+ core::map<v2s16, MapSector*>::Node *n = m_sectors.find(p);
+
+ if(n == NULL)
+ return NULL;
+
+ MapSector *sector = n->getValue();
+
+ // Cache the last result
+ m_sector_cache_p = p;
+ m_sector_cache = sector;
+
+ // Reset inactivity timer
+ sector->usage_timer = 0.0;
+ return sector;
+}
+
+MapSector * Map::getSectorNoGenerateNoEx(v2s16 p)
+{
+ //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
+
+ return getSectorNoGenerateNoExNoLock(p);
+}
+
+MapSector * Map::getSectorNoGenerate(v2s16 p)
+{
+ MapSector *sector = getSectorNoGenerateNoEx(p);
+ if(sector == NULL)
+ throw InvalidPositionException();
+
+ return sector;
+}
+
+MapBlock * Map::getBlockNoCreate(v3s16 p3d)
+{
+ v2s16 p2d(p3d.X, p3d.Z);
+ MapSector * sector = getSectorNoGenerate(p2d);
+
+ MapBlock *block = sector->getBlockNoCreate(p3d.Y);
+
+ return block;
+}
+
+MapBlock * Map::getBlockNoCreateNoEx(v3s16 p3d)
+{
+ try
+ {
+ v2s16 p2d(p3d.X, p3d.Z);
+ MapSector * sector = getSectorNoGenerate(p2d);
+ MapBlock *block = sector->getBlockNoCreate(p3d.Y);
+ return block;
+ }
+ catch(InvalidPositionException &e)
+ {
+ return NULL;
+ }
+}
+
+/*MapBlock * Map::getBlockCreate(v3s16 p3d)
+{
+ v2s16 p2d(p3d.X, p3d.Z);
+ MapSector * sector = getSectorCreate(p2d);
+ assert(sector);
+ MapBlock *block = sector->getBlockNoCreate(p3d.Y);
+ if(block)
+ return block;
+ block = sector->createBlankBlock(p3d.Y);
+ return block;
+}*/
+
+bool Map::isNodeUnderground(v3s16 p)
+{
+ v3s16 blockpos = getNodeBlockPos(p);
+ try{
+ MapBlock * block = getBlockNoCreate(blockpos);
+ return block->getIsUnderground();
+ }
+ catch(InvalidPositionException &e)
+ {
+ return false;
+ }
+}
+
+/*
+ Goes recursively through the neighbours of the node.
+
+ Alters only transparent nodes.
+
+ If the lighting of the neighbour is lower than the lighting of
+ the node was (before changing it to 0 at the step before), the
+ lighting of the neighbour is set to 0 and then the same stuff
+ repeats for the neighbour.
+
+ The ending nodes of the routine are stored in light_sources.
+ This is useful when a light is removed. In such case, this
+ routine can be called for the light node and then again for
+ light_sources to re-light the area without the removed light.
+
+ values of from_nodes are lighting values.
+*/
+void Map::unspreadLight(enum LightBank bank,
+ core::map<v3s16, u8> & from_nodes,
+ core::map<v3s16, bool> & light_sources,
+ core::map<v3s16, MapBlock*> & modified_blocks)
+{
+ v3s16 dirs[6] = {
+ v3s16(0,0,1), // back
+ v3s16(0,1,0), // top
+ v3s16(1,0,0), // right
+ v3s16(0,0,-1), // front
+ v3s16(0,-1,0), // bottom
+ v3s16(-1,0,0), // left
+ };
+
+ if(from_nodes.size() == 0)
+ return;
+
+ u32 blockchangecount = 0;
+
+ core::map<v3s16, u8> unlighted_nodes;
+ core::map<v3s16, u8>::Iterator j;
+ j = from_nodes.getIterator();
+
+ /*
+ Initialize block cache
+ */
+ v3s16 blockpos_last;
+ MapBlock *block = NULL;
+ // Cache this a bit, too
+ bool block_checked_in_modified = false;
+
+ for(; j.atEnd() == false; j++)
+ {
+ v3s16 pos = j.getNode()->getKey();
+ v3s16 blockpos = getNodeBlockPos(pos);
+
+ // Only fetch a new block if the block position has changed
+ try{
+ if(block == NULL || blockpos != blockpos_last){
+ block = getBlockNoCreate(blockpos);
+ blockpos_last = blockpos;
+
+ block_checked_in_modified = false;
+ blockchangecount++;
+ }
+ }
+ catch(InvalidPositionException &e)
+ {
+ continue;
+ }
+
+ if(block->isDummy())
+ continue;
+
+ // Calculate relative position in block
+ v3s16 relpos = pos - blockpos_last * MAP_BLOCKSIZE;
+
+ // Get node straight from the block
+ MapNode n = block->getNode(relpos);
+
+ u8 oldlight = j.getNode()->getValue();
+
+ // Loop through 6 neighbors
+ for(u16 i=0; i<6; i++)
+ {
+ // Get the position of the neighbor node
+ v3s16 n2pos = pos + dirs[i];
+
+ // Get the block where the node is located
+ v3s16 blockpos = getNodeBlockPos(n2pos);
+
+ try
+ {
+ // Only fetch a new block if the block position has changed
+ try{
+ if(block == NULL || blockpos != blockpos_last){
+ block = getBlockNoCreate(blockpos);
+ blockpos_last = blockpos;
+
+ block_checked_in_modified = false;
+ blockchangecount++;
+ }
+ }
+ catch(InvalidPositionException &e)
+ {
+ continue;
+ }
+
+ // Calculate relative position in block
+ v3s16 relpos = n2pos - blockpos * MAP_BLOCKSIZE;
+ // Get node straight from the block
+ MapNode n2 = block->getNode(relpos);
+
+ bool changed = false;
+
+ //TODO: Optimize output by optimizing light_sources?
+
+ /*
+ If the neighbor is dimmer than what was specified
+ as oldlight (the light of the previous node)
+ */
+ if(n2.getLight(bank) < oldlight)
+ {
+ /*
+ And the neighbor is transparent and it has some light
+ */
+ if(n2.light_propagates() && n2.getLight(bank) != 0)
+ {
+ /*
+ Set light to 0 and add to queue
+ */
+
+ u8 current_light = n2.getLight(bank);
+ n2.setLight(bank, 0);
+ block->setNode(relpos, n2);
+
+ unlighted_nodes.insert(n2pos, current_light);
+ changed = true;
+
+ /*
+ Remove from light_sources if it is there
+ NOTE: This doesn't happen nearly at all
+ */
+ /*if(light_sources.find(n2pos))
+ {
+ std::cout<<"Removed from light_sources"<<std::endl;
+ light_sources.remove(n2pos);
+ }*/
+ }
+
+ /*// DEBUG
+ if(light_sources.find(n2pos) != NULL)
+ light_sources.remove(n2pos);*/
+ }
+ else{
+ light_sources.insert(n2pos, true);
+ }
+
+ // Add to modified_blocks
+ if(changed == true && block_checked_in_modified == false)
+ {
+ // If the block is not found in modified_blocks, add.
+ if(modified_blocks.find(blockpos) == NULL)
+ {
+ modified_blocks.insert(blockpos, block);
+ }
+ block_checked_in_modified = true;
+ }
+ }
+ catch(InvalidPositionException &e)
+ {
+ continue;
+ }
+ }
+ }
+
+ /*dstream<<"unspreadLight(): Changed block "
+ <<blockchangecount<<" times"
+ <<" for "<<from_nodes.size()<<" nodes"
+ <<std::endl;*/
+
+ if(unlighted_nodes.size() > 0)
+ unspreadLight(bank, unlighted_nodes, light_sources, modified_blocks);
+}
+
+/*
+ A single-node wrapper of the above
+*/
+void Map::unLightNeighbors(enum LightBank bank,
+ v3s16 pos, u8 lightwas,
+ core::map<v3s16, bool> & light_sources,
+ core::map<v3s16, MapBlock*> & modified_blocks)
+{
+ core::map<v3s16, u8> from_nodes;
+ from_nodes.insert(pos, lightwas);
+
+ unspreadLight(bank, from_nodes, light_sources, modified_blocks);
+}
+
+/*
+ Lights neighbors of from_nodes, collects all them and then
+ goes on recursively.
+*/
+void Map::spreadLight(enum LightBank bank,
+ core::map<v3s16, bool> & from_nodes,
+ core::map<v3s16, MapBlock*> & modified_blocks)
+{
+ const v3s16 dirs[6] = {
+ v3s16(0,0,1), // back
+ v3s16(0,1,0), // top
+ v3s16(1,0,0), // right
+ v3s16(0,0,-1), // front
+ v3s16(0,-1,0), // bottom
+ v3s16(-1,0,0), // left
+ };
+
+ if(from_nodes.size() == 0)
+ return;
+
+ u32 blockchangecount = 0;
+
+ core::map<v3s16, bool> lighted_nodes;
+ core::map<v3s16, bool>::Iterator j;
+ j = from_nodes.getIterator();
+
+ /*
+ Initialize block cache
+ */
+ v3s16 blockpos_last;
+ MapBlock *block = NULL;
+ // Cache this a bit, too
+ bool block_checked_in_modified = false;
+
+ for(; j.atEnd() == false; j++)
+ //for(; j != from_nodes.end(); j++)
+ {
+ v3s16 pos = j.getNode()->getKey();
+ //v3s16 pos = *j;
+ //dstream<<"pos=("<<pos.X<<","<<pos.Y<<","<<pos.Z<<")"<<std::endl;
+ v3s16 blockpos = getNodeBlockPos(pos);
+
+ // Only fetch a new block if the block position has changed
+ try{
+ if(block == NULL || blockpos != blockpos_last){
+ block = getBlockNoCreate(blockpos);
+ blockpos_last = blockpos;
+
+ block_checked_in_modified = false;
+ blockchangecount++;
+ }
+ }
+ catch(InvalidPositionException &e)
+ {
+ continue;
+ }
+
+ if(block->isDummy())
+ continue;
+
+ // Calculate relative position in block
+ v3s16 relpos = pos - blockpos_last * MAP_BLOCKSIZE;
+
+ // Get node straight from the block
+ MapNode n = block->getNode(relpos);
+
+ u8 oldlight = n.getLight(bank);
+ u8 newlight = diminish_light(oldlight);
+
+ // Loop through 6 neighbors
+ for(u16 i=0; i<6; i++){
+ // Get the position of the neighbor node
+ v3s16 n2pos = pos + dirs[i];
+
+ // Get the block where the node is located
+ v3s16 blockpos = getNodeBlockPos(n2pos);
+
+ try
+ {
+ // Only fetch a new block if the block position has changed
+ try{
+ if(block == NULL || blockpos != blockpos_last){
+ block = getBlockNoCreate(blockpos);
+ blockpos_last = blockpos;
+
+ block_checked_in_modified = false;
+ blockchangecount++;
+ }
+ }
+ catch(InvalidPositionException &e)
+ {
+ continue;
+ }
+
+ // Calculate relative position in block
+ v3s16 relpos = n2pos - blockpos * MAP_BLOCKSIZE;
+ // Get node straight from the block
+ MapNode n2 = block->getNode(relpos);
+
+ bool changed = false;
+ /*
+ If the neighbor is brighter than the current node,
+ add to list (it will light up this node on its turn)
+ */
+ if(n2.getLight(bank) > undiminish_light(oldlight))
+ {
+ lighted_nodes.insert(n2pos, true);
+ //lighted_nodes.push_back(n2pos);
+ changed = true;
+ }
+ /*
+ If the neighbor is dimmer than how much light this node
+ would spread on it, add to list
+ */
+ if(n2.getLight(bank) < newlight)
+ {
+ if(n2.light_propagates())
+ {
+ n2.setLight(bank, newlight);
+ block->setNode(relpos, n2);
+ lighted_nodes.insert(n2pos, true);
+ //lighted_nodes.push_back(n2pos);
+ changed = true;
+ }
+ }
+
+ // Add to modified_blocks
+ if(changed == true && block_checked_in_modified == false)
+ {
+ // If the block is not found in modified_blocks, add.
+ if(modified_blocks.find(blockpos) == NULL)
+ {
+ modified_blocks.insert(blockpos, block);
+ }
+ block_checked_in_modified = true;
+ }
+ }
+ catch(InvalidPositionException &e)
+ {
+ continue;
+ }
+ }
+ }
+
+ /*dstream<<"spreadLight(): Changed block "
+ <<blockchangecount<<" times"
+ <<" for "<<from_nodes.size()<<" nodes"
+ <<std::endl;*/
+
+ if(lighted_nodes.size() > 0)
+ spreadLight(bank, lighted_nodes, modified_blocks);
+}
+
+/*
+ A single-node source variation of the above.
+*/
+void Map::lightNeighbors(enum LightBank bank,
+ v3s16 pos,
+ core::map<v3s16, MapBlock*> & modified_blocks)
+{
+ core::map<v3s16, bool> from_nodes;
+ from_nodes.insert(pos, true);
+ spreadLight(bank, from_nodes, modified_blocks);
+}
+
+v3s16 Map::getBrightestNeighbour(enum LightBank bank, v3s16 p)
+{
+ v3s16 dirs[6] = {
+ v3s16(0,0,1), // back
+ v3s16(0,1,0), // top
+ v3s16(1,0,0), // right
+ v3s16(0,0,-1), // front
+ v3s16(0,-1,0), // bottom
+ v3s16(-1,0,0), // left
+ };
+
+ u8 brightest_light = 0;
+ v3s16 brightest_pos(0,0,0);
+ bool found_something = false;
+
+ // Loop through 6 neighbors
+ for(u16 i=0; i<6; i++){
+ // Get the position of the neighbor node
+ v3s16 n2pos = p + dirs[i];
+ MapNode n2;
+ try{
+ n2 = getNode(n2pos);
+ }
+ catch(InvalidPositionException &e)
+ {
+ continue;
+ }
+ if(n2.getLight(bank) > brightest_light || found_something == false){
+ brightest_light = n2.getLight(bank);
+ brightest_pos = n2pos;
+ found_something = true;
+ }
+ }
+
+ if(found_something == false)
+ throw InvalidPositionException();
+
+ return brightest_pos;
+}
+
+/*
+ Propagates sunlight down from a node.
+ Starting point gets sunlight.
+
+ Returns the lowest y value of where the sunlight went.
+
+ Mud is turned into grass in where the sunlight stops.
+*/
+s16 Map::propagateSunlight(v3s16 start,
+ core::map<v3s16, MapBlock*> & modified_blocks)
+{
+ s16 y = start.Y;
+ for(; ; y--)
+ {
+ v3s16 pos(start.X, y, start.Z);
+
+ v3s16 blockpos = getNodeBlockPos(pos);
+ MapBlock *block;
+ try{
+ block = getBlockNoCreate(blockpos);
+ }
+ catch(InvalidPositionException &e)
+ {
+ break;
+ }
+
+ v3s16 relpos = pos - blockpos*MAP_BLOCKSIZE;
+ MapNode n = block->getNode(relpos);
+
+ if(n.sunlight_propagates())
+ {
+ n.setLight(LIGHTBANK_DAY, LIGHT_SUN);
+ block->setNode(relpos, n);
+
+ modified_blocks.insert(blockpos, block);
+ }
+ else
+ {
+ // Turn mud into grass
+ if(n.d == CONTENT_MUD)
+ {
+ n.d = CONTENT_GRASS;
+ block->setNode(relpos, n);
+ modified_blocks.insert(blockpos, block);
+ }
+
+ // Sunlight goes no further
+ break;
+ }
+ }
+ return y + 1;
+}
+
+void Map::updateLighting(enum LightBank bank,
+ core::map<v3s16, MapBlock*> & a_blocks,
+ core::map<v3s16, MapBlock*> & modified_blocks)
+{
+ /*m_dout<<DTIME<<"Map::updateLighting(): "
+ <<a_blocks.size()<<" blocks."<<std::endl;*/
+
+ //TimeTaker timer("updateLighting");
+
+ // For debugging
+ //bool debug=true;
+ //u32 count_was = modified_blocks.size();
+
+ core::map<v3s16, MapBlock*> blocks_to_update;
+
+ core::map<v3s16, bool> light_sources;
+
+ core::map<v3s16, u8> unlight_from;
+
+ core::map<v3s16, MapBlock*>::Iterator i;
+ i = a_blocks.getIterator();
+ for(; i.atEnd() == false; i++)
+ {
+ MapBlock *block = i.getNode()->getValue();
+
+ for(;;)
+ {
+ // Don't bother with dummy blocks.
+ if(block->isDummy())
+ break;
+
+ v3s16 pos = block->getPos();
+ modified_blocks.insert(pos, block);
+
+ blocks_to_update.insert(pos, block);
+
+ /*
+ Clear all light from block
+ */
+ for(s16 z=0; z<MAP_BLOCKSIZE; z++)
+ for(s16 x=0; x<MAP_BLOCKSIZE; x++)
+ for(s16 y=0; y<MAP_BLOCKSIZE; y++)
+ {
+
+ try{
+ v3s16 p(x,y,z);
+ MapNode n = block->getNode(v3s16(x,y,z));
+ u8 oldlight = n.getLight(bank);
+ n.setLight(bank, 0);
+ block->setNode(v3s16(x,y,z), n);
+
+ // Collect borders for unlighting
+ if(x==0 || x == MAP_BLOCKSIZE-1
+ || y==0 || y == MAP_BLOCKSIZE-1
+ || z==0 || z == MAP_BLOCKSIZE-1)
+ {
+ v3s16 p_map = p + v3s16(
+ MAP_BLOCKSIZE*pos.X,
+ MAP_BLOCKSIZE*pos.Y,
+ MAP_BLOCKSIZE*pos.Z);
+ unlight_from.insert(p_map, oldlight);
+ }
+ }
+ catch(InvalidPositionException &e)
+ {
+ /*
+ This would happen when dealing with a
+ dummy block.
+ */
+ //assert(0);
+ dstream<<"updateLighting(): InvalidPositionException"
+ <<std::endl;
+ }
+ }
+
+ if(bank == LIGHTBANK_DAY)
+ {
+ bool bottom_valid = block->propagateSunlight(light_sources);
+
+ // If bottom is valid, we're done.
+ if(bottom_valid)
+ break;
+ }
+ else if(bank == LIGHTBANK_NIGHT)
+ {
+ // For night lighting, sunlight is not propagated
+ break;
+ }
+ else
+ {
+ // Invalid lighting bank
+ assert(0);
+ }
+
+ /*dstream<<"Bottom for sunlight-propagated block ("
+ <<pos.X<<","<<pos.Y<<","<<pos.Z<<") not valid"
+ <<std::endl;*/
+
+ // Bottom sunlight is not valid; get the block and loop to it
+
+ pos.Y--;
+ try{
+ block = getBlockNoCreate(pos);
+ }
+ catch(InvalidPositionException &e)
+ {
+ assert(0);
+ }
+
+ }
+ }
+
+#if 0
+ {
+ TimeTaker timer("unspreadLight");
+ unspreadLight(bank, unlight_from, light_sources, modified_blocks);
+ }
+
+ if(debug)
+ {
+ u32 diff = modified_blocks.size() - count_was;
+ count_was = modified_blocks.size();
+ dstream<<"unspreadLight modified "<<diff<<std::endl;
+ }
+
+ {
+ TimeTaker timer("spreadLight");
+ spreadLight(bank, light_sources, modified_blocks);
+ }
+
+ if(debug)
+ {
+ u32 diff = modified_blocks.size() - count_was;
+ count_was = modified_blocks.size();
+ dstream<<"spreadLight modified "<<diff<<std::endl;
+ }
+#endif
+
+ {
+ //MapVoxelManipulator vmanip(this);
+
+ // Make a manual voxel manipulator and load all the blocks
+ // that touch the requested blocks
+ ManualMapVoxelManipulator vmanip(this);
+ core::map<v3s16, MapBlock*>::Iterator i;
+ i = blocks_to_update.getIterator();
+ for(; i.atEnd() == false; i++)
+ {
+ MapBlock *block = i.getNode()->getValue();
+ v3s16 p = block->getPos();
+
+ // Add all surrounding blocks
+ vmanip.initialEmerge(p - v3s16(1,1,1), p + v3s16(1,1,1));
+
+ /*
+ Add all surrounding blocks that have up-to-date lighting
+ NOTE: This doesn't quite do the job (not everything
+ appropriate is lighted)
+ */
+ /*for(s16 z=-1; z<=1; z++)
+ for(s16 y=-1; y<=1; y++)
+ for(s16 x=-1; x<=1; x++)
+ {
+ v3s16 p(x,y,z);
+ MapBlock *block = getBlockNoCreateNoEx(p);
+ if(block == NULL)
+ continue;
+ if(block->isDummy())
+ continue;
+ if(block->getLightingExpired())
+ continue;
+ vmanip.initialEmerge(p, p);
+ }*/
+
+ // Lighting of block will be updated completely
+ block->setLightingExpired(false);
+ }
+
+ {
+ //TimeTaker timer("unSpreadLight");
+ vmanip.unspreadLight(bank, unlight_from, light_sources);
+ }
+ {
+ //TimeTaker timer("spreadLight");
+ vmanip.spreadLight(bank, light_sources);
+ }
+ {
+ //TimeTaker timer("blitBack");
+ vmanip.blitBack(modified_blocks);
+ }
+ /*dstream<<"emerge_time="<<emerge_time<<std::endl;
+ emerge_time = 0;*/
+ }
+
+ //m_dout<<"Done ("<<getTimestamp()<<")"<<std::endl;
+}
+
+void Map::updateLighting(core::map<v3s16, MapBlock*> & a_blocks,
+ core::map<v3s16, MapBlock*> & modified_blocks)
+{
+ updateLighting(LIGHTBANK_DAY, a_blocks, modified_blocks);
+ updateLighting(LIGHTBANK_NIGHT, a_blocks, modified_blocks);
+
+ /*
+ Update information about whether day and night light differ
+ */
+ for(core::map<v3s16, MapBlock*>::Iterator
+ i = modified_blocks.getIterator();
+ i.atEnd() == false; i++)
+ {
+ MapBlock *block = i.getNode()->getValue();
+ block->updateDayNightDiff();
+ }
+}
+
+/*
+ This is called after changing a node from transparent to opaque.
+ The lighting value of the node should be left as-is after changing
+ other values. This sets the lighting value to 0.
+*/
+void Map::addNodeAndUpdate(v3s16 p, MapNode n,
+ core::map<v3s16, MapBlock*> &modified_blocks)
+{
+ /*PrintInfo(m_dout);
+ m_dout<<DTIME<<"Map::addNodeAndUpdate(): p=("
+ <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
+
+ /*
+ From this node to nodes underneath:
+ If lighting is sunlight (1.0), unlight neighbours and
+ set lighting to 0.
+ Else discontinue.
+ */
+
+ v3s16 toppos = p + v3s16(0,1,0);
+ v3s16 bottompos = p + v3s16(0,-1,0);
+
+ bool node_under_sunlight = true;
+ core::map<v3s16, bool> light_sources;
+
+ /*
+ If there is a node at top and it doesn't have sunlight,
+ there has not been any sunlight going down.
+
+ Otherwise there probably is.
+ */
+ try{
+ MapNode topnode = getNode(toppos);
+
+ if(topnode.getLight(LIGHTBANK_DAY) != LIGHT_SUN)
+ node_under_sunlight = false;
+ }
+ catch(InvalidPositionException &e)
+ {
+ }
+
+ /*
+ If the new node doesn't propagate sunlight and there is
+ grass below, change it to mud
+ */
+ if(content_features(n.d).sunlight_propagates == false)
+ {
+ try{
+ MapNode bottomnode = getNode(bottompos);
+
+ if(bottomnode.d == CONTENT_GRASS
+ || bottomnode.d == CONTENT_GRASS_FOOTSTEPS)
+ {
+ bottomnode.d = CONTENT_MUD;
+ setNode(bottompos, bottomnode);
+ }
+ }
+ catch(InvalidPositionException &e)
+ {
+ }
+ }
+
+ /*
+ If the new node is mud and it is under sunlight, change it
+ to grass
+ */
+ if(n.d == CONTENT_MUD && node_under_sunlight)
+ {
+ n.d = CONTENT_GRASS;
+ }
+
+ /*
+ Remove all light that has come out of this node
+ */
+
+ enum LightBank banks[] =
+ {
+ LIGHTBANK_DAY,
+ LIGHTBANK_NIGHT
+ };
+ for(s32 i=0; i<2; i++)
+ {
+ enum LightBank bank = banks[i];
+
+ u8 lightwas = getNode(p).getLight(bank);
+
+ // Add the block of the added node to modified_blocks
+ v3s16 blockpos = getNodeBlockPos(p);
+ MapBlock * block = getBlockNoCreate(blockpos);
+ assert(block != NULL);
+ modified_blocks.insert(blockpos, block);
+
+ assert(isValidPosition(p));
+
+ // Unlight neighbours of node.
+ // This means setting light of all consequent dimmer nodes
+ // to 0.
+ // This also collects the nodes at the border which will spread
+ // light again into this.
+ unLightNeighbors(bank, p, lightwas, light_sources, modified_blocks);
+
+ n.setLight(bank, 0);
+ }
+
+ /*
+ If node lets sunlight through and is under sunlight, it has
+ sunlight too.
+ */
+ if(node_under_sunlight && content_features(n.d).sunlight_propagates)
+ {
+ n.setLight(LIGHTBANK_DAY, LIGHT_SUN);
+ }
+
+ /*
+ Set the node on the map
+ */
+
+ setNode(p, n);
+
+ /*
+ Add intial metadata
+ */
+
+ NodeMetadata *meta_proto = content_features(n.d).initial_metadata;
+ if(meta_proto)
+ {
+ NodeMetadata *meta = meta_proto->clone();
+ setNodeMetadata(p, meta);
+ }
+
+ /*
+ If node is under sunlight and doesn't let sunlight through,
+ take all sunlighted nodes under it and clear light from them
+ and from where the light has been spread.
+ TODO: This could be optimized by mass-unlighting instead
+ of looping
+ */
+ if(node_under_sunlight && !content_features(n.d).sunlight_propagates)
+ {
+ s16 y = p.Y - 1;
+ for(;; y--){
+ //m_dout<<DTIME<<"y="<<y<<std::endl;
+ v3s16 n2pos(p.X, y, p.Z);
+
+ MapNode n2;
+ try{
+ n2 = getNode(n2pos);
+ }
+ catch(InvalidPositionException &e)
+ {
+ break;
+ }
+
+ if(n2.getLight(LIGHTBANK_DAY) == LIGHT_SUN)
+ {
+ unLightNeighbors(LIGHTBANK_DAY,
+ n2pos, n2.getLight(LIGHTBANK_DAY),
+ light_sources, modified_blocks);
+ n2.setLight(LIGHTBANK_DAY, 0);
+ setNode(n2pos, n2);
+ }
+ else
+ break;
+ }
+ }
+
+ for(s32 i=0; i<2; i++)
+ {
+ enum LightBank bank = banks[i];
+
+ /*
+ Spread light from all nodes that might be capable of doing so
+ */
+ spreadLight(bank, light_sources, modified_blocks);
+ }
+
+ /*
+ Update information about whether day and night light differ
+ */
+ for(core::map<v3s16, MapBlock*>::Iterator
+ i = modified_blocks.getIterator();
+ i.atEnd() == false; i++)
+ {
+ MapBlock *block = i.getNode()->getValue();
+ block->updateDayNightDiff();
+ }
+
+ /*
+ Add neighboring liquid nodes and the node itself if it is
+ liquid (=water node was added) to transform queue.
+ */
+ v3s16 dirs[7] = {
+ v3s16(0,0,0), // self
+ v3s16(0,0,1), // back
+ v3s16(0,1,0), // top
+ v3s16(1,0,0), // right
+ v3s16(0,0,-1), // front
+ v3s16(0,-1,0), // bottom
+ v3s16(-1,0,0), // left
+ };
+ for(u16 i=0; i<7; i++)
+ {
+ try
+ {
+
+ v3s16 p2 = p + dirs[i];
+
+ MapNode n2 = getNode(p2);
+ if(content_liquid(n2.d))
+ {
+ m_transforming_liquid.push_back(p2);
+ }
+
+ }catch(InvalidPositionException &e)
+ {
+ }
+ }
+}
+
+/*
+*/
+void Map::removeNodeAndUpdate(v3s16 p,
+ core::map<v3s16, MapBlock*> &modified_blocks)
+{
+ /*PrintInfo(m_dout);
+ m_dout<<DTIME<<"Map::removeNodeAndUpdate(): p=("
+ <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
+
+ bool node_under_sunlight = true;
+
+ v3s16 toppos = p + v3s16(0,1,0);
+
+ // Node will be replaced with this
+ u8 replace_material = CONTENT_AIR;
+
+ /*
+ If there is a node at top and it doesn't have sunlight,
+ there will be no sunlight going down.
+ */
+ try{
+ MapNode topnode = getNode(toppos);
+
+ if(topnode.getLight(LIGHTBANK_DAY) != LIGHT_SUN)
+ node_under_sunlight = false;
+ }
+ catch(InvalidPositionException &e)
+ {
+ }
+
+ core::map<v3s16, bool> light_sources;
+
+ enum LightBank banks[] =
+ {
+ LIGHTBANK_DAY,
+ LIGHTBANK_NIGHT
+ };
+ for(s32 i=0; i<2; i++)
+ {
+ enum LightBank bank = banks[i];
+
+ /*
+ Unlight neighbors (in case the node is a light source)
+ */
+ unLightNeighbors(bank, p,
+ getNode(p).getLight(bank),
+ light_sources, modified_blocks);
+ }
+
+ /*
+ Remove node metadata
+ */
+
+ removeNodeMetadata(p);
+
+ /*
+ Remove the node.
+ This also clears the lighting.
+ */
+
+ MapNode n;
+ n.d = replace_material;
+ setNode(p, n);
+
+ for(s32 i=0; i<2; i++)
+ {
+ enum LightBank bank = banks[i];
+
+ /*
+ Recalculate lighting
+ */
+ spreadLight(bank, light_sources, modified_blocks);
+ }
+
+ // Add the block of the removed node to modified_blocks
+ v3s16 blockpos = getNodeBlockPos(p);
+ MapBlock * block = getBlockNoCreate(blockpos);
+ assert(block != NULL);
+ modified_blocks.insert(blockpos, block);
+
+ /*
+ If the removed node was under sunlight, propagate the
+ sunlight down from it and then light all neighbors
+ of the propagated blocks.
+ */
+ if(node_under_sunlight)
+ {
+ s16 ybottom = propagateSunlight(p, modified_blocks);
+ /*m_dout<<DTIME<<"Node was under sunlight. "
+ "Propagating sunlight";
+ m_dout<<DTIME<<" -> ybottom="<<ybottom<<std::endl;*/
+ s16 y = p.Y;
+ for(; y >= ybottom; y--)
+ {
+ v3s16 p2(p.X, y, p.Z);
+ /*m_dout<<DTIME<<"lighting neighbors of node ("
+ <<p2.X<<","<<p2.Y<<","<<p2.Z<<")"
+ <<std::endl;*/
+ lightNeighbors(LIGHTBANK_DAY, p2, modified_blocks);
+ }
+ }
+ else
+ {
+ // Set the lighting of this node to 0
+ // TODO: Is this needed? Lighting is cleared up there already.
+ try{
+ MapNode n = getNode(p);
+ n.setLight(LIGHTBANK_DAY, 0);
+ setNode(p, n);
+ }
+ catch(InvalidPositionException &e)
+ {
+ assert(0);
+ }
+ }
+
+ for(s32 i=0; i<2; i++)
+ {
+ enum LightBank bank = banks[i];
+
+ // Get the brightest neighbour node and propagate light from it
+ v3s16 n2p = getBrightestNeighbour(bank, p);
+ try{
+ MapNode n2 = getNode(n2p);
+ lightNeighbors(bank, n2p, modified_blocks);
+ }
+ catch(InvalidPositionException &e)
+ {
+ }
+ }
+
+ /*
+ Update information about whether day and night light differ
+ */
+ for(core::map<v3s16, MapBlock*>::Iterator
+ i = modified_blocks.getIterator();
+ i.atEnd() == false; i++)
+ {
+ MapBlock *block = i.getNode()->getValue();
+ block->updateDayNightDiff();
+ }
+
+ /*
+ Add neighboring liquid nodes to transform queue.
+ */
+ v3s16 dirs[6] = {
+ v3s16(0,0,1), // back
+ v3s16(0,1,0), // top
+ v3s16(1,0,0), // right
+ v3s16(0,0,-1), // front
+ v3s16(0,-1,0), // bottom
+ v3s16(-1,0,0), // left
+ };
+ for(u16 i=0; i<6; i++)
+ {
+ try
+ {
+
+ v3s16 p2 = p + dirs[i];
+
+ MapNode n2 = getNode(p2);
+ if(content_liquid(n2.d))
+ {
+ m_transforming_liquid.push_back(p2);
+ }
+
+ }catch(InvalidPositionException &e)
+ {
+ }
+ }
+}
+
+bool Map::addNodeWithEvent(v3s16 p, MapNode n)
+{
+ MapEditEvent event;
+ event.type = MEET_ADDNODE;
+ event.p = p;
+ event.n = n;
+
+ bool succeeded = true;
+ try{
+ core::map<v3s16, MapBlock*> modified_blocks;
+ addNodeAndUpdate(p, n, modified_blocks);
+
+ // Copy modified_blocks to event
+ for(core::map<v3s16, MapBlock*>::Iterator
+ i = modified_blocks.getIterator();
+ i.atEnd()==false; i++)
+ {
+ event.modified_blocks.insert(i.getNode()->getKey(), false);
+ }
+ }
+ catch(InvalidPositionException &e){
+ succeeded = false;
+ }
+
+ dispatchEvent(&event);
+
+ return succeeded;
+}
+
+bool Map::removeNodeWithEvent(v3s16 p)
+{
+ MapEditEvent event;
+ event.type = MEET_REMOVENODE;
+ event.p = p;
+
+ bool succeeded = true;
+ try{
+ core::map<v3s16, MapBlock*> modified_blocks;
+ removeNodeAndUpdate(p, modified_blocks);
+
+ // Copy modified_blocks to event
+ for(core::map<v3s16, MapBlock*>::Iterator
+ i = modified_blocks.getIterator();
+ i.atEnd()==false; i++)
+ {
+ event.modified_blocks.insert(i.getNode()->getKey(), false);
+ }
+ }
+ catch(InvalidPositionException &e){
+ succeeded = false;
+ }
+
+ dispatchEvent(&event);
+
+ return succeeded;
+}
+
+bool Map::dayNightDiffed(v3s16 blockpos)
+{
+ try{
+ v3s16 p = blockpos + v3s16(0,0,0);
+ MapBlock *b = getBlockNoCreate(p);
+ if(b->dayNightDiffed())
+ return true;
+ }
+ catch(InvalidPositionException &e){}
+ // Leading edges
+ try{
+ v3s16 p = blockpos + v3s16(-1,0,0);
+ MapBlock *b = getBlockNoCreate(p);
+ if(b->dayNightDiffed())
+ return true;
+ }
+ catch(InvalidPositionException &e){}
+ try{
+ v3s16 p = blockpos + v3s16(0,-1,0);
+ MapBlock *b = getBlockNoCreate(p);
+ if(b->dayNightDiffed())
+ return true;
+ }
+ catch(InvalidPositionException &e){}
+ try{
+ v3s16 p = blockpos + v3s16(0,0,-1);
+ MapBlock *b = getBlockNoCreate(p);
+ if(b->dayNightDiffed())
+ return true;
+ }
+ catch(InvalidPositionException &e){}
+ // Trailing edges
+ try{
+ v3s16 p = blockpos + v3s16(1,0,0);
+ MapBlock *b = getBlockNoCreate(p);
+ if(b->dayNightDiffed())
+ return true;
+ }
+ catch(InvalidPositionException &e){}
+ try{
+ v3s16 p = blockpos + v3s16(0,1,0);
+ MapBlock *b = getBlockNoCreate(p);
+ if(b->dayNightDiffed())
+ return true;
+ }
+ catch(InvalidPositionException &e){}
+ try{
+ v3s16 p = blockpos + v3s16(0,0,1);
+ MapBlock *b = getBlockNoCreate(p);
+ if(b->dayNightDiffed())
+ return true;
+ }
+ catch(InvalidPositionException &e){}
+
+ return false;
+}
+
+/*
+ Updates usage timers
+*/
+void Map::timerUpdate(float dtime)
+{
+ //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
+
+ core::map<v2s16, MapSector*>::Iterator si;
+
+ si = m_sectors.getIterator();
+ for(; si.atEnd() == false; si++)
+ {
+ MapSector *sector = si.getNode()->getValue();
+ sector->usage_timer += dtime;
+ }
+}
+
+void Map::deleteSectors(core::list<v2s16> &list, bool only_blocks)
+{
+ /*
+ Wait for caches to be removed before continuing.
+
+ This disables the existence of caches while locked
+ */
+ //SharedPtr<JMutexAutoLock> cachelock(m_blockcachelock.waitCaches());
+
+ core::list<v2s16>::Iterator j;
+ for(j=list.begin(); j!=list.end(); j++)
+ {
+ MapSector *sector = m_sectors[*j];
+ if(only_blocks)
+ {
+ sector->deleteBlocks();
+ }
+ else
+ {
+ /*
+ If sector is in sector cache, remove it from there
+ */
+ if(m_sector_cache == sector)
+ {
+ m_sector_cache = NULL;
+ }
+ /*
+ Remove from map and delete
+ */
+ m_sectors.remove(*j);
+ delete sector;
+ }
+ }
+}
+
+u32 Map::deleteUnusedSectors(float timeout, bool only_blocks,
+ core::list<v3s16> *deleted_blocks)
+{
+ //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
+
+ core::list<v2s16> sector_deletion_queue;
+ core::map<v2s16, MapSector*>::Iterator i = m_sectors.getIterator();
+ for(; i.atEnd() == false; i++)
+ {
+ MapSector *sector = i.getNode()->getValue();
+ /*
+ Delete sector from memory if it hasn't been used in a long time
+ */
+ if(sector->usage_timer > timeout)
+ {
+ sector_deletion_queue.push_back(i.getNode()->getKey());
+
+ if(deleted_blocks != NULL)
+ {
+ // Collect positions of blocks of sector
+ MapSector *sector = i.getNode()->getValue();
+ core::list<MapBlock*> blocks;
+ sector->getBlocks(blocks);
+ for(core::list<MapBlock*>::Iterator i = blocks.begin();
+ i != blocks.end(); i++)
+ {
+ deleted_blocks->push_back((*i)->getPos());
+ }
+ }
+ }
+ }
+ deleteSectors(sector_deletion_queue, only_blocks);
+ return sector_deletion_queue.getSize();
+}
+
+void Map::PrintInfo(std::ostream &out)
+{
+ out<<"Map: ";
+}
+
+#define WATER_DROP_BOOST 4
+
+void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
+{
+ DSTACK(__FUNCTION_NAME);
+ //TimeTaker timer("transformLiquids()");
+
+ u32 loopcount = 0;
+ u32 initial_size = m_transforming_liquid.size();
+
+ /*if(initial_size != 0)
+ dstream<<"transformLiquids(): initial_size="<<initial_size<<std::endl;*/
+
+ while(m_transforming_liquid.size() != 0)
+ {
+ /*
+ Get a queued transforming liquid node
+ */
+ v3s16 p0 = m_transforming_liquid.pop_front();
+
+ MapNode n0 = getNode(p0);
+
+ // Don't deal with non-liquids
+ if(content_liquid(n0.d) == false)
+ continue;
+
+ bool is_source = !content_flowing_liquid(n0.d);
+
+ u8 liquid_level = 8;
+ if(is_source == false)
+ liquid_level = n0.param2 & 0x0f;
+
+ // Turn possible source into non-source
+ u8 nonsource_c = make_liquid_flowing(n0.d);
+
+ /*
+ If not source, check that some node flows into this one
+ and what is the level of liquid in this one
+ */
+ if(is_source == false)
+ {
+ s8 new_liquid_level_max = -1;
+
+ v3s16 dirs_from[5] = {
+ v3s16(0,1,0), // top
+ v3s16(0,0,1), // back
+ v3s16(1,0,0), // right
+ v3s16(0,0,-1), // front
+ v3s16(-1,0,0), // left
+ };
+ for(u16 i=0; i<5; i++)
+ {
+ try
+ {
+
+ bool from_top = (i==0);
+
+ v3s16 p2 = p0 + dirs_from[i];
+ MapNode n2 = getNode(p2);
+
+ if(content_liquid(n2.d))
+ {
+ u8 n2_nonsource_c = make_liquid_flowing(n2.d);
+ // Check that the liquids are the same type
+ if(n2_nonsource_c != nonsource_c)
+ {
+ dstream<<"WARNING: Not handling: different liquids"
+ " collide"<<std::endl;
+ continue;
+ }
+ bool n2_is_source = !content_flowing_liquid(n2.d);
+ s8 n2_liquid_level = 8;
+ if(n2_is_source == false)
+ n2_liquid_level = n2.param2 & 0x07;
+
+ s8 new_liquid_level = -1;
+ if(from_top)
+ {
+ //new_liquid_level = 7;
+ if(n2_liquid_level >= 7 - WATER_DROP_BOOST)
+ new_liquid_level = 7;
+ else
+ new_liquid_level = n2_liquid_level + WATER_DROP_BOOST;
+ }
+ else if(n2_liquid_level > 0)
+ {
+ new_liquid_level = n2_liquid_level - 1;
+ }
+
+ if(new_liquid_level > new_liquid_level_max)
+ new_liquid_level_max = new_liquid_level;
+ }
+
+ }catch(InvalidPositionException &e)
+ {
+ }
+ } //for
+
+ /*
+ If liquid level should be something else, update it and
+ add all the neighboring water nodes to the transform queue.
+ */
+ if(new_liquid_level_max != liquid_level)
+ {
+ if(new_liquid_level_max == -1)
+ {
+ // Remove water alltoghether
+ n0.d = CONTENT_AIR;
+ n0.param2 = 0;
+ setNode(p0, n0);
+ }
+ else
+ {
+ n0.param2 = new_liquid_level_max;
+ setNode(p0, n0);
+ }
+
+ // Block has been modified
+ {
+ v3s16 blockpos = getNodeBlockPos(p0);
+ MapBlock *block = getBlockNoCreateNoEx(blockpos);
+ if(block != NULL)
+ modified_blocks.insert(blockpos, block);
+ }
+
+ /*
+ Add neighboring non-source liquid nodes to transform queue.
+ */
+ v3s16 dirs[6] = {
+ v3s16(0,0,1), // back
+ v3s16(0,1,0), // top
+ v3s16(1,0,0), // right
+ v3s16(0,0,-1), // front
+ v3s16(0,-1,0), // bottom
+ v3s16(-1,0,0), // left
+ };
+ for(u16 i=0; i<6; i++)
+ {
+ try
+ {
+
+ v3s16 p2 = p0 + dirs[i];
+
+ MapNode n2 = getNode(p2);
+ if(content_flowing_liquid(n2.d))
+ {
+ m_transforming_liquid.push_back(p2);
+ }
+
+ }catch(InvalidPositionException &e)
+ {
+ }
+ }
+ }
+ }
+
+ // Get a new one from queue if the node has turned into non-water
+ if(content_liquid(n0.d) == false)
+ continue;
+
+ /*
+ Flow water from this node
+ */
+ v3s16 dirs_to[5] = {
+ v3s16(0,-1,0), // bottom
+ v3s16(0,0,1), // back
+ v3s16(1,0,0), // right
+ v3s16(0,0,-1), // front
+ v3s16(-1,0,0), // left
+ };
+ for(u16 i=0; i<5; i++)
+ {
+ try
+ {
+
+ bool to_bottom = (i == 0);
+
+ // If liquid is at lowest possible height, it's not going
+ // anywhere except down
+ if(liquid_level == 0 && to_bottom == false)
+ continue;
+
+ u8 liquid_next_level = 0;
+ // If going to bottom
+ if(to_bottom)
+ {
+ //liquid_next_level = 7;
+ if(liquid_level >= 7 - WATER_DROP_BOOST)
+ liquid_next_level = 7;
+ else
+ liquid_next_level = liquid_level + WATER_DROP_BOOST;
+ }
+ else
+ liquid_next_level = liquid_level - 1;
+
+ bool n2_changed = false;
+ bool flowed = false;
+
+ v3s16 p2 = p0 + dirs_to[i];
+
+ MapNode n2 = getNode(p2);
+ //dstream<<"[1] n2.param="<<(int)n2.param<<std::endl;
+
+ if(content_liquid(n2.d))
+ {
+ u8 n2_nonsource_c = make_liquid_flowing(n2.d);
+ // Check that the liquids are the same type
+ if(n2_nonsource_c != nonsource_c)
+ {
+ dstream<<"WARNING: Not handling: different liquids"
+ " collide"<<std::endl;
+ continue;
+ }
+ bool n2_is_source = !content_flowing_liquid(n2.d);
+ u8 n2_liquid_level = 8;
+ if(n2_is_source == false)
+ n2_liquid_level = n2.param2 & 0x07;
+
+ if(to_bottom)
+ {
+ flowed = true;
+ }
+
+ if(n2_is_source)
+ {
+ // Just flow into the source, nothing changes.
+ // n2_changed is not set because destination didn't change
+ flowed = true;
+ }
+ else
+ {
+ if(liquid_next_level > liquid_level)
+ {
+ n2.param2 = liquid_next_level;
+ setNode(p2, n2);
+
+ n2_changed = true;
+ flowed = true;
+ }
+ }
+ }
+ else if(n2.d == CONTENT_AIR)
+ {
+ n2.d = nonsource_c;
+ n2.param2 = liquid_next_level;
+ setNode(p2, n2);
+
+ n2_changed = true;
+ flowed = true;
+ }
+
+ //dstream<<"[2] n2.param="<<(int)n2.param<<std::endl;
+
+ if(n2_changed)
+ {
+ m_transforming_liquid.push_back(p2);
+
+ v3s16 blockpos = getNodeBlockPos(p2);
+ MapBlock *block = getBlockNoCreateNoEx(blockpos);
+ if(block != NULL)
+ modified_blocks.insert(blockpos, block);
+ }
+
+ // If n2_changed to bottom, don't flow anywhere else
+ if(to_bottom && flowed && !is_source)
+ break;
+
+ }catch(InvalidPositionException &e)
+ {
+ }
+ }
+
+ loopcount++;
+ //if(loopcount >= 100000)
+ if(loopcount >= initial_size * 1)
+ break;
+ }
+ //dstream<<"Map::transformLiquids(): loopcount="<<loopcount<<std::endl;
+}
+
+NodeMetadata* Map::getNodeMetadata(v3s16 p)
+{
+ v3s16 blockpos = getNodeBlockPos(p);
+ v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
+ MapBlock *block = getBlockNoCreateNoEx(blockpos);
+ if(block == NULL)
+ {
+ dstream<<"WARNING: Map::setNodeMetadata(): Block not found"
+ <<std::endl;
+ return NULL;
+ }
+ NodeMetadata *meta = block->m_node_metadata.get(p_rel);
+ return meta;
+}
+
+void Map::setNodeMetadata(v3s16 p, NodeMetadata *meta)
+{
+ v3s16 blockpos = getNodeBlockPos(p);
+ v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
+ MapBlock *block = getBlockNoCreateNoEx(blockpos);
+ if(block == NULL)
+ {
+ dstream<<"WARNING: Map::setNodeMetadata(): Block not found"
+ <<std::endl;
+ return;
+ }
+ block->m_node_metadata.set(p_rel, meta);
+}
+
+void Map::removeNodeMetadata(v3s16 p)
+{
+ v3s16 blockpos = getNodeBlockPos(p);
+ v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
+ MapBlock *block = getBlockNoCreateNoEx(blockpos);
+ if(block == NULL)
+ {
+ dstream<<"WARNING: Map::removeNodeMetadata(): Block not found"
+ <<std::endl;
+ return;
+ }
+ block->m_node_metadata.remove(p_rel);
+}
+
+void Map::nodeMetadataStep(float dtime,
+ core::map<v3s16, MapBlock*> &changed_blocks)
+{
+ /*
+ NOTE:
+ Currently there is no way to ensure that all the necessary
+ blocks are loaded when this is run. (They might get unloaded)
+ NOTE: ^- Actually, that might not be so. In a quick test it
+ reloaded a block with a furnace when I walked back to it from
+ a distance.
+ */
+ core::map<v2s16, MapSector*>::Iterator si;
+ si = m_sectors.getIterator();
+ for(; si.atEnd() == false; si++)
+ {
+ MapSector *sector = si.getNode()->getValue();
+ core::list< MapBlock * > sectorblocks;
+ sector->getBlocks(sectorblocks);
+ core::list< MapBlock * >::Iterator i;
+ for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++)
+ {
+ MapBlock *block = *i;
+ bool changed = block->m_node_metadata.step(dtime);
+ if(changed)
+ changed_blocks[block->getPos()] = block;
+ }
+ }
+}
+
+/*
+ ServerMap
+*/
+
+ServerMap::ServerMap(std::string savedir):
+ Map(dout_server),
+ m_seed(0),
+ m_map_metadata_changed(true)
+{
+ dstream<<__FUNCTION_NAME<<std::endl;
+
+ //m_chunksize = 64;
+ //m_chunksize = 16; // Too slow
+ m_chunksize = 8; // Takes a few seconds
+ //m_chunksize = 4;
+ //m_chunksize = 2;
+
+ m_seed = (((u64)(myrand()%0xffff)<<0)
+ + ((u64)(myrand()%0xffff)<<16)
+ + ((u64)(myrand()%0xffff)<<32)
+ + ((u64)(myrand()%0xffff)<<48));
+
+ /*
+ Experimental and debug stuff
+ */
+
+ {
+ }
+
+ /*
+ Try to load map; if not found, create a new one.
+ */
+
+ m_savedir = savedir;
+ m_map_saving_enabled = false;
+
+ try
+ {
+ // If directory exists, check contents and load if possible
+ if(fs::PathExists(m_savedir))
+ {
+ // If directory is empty, it is safe to save into it.
+ if(fs::GetDirListing(m_savedir).size() == 0)
+ {
+ dstream<<DTIME<<"Server: Empty save directory is valid."
+ <<std::endl;
+ m_map_saving_enabled = true;
+ }
+ else
+ {
+ try{
+ // Load map metadata (seed, chunksize)
+ loadMapMeta();
+
+ // Load chunk metadata
+ loadChunkMeta();
+ }
+ catch(FileNotGoodException &e){
+ dstream<<DTIME<<"WARNING: Server: Could not load "
+ <<"metafile(s). Disabling chunk-based "
+ <<"generation."<<std::endl;
+ m_chunksize = 0;
+ }
+
+ /*// Load sector (0,0) and throw and exception on fail
+ if(loadSectorFull(v2s16(0,0)) == false)
+ throw LoadError("Failed to load sector (0,0)");*/
+
+ /*dstream<<DTIME<<"Server: Successfully loaded chunk "
+ "metadata and sector (0,0) from "<<savedir<<
+ ", assuming valid save directory."
+ <<std::endl;*/
+
+ dstream<<DTIME<<"INFO: Server: Successfully loaded map "
+ <<"and chunk metadata from "<<savedir
+ <<", assuming valid save directory."
+ <<std::endl;
+
+ m_map_saving_enabled = true;
+ // Map loaded, not creating new one
+ return;
+ }
+ }
+ // If directory doesn't exist, it is safe to save to it
+ else{
+ m_map_saving_enabled = true;
+ }
+ }
+ catch(std::exception &e)
+ {
+ dstream<<DTIME<<"WARNING: Server: Failed to load map from "<<savedir
+ <<", exception: "<<e.what()<<std::endl;
+ dstream<<"Please remove the map or fix it."<<std::endl;
+ dstream<<"WARNING: Map saving will be disabled."<<std::endl;
+ }
+
+ dstream<<DTIME<<"INFO: Initializing new map."<<std::endl;
+
+ // Create zero sector
+ emergeSector(v2s16(0,0));
+
+ // Initially write whole map
+ save(false);
+}
+
+ServerMap::~ServerMap()
+{
+ dstream<<__FUNCTION_NAME<<std::endl;
+
+ try
+ {
+ if(m_map_saving_enabled)
+ {
+ //save(false);
+ // Save only changed parts
+ save(true);
+ dstream<<DTIME<<"Server: saved map to "<<m_savedir<<std::endl;
+ }
+ else
+ {
+ dstream<<DTIME<<"Server: map not saved"<<std::endl;
+ }
+ }
+ catch(std::exception &e)
+ {
+ dstream<<DTIME<<"Server: Failed to save map to "<<m_savedir
+ <<", exception: "<<e.what()<<std::endl;
+ }
+
+ /*
+ Free all MapChunks
+ */
+ core::map<v2s16, MapChunk*>::Iterator i = m_chunks.getIterator();
+ for(; i.atEnd() == false; i++)
+ {
+ MapChunk *chunk = i.getNode()->getValue();
+ delete chunk;
+ }
+}
+
+/*
+ Some helper functions for the map generator
+*/
+
+s16 find_ground_level(VoxelManipulator &vmanip, v2s16 p2d)
+{
+ v3s16 em = vmanip.m_area.getExtent();
+ s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
+ s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
+ u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
+ s16 y;
+ for(y=y_nodes_max; y>=y_nodes_min; y--)
+ {
+ MapNode &n = vmanip.m_data[i];
+ if(content_walkable(n.d))
+ break;
+
+ vmanip.m_area.add_y(em, i, -1);
+ }
+ if(y >= y_nodes_min)
+ return y;
+ else
+ return y_nodes_min;
+}
+
+s16 find_ground_level_clever(VoxelManipulator &vmanip, v2s16 p2d)
+{
+ v3s16 em = vmanip.m_area.getExtent();
+ s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
+ s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
+ u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
+ s16 y;
+ for(y=y_nodes_max; y>=y_nodes_min; y--)
+ {
+ MapNode &n = vmanip.m_data[i];
+ if(content_walkable(n.d)
+ && n.d != CONTENT_TREE
+ && n.d != CONTENT_LEAVES)
+ break;
+
+ vmanip.m_area.add_y(em, i, -1);
+ }
+ if(y >= y_nodes_min)
+ return y;
+ else
+ return y_nodes_min;
+}
+
+void make_tree(VoxelManipulator &vmanip, v3s16 p0)
+{
+ MapNode treenode(CONTENT_TREE);
+ MapNode leavesnode(CONTENT_LEAVES);
+
+ s16 trunk_h = myrand_range(3, 6);
+ v3s16 p1 = p0;
+ for(s16 ii=0; ii<trunk_h; ii++)
+ {
+ if(vmanip.m_area.contains(p1))
+ vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
+ p1.Y++;
+ }
+
+ // p1 is now the last piece of the trunk
+ p1.Y -= 1;
+
+ VoxelArea leaves_a(v3s16(-2,-2,-2), v3s16(2,2,2));
+ //SharedPtr<u8> leaves_d(new u8[leaves_a.getVolume()]);
+ Buffer<u8> leaves_d(leaves_a.getVolume());
+ for(s32 i=0; i<leaves_a.getVolume(); i++)
+ leaves_d[i] = 0;
+
+ // Force leaves at near the end of the trunk
+ {
+ s16 d = 1;
+ for(s16 z=-d; z<=d; z++)
+ for(s16 y=-d; y<=d; y++)
+ for(s16 x=-d; x<=d; x++)
+ {
+ leaves_d[leaves_a.index(v3s16(x,y,z))] = 1;
+ }
+ }
+
+ // Add leaves randomly
+ for(u32 iii=0; iii<7; iii++)
+ {
+ s16 d = 1;
+
+ v3s16 p(
+ myrand_range(leaves_a.MinEdge.X, leaves_a.MaxEdge.X-d),
+ myrand_range(leaves_a.MinEdge.Y, leaves_a.MaxEdge.Y-d),
+ myrand_range(leaves_a.MinEdge.Z, leaves_a.MaxEdge.Z-d)
+ );
+
+ for(s16 z=0; z<=d; z++)
+ for(s16 y=0; y<=d; y++)
+ for(s16 x=0; x<=d; x++)
+ {
+ leaves_d[leaves_a.index(p+v3s16(x,y,z))] = 1;
+ }
+ }
+
+ // Blit leaves to vmanip
+ for(s16 z=leaves_a.MinEdge.Z; z<=leaves_a.MaxEdge.Z; z++)
+ for(s16 y=leaves_a.MinEdge.Y; y<=leaves_a.MaxEdge.Y; y++)
+ for(s16 x=leaves_a.MinEdge.X; x<=leaves_a.MaxEdge.X; x++)
+ {
+ v3s16 p(x,y,z);
+ p += p1;
+ if(vmanip.m_area.contains(p) == false)
+ continue;
+ u32 vi = vmanip.m_area.index(p);
+ if(vmanip.m_data[vi].d != CONTENT_AIR)
+ continue;
+ u32 i = leaves_a.index(x,y,z);
+ if(leaves_d[i] == 1)
+ vmanip.m_data[vi] = leavesnode;
+ }
+}
+
+/*
+ Helpers for noise
+*/
+
+/*// -1->0, 0->1, 1->0
+double contour(double v)
+{
+ v = fabs(v);
+ if(v >= 1.0)
+ return 0.0;
+ return (1.0-v);
+}*/
+
+/*
+ Noise functions. Make sure seed is mangled differently in each one.
+*/
+
+// This affects the shape of the contour
+#define CAVE_NOISE_SCALE 10.0
+
+NoiseParams get_cave_noise1_params(u64 seed)
+{
+ return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 5, 0.7,
+ 200, CAVE_NOISE_SCALE);
+}
+
+NoiseParams get_cave_noise2_params(u64 seed)
+{
+ return NoiseParams(NOISE_PERLIN_CONTOUR_FLIP_YZ, seed+10325, 5, 0.7,
+ 200, CAVE_NOISE_SCALE);
+}
+
+#define CAVE_NOISE_THRESHOLD (2.5/CAVE_NOISE_SCALE)
+
+bool is_cave(u64 seed, v3s16 p)
+{
+ double d1 = noise3d_param(get_cave_noise1_params(seed), p.X,p.Y,p.Z);
+ double d2 = noise3d_param(get_cave_noise2_params(seed), p.X,p.Y,p.Z);
+ return d1*d2 > CAVE_NOISE_THRESHOLD;
+}
+
+// Amount of trees per area in nodes
+double tree_amount_2d(u64 seed, v2s16 p)
+{
+ double noise = noise2d_perlin(
+ 0.5+(float)p.X/250, 0.5+(float)p.Y/250,
+ seed+2, 5, 0.66);
+ double zeroval = -0.3;
+ if(noise < zeroval)
+ return 0;
+ else
+ return 0.04 * (noise-zeroval) / (1.0-zeroval);
+}
+
+#define AVERAGE_MUD_AMOUNT 4
+
+double base_rock_level_2d(u64 seed, v2s16 p)
+{
+ // The base ground level
+ double base = (double)WATER_LEVEL - (double)AVERAGE_MUD_AMOUNT
+ + 20. * noise2d_perlin(
+ 0.5+(float)p.X/500., 0.5+(float)p.Y/500.,
+ (seed>>32)+654879876, 6, 0.6);
+
+ /*// A bit hillier one
+ double base2 = WATER_LEVEL - 4.0 + 40. * noise2d_perlin(
+ 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
+ (seed>>27)+90340, 6, 0.69);
+ if(base2 > base)
+ base = base2;*/
+#if 1
+ // Higher ground level
+ double higher = (double)WATER_LEVEL + 25. + 35. * noise2d_perlin(
+ 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
+ seed+85039, 5, 0.69);
+ //higher = 30; // For debugging
+
+ // Limit higher to at least base
+ if(higher < base)
+ higher = base;
+
+ // Steepness factor of cliffs
+ double b = 1.0 + 1.0 * noise2d_perlin(
+ 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
+ seed-932, 7, 0.7);
+ b = rangelim(b, 0.0, 1000.0);
+ b = pow(b, 5);
+ b *= 7;
+ b = rangelim(b, 3.0, 1000.0);
+ //dstream<<"b="<<b<<std::endl;
+ //double b = 20;
+
+ // Offset to more low
+ double a_off = -0.2;
+ // High/low selector
+ /*double a = 0.5 + b * (a_off + noise2d_perlin(
+ 0.5+(float)p.X/500., 0.5+(float)p.Y/500.,
+ seed-359, 6, 0.7));*/
+ double a = (double)0.5 + b * (a_off + noise2d_perlin(
+ 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
+ seed-359, 5, 0.60));
+ // Limit
+ a = rangelim(a, 0.0, 1.0);
+
+ //dstream<<"a="<<a<<std::endl;
+
+ double h = base*(1.0-a) + higher*a;
+#else
+ double h = base;
+#endif
+ return h;
+}
+
+double get_mud_add_amount(u64 seed, v2s16 p)
+{
+ return ((float)AVERAGE_MUD_AMOUNT + 3.0 * noise2d_perlin(
+ 0.5+(float)p.X/200, 0.5+(float)p.Y/200,
+ seed+91013, 3, 0.55));
+}
+
+/*
+ Adds random objects to block, depending on the content of the block
+*/
+void addRandomObjects(MapBlock *block)
+{
+ for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
+ for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
+ {
+ bool last_node_walkable = false;
+ for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
+ {
+ v3s16 p(x0,y0,z0);
+ MapNode n = block->getNodeNoEx(p);
+ if(n.d == CONTENT_IGNORE)
+ continue;
+ if(content_features(n.d).liquid_type != LIQUID_NONE)
+ continue;
+ if(content_features(n.d).walkable)
+ {
+ last_node_walkable = true;
+ continue;
+ }
+ if(last_node_walkable)
+ {
+ // If block contains light information
+ if(content_features(n.d).param_type == CPT_LIGHT)
+ {
+ if(n.getLight(LIGHTBANK_DAY) <= 3)
+ {
+ if(myrand() % 300 == 0)
+ {
+ v3f pos_f = intToFloat(p+block->getPosRelative(), BS);
+ pos_f.Y -= BS*0.4;
+ ServerActiveObject *obj = new RatSAO(NULL, 0, pos_f);
+ std::string data = obj->getStaticData();
+ StaticObject s_obj(obj->getType(),
+ obj->getBasePosition(), data);
+ // Add some
+ block->m_static_objects.insert(0, s_obj);
+ block->m_static_objects.insert(0, s_obj);
+ block->m_static_objects.insert(0, s_obj);
+ block->m_static_objects.insert(0, s_obj);
+ block->m_static_objects.insert(0, s_obj);
+ block->m_static_objects.insert(0, s_obj);
+ delete obj;
+ }
+ if(myrand() % 300 == 0)
+ {
+ v3f pos_f = intToFloat(p+block->getPosRelative(), BS);
+ pos_f.Y -= BS*0.4;
+ ServerActiveObject *obj = new Oerkki1SAO(NULL,0,pos_f);
+ std::string data = obj->getStaticData();
+ StaticObject s_obj(obj->getType(),
+ obj->getBasePosition(), data);
+ // Add one
+ block->m_static_objects.insert(0, s_obj);
+ delete obj;
+ }
+ }
+ }
+ }
+ last_node_walkable = false;
+ }
+ }
+ block->setChangedFlag();
+}
+
+/*
+ This is the main map generation method
+*/
+
+#define VMANIP_FLAG_DUNGEON VOXELFLAG_CHECKED1
+
+void makeChunk(ChunkMakeData *data)
+{
+ if(data->no_op)
+ return;
+
+ s16 y_nodes_min = data->y_blocks_min * MAP_BLOCKSIZE;
+ s16 y_nodes_max = data->y_blocks_max * MAP_BLOCKSIZE + MAP_BLOCKSIZE - 1;
+ s16 h_blocks = data->y_blocks_max - data->y_blocks_min + 1;
+ u32 relative_volume = (u32)data->sectorpos_base_size*MAP_BLOCKSIZE
+ *(u32)data->sectorpos_base_size*MAP_BLOCKSIZE
+ *(u32)h_blocks*MAP_BLOCKSIZE;
+ v3s16 bigarea_blocks_min(
+ data->sectorpos_bigbase.X,
+ data->y_blocks_min,
+ data->sectorpos_bigbase.Y
+ );
+ v3s16 bigarea_blocks_max(
+ data->sectorpos_bigbase.X + data->sectorpos_bigbase_size - 1,
+ data->y_blocks_max,
+ data->sectorpos_bigbase.Y + data->sectorpos_bigbase_size - 1
+ );
+ s16 lighting_min_d = 0-data->max_spread_amount;
+ s16 lighting_max_d = data->sectorpos_base_size*MAP_BLOCKSIZE
+ + data->max_spread_amount-1;
+
+ // Clear all flags
+ data->vmanip.clearFlag(0xff);
+
+ TimeTaker timer_generate("makeChunk() generate");
+
+ // Maximum height of the stone surface and obstacles.
+ // This is used to disable cave generation from going too high.
+ s16 stone_surface_max_y = 0;
+
+ /*
+ Generate general ground level to full area
+ */
+ {
+ // 22ms @cs=8
+ TimeTaker timer1("Generating ground level");
+
+ //NoiseBuffer noisebuf1;
+ //NoiseBuffer noisebuf2;
+ NoiseBuffer noisebuf_cave;
+ {
+ v3f minpos_f(
+ data->sectorpos_bigbase.X*MAP_BLOCKSIZE,
+ y_nodes_min,
+ data->sectorpos_bigbase.Y*MAP_BLOCKSIZE
+ );
+ v3f maxpos_f = minpos_f + v3f(
+ data->sectorpos_bigbase_size*MAP_BLOCKSIZE,
+ y_nodes_max-y_nodes_min,
+ data->sectorpos_bigbase_size*MAP_BLOCKSIZE
+ );
+ //v3f samplelength_f = v3f(4.0, 4.0, 4.0);
+
+ TimeTaker timer("noisebuf.create");
+
+ noisebuf_cave.create(get_cave_noise1_params(data->seed),
+ minpos_f.X, minpos_f.Y, minpos_f.Z,
+ maxpos_f.X, maxpos_f.Y, maxpos_f.Z,
+ 4, 4, 4);
+
+ noisebuf_cave.multiply(get_cave_noise2_params(data->seed));
+
+ /*noisebuf1.create(data->seed+25104, 6, 0.60, 200.0, false,
+ minpos_f.X, minpos_f.Y, minpos_f.Z,
+ maxpos_f.X, maxpos_f.Y, maxpos_f.Z,
+ samplelength_f.X, samplelength_f.Y, samplelength_f.Z);*/
+ /*noisebuf1.create(data->seed+25104, 3, 0.60, 25.0, false,
+ minpos_f.X, minpos_f.Y, minpos_f.Z,
+ maxpos_f.X, maxpos_f.Y, maxpos_f.Z,
+ samplelength_f.X, samplelength_f.Y, samplelength_f.Z);
+ noisebuf2.create(data->seed+25105, 4, 0.50, 200.0, false,
+ minpos_f.X, minpos_f.Y, minpos_f.Z,
+ maxpos_f.X, maxpos_f.Y, maxpos_f.Z,
+ samplelength_f.X, samplelength_f.Y, samplelength_f.Z);*/
+ }
+
+#if 0
+ for(s16 x=0; x<data->sectorpos_bigbase_size*MAP_BLOCKSIZE; x++)
+ for(s16 z=0; z<data->sectorpos_bigbase_size*MAP_BLOCKSIZE; z++)
+ {
+ // Node position
+ v2s16 p2d = data->sectorpos_bigbase*MAP_BLOCKSIZE + v2s16(x,z);
+
+ // Ground height at this point
+ float surface_y_f = 0.0;
+
+ // Use perlin noise for ground height
+ surface_y_f = base_rock_level_2d(data->seed, p2d);
+ //surface_y_f = base_rock_level_2d(data->seed, p2d);
+
+ // Convert to integer
+ s16 surface_y = (s16)surface_y_f;
+
+ // Log it
+ if(surface_y > stone_surface_max_y)
+ stone_surface_max_y = surface_y;
+
+ /*
+ Fill ground with stone
+ */
+ {
+ // Use fast index incrementing
+ v3s16 em = data->vmanip.m_area.getExtent();
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_nodes_min, p2d.Y));
+ for(s16 y=y_nodes_min; y<=y_nodes_max; y++)
+ {
+ // Skip if already generated.
+ // This is done here because there might be a cave at
+ // any point in ground, which could look like it
+ // wasn't generated.
+ if(data->vmanip.m_data[i].d != CONTENT_AIR)
+ break;
+
+ /*s16 noiseval = 50.0 * noise3d_perlin(
+ 0.5+(float)p2d.X/100.0,
+ 0.5+(float)y/100.0,
+ 0.5+(float)p2d.Y/100.0,
+ data->seed+123, 5, 0.5);*/
+ double noiseval = 64.0 * noisebuf1.get(p2d.X, y, p2d.Y);
+ /*double noiseval = 30.0 * noisebuf1.get(p2d.X, y, p2d.Y);
+ noiseval *= MYMAX(0, -0.2 + noisebuf2.get(p2d.X, y, p2d.Y));*/
+
+ //if(y < surface_y + noiseval)
+ if(noiseval > 0)
+ //if(noiseval > y)
+ data->vmanip.m_data[i].d = CONTENT_STONE;
+
+ data->vmanip.m_area.add_y(em, i, 1);
+ }
+ }
+ }
+#endif
+
+#if 1
+ for(s16 x=0; x<data->sectorpos_bigbase_size*MAP_BLOCKSIZE; x++)
+ for(s16 z=0; z<data->sectorpos_bigbase_size*MAP_BLOCKSIZE; z++)
+ {
+ // Node position
+ v2s16 p2d = data->sectorpos_bigbase*MAP_BLOCKSIZE + v2s16(x,z);
+
+ /*
+ Skip of already generated
+ */
+ /*{
+ v3s16 p(p2d.X, y_nodes_min, p2d.Y);
+ if(data->vmanip.m_data[data->vmanip.m_area.index(p)].d != CONTENT_AIR)
+ continue;
+ }*/
+
+#define CAVE_TEST 0
+
+#if CAVE_TEST
+ /*
+ Fill ground with stone
+ */
+ {
+ // Use fast index incrementing
+ v3s16 em = data->vmanip.m_area.getExtent();
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_nodes_min, p2d.Y));
+ for(s16 y=y_nodes_min; y<=y_nodes_max; y++)
+ {
+ // Skip if already generated.
+ // This is done here because there might be a cave at
+ // any point in ground, which could look like it
+ // wasn't generated.
+ if(data->vmanip.m_data[i].d != CONTENT_AIR)
+ break;
+
+ //if(is_cave(data->seed, v3s16(p2d.X, y, p2d.Y)))
+ if(noisebuf_cave.get(p2d.X,y,p2d.Y) > CAVE_NOISE_THRESHOLD)
+ {
+ data->vmanip.m_data[i].d = CONTENT_STONE;
+ }
+ else
+ {
+ data->vmanip.m_data[i].d = CONTENT_AIR;
+ }
+
+ data->vmanip.m_area.add_y(em, i, 1);
+ }
+ }
+#else
+ // Ground height at this point
+ float surface_y_f = 0.0;
+
+ // Use perlin noise for ground height
+ surface_y_f = base_rock_level_2d(data->seed, p2d);
+
+ // Convert to integer
+ s16 surface_y = (s16)surface_y_f;
+
+ // Log it
+ if(surface_y > stone_surface_max_y)
+ stone_surface_max_y = surface_y;
+
+ /*
+ Fill ground with stone
+ */
+ {
+ // Use fast index incrementing
+ v3s16 em = data->vmanip.m_area.getExtent();
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_nodes_min, p2d.Y));
+ for(s16 y=y_nodes_min; y<surface_y && y<=y_nodes_max; y++)
+ {
+ // Skip if already generated.
+ // This is done here because there might be a cave at
+ // any point in ground, which could look like it
+ // wasn't generated.
+ if(data->vmanip.m_data[i].d != CONTENT_AIR)
+ break;
+
+ //if(is_cave(data->seed, v3s16(p2d.X, y, p2d.Y)))
+ if(noisebuf_cave.get(p2d.X,y,p2d.Y) > CAVE_NOISE_THRESHOLD)
+ {
+ // Set tunnel flag
+ data->vmanip.m_flags[i] |= VMANIP_FLAG_DUNGEON;
+ // Is now air
+ data->vmanip.m_data[i].d = CONTENT_AIR;
+ }
+ else
+ {
+ data->vmanip.m_data[i].d = CONTENT_STONE;
+ }
+
+ data->vmanip.m_area.add_y(em, i, 1);
+ }
+ }
+#endif // !CAVE_TEST
+ }
+#endif
+
+ }//timer1
+
+ /*
+ Randomize some parameters
+ */
+
+ //s32 stone_obstacle_count = 0;
+ /*s32 stone_obstacle_count =
+ rangelim((1.0+noise2d(data->seed+897,
+ data->sectorpos_base.X, data->sectorpos_base.Y))/2.0 * 30, 0, 100000);*/
+
+ //s16 stone_obstacle_max_height = 0;
+ /*s16 stone_obstacle_max_height =
+ rangelim((1.0+noise2d(data->seed+5902,
+ data->sectorpos_base.X, data->sectorpos_base.Y))/2.0 * 30, 0, 100000);*/
+
+ // Set to 0 to disable everything but lighting
+#if 1
+
+ /*
+ Loop this part, it will make stuff look older and newer nicely
+ */
+ const u32 age_loops = 2;
+ for(u32 i_age=0; i_age<age_loops; i_age++)
+ { // Aging loop
+ /******************************
+ BEGINNING OF AGING LOOP
+ ******************************/
+
+#if 1
+ {
+ // 24ms @cs=8
+ //TimeTaker timer1("caves");
+
+ /*
+ Make caves
+ */
+ //u32 caves_count = relative_volume / 400000;
+ u32 caves_count = 0;
+ u32 bruises_count = relative_volume * stone_surface_max_y / 40000000;
+ if(stone_surface_max_y < WATER_LEVEL)
+ bruises_count = 0;
+ /*u32 caves_count = 0;
+ u32 bruises_count = 0;*/
+ for(u32 jj=0; jj<caves_count+bruises_count; jj++)
+ {
+ s16 min_tunnel_diameter = 3;
+ s16 max_tunnel_diameter = 5;
+ u16 tunnel_routepoints = 20;
+
+ v3f main_direction(0,0,0);
+
+ bool bruise_surface = (jj > caves_count);
+
+ if(bruise_surface)
+ {
+ min_tunnel_diameter = 5;
+ max_tunnel_diameter = myrand_range(10, 20);
+ /*min_tunnel_diameter = MYMAX(0, stone_surface_max_y/6);
+ max_tunnel_diameter = myrand_range(MYMAX(0, stone_surface_max_y/6), MYMAX(0, stone_surface_max_y/2));*/
+
+ /*s16 tunnel_rou = rangelim(25*(0.5+1.0*noise2d(data->seed+42,
+ data->sectorpos_base.X, data->sectorpos_base.Y)), 0, 15);*/
+
+ tunnel_routepoints = 5;
+ }
+ else
+ {
+ }
+
+ // Allowed route area size in nodes
+ v3s16 ar(
+ data->sectorpos_base_size*MAP_BLOCKSIZE,
+ h_blocks*MAP_BLOCKSIZE,
+ data->sectorpos_base_size*MAP_BLOCKSIZE
+ );
+
+ // Area starting point in nodes
+ v3s16 of(
+ data->sectorpos_base.X*MAP_BLOCKSIZE,
+ data->y_blocks_min*MAP_BLOCKSIZE,
+ data->sectorpos_base.Y*MAP_BLOCKSIZE
+ );
+
+ // Allow a bit more
+ //(this should be more than the maximum radius of the tunnel)
+ //s16 insure = 5; // Didn't work with max_d = 20
+ s16 insure = 10;
+ s16 more = data->max_spread_amount - max_tunnel_diameter/2 - insure;
+ ar += v3s16(1,0,1) * more * 2;
+ of -= v3s16(1,0,1) * more;
+
+ s16 route_y_min = 0;
+ // Allow half a diameter + 7 over stone surface
+ s16 route_y_max = -of.Y + stone_surface_max_y + max_tunnel_diameter/2 + 7;
+
+ /*// If caves, don't go through surface too often
+ if(bruise_surface == false)
+ route_y_max -= myrand_range(0, max_tunnel_diameter*2);*/
+
+ // Limit maximum to area
+ route_y_max = rangelim(route_y_max, 0, ar.Y-1);
+
+ if(bruise_surface)
+ {
+ /*// Minimum is at y=0
+ route_y_min = -of.Y - 0;*/
+ // Minimum is at y=max_tunnel_diameter/4
+ //route_y_min = -of.Y + max_tunnel_diameter/4;
+ //s16 min = -of.Y + max_tunnel_diameter/4;
+ s16 min = -of.Y + 0;
+ route_y_min = myrand_range(min, min + max_tunnel_diameter);
+ route_y_min = rangelim(route_y_min, 0, route_y_max);
+ }
+
+ /*dstream<<"route_y_min = "<<route_y_min
+ <<", route_y_max = "<<route_y_max<<std::endl;*/
+
+ s16 route_start_y_min = route_y_min;
+ s16 route_start_y_max = route_y_max;
+
+ // Start every 2nd cave from surface
+ bool coming_from_surface = (jj % 2 == 0 && bruise_surface == false);
+
+ if(coming_from_surface)
+ {
+ route_start_y_min = -of.Y + stone_surface_max_y + 10;
+ }
+
+ route_start_y_min = rangelim(route_start_y_min, 0, ar.Y-1);
+ route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y-1);
+
+ // Randomize starting position
+ v3f orp(
+ (float)(myrand()%ar.X)+0.5,
+ (float)(myrand_range(route_start_y_min, route_start_y_max))+0.5,
+ (float)(myrand()%ar.Z)+0.5
+ );
+
+ MapNode airnode(CONTENT_AIR);
+
+ /*
+ Generate some tunnel starting from orp
+ */
+
+ for(u16 j=0; j<tunnel_routepoints; j++)
+ {
+ if(j%7==0 && bruise_surface == false)
+ {
+ main_direction = v3f(
+ ((float)(myrand()%20)-(float)10)/10,
+ ((float)(myrand()%20)-(float)10)/30,
+ ((float)(myrand()%20)-(float)10)/10
+ );
+ main_direction *= (float)myrand_range(1, 3);
+ }
+
+ // Randomize size
+ s16 min_d = min_tunnel_diameter;
+ s16 max_d = max_tunnel_diameter;
+ s16 rs = myrand_range(min_d, max_d);
+
+ v3s16 maxlen;
+ if(bruise_surface)
+ {
+ maxlen = v3s16(rs*7,rs*7,rs*7);
+ }
+ else
+ {
+ maxlen = v3s16(rs*4, myrand_range(1, rs*3), rs*4);
+ }
+
+ v3f vec;
+
+ if(coming_from_surface && j < 3)
+ {
+ vec = v3f(
+ (float)(myrand()%(maxlen.X*2))-(float)maxlen.X,
+ (float)(myrand()%(maxlen.Y*1))-(float)maxlen.Y,
+ (float)(myrand()%(maxlen.Z*2))-(float)maxlen.Z
+ );
+ }
+ else
+ {
+ vec = v3f(
+ (float)(myrand()%(maxlen.X*2))-(float)maxlen.X,
+ (float)(myrand()%(maxlen.Y*2))-(float)maxlen.Y,
+ (float)(myrand()%(maxlen.Z*2))-(float)maxlen.Z
+ );
+ }
+
+ vec += main_direction;
+
+ v3f rp = orp + vec;
+ if(rp.X < 0)
+ rp.X = 0;
+ else if(rp.X >= ar.X)
+ rp.X = ar.X-1;
+ if(rp.Y < route_y_min)
+ rp.Y = route_y_min;
+ else if(rp.Y >= route_y_max)
+ rp.Y = route_y_max-1;
+ if(rp.Z < 0)
+ rp.Z = 0;
+ else if(rp.Z >= ar.Z)
+ rp.Z = ar.Z-1;
+ vec = rp - orp;
+
+ for(float f=0; f<1.0; f+=1.0/vec.getLength())
+ {
+ v3f fp = orp + vec * f;
+ v3s16 cp(fp.X, fp.Y, fp.Z);
+
+ s16 d0 = -rs/2;
+ s16 d1 = d0 + rs - 1;
+ for(s16 z0=d0; z0<=d1; z0++)
+ {
+ //s16 si = rs - MYMAX(0, abs(z0)-rs/4);
+ s16 si = rs - MYMAX(0, abs(z0)-rs/7);
+ for(s16 x0=-si; x0<=si-1; x0++)
+ {
+ s16 maxabsxz = MYMAX(abs(x0), abs(z0));
+ //s16 si2 = rs - MYMAX(0, maxabsxz-rs/4);
+ s16 si2 = rs - MYMAX(0, maxabsxz-rs/7);
+ //s16 si2 = rs - abs(x0);
+ for(s16 y0=-si2+1+2; y0<=si2-1; y0++)
+ {
+ s16 z = cp.Z + z0;
+ s16 y = cp.Y + y0;
+ s16 x = cp.X + x0;
+ v3s16 p(x,y,z);
+ /*if(isInArea(p, ar) == false)
+ continue;*/
+ // Check only height
+ if(y < 0 || y >= ar.Y)
+ continue;
+ p += of;
+
+ //assert(data->vmanip.m_area.contains(p));
+ if(data->vmanip.m_area.contains(p) == false)
+ {
+ dstream<<"WARNING: "<<__FUNCTION_NAME
+ <<":"<<__LINE__<<": "
+ <<"point not in area"
+ <<std::endl;
+ continue;
+ }
+
+ // Just set it to air, it will be changed to
+ // water afterwards
+ u32 i = data->vmanip.m_area.index(p);
+ data->vmanip.m_data[i] = airnode;
+
+ if(bruise_surface == false)
+ {
+ // Set tunnel flag
+ data->vmanip.m_flags[i] |= VMANIP_FLAG_DUNGEON;
+ }
+ }
+ }
+ }
+ }
+
+ orp = rp;
+ }
+
+ }
+
+ }//timer1
+#endif
+
+#if 1
+ {
+ // 46ms @cs=8
+ //TimeTaker timer1("ore veins");
+
+ /*
+ Make ore veins
+ */
+ for(u32 jj=0; jj<relative_volume/1000; jj++)
+ {
+ s16 max_vein_diameter = 3;
+
+ // Allowed route area size in nodes
+ v3s16 ar(
+ data->sectorpos_base_size*MAP_BLOCKSIZE,
+ h_blocks*MAP_BLOCKSIZE,
+ data->sectorpos_base_size*MAP_BLOCKSIZE
+ );
+
+ // Area starting point in nodes
+ v3s16 of(
+ data->sectorpos_base.X*MAP_BLOCKSIZE,
+ data->y_blocks_min*MAP_BLOCKSIZE,
+ data->sectorpos_base.Y*MAP_BLOCKSIZE
+ );
+
+ // Allow a bit more
+ //(this should be more than the maximum radius of the tunnel)
+ s16 insure = 3;
+ s16 more = data->max_spread_amount - max_vein_diameter/2 - insure;
+ ar += v3s16(1,0,1) * more * 2;
+ of -= v3s16(1,0,1) * more;
+
+ // Randomize starting position
+ v3f orp(
+ (float)(myrand()%ar.X)+0.5,
+ (float)(myrand()%ar.Y)+0.5,
+ (float)(myrand()%ar.Z)+0.5
+ );
+
+ // Randomize mineral
+ u8 mineral;
+ if(myrand()%3 != 0)
+ mineral = MINERAL_COAL;
+ else
+ mineral = MINERAL_IRON;
+
+ /*
+ Generate some vein starting from orp
+ */
+
+ for(u16 j=0; j<2; j++)
+ {
+ /*v3f rp(
+ (float)(myrand()%ar.X)+0.5,
+ (float)(myrand()%ar.Y)+0.5,
+ (float)(myrand()%ar.Z)+0.5
+ );
+ v3f vec = rp - orp;*/
+
+ v3s16 maxlen(5, 5, 5);
+ v3f vec(
+ (float)(myrand()%(maxlen.X*2))-(float)maxlen.X,
+ (float)(myrand()%(maxlen.Y*2))-(float)maxlen.Y,
+ (float)(myrand()%(maxlen.Z*2))-(float)maxlen.Z
+ );
+ v3f rp = orp + vec;
+ if(rp.X < 0)
+ rp.X = 0;
+ else if(rp.X >= ar.X)
+ rp.X = ar.X;
+ if(rp.Y < 0)
+ rp.Y = 0;
+ else if(rp.Y >= ar.Y)
+ rp.Y = ar.Y;
+ if(rp.Z < 0)
+ rp.Z = 0;
+ else if(rp.Z >= ar.Z)
+ rp.Z = ar.Z;
+ vec = rp - orp;
+
+ // Randomize size
+ s16 min_d = 0;
+ s16 max_d = max_vein_diameter;
+ s16 rs = myrand_range(min_d, max_d);
+
+ for(float f=0; f<1.0; f+=1.0/vec.getLength())
+ {
+ v3f fp = orp + vec * f;
+ v3s16 cp(fp.X, fp.Y, fp.Z);
+ s16 d0 = -rs/2;
+ s16 d1 = d0 + rs - 1;
+ for(s16 z0=d0; z0<=d1; z0++)
+ {
+ s16 si = rs - abs(z0);
+ for(s16 x0=-si; x0<=si-1; x0++)
+ {
+ s16 si2 = rs - abs(x0);
+ for(s16 y0=-si2+1; y0<=si2-1; y0++)
+ {
+ // Don't put mineral to every place
+ if(myrand()%5 != 0)
+ continue;
+
+ s16 z = cp.Z + z0;
+ s16 y = cp.Y + y0;
+ s16 x = cp.X + x0;
+ v3s16 p(x,y,z);
+ /*if(isInArea(p, ar) == false)
+ continue;*/
+ // Check only height
+ if(y < 0 || y >= ar.Y)
+ continue;
+ p += of;
+
+ assert(data->vmanip.m_area.contains(p));
+
+ // Just set it to air, it will be changed to
+ // water afterwards
+ u32 i = data->vmanip.m_area.index(p);
+ MapNode *n = &data->vmanip.m_data[i];
+ if(n->d == CONTENT_STONE)
+ n->param = mineral;
+ }
+ }
+ }
+ }
+
+ orp = rp;
+ }
+
+ }
+
+ }//timer1
+#endif
+
+#if 1
+ {
+ // 15ms @cs=8
+ TimeTaker timer1("add mud");
+
+ /*
+ Add mud to the central chunk
+ */
+
+ for(s16 x=0; x<data->sectorpos_base_size*MAP_BLOCKSIZE; x++)
+ for(s16 z=0; z<data->sectorpos_base_size*MAP_BLOCKSIZE; z++)
+ {
+ // Node position in 2d
+ v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+
+ // Randomize mud amount
+ s16 mud_add_amount = get_mud_add_amount(data->seed, p2d) / 2.0;
+
+ // Find ground level
+ s16 surface_y = find_ground_level_clever(data->vmanip, p2d);
+
+ /*
+ If topmost node is grass, change it to mud.
+ It might be if it was flown to there from a neighboring
+ chunk and then converted.
+ */
+ {
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, surface_y, p2d.Y));
+ MapNode *n = &data->vmanip.m_data[i];
+ if(n->d == CONTENT_GRASS)
+ *n = MapNode(CONTENT_MUD);
+ //n->d = CONTENT_MUD;
+ }
+
+ /*
+ Add mud on ground
+ */
+ {
+ s16 mudcount = 0;
+ v3s16 em = data->vmanip.m_area.getExtent();
+ s16 y_start = surface_y+1;
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
+ for(s16 y=y_start; y<=y_nodes_max; y++)
+ {
+ if(mudcount >= mud_add_amount)
+ break;
+
+ MapNode &n = data->vmanip.m_data[i];
+ n = MapNode(CONTENT_MUD);
+ //n.d = CONTENT_MUD;
+ mudcount++;
+
+ data->vmanip.m_area.add_y(em, i, 1);
+ }
+ }
+
+ }
+
+ }//timer1
+#endif
+
+#if 1
+ {
+ // 340ms @cs=8
+ TimeTaker timer1("flow mud");
+
+ /*
+ Flow mud away from steep edges
+ */
+
+ // Limit area by 1 because mud is flown into neighbors.
+ s16 mudflow_minpos = 0-data->max_spread_amount+1;
+ s16 mudflow_maxpos = data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount-2;
+
+ // Iterate a few times
+ for(s16 k=0; k<3; k++)
+ {
+
+ for(s16 x=mudflow_minpos;
+ x<=mudflow_maxpos;
+ x++)
+ for(s16 z=mudflow_minpos;
+ z<=mudflow_maxpos;
+ z++)
+ {
+ // Invert coordinates every 2nd iteration
+ if(k%2 == 0)
+ {
+ x = mudflow_maxpos - (x-mudflow_minpos);
+ z = mudflow_maxpos - (z-mudflow_minpos);
+ }
+
+ // Node position in 2d
+ v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+
+ v3s16 em = data->vmanip.m_area.getExtent();
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
+ s16 y=y_nodes_max;
+
+ for(;; y--)
+ {
+ MapNode *n = NULL;
+ // Find mud
+ for(; y>=y_nodes_min; y--)
+ {
+ n = &data->vmanip.m_data[i];
+ //if(content_walkable(n->d))
+ // break;
+ if(n->d == CONTENT_MUD || n->d == CONTENT_GRASS)
+ break;
+
+ data->vmanip.m_area.add_y(em, i, -1);
+ }
+
+ // Stop if out of area
+ //if(data->vmanip.m_area.contains(i) == false)
+ if(y < y_nodes_min)
+ break;
+
+ /*// If not mud, do nothing to it
+ MapNode *n = &data->vmanip.m_data[i];
+ if(n->d != CONTENT_MUD && n->d != CONTENT_GRASS)
+ continue;*/
+
+ /*
+ Don't flow it if the stuff under it is not mud
+ */
+ {
+ u32 i2 = i;
+ data->vmanip.m_area.add_y(em, i2, -1);
+ // Cancel if out of area
+ if(data->vmanip.m_area.contains(i2) == false)
+ continue;
+ MapNode *n2 = &data->vmanip.m_data[i2];
+ if(n2->d != CONTENT_MUD && n2->d != CONTENT_GRASS)
+ continue;
+ }
+
+ // Make it exactly mud
+ n->d = CONTENT_MUD;
+
+ /*s16 recurse_count = 0;
+ mudflow_recurse:*/
+
+ v3s16 dirs4[4] = {
+ v3s16(0,0,1), // back
+ v3s16(1,0,0), // right
+ v3s16(0,0,-1), // front
+ v3s16(-1,0,0), // left
+ };
+
+ // Theck that upper is air or doesn't exist.
+ // Cancel dropping if upper keeps it in place
+ u32 i3 = i;
+ data->vmanip.m_area.add_y(em, i3, 1);
+ if(data->vmanip.m_area.contains(i3) == true
+ && content_walkable(data->vmanip.m_data[i3].d) == true)
+ {
+ continue;
+ }
+
+ // Drop mud on side
+
+ for(u32 di=0; di<4; di++)
+ {
+ v3s16 dirp = dirs4[di];
+ u32 i2 = i;
+ // Move to side
+ data->vmanip.m_area.add_p(em, i2, dirp);
+ // Fail if out of area
+ if(data->vmanip.m_area.contains(i2) == false)
+ continue;
+ // Check that side is air
+ MapNode *n2 = &data->vmanip.m_data[i2];
+ if(content_walkable(n2->d))
+ continue;
+ // Check that under side is air
+ data->vmanip.m_area.add_y(em, i2, -1);
+ if(data->vmanip.m_area.contains(i2) == false)
+ continue;
+ n2 = &data->vmanip.m_data[i2];
+ if(content_walkable(n2->d))
+ continue;
+ /*// Check that under that is air (need a drop of 2)
+ data->vmanip.m_area.add_y(em, i2, -1);
+ if(data->vmanip.m_area.contains(i2) == false)
+ continue;
+ n2 = &data->vmanip.m_data[i2];
+ if(content_walkable(n2->d))
+ continue;*/
+ // Loop further down until not air
+ do{
+ data->vmanip.m_area.add_y(em, i2, -1);
+ // Fail if out of area
+ if(data->vmanip.m_area.contains(i2) == false)
+ continue;
+ n2 = &data->vmanip.m_data[i2];
+ }while(content_walkable(n2->d) == false);
+ // Loop one up so that we're in air
+ data->vmanip.m_area.add_y(em, i2, 1);
+ n2 = &data->vmanip.m_data[i2];
+
+ // Move mud to new place
+ *n2 = *n;
+ // Set old place to be air
+ *n = MapNode(CONTENT_AIR);
+
+ // Done
+ break;
+ }
+ }
+ }
+
+ }
+
+ }//timer1
+#endif
+
+#if 1
+ {
+ // 50ms @cs=8
+ TimeTaker timer1("add water");
+
+ /*
+ Add water to the central chunk (and a bit more)
+ */
+
+ for(s16 x=0-data->max_spread_amount;
+ x<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount;
+ x++)
+ for(s16 z=0-data->max_spread_amount;
+ z<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount;
+ z++)
+ {
+ // Node position in 2d
+ v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+
+ // Find ground level
+ //s16 surface_y = find_ground_level(data->vmanip, p2d);
+
+ /*
+ If ground level is over water level, skip.
+ NOTE: This leaves caves near water without water,
+ which looks especially crappy when the nearby water
+ won't start flowing either for some reason
+ */
+ /*if(surface_y > WATER_LEVEL)
+ continue;*/
+
+ /*
+ Add water on ground
+ */
+ {
+ v3s16 em = data->vmanip.m_area.getExtent();
+ u8 light = LIGHT_MAX;
+ // Start at global water surface level
+ s16 y_start = WATER_LEVEL;
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
+ MapNode *n = &data->vmanip.m_data[i];
+
+ for(s16 y=y_start; y>=y_nodes_min; y--)
+ {
+ n = &data->vmanip.m_data[i];
+
+ // Stop when there is no water and no air
+ if(n->d != CONTENT_AIR && n->d != CONTENT_WATERSOURCE
+ && n->d != CONTENT_WATER)
+ {
+
+ break;
+ }
+
+ // Make water only not in caves
+ if(!(data->vmanip.m_flags[i]&VMANIP_FLAG_DUNGEON))
+ {
+ n->d = CONTENT_WATERSOURCE;
+ //n->setLight(LIGHTBANK_DAY, light);
+
+ // Add to transforming liquid queue (in case it'd
+ // start flowing)
+ v3s16 p = v3s16(p2d.X, y, p2d.Y);
+ data->transforming_liquid.push_back(p);
+ }
+
+ // Next one
+ data->vmanip.m_area.add_y(em, i, -1);
+ if(light > 0)
+ light--;
+ }
+ }
+
+ }
+
+ }//timer1
+#endif
+
+ } // Aging loop
+ /***********************
+ END OF AGING LOOP
+ ************************/
+
+#if 1
+ {
+ //TimeTaker timer1("convert mud to sand");
+
+ /*
+ Convert mud to sand
+ */
+
+ //s16 mud_add_amount = myrand_range(2, 4);
+ //s16 mud_add_amount = 0;
+
+ /*for(s16 x=0; x<data->sectorpos_base_size*MAP_BLOCKSIZE; x++)
+ for(s16 z=0; z<data->sectorpos_base_size*MAP_BLOCKSIZE; z++)*/
+ for(s16 x=0-data->max_spread_amount+1;
+ x<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount-1;
+ x++)
+ for(s16 z=0-data->max_spread_amount+1;
+ z<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount-1;
+ z++)
+ {
+ // Node position in 2d
+ v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+
+ // Determine whether to have sand here
+ double sandnoise = noise2d_perlin(
+ 0.5+(float)p2d.X/500, 0.5+(float)p2d.Y/500,
+ data->seed+59420, 3, 0.50);
+
+ bool have_sand = (sandnoise > -0.15);
+
+ if(have_sand == false)
+ continue;
+
+ // Find ground level
+ s16 surface_y = find_ground_level_clever(data->vmanip, p2d);
+
+ if(surface_y > WATER_LEVEL + 2)
+ continue;
+
+ {
+ v3s16 em = data->vmanip.m_area.getExtent();
+ s16 y_start = surface_y;
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
+ u32 not_sand_counter = 0;
+ for(s16 y=y_start; y>=y_nodes_min; y--)
+ {
+ MapNode *n = &data->vmanip.m_data[i];
+ if(n->d == CONTENT_MUD || n->d == CONTENT_GRASS)
+ {
+ n->d = CONTENT_SAND;
+ }
+ else
+ {
+ not_sand_counter++;
+ if(not_sand_counter > 3)
+ break;
+ }
+
+ data->vmanip.m_area.add_y(em, i, -1);
+ }
+ }
+
+ }
+
+ }//timer1
+#endif
+
+#if 1
+ {
+ // 1ms @cs=8
+ //TimeTaker timer1("generate trees");
+
+ /*
+ Generate some trees
+ */
+ {
+ // Divide area into parts
+ s16 div = 8;
+ s16 sidelen = data->sectorpos_base_size*MAP_BLOCKSIZE / div;
+ double area = sidelen * sidelen;
+ for(s16 x0=0; x0<div; x0++)
+ for(s16 z0=0; z0<div; z0++)
+ {
+ // Center position of part of division
+ v2s16 p2d_center(
+ data->sectorpos_base.X*MAP_BLOCKSIZE + sidelen/2 + sidelen*x0,
+ data->sectorpos_base.Y*MAP_BLOCKSIZE + sidelen/2 + sidelen*z0
+ );
+ // Minimum edge of part of division
+ v2s16 p2d_min(
+ data->sectorpos_base.X*MAP_BLOCKSIZE + sidelen*x0,
+ data->sectorpos_base.Y*MAP_BLOCKSIZE + sidelen*z0
+ );
+ // Maximum edge of part of division
+ v2s16 p2d_max(
+ data->sectorpos_base.X*MAP_BLOCKSIZE + sidelen + sidelen*x0 - 1,
+ data->sectorpos_base.Y*MAP_BLOCKSIZE + sidelen + sidelen*z0 - 1
+ );
+ // Amount of trees
+ u32 tree_count = area * tree_amount_2d(data->seed, p2d_center);
+ // Put trees in random places on part of division
+ for(u32 i=0; i<tree_count; i++)
+ {
+ s16 x = myrand_range(p2d_min.X, p2d_max.X);
+ s16 z = myrand_range(p2d_min.Y, p2d_max.Y);
+ s16 y = find_ground_level(data->vmanip, v2s16(x,z));
+ // Don't make a tree under water level
+ if(y < WATER_LEVEL)
+ continue;
+ // Don't make a tree so high that it doesn't fit
+ if(y > y_nodes_max - 6)
+ continue;
+ v3s16 p(x,y,z);
+ /*
+ Trees grow only on mud and grass
+ */
+ {
+ u32 i = data->vmanip.m_area.index(v3s16(p));
+ MapNode *n = &data->vmanip.m_data[i];
+ if(n->d != CONTENT_MUD && n->d != CONTENT_GRASS)
+ continue;
+ }
+ p.Y++;
+ // Make a tree
+ make_tree(data->vmanip, p);
+ }
+ }
+ /*u32 tree_max = relative_area / 60;
+ //u32 count = myrand_range(0, tree_max);
+ for(u32 i=0; i<count; i++)
+ {
+ s16 x = myrand_range(0, data->sectorpos_base_size*MAP_BLOCKSIZE-1);
+ s16 z = myrand_range(0, data->sectorpos_base_size*MAP_BLOCKSIZE-1);
+ x += data->sectorpos_base.X*MAP_BLOCKSIZE;
+ z += data->sectorpos_base.Y*MAP_BLOCKSIZE;
+ s16 y = find_ground_level(data->vmanip, v2s16(x,z));
+ // Don't make a tree under water level
+ if(y < WATER_LEVEL)
+ continue;
+ v3s16 p(x,y+1,z);
+ // Make a tree
+ make_tree(data->vmanip, p);
+ }*/
+ }
+
+ }//timer1
+#endif
+
+#if 1
+ {
+ // 19ms @cs=8
+ //TimeTaker timer1("grow grass");
+
+ /*
+ Grow grass
+ */
+
+ /*for(s16 x=0-4; x<data->sectorpos_base_size*MAP_BLOCKSIZE+4; x++)
+ for(s16 z=0-4; z<data->sectorpos_base_size*MAP_BLOCKSIZE+4; z++)*/
+ for(s16 x=0-data->max_spread_amount;
+ x<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount;
+ x++)
+ for(s16 z=0-data->max_spread_amount;
+ z<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount;
+ z++)
+ {
+ // Node position in 2d
+ v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+
+ /*
+ Find the lowest surface to which enough light ends up
+ to make grass grow.
+
+ Basically just wait until not air and not leaves.
+ */
+ s16 surface_y = 0;
+ {
+ v3s16 em = data->vmanip.m_area.getExtent();
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
+ s16 y;
+ // Go to ground level
+ for(y=y_nodes_max; y>=y_nodes_min; y--)
+ {
+ MapNode &n = data->vmanip.m_data[i];
+ if(n.d != CONTENT_AIR
+ && n.d != CONTENT_LEAVES)
+ break;
+ data->vmanip.m_area.add_y(em, i, -1);
+ }
+ if(y >= y_nodes_min)
+ surface_y = y;
+ else
+ surface_y = y_nodes_min;
+ }
+
+ u32 i = data->vmanip.m_area.index(p2d.X, surface_y, p2d.Y);
+ MapNode *n = &data->vmanip.m_data[i];
+ if(n->d == CONTENT_MUD)
+ n->d = CONTENT_GRASS;
+ }
+
+ }//timer1
+#endif
+
+#endif // Disable all but lighting
+
+ /*
+ Initial lighting (sunlight)
+ */
+
+ core::map<v3s16, bool> light_sources;
+
+ {
+ // 750ms @cs=8, can't optimize more
+ TimeTaker timer1("initial lighting");
+
+ // NOTE: This is no used... umm... for some reason!
+#if 0
+ /*
+ Go through the edges and add all nodes that have light to light_sources
+ */
+
+ // Four edges
+ for(s16 i=0; i<4; i++)
+ // Edge length
+ for(s16 j=lighting_min_d;
+ j<=lighting_max_d;
+ j++)
+ {
+ s16 x;
+ s16 z;
+ // +-X
+ if(i == 0 || i == 1)
+ {
+ x = (i==0) ? lighting_min_d : lighting_max_d;
+ if(i == 0)
+ z = lighting_min_d;
+ else
+ z = lighting_max_d;
+ }
+ // +-Z
+ else
+ {
+ z = (i==0) ? lighting_min_d : lighting_max_d;
+ if(i == 0)
+ x = lighting_min_d;
+ else
+ x = lighting_max_d;
+ }
+
+ // Node position in 2d
+ v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+
+ {
+ v3s16 em = data->vmanip.m_area.getExtent();
+ s16 y_start = y_nodes_max;
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
+ for(s16 y=y_start; y>=y_nodes_min; y--)
+ {
+ MapNode *n = &data->vmanip.m_data[i];
+ if(n->getLight(LIGHTBANK_DAY) != 0)
+ {
+ light_sources.insert(v3s16(p2d.X, y, p2d.Y), true);
+ }
+ //NOTE: This is broken, at least the index has to
+ // be incremented
+ }
+ }
+ }
+#endif
+
+#if 1
+ /*
+ Go through the edges and apply sunlight to them, not caring
+ about neighbors
+ */
+
+ // Four edges
+ for(s16 i=0; i<4; i++)
+ // Edge length
+ for(s16 j=lighting_min_d;
+ j<=lighting_max_d;
+ j++)
+ {
+ s16 x;
+ s16 z;
+ // +-X
+ if(i == 0 || i == 1)
+ {
+ x = (i==0) ? lighting_min_d : lighting_max_d;
+ if(i == 0)
+ z = lighting_min_d;
+ else
+ z = lighting_max_d;
+ }
+ // +-Z
+ else
+ {
+ z = (i==0) ? lighting_min_d : lighting_max_d;
+ if(i == 0)
+ x = lighting_min_d;
+ else
+ x = lighting_max_d;
+ }
+
+ // Node position in 2d
+ v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+
+ // Loop from top to down
+ {
+ u8 light = LIGHT_SUN;
+ v3s16 em = data->vmanip.m_area.getExtent();
+ s16 y_start = y_nodes_max;
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
+ for(s16 y=y_start; y>=y_nodes_min; y--)
+ {
+ MapNode *n = &data->vmanip.m_data[i];
+ if(light_propagates_content(n->d) == false)
+ {
+ light = 0;
+ }
+ else if(light != LIGHT_SUN
+ || sunlight_propagates_content(n->d) == false)
+ {
+ if(light > 0)
+ light--;
+ }
+
+ n->setLight(LIGHTBANK_DAY, light);
+ n->setLight(LIGHTBANK_NIGHT, 0);
+
+ if(light != 0)
+ {
+ // Insert light source
+ light_sources.insert(v3s16(p2d.X, y, p2d.Y), true);
+ }
+
+ // Increment index by y
+ data->vmanip.m_area.add_y(em, i, -1);
+ }
+ }
+ }
+#endif
+
+ /*for(s16 x=0; x<data->sectorpos_base_size*MAP_BLOCKSIZE; x++)
+ for(s16 z=0; z<data->sectorpos_base_size*MAP_BLOCKSIZE; z++)*/
+ /*for(s16 x=0-data->max_spread_amount+1;
+ x<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount-1;
+ x++)
+ for(s16 z=0-data->max_spread_amount+1;
+ z<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount-1;
+ z++)*/
+#if 1
+ /*
+ This has to be 1 smaller than the actual area, because
+ neighboring nodes are checked.
+ */
+ for(s16 x=lighting_min_d+1;
+ x<=lighting_max_d-1;
+ x++)
+ for(s16 z=lighting_min_d+1;
+ z<=lighting_max_d-1;
+ z++)
+ {
+ // Node position in 2d
+ v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+
+ /*
+ Apply initial sunlight
+ */
+ {
+ u8 light = LIGHT_SUN;
+ bool add_to_sources = false;
+ v3s16 em = data->vmanip.m_area.getExtent();
+ s16 y_start = y_nodes_max;
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
+ for(s16 y=y_start; y>=y_nodes_min; y--)
+ {
+ MapNode *n = &data->vmanip.m_data[i];
+
+ if(light_propagates_content(n->d) == false)
+ {
+ light = 0;
+ }
+ else if(light != LIGHT_SUN
+ || sunlight_propagates_content(n->d) == false)
+ {
+ if(light > 0)
+ light--;
+ }
+
+ // This doesn't take much time
+ if(add_to_sources == false)
+ {
+ /*
+ Check sides. If side is not air or water, start
+ adding to light_sources.
+ */
+ v3s16 dirs4[4] = {
+ v3s16(0,0,1), // back
+ v3s16(1,0,0), // right
+ v3s16(0,0,-1), // front
+ v3s16(-1,0,0), // left
+ };
+ for(u32 di=0; di<4; di++)
+ {
+ v3s16 dirp = dirs4[di];
+ u32 i2 = i;
+ data->vmanip.m_area.add_p(em, i2, dirp);
+ MapNode *n2 = &data->vmanip.m_data[i2];
+ if(
+ n2->d != CONTENT_AIR
+ && n2->d != CONTENT_WATERSOURCE
+ && n2->d != CONTENT_WATER
+ ){
+ add_to_sources = true;
+ break;
+ }
+ }
+ }
+
+ n->setLight(LIGHTBANK_DAY, light);
+ n->setLight(LIGHTBANK_NIGHT, 0);
+
+ // This doesn't take much time
+ if(light != 0 && add_to_sources)
+ {
+ // Insert light source
+ light_sources.insert(v3s16(p2d.X, y, p2d.Y), true);
+ }
+
+ // Increment index by y
+ data->vmanip.m_area.add_y(em, i, -1);
+ }
+ }
+ }
+#endif
+
+ }//timer1
+
+ // Spread light around
+ {
+ TimeTaker timer("makeChunk() spreadLight");
+ data->vmanip.spreadLight(LIGHTBANK_DAY, light_sources);
+ }
+
+ /*
+ Generation ended
+ */
+
+ timer_generate.stop();
+}
+
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+
+void ServerMap::initChunkMake(ChunkMakeData &data, v2s16 chunkpos)
+{
+ if(m_chunksize == 0)
+ {
+ data.no_op = true;
+ return;
+ }
+
+ data.no_op = false;
+
+ // The distance how far into the neighbors the generator is allowed to go.
+ s16 max_spread_amount_sectors = 2;
+ assert(max_spread_amount_sectors <= m_chunksize);
+ s16 max_spread_amount = max_spread_amount_sectors * MAP_BLOCKSIZE;
+
+ s16 y_blocks_min = -4;
+ s16 y_blocks_max = 3;
+
+ v2s16 sectorpos_base = chunk_to_sector(chunkpos);
+ s16 sectorpos_base_size = m_chunksize;
+
+ v2s16 sectorpos_bigbase =
+ sectorpos_base - v2s16(1,1) * max_spread_amount_sectors;
+ s16 sectorpos_bigbase_size =
+ sectorpos_base_size + 2 * max_spread_amount_sectors;
+
+ data.seed = m_seed;
+ data.chunkpos = chunkpos;
+ data.y_blocks_min = y_blocks_min;
+ data.y_blocks_max = y_blocks_max;
+ data.sectorpos_base = sectorpos_base;
+ data.sectorpos_base_size = sectorpos_base_size;
+ data.sectorpos_bigbase = sectorpos_bigbase;
+ data.sectorpos_bigbase_size = sectorpos_bigbase_size;
+ data.max_spread_amount = max_spread_amount;
+
+ /*
+ Create the whole area of this and the neighboring chunks
+ */
+ {
+ TimeTaker timer("initChunkMake() create area");
+
+ for(s16 x=0; x<sectorpos_bigbase_size; x++)
+ for(s16 z=0; z<sectorpos_bigbase_size; z++)
+ {
+ v2s16 sectorpos = sectorpos_bigbase + v2s16(x,z);
+ ServerMapSector *sector = createSector(sectorpos);
+ assert(sector);
+
+ for(s16 y=y_blocks_min; y<=y_blocks_max; y++)
+ {
+ v3s16 blockpos(sectorpos.X, y, sectorpos.Y);
+ MapBlock *block = createBlock(blockpos);
+
+ // Lighting won't be calculated
+ //block->setLightingExpired(true);
+ // Lighting will be calculated
+ block->setLightingExpired(false);
+
+ /*
+ Block gets sunlight if this is true.
+
+ This should be set to true when the top side of a block
+ is completely exposed to the sky.
+
+ Actually this doesn't matter now because the
+ initial lighting is done here.
+ */
+ block->setIsUnderground(y != y_blocks_max);
+ }
+ }
+ }
+
+ /*
+ Now we have a big empty area.
+
+ Make a ManualMapVoxelManipulator that contains this and the
+ neighboring chunks
+ */
+
+ v3s16 bigarea_blocks_min(
+ sectorpos_bigbase.X,
+ y_blocks_min,
+ sectorpos_bigbase.Y
+ );
+ v3s16 bigarea_blocks_max(
+ sectorpos_bigbase.X + sectorpos_bigbase_size - 1,
+ y_blocks_max,
+ sectorpos_bigbase.Y + sectorpos_bigbase_size - 1
+ );
+
+ data.vmanip.setMap(this);
+ // Add the area
+ {
+ TimeTaker timer("initChunkMake() initialEmerge");
+ data.vmanip.initialEmerge(bigarea_blocks_min, bigarea_blocks_max);
+ }
+
+}
+
+MapChunk* ServerMap::finishChunkMake(ChunkMakeData &data,
+ core::map<v3s16, MapBlock*> &changed_blocks)
+{
+ if(data.no_op)
+ return NULL;
+
+ /*
+ Blit generated stuff to map
+ */
+ {
+ // 70ms @cs=8
+ //TimeTaker timer("generateChunkRaw() blitBackAll");
+ data.vmanip.blitBackAll(&changed_blocks);
+ }
+
+ /*
+ Update day/night difference cache of the MapBlocks
+ */
+ {
+ for(core::map<v3s16, MapBlock*>::Iterator i = changed_blocks.getIterator();
+ i.atEnd() == false; i++)
+ {
+ MapBlock *block = i.getNode()->getValue();
+ block->updateDayNightDiff();
+ }
+ }
+
+ /*
+ Copy transforming liquid information
+ */
+ while(data.transforming_liquid.size() > 0)
+ {
+ v3s16 p = data.transforming_liquid.pop_front();
+ m_transforming_liquid.push_back(p);
+ }
+
+ /*
+ Add random objects to blocks
+ */
+ {
+ for(s16 x=0; x<data.sectorpos_base_size; x++)
+ for(s16 z=0; z<data.sectorpos_base_size; z++)
+ {
+ v2s16 sectorpos = data.sectorpos_base + v2s16(x,z);
+ ServerMapSector *sector = createSector(sectorpos);
+ assert(sector);
+
+ for(s16 y=data.y_blocks_min; y<=data.y_blocks_max; y++)
+ {
+ v3s16 blockpos(sectorpos.X, y, sectorpos.Y);
+ MapBlock *block = createBlock(blockpos);
+ addRandomObjects(block);
+ }
+ }
+ }
+
+ /*
+ Create chunk metadata
+ */
+
+ for(s16 x=-1; x<=1; x++)
+ for(s16 y=-1; y<=1; y++)
+ {
+ v2s16 chunkpos0 = data.chunkpos + v2s16(x,y);
+ // Add chunk meta information
+ MapChunk *chunk = getChunk(chunkpos0);
+ if(chunk == NULL)
+ {
+ chunk = new MapChunk();
+ m_chunks.insert(chunkpos0, chunk);
+ }
+ //chunk->setIsVolatile(true);
+ if(chunk->getGenLevel() > GENERATED_PARTLY)
+ chunk->setGenLevel(GENERATED_PARTLY);
+ }
+
+ /*
+ Set central chunk non-volatile
+ */
+ MapChunk *chunk = getChunk(data.chunkpos);
+ assert(chunk);
+ // Set non-volatile
+ //chunk->setIsVolatile(false);
+ chunk->setGenLevel(GENERATED_FULLY);
+
+ /*
+ Save changed parts of map
+ */
+ save(true);
+
+ return chunk;
+}
+
+#if 0
+// NOTE: Deprecated
+MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
+ core::map<v3s16, MapBlock*> &changed_blocks,
+ bool force)
+{
+ DSTACK(__FUNCTION_NAME);
+
+ /*
+ Don't generate if already fully generated
+ */
+ if(force == false)
+ {
+ MapChunk *chunk = getChunk(chunkpos);
+ if(chunk != NULL && chunk->getGenLevel() == GENERATED_FULLY)
+ {
+ dstream<<"generateChunkRaw(): Chunk "
+ <<"("<<chunkpos.X<<","<<chunkpos.Y<<")"
+ <<" already generated"<<std::endl;
+ return chunk;
+ }
+ }
+
+ dstream<<"generateChunkRaw(): Generating chunk "
+ <<"("<<chunkpos.X<<","<<chunkpos.Y<<")"
+ <<std::endl;
+
+ TimeTaker timer("generateChunkRaw()");
+
+ ChunkMakeData data;
+
+ // Initialize generation
+ initChunkMake(data, chunkpos);
+
+ // Generate stuff
+ makeChunk(&data);
+
+ // Finalize generation
+ MapChunk *chunk = finishChunkMake(data, changed_blocks);
+
+ /*
+ Return central chunk (which was requested)
+ */
+ return chunk;
+}
+
+// NOTE: Deprecated
+MapChunk* ServerMap::generateChunk(v2s16 chunkpos1,
+ core::map<v3s16, MapBlock*> &changed_blocks)
+{
+ dstream<<"generateChunk(): Generating chunk "
+ <<"("<<chunkpos1.X<<","<<chunkpos1.Y<<")"
+ <<std::endl;
+
+ /*for(s16 x=-1; x<=1; x++)
+ for(s16 y=-1; y<=1; y++)*/
+ for(s16 x=-0; x<=0; x++)
+ for(s16 y=-0; y<=0; y++)
+ {
+ v2s16 chunkpos0 = chunkpos1 + v2s16(x,y);
+ MapChunk *chunk = getChunk(chunkpos0);
+ // Skip if already generated
+ if(chunk != NULL && chunk->getGenLevel() == GENERATED_FULLY)
+ continue;
+ generateChunkRaw(chunkpos0, changed_blocks);
+ }
+
+ assert(chunkNonVolatile(chunkpos1));
+
+ MapChunk *chunk = getChunk(chunkpos1);
+ return chunk;
+}
+#endif
+
+ServerMapSector * ServerMap::createSector(v2s16 p2d)
+{
+ DSTACK("%s: p2d=(%d,%d)",
+ __FUNCTION_NAME,
+ p2d.X, p2d.Y);
+
+ /*
+ Check if it exists already in memory
+ */
+ ServerMapSector *sector = (ServerMapSector*)getSectorNoGenerateNoEx(p2d);
+ if(sector != NULL)
+ return sector;
+
+ /*
+ Try to load it from disk (with blocks)
+ */
+ if(loadSectorFull(p2d) == true)
+ {
+ ServerMapSector *sector = (ServerMapSector*)getSectorNoGenerateNoEx(p2d);
+ if(sector == NULL)
+ {
+ dstream<<"ServerMap::createSector(): loadSectorFull didn't make a sector"<<std::endl;
+ throw InvalidPositionException("");
+ }
+ return sector;
+ }
+
+ /*
+ Do not create over-limit
+ */
+ if(p2d.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p2d.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p2d.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p2d.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE)
+ throw InvalidPositionException("createSector(): pos. over limit");
+
+ /*
+ Generate blank sector
+ */
+
+ sector = new ServerMapSector(this, p2d);
+
+ // Sector position on map in nodes
+ v2s16 nodepos2d = p2d * MAP_BLOCKSIZE;
+
+ /*
+ Insert to container
+ */
+ m_sectors.insert(p2d, sector);
+
+ return sector;
+}
+
+#if 0
+MapSector * ServerMap::emergeSector(v2s16 p2d,
+ core::map<v3s16, MapBlock*> &changed_blocks)
+{
+ DSTACK("%s: p2d=(%d,%d)",
+ __FUNCTION_NAME,
+ p2d.X, p2d.Y);
+
+ /*
+ Check chunk status
+ */
+ v2s16 chunkpos = sector_to_chunk(p2d);
+ /*bool chunk_nonvolatile = false;
+ MapChunk *chunk = getChunk(chunkpos);
+ if(chunk && chunk->getIsVolatile() == false)
+ chunk_nonvolatile = true;*/
+ bool chunk_nonvolatile = chunkNonVolatile(chunkpos);
+
+ /*
+ If chunk is not fully generated, generate chunk
+ */
+ if(chunk_nonvolatile == false)
+ {
+ // Generate chunk and neighbors
+ generateChunk(chunkpos, changed_blocks);
+ }
+
+ /*
+ Return sector if it exists now
+ */
+ MapSector *sector = getSectorNoGenerateNoEx(p2d);
+ if(sector != NULL)
+ return sector;
+
+ /*
+ Try to load it from disk
+ */
+ if(loadSectorFull(p2d) == true)
+ {
+ MapSector *sector = getSectorNoGenerateNoEx(p2d);
+ if(sector == NULL)
+ {
+ dstream<<"ServerMap::emergeSector(): loadSectorFull didn't make a sector"<<std::endl;
+ throw InvalidPositionException("");
+ }
+ return sector;
+ }
+
+ /*
+ generateChunk should have generated the sector
+ */
+ //assert(0);
+
+ dstream<<"WARNING: ServerMap::emergeSector: Cannot find sector ("
+ <<p2d.X<<","<<p2d.Y<<" and chunk is already generated. "
+ <<std::endl;
+
+#if 0
+ dstream<<"WARNING: Creating an empty sector."<<std::endl;
+
+ return createSector(p2d);
+
+#endif
+
+#if 1
+ dstream<<"WARNING: Forcing regeneration of chunk."<<std::endl;
+
+ // Generate chunk
+ generateChunkRaw(chunkpos, changed_blocks, true);
+
+ /*
+ Return sector if it exists now
+ */
+ sector = getSectorNoGenerateNoEx(p2d);
+ if(sector != NULL)
+ return sector;
+
+ dstream<<"ERROR: Could not get sector from anywhere."<<std::endl;
+
+ assert(0);
+#endif
+
+ /*
+ Generate directly
+ */
+ //return generateSector();
+}
+#endif
+
+/*
+ NOTE: This is not used for main map generation, only for blocks
+ that are very high or low
+*/
+MapBlock * ServerMap::generateBlock(
+ v3s16 p,
+ MapBlock *original_dummy,
+ ServerMapSector *sector,
+ core::map<v3s16, MapBlock*> &changed_blocks,
+ core::map<v3s16, MapBlock*> &lighting_invalidated_blocks
+)
+{
+ DSTACK("%s: p=(%d,%d,%d)",
+ __FUNCTION_NAME,
+ p.X, p.Y, p.Z);
+
+ // If chunks are disabled
+ /*if(m_chunksize == 0)
+ {
+ dstream<<"ServerMap::generateBlock(): Chunks disabled -> "
+ <<"not generating."<<std::endl;
+ return NULL;
+ }*/
+
+ /*dstream<<"generateBlock(): "
+ <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
+ <<std::endl;*/
+
+ MapBlock *block = original_dummy;
+
+ v2s16 p2d(p.X, p.Z);
+ s16 block_y = p.Y;
+ v2s16 p2d_nodes = p2d * MAP_BLOCKSIZE;
+
+ /*
+ Do not generate over-limit
+ */
+ if(blockpos_over_limit(p))
+ {
+ dstream<<__FUNCTION_NAME<<": Block position over limit"<<std::endl;
+ throw InvalidPositionException("generateBlock(): pos. over limit");
+ }
+
+ /*
+ If block doesn't exist, create one.
+ If it exists, it is a dummy. In that case unDummify() it.
+
+ NOTE: This already sets the map as the parent of the block
+ */
+ if(block == NULL)
+ {
+ block = sector->createBlankBlockNoInsert(block_y);
+ }
+ else
+ {
+ // Remove the block so that nobody can get a half-generated one.
+ sector->removeBlock(block);
+ // Allocate the block to contain the generated data
+ block->unDummify();
+ }
+
+#if 0
+ /*
+ Generate a completely empty block
+ */
+ for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
+ for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
+ {
+ for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
+ {
+ MapNode n;
+ n.d = CONTENT_AIR;
+ block->setNode(v3s16(x0,y0,z0), n);
+ }
+ }
+#else
+ /*
+ Generate a proper block
+ */
+
+ u8 water_material = CONTENT_WATERSOURCE;
+
+ s32 lowest_ground_y = 32767;
+ s32 highest_ground_y = -32768;
+
+ for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
+ for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
+ {
+ //dstream<<"generateBlock: x0="<<x0<<", z0="<<z0<<std::endl;
+
+ //s16 surface_y = 0;
+
+ s16 mud_add_amount = get_mud_add_amount(m_seed, p2d_nodes+v2s16(x0,z0));
+
+ s16 surface_y = base_rock_level_2d(m_seed, p2d_nodes+v2s16(x0,z0))
+ + mud_add_amount;
+
+ /*// Less mud if it is high
+ if(surface_y >= 64)
+ {
+ mud_add_amount--;
+ surface_y--;
+ }*/
+
+ // If chunks are disabled
+ if(m_chunksize == 0)
+ surface_y = WATER_LEVEL + 1;
+
+ if(surface_y < lowest_ground_y)
+ lowest_ground_y = surface_y;
+ if(surface_y > highest_ground_y)
+ highest_ground_y = surface_y;
+
+ s32 surface_depth = AVERAGE_MUD_AMOUNT;
+
+ for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
+ {
+ s16 real_y = block_y * MAP_BLOCKSIZE + y0;
+ MapNode n;
+ /*
+ Calculate lighting
+
+ NOTE: If there are some man-made structures above the
+ newly created block, they won't be taken into account.
+ */
+ if(real_y > surface_y)
+ n.setLight(LIGHTBANK_DAY, LIGHT_SUN);
+
+ /*
+ Calculate material
+ */
+
+ // If node is over heightmap y, it's air or water
+ if(real_y > surface_y)
+ {
+ // If under water level, it's water
+ if(real_y < WATER_LEVEL)
+ {
+ n.d = water_material;
+ n.setLight(LIGHTBANK_DAY,
+ diminish_light(LIGHT_SUN, WATER_LEVEL-real_y+1));
+ /*
+ Add to transforming liquid queue (in case it'd
+ start flowing)
+ */
+ v3s16 real_pos = v3s16(x0,y0,z0) + p*MAP_BLOCKSIZE;
+ m_transforming_liquid.push_back(real_pos);
+ }
+ // else air
+ else
+ n.d = CONTENT_AIR;
+ }
+ // Else it's ground or caves (air)
+ else
+ {
+ // If it's cave, it's cave
+ if(is_cave(m_seed, block->getPosRelative()+v3s16(x0,y0,z0)))
+ {
+ n.d = CONTENT_AIR;
+ }
+ // If it's surface_depth under ground, it's stone
+ else if(real_y <= surface_y - surface_depth)
+ {
+ n.d = CONTENT_STONE;
+ }
+ else
+ {
+ // It is mud if it is under the first ground
+ // level or under water
+ if(real_y < WATER_LEVEL || real_y <= surface_y - 1)
+ {
+ n.d = CONTENT_MUD;
+ }
+ else
+ {
+ n.d = CONTENT_GRASS;
+ }
+
+ //n.d = CONTENT_MUD;
+
+ /*// If under water level, it's mud
+ if(real_y < WATER_LEVEL)
+ n.d = CONTENT_MUD;
+ // Only the topmost node is grass
+ else if(real_y <= surface_y - 1)
+ n.d = CONTENT_MUD;
+ else
+ n.d = CONTENT_GRASS;*/
+ }
+ }
+
+ block->setNode(v3s16(x0,y0,z0), n);
+ }
+ }
+
+ /*
+ Calculate some helper variables
+ */
+
+ // Completely underground if the highest part of block is under lowest
+ // ground height.
+ // This has to be very sure; it's probably one too strict now but
+ // that's just better.
+ bool completely_underground =
+ block_y * MAP_BLOCKSIZE + MAP_BLOCKSIZE < lowest_ground_y;
+
+ bool some_part_underground = block_y * MAP_BLOCKSIZE <= highest_ground_y;
+
+ bool mostly_underwater_surface = false;
+ if(highest_ground_y < WATER_LEVEL
+ && some_part_underground && !completely_underground)
+ mostly_underwater_surface = true;
+
+#if 0
+ /*
+ Generate caves
+ */
+
+ float caves_amount = 0.5;
+
+ // Initialize temporary table
+ const s32 ued = MAP_BLOCKSIZE;
+ bool underground_emptiness[ued*ued*ued];
+ for(s32 i=0; i<ued*ued*ued; i++)
+ {
+ underground_emptiness[i] = 0;
+ }
+
+ // Fill table
+#if 1
+ {
+ /*
+ Initialize orp and ors. Try to find if some neighboring
+ MapBlock has a tunnel ended in its side
+ */
+
+ v3f orp(
+ (float)(myrand()%ued)+0.5,
+ (float)(myrand()%ued)+0.5,
+ (float)(myrand()%ued)+0.5
+ );
+
+ bool found_existing = false;
+
+ // Check z-
+ try
+ {
+ s16 z = -1;
+ for(s16 y=0; y<ued; y++)
+ for(s16 x=0; x<ued; x++)
+ {
+ v3s16 ap = v3s16(x,y,z) + block->getPosRelative();
+ if(getNode(ap).d == CONTENT_AIR)
+ {
+ orp = v3f(x+1,y+1,0);
+ found_existing = true;
+ goto continue_generating;
+ }
+ }
+ }
+ catch(InvalidPositionException &e){}
+
+ // Check z+
+ try
+ {
+ s16 z = ued;
+ for(s16 y=0; y<ued; y++)
+ for(s16 x=0; x<ued; x++)
+ {
+ v3s16 ap = v3s16(x,y,z) + block->getPosRelative();
+ if(getNode(ap).d == CONTENT_AIR)
+ {
+ orp = v3f(x+1,y+1,ued-1);
+ found_existing = true;
+ goto continue_generating;
+ }
+ }
+ }
+ catch(InvalidPositionException &e){}
+
+ // Check x-
+ try
+ {
+ s16 x = -1;
+ for(s16 y=0; y<ued; y++)
+ for(s16 z=0; z<ued; z++)
+ {
+ v3s16 ap = v3s16(x,y,z) + block->getPosRelative();
+ if(getNode(ap).d == CONTENT_AIR)
+ {
+ orp = v3f(0,y+1,z+1);
+ found_existing = true;
+ goto continue_generating;
+ }
+ }
+ }
+ catch(InvalidPositionException &e){}
+
+ // Check x+
+ try
+ {
+ s16 x = ued;
+ for(s16 y=0; y<ued; y++)
+ for(s16 z=0; z<ued; z++)
+ {
+ v3s16 ap = v3s16(x,y,z) + block->getPosRelative();
+ if(getNode(ap).d == CONTENT_AIR)
+ {
+ orp = v3f(ued-1,y+1,z+1);
+ found_existing = true;
+ goto continue_generating;
+ }
+ }
+ }
+ catch(InvalidPositionException &e){}
+
+ // Check y-
+ try
+ {
+ s16 y = -1;
+ for(s16 x=0; x<ued; x++)
+ for(s16 z=0; z<ued; z++)
+ {
+ v3s16 ap = v3s16(x,y,z) + block->getPosRelative();
+ if(getNode(ap).d == CONTENT_AIR)
+ {
+ orp = v3f(x+1,0,z+1);
+ found_existing = true;
+ goto continue_generating;
+ }
+ }
+ }
+ catch(InvalidPositionException &e){}
+
+ // Check y+
+ try
+ {
+ s16 y = ued;
+ for(s16 x=0; x<ued; x++)
+ for(s16 z=0; z<ued; z++)
+ {
+ v3s16 ap = v3s16(x,y,z) + block->getPosRelative();
+ if(getNode(ap).d == CONTENT_AIR)
+ {
+ orp = v3f(x+1,ued-1,z+1);
+ found_existing = true;
+ goto continue_generating;
+ }
+ }
+ }
+ catch(InvalidPositionException &e){}
+
+continue_generating:
+
+ /*
+ Choose whether to actually generate cave
+ */
+ bool do_generate_caves = true;
+ // Don't generate if no part is underground
+ if(!some_part_underground)
+ {
+ do_generate_caves = false;
+ }
+ // Don't generate if mostly underwater surface
+ /*else if(mostly_underwater_surface)
+ {
+ do_generate_caves = false;
+ }*/
+ // Partly underground = cave
+ else if(!completely_underground)
+ {
+ do_generate_caves = (rand() % 100 <= (s32)(caves_amount*100));
+ }
+ // Found existing cave underground
+ else if(found_existing && completely_underground)
+ {
+ do_generate_caves = (rand() % 100 <= (s32)(caves_amount*100));
+ }
+ // Underground and no caves found
+ else
+ {
+ do_generate_caves = (rand() % 300 <= (s32)(caves_amount*100));
+ }
+
+ if(do_generate_caves)
+ {
+ /*
+ Generate some tunnel starting from orp and ors
+ */
+ for(u16 i=0; i<3; i++)
+ {
+ v3f rp(
+ (float)(myrand()%ued)+0.5,
+ (float)(myrand()%ued)+0.5,
+ (float)(myrand()%ued)+0.5
+ );
+ s16 min_d = 0;
+ s16 max_d = 4;
+ s16 rs = (myrand()%(max_d-min_d+1))+min_d;
+
+ v3f vec = rp - orp;
+
+ for(float f=0; f<1.0; f+=0.04)
+ {
+ v3f fp = orp + vec * f;
+ v3s16 cp(fp.X, fp.Y, fp.Z);
+ s16 d0 = -rs/2;
+ s16 d1 = d0 + rs - 1;
+ for(s16 z0=d0; z0<=d1; z0++)
+ {
+ s16 si = rs - abs(z0);
+ for(s16 x0=-si; x0<=si-1; x0++)
+ {
+ s16 si2 = rs - abs(x0);
+ for(s16 y0=-si2+1; y0<=si2-1; y0++)
+ {
+ s16 z = cp.Z + z0;
+ s16 y = cp.Y + y0;
+ s16 x = cp.X + x0;
+ v3s16 p(x,y,z);
+ if(isInArea(p, ued) == false)
+ continue;
+ underground_emptiness[ued*ued*z + ued*y + x] = 1;
+ }
+ }
+ }
+ }
+
+ orp = rp;
+ }
+ }
+ }
+#endif
+
+ // Set to true if has caves.
+ // Set when some non-air is changed to air when making caves.
+ bool has_caves = false;
+
+ /*
+ Apply temporary cave data to block
+ */
+
+ for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
+ for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
+ {
+ for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
+ {
+ MapNode n = block->getNode(v3s16(x0,y0,z0));
+
+ // Create caves
+ if(underground_emptiness[
+ ued*ued*(z0*ued/MAP_BLOCKSIZE)
+ +ued*(y0*ued/MAP_BLOCKSIZE)
+ +(x0*ued/MAP_BLOCKSIZE)])
+ {
+ if(content_features(n.d).walkable/*is_ground_content(n.d)*/)
+ {
+ // Has now caves
+ has_caves = true;
+ // Set air to node
+ n.d = CONTENT_AIR;
+ }
+ }
+
+ block->setNode(v3s16(x0,y0,z0), n);
+ }
+ }
+#endif
+
+ /*
+ This is used for guessing whether or not the block should
+ receive sunlight from the top if the block above doesn't exist
+ */
+ block->setIsUnderground(completely_underground);
+
+ /*
+ Force lighting update if some part of block is partly
+ underground and has caves.
+ */
+ /*if(some_part_underground && !completely_underground && has_caves)
+ {
+ //dstream<<"Half-ground caves"<<std::endl;
+ lighting_invalidated_blocks[block->getPos()] = block;
+ }*/
+
+ // DEBUG: Always update lighting
+ //lighting_invalidated_blocks[block->getPos()] = block;
+
+ /*
+ Add some minerals
+ */
+
+ if(some_part_underground)
+ {
+ s16 underground_level = (lowest_ground_y/MAP_BLOCKSIZE - block_y)+1;
+
+ /*
+ Add meseblocks
+ */
+ for(s16 i=0; i<underground_level/4 + 1; i++)
+ {
+ if(myrand()%50 == 0)
+ {
+ v3s16 cp(
+ (myrand()%(MAP_BLOCKSIZE-2))+1,
+ (myrand()%(MAP_BLOCKSIZE-2))+1,
+ (myrand()%(MAP_BLOCKSIZE-2))+1
+ );
+
+ MapNode n;
+ n.d = CONTENT_MESE;
+
+ for(u16 i=0; i<27; i++)
+ {
+ if(block->getNode(cp+g_27dirs[i]).d == CONTENT_STONE)
+ if(myrand()%8 == 0)
+ block->setNode(cp+g_27dirs[i], n);
+ }
+ }
+ }
+
+ /*
+ Add coal
+ */
+ u16 coal_amount = 30;
+ u16 coal_rareness = 60 / coal_amount;
+ if(coal_rareness == 0)
+ coal_rareness = 1;
+ if(myrand()%coal_rareness == 0)
+ {
+ u16 a = myrand() % 16;
+ u16 amount = coal_amount * a*a*a / 1000;
+ for(s16 i=0; i<amount; i++)
+ {
+ v3s16 cp(
+ (myrand()%(MAP_BLOCKSIZE-2))+1,
+ (myrand()%(MAP_BLOCKSIZE-2))+1,
+ (myrand()%(MAP_BLOCKSIZE-2))+1
+ );
+
+ MapNode n;
+ n.d = CONTENT_STONE;
+ n.param = MINERAL_COAL;
+
+ for(u16 i=0; i<27; i++)
+ {
+ if(block->getNode(cp+g_27dirs[i]).d == CONTENT_STONE)
+ if(myrand()%8 == 0)
+ block->setNode(cp+g_27dirs[i], n);
+ }
+ }
+ }
+
+ /*
+ Add iron
+ */
+ //TODO: change to iron_amount or whatever
+ u16 iron_amount = 15;
+ u16 iron_rareness = 60 / iron_amount;
+ if(iron_rareness == 0)
+ iron_rareness = 1;
+ if(myrand()%iron_rareness == 0)
+ {
+ u16 a = myrand() % 16;
+ u16 amount = iron_amount * a*a*a / 1000;
+ for(s16 i=0; i<amount; i++)
+ {
+ v3s16 cp(
+ (myrand()%(MAP_BLOCKSIZE-2))+1,
+ (myrand()%(MAP_BLOCKSIZE-2))+1,
+ (myrand()%(MAP_BLOCKSIZE-2))+1
+ );
+
+ MapNode n;
+ n.d = CONTENT_STONE;
+ n.param = MINERAL_IRON;
+
+ for(u16 i=0; i<27; i++)
+ {
+ if(block->getNode(cp+g_27dirs[i]).d == CONTENT_STONE)
+ if(myrand()%8 == 0)
+ block->setNode(cp+g_27dirs[i], n);
+ }
+ }
+ }
+ }
+
+ /*
+ Create a few rats in empty blocks underground
+ */
+ if(completely_underground)
+ {
+ //for(u16 i=0; i<2; i++)
+ {
+ v3s16 cp(
+ (myrand()%(MAP_BLOCKSIZE-2))+1,
+ (myrand()%(MAP_BLOCKSIZE-2))+1,
+ (myrand()%(MAP_BLOCKSIZE-2))+1
+ );
+
+ // Check that the place is empty
+ //if(!is_ground_content(block->getNode(cp).d))
+ if(1)
+ {
+ RatObject *obj = new RatObject(NULL, -1, intToFloat(cp, BS));
+ block->addObject(obj);
+ }
+ }
+ }
+
+#endif // end of proper block generation
+
+ /*
+ Add block to sector.
+ */
+ sector->insertBlock(block);
+
+ // Lighting is invalid after generation.
+ block->setLightingExpired(true);
+
+#if 0
+ /*
+ Debug information
+ */
+ dstream
+ <<"lighting_invalidated_blocks.size()"
+ <<", has_caves"
+ <<", completely_ug"
+ <<", some_part_ug"
+ <<" "<<lighting_invalidated_blocks.size()
+ <<", "<<has_caves
+ <<", "<<completely_underground
+ <<", "<<some_part_underground
+ <<std::endl;
+#endif
+
+ return block;
+}
+
+MapBlock * ServerMap::createBlock(v3s16 p)
+{
+ DSTACK("%s: p=(%d,%d,%d)",
+ __FUNCTION_NAME, p.X, p.Y, p.Z);
+
+ /*
+ Do not create over-limit
+ */
+ if(p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE)
+ throw InvalidPositionException("createBlock(): pos. over limit");
+
+ v2s16 p2d(p.X, p.Z);
+ s16 block_y = p.Y;
+ /*
+ This will create or load a sector if not found in memory.
+ If block exists on disk, it will be loaded.
+
+ NOTE: On old save formats, this will be slow, as it generates
+ lighting on blocks for them.
+ */
+ ServerMapSector *sector;
+ try{
+ sector = (ServerMapSector*)createSector(p2d);
+ assert(sector->getId() == MAPSECTOR_SERVER);
+ }
+ catch(InvalidPositionException &e)
+ {
+ dstream<<"createBlock: createSector() failed"<<std::endl;
+ throw e;
+ }
+ /*
+ NOTE: This should not be done, or at least the exception
+ should not be passed on as std::exception, because it
+ won't be catched at all.
+ */
+ /*catch(std::exception &e)
+ {
+ dstream<<"createBlock: createSector() failed: "
+ <<e.what()<<std::endl;
+ throw e;
+ }*/
+
+ /*
+ Try to get a block from the sector
+ */
+
+ MapBlock *block = sector->getBlockNoCreateNoEx(block_y);
+ if(block)
+ return block;
+ // Create blank
+ block = sector->createBlankBlock(block_y);
+ return block;
+}
+
+MapBlock * ServerMap::emergeBlock(
+ v3s16 p,
+ bool only_from_disk,
+ core::map<v3s16, MapBlock*> &changed_blocks,
+ core::map<v3s16, MapBlock*> &lighting_invalidated_blocks
+)
+{
+ DSTACK("%s: p=(%d,%d,%d), only_from_disk=%d",
+ __FUNCTION_NAME,
+ p.X, p.Y, p.Z, only_from_disk);
+
+ /*
+ Do not generate over-limit
+ */
+ if(p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE)
+ throw InvalidPositionException("emergeBlock(): pos. over limit");
+
+ v2s16 p2d(p.X, p.Z);
+ s16 block_y = p.Y;
+ /*
+ This will create or load a sector if not found in memory.
+ If block exists on disk, it will be loaded.
+ */
+ ServerMapSector *sector;
+ try{
+ sector = (ServerMapSector*)emergeSector(p2d, changed_blocks);
+ assert(sector->getId() == MAPSECTOR_SERVER);
+ }
+ catch(InvalidPositionException &e)
+ {
+ dstream<<"emergeBlock: emergeSector() failed: "
+ <<e.what()<<std::endl;
+ dstream<<"Path to failed sector: "<<getSectorDir(p2d)
+ <<std::endl
+ <<"You could try to delete it."<<std::endl;
+ throw e;
+ }
+ catch(VersionMismatchException &e)
+ {
+ dstream<<"emergeBlock: emergeSector() failed: "
+ <<e.what()<<std::endl;
+ dstream<<"Path to failed sector: "<<getSectorDir(p2d)
+ <<std::endl
+ <<"You could try to delete it."<<std::endl;
+ throw e;
+ }
+ /*
+ NOTE: This should not be done, or at least the exception
+ should not be passed on as std::exception, because it
+ won't be catched at all.
+ */
+ /*catch(std::exception &e)
+ {
+ dstream<<"emergeBlock: emergeSector() failed: "
+ <<e.what()<<std::endl;
+ dstream<<"Path to failed sector: "<<getSectorDir(p2d)
+ <<std::endl
+ <<"You could try to delete it."<<std::endl;
+ throw e;
+ }*/
+
+ /*
+ Try to get a block from the sector
+ */
+
+ bool does_not_exist = false;
+ bool lighting_expired = false;
+ MapBlock *block = sector->getBlockNoCreateNoEx(block_y);
+
+ if(block == NULL)
+ {
+ does_not_exist = true;
+ }
+ else if(block->isDummy() == true)
+ {
+ does_not_exist = true;
+ }
+ else if(block->getLightingExpired())
+ {
+ lighting_expired = true;
+ }
+ else
+ {
+ // Valid block
+ //dstream<<"emergeBlock(): Returning already valid block"<<std::endl;
+ return block;
+ }
+
+ /*
+ If block was not found on disk and not going to generate a
+ new one, make sure there is a dummy block in place.
+ */
+ if(only_from_disk && (does_not_exist || lighting_expired))
+ {
+ //dstream<<"emergeBlock(): Was not on disk but not generating"<<std::endl;
+
+ if(block == NULL)
+ {
+ // Create dummy block
+ block = new MapBlock(this, p, true);
+
+ // Add block to sector
+ sector->insertBlock(block);
+ }
+ // Done.
+ return block;
+ }
+
+ //dstream<<"Not found on disk, generating."<<std::endl;
+ // 0ms
+ //TimeTaker("emergeBlock() generate");
+
+ //dstream<<"emergeBlock(): Didn't find valid block -> making one"<<std::endl;
+
+ /*
+ If the block doesn't exist, generate the block.
+ */
+ if(does_not_exist)
+ {
+ block = generateBlock(p, block, sector, changed_blocks,
+ lighting_invalidated_blocks);
+ }
+
+ if(lighting_expired)
+ {
+ lighting_invalidated_blocks.insert(p, block);
+ }
+
+ /*
+ Initially update sunlight
+ */
+
+ {
+ core::map<v3s16, bool> light_sources;
+ bool black_air_left = false;
+ bool bottom_invalid =
+ block->propagateSunlight(light_sources, true,
+ &black_air_left, true);
+
+ // If sunlight didn't reach everywhere and part of block is
+ // above ground, lighting has to be properly updated
+ //if(black_air_left && some_part_underground)
+ if(black_air_left)
+ {
+ lighting_invalidated_blocks[block->getPos()] = block;
+ }
+
+ if(bottom_invalid)
+ {
+ lighting_invalidated_blocks[block->getPos()] = block;
+ }
+ }
+
+ return block;
+}
+
+s16 ServerMap::findGroundLevel(v2s16 p2d)
+{
+ /*
+ Uh, just do something random...
+ */
+ // Find existing map from top to down
+ s16 max=63;
+ s16 min=-64;
+ v3s16 p(p2d.X, max, p2d.Y);
+ for(; p.Y>min; p.Y--)
+ {
+ MapNode n = getNodeNoEx(p);
+ if(n.d != CONTENT_IGNORE)
+ break;
+ }
+ if(p.Y == min)
+ goto plan_b;
+ // If this node is not air, go to plan b
+ if(getNodeNoEx(p).d != CONTENT_AIR)
+ goto plan_b;
+ // Search existing walkable and return it
+ for(; p.Y>min; p.Y--)
+ {
+ MapNode n = getNodeNoEx(p);
+ if(content_walkable(n.d) && n.d != CONTENT_IGNORE)
+ return p.Y;
+ }
+ // Move to plan b
+plan_b:
+ /*
+ Plan B: Get from map generator perlin noise function
+ */
+ // This won't work if proper generation is disabled
+ if(m_chunksize == 0)
+ return WATER_LEVEL+2;
+ double level = base_rock_level_2d(m_seed, p2d) + AVERAGE_MUD_AMOUNT;
+ return (s16)level;
+}
+
+void ServerMap::createDir(std::string path)
+{
+ if(fs::CreateDir(path) == false)
+ {
+ m_dout<<DTIME<<"ServerMap: Failed to create directory "
+ <<"\""<<path<<"\""<<std::endl;
+ throw BaseException("ServerMap failed to create directory");
+ }
+}
+
+std::string ServerMap::getSectorSubDir(v2s16 pos)
+{
+ char cc[9];
+ snprintf(cc, 9, "%.4x%.4x",
+ (unsigned int)pos.X&0xffff,
+ (unsigned int)pos.Y&0xffff);
+
+ return std::string(cc);
+}
+
+std::string ServerMap::getSectorDir(v2s16 pos)
+{
+ return m_savedir + "/sectors/" + getSectorSubDir(pos);
+}
+
+v2s16 ServerMap::getSectorPos(std::string dirname)
+{
+ if(dirname.size() != 8)
+ throw InvalidFilenameException("Invalid sector directory name");
+ unsigned int x, y;
+ int r = sscanf(dirname.c_str(), "%4x%4x", &x, &y);
+ if(r != 2)
+ throw InvalidFilenameException("Invalid sector directory name");
+ v2s16 pos((s16)x, (s16)y);
+ return pos;
+}
+
+v3s16 ServerMap::getBlockPos(std::string sectordir, std::string blockfile)
+{
+ v2s16 p2d = getSectorPos(sectordir);
+
+ if(blockfile.size() != 4){
+ throw InvalidFilenameException("Invalid block filename");
+ }
+ unsigned int y;
+ int r = sscanf(blockfile.c_str(), "%4x", &y);
+ if(r != 1)
+ throw InvalidFilenameException("Invalid block filename");
+ return v3s16(p2d.X, y, p2d.Y);
+}
+
+void ServerMap::save(bool only_changed)
+{
+ DSTACK(__FUNCTION_NAME);
+ if(m_map_saving_enabled == false)
+ {
+ dstream<<DTIME<<"WARNING: Not saving map, saving disabled."<<std::endl;
+ return;
+ }
+
+ if(only_changed == false)
+ dstream<<DTIME<<"ServerMap: Saving whole map, this can take time."
+ <<std::endl;
+
+ if(only_changed == false || m_map_metadata_changed)
+ {
+ saveMapMeta();
+ }
+
+ // Disable saving chunk metadata if chunks are disabled
+ if(m_chunksize != 0)
+ {
+ if(only_changed == false || anyChunkModified())
+ saveChunkMeta();
+ }
+
+ u32 sector_meta_count = 0;
+ u32 block_count = 0;
+
+ { //sectorlock
+ //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
+
+ core::map<v2s16, MapSector*>::Iterator i = m_sectors.getIterator();
+ for(; i.atEnd() == false; i++)
+ {
+ ServerMapSector *sector = (ServerMapSector*)i.getNode()->getValue();
+ assert(sector->getId() == MAPSECTOR_SERVER);
+
+ if(sector->differs_from_disk || only_changed == false)
+ {
+ saveSectorMeta(sector);
+ sector_meta_count++;
+ }
+ core::list<MapBlock*> blocks;
+ sector->getBlocks(blocks);
+ core::list<MapBlock*>::Iterator j;
+ for(j=blocks.begin(); j!=blocks.end(); j++)
+ {
+ MapBlock *block = *j;
+ if(block->getChangedFlag() || only_changed == false)
+ {
+ saveBlock(block);
+ block_count++;
+
+ /*dstream<<"ServerMap: Written block ("
+ <<block->getPos().X<<","
+ <<block->getPos().Y<<","
+ <<block->getPos().Z<<")"
+ <<std::endl;*/
+ }
+ }
+ }
+
+ }//sectorlock
+
+ /*
+ Only print if something happened or saved whole map
+ */
+ if(only_changed == false || sector_meta_count != 0
+ || block_count != 0)
+ {
+ dstream<<DTIME<<"ServerMap: Written: "
+ <<sector_meta_count<<" sector metadata files, "
+ <<block_count<<" block files"
+ <<std::endl;
+ }
+}
+
+#if 0
+// NOTE: Doing this is insane. Deprecated and probably broken.
+void ServerMap::loadAll()
+{
+ DSTACK(__FUNCTION_NAME);
+ dstream<<DTIME<<"ServerMap: Loading map..."<<std::endl;
+
+ loadMapMeta();
+ loadChunkMeta();
+
+ std::vector<fs::DirListNode> list = fs::GetDirListing(m_savedir+"/sectors/");
+
+ dstream<<DTIME<<"There are "<<list.size()<<" sectors."<<std::endl;
+
+ //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
+
+ s32 counter = 0;
+ s32 printed_counter = -100000;
+ s32 count = list.size();
+
+ std::vector<fs::DirListNode>::iterator i;
+ for(i=list.begin(); i!=list.end(); i++)
+ {
+ if(counter > printed_counter + 10)
+ {
+ dstream<<DTIME<<counter<<"/"<<count<<std::endl;
+ printed_counter = counter;
+ }
+ counter++;
+
+ MapSector *sector = NULL;
+
+ // We want directories
+ if(i->dir == false)
+ continue;
+ try{
+ sector = loadSectorMeta(i->name);
+ }
+ catch(InvalidFilenameException &e)
+ {
+ // This catches unknown crap in directory
+ }
+
+ std::vector<fs::DirListNode> list2 = fs::GetDirListing
+ (m_savedir+"/sectors/"+i->name);
+ std::vector<fs::DirListNode>::iterator i2;
+ for(i2=list2.begin(); i2!=list2.end(); i2++)
+ {
+ // We want files
+ if(i2->dir)
+ continue;
+ try{
+ loadBlock(i->name, i2->name, sector);
+ }
+ catch(InvalidFilenameException &e)
+ {
+ // This catches unknown crap in directory
+ }
+ }
+ }
+ dstream<<DTIME<<"ServerMap: Map loaded."<<std::endl;
+}
+#endif
+
+#if 0
+void ServerMap::saveMasterHeightmap()
+{
+ DSTACK(__FUNCTION_NAME);
+
+ dstream<<"DEPRECATED: "<<__FUNCTION_NAME<<std::endl;
+
+ createDir(m_savedir);
+
+ /*std::string fullpath = m_savedir + "/master_heightmap";
+ std::ofstream o(fullpath.c_str(), std::ios_base::binary);
+ if(o.good() == false)
+ throw FileNotGoodException("Cannot open master heightmap");*/
+
+ // Format used for writing
+ //u8 version = SER_FMT_VER_HIGHEST;
+}
+
+void ServerMap::loadMasterHeightmap()
+{
+ DSTACK(__FUNCTION_NAME);
+
+ dstream<<"DEPRECATED: "<<__FUNCTION_NAME<<std::endl;
+
+ /*std::string fullpath = m_savedir + "/master_heightmap";
+ std::ifstream is(fullpath.c_str(), std::ios_base::binary);
+ if(is.good() == false)
+ throw FileNotGoodException("Cannot open master heightmap");*/
+}
+#endif
+
+void ServerMap::saveMapMeta()
+{
+ DSTACK(__FUNCTION_NAME);
+
+ dstream<<"INFO: ServerMap::saveMapMeta(): "
+ <<"seed="<<m_seed<<", chunksize="<<m_chunksize
+ <<std::endl;
+
+ createDir(m_savedir);
+
+ std::string fullpath = m_savedir + "/map_meta.txt";
+ std::ofstream os(fullpath.c_str(), std::ios_base::binary);
+ if(os.good() == false)
+ {
+ dstream<<"ERROR: ServerMap::saveMapMeta(): "
+ <<"could not open"<<fullpath<<std::endl;
+ throw FileNotGoodException("Cannot open chunk metadata");
+ }
+
+ Settings params;
+ params.setU64("seed", m_seed);
+ params.setS32("chunksize", m_chunksize);
+
+ params.writeLines(os);
+
+ os<<"[end_of_params]\n";
+
+ m_map_metadata_changed = false;
+}
+
+void ServerMap::loadMapMeta()
+{
+ DSTACK(__FUNCTION_NAME);
+
+ dstream<<"INFO: ServerMap::loadMapMeta(): Loading map metadata"
+ <<std::endl;
+
+ std::string fullpath = m_savedir + "/map_meta.txt";
+ std::ifstream is(fullpath.c_str(), std::ios_base::binary);
+ if(is.good() == false)
+ {
+ dstream<<"ERROR: ServerMap::loadMapMeta(): "
+ <<"could not open"<<fullpath<<std::endl;
+ throw FileNotGoodException("Cannot open map metadata");
+ }
+
+ Settings params;
+
+ for(;;)
+ {
+ if(is.eof())
+ throw SerializationError
+ ("ServerMap::loadMapMeta(): [end_of_params] not found");
+ std::string line;
+ std::getline(is, line);
+ std::string trimmedline = trim(line);
+ if(trimmedline == "[end_of_params]")
+ break;
+ params.parseConfigLine(line);
+ }
+
+ m_seed = params.getU64("seed");
+ m_chunksize = params.getS32("chunksize");
+
+ dstream<<"INFO: ServerMap::loadMapMeta(): "
+ <<"seed="<<m_seed<<", chunksize="<<m_chunksize
+ <<std::endl;
+}
+
+void ServerMap::saveChunkMeta()
+{
+ DSTACK(__FUNCTION_NAME);
+
+ // This should not be called if chunks are disabled.
+ assert(m_chunksize != 0);
+
+ u32 count = m_chunks.size();
+
+ dstream<<"INFO: ServerMap::saveChunkMeta(): Saving metadata of "
+ <<count<<" chunks"<<std::endl;
+
+ createDir(m_savedir);
+
+ std::string fullpath = m_savedir + "/chunk_meta";
+ std::ofstream os(fullpath.c_str(), std::ios_base::binary);
+ if(os.good() == false)
+ {
+ dstream<<"ERROR: ServerMap::saveChunkMeta(): "
+ <<"could not open"<<fullpath<<std::endl;
+ throw FileNotGoodException("Cannot open chunk metadata");
+ }
+
+ u8 version = 0;
+
+ // Write version
+ os.write((char*)&version, 1);
+
+ u8 buf[4];
+
+ // Write count
+ writeU32(buf, count);
+ os.write((char*)buf, 4);
+
+ for(core::map<v2s16, MapChunk*>::Iterator
+ i = m_chunks.getIterator();
+ i.atEnd()==false; i++)
+ {
+ v2s16 p = i.getNode()->getKey();
+ MapChunk *chunk = i.getNode()->getValue();
+ // Write position
+ writeV2S16(buf, p);
+ os.write((char*)buf, 4);
+ // Write chunk data
+ chunk->serialize(os, version);
+ }
+
+ setChunksNonModified();
+}
+
+void ServerMap::loadChunkMeta()
+{
+ DSTACK(__FUNCTION_NAME);
+
+ dstream<<"INFO: ServerMap::loadChunkMeta(): Loading chunk metadata"
+ <<std::endl;
+
+ std::string fullpath = m_savedir + "/chunk_meta";
+ std::ifstream is(fullpath.c_str(), std::ios_base::binary);
+ if(is.good() == false)
+ {
+ dstream<<"ERROR: ServerMap::loadChunkMeta(): "
+ <<"could not open"<<fullpath<<std::endl;
+ throw FileNotGoodException("Cannot open chunk metadata");
+ }
+
+ u8 version = 0;
+
+ // Read version
+ is.read((char*)&version, 1);
+
+ u8 buf[4];
+
+ // Read count
+ is.read((char*)buf, 4);
+ u32 count = readU32(buf);
+
+ dstream<<"INFO: ServerMap::loadChunkMeta(): Loading metadata of "
+ <<count<<" chunks"<<std::endl;
+
+ for(u32 i=0; i<count; i++)
+ {
+ v2s16 p;
+ MapChunk *chunk = new MapChunk();
+ // Read position
+ is.read((char*)buf, 4);
+ p = readV2S16(buf);
+ // Read chunk data
+ chunk->deSerialize(is, version);
+ m_chunks.insert(p, chunk);
+ }
+}
+
+void ServerMap::saveSectorMeta(ServerMapSector *sector)
+{
+ DSTACK(__FUNCTION_NAME);
+ // Format used for writing
+ u8 version = SER_FMT_VER_HIGHEST;
+ // Get destination
+ v2s16 pos = sector->getPos();
+ createDir(m_savedir);
+ createDir(m_savedir+"/sectors");
+ std::string dir = getSectorDir(pos);
+ createDir(dir);
+
+ std::string fullpath = dir + "/meta";
+ std::ofstream o(fullpath.c_str(), std::ios_base::binary);
+ if(o.good() == false)
+ throw FileNotGoodException("Cannot open sector metafile");
+
+ sector->serialize(o, version);
+
+ sector->differs_from_disk = false;
+}
+
+MapSector* ServerMap::loadSectorMeta(std::string dirname)
+{
+ DSTACK(__FUNCTION_NAME);
+ // Get destination
+ v2s16 p2d = getSectorPos(dirname);
+ std::string dir = m_savedir + "/sectors/" + dirname;
+
+ ServerMapSector *sector = NULL;
+
+ std::string fullpath = dir + "/meta";
+ std::ifstream is(fullpath.c_str(), std::ios_base::binary);
+ if(is.good() == false)
+ {
+ // If the directory exists anyway, it probably is in some old
+ // format. Just go ahead and create the sector.
+ if(fs::PathExists(dir))
+ {
+ dstream<<"ServerMap::loadSectorMeta(): Sector metafile "
+ <<fullpath<<" doesn't exist but directory does."
+ <<" Continuing with a sector with no metadata."
+ <<std::endl;
+ sector = new ServerMapSector(this, p2d);
+ m_sectors.insert(p2d, sector);
+ }
+ else
+ throw FileNotGoodException("Cannot open sector metafile");
+ }
+ else
+ {
+ sector = ServerMapSector::deSerialize
+ (is, this, p2d, m_sectors);
+ }
+
+ sector->differs_from_disk = false;
+
+ return sector;
+}
+
+bool ServerMap::loadSectorFull(v2s16 p2d)
+{
+ DSTACK(__FUNCTION_NAME);
+ std::string sectorsubdir = getSectorSubDir(p2d);
+
+ MapSector *sector = NULL;
+
+ //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
+
+ try{
+ sector = loadSectorMeta(sectorsubdir);
+ }
+ catch(InvalidFilenameException &e)
+ {
+ return false;
+ }
+ catch(FileNotGoodException &e)
+ {
+ return false;
+ }
+ catch(std::exception &e)
+ {
+ return false;
+ }
+
+ /*
+ Load blocks
+ */
+ std::vector<fs::DirListNode> list2 = fs::GetDirListing
+ (m_savedir+"/sectors/"+sectorsubdir);
+ std::vector<fs::DirListNode>::iterator i2;
+ for(i2=list2.begin(); i2!=list2.end(); i2++)
+ {
+ // We want files
+ if(i2->dir)
+ continue;
+ try{
+ loadBlock(sectorsubdir, i2->name, sector);
+ }
+ catch(InvalidFilenameException &e)
+ {
+ // This catches unknown crap in directory
+ }
+ }
+ return true;
+}
+
+void ServerMap::saveBlock(MapBlock *block)
+{
+ DSTACK(__FUNCTION_NAME);
+ /*
+ Dummy blocks are not written
+ */
+ if(block->isDummy())
+ {
+ /*v3s16 p = block->getPos();
+ dstream<<"ServerMap::saveBlock(): WARNING: Not writing dummy block "
+ <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
+ return;
+ }
+
+ // Format used for writing
+ u8 version = SER_FMT_VER_HIGHEST;
+ // Get destination
+ v3s16 p3d = block->getPos();
+ v2s16 p2d(p3d.X, p3d.Z);
+ createDir(m_savedir);
+ createDir(m_savedir+"/sectors");
+ std::string dir = getSectorDir(p2d);
+ createDir(dir);
+
+ // Block file is map/sectors/xxxxxxxx/xxxx
+ char cc[5];
+ snprintf(cc, 5, "%.4x", (unsigned int)p3d.Y&0xffff);
+ std::string fullpath = dir + "/" + cc;
+ std::ofstream o(fullpath.c_str(), std::ios_base::binary);
+ if(o.good() == false)
+ throw FileNotGoodException("Cannot open block data");
+
+ /*
+ [0] u8 serialization version
+ [1] data
+ */
+ o.write((char*)&version, 1);
+
+ block->serialize(o, version);
+
+ /*
+ Versions up from 9 have block objects.
+ */
+ if(version >= 9)
+ {
+ block->serializeObjects(o, version);
+ }
+
+ /*
+ Versions up from 15 have static objects.
+ */
+ if(version >= 15)
+ {
+ block->m_static_objects.serialize(o);
+ }
+
+ // We just wrote it to the disk
+ block->resetChangedFlag();
+}
+
+void ServerMap::loadBlock(std::string sectordir, std::string blockfile, MapSector *sector)
+{
+ DSTACK(__FUNCTION_NAME);
+
+ // Block file is map/sectors/xxxxxxxx/xxxx
+ std::string fullpath = m_savedir+"/sectors/"+sectordir+"/"+blockfile;
+ try{
+
+ std::ifstream is(fullpath.c_str(), std::ios_base::binary);
+ if(is.good() == false)
+ throw FileNotGoodException("Cannot open block file");
+
+ v3s16 p3d = getBlockPos(sectordir, blockfile);
+ v2s16 p2d(p3d.X, p3d.Z);
+
+ assert(sector->getPos() == p2d);
+
+ u8 version = SER_FMT_VER_INVALID;
+ is.read((char*)&version, 1);
+
+ if(is.fail())
+ throw SerializationError("ServerMap::loadBlock(): Failed"
+ " to read MapBlock version");
+
+ /*u32 block_size = MapBlock::serializedLength(version);
+ SharedBuffer<u8> data(block_size);
+ is.read((char*)*data, block_size);*/
+
+ // This will always return a sector because we're the server
+ //MapSector *sector = emergeSector(p2d);
+
+ MapBlock *block = NULL;
+ bool created_new = false;
+ try{
+ block = sector->getBlockNoCreate(p3d.Y);
+ }
+ catch(InvalidPositionException &e)
+ {
+ block = sector->createBlankBlockNoInsert(p3d.Y);
+ created_new = true;
+ }
+
+ // deserialize block data
+ block->deSerialize(is, version);
+
+ /*
+ Versions up from 9 have block objects.
+ */
+ if(version >= 9)
+ {
+ block->updateObjects(is, version, NULL, 0);
+ }
+
+ /*
+ Versions up from 15 have static objects.
+ */
+ if(version >= 15)
+ {
+ block->m_static_objects.deSerialize(is);
+ }
+
+ if(created_new)
+ sector->insertBlock(block);
+
+ /*
+ Convert old formats to new and save
+ */
+
+ // Save old format blocks in new format
+ if(version < SER_FMT_VER_HIGHEST)
+ {
+ saveBlock(block);
+ }
+
+ // We just loaded it from the disk, so it's up-to-date.
+ block->resetChangedFlag();
+
+ }
+ catch(SerializationError &e)
+ {
+ dstream<<"WARNING: Invalid block data on disk "
+ "(SerializationError). Ignoring. "
+ "A new one will be generated."
+ <<std::endl;
+
+ // TODO: Backup file; name is in fullpath.
+ }
+}
+
+void ServerMap::PrintInfo(std::ostream &out)
+{
+ out<<"ServerMap: ";
+}
+
+#ifndef SERVER
+
+/*
+ ClientMap
+*/
+
+ClientMap::ClientMap(
+ Client *client,
+ MapDrawControl &control,
+ scene::ISceneNode* parent,
+ scene::ISceneManager* mgr,
+ s32 id
+):
+ Map(dout_client),
+ scene::ISceneNode(parent, mgr, id),
+ m_client(client),
+ m_control(control),
+ m_camera_position(0,0,0),
+ m_camera_direction(0,0,1)
+{
+ m_camera_mutex.Init();
+ assert(m_camera_mutex.IsInitialized());
+
+ m_box = core::aabbox3d<f32>(-BS*1000000,-BS*1000000,-BS*1000000,
+ BS*1000000,BS*1000000,BS*1000000);
+}
+
+ClientMap::~ClientMap()
+{
+ /*JMutexAutoLock lock(mesh_mutex);
+
+ if(mesh != NULL)
+ {
+ mesh->drop();
+ mesh = NULL;
+ }*/
+}
+
+MapSector * ClientMap::emergeSector(v2s16 p2d)
+{
+ DSTACK(__FUNCTION_NAME);
+ // Check that it doesn't exist already
+ try{
+ return getSectorNoGenerate(p2d);
+ }
+ catch(InvalidPositionException &e)
+ {
+ }
+
+ // Create a sector
+ ClientMapSector *sector = new ClientMapSector(this, p2d);
+
+ {
+ //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
+ m_sectors.insert(p2d, sector);
+ }
+
+ return sector;
+}
+
+void ClientMap::deSerializeSector(v2s16 p2d, std::istream &is)
+{
+ DSTACK(__FUNCTION_NAME);
+ ClientMapSector *sector = NULL;
+
+ //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
+
+ core::map<v2s16, MapSector*>::Node *n = m_sectors.find(p2d);
+
+ if(n != NULL)
+ {
+ sector = (ClientMapSector*)n->getValue();
+ assert(sector->getId() == MAPSECTOR_CLIENT);
+ }
+ else
+ {
+ sector = new ClientMapSector(this, p2d);
+ {
+ //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
+ m_sectors.insert(p2d, sector);
+ }
+ }
+
+ sector->deSerialize(is);
+}
+
+void ClientMap::OnRegisterSceneNode()
+{
+ if(IsVisible)
+ {
+ SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
+ SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
+ }
+
+ ISceneNode::OnRegisterSceneNode();
+}
+
+void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
+{
+ //m_dout<<DTIME<<"Rendering map..."<<std::endl;
+ DSTACK(__FUNCTION_NAME);
+
+ bool is_transparent_pass = pass == scene::ESNRP_TRANSPARENT;
+
+ /*
+ Get time for measuring timeout.
+
+ Measuring time is very useful for long delays when the
+ machine is swapping a lot.
+ */
+ int time1 = time(0);
+
+ //u32 daynight_ratio = m_client->getDayNightRatio();
+
+ m_camera_mutex.Lock();
+ v3f camera_position = m_camera_position;
+ v3f camera_direction = m_camera_direction;
+ m_camera_mutex.Unlock();
+
+ /*
+ Get all blocks and draw all visible ones
+ */
+
+ v3s16 cam_pos_nodes(
+ camera_position.X / BS,
+ camera_position.Y / BS,
+ camera_position.Z / BS);
+
+ v3s16 box_nodes_d = m_control.wanted_range * v3s16(1,1,1);
+
+ v3s16 p_nodes_min = cam_pos_nodes - box_nodes_d;
+ v3s16 p_nodes_max = cam_pos_nodes + box_nodes_d;
+
+ // Take a fair amount as we will be dropping more out later
+ v3s16 p_blocks_min = getNodeBlockPos(p_nodes_min) - v3s16(1,1,1);
+ v3s16 p_blocks_max = getNodeBlockPos(p_nodes_max) + v3s16(1,1,1);
+
+ u32 vertex_count = 0;
+
+ // For limiting number of mesh updates per frame
+ u32 mesh_update_count = 0;
+
+ u32 blocks_would_have_drawn = 0;
+ u32 blocks_drawn = 0;
+
+ //NOTE: The sectors map should be locked but we're not doing it
+ // because it'd cause too much delays
+
+ int timecheck_counter = 0;
+ core::map<v2s16, MapSector*>::Iterator si;
+ si = m_sectors.getIterator();
+ for(; si.atEnd() == false; si++)
+ {
+ {
+ timecheck_counter++;
+ if(timecheck_counter > 50)
+ {
+ timecheck_counter = 0;
+ int time2 = time(0);
+ if(time2 > time1 + 4)
+ {
+ dstream<<"ClientMap::renderMap(): "
+ "Rendering takes ages, returning."
+ <<std::endl;
+ return;
+ }
+ }
+ }
+
+ MapSector *sector = si.getNode()->getValue();
+ v2s16 sp = sector->getPos();
+
+ if(m_control.range_all == false)
+ {
+ if(sp.X < p_blocks_min.X
+ || sp.X > p_blocks_max.X
+ || sp.Y < p_blocks_min.Z
+ || sp.Y > p_blocks_max.Z)
+ continue;
+ }
+
+ core::list< MapBlock * > sectorblocks;
+ sector->getBlocks(sectorblocks);
+
+ /*
+ Draw blocks
+ */
+
+ core::list< MapBlock * >::Iterator i;
+ for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++)
+ {
+ MapBlock *block = *i;
+
+ /*
+ Compare block position to camera position, skip
+ if not seen on display
+ */
+
+ float range = 100000 * BS;
+ if(m_control.range_all == false)
+ range = m_control.wanted_range * BS;
+
+ float d = 0.0;
+ if(isBlockInSight(block->getPos(), camera_position,
+ camera_direction, range, &d) == false)
+ {
+ continue;
+ }
+
+ // This is ugly (spherical distance limit?)
+ /*if(m_control.range_all == false &&
+ d - 0.5*BS*MAP_BLOCKSIZE > range)
+ continue;*/
+
+#if 1
+ /*
+ Update expired mesh (used for day/night change)
+
+ It doesn't work exactly like it should now with the
+ tasked mesh update but whatever.
+ */
+
+ bool mesh_expired = false;
+
+ {
+ JMutexAutoLock lock(block->mesh_mutex);
+
+ mesh_expired = block->getMeshExpired();
+
+ // Mesh has not been expired and there is no mesh:
+ // block has no content
+ if(block->mesh == NULL && mesh_expired == false)
+ continue;
+ }
+
+ f32 faraway = BS*50;
+ //f32 faraway = m_control.wanted_range * BS;
+
+ /*
+ This has to be done with the mesh_mutex unlocked
+ */
+ // Pretty random but this should work somewhat nicely
+ if(mesh_expired && (
+ (mesh_update_count < 3
+ && (d < faraway || mesh_update_count < 2)
+ )
+ ||
+ (m_control.range_all && mesh_update_count < 20)
+ )
+ )
+ /*if(mesh_expired && mesh_update_count < 6
+ && (d < faraway || mesh_update_count < 3))*/
+ {
+ mesh_update_count++;
+
+ // Mesh has been expired: generate new mesh
+ //block->updateMesh(daynight_ratio);
+ m_client->addUpdateMeshTask(block->getPos());
+
+ mesh_expired = false;
+ }
+
+#endif
+ /*
+ Draw the faces of the block
+ */
+ {
+ JMutexAutoLock lock(block->mesh_mutex);
+
+ scene::SMesh *mesh = block->mesh;
+
+ if(mesh == NULL)
+ continue;
+
+ blocks_would_have_drawn++;
+ if(blocks_drawn >= m_control.wanted_max_blocks
+ && m_control.range_all == false
+ && d > m_control.wanted_min_range * BS)
+ continue;
+ blocks_drawn++;
+
+ u32 c = mesh->getMeshBufferCount();
+
+ for(u32 i=0; i<c; i++)
+ {
+ scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
+ const video::SMaterial& material = buf->getMaterial();
+ video::IMaterialRenderer* rnd =
+ driver->getMaterialRenderer(material.MaterialType);
+ bool transparent = (rnd && rnd->isTransparent());
+ // Render transparent on transparent pass and likewise.
+ if(transparent == is_transparent_pass)
+ {
+ /*
+ This *shouldn't* hurt too much because Irrlicht
+ doesn't change opengl textures if the old
+ material is set again.
+ */
+ driver->setMaterial(buf->getMaterial());
+ driver->drawMeshBuffer(buf);
+ vertex_count += buf->getVertexCount();
+ }
+ }
+ }
+ } // foreach sectorblocks
+ }
+
+ m_control.blocks_drawn = blocks_drawn;
+ m_control.blocks_would_have_drawn = blocks_would_have_drawn;
+
+ /*dstream<<"renderMap(): is_transparent_pass="<<is_transparent_pass
+ <<", rendered "<<vertex_count<<" vertices."<<std::endl;*/
+}
+
+bool ClientMap::setTempMod(v3s16 p, NodeMod mod,
+ core::map<v3s16, MapBlock*> *affected_blocks)
+{
+ bool changed = false;
+ /*
+ Add it to all blocks touching it
+ */
+ v3s16 dirs[7] = {
+ v3s16(0,0,0), // this
+ v3s16(0,0,1), // back
+ v3s16(0,1,0), // top
+ v3s16(1,0,0), // right
+ v3s16(0,0,-1), // front
+ v3s16(0,-1,0), // bottom
+ v3s16(-1,0,0), // left
+ };
+ for(u16 i=0; i<7; i++)
+ {
+ v3s16 p2 = p + dirs[i];
+ // Block position of neighbor (or requested) node
+ v3s16 blockpos = getNodeBlockPos(p2);
+ MapBlock * blockref = getBlockNoCreateNoEx(blockpos);
+ if(blockref == NULL)
+ continue;
+ // Relative position of requested node
+ v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
+ if(blockref->setTempMod(relpos, mod))
+ {
+ changed = true;
+ }
+ }
+ if(changed && affected_blocks!=NULL)
+ {
+ for(u16 i=0; i<7; i++)
+ {
+ v3s16 p2 = p + dirs[i];
+ // Block position of neighbor (or requested) node
+ v3s16 blockpos = getNodeBlockPos(p2);
+ MapBlock * blockref = getBlockNoCreateNoEx(blockpos);
+ if(blockref == NULL)
+ continue;
+ affected_blocks->insert(blockpos, blockref);
+ }
+ }
+ return changed;
+}
+
+bool ClientMap::clearTempMod(v3s16 p,
+ core::map<v3s16, MapBlock*> *affected_blocks)
+{
+ bool changed = false;
+ v3s16 dirs[7] = {
+ v3s16(0,0,0), // this
+ v3s16(0,0,1), // back
+ v3s16(0,1,0), // top
+ v3s16(1,0,0), // right
+ v3s16(0,0,-1), // front
+ v3s16(0,-1,0), // bottom
+ v3s16(-1,0,0), // left
+ };
+ for(u16 i=0; i<7; i++)
+ {
+ v3s16 p2 = p + dirs[i];
+ // Block position of neighbor (or requested) node
+ v3s16 blockpos = getNodeBlockPos(p2);
+ MapBlock * blockref = getBlockNoCreateNoEx(blockpos);
+ if(blockref == NULL)
+ continue;
+ // Relative position of requested node
+ v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
+ if(blockref->clearTempMod(relpos))
+ {
+ changed = true;
+ }
+ }
+ if(changed && affected_blocks!=NULL)
+ {
+ for(u16 i=0; i<7; i++)
+ {
+ v3s16 p2 = p + dirs[i];
+ // Block position of neighbor (or requested) node
+ v3s16 blockpos = getNodeBlockPos(p2);
+ MapBlock * blockref = getBlockNoCreateNoEx(blockpos);
+ if(blockref == NULL)
+ continue;
+ affected_blocks->insert(blockpos, blockref);
+ }
+ }
+ return changed;
+}
+
+void ClientMap::expireMeshes(bool only_daynight_diffed)
+{
+ TimeTaker timer("expireMeshes()");
+
+ core::map<v2s16, MapSector*>::Iterator si;
+ si = m_sectors.getIterator();
+ for(; si.atEnd() == false; si++)
+ {
+ MapSector *sector = si.getNode()->getValue();
+
+ core::list< MapBlock * > sectorblocks;
+ sector->getBlocks(sectorblocks);
+
+ core::list< MapBlock * >::Iterator i;
+ for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++)
+ {
+ MapBlock *block = *i;
+
+ if(only_daynight_diffed && dayNightDiffed(block->getPos()) == false)
+ {
+ continue;
+ }
+
+ {
+ JMutexAutoLock lock(block->mesh_mutex);
+ if(block->mesh != NULL)
+ {
+ /*block->mesh->drop();
+ block->mesh = NULL;*/
+ block->setMeshExpired(true);
+ }
+ }
+ }
+ }
+}
+
+void ClientMap::updateMeshes(v3s16 blockpos, u32 daynight_ratio)
+{
+ assert(mapType() == MAPTYPE_CLIENT);
+
+ try{
+ v3s16 p = blockpos + v3s16(0,0,0);
+ MapBlock *b = getBlockNoCreate(p);
+ b->updateMesh(daynight_ratio);
+ //b->setMeshExpired(true);
+ }
+ catch(InvalidPositionException &e){}
+ // Leading edge
+ try{
+ v3s16 p = blockpos + v3s16(-1,0,0);
+ MapBlock *b = getBlockNoCreate(p);
+ b->updateMesh(daynight_ratio);
+ //b->setMeshExpired(true);
+ }
+ catch(InvalidPositionException &e){}
+ try{
+ v3s16 p = blockpos + v3s16(0,-1,0);
+ MapBlock *b = getBlockNoCreate(p);
+ b->updateMesh(daynight_ratio);
+ //b->setMeshExpired(true);
+ }
+ catch(InvalidPositionException &e){}
+ try{
+ v3s16 p = blockpos + v3s16(0,0,-1);
+ MapBlock *b = getBlockNoCreate(p);
+ b->updateMesh(daynight_ratio);
+ //b->setMeshExpired(true);
+ }
+ catch(InvalidPositionException &e){}
+}
+
+#if 0
+/*
+ Update mesh of block in which the node is, and if the node is at the
+ leading edge, update the appropriate leading blocks too.
+*/
+void ClientMap::updateNodeMeshes(v3s16 nodepos, u32 daynight_ratio)
+{
+ v3s16 dirs[4] = {
+ v3s16(0,0,0),
+ v3s16(-1,0,0),
+ v3s16(0,-1,0),
+ v3s16(0,0,-1),
+ };
+ v3s16 blockposes[4];
+ for(u32 i=0; i<4; i++)
+ {
+ v3s16 np = nodepos + dirs[i];
+ blockposes[i] = getNodeBlockPos(np);
+ // Don't update mesh of block if it has been done already
+ bool already_updated = false;
+ for(u32 j=0; j<i; j++)
+ {
+ if(blockposes[j] == blockposes[i])
+ {
+ already_updated = true;
+ break;
+ }
+ }
+ if(already_updated)
+ continue;
+ // Update mesh
+ MapBlock *b = getBlockNoCreate(blockposes[i]);
+ b->updateMesh(daynight_ratio);
+ }
+}
+#endif
+
+void ClientMap::PrintInfo(std::ostream &out)
+{
+ out<<"ClientMap: ";
+}
+
+#endif // !SERVER
+
+/*
+ MapVoxelManipulator
+*/
+
+MapVoxelManipulator::MapVoxelManipulator(Map *map)
+{
+ m_map = map;
+}
+
+MapVoxelManipulator::~MapVoxelManipulator()
+{
+ /*dstream<<"MapVoxelManipulator: blocks: "<<m_loaded_blocks.size()
+ <<std::endl;*/
+}
+
+void MapVoxelManipulator::emerge(VoxelArea a, s32 caller_id)
+{
+ TimeTaker timer1("emerge", &emerge_time);
+
+ // Units of these are MapBlocks
+ v3s16 p_min = getNodeBlockPos(a.MinEdge);
+ v3s16 p_max = getNodeBlockPos(a.MaxEdge);
+
+ VoxelArea block_area_nodes
+ (p_min*MAP_BLOCKSIZE, (p_max+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
+
+ addArea(block_area_nodes);
+
+ for(s32 z=p_min.Z; z<=p_max.Z; z++)
+ for(s32 y=p_min.Y; y<=p_max.Y; y++)
+ for(s32 x=p_min.X; x<=p_max.X; x++)
+ {
+ v3s16 p(x,y,z);
+ core::map<v3s16, bool>::Node *n;
+ n = m_loaded_blocks.find(p);
+ if(n != NULL)
+ continue;
+
+ bool block_data_inexistent = false;
+ try
+ {
+ TimeTaker timer1("emerge load", &emerge_load_time);
+
+ /*dstream<<"Loading block (caller_id="<<caller_id<<")"
+ <<" ("<<p.X<<","<<p.Y<<","<<p.Z<<")"
+ <<" wanted area: ";
+ a.print(dstream);
+ dstream<<std::endl;*/
+
+ MapBlock *block = m_map->getBlockNoCreate(p);
+ if(block->isDummy())
+ block_data_inexistent = true;
+ else
+ block->copyTo(*this);
+ }
+ catch(InvalidPositionException &e)
+ {
+ block_data_inexistent = true;
+ }
+
+ if(block_data_inexistent)
+ {
+ VoxelArea a(p*MAP_BLOCKSIZE, (p+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
+ // Fill with VOXELFLAG_INEXISTENT
+ for(s32 z=a.MinEdge.Z; z<=a.MaxEdge.Z; z++)
+ for(s32 y=a.MinEdge.Y; y<=a.MaxEdge.Y; y++)
+ {
+ s32 i = m_area.index(a.MinEdge.X,y,z);
+ memset(&m_flags[i], VOXELFLAG_INEXISTENT, MAP_BLOCKSIZE);
+ }
+ }
+
+ m_loaded_blocks.insert(p, !block_data_inexistent);
+ }
+
+ //dstream<<"emerge done"<<std::endl;
+}
+
+/*
+ SUGG: Add an option to only update eg. water and air nodes.
+ This will make it interfere less with important stuff if
+ run on background.
+*/
+void MapVoxelManipulator::blitBack
+ (core::map<v3s16, MapBlock*> & modified_blocks)
+{
+ if(m_area.getExtent() == v3s16(0,0,0))
+ return;
+
+ //TimeTaker timer1("blitBack");
+
+ /*dstream<<"blitBack(): m_loaded_blocks.size()="
+ <<m_loaded_blocks.size()<<std::endl;*/
+
+ /*
+ Initialize block cache
+ */
+ v3s16 blockpos_last;
+ MapBlock *block = NULL;
+ bool block_checked_in_modified = false;
+
+ for(s32 z=m_area.MinEdge.Z; z<=m_area.MaxEdge.Z; z++)
+ for(s32 y=m_area.MinEdge.Y; y<=m_area.MaxEdge.Y; y++)
+ for(s32 x=m_area.MinEdge.X; x<=m_area.MaxEdge.X; x++)
+ {
+ v3s16 p(x,y,z);
+
+ u8 f = m_flags[m_area.index(p)];
+ if(f & (VOXELFLAG_NOT_LOADED|VOXELFLAG_INEXISTENT))
+ continue;
+
+ MapNode &n = m_data[m_area.index(p)];
+
+ v3s16 blockpos = getNodeBlockPos(p);
+
+ try
+ {
+ // Get block
+ if(block == NULL || blockpos != blockpos_last){
+ block = m_map->getBlockNoCreate(blockpos);
+ blockpos_last = blockpos;
+ block_checked_in_modified = false;
+ }
+
+ // Calculate relative position in block
+ v3s16 relpos = p - blockpos * MAP_BLOCKSIZE;
+
+ // Don't continue if nothing has changed here
+ if(block->getNode(relpos) == n)
+ continue;
+
+ //m_map->setNode(m_area.MinEdge + p, n);
+ block->setNode(relpos, n);
+
+ /*
+ Make sure block is in modified_blocks
+ */
+ if(block_checked_in_modified == false)
+ {
+ modified_blocks[blockpos] = block;
+ block_checked_in_modified = true;
+ }
+ }
+ catch(InvalidPositionException &e)
+ {
+ }
+ }
+}
+
+ManualMapVoxelManipulator::ManualMapVoxelManipulator(Map *map):
+ MapVoxelManipulator(map),
+ m_create_area(false)
+{
+}
+
+ManualMapVoxelManipulator::~ManualMapVoxelManipulator()
+{
+}
+
+void ManualMapVoxelManipulator::emerge(VoxelArea a, s32 caller_id)
+{
+ // Just create the area so that it can be pointed to
+ VoxelManipulator::emerge(a, caller_id);
+}
+
+void ManualMapVoxelManipulator::initialEmerge(
+ v3s16 blockpos_min, v3s16 blockpos_max)
+{
+ TimeTaker timer1("initialEmerge", &emerge_time);
+
+ // Units of these are MapBlocks
+ v3s16 p_min = blockpos_min;
+ v3s16 p_max = blockpos_max;
+
+ VoxelArea block_area_nodes
+ (p_min*MAP_BLOCKSIZE, (p_max+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
+
+ u32 size_MB = block_area_nodes.getVolume()*4/1000000;
+ if(size_MB >= 1)
+ {
+ dstream<<"initialEmerge: area: ";
+ block_area_nodes.print(dstream);
+ dstream<<" ("<<size_MB<<"MB)";
+ dstream<<std::endl;
+ }
+
+ addArea(block_area_nodes);
+
+ for(s32 z=p_min.Z; z<=p_max.Z; z++)
+ for(s32 y=p_min.Y; y<=p_max.Y; y++)
+ for(s32 x=p_min.X; x<=p_max.X; x++)
+ {
+ v3s16 p(x,y,z);
+ core::map<v3s16, bool>::Node *n;
+ n = m_loaded_blocks.find(p);
+ if(n != NULL)
+ continue;
+
+ bool block_data_inexistent = false;
+ try
+ {
+ TimeTaker timer1("emerge load", &emerge_load_time);
+
+ MapBlock *block = m_map->getBlockNoCreate(p);
+ if(block->isDummy())
+ block_data_inexistent = true;
+ else
+ block->copyTo(*this);
+ }
+ catch(InvalidPositionException &e)
+ {
+ block_data_inexistent = true;
+ }
+
+ if(block_data_inexistent)
+ {
+ /*
+ Mark area inexistent
+ */
+ VoxelArea a(p*MAP_BLOCKSIZE, (p+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
+ // Fill with VOXELFLAG_INEXISTENT
+ for(s32 z=a.MinEdge.Z; z<=a.MaxEdge.Z; z++)
+ for(s32 y=a.MinEdge.Y; y<=a.MaxEdge.Y; y++)
+ {
+ s32 i = m_area.index(a.MinEdge.X,y,z);
+ memset(&m_flags[i], VOXELFLAG_INEXISTENT, MAP_BLOCKSIZE);
+ }
+ }
+
+ m_loaded_blocks.insert(p, !block_data_inexistent);
+ }
+}
+
+void ManualMapVoxelManipulator::blitBackAll(
+ core::map<v3s16, MapBlock*> * modified_blocks)
+{
+ if(m_area.getExtent() == v3s16(0,0,0))
+ return;
+
+ /*
+ Copy data of all blocks
+ */
+ for(core::map<v3s16, bool>::Iterator
+ i = m_loaded_blocks.getIterator();
+ i.atEnd() == false; i++)
+ {
+ bool existed = i.getNode()->getValue();
+ if(existed == false)
+ continue;
+ v3s16 p = i.getNode()->getKey();
+ MapBlock *block = m_map->getBlockNoCreateNoEx(p);
+ if(block == NULL)
+ {
+ dstream<<"WARNING: "<<__FUNCTION_NAME
+ <<": got NULL block "
+ <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
+ <<std::endl;
+ continue;
+ }
+
+ block->copyFrom(*this);
+
+ if(modified_blocks)
+ modified_blocks->insert(p, block);
+ }
+}
+
+//END
diff --git a/misc/noise.cpp b/misc/noise.cpp
new file mode 100644
index 000000000..e7923dae1
--- /dev/null
+++ b/misc/noise.cpp
@@ -0,0 +1,410 @@
+/*
+Minetest-c55
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include <math.h>
+#include "noise.h"
+#include <iostream>
+#include "debug.h"
+
+#define NOISE_MAGIC_X 1619
+#define NOISE_MAGIC_Y 31337
+#define NOISE_MAGIC_Z 52591
+#define NOISE_MAGIC_SEED 1013
+
+double cos_lookup[16] = {
+ 1.0,0.9238,0.7071,0.3826,0,-0.3826,-0.7071,-0.9238,
+ 1.0,-0.9238,-0.7071,-0.3826,0,0.3826,0.7071,0.9238
+};
+
+double dotProduct(double vx, double vy, double wx, double wy){
+ return vx*wx+vy*wy;
+}
+
+double easeCurve(double t){
+ return 6*pow(t,5)-15*pow(t,4)+10*pow(t,3);
+}
+
+double linearInterpolation(double x0, double x1, double t){
+ return x0+(x1-x0)*t;
+}
+
+double biLinearInterpolation(double x0y0, double x1y0, double x0y1, double x1y1, double x, double y){
+ double tx = easeCurve(x);
+ double ty = easeCurve(y);
+ /*double tx = x;
+ double ty = y;*/
+ double u = linearInterpolation(x0y0,x1y0,tx);
+ double v = linearInterpolation(x0y1,x1y1,tx);
+ return linearInterpolation(u,v,ty);
+}
+
+double triLinearInterpolation(
+ double v000, double v100, double v010, double v110,
+ double v001, double v101, double v011, double v111,
+ double x, double y, double z)
+{
+ /*double tx = easeCurve(x);
+ double ty = easeCurve(y);
+ double tz = easeCurve(z);*/
+ double tx = x;
+ double ty = y;
+ double tz = z;
+ return(
+ v000*(1-tx)*(1-ty)*(1-tz) +
+ v100*tx*(1-ty)*(1-tz) +
+ v010*(1-tx)*ty*(1-tz) +
+ v110*tx*ty*(1-tz) +
+ v001*(1-tx)*(1-ty)*tz +
+ v101*tx*(1-ty)*tz +
+ v011*(1-tx)*ty*tz +
+ v111*tx*ty*tz
+ );
+}
+
+double noise2d(int x, int y, int seed)
+{
+ int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y
+ + NOISE_MAGIC_SEED * seed) & 0x7fffffff;
+ n = (n>>13)^n;
+ n = (n * (n*n*60493+19990303) + 1376312589) & 0x7fffffff;
+ return 1.0 - (double)n/1073741824;
+}
+
+double noise3d(int x, int y, int z, int seed)
+{
+ int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y + NOISE_MAGIC_Z * z
+ + NOISE_MAGIC_SEED * seed) & 0x7fffffff;
+ n = (n>>13)^n;
+ n = (n * (n*n*60493+19990303) + 1376312589) & 0x7fffffff;
+ return 1.0 - (double)n/1073741824;
+}
+
+#if 0
+double noise2d_gradient(double x, double y, int seed)
+{
+ // Calculate the integer coordinates
+ int x0 = (x > 0.0 ? (int)x : (int)x - 1);
+ int y0 = (y > 0.0 ? (int)y : (int)y - 1);
+ // Calculate the remaining part of the coordinates
+ double xl = x - (double)x0;
+ double yl = y - (double)y0;
+ // Calculate random cosine lookup table indices for the integer corners.
+ // They are looked up as unit vector gradients from the lookup table.
+ int n00 = (int)((noise2d(x0, y0, seed)+1)*8);
+ int n10 = (int)((noise2d(x0+1, y0, seed)+1)*8);
+ int n01 = (int)((noise2d(x0, y0+1, seed)+1)*8);
+ int n11 = (int)((noise2d(x0+1, y0+1, seed)+1)*8);
+ // Make a dot product for the gradients and the positions, to get the values
+ double s = dotProduct(cos_lookup[n00], cos_lookup[(n00+12)%16], xl, yl);
+ double u = dotProduct(-cos_lookup[n10], cos_lookup[(n10+12)%16], 1.-xl, yl);
+ double v = dotProduct(cos_lookup[n01], -cos_lookup[(n01+12)%16], xl, 1.-yl);
+ double w = dotProduct(-cos_lookup[n11], -cos_lookup[(n11+12)%16], 1.-xl, 1.-yl);
+ // Interpolate between the values
+ return biLinearInterpolation(s,u,v,w,xl,yl);
+}
+#endif
+
+#if 1
+double noise2d_gradient(double x, double y, int seed)
+{
+ // Calculate the integer coordinates
+ int x0 = (x > 0.0 ? (int)x : (int)x - 1);
+ int y0 = (y > 0.0 ? (int)y : (int)y - 1);
+ // Calculate the remaining part of the coordinates
+ double xl = x - (double)x0;
+ double yl = y - (double)y0;
+ // Get values for corners of cube
+ double v00 = noise2d(x0, y0, seed);
+ double v10 = noise2d(x0+1, y0, seed);
+ double v01 = noise2d(x0, y0+1, seed);
+ double v11 = noise2d(x0+1, y0+1, seed);
+ // Interpolate
+ return biLinearInterpolation(v00,v10,v01,v11,xl,yl);
+}
+#endif
+
+double noise3d_gradient(double x, double y, double z, int seed)
+{
+ // Calculate the integer coordinates
+ int x0 = (x > 0.0 ? (int)x : (int)x - 1);
+ int y0 = (y > 0.0 ? (int)y : (int)y - 1);
+ int z0 = (z > 0.0 ? (int)z : (int)z - 1);
+ // Calculate the remaining part of the coordinates
+ double xl = x - (double)x0;
+ double yl = y - (double)y0;
+ double zl = z - (double)z0;
+ // Get values for corners of cube
+ double v000 = noise3d(x0, y0, z0, seed);
+ double v100 = noise3d(x0+1, y0, z0, seed);
+ double v010 = noise3d(x0, y0+1, z0, seed);
+ double v110 = noise3d(x0+1, y0+1, z0, seed);
+ double v001 = noise3d(x0, y0, z0+1, seed);
+ double v101 = noise3d(x0+1, y0, z0+1, seed);
+ double v011 = noise3d(x0, y0+1, z0+1, seed);
+ double v111 = noise3d(x0+1, y0+1, z0+1, seed);
+ // Interpolate
+ return triLinearInterpolation(v000,v100,v010,v110,v001,v101,v011,v111,xl,yl,zl);
+}
+
+double noise2d_perlin(double x, double y, int seed,
+ int octaves, double persistence)
+{
+ double a = 0;
+ double f = 1.0;
+ double g = 1.0;
+ for(int i=0; i<octaves; i++)
+ {
+ a += g * noise2d_gradient(x*f, y*f, seed+i);
+ f *= 2.0;
+ g *= persistence;
+ }
+ return a;
+}
+
+double noise2d_perlin_abs(double x, double y, int seed,
+ int octaves, double persistence)
+{
+ double a = 0;
+ double f = 1.0;
+ double g = 1.0;
+ for(int i=0; i<octaves; i++)
+ {
+ a += g * fabs(noise2d_gradient(x*f, y*f, seed+i));
+ f *= 2.0;
+ g *= persistence;
+ }
+ return a;
+}
+
+double noise3d_perlin(double x, double y, double z, int seed,
+ int octaves, double persistence)
+{
+ double a = 0;
+ double f = 1.0;
+ double g = 1.0;
+ for(int i=0; i<octaves; i++)
+ {
+ a += g * noise3d_gradient(x*f, y*f, z*f, seed+i);
+ f *= 2.0;
+ g *= persistence;
+ }
+ return a;
+}
+
+double noise3d_perlin_abs(double x, double y, double z, int seed,
+ int octaves, double persistence)
+{
+ double a = 0;
+ double f = 1.0;
+ double g = 1.0;
+ for(int i=0; i<octaves; i++)
+ {
+ a += g * fabs(noise3d_gradient(x*f, y*f, z*f, seed+i));
+ f *= 2.0;
+ g *= persistence;
+ }
+ return a;
+}
+
+// -1->0, 0->1, 1->0
+double contour(double v)
+{
+ v = fabs(v);
+ if(v >= 1.0)
+ return 0.0;
+ return (1.0-v);
+}
+
+double noise3d_param(const NoiseParams &param, double x, double y, double z)
+{
+ double s = param.pos_scale;
+ x /= s;
+ y /= s;
+ z /= s;
+
+ if(param.type == NOISE_PERLIN)
+ {
+ return param.noise_scale*noise3d_perlin(x,y,z, param.seed,
+ param.octaves,
+ param.persistence);
+ }
+ else if(param.type == NOISE_PERLIN_ABS)
+ {
+ return param.noise_scale*noise3d_perlin_abs(x,y,z, param.seed,
+ param.octaves,
+ param.persistence);
+ }
+ else if(param.type == NOISE_PERLIN_CONTOUR)
+ {
+ return contour(param.noise_scale*noise3d_perlin(x,y,z,
+ param.seed, param.octaves,
+ param.persistence));
+ }
+ else if(param.type == NOISE_PERLIN_CONTOUR_FLIP_YZ)
+ {
+ return contour(param.noise_scale*noise3d_perlin(x,z,y,
+ param.seed, param.octaves,
+ param.persistence));
+ }
+ else assert(0);
+}
+
+/*
+ NoiseBuffer
+*/
+
+NoiseBuffer::NoiseBuffer():
+ m_data(NULL)
+{
+}
+
+NoiseBuffer::~NoiseBuffer()
+{
+ clear();
+}
+
+void NoiseBuffer::clear()
+{
+ if(m_data)
+ delete[] m_data;
+ m_data = NULL;
+ m_size_x = 0;
+ m_size_y = 0;
+ m_size_z = 0;
+}
+
+void NoiseBuffer::create(const NoiseParams &param,
+ double first_x, double first_y, double first_z,
+ double last_x, double last_y, double last_z,
+ double samplelength_x, double samplelength_y, double samplelength_z)
+{
+ clear();
+
+ m_start_x = first_x - samplelength_x;
+ m_start_y = first_y - samplelength_y;
+ m_start_z = first_z - samplelength_z;
+ m_samplelength_x = samplelength_x;
+ m_samplelength_y = samplelength_y;
+ m_samplelength_z = samplelength_z;
+
+ m_size_x = (last_x - m_start_x)/samplelength_x + 2;
+ m_size_y = (last_y - m_start_y)/samplelength_y + 2;
+ m_size_z = (last_z - m_start_z)/samplelength_z + 2;
+
+ m_data = new double[m_size_x*m_size_y*m_size_z];
+
+ for(int x=0; x<m_size_x; x++)
+ for(int y=0; y<m_size_y; y++)
+ for(int z=0; z<m_size_z; z++)
+ {
+ double xd = (m_start_x + (double)x*m_samplelength_x);
+ double yd = (m_start_y + (double)y*m_samplelength_y);
+ double zd = (m_start_z + (double)z*m_samplelength_z);
+ double a = noise3d_param(param, xd,yd,zd);
+ intSet(x,y,z, a);
+ }
+}
+
+void NoiseBuffer::multiply(const NoiseParams &param)
+{
+ assert(m_data != NULL);
+
+ for(int x=0; x<m_size_x; x++)
+ for(int y=0; y<m_size_y; y++)
+ for(int z=0; z<m_size_z; z++)
+ {
+ double xd = (m_start_x + (double)x*m_samplelength_x);
+ double yd = (m_start_y + (double)y*m_samplelength_y);
+ double zd = (m_start_z + (double)z*m_samplelength_z);
+ double a = noise3d_param(param, xd,yd,zd);
+ intMultiply(x,y,z, a);
+ }
+}
+
+// Deprecated
+void NoiseBuffer::create(int seed, int octaves, double persistence,
+ bool abs,
+ double first_x, double first_y, double first_z,
+ double last_x, double last_y, double last_z,
+ double samplelength_x, double samplelength_y, double samplelength_z)
+{
+ NoiseParams param;
+ param.type = abs ? NOISE_PERLIN_ABS : NOISE_PERLIN;
+ param.seed = seed;
+ param.octaves = octaves;
+ param.persistence = persistence;
+
+ create(param, first_x, first_y, first_z,
+ last_x, last_y, last_z,
+ samplelength_x, samplelength_y, samplelength_z);
+}
+
+void NoiseBuffer::intSet(int x, int y, int z, double d)
+{
+ int i = m_size_x*m_size_y*z + m_size_x*y + x;
+ assert(i >= 0);
+ assert(i < m_size_x*m_size_y*m_size_z);
+ m_data[i] = d;
+}
+
+void NoiseBuffer::intMultiply(int x, int y, int z, double d)
+{
+ int i = m_size_x*m_size_y*z + m_size_x*y + x;
+ assert(i >= 0);
+ assert(i < m_size_x*m_size_y*m_size_z);
+ m_data[i] = m_data[i] * d;
+}
+
+double NoiseBuffer::intGet(int x, int y, int z)
+{
+ int i = m_size_x*m_size_y*z + m_size_x*y + x;
+ assert(i >= 0);
+ assert(i < m_size_x*m_size_y*m_size_z);
+ return m_data[i];
+}
+
+double NoiseBuffer::get(double x, double y, double z)
+{
+ x -= m_start_x;
+ y -= m_start_y;
+ z -= m_start_z;
+ x /= m_samplelength_x;
+ y /= m_samplelength_y;
+ z /= m_samplelength_z;
+ // Calculate the integer coordinates
+ int x0 = (x > 0.0 ? (int)x : (int)x - 1);
+ int y0 = (y > 0.0 ? (int)y : (int)y - 1);
+ int z0 = (z > 0.0 ? (int)z : (int)z - 1);
+ // Calculate the remaining part of the coordinates
+ double xl = x - (double)x0;
+ double yl = y - (double)y0;
+ double zl = z - (double)z0;
+ // Get values for corners of cube
+ double v000 = intGet(x0, y0, z0);
+ double v100 = intGet(x0+1, y0, z0);
+ double v010 = intGet(x0, y0+1, z0);
+ double v110 = intGet(x0+1, y0+1, z0);
+ double v001 = intGet(x0, y0, z0+1);
+ double v101 = intGet(x0+1, y0, z0+1);
+ double v011 = intGet(x0, y0+1, z0+1);
+ double v111 = intGet(x0+1, y0+1, z0+1);
+ // Interpolate
+ return triLinearInterpolation(v000,v100,v010,v110,v001,v101,v011,v111,xl,yl,zl);
+}
+
diff --git a/misc/noise.h b/misc/noise.h
new file mode 100644
index 000000000..59ca1bf94
--- /dev/null
+++ b/misc/noise.h
@@ -0,0 +1,108 @@
+/*
+Minetest-c55
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#ifndef NOISE_HEADER
+#define NOISE_HEADER
+
+double easeCurve(double t);
+
+// Return value: -1 ... 1
+double noise2d(int x, int y, int seed);
+double noise3d(int x, int y, int z, int seed);
+
+double noise2d_gradient(double x, double y, int seed);
+double noise3d_gradient(double x, double y, double z, int seed);
+
+double noise2d_perlin(double x, double y, int seed,
+ int octaves, double persistence);
+
+double noise2d_perlin_abs(double x, double y, int seed,
+ int octaves, double persistence);
+
+double noise3d_perlin(double x, double y, double z, int seed,
+ int octaves, double persistence);
+
+double noise3d_perlin_abs(double x, double y, double z, int seed,
+ int octaves, double persistence);
+
+enum NoiseType
+{
+ NOISE_PERLIN,
+ NOISE_PERLIN_ABS,
+ NOISE_PERLIN_CONTOUR,
+ NOISE_PERLIN_CONTOUR_FLIP_YZ
+};
+
+struct NoiseParams
+{
+ NoiseType type;
+ int seed;
+ int octaves;
+ double persistence;
+ double pos_scale;
+ double noise_scale; // Useful for contour noises
+
+ NoiseParams(NoiseType type_=NOISE_PERLIN, int seed_=0,
+ int octaves_=3, double persistence_=0.5,
+ double pos_scale_=100.0, double noise_scale_=1.0):
+ type(type_),
+ seed(seed_),
+ octaves(octaves_),
+ persistence(persistence_),
+ pos_scale(pos_scale_),
+ noise_scale(noise_scale_)
+ {
+ }
+};
+
+double noise3d_param(const NoiseParams &param, double x, double y, double z);
+
+class NoiseBuffer
+{
+public:
+ NoiseBuffer();
+ ~NoiseBuffer();
+
+ void clear();
+ void create(const NoiseParams &param,
+ double first_x, double first_y, double first_z,
+ double last_x, double last_y, double last_z,
+ double samplelength_x, double samplelength_y, double samplelength_z);
+ void multiply(const NoiseParams &param);
+ // Deprecated
+ void create(int seed, int octaves, double persistence,
+ bool abs,
+ double first_x, double first_y, double first_z,
+ double last_x, double last_y, double last_z,
+ double samplelength_x, double samplelength_y, double samplelength_z);
+
+ void intSet(int x, int y, int z, double d);
+ void intMultiply(int x, int y, int z, double d);
+ double intGet(int x, int y, int z);
+ double get(double x, double y, double z);
+
+private:
+ double *m_data;
+ double m_start_x, m_start_y, m_start_z;
+ double m_samplelength_x, m_samplelength_y, m_samplelength_z;
+ int m_size_x, m_size_y, m_size_z;
+};
+
+#endif
+
diff --git a/pnoise.py b/pnoise.py
new file mode 100644
index 000000000..fcab5ac15
--- /dev/null
+++ b/pnoise.py
@@ -0,0 +1,102 @@
+#
+# A python perlin noise implementation, from
+# http://www.fundza.com/c4serious/noise/perlin/perlin.html
+#
+# This is used for testing how to create maps with a python script.
+#
+
+import math
+p = (
+151,160,137,91,90,15,131,13,201,95,96,53,194,233,7,225,140,36,103,
+30,69,142,8,99,37,240,21,10,23,190,6,148,247,120,234,75,0,26,197,
+62,94,252,219,203,117,35,11,32,57,177,33,88,237,149,56,87,174,20,
+125,136,171,168,68,175,74,165,71,134,139,48,27,166,77,146,158,231,
+83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,102,
+143,54,65,25,63,161,1,216,80,73,209,76,132,187,208,89,18,169,200,
+196,135,130,116,188,159,86,164,100,109,198,173,186,3,64,52,217,226,
+250,124,123,5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,
+58,17,182,189,28,42,223,183,170,213,119,248,152,2,44,154,163,70,
+221,153,101,155,167,43,172,9,129,22,39,253,19,98,108,110,79,113,
+224,232,178,185,112,104,218,246,97,228,251,34,242,193,238,210,144,
+12,191,179,162,241,81,51,145,235,249,14,239,107,49,192,214,31,181,
+199,106,157,184,84,204,176,115,121,50,45,127,4,150,254,138,236,
+205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180,
+151,160,137,91,90,15,131,13,201,95,96,53,194,233,7,225,140,36,103,
+30,69,142,8,99,37,240,21,10,23,190,6,148,247,120,234,75,0,26,197,
+62,94,252,219,203,117,35,11,32,57,177,33,88,237,149,56,87,174,20,
+125,136,171,168,68,175,74,165,71,134,139,48,27,166,77,146,158,231,
+83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,102,
+143,54,65,25,63,161,1,216,80,73,209,76,132,187,208,89,18,169,200,
+196,135,130,116,188,159,86,164,100,109,198,173,186,3,64,52,217,226,
+250,124,123,5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,
+58,17,182,189,28,42,223,183,170,213,119,248,152,2,44,154,163,70,
+221,153,101,155,167,43,172,9,129,22,39,253,19,98,108,110,79,113,
+224,232,178,185,112,104,218,246,97,228,251,34,242,193,238,210,144,
+12,191,179,162,241,81,51,145,235,249,14,239,107,49,192,214,31,181,
+199,106,157,184,84,204,176,115,121,50,45,127,4,150,254,138,236,
+205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180)
+
+def lerp(t, a, b):
+ return a + t * (b - a)
+
+def fade(t):
+ return t * t * t * (t * (t * 6 - 15) + 10)
+
+def grad(hash, x, y, z):
+ h = hash & 15
+ if h < 8:
+ u = x
+ else:
+ u = y
+ if h < 4:
+ v = y
+ elif h == 12 or h == 14:
+ v = x
+ else:
+ v = z
+ if h & 1 != 0:
+ u = -u
+ if h & 2 != 0:
+ v = -v
+ return u + v
+
+def pnoise(x, y, z):
+ global p
+ X = int(math.floor(x)) & 255
+ Y = int(math.floor(y)) & 255
+ Z = int(math.floor(z)) & 255
+ x -= math.floor(x)
+ y -= math.floor(y)
+ z -= math.floor(z)
+
+ u = fade(x)
+ v = fade(y)
+ w = fade(z)
+
+ A = p[X] + Y
+ AA = p[A] + Z
+ AB = p[A + 1] + Z
+ B = p[X + 1] + Y
+ BA = p[B] + Z
+ BB = p[B + 1] + Z
+
+ pAA = p[AA]
+ pAB = p[AB]
+ pBA = p[BA]
+ pBB = p[BB]
+ pAA1 = p[AA + 1]
+ pBA1 = p[BA + 1]
+ pAB1 = p[AB + 1]
+ pBB1 = p[BB + 1]
+
+ gradAA = grad(pAA, x, y, z)
+ gradBA = grad(pBA, x-1, y, z)
+ gradAB = grad(pAB, x, y-1, z)
+ gradBB = grad(pBB, x-1, y-1, z)
+ gradAA1 = grad(pAA1,x, y, z-1)
+ gradBA1 = grad(pBA1,x-1, y, z-1)
+ gradAB1 = grad(pAB1,x, y-1, z-1)
+ gradBB1 = grad(pBB1,x-1, y-1, z-1)
+ return lerp(w,
+ lerp(v, lerp(u, gradAA, gradBA), lerp(u, gradAB, gradBB)),
+ lerp(v, lerp(u, gradAA1,gradBA1),lerp(u, gradAB1,gradBB1)))
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index d38ea3f0b..f912f68cd 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -50,6 +50,9 @@ configure_file(
)
set(common_SRCS
+ auth.cpp
+ collision.cpp
+ nodemetadata.cpp
serverobject.cpp
noise.cpp
mineral.cpp
@@ -67,6 +70,7 @@ set(common_SRCS
connection.cpp
environment.cpp
server.cpp
+ servercommand.cpp
socket.cpp
mapblock.cpp
mapsector.cpp
@@ -74,20 +78,26 @@ set(common_SRCS
player.cpp
utility.cpp
test.cpp
+ sha1.cpp
+ base64.cpp
)
# Client sources
set(minetest_SRCS
${common_SRCS}
+ keycode.cpp
+ clouds.cpp
clientobject.cpp
+ guiFurnaceMenu.cpp
guiMainMenu.cpp
guiMessageMenu.cpp
guiTextInputMenu.cpp
guiInventoryMenu.cpp
guiPauseMenu.cpp
- irrlichtwrapper.cpp
+ guiPasswordChange.cpp
client.cpp
tile.cpp
+ game.cpp
main.cpp
)
@@ -104,7 +114,6 @@ include_directories(
${CMAKE_BUILD_TYPE}
${PNG_INCLUDE_DIR}
"${PROJECT_SOURCE_DIR}/jthread"
- "${PROJECT_SOURCE_DIR}/lua/src"
)
set(EXECUTABLE_OUTPUT_PATH ../bin)
@@ -123,9 +132,7 @@ if(BUILD_CLIENT)
${PLATFORM_LIBS}
${CLIENT_PLATFORM_LIBS}
jthread
- lua
)
- #${CMAKE_CURRENT_SOURCE_DIR}/lua/build/liblua
endif(BUILD_CLIENT)
if(BUILD_SERVER)
@@ -135,7 +142,6 @@ if(BUILD_SERVER)
${ZLIB_LIBRARIES}
${PLATFORM_LIBS}
jthread
- lua
)
endif(BUILD_SERVER)
@@ -148,7 +154,8 @@ if(MSVC)
# EHa enables SEH exceptions (used for catching segfaults)
set(CMAKE_CXX_FLAGS_RELEASE "/EHa /MD /O2 /Ob2 /Oi /Ot /Oy /GL /FD /MT /GS- /arch:SSE /fp:fast /D NDEBUG /D _HAS_ITERATOR_DEBUGGING=0 /TP")
- set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/LTCG /NODEFAULTLIB:\"libcmtd.lib\" /NODEFAULTLIB:\"libcmt.lib\"")
+ #set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/LTCG /NODEFAULTLIB:\"libcmtd.lib\" /NODEFAULTLIB:\"libcmt.lib\"")
+ set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/LTCG")
# Debug build doesn't catch exceptions by itself
# Add some optimizations because otherwise it's VERY slow
@@ -168,6 +175,8 @@ else()
set(WARNING_FLAGS "")
endif()
+ set(WARNING_FLAGS "${WARNING_FLAGS} -Wno-unused-but-set-variable")
+
set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG ${WARNING_FLAGS} -O3 -ffast-math -Wall -fomit-frame-pointer -pipe -funroll-loops")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O1 -Wall")
@@ -192,8 +201,6 @@ endif()
# Example configuration file
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/../minetest.conf.example" DESTINATION ${EXAMPLE_CONF_DIR})
-install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../data/scripts" DESTINATION ${DATADIR})
-
if(BUILD_CLIENT)
install(TARGETS minetest DESTINATION ${BINDIR})
@@ -218,6 +225,5 @@ endif(BUILD_SERVER)
# Subdirectories
add_subdirectory(jthread)
-add_subdirectory(lua)
#end
diff --git a/src/activeobject.h b/src/activeobject.h
index 103d90d12..382f7f798 100644
--- a/src/activeobject.h
+++ b/src/activeobject.h
@@ -38,7 +38,9 @@ struct ActiveObjectMessage
#define ACTIVEOBJECT_TYPE_INVALID 0
#define ACTIVEOBJECT_TYPE_TEST 1
-#define ACTIVEOBJECT_TYPE_LUA 2
+#define ACTIVEOBJECT_TYPE_ITEM 2
+#define ACTIVEOBJECT_TYPE_RAT 3
+#define ACTIVEOBJECT_TYPE_OERKKI1 4
/*
Parent class for ServerActiveObject and ClientActiveObject
diff --git a/src/auth.cpp b/src/auth.cpp
new file mode 100644
index 000000000..5d61243c6
--- /dev/null
+++ b/src/auth.cpp
@@ -0,0 +1,264 @@
+/*
+Minetest-c55
+Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "auth.h"
+#include <fstream>
+#include <jmutexautolock.h>
+//#include "main.h" // for g_settings
+#include <sstream>
+#include "strfnd.h"
+#include "debug.h"
+
+// Convert a privileges value into a human-readable string,
+// with each component separated by a comma.
+std::string privsToString(u64 privs)
+{
+ std::ostringstream os(std::ios_base::binary);
+ if(privs & PRIV_BUILD)
+ os<<"build,";
+ if(privs & PRIV_TELEPORT)
+ os<<"teleport,";
+ if(privs & PRIV_SETTIME)
+ os<<"settime,";
+ if(privs & PRIV_PRIVS)
+ os<<"privs,";
+ if(privs & PRIV_SHOUT)
+ os<<"shout,";
+ if(os.tellp())
+ {
+ // Drop the trailing comma. (Why on earth can't
+ // you truncate a C++ stream anyway???)
+ std::string tmp = os.str();
+ return tmp.substr(0, tmp.length() -1);
+ }
+ return os.str();
+}
+
+// Converts a comma-seperated list of privilege values into a
+// privileges value. The reverse of privsToString(). Returns
+// PRIV_INVALID if there is anything wrong with the input.
+u64 stringToPrivs(std::string str)
+{
+ u64 privs=0;
+ Strfnd f(str);
+ while(f.atend() == false)
+ {
+ std::string s = trim(f.next(","));
+ if(s == "build")
+ privs |= PRIV_BUILD;
+ else if(s == "teleport")
+ privs |= PRIV_TELEPORT;
+ else if(s == "settime")
+ privs |= PRIV_SETTIME;
+ else if(s == "privs")
+ privs |= PRIV_PRIVS;
+ else if(s == "shout")
+ privs |= PRIV_SHOUT;
+ else
+ return PRIV_INVALID;
+ }
+ return privs;
+}
+
+AuthManager::AuthManager(const std::string &authfilepath):
+ m_authfilepath(authfilepath),
+ m_modified(false)
+{
+ m_mutex.Init();
+
+ try{
+ load();
+ }
+ catch(SerializationError &e)
+ {
+ dstream<<"WARNING: AuthManager: creating "
+ <<m_authfilepath<<std::endl;
+ }
+}
+
+AuthManager::~AuthManager()
+{
+ save();
+}
+
+void AuthManager::load()
+{
+ JMutexAutoLock lock(m_mutex);
+
+ dstream<<"AuthManager: loading from "<<m_authfilepath<<std::endl;
+ std::ifstream is(m_authfilepath.c_str(), std::ios::binary);
+ if(is.good() == false)
+ {
+ dstream<<"AuthManager: failed loading from "<<m_authfilepath<<std::endl;
+ throw SerializationError("AuthManager::load(): Couldn't open file");
+ }
+
+ for(;;)
+ {
+ if(is.eof() || is.good() == false)
+ break;
+
+ // Read a line
+ std::string line;
+ std::getline(is, line, '\n');
+
+ std::istringstream iss(line);
+
+ // Read name
+ std::string name;
+ std::getline(iss, name, ':');
+
+ // Read password
+ std::string pwd;
+ std::getline(iss, pwd, ':');
+
+ // Read privileges
+ std::string stringprivs;
+ std::getline(iss, stringprivs, ':');
+ u64 privs = stringToPrivs(stringprivs);
+
+ // Store it
+ AuthData ad;
+ ad.pwd = pwd;
+ ad.privs = privs;
+ m_authdata[name] = ad;
+ }
+
+ m_modified = false;
+}
+
+void AuthManager::save()
+{
+ JMutexAutoLock lock(m_mutex);
+
+ dstream<<"AuthManager: saving to "<<m_authfilepath<<std::endl;
+ std::ofstream os(m_authfilepath.c_str(), std::ios::binary);
+ if(os.good() == false)
+ {
+ dstream<<"AuthManager: failed saving to "<<m_authfilepath<<std::endl;
+ throw SerializationError("AuthManager::save(): Couldn't open file");
+ }
+
+ for(core::map<std::string, AuthData>::Iterator
+ i = m_authdata.getIterator();
+ i.atEnd()==false; i++)
+ {
+ std::string name = i.getNode()->getKey();
+ if(name == "")
+ continue;
+ AuthData ad = i.getNode()->getValue();
+ os<<name<<":"<<ad.pwd<<":"<<privsToString(ad.privs)<<"\n";
+ }
+
+ m_modified = false;
+}
+
+bool AuthManager::exists(const std::string &username)
+{
+ JMutexAutoLock lock(m_mutex);
+
+ core::map<std::string, AuthData>::Node *n;
+ n = m_authdata.find(username);
+ if(n == NULL)
+ return false;
+ return true;
+}
+
+void AuthManager::set(const std::string &username, AuthData ad)
+{
+ JMutexAutoLock lock(m_mutex);
+
+ m_authdata[username] = ad;
+
+ m_modified = true;
+}
+
+void AuthManager::add(const std::string &username)
+{
+ JMutexAutoLock lock(m_mutex);
+
+ m_authdata[username] = AuthData();
+
+ m_modified = true;
+}
+
+std::string AuthManager::getPassword(const std::string &username)
+{
+ JMutexAutoLock lock(m_mutex);
+
+ core::map<std::string, AuthData>::Node *n;
+ n = m_authdata.find(username);
+ if(n == NULL)
+ throw AuthNotFoundException("");
+
+ return n->getValue().pwd;
+}
+
+void AuthManager::setPassword(const std::string &username,
+ const std::string &password)
+{
+ JMutexAutoLock lock(m_mutex);
+
+ core::map<std::string, AuthData>::Node *n;
+ n = m_authdata.find(username);
+ if(n == NULL)
+ throw AuthNotFoundException("");
+
+ AuthData ad = n->getValue();
+ ad.pwd = password;
+ n->setValue(ad);
+
+ m_modified = true;
+}
+
+u64 AuthManager::getPrivs(const std::string &username)
+{
+ JMutexAutoLock lock(m_mutex);
+
+ core::map<std::string, AuthData>::Node *n;
+ n = m_authdata.find(username);
+ if(n == NULL)
+ throw AuthNotFoundException("");
+
+ return n->getValue().privs;
+}
+
+void AuthManager::setPrivs(const std::string &username, u64 privs)
+{
+ JMutexAutoLock lock(m_mutex);
+
+ core::map<std::string, AuthData>::Node *n;
+ n = m_authdata.find(username);
+ if(n == NULL)
+ throw AuthNotFoundException("");
+
+ AuthData ad = n->getValue();
+ ad.privs = privs;
+ n->setValue(ad);
+
+ m_modified = true;
+}
+
+bool AuthManager::isModified()
+{
+ JMutexAutoLock lock(m_mutex);
+ return m_modified;
+}
+
+
diff --git a/src/auth.h b/src/auth.h
new file mode 100644
index 000000000..62dced2a3
--- /dev/null
+++ b/src/auth.h
@@ -0,0 +1,101 @@
+/*
+Minetest-c55
+Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#ifndef AUTH_HEADER
+#define AUTH_HEADER
+
+#include <string>
+#include <jthread.h>
+#include <jmutex.h>
+#include "common_irrlicht.h"
+#include "exceptions.h"
+
+// Player privileges. These form a bitmask stored in the privs field
+// of the player, and define things they're allowed to do. See also
+// the static methods Player::privsToString and stringToPrivs that
+// convert these to human-readable form.
+const u64 PRIV_BUILD = 1; // Can build - i.e. modify the world
+const u64 PRIV_TELEPORT = 2; // Can teleport
+const u64 PRIV_SETTIME = 4; // Can set the time
+const u64 PRIV_PRIVS = 8; // Can grant and revoke privileges
+const u64 PRIV_SERVER = 16; // Can manage the server (e.g. shutodwn
+ // ,settings)
+const u64 PRIV_SHOUT = 32; // Can broadcast chat messages to all
+ // players
+
+// Default privileges - these can be overriden for new players using the
+// config option "default_privs" - however, this value still applies for
+// players that existed before the privileges system was added.
+const u64 PRIV_DEFAULT = PRIV_BUILD|PRIV_SHOUT;
+const u64 PRIV_ALL = 0x7FFFFFFFFFFFFFFFULL;
+const u64 PRIV_INVALID = 0x8000000000000000ULL;
+
+// Convert a privileges value into a human-readable string,
+// with each component separated by a comma.
+std::string privsToString(u64 privs);
+
+// Converts a comma-seperated list of privilege values into a
+// privileges value. The reverse of privsToString(). Returns
+// PRIV_INVALID if there is anything wrong with the input.
+u64 stringToPrivs(std::string str);
+
+struct AuthData
+{
+ std::string pwd;
+ u64 privs;
+
+ AuthData():
+ privs(PRIV_DEFAULT)
+ {
+ }
+};
+
+class AuthNotFoundException : public BaseException
+{
+public:
+ AuthNotFoundException(const char *s):
+ BaseException(s)
+ {}
+};
+
+class AuthManager
+{
+public:
+ AuthManager(const std::string &authfilepath);
+ ~AuthManager();
+ void load();
+ void save();
+ bool exists(const std::string &username);
+ void set(const std::string &username, AuthData ad);
+ void add(const std::string &username);
+ std::string getPassword(const std::string &username);
+ void setPassword(const std::string &username,
+ const std::string &password);
+ u64 getPrivs(const std::string &username);
+ void setPrivs(const std::string &username, u64 privs);
+ bool isModified();
+private:
+ JMutex m_mutex;
+ std::string m_authfilepath;
+ core::map<std::string, AuthData> m_authdata;
+ bool m_modified;
+};
+
+#endif
+
diff --git a/src/base64.cpp b/src/base64.cpp
new file mode 100644
index 000000000..0dfba5013
--- /dev/null
+++ b/src/base64.cpp
@@ -0,0 +1,124 @@
+/*
+ base64.cpp and base64.h
+
+ Copyright (C) 2004-2008 René Nyffenegger
+
+ This source code is provided 'as-is', without any express or implied
+ warranty. In no event will the author be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this source code must not be misrepresented; you must not
+ claim that you wrote the original source code. If you use this source code
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original source code.
+
+ 3. This notice may not be removed or altered from any source distribution.
+
+ René Nyffenegger rene.nyffenegger@adp-gmbh.ch
+
+*/
+
+#include "base64.h"
+#include <iostream>
+
+static const std::string base64_chars =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "0123456789+/";
+
+
+static inline bool is_base64(unsigned char c) {
+ return (isalnum(c) || (c == '+') || (c == '/'));
+}
+
+std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
+ std::string ret;
+ int i = 0;
+ int j = 0;
+ unsigned char char_array_3[3];
+ unsigned char char_array_4[4];
+
+ while (in_len--) {
+ char_array_3[i++] = *(bytes_to_encode++);
+ if (i == 3) {
+ char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
+ char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
+ char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
+ char_array_4[3] = char_array_3[2] & 0x3f;
+
+ for(i = 0; (i <4) ; i++)
+ ret += base64_chars[char_array_4[i]];
+ i = 0;
+ }
+ }
+
+ if (i)
+ {
+ for(j = i; j < 3; j++)
+ char_array_3[j] = '\0';
+
+ char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
+ char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
+ char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
+ char_array_4[3] = char_array_3[2] & 0x3f;
+
+ for (j = 0; (j < i + 1); j++)
+ ret += base64_chars[char_array_4[j]];
+
+ // Don't pad it with =
+ /*while((i++ < 3))
+ ret += '=';*/
+
+ }
+
+ return ret;
+
+}
+
+std::string base64_decode(std::string const& encoded_string) {
+ int in_len = encoded_string.size();
+ int i = 0;
+ int j = 0;
+ int in_ = 0;
+ unsigned char char_array_4[4], char_array_3[3];
+ std::string ret;
+
+ while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
+ char_array_4[i++] = encoded_string[in_]; in_++;
+ if (i ==4) {
+ for (i = 0; i <4; i++)
+ char_array_4[i] = base64_chars.find(char_array_4[i]);
+
+ char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
+ char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
+ char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
+
+ for (i = 0; (i < 3); i++)
+ ret += char_array_3[i];
+ i = 0;
+ }
+ }
+
+ if (i) {
+ for (j = i; j <4; j++)
+ char_array_4[j] = 0;
+
+ for (j = 0; j <4; j++)
+ char_array_4[j] = base64_chars.find(char_array_4[j]);
+
+ char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
+ char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
+ char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
+
+ for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
+ }
+
+ return ret;
+}
diff --git a/src/base64.h b/src/base64.h
new file mode 100644
index 000000000..65d5db8b2
--- /dev/null
+++ b/src/base64.h
@@ -0,0 +1,4 @@
+#include <string>
+
+std::string base64_encode(unsigned char const* , unsigned int len);
+std::string base64_decode(std::string const& s);
diff --git a/src/client.cpp b/src/client.cpp
index 3ea666549..e494056f2 100644
--- a/src/client.cpp
+++ b/src/client.cpp
@@ -26,24 +26,38 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <sstream>
#include "porting.h"
-void * ClientUpdateThread::Thread()
+void * MeshUpdateThread::Thread()
{
ThreadStarted();
DSTACK(__FUNCTION_NAME);
BEGIN_DEBUG_EXCEPTION_HANDLER
-
+
while(getRun())
{
- m_client->asyncStep();
+ QueuedMeshUpdate *q = m_queue_in.pop();
+ if(q == NULL)
+ {
+ sleep_ms(50);
+ continue;
+ }
- //m_client->updateSomeExpiredMeshes();
+ scene::SMesh *mesh_new = NULL;
+ mesh_new = makeMapBlockMesh(q->data);
- bool was = m_client->AsyncProcessData();
+ MeshUpdateResult r;
+ r.p = q->p;
+ r.mesh = mesh_new;
+ r.ack_block_to_server = q->ack_block_to_server;
- if(was == false)
- sleep_ms(10);
+ /*dstream<<"MeshUpdateThread: Processed "
+ <<"("<<q->p.X<<","<<q->p.Y<<","<<q->p.Z<<")"
+ <<std::endl;*/
+
+ m_queue_out.push_back(r);
+
+ delete q;
}
END_DEBUG_EXCEPTION_HANDLER
@@ -54,8 +68,9 @@ void * ClientUpdateThread::Thread()
Client::Client(
IrrlichtDevice *device,
const char *playername,
+ std::string password,
MapDrawControl &control):
- m_thread(this),
+ m_mesh_update_thread(),
m_env(
new ClientMap(this, control,
device->getSceneManager()->getRootSceneNode(),
@@ -67,67 +82,64 @@ Client::Client(
camera_position(0,0,0),
camera_direction(0,0,1),
m_server_ser_ver(SER_FMT_VER_INVALID),
- m_step_dtime(0.0),
m_inventory_updated(false),
- m_time_of_day(0)
+ m_time_of_day(0),
+ m_map_seed(0),
+ m_password(password),
+ m_access_denied(false)
{
m_packetcounter_timer = 0.0;
m_delete_unused_sectors_timer = 0.0;
m_connection_reinit_timer = 0.0;
m_avg_rtt_timer = 0.0;
m_playerpos_send_timer = 0.0;
+ m_ignore_damage_timer = 0.0;
- //m_fetchblock_mutex.Init();
- m_incoming_queue_mutex.Init();
- m_env_mutex.Init();
- m_con_mutex.Init();
- m_step_dtime_mutex.Init();
+ //m_env_mutex.Init();
+ //m_con_mutex.Init();
- m_thread.Start();
+ m_mesh_update_thread.Start();
/*
Add local player
*/
{
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
Player *player = new LocalPlayer();
player->updateName(playername);
m_env.addPlayer(player);
+
+ // Initialize player in the inventory context
+ m_inventory_context.current_player = player;
}
-
- // Add some active objects for testing
- /*{
- ClientActiveObject *obj = new TestCAO(0, v3f(0, 10*BS, 0));
- m_env.addActiveObject(obj);
- }*/
}
Client::~Client()
{
{
- JMutexAutoLock conlock(m_con_mutex);
+ //JMutexAutoLock conlock(m_con_mutex); //bulk comment-out
m_con.Disconnect();
}
- m_thread.setRun(false);
- while(m_thread.IsRunning())
+ m_mesh_update_thread.setRun(false);
+ while(m_mesh_update_thread.IsRunning())
sleep_ms(100);
}
void Client::connect(Address address)
{
DSTACK(__FUNCTION_NAME);
- JMutexAutoLock lock(m_con_mutex);
+ //JMutexAutoLock lock(m_con_mutex); //bulk comment-out
m_con.setTimeoutMs(0);
m_con.Connect(address);
}
bool Client::connectedAndInitialized()
{
- JMutexAutoLock lock(m_con_mutex);
+ //JMutexAutoLock lock(m_con_mutex); //bulk comment-out
if(m_con.Connected() == false)
return false;
@@ -146,6 +158,10 @@ void Client::step(float dtime)
if(dtime > 2.0)
dtime = 2.0;
+ if(m_ignore_damage_timer > dtime)
+ m_ignore_damage_timer -= dtime;
+ else
+ m_ignore_damage_timer = 0.0;
//dstream<<"Client steps "<<dtime<<std::endl;
@@ -158,7 +174,7 @@ void Client::step(float dtime)
{
//TimeTaker timer("m_con_mutex + m_con.RunTimeouts()", m_device);
// 0ms
- JMutexAutoLock lock(m_con_mutex);
+ //JMutexAutoLock lock(m_con_mutex); //bulk comment-out
m_con.RunTimeouts(dtime);
}
@@ -194,7 +210,7 @@ void Client::step(float dtime)
//counter = 180.0;
counter = 60.0;
- JMutexAutoLock lock(m_env_mutex);
+ //JMutexAutoLock lock(m_env_mutex); //bulk comment-out
core::list<v3s16> deleted_blocks;
@@ -223,7 +239,7 @@ void Client::step(float dtime)
*/
// Env is locked so con can be locked.
- JMutexAutoLock lock(m_con_mutex);
+ //JMutexAutoLock lock(m_con_mutex); //bulk comment-out
core::list<v3s16>::Iterator i = deleted_blocks.begin();
core::list<v3s16> sendlist;
@@ -277,7 +293,7 @@ void Client::step(float dtime)
{
counter = 2.0;
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
Player *myplayer = m_env.getLocalPlayer();
assert(myplayer != NULL);
@@ -286,11 +302,20 @@ void Client::step(float dtime)
// [0] u16 TOSERVER_INIT
// [2] u8 SER_FMT_VER_HIGHEST
// [3] u8[20] player_name
- SharedBuffer<u8> data(2+1+PLAYERNAME_SIZE);
+ // [23] u8[28] password
+ SharedBuffer<u8> data(2+1+PLAYERNAME_SIZE+PASSWORD_SIZE);
writeU16(&data[0], TOSERVER_INIT);
writeU8(&data[2], SER_FMT_VER_HIGHEST);
+
memset((char*)&data[3], 0, PLAYERNAME_SIZE);
snprintf((char*)&data[3], PLAYERNAME_SIZE, "%s", myplayer->getName());
+
+ /*dstream<<"Client: password hash is \""<<m_password<<"\""
+ <<std::endl;*/
+
+ memset((char*)&data[23], 0, PASSWORD_SIZE);
+ snprintf((char*)&data[23], PASSWORD_SIZE, "%s", m_password.c_str());
+
// Send as unreliable
Send(0, data, false);
}
@@ -303,9 +328,12 @@ void Client::step(float dtime)
Do stuff if connected
*/
+ /*
+ Handle environment
+ */
{
// 0ms
- JMutexAutoLock lock(m_env_mutex);
+ //JMutexAutoLock lock(m_env_mutex); //bulk comment-out
// Control local player (0ms)
LocalPlayer *player = m_env.getLocalPlayer();
@@ -333,20 +361,53 @@ void Client::step(float dtime)
{
}
}
- }
+ /*
+ Get events
+ */
+ for(;;)
+ {
+ ClientEnvEvent event = m_env.getClientEvent();
+ if(event.type == CEE_NONE)
+ {
+ break;
+ }
+ else if(event.type == CEE_PLAYER_DAMAGE)
+ {
+ if(m_ignore_damage_timer <= 0)
+ {
+ u8 damage = event.player_damage.amount;
+ sendDamage(damage);
+
+ // Add to ClientEvent queue
+ ClientEvent event;
+ event.type = CE_PLAYER_DAMAGE;
+ event.player_damage.amount = damage;
+ m_client_event_queue.push_back(event);
+ }
+ }
+ }
+ }
+
+ /*
+ Print some info
+ */
{
float &counter = m_avg_rtt_timer;
counter += dtime;
if(counter >= 10)
{
counter = 0.0;
- JMutexAutoLock lock(m_con_mutex);
+ //JMutexAutoLock lock(m_con_mutex); //bulk comment-out
// connectedAndInitialized() is true, peer exists.
con::Peer *peer = m_con.GetPeer(PEER_ID_SERVER);
dstream<<DTIME<<"Client: avg_rtt="<<peer->avg_rtt<<std::endl;
}
}
+
+ /*
+ Send player position to server
+ */
{
float &counter = m_playerpos_send_timer;
counter += dtime;
@@ -357,28 +418,51 @@ void Client::step(float dtime)
}
}
- /*{
- JMutexAutoLock lock(m_step_dtime_mutex);
- m_step_dtime += dtime;
- }*/
-}
-
-float Client::asyncStep()
-{
- DSTACK(__FUNCTION_NAME);
- //dstream<<"Client::asyncStep()"<<std::endl;
-
- /*float dtime;
+ /*
+ Replace updated meshes
+ */
{
- JMutexAutoLock lock1(m_step_dtime_mutex);
- if(m_step_dtime < 0.001)
- return 0.0;
- dtime = m_step_dtime;
- m_step_dtime = 0.0;
- }
+ //JMutexAutoLock lock(m_env_mutex); //bulk comment-out
- return dtime;*/
- return 0.0;
+ //TimeTaker timer("** Processing mesh update result queue");
+ // 0ms
+
+ /*dstream<<"Mesh update result queue size is "
+ <<m_mesh_update_thread.m_queue_out.size()
+ <<std::endl;*/
+
+ while(m_mesh_update_thread.m_queue_out.size() > 0)
+ {
+ MeshUpdateResult r = m_mesh_update_thread.m_queue_out.pop_front();
+ MapBlock *block = m_env.getMap().getBlockNoCreateNoEx(r.p);
+ if(block)
+ {
+ block->replaceMesh(r.mesh);
+ }
+ if(r.ack_block_to_server)
+ {
+ /*dstream<<"Client: ACK block ("<<r.p.X<<","<<r.p.Y
+ <<","<<r.p.Z<<")"<<std::endl;*/
+ /*
+ Acknowledge block
+ */
+ /*
+ [0] u16 command
+ [2] u8 count
+ [3] v3s16 pos_0
+ [3+6] v3s16 pos_1
+ ...
+ */
+ u32 replysize = 2+1+6;
+ SharedBuffer<u8> reply(replysize);
+ writeU16(&reply[0], TOSERVER_GOTBLOCKS);
+ reply[2] = 1;
+ writeV3S16(&reply[3], r.p);
+ // Send as reliable
+ m_con.Send(PEER_ID_SERVER, 1, reply, true);
+ }
+ }
+ }
}
// Virtual methods from con::PeerHandler
@@ -418,13 +502,13 @@ void Client::ReceiveAll()
void Client::Receive()
{
DSTACK(__FUNCTION_NAME);
- u32 data_maxsize = 10000;
+ u32 data_maxsize = 200000;
Buffer<u8> data(data_maxsize);
u16 sender_peer_id;
u32 datasize;
{
//TimeTaker t1("con mutex and receive", m_device);
- JMutexAutoLock lock(m_con_mutex);
+ //JMutexAutoLock lock(m_con_mutex); //bulk comment-out
datasize = m_con.Receive(sender_peer_id, *data, data_maxsize);
}
//TimeTaker t1("ProcessData", m_device);
@@ -464,7 +548,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
con::Peer *peer;
{
- JMutexAutoLock lock(m_con_mutex);
+ //JMutexAutoLock lock(m_con_mutex); //bulk comment-out
// All data is coming from the server
// PeerNotFoundException is handled by caller.
peer = m_con.GetPeer(PEER_ID_SERVER);
@@ -474,8 +558,6 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
//dstream<<"Client received command="<<(int)command<<std::endl;
- // Execute fast commands straight away
-
if(command == TOCLIENT_INIT)
{
if(datasize < 3)
@@ -503,7 +585,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
v3f playerpos_f = intToFloat(playerpos_s16, BS) - v3f(0, BS/2, 0);
{ //envlock
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
// Set player position
Player *player = m_env.getLocalPlayer();
@@ -511,6 +593,13 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
player->setPosition(playerpos_f);
}
+ if(datasize >= 2+1+6+8)
+ {
+ // Get map seed
+ m_map_seed = readU64(&data[2+1+6]);
+ dstream<<"Client: received map seed: "<<m_map_seed<<std::endl;
+ }
+
// Reply to server
u32 replysize = 2;
SharedBuffer<u8> reply(replysize);
@@ -520,7 +609,23 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
return;
}
-
+
+ if(command == TOCLIENT_ACCESS_DENIED)
+ {
+ // The server didn't like our password. Note, this needs
+ // to be processed even if the serialisation format has
+ // not been agreed yet, the same as TOCLIENT_INIT.
+ m_access_denied = true;
+ m_access_denied_reason = L"Unknown";
+ if(datasize >= 4)
+ {
+ std::string datastring((char*)&data[2], datasize-2);
+ std::istringstream is(datastring, std::ios_base::binary);
+ m_access_denied_reason = deSerializeWideString(is);
+ }
+ return;
+ }
+
if(ser_version == SER_FMT_VER_INVALID)
{
dout_client<<DTIME<<"WARNING: Client: Server serialization"
@@ -542,7 +647,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
p.Y = readS16(&data[4]);
p.Z = readS16(&data[6]);
- //TimeTaker t1("TOCLIENT_REMOVENODE", g_device);
+ //TimeTaker t1("TOCLIENT_REMOVENODE");
// This will clear the cracking animation after digging
((ClientMap&)m_env.getMap()).clearTempMod(p);
@@ -559,20 +664,145 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
p.Y = readS16(&data[4]);
p.Z = readS16(&data[6]);
- //TimeTaker t1("TOCLIENT_ADDNODE", g_device);
+ //TimeTaker t1("TOCLIENT_ADDNODE");
MapNode n;
n.deSerialize(&data[8], ser_version);
addNode(p, n);
}
+ else if(command == TOCLIENT_BLOCKDATA)
+ {
+ // Ignore too small packet
+ if(datasize < 8)
+ return;
+
+ v3s16 p;
+ p.X = readS16(&data[2]);
+ p.Y = readS16(&data[4]);
+ p.Z = readS16(&data[6]);
+
+ /*dout_client<<DTIME<<"Client: Thread: BLOCKDATA for ("
+ <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
+ /*dstream<<DTIME<<"Client: Thread: BLOCKDATA for ("
+ <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
+
+ std::string datastring((char*)&data[8], datasize-8);
+ std::istringstream istr(datastring, std::ios_base::binary);
+
+ MapSector *sector;
+ MapBlock *block;
+
+ { //envlock
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
+
+ v2s16 p2d(p.X, p.Z);
+ sector = m_env.getMap().emergeSector(p2d);
+
+ v2s16 sp = sector->getPos();
+ if(sp != p2d)
+ {
+ dstream<<"ERROR: Got sector with getPos()="
+ <<"("<<sp.X<<","<<sp.Y<<"), tried to get"
+ <<"("<<p2d.X<<","<<p2d.Y<<")"<<std::endl;
+ }
+
+ assert(sp == p2d);
+ //assert(sector->getPos() == p2d);
+
+ //TimeTaker timer("MapBlock deSerialize");
+ // 0ms
+
+ try{
+ block = sector->getBlockNoCreate(p.Y);
+ /*
+ Update an existing block
+ */
+ //dstream<<"Updating"<<std::endl;
+ block->deSerialize(istr, ser_version);
+ //block->setChangedFlag();
+ }
+ catch(InvalidPositionException &e)
+ {
+ /*
+ Create a new block
+ */
+ //dstream<<"Creating new"<<std::endl;
+ block = new MapBlock(&m_env.getMap(), p);
+ block->deSerialize(istr, ser_version);
+ sector->insertBlock(block);
+ //block->setChangedFlag();
+
+ //DEBUG
+ /*NodeMod mod;
+ mod.type = NODEMOD_CHANGECONTENT;
+ mod.param = CONTENT_MESE;
+ block->setTempMod(v3s16(8,10,8), mod);
+ block->setTempMod(v3s16(8,9,8), mod);
+ block->setTempMod(v3s16(8,8,8), mod);
+ block->setTempMod(v3s16(8,7,8), mod);
+ block->setTempMod(v3s16(8,6,8), mod);*/
+#if 0
+ /*
+ Add some coulds
+ Well, this is a dumb way to do it, they should just
+ be drawn as separate objects. But the looks of them
+ can be tested this way.
+ */
+ if(p.Y == 3)
+ {
+ NodeMod mod;
+ mod.type = NODEMOD_CHANGECONTENT;
+ mod.param = CONTENT_CLOUD;
+ v3s16 p2;
+ p2.Y = 8;
+ for(p2.X=3; p2.X<=13; p2.X++)
+ for(p2.Z=3; p2.Z<=13; p2.Z++)
+ {
+ block->setTempMod(p2, mod);
+ }
+ }
+#endif
+ }
+ } //envlock
+
+#if 0
+ /*
+ Acknowledge block
+ */
+ /*
+ [0] u16 command
+ [2] u8 count
+ [3] v3s16 pos_0
+ [3+6] v3s16 pos_1
+ ...
+ */
+ u32 replysize = 2+1+6;
+ SharedBuffer<u8> reply(replysize);
+ writeU16(&reply[0], TOSERVER_GOTBLOCKS);
+ reply[2] = 1;
+ writeV3S16(&reply[3], p);
+ // Send as reliable
+ m_con.Send(PEER_ID_SERVER, 1, reply, true);
+#endif
+
+ /*
+ Update Mesh of this block and blocks at x-, y- and z-.
+ Environment should not be locked as it interlocks with the
+ main thread, from which is will want to retrieve textures.
+ */
+
+ //m_env.getClientMap().updateMeshes(block->getPos(), getDayNightRatio());
+
+ addUpdateMeshTaskWithEdge(p, true);
+ }
else if(command == TOCLIENT_PLAYERPOS)
{
dstream<<"WARNING: Received deprecated TOCLIENT_PLAYERPOS"
<<std::endl;
/*u16 our_peer_id;
{
- JMutexAutoLock lock(m_con_mutex);
+ //JMutexAutoLock lock(m_con_mutex); //bulk comment-out
our_peer_id = m_con.GetPeerID();
}
// Cancel if we don't have a peer id
@@ -584,7 +814,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
}*/
{ //envlock
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
u32 player_size = 2+12+12+4+4;
@@ -638,7 +868,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
{
u16 our_peer_id;
{
- JMutexAutoLock lock(m_con_mutex);
+ //JMutexAutoLock lock(m_con_mutex); //bulk comment-out
our_peer_id = m_con.GetPeerID();
}
// Cancel if we don't have a peer id
@@ -652,7 +882,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
//dstream<<DTIME<<"Client: Server reports players:"<<std::endl;
{ //envlock
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
u32 item_size = 2+PLAYERNAME_SIZE;
u32 player_count = (datasize-2) / item_size;
@@ -751,7 +981,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
//dstream<<"Client received TOCLIENT_SECTORMETA"<<std::endl;
{ //envlock
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
std::string datastring((char*)&data[2], datasize-2);
std::istringstream is(datastring, std::ios_base::binary);
@@ -785,7 +1015,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
{ //envlock
//TimeTaker t2("mutex locking", m_device);
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
//t2.stop();
//TimeTaker t3("istringstream init", m_device);
@@ -820,7 +1050,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
{ //envlock
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
u8 buf[12];
@@ -976,31 +1206,23 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
if(datasize < 4)
return;
- u16 time = readU16(&data[2]);
- time = time % 24000;
- m_time_of_day.set(time);
- //dstream<<"Client: time="<<time<<std::endl;
+ u16 time_of_day = readU16(&data[2]);
+ time_of_day = time_of_day % 24000;
+ //dstream<<"Client: time_of_day="<<time_of_day<<std::endl;
/*
- Day/night
-
time_of_day:
0 = midnight
12000 = midday
*/
{
- u32 dr = time_to_daynight_ratio(m_time_of_day.get());
+ m_env.setTimeOfDay(time_of_day);
- dstream<<"Client: time_of_day="<<m_time_of_day.get()
+ u32 dr = m_env.getDayNightRatio();
+
+ dstream<<"Client: time_of_day="<<time_of_day
<<", dr="<<dr
<<std::endl;
-
- if(dr != m_env.getDayNightRatio())
- {
- dout_client<<DTIME<<"Client: changing day-night ratio"<<std::endl;
- m_env.setDayNightRatio(dr);
- m_env.expireMeshes(true);
- }
}
}
@@ -1033,316 +1255,163 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
}
else if(command == TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD)
{
- /*
- u16 command
- u16 count of removed objects
- for all removed objects {
- u16 id
- }
- u16 count of added objects
- for all added objects {
- u16 id
- u8 type
- u16 initialization data length
- string initialization data
- }
- */
+ //if(g_settings.getBool("enable_experimental"))
+ {
+ /*
+ u16 command
+ u16 count of removed objects
+ for all removed objects {
+ u16 id
+ }
+ u16 count of added objects
+ for all added objects {
+ u16 id
+ u8 type
+ u16 initialization data length
+ string initialization data
+ }
+ */
- char buf[6];
- // Get all data except the command number
- std::string datastring((char*)&data[2], datasize-2);
- // Throw them in an istringstream
- std::istringstream is(datastring, std::ios_base::binary);
+ char buf[6];
+ // Get all data except the command number
+ std::string datastring((char*)&data[2], datasize-2);
+ // Throw them in an istringstream
+ std::istringstream is(datastring, std::ios_base::binary);
- // Read stuff
-
- // Read removed objects
- is.read(buf, 2);
- u16 removed_count = readU16((u8*)buf);
- for(u16 i=0; i<removed_count; i++)
- {
+ // Read stuff
+
+ // Read removed objects
is.read(buf, 2);
- u16 id = readU16((u8*)buf);
- // Remove it
+ u16 removed_count = readU16((u8*)buf);
+ for(u16 i=0; i<removed_count; i++)
{
- JMutexAutoLock envlock(m_env_mutex);
- m_env.removeActiveObject(id);
+ is.read(buf, 2);
+ u16 id = readU16((u8*)buf);
+ // Remove it
+ {
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
+ m_env.removeActiveObject(id);
+ }
}
- }
-
- // Read added objects
- is.read(buf, 2);
- u16 added_count = readU16((u8*)buf);
- for(u16 i=0; i<added_count; i++)
- {
+
+ // Read added objects
is.read(buf, 2);
- u16 id = readU16((u8*)buf);
- is.read(buf, 1);
- u8 type = readU8((u8*)buf);
- std::string data = deSerializeLongString(is);
- // Add it
+ u16 added_count = readU16((u8*)buf);
+ for(u16 i=0; i<added_count; i++)
{
- JMutexAutoLock envlock(m_env_mutex);
- m_env.addActiveObject(id, type, data);
+ is.read(buf, 2);
+ u16 id = readU16((u8*)buf);
+ is.read(buf, 1);
+ u8 type = readU8((u8*)buf);
+ std::string data = deSerializeLongString(is);
+ // Add it
+ {
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
+ m_env.addActiveObject(id, type, data);
+ }
}
}
}
else if(command == TOCLIENT_ACTIVE_OBJECT_MESSAGES)
{
- /*
- u16 command
- for all objects
- {
- u16 id
- u16 message length
- string message
- }
- */
- char buf[6];
- // Get all data except the command number
- std::string datastring((char*)&data[2], datasize-2);
- // Throw them in an istringstream
- std::istringstream is(datastring, std::ios_base::binary);
-
- while(is.eof() == false)
+ //if(g_settings.getBool("enable_experimental"))
{
- // Read stuff
- is.read(buf, 2);
- u16 id = readU16((u8*)buf);
- if(is.eof())
- break;
- is.read(buf, 2);
- u16 message_size = readU16((u8*)buf);
- std::string message;
- message.reserve(message_size);
- for(u16 i=0; i<message_size; i++)
- {
- is.read(buf, 1);
- message.append(buf, 1);
- }
- // Pass on to the environment
+ /*
+ u16 command
+ for all objects
+ {
+ u16 id
+ u16 message length
+ string message
+ }
+ */
+ char buf[6];
+ // Get all data except the command number
+ std::string datastring((char*)&data[2], datasize-2);
+ // Throw them in an istringstream
+ std::istringstream is(datastring, std::ios_base::binary);
+
+ while(is.eof() == false)
{
- JMutexAutoLock envlock(m_env_mutex);
- m_env.processActiveObjectMessage(id, message);
+ // Read stuff
+ is.read(buf, 2);
+ u16 id = readU16((u8*)buf);
+ if(is.eof())
+ break;
+ is.read(buf, 2);
+ u16 message_size = readU16((u8*)buf);
+ std::string message;
+ message.reserve(message_size);
+ for(u16 i=0; i<message_size; i++)
+ {
+ is.read(buf, 1);
+ message.append(buf, 1);
+ }
+ // Pass on to the environment
+ {
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
+ m_env.processActiveObjectMessage(id, message);
+ }
}
}
}
- // Default to queueing it (for slow commands)
- else
- {
- JMutexAutoLock lock(m_incoming_queue_mutex);
-
- IncomingPacket packet(data, datasize);
- m_incoming_queue.push_back(packet);
- }
-}
-
-/*
- Returns true if there was something in queue
-*/
-bool Client::AsyncProcessPacket()
-{
- DSTACK(__FUNCTION_NAME);
-
- try //for catching con::PeerNotFoundException
+ else if(command == TOCLIENT_HP)
{
-
- con::Peer *peer;
- {
- JMutexAutoLock lock(m_con_mutex);
- // All data is coming from the server
- peer = m_con.GetPeer(PEER_ID_SERVER);
- }
-
- u8 ser_version = m_server_ser_ver;
-
- IncomingPacket packet = getPacket();
- u8 *data = packet.m_data;
- u32 datasize = packet.m_datalen;
-
- // An empty packet means queue is empty
- if(data == NULL){
- return false;
+ std::string datastring((char*)&data[2], datasize-2);
+ std::istringstream is(datastring, std::ios_base::binary);
+ Player *player = m_env.getLocalPlayer();
+ assert(player != NULL);
+ u8 hp = readU8(is);
+ player->hp = hp;
}
-
- if(datasize < 2)
- return true;
-
- ToClientCommand command = (ToClientCommand)readU16(&data[0]);
-
- if(command == TOCLIENT_BLOCKDATA)
+ else if(command == TOCLIENT_MOVE_PLAYER)
{
- // Ignore too small packet
- if(datasize < 8)
- return true;
- /*if(datasize < 8 + MapBlock::serializedLength(ser_version))
- goto getdata;*/
-
- v3s16 p;
- p.X = readS16(&data[2]);
- p.Y = readS16(&data[4]);
- p.Z = readS16(&data[6]);
-
- /*dout_client<<DTIME<<"Client: Thread: BLOCKDATA for ("
- <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
-
- /*dstream<<DTIME<<"Client: Thread: BLOCKDATA for ("
- <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
-
- std::string datastring((char*)&data[8], datasize-8);
- std::istringstream istr(datastring, std::ios_base::binary);
-
- MapSector *sector;
- MapBlock *block;
-
- { //envlock
- JMutexAutoLock envlock(m_env_mutex);
-
- v2s16 p2d(p.X, p.Z);
- sector = m_env.getMap().emergeSector(p2d);
-
- v2s16 sp = sector->getPos();
- if(sp != p2d)
- {
- dstream<<"ERROR: Got sector with getPos()="
- <<"("<<sp.X<<","<<sp.Y<<"), tried to get"
- <<"("<<p2d.X<<","<<p2d.Y<<")"<<std::endl;
- }
-
- assert(sp == p2d);
- //assert(sector->getPos() == p2d);
-
- try{
- block = sector->getBlockNoCreate(p.Y);
- /*
- Update an existing block
- */
- //dstream<<"Updating"<<std::endl;
- block->deSerialize(istr, ser_version);
- //block->setChangedFlag();
- }
- catch(InvalidPositionException &e)
- {
- /*
- Create a new block
- */
- //dstream<<"Creating new"<<std::endl;
- block = new MapBlock(&m_env.getMap(), p);
- block->deSerialize(istr, ser_version);
- sector->insertBlock(block);
- //block->setChangedFlag();
-
- //DEBUG
- /*NodeMod mod;
- mod.type = NODEMOD_CHANGECONTENT;
- mod.param = CONTENT_MESE;
- block->setTempMod(v3s16(8,10,8), mod);
- block->setTempMod(v3s16(8,9,8), mod);
- block->setTempMod(v3s16(8,8,8), mod);
- block->setTempMod(v3s16(8,7,8), mod);
- block->setTempMod(v3s16(8,6,8), mod);*/
-
- /*
- Add some coulds
- Well, this is a dumb way to do it, they should just
- be drawn as separate objects.
- */
- /*if(p.Y == 3)
- {
- NodeMod mod;
- mod.type = NODEMOD_CHANGECONTENT;
- mod.param = CONTENT_CLOUD;
- v3s16 p2;
- p2.Y = 8;
- for(p2.X=3; p2.X<=13; p2.X++)
- for(p2.Z=3; p2.Z<=13; p2.Z++)
- {
- block->setTempMod(p2, mod);
- }
- }*/
- }
- } //envlock
-
- /*
- Acknowledge block.
- */
- /*
- [0] u16 command
- [2] u8 count
- [3] v3s16 pos_0
- [3+6] v3s16 pos_1
- ...
- */
- u32 replysize = 2+1+6;
- SharedBuffer<u8> reply(replysize);
- writeU16(&reply[0], TOSERVER_GOTBLOCKS);
- reply[2] = 1;
- writeV3S16(&reply[3], p);
- // Send as reliable
- m_con.Send(PEER_ID_SERVER, 1, reply, true);
+ std::string datastring((char*)&data[2], datasize-2);
+ std::istringstream is(datastring, std::ios_base::binary);
+ Player *player = m_env.getLocalPlayer();
+ assert(player != NULL);
+ v3f pos = readV3F1000(is);
+ f32 pitch = readF1000(is);
+ f32 yaw = readF1000(is);
+ player->setPosition(pos);
+ /*player->setPitch(pitch);
+ player->setYaw(yaw);*/
+
+ dstream<<"Client got TOCLIENT_MOVE_PLAYER"
+ <<" pos=("<<pos.X<<","<<pos.Y<<","<<pos.Z<<")"
+ <<" pitch="<<pitch
+ <<" yaw="<<yaw
+ <<std::endl;
/*
- Update Mesh of this block and blocks at x-, y- and z-.
- Environment should not be locked as it interlocks with the
- main thread, from which is will want to retrieve textures.
+ Add to ClientEvent queue.
+ This has to be sent to the main program because otherwise
+ it would just force the pitch and yaw values to whatever
+ the camera points to.
*/
-
- m_env.getClientMap().updateMeshes(block->getPos(), getDayNightRatio());
+ ClientEvent event;
+ event.type = CE_PLAYER_FORCE_MOVE;
+ event.player_force_move.pitch = pitch;
+ event.player_force_move.yaw = yaw;
+ m_client_event_queue.push_back(event);
+
+ // Ignore damage for a few seconds, so that the player doesn't
+ // get damage from falling on ground
+ m_ignore_damage_timer = 3.0;
}
else
{
dout_client<<DTIME<<"WARNING: Client: Ignoring unknown command "
<<command<<std::endl;
}
-
- return true;
-
- } //try
- catch(con::PeerNotFoundException &e)
- {
- /*dout_client<<DTIME<<"Client::AsyncProcessData(): Cancelling: The server"
- " connection doesn't exist (a timeout or not yet connected?)"<<std::endl;*/
- return false;
- }
-}
-
-bool Client::AsyncProcessData()
-{
- for(;;)
- {
- bool r = AsyncProcessPacket();
- if(r == false)
- break;
- }
- return false;
}
void Client::Send(u16 channelnum, SharedBuffer<u8> data, bool reliable)
{
- JMutexAutoLock lock(m_con_mutex);
+ //JMutexAutoLock lock(m_con_mutex); //bulk comment-out
m_con.Send(PEER_ID_SERVER, channelnum, data, reliable);
}
-IncomingPacket Client::getPacket()
-{
- JMutexAutoLock lock(m_incoming_queue_mutex);
-
- core::list<IncomingPacket>::Iterator i;
- // Refer to first one
- i = m_incoming_queue.begin();
-
- // If queue is empty, return empty packet
- if(i == m_incoming_queue.end()){
- IncomingPacket packet;
- return packet;
- }
-
- // Pop out first packet and return it
- IncomingPacket packet = *i;
- m_incoming_queue.erase(i);
- return packet;
-}
-
void Client::groundAction(u8 action, v3s16 nodepos_undersurface,
v3s16 nodepos_oversurface, u16 item)
{
@@ -1402,6 +1471,31 @@ void Client::clickObject(u8 button, v3s16 blockpos, s16 id, u16 item)
Send(0, data, true);
}
+void Client::clickActiveObject(u8 button, u16 id, u16 item)
+{
+ if(connectedAndInitialized() == false){
+ dout_client<<DTIME<<"Client::clickActiveObject() "
+ "cancelled (not connected)"
+ <<std::endl;
+ return;
+ }
+
+ /*
+ length: 7
+ [0] u16 command
+ [2] u8 button (0=left, 1=right)
+ [3] u16 id
+ [5] u16 item
+ */
+ u8 datasize = 2 + 1 + 6 + 2 + 2;
+ SharedBuffer<u8> data(datasize);
+ writeU16(&data[0], TOSERVER_CLICK_ACTIVEOBJECT);
+ writeU8(&data[2], button);
+ writeU16(&data[3], id);
+ writeU16(&data[5], item);
+ Send(0, data, true);
+}
+
void Client::sendSignText(v3s16 blockpos, s16 id, std::string text)
{
/*
@@ -1441,6 +1535,40 @@ void Client::sendSignText(v3s16 blockpos, s16 id, std::string text)
Send(0, data, true);
}
+void Client::sendSignNodeText(v3s16 p, std::string text)
+{
+ /*
+ u16 command
+ v3s16 p
+ u16 textlen
+ textdata
+ */
+ std::ostringstream os(std::ios_base::binary);
+ u8 buf[12];
+
+ // Write command
+ writeU16(buf, TOSERVER_SIGNNODETEXT);
+ os.write((char*)buf, 2);
+
+ // Write p
+ writeV3S16(buf, p);
+ os.write((char*)buf, 6);
+
+ u16 textlen = text.size();
+ // Write text length
+ writeS16(buf, textlen);
+ os.write((char*)buf, 2);
+
+ // Write text
+ os.write((char*)text.c_str(), textlen);
+
+ // Make data buffer
+ std::string s = os.str();
+ SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+ // Send as reliable
+ Send(0, data, true);
+}
+
void Client::sendInventoryAction(InventoryAction *a)
{
std::ostringstream os(std::ios_base::binary);
@@ -1487,9 +1615,61 @@ void Client::sendChatMessage(const std::wstring &message)
Send(0, data, true);
}
+void Client::sendChangePassword(const std::wstring oldpassword,
+ const std::wstring newpassword)
+{
+ Player *player = m_env.getLocalPlayer();
+ if(player == NULL)
+ return;
+
+ std::string playername = player->getName();
+ std::string oldpwd = translatePassword(playername, oldpassword);
+ std::string newpwd = translatePassword(playername, newpassword);
+
+ std::ostringstream os(std::ios_base::binary);
+ u8 buf[2+PASSWORD_SIZE*2];
+ /*
+ [0] u16 TOSERVER_PASSWORD
+ [2] u8[28] old password
+ [30] u8[28] new password
+ */
+
+ writeU16(buf, TOSERVER_PASSWORD);
+ for(u32 i=0;i<PASSWORD_SIZE-1;i++)
+ {
+ buf[2+i] = i<oldpwd.length()?oldpwd[i]:0;
+ buf[30+i] = i<newpwd.length()?newpwd[i]:0;
+ }
+ buf[2+PASSWORD_SIZE-1] = 0;
+ buf[30+PASSWORD_SIZE-1] = 0;
+ os.write((char*)buf, 2+PASSWORD_SIZE*2);
+
+ // Make data buffer
+ std::string s = os.str();
+ SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+ // Send as reliable
+ Send(0, data, true);
+}
+
+
+void Client::sendDamage(u8 damage)
+{
+ DSTACK(__FUNCTION_NAME);
+ std::ostringstream os(std::ios_base::binary);
+
+ writeU16(os, TOSERVER_DAMAGE);
+ writeU8(os, damage);
+
+ // Make data buffer
+ std::string s = os.str();
+ SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+ // Send as reliable
+ Send(0, data, true);
+}
+
void Client::sendPlayerPos()
{
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
Player *myplayer = m_env.getLocalPlayer();
if(myplayer == NULL)
@@ -1497,7 +1677,7 @@ void Client::sendPlayerPos()
u16 our_peer_id;
{
- JMutexAutoLock lock(m_con_mutex);
+ //JMutexAutoLock lock(m_con_mutex); //bulk comment-out
our_peer_id = m_con.GetPeerID();
}
@@ -1536,7 +1716,7 @@ void Client::sendPlayerPos()
void Client::removeNode(v3s16 p)
{
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
core::map<v3s16, MapBlock*> modified_blocks;
@@ -1554,13 +1734,14 @@ void Client::removeNode(v3s16 p)
i.atEnd() == false; i++)
{
v3s16 p = i.getNode()->getKey();
- m_env.getClientMap().updateMeshes(p, m_env.getDayNightRatio());
+ //m_env.getClientMap().updateMeshes(p, m_env.getDayNightRatio());
+ addUpdateMeshTaskWithEdge(p);
}
}
void Client::addNode(v3s16 p, MapNode n)
{
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
TimeTaker timer1("Client::addNode()");
@@ -1574,14 +1755,15 @@ void Client::addNode(v3s16 p, MapNode n)
catch(InvalidPositionException &e)
{}
- TimeTaker timer2("Client::addNode(): updateMeshes");
+ //TimeTaker timer2("Client::addNode(): updateMeshes");
for(core::map<v3s16, MapBlock * >::Iterator
i = modified_blocks.getIterator();
i.atEnd() == false; i++)
{
v3s16 p = i.getNode()->getKey();
- m_env.getClientMap().updateMeshes(p, m_env.getDayNightRatio());
+ //m_env.getClientMap().updateMeshes(p, m_env.getDayNightRatio());
+ addUpdateMeshTaskWithEdge(p);
}
}
@@ -1594,13 +1776,18 @@ void Client::updateCamera(v3f pos, v3f dir)
MapNode Client::getNode(v3s16 p)
{
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
return m_env.getMap().getNode(p);
}
+NodeMetadata* Client::getNodeMetadata(v3s16 p)
+{
+ return m_env.getMap().getNodeMetadata(p);
+}
+
v3f Client::getPlayerPosition()
{
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
LocalPlayer *player = m_env.getLocalPlayer();
assert(player != NULL);
return player->getPosition();
@@ -1608,7 +1795,7 @@ v3f Client::getPlayerPosition()
void Client::setPlayerControl(PlayerControl &control)
{
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
LocalPlayer *player = m_env.getLocalPlayer();
assert(player != NULL);
player->control = control;
@@ -1619,7 +1806,7 @@ void Client::setPlayerControl(PlayerControl &control)
bool Client::getLocalInventoryUpdated()
{
// m_inventory_updated is behind envlock
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
bool updated = m_inventory_updated;
m_inventory_updated = false;
return updated;
@@ -1628,19 +1815,57 @@ bool Client::getLocalInventoryUpdated()
// Copies the inventory of the local player to parameter
void Client::getLocalInventory(Inventory &dst)
{
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
Player *player = m_env.getLocalPlayer();
assert(player != NULL);
dst = player->inventory;
}
+InventoryContext *Client::getInventoryContext()
+{
+ return &m_inventory_context;
+}
+
+Inventory* Client::getInventory(InventoryContext *c, std::string id)
+{
+ if(id == "current_player")
+ {
+ assert(c->current_player);
+ return &(c->current_player->inventory);
+ }
+
+ Strfnd fn(id);
+ std::string id0 = fn.next(":");
+
+ if(id0 == "nodemeta")
+ {
+ v3s16 p;
+ p.X = stoi(fn.next(","));
+ p.Y = stoi(fn.next(","));
+ p.Z = stoi(fn.next(","));
+ NodeMetadata* meta = getNodeMetadata(p);
+ if(meta)
+ return meta->getInventory();
+ dstream<<"nodemeta at ("<<p.X<<","<<p.Y<<","<<p.Z<<"): "
+ <<"no metadata found"<<std::endl;
+ return NULL;
+ }
+
+ dstream<<__FUNCTION_NAME<<": unknown id "<<id<<std::endl;
+ return NULL;
+}
+void Client::inventoryAction(InventoryAction *a)
+{
+ sendInventoryAction(a);
+}
+
MapBlockObject * Client::getSelectedObject(
f32 max_d,
v3f from_pos_f_on_map,
core::line3d<f32> shootline_on_map
)
{
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
core::array<DistanceSortedObject> objects;
@@ -1699,15 +1924,57 @@ MapBlockObject * Client::getSelectedObject(
return NULL;
}
+ClientActiveObject * Client::getSelectedActiveObject(
+ f32 max_d,
+ v3f from_pos_f_on_map,
+ core::line3d<f32> shootline_on_map
+ )
+{
+ core::array<DistanceSortedActiveObject> objects;
+
+ m_env.getActiveObjects(from_pos_f_on_map, max_d, objects);
+
+ //dstream<<"Collected "<<objects.size()<<" nearby objects"<<std::endl;
+
+ // Sort them.
+ // After this, the closest object is the first in the array.
+ objects.sort();
+
+ for(u32 i=0; i<objects.size(); i++)
+ {
+ ClientActiveObject *obj = objects[i].obj;
+
+ core::aabbox3d<f32> *selection_box = obj->getSelectionBox();
+ if(selection_box == NULL)
+ continue;
+
+ v3f pos = obj->getPosition();
+
+ core::aabbox3d<f32> offsetted_box(
+ selection_box->MinEdge + pos,
+ selection_box->MaxEdge + pos
+ );
+
+ if(offsetted_box.intersectsWithLine(shootline_on_map))
+ {
+ //dstream<<"Returning selected object"<<std::endl;
+ return obj;
+ }
+ }
+
+ //dstream<<"No object selected; returning NULL."<<std::endl;
+ return NULL;
+}
+
void Client::printDebugInfo(std::ostream &os)
{
//JMutexAutoLock lock1(m_fetchblock_mutex);
- JMutexAutoLock lock2(m_incoming_queue_mutex);
+ /*JMutexAutoLock lock2(m_incoming_queue_mutex);
os<<"m_incoming_queue.getSize()="<<m_incoming_queue.getSize()
//<<", m_fetchblock_history.size()="<<m_fetchblock_history.size()
//<<", m_opt_not_found_history.size()="<<m_opt_not_found_history.size()
- <<std::endl;
+ <<std::endl;*/
}
/*s32 Client::getDayNightIndex()
@@ -1718,63 +1985,106 @@ void Client::printDebugInfo(std::ostream &os)
u32 Client::getDayNightRatio()
{
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
return m_env.getDayNightRatio();
}
-/*void Client::updateSomeExpiredMeshes()
+u16 Client::getHP()
{
- TimeTaker timer("updateSomeExpiredMeshes()", g_device);
-
- Player *player;
- {
- JMutexAutoLock envlock(m_env_mutex);
- player = m_env.getLocalPlayer();
- }
+ Player *player = m_env.getLocalPlayer();
+ assert(player != NULL);
+ return player->hp;
+}
- u32 daynight_ratio = getDayNightRatio();
+void Client::addUpdateMeshTask(v3s16 p, bool ack_to_server)
+{
+ /*dstream<<"Client::addUpdateMeshTask(): "
+ <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
+ <<std::endl;*/
- v3f playerpos = player->getPosition();
- v3f playerspeed = player->getSpeed();
+ MapBlock *b = m_env.getMap().getBlockNoCreateNoEx(p);
+ if(b == NULL)
+ return;
- v3s16 center_nodepos = floatToInt(playerpos, BS);
- v3s16 center = getNodeBlockPos(center_nodepos);
+ /*
+ Create a task to update the mesh of the block
+ */
+
+ MeshMakeData *data = new MeshMakeData;
+
+ {
+ //TimeTaker timer("data fill");
+ // 0ms
+ data->fill(getDayNightRatio(), b);
+ }
- u32 counter = 0;
+ // Debug wait
+ //while(m_mesh_update_thread.m_queue_in.size() > 0) sleep_ms(10);
+
+ // Add task to queue
+ m_mesh_update_thread.m_queue_in.addBlock(p, data, ack_to_server);
- s16 d_max = 5;
+ /*dstream<<"Mesh update input queue size is "
+ <<m_mesh_update_thread.m_queue_in.size()
+ <<std::endl;*/
- for(s16 d = 0; d <= d_max; d++)
+#if 0
+ // Temporary test: make mesh directly in here
{
- core::list<v3s16> list;
- getFacePositions(list, d);
-
- core::list<v3s16>::Iterator li;
- for(li=list.begin(); li!=list.end(); li++)
- {
- v3s16 p = *li + center;
- MapBlock *block = NULL;
- try
- {
- //JMutexAutoLock envlock(m_env_mutex);
- block = m_env.getMap().getBlockNoCreate(p);
- }
- catch(InvalidPositionException &e)
- {
- }
+ //TimeTaker timer("make mesh");
+ // 10ms
+ scene::SMesh *mesh_new = NULL;
+ mesh_new = makeMapBlockMesh(data);
+ b->replaceMesh(mesh_new);
+ delete data;
+ }
+#endif
- if(block == NULL)
- continue;
+ b->setMeshExpired(false);
+}
- if(block->getMeshExpired() == false)
- continue;
+void Client::addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server)
+{
+ /*{
+ v3s16 p = blockpos;
+ dstream<<"Client::addUpdateMeshTaskWithEdge(): "
+ <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
+ <<std::endl;
+ }*/
- block->updateMesh(daynight_ratio);
+ try{
+ v3s16 p = blockpos + v3s16(0,0,0);
+ //MapBlock *b = m_env.getMap().getBlockNoCreate(p);
+ addUpdateMeshTask(p, ack_to_server);
+ }
+ catch(InvalidPositionException &e){}
+ // Leading edge
+ try{
+ v3s16 p = blockpos + v3s16(-1,0,0);
+ addUpdateMeshTask(p);
+ }
+ catch(InvalidPositionException &e){}
+ try{
+ v3s16 p = blockpos + v3s16(0,-1,0);
+ addUpdateMeshTask(p);
+ }
+ catch(InvalidPositionException &e){}
+ try{
+ v3s16 p = blockpos + v3s16(0,0,-1);
+ addUpdateMeshTask(p);
+ }
+ catch(InvalidPositionException &e){}
+}
- counter++;
- if(counter >= 5)
- return;
- }
+ClientEvent Client::getClientEvent()
+{
+ if(m_client_event_queue.size() == 0)
+ {
+ ClientEvent event;
+ event.type = CE_NONE;
+ return event;
}
-}*/
+ return m_client_event_queue.pop_front();
+}
+
diff --git a/src/client.h b/src/client.h
index fb1e70722..1bfbe6296 100644
--- a/src/client.h
+++ b/src/client.h
@@ -37,80 +37,177 @@ public:
{}
};
-class Client;
-
-class ClientUpdateThread : public SimpleThread
+struct QueuedMeshUpdate
{
- Client *m_client;
+ v3s16 p;
+ MeshMakeData *data;
+ bool ack_block_to_server;
+
+ QueuedMeshUpdate():
+ p(-1337,-1337,-1337),
+ data(NULL),
+ ack_block_to_server(false)
+ {
+ }
+
+ ~QueuedMeshUpdate()
+ {
+ if(data)
+ delete data;
+ }
+};
+/*
+ A thread-safe queue of mesh update tasks
+*/
+class MeshUpdateQueue
+{
public:
-
- ClientUpdateThread(Client *client):
- SimpleThread(),
- m_client(client)
+ MeshUpdateQueue()
{
+ m_mutex.Init();
}
- void * Thread();
-};
+ ~MeshUpdateQueue()
+ {
+ JMutexAutoLock lock(m_mutex);
-struct IncomingPacket
-{
- IncomingPacket()
+ core::list<QueuedMeshUpdate*>::Iterator i;
+ for(i=m_queue.begin(); i!=m_queue.end(); i++)
+ {
+ QueuedMeshUpdate *q = *i;
+ delete q;
+ }
+ }
+
+ /*
+ peer_id=0 adds with nobody to send to
+ */
+ void addBlock(v3s16 p, MeshMakeData *data, bool ack_block_to_server)
{
- m_data = NULL;
- m_datalen = 0;
- m_refcount = NULL;
+ DSTACK(__FUNCTION_NAME);
+
+ assert(data);
+
+ JMutexAutoLock lock(m_mutex);
+
+ /*
+ Find if block is already in queue.
+ If it is, update the data and quit.
+ */
+ core::list<QueuedMeshUpdate*>::Iterator i;
+ for(i=m_queue.begin(); i!=m_queue.end(); i++)
+ {
+ QueuedMeshUpdate *q = *i;
+ if(q->p == p)
+ {
+ if(q->data)
+ delete q->data;
+ q->data = data;
+ if(ack_block_to_server)
+ q->ack_block_to_server = true;
+ return;
+ }
+ }
+
+ /*
+ Add the block
+ */
+ QueuedMeshUpdate *q = new QueuedMeshUpdate;
+ q->p = p;
+ q->data = data;
+ q->ack_block_to_server = ack_block_to_server;
+ m_queue.push_back(q);
}
- IncomingPacket(const IncomingPacket &a)
+
+ // Returned pointer must be deleted
+ // Returns NULL if queue is empty
+ QueuedMeshUpdate * pop()
{
- m_data = a.m_data;
- m_datalen = a.m_datalen;
- m_refcount = a.m_refcount;
- if(m_refcount != NULL)
- (*m_refcount)++;
+ JMutexAutoLock lock(m_mutex);
+
+ core::list<QueuedMeshUpdate*>::Iterator i = m_queue.begin();
+ if(i == m_queue.end())
+ return NULL;
+ QueuedMeshUpdate *q = *i;
+ m_queue.erase(i);
+ return q;
}
- IncomingPacket(u8 *data, u32 datalen)
+
+ u32 size()
{
- m_data = new u8[datalen];
- memcpy(m_data, data, datalen);
- m_datalen = datalen;
- m_refcount = new s32(1);
+ JMutexAutoLock lock(m_mutex);
+ return m_queue.size();
}
- ~IncomingPacket()
+
+private:
+ core::list<QueuedMeshUpdate*> m_queue;
+ JMutex m_mutex;
+};
+
+struct MeshUpdateResult
+{
+ v3s16 p;
+ scene::SMesh *mesh;
+ bool ack_block_to_server;
+
+ MeshUpdateResult():
+ p(-1338,-1338,-1338),
+ mesh(NULL),
+ ack_block_to_server(false)
{
- if(m_refcount != NULL){
- assert(*m_refcount > 0);
- (*m_refcount)--;
- if(*m_refcount == 0){
- if(m_data != NULL)
- delete[] m_data;
- delete m_refcount;
- }
- }
}
- /*IncomingPacket & operator=(IncomingPacket a)
+};
+
+class MeshUpdateThread : public SimpleThread
+{
+public:
+
+ MeshUpdateThread()
{
- m_data = a.m_data;
- m_datalen = a.m_datalen;
- m_refcount = a.m_refcount;
- (*m_refcount)++;
- return *this;
- }*/
- u8 *m_data;
- u32 m_datalen;
- s32 *m_refcount;
+ }
+
+ void * Thread();
+
+ MeshUpdateQueue m_queue_in;
+
+ MutexedQueue<MeshUpdateResult> m_queue_out;
+};
+
+enum ClientEventType
+{
+ CE_NONE,
+ CE_PLAYER_DAMAGE,
+ CE_PLAYER_FORCE_MOVE
+};
+
+struct ClientEvent
+{
+ ClientEventType type;
+ union{
+ struct{
+ } none;
+ struct{
+ u8 amount;
+ } player_damage;
+ struct{
+ f32 pitch;
+ f32 yaw;
+ } player_force_move;
+ };
};
-class Client : public con::PeerHandler
+class Client : public con::PeerHandler, public InventoryManager
{
public:
/*
- NOTE: Every public method should be thread-safe
+ NOTE: Nothing is thread-safe here.
*/
+
Client(
IrrlichtDevice *device,
const char *playername,
+ std::string password,
MapDrawControl &control
);
@@ -138,7 +235,7 @@ public:
// Called from updater thread
// Returns dtime
- float asyncStep();
+ //float asyncStep();
void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
// Returns true if something was received
@@ -147,15 +244,20 @@ public:
void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
// Pops out a packet from the packet queue
- IncomingPacket getPacket();
+ //IncomingPacket getPacket();
void groundAction(u8 action, v3s16 nodepos_undersurface,
v3s16 nodepos_oversurface, u16 item);
void clickObject(u8 button, v3s16 blockpos, s16 id, u16 item);
+ void clickActiveObject(u8 button, u16 id, u16 item);
void sendSignText(v3s16 blockpos, s16 id, std::string text);
+ void sendSignNodeText(v3s16 p, std::string text);
void sendInventoryAction(InventoryAction *a);
void sendChatMessage(const std::wstring &message);
+ void sendChangePassword(const std::wstring oldpassword,
+ const std::wstring newpassword);
+ void sendDamage(u8 damage);
// locks envlock
void removeNode(v3s16 p);
@@ -166,6 +268,8 @@ public:
// Returns InvalidPositionException if not found
MapNode getNode(v3s16 p);
+ // Wrapper to Map
+ NodeMetadata* getNodeMetadata(v3s16 p);
v3f getPlayerPosition();
@@ -177,6 +281,11 @@ public:
// Copies the inventory of the local player to parameter
void getLocalInventory(Inventory &dst);
+ InventoryContext *getInventoryContext();
+
+ Inventory* getInventory(InventoryContext *c, std::string id);
+ void inventoryAction(InventoryAction *a);
+
// Gets closest object pointed by the shootline
// Returns NULL if not found
MapBlockObject * getSelectedObject(
@@ -185,16 +294,26 @@ public:
core::line3d<f32> shootline_on_map
);
+ // Gets closest object pointed by the shootline
+ // Returns NULL if not found
+ ClientActiveObject * getSelectedActiveObject(
+ f32 max_d,
+ v3f from_pos_f_on_map,
+ core::line3d<f32> shootline_on_map
+ );
+
// Prints a line or two of info
void printDebugInfo(std::ostream &os);
u32 getDayNightRatio();
+ u16 getHP();
+
//void updateSomeExpiredMeshes();
void setTempMod(v3s16 p, NodeMod mod)
{
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
core::map<v3s16, MapBlock*> affected_blocks;
@@ -210,7 +329,7 @@ public:
}
void clearTempMod(v3s16 p)
{
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
core::map<v3s16, MapBlock*> affected_blocks;
@@ -227,7 +346,7 @@ public:
float getAvgRtt()
{
- JMutexAutoLock lock(m_con_mutex);
+ //JMutexAutoLock lock(m_con_mutex); //bulk comment-out
con::Peer *peer = m_con.GetPeerNoEx(PEER_ID_SERVER);
if(peer == NULL)
return 0.0;
@@ -244,7 +363,7 @@ public:
void addChatMessage(const std::wstring &message)
{
- JMutexAutoLock envlock(m_env_mutex);
+ //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
LocalPlayer *player = m_env.getLocalPlayer();
assert(player != NULL);
std::wstring name = narrow_to_wide(player->getName());
@@ -252,6 +371,25 @@ public:
(std::wstring)L"<"+name+L"> "+message);
}
+ u64 getMapSeed(){ return m_map_seed; }
+
+ void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false);
+ // Including blocks at appropriate edges
+ void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=false);
+
+ // Get event from queue. CE_NONE is returned if queue is empty.
+ ClientEvent getClientEvent();
+
+ inline bool accessDenied()
+ {
+ return m_access_denied;
+ }
+
+ inline std::wstring accessDeniedReason()
+ {
+ return m_access_denied_reason;
+ }
+
private:
// Virtual methods from con::PeerHandler
@@ -270,20 +408,13 @@ private:
float m_connection_reinit_timer;
float m_avg_rtt_timer;
float m_playerpos_send_timer;
+ float m_ignore_damage_timer; // Used after server moves player
- ClientUpdateThread m_thread;
+ MeshUpdateThread m_mesh_update_thread;
- // NOTE: If connection and environment are both to be locked,
- // environment shall be locked first.
-
ClientEnvironment m_env;
- JMutex m_env_mutex;
con::Connection m_con;
- JMutex m_con_mutex;
-
- core::list<IncomingPacket> m_incoming_queue;
- JMutex m_incoming_queue_mutex;
IrrlichtDevice *m_device;
@@ -293,9 +424,6 @@ private:
// Server serialization version
u8 m_server_ser_ver;
- float m_step_dtime;
- JMutex m_step_dtime_mutex;
-
// This is behind m_env_mutex.
bool m_inventory_updated;
@@ -304,13 +432,24 @@ private:
PacketCounter m_packetcounter;
// Received from the server. 0-23999
- MutexedVariable<u32> m_time_of_day;
+ u32 m_time_of_day;
// 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
//s32 m_daynight_i;
//u32 m_daynight_ratio;
Queue<std::wstring> m_chat_queue;
+
+ // The seed returned by the server in TOCLIENT_INIT is stored here
+ u64 m_map_seed;
+
+ std::string m_password;
+ bool m_access_denied;
+ std::wstring m_access_denied_reason;
+
+ InventoryContext m_inventory_context;
+
+ Queue<ClientEvent> m_client_event_queue;
};
#endif // !SERVER
diff --git a/src/clientobject.cpp b/src/clientobject.cpp
index bbe108e15..402535ffc 100644
--- a/src/clientobject.cpp
+++ b/src/clientobject.cpp
@@ -22,6 +22,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "porting.h"
#include "constants.h"
#include "utility.h"
+#include "environment.h"
+#include "tile.h"
+
+/*
+ ClientActiveObject
+*/
+
+core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
ClientActiveObject::ClientActiveObject(u16 id):
ActiveObject(id)
@@ -35,47 +43,55 @@ ClientActiveObject::~ClientActiveObject()
ClientActiveObject* ClientActiveObject::create(u8 type)
{
- if(type == ACTIVEOBJECT_TYPE_INVALID)
+ // Find factory function
+ core::map<u16, Factory>::Node *n;
+ n = m_types.find(type);
+ if(n == NULL)
{
- dstream<<"ClientActiveObject::create(): passed "
- <<"ACTIVEOBJECT_TYPE_INVALID"<<std::endl;
- return NULL;
- }
- else if(type == ACTIVEOBJECT_TYPE_TEST)
- {
- dstream<<"ClientActiveObject::create(): passed "
- <<"ACTIVEOBJECT_TYPE_TEST"<<std::endl;
- return new TestCAO(0);
- }
- else if(type == ACTIVEOBJECT_TYPE_LUA)
- {
- dstream<<"ClientActiveObject::create(): passed "
- <<"ACTIVEOBJECT_TYPE_LUA"<<std::endl;
- return new LuaCAO(0);
- }
- else
- {
- dstream<<"ClientActiveObject::create(): passed "
- <<"unknown type="<<type<<std::endl;
+ // If factory is not found, just return.
+ dstream<<"WARNING: ClientActiveObject: No factory for type="
+ <<type<<std::endl;
return NULL;
}
+
+ Factory f = n->getValue();
+ ClientActiveObject *object = (*f)();
+ return object;
+}
+
+void ClientActiveObject::registerType(u16 type, Factory f)
+{
+ core::map<u16, Factory>::Node *n;
+ n = m_types.find(type);
+ if(n)
+ return;
+ m_types.insert(type, f);
}
/*
TestCAO
*/
-TestCAO::TestCAO(u16 id):
- ClientActiveObject(id),
+// Prototype
+TestCAO proto_TestCAO;
+
+TestCAO::TestCAO():
+ ClientActiveObject(0),
m_node(NULL),
m_position(v3f(0,10*BS,0))
{
+ ClientActiveObject::registerType(getType(), create);
}
TestCAO::~TestCAO()
{
}
+ClientActiveObject* TestCAO::create()
+{
+ return new TestCAO();
+}
+
void TestCAO::addToScene(scene::ISceneManager *smgr)
{
if(m_node != NULL)
@@ -99,7 +115,7 @@ void TestCAO::addToScene(scene::ISceneManager *smgr)
buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
buf->getMaterial().setTexture
- (0, driver->getTexture(porting::getDataPath("rat.png").c_str()));
+ (0, driver->getTexture(getTexturePath("rat.png").c_str()));
buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
@@ -138,7 +154,7 @@ void TestCAO::updateNodePos()
//m_node->setRotation(v3f(0, 45, 0));
}
-void TestCAO::step(float dtime)
+void TestCAO::step(float dtime, ClientEnvironment *env)
{
if(m_node)
{
@@ -167,511 +183,559 @@ void TestCAO::processMessage(const std::string &data)
}
/*
- LuaCAO
+ ItemCAO
*/
-extern "C"{
-#include "lstring.h"
+#include "inventory.h"
+
+// Prototype
+ItemCAO proto_ItemCAO;
+
+ItemCAO::ItemCAO():
+ ClientActiveObject(0),
+ m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
+ m_node(NULL),
+ m_position(v3f(0,10*BS,0))
+{
+ ClientActiveObject::registerType(getType(), create);
}
-/*
- Callbacks in script:
-
- on_step(self, dtime)
- on_process_message(self, data)
- on_initialize(self, data)
- TODO:
- string on_get_info_text(self)
- on_block_removed_near({X=,Y=,Z=}, node)
- on_block_placed_near({X=,Y=,Z=}, node)
-*/
+ItemCAO::~ItemCAO()
+{
+}
-/*
- object_set_position(self, p)
-*/
-static int lf_object_set_position(lua_State *L)
-{
- // 2: position
- assert(lua_istable(L, -1));
- lua_pushstring(L, "X");
- lua_gettable(L, -2);
- lua_Number x = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pushstring(L, "Y");
- lua_gettable(L, -2);
- lua_Number y = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pushstring(L, "Z");
- lua_gettable(L, -2);
- lua_Number z = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pop(L, 1);
- // 1: self
- LuaCAO *self = (LuaCAO*)lua_touserdata(L, -1);
- lua_pop(L, 1);
-
- assert(self);
-
- self->setPosition(v3f(x*BS,y*BS,z*BS));
-
- return 0; // Number of return values
+ClientActiveObject* ItemCAO::create()
+{
+ return new ItemCAO();
}
-/*
- object_set_rotation(self, p)
-*/
-static int lf_object_set_rotation(lua_State *L)
-{
- // 2: position
- assert(lua_istable(L, -1));
- lua_pushstring(L, "X");
- lua_gettable(L, -2);
- lua_Number x = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pushstring(L, "Y");
- lua_gettable(L, -2);
- lua_Number y = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pushstring(L, "Z");
- lua_gettable(L, -2);
- lua_Number z = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pop(L, 1);
- // 1: self
- LuaCAO *self = (LuaCAO*)lua_touserdata(L, -1);
- lua_pop(L, 1);
-
- assert(self);
+void ItemCAO::addToScene(scene::ISceneManager *smgr)
+{
+ if(m_node != NULL)
+ return;
- self->setRotation(v3f(x,y,z));
+ video::IVideoDriver* driver = smgr->getVideoDriver();
- return 0; // Number of return values
+ scene::SMesh *mesh = new scene::SMesh();
+ scene::IMeshBuffer *buf = new scene::SMeshBuffer();
+ video::SColor c(255,255,255,255);
+ video::S3DVertex vertices[4] =
+ {
+ /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
+ video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
+ video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
+ video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
+ video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
+ video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
+ video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
+ video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
+ };
+ u16 indices[] = {0,1,2,2,3,0};
+ buf->append(vertices, 4, indices, 6);
+ // Set material
+ buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
+ buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
+ //buf->getMaterial().setTexture(0, NULL);
+ // Initialize with the stick texture
+ buf->getMaterial().setTexture
+ (0, driver->getTexture(getTexturePath("stick.png").c_str()));
+ buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
+ buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
+ buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
+ // Add to mesh
+ mesh->addMeshBuffer(buf);
+ buf->drop();
+ m_node = smgr->addMeshSceneNode(mesh, NULL);
+ mesh->drop();
+ // Set it to use the materials of the meshbuffers directly.
+ // This is needed for changing the texture in the future
+ m_node->setReadOnlyMaterials(true);
+ updateNodePos();
}
-/*
- object_add_to_mesh(self, image, corners, backface_culling)
- corners is an array like this:
- {{x,y,z},{x,y,z},{x,y,z},{x,y,z}}
-*/
-static int lf_object_add_to_mesh(lua_State *L)
+void ItemCAO::removeFromScene()
{
- // 4: backface_culling
- bool backface_culling = lua_toboolean(L, -1);
- lua_pop(L, 1);
- // 3: corners
- if(lua_istable(L, -1) == false)
- {
- dstream<<"ERROR: object_add_to_mesh(): parameter 3 not a table"
- <<std::endl;
- return 0;
- }
- v3f corners[4];
- // Loop table
- for(int i=0; i<4; i++)
+ if(m_node == NULL)
+ return;
+
+ m_node->remove();
+ m_node = NULL;
+}
+
+void ItemCAO::updateLight(u8 light_at_pos)
+{
+ if(m_node == NULL)
+ return;
+
+ u8 li = decode_light(light_at_pos);
+ video::SColor color(255,li,li,li);
+
+ scene::IMesh *mesh = m_node->getMesh();
+ if(mesh == NULL)
+ return;
+
+ u16 mc = mesh->getMeshBufferCount();
+ for(u16 j=0; j<mc; j++)
{
- // Get child table
- lua_pushinteger(L, i+1);
- lua_gettable(L, -2);
- if(lua_istable(L, -1) == false)
+ scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
+ video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
+ u16 vc = buf->getVertexCount();
+ for(u16 i=0; i<vc; i++)
{
- dstream<<"ERROR: object_add_to_mesh(): parameter 3 not a"
- " table of tables"<<std::endl;
- return 0;
+ vertices[i].Color = color;
}
-
- // Get x, y and z from the child table
-
- lua_pushinteger(L, 1);
- lua_gettable(L, -2);
- corners[i].X = lua_tonumber(L, -1) * BS;
- lua_pop(L, 1);
-
- lua_pushinteger(L, 2);
- lua_gettable(L, -2);
- corners[i].Y = lua_tonumber(L, -1) * BS;
- lua_pop(L, 1);
-
- lua_pushinteger(L, 3);
- lua_gettable(L, -2);
- corners[i].Z = lua_tonumber(L, -1) * BS;
- lua_pop(L, 1);
-
- // Pop child table
- lua_pop(L, 1);
}
- lua_pop(L, 1);
- // 2: image
- const char *image = lua_tostring(L, -1);
- lua_pop(L, 1);
- // 1: self
- LuaCAO *self = (LuaCAO*)lua_touserdata(L, -1);
- lua_pop(L, 1);
-
- assert(self);
-
- self->addToMesh(image, corners, backface_culling);
-
- return 0; // Number of return values
}
-/*
- object_clear_mesh(self)
-*/
-static int lf_object_clear_mesh(lua_State *L)
+v3s16 ItemCAO::getLightPosition()
{
- // 1: self
- LuaCAO *self = (LuaCAO*)lua_touserdata(L, -1);
- lua_pop(L, 1);
-
- assert(self);
+ return floatToInt(m_position, BS);
+}
- self->clearMesh();
+void ItemCAO::updateNodePos()
+{
+ if(m_node == NULL)
+ return;
- return 0;
+ m_node->setPosition(m_position);
}
-LuaCAO::LuaCAO(u16 id):
- ClientActiveObject(id),
- L(NULL),
- m_smgr(NULL),
- m_node(NULL),
- m_mesh(NULL),
- m_position(v3f(0,10*BS,0))
+void ItemCAO::step(float dtime, ClientEnvironment *env)
{
- dstream<<"LuaCAO::LuaCAO(): id="<<id<<std::endl;
- L = lua_open();
- assert(L);
-
- // Load libraries
- luaopen_base(L);
- luaopen_table(L);
- luaopen_string(L);
- luaopen_math(L);
-
- // Disable some stuff
- const char *to_disable[] = {
- "arg",
- "debug",
- "dofile",
- "io",
- "loadfile",
- "os",
- "package",
- "require",
- NULL
- };
- const char **td = to_disable;
- do{
- lua_pushnil(L);
- lua_setglobal(L, *td);
- }while(*(++td));
-
- // Add globals
- //lua_pushlightuserdata(L, this);
- //lua_setglobal(L, "self");
-
- // Register functions
- lua_register(L, "object_set_position", lf_object_set_position);
- lua_register(L, "object_set_rotation", lf_object_set_rotation);
- lua_register(L, "object_add_to_mesh", lf_object_add_to_mesh);
- lua_register(L, "object_clear_mesh", lf_object_clear_mesh);
+ if(m_node)
+ {
+ /*v3f rot = m_node->getRotation();
+ rot.Y += dtime * 120;
+ m_node->setRotation(rot);*/
+ LocalPlayer *player = env->getLocalPlayer();
+ assert(player);
+ v3f rot = m_node->getRotation();
+ rot.Y = 180.0 - (player->getYaw());
+ m_node->setRotation(rot);
+ }
}
-LuaCAO::~LuaCAO()
+void ItemCAO::processMessage(const std::string &data)
{
- lua_close(L);
+ dstream<<"ItemCAO: Got message"<<std::endl;
+ std::istringstream is(data, std::ios::binary);
+ // command
+ u8 cmd = readU8(is);
+ if(cmd == 0)
+ {
+ // pos
+ m_position = readV3F1000(is);
+ updateNodePos();
+ }
}
-void LuaCAO::step(float dtime)
+void ItemCAO::initialize(const std::string &data)
{
- /*
- Call step(self, dtime) from lua
- */
+ dstream<<"ItemCAO: Got init data"<<std::endl;
- const char *funcname = "on_step";
- lua_getglobal(L, funcname);
- if(!lua_isfunction(L,-1))
{
- lua_pop(L,1);
- dstream<<"WARNING: LuaCAO: Function not found: "
- <<funcname<<std::endl;
- return;
+ std::istringstream is(data, std::ios::binary);
+ // version
+ u8 version = readU8(is);
+ // check version
+ if(version != 0)
+ return;
+ // pos
+ m_position = readV3F1000(is);
+ // inventorystring
+ m_inventorystring = deSerializeString(is);
}
- // Parameters:
- // 1: self
- lua_pushlightuserdata(L, this);
- // 2: dtime
- lua_pushnumber(L, dtime);
-
- // Call (2 parameters, 0 result)
- if(lua_pcall(L, 2, 0, 0))
- {
- dstream<<"WARNING: LuaCAO: Error running function "
- <<funcname<<": "
- <<lua_tostring(L,-1)<<std::endl;
- return;
- }
-}
+ updateNodePos();
-void LuaCAO::processMessage(const std::string &data)
-{
/*
- Call process_message(self, data) from lua
+ Update image of node
*/
+
+ if(m_node == NULL)
+ return;
+
+ scene::IMesh *mesh = m_node->getMesh();
+
+ if(mesh == NULL)
+ return;
- const char *funcname = "on_process_message";
- lua_getglobal(L, funcname);
- if(!lua_isfunction(L,-1))
- {
- lua_pop(L,1);
- dstream<<"WARNING: LuaCAO: Function not found: "
- <<funcname<<std::endl;
+ scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
+
+ if(buf == NULL)
return;
+
+ // Create an inventory item to see what is its image
+ std::istringstream is(m_inventorystring, std::ios_base::binary);
+ video::ITexture *texture = NULL;
+ try{
+ InventoryItem *item = NULL;
+ item = InventoryItem::deSerialize(is);
+ dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
+ <<m_inventorystring<<"\" -> item="<<item
+ <<std::endl;
+ if(item)
+ {
+ texture = item->getImage();
+ delete item;
+ }
}
-
- // Parameters:
- // 1: self
- lua_pushlightuserdata(L, this);
- // 2: data
- lua_pushlstring(L, data.c_str(), data.size());
-
- // Call (2 parameters, 0 results)
- if(lua_pcall(L, 2, 1, 0))
+ catch(SerializationError &e)
{
- dstream<<"WARNING: LuaCAO: Error running function "
- <<funcname<<": "
- <<lua_tostring(L,-1)<<std::endl;
- return;
+ dstream<<"WARNING: "<<__FUNCTION_NAME
+ <<": error deSerializing inventorystring \""
+ <<m_inventorystring<<"\""<<std::endl;
}
+
+ // Set meshbuffer texture
+ buf->getMaterial().setTexture(0, texture);
+
}
-void LuaCAO::initialize(const std::string &data)
+/*
+ RatCAO
+*/
+
+#include "inventory.h"
+
+// Prototype
+RatCAO proto_RatCAO;
+
+RatCAO::RatCAO():
+ ClientActiveObject(0),
+ m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS/2.,BS/3.),
+ m_node(NULL),
+ m_position(v3f(0,10*BS,0)),
+ m_yaw(0)
{
- dstream<<"LuaCAO::initialize(): id="<<getId()<<std::endl;
+ ClientActiveObject::registerType(getType(), create);
+}
- std::istringstream is(data, std::ios::binary);
- std::string script = deSerializeLongString(is);
- std::string other = deSerializeLongString(is);
+RatCAO::~RatCAO()
+{
+}
- /*dstream<<"=== script (size="<<script.size()<<")"<<std::endl
- <<script<<std::endl
- <<"==="<<std::endl;*/
- dstream<<"LuaCAO::initialize(): script size="<<script.size()<<std::endl;
-
- /*dstream<<"other.size()="<<other.size()<<std::endl;
- dstream<<"other=\""<<other<<"\""<<std::endl;*/
-
- // Load the script to lua
- loadScript(script);
+ClientActiveObject* RatCAO::create()
+{
+ return new RatCAO();
+}
- /*
- Call initialize(self, data) in the script
- */
-
- const char *funcname = "on_initialize";
- lua_getglobal(L, funcname);
- if(!lua_isfunction(L,-1))
- {
- lua_pop(L,1);
- dstream<<"WARNING: LuaCAO: Function not found: "
- <<funcname<<std::endl;
+void RatCAO::addToScene(scene::ISceneManager *smgr)
+{
+ if(m_node != NULL)
return;
- }
- // Parameters:
- // 1: self
- lua_pushlightuserdata(L, this);
- // 2: data (other)
- lua_pushlstring(L, other.c_str(), other.size());
+ video::IVideoDriver* driver = smgr->getVideoDriver();
- // Call (2 parameters, 0 result)
- if(lua_pcall(L, 2, 0, 0))
+ scene::SMesh *mesh = new scene::SMesh();
+ scene::IMeshBuffer *buf = new scene::SMeshBuffer();
+ video::SColor c(255,255,255,255);
+ video::S3DVertex vertices[4] =
{
- dstream<<"WARNING: LuaCAO: Error running function "
- <<funcname<<": "
- <<lua_tostring(L,-1)<<std::endl;
+ video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
+ video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
+ video::S3DVertex(BS/2,BS/2,0, 0,0,0, c, 1,0),
+ video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c, 0,0),
+ };
+ u16 indices[] = {0,1,2,2,3,0};
+ buf->append(vertices, 4, indices, 6);
+ // Set material
+ buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
+ buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
+ //buf->getMaterial().setTexture(0, NULL);
+ buf->getMaterial().setTexture
+ (0, driver->getTexture(getTexturePath("rat.png").c_str()));
+ buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
+ buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
+ buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
+ // Add to mesh
+ mesh->addMeshBuffer(buf);
+ buf->drop();
+ m_node = smgr->addMeshSceneNode(mesh, NULL);
+ mesh->drop();
+ // Set it to use the materials of the meshbuffers directly.
+ // This is needed for changing the texture in the future
+ m_node->setReadOnlyMaterials(true);
+ updateNodePos();
+}
+
+void RatCAO::removeFromScene()
+{
+ if(m_node == NULL)
return;
- }
+ m_node->remove();
+ m_node = NULL;
}
-void LuaCAO::loadScript(const std::string script)
+void RatCAO::updateLight(u8 light_at_pos)
{
- int ret;
- ret = luaL_loadstring(L, script.c_str());
- if(ret)
- {
- const char *message = lua_tostring(L, -1);
- lua_pop(L, 1);
- dstream<<"LuaCAO::loadScript(): lua_loadstring failed: "
- <<message<<std::endl;
- assert(0);
+ if(m_node == NULL)
return;
- }
- ret = lua_pcall(L, 0, 0, 0);
- if(ret)
- {
- const char *message = lua_tostring(L, -1);
- lua_pop(L, 1);
- dstream<<"LuaCAO::loadScript(): lua_pcall failed: "
- <<message<<std::endl;
- assert(0);
+
+ u8 li = decode_light(light_at_pos);
+ video::SColor color(255,li,li,li);
+
+ scene::IMesh *mesh = m_node->getMesh();
+ if(mesh == NULL)
return;
+
+ u16 mc = mesh->getMeshBufferCount();
+ for(u16 j=0; j<mc; j++)
+ {
+ scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
+ video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
+ u16 vc = buf->getVertexCount();
+ for(u16 i=0; i<vc; i++)
+ {
+ vertices[i].Color = color;
+ }
}
}
-void LuaCAO::addToScene(scene::ISceneManager *smgr)
+v3s16 RatCAO::getLightPosition()
{
- if(m_smgr != NULL)
- {
- dstream<<"WARNING: LuaCAO::addToScene() called more than once"
- <<std::endl;
+ return floatToInt(m_position+v3f(0,BS*0.5,0), BS);
+}
+
+void RatCAO::updateNodePos()
+{
+ if(m_node == NULL)
return;
- }
- if(m_node != NULL)
+ //m_node->setPosition(m_position);
+ m_node->setPosition(pos_translator.vect_show);
+
+ v3f rot = m_node->getRotation();
+ rot.Y = 180.0 - m_yaw;
+ m_node->setRotation(rot);
+}
+
+void RatCAO::step(float dtime, ClientEnvironment *env)
+{
+ pos_translator.translate(dtime);
+ updateNodePos();
+}
+
+void RatCAO::processMessage(const std::string &data)
+{
+ //dstream<<"RatCAO: Got message"<<std::endl;
+ std::istringstream is(data, std::ios::binary);
+ // command
+ u8 cmd = readU8(is);
+ if(cmd == 0)
{
- dstream<<"WARNING: LuaCAO::addToScene(): m_node != NULL"
- <<std::endl;
- return;
+ // pos
+ m_position = readV3F1000(is);
+ pos_translator.update(m_position);
+ // yaw
+ m_yaw = readF1000(is);
+ updateNodePos();
}
+}
+
+void RatCAO::initialize(const std::string &data)
+{
+ //dstream<<"RatCAO: Got init data"<<std::endl;
- m_smgr = smgr;
-
- if(m_mesh == NULL)
- {
- m_mesh = new scene::SMesh();
- m_node = smgr->addMeshSceneNode(m_mesh, NULL);
-
- /*v3f corners[4] = {
- v3f(-BS/2,-BS/4,0),
- v3f(BS/2,-BS/4,0),
- v3f(BS/2,BS/4,0),
- v3f(-BS/2,BS/4,0),
- };
- addToMesh("rat.png", corners, false);*/
- }
- else
{
- dstream<<"WARNING: LuaCAO::addToScene(): m_mesh != NULL"
- <<std::endl;
- return;
+ std::istringstream is(data, std::ios::binary);
+ // version
+ u8 version = readU8(is);
+ // check version
+ if(version != 0)
+ return;
+ // pos
+ m_position = readV3F1000(is);
+ pos_translator.init(m_position);
}
updateNodePos();
}
-void LuaCAO::addToMesh(const char *image, v3f *corners, bool backface_culling)
+/*
+ Oerkki1CAO
+*/
+
+#include "inventory.h"
+
+// Prototype
+Oerkki1CAO proto_Oerkki1CAO;
+
+Oerkki1CAO::Oerkki1CAO():
+ ClientActiveObject(0),
+ m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2.,BS/3.),
+ m_node(NULL),
+ m_position(v3f(0,10*BS,0)),
+ m_yaw(0)
{
- dstream<<"INFO: addToMesh called"<<std::endl;
+ ClientActiveObject::registerType(getType(), create);
+}
- video::IVideoDriver* driver = m_smgr->getVideoDriver();
+Oerkki1CAO::~Oerkki1CAO()
+{
+}
+
+ClientActiveObject* Oerkki1CAO::create()
+{
+ return new Oerkki1CAO();
+}
+
+void Oerkki1CAO::addToScene(scene::ISceneManager *smgr)
+{
+ if(m_node != NULL)
+ return;
+
+ video::IVideoDriver* driver = smgr->getVideoDriver();
- assert(m_mesh);
-
+ scene::SMesh *mesh = new scene::SMesh();
scene::IMeshBuffer *buf = new scene::SMeshBuffer();
video::SColor c(255,255,255,255);
video::S3DVertex vertices[4] =
{
- video::S3DVertex(corners[0], v3f(0,0,0), c, v2f(0,1)),
- video::S3DVertex(corners[1], v3f(0,0,0), c, v2f(1,1)),
- video::S3DVertex(corners[2], v3f(0,0,0), c, v2f(1,0)),
- video::S3DVertex(corners[3], v3f(0,0,0), c, v2f(0,0)),
- /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
- video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
- video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
- video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
+ video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
+ video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
+ video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
+ video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
};
u16 indices[] = {0,1,2,2,3,0};
buf->append(vertices, 4, indices, 6);
// Set material
buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
- buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, backface_culling);
+ buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
+ //buf->getMaterial().setTexture(0, NULL);
buf->getMaterial().setTexture
- (0, driver->getTexture(porting::getDataPath(image).c_str()));
+ (0, driver->getTexture(getTexturePath("oerkki1.png").c_str()));
buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
// Add to mesh
- m_mesh->addMeshBuffer(buf);
+ mesh->addMeshBuffer(buf);
buf->drop();
- // Reset mesh
- if(m_node)
- m_node->setMesh(m_mesh);
+ m_node = smgr->addMeshSceneNode(mesh, NULL);
+ mesh->drop();
+ // Set it to use the materials of the meshbuffers directly.
+ // This is needed for changing the texture in the future
+ m_node->setReadOnlyMaterials(true);
+ updateNodePos();
}
-void LuaCAO::clearMesh()
+void Oerkki1CAO::removeFromScene()
{
- if(m_node)
- {
- m_node->setMesh(NULL);
- }
-
- if(m_mesh)
- {
- m_mesh->drop();
- m_mesh = NULL;
- }
+ if(m_node == NULL)
+ return;
+
+ m_node->remove();
+ m_node = NULL;
}
-void LuaCAO::removeFromScene()
+void Oerkki1CAO::updateLight(u8 light_at_pos)
{
if(m_node == NULL)
return;
- if(m_node)
- {
- m_node->remove();
- m_node = NULL;
- }
- if(m_mesh)
+ if(light_at_pos <= 2)
{
- m_mesh->drop();
- m_mesh = NULL;
+ m_node->setVisible(false);
+ return;
}
- m_smgr = NULL;
-}
+ m_node->setVisible(true);
-void LuaCAO::updateLight(u8 light_at_pos)
-{
+ u8 li = decode_light(light_at_pos);
+ video::SColor color(255,li,li,li);
+
+ scene::IMesh *mesh = m_node->getMesh();
+ if(mesh == NULL)
+ return;
+
+ u16 mc = mesh->getMeshBufferCount();
+ for(u16 j=0; j<mc; j++)
+ {
+ scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
+ video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
+ u16 vc = buf->getVertexCount();
+ for(u16 i=0; i<vc; i++)
+ {
+ vertices[i].Color = color;
+ }
+ }
}
-v3s16 LuaCAO::getLightPosition()
+v3s16 Oerkki1CAO::getLightPosition()
{
- return floatToInt(m_position, BS);
+ return floatToInt(m_position+v3f(0,BS*1.5,0), BS);
}
-void LuaCAO::updateNodePos()
+void Oerkki1CAO::updateNodePos()
{
if(m_node == NULL)
return;
- m_node->setPosition(m_position);
- m_node->setRotation(m_rotation);
+ //m_node->setPosition(m_position);
+ m_node->setPosition(pos_translator.vect_show);
+
+ v3f rot = m_node->getRotation();
+ rot.Y = 180.0 - m_yaw + 90.0;
+ m_node->setRotation(rot);
}
-void LuaCAO::setPosition(v3f pos)
+void Oerkki1CAO::step(float dtime, ClientEnvironment *env)
{
- m_position = pos;
+ pos_translator.translate(dtime);
updateNodePos();
-}
-v3f LuaCAO::getPosition()
-{
- return m_position;
+ LocalPlayer *player = env->getLocalPlayer();
+ assert(player);
+
+ v3f playerpos = player->getPosition();
+ v2f playerpos_2d(playerpos.X,playerpos.Z);
+ v2f objectpos_2d(m_position.X,m_position.Z);
+
+ if(fabs(m_position.Y - playerpos.Y) < 3.0*BS &&
+ objectpos_2d.getDistanceFrom(playerpos_2d) < 1.0*BS)
+ {
+ if(m_attack_interval.step(dtime, 0.5))
+ {
+ env->damageLocalPlayer(2);
+ }
+ }
}
-void LuaCAO::setRotation(v3f rot)
+void Oerkki1CAO::processMessage(const std::string &data)
{
- m_rotation = rot;
- updateNodePos();
+ //dstream<<"Oerkki1CAO: Got message"<<std::endl;
+ std::istringstream is(data, std::ios::binary);
+ // command
+ u8 cmd = readU8(is);
+ if(cmd == 0)
+ {
+ // pos
+ m_position = readV3F1000(is);
+ pos_translator.update(m_position);
+ // yaw
+ m_yaw = readF1000(is);
+ updateNodePos();
+ }
}
-v3f LuaCAO::getRotation()
+void Oerkki1CAO::initialize(const std::string &data)
{
- return m_rotation;
+ //dstream<<"Oerkki1CAO: Got init data"<<std::endl;
+
+ {
+ std::istringstream is(data, std::ios::binary);
+ // version
+ u8 version = readU8(is);
+ // check version
+ if(version != 0)
+ return;
+ // pos
+ m_position = readV3F1000(is);
+ pos_translator.init(m_position);
+ }
+
+ updateNodePos();
}
diff --git a/src/clientobject.h b/src/clientobject.h
index 840fe5ede..8d211fef3 100644
--- a/src/clientobject.h
+++ b/src/clientobject.h
@@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "common_irrlicht.h"
#include "activeobject.h"
+#include "utility.h"
/*
@@ -35,6 +36,65 @@ Some planning
*/
+/*
+ SmoothTranslator
+*/
+
+struct SmoothTranslator
+{
+ v3f vect_old;
+ f32 anim_counter;
+ f32 anim_time;
+ f32 anim_time_counter;
+ v3f vect_show;
+ v3f vect_aim;
+
+ SmoothTranslator():
+ vect_old(0,0,0),
+ anim_counter(0),
+ anim_time(0),
+ anim_time_counter(0),
+ vect_show(0,0,0),
+ vect_aim(0,0,0)
+ {}
+
+ void init(v3f vect)
+ {
+ vect_old = vect;
+ vect_show = vect;
+ vect_aim = vect;
+ }
+
+ void update(v3f vect_new)
+ {
+ vect_old = vect_show;
+ vect_aim = vect_new;
+ if(anim_time < 0.001 || anim_time > 1.0)
+ anim_time = anim_time_counter;
+ else
+ anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
+ anim_time_counter = 0;
+ anim_counter = 0;
+ }
+
+ void translate(f32 dtime)
+ {
+ anim_time_counter = anim_time_counter + dtime;
+ anim_counter = anim_counter + dtime;
+ v3f vect_move = vect_aim - vect_old;
+ f32 moveratio = 1.0;
+ if(anim_time > 0.001)
+ moveratio = anim_time_counter / anim_time;
+ // Move a bit less than should, to avoid oscillation
+ moveratio = moveratio * 0.8;
+ if(moveratio > 1.5)
+ moveratio = 1.5;
+ vect_show = vect_old + vect_move * moveratio;
+ }
+};
+
+class ClientEnvironment;
+
class ClientActiveObject : public ActiveObject
{
public:
@@ -46,16 +106,21 @@ public:
// 0 <= light_at_pos <= LIGHT_SUN
virtual void updateLight(u8 light_at_pos){}
virtual v3s16 getLightPosition(){return v3s16(0,0,0);}
+ virtual core::aabbox3d<f32>* getSelectionBox(){return NULL;}
+ virtual core::aabbox3d<f32>* getCollisionBox(){return NULL;}
+ virtual v3f getPosition(){return v3f(0,0,0);}
// Step object in time
- virtual void step(float dtime){}
+ virtual void step(float dtime, ClientEnvironment *env){}
// Process a message sent by the server side object
virtual void processMessage(const std::string &data){}
+ virtual std::string infoText() {return "";}
+
/*
- This takes the return value of getClientInitializationData
- TODO: Usage of this
+ This takes the return value of
+ ServerActiveObject::getClientInitializationData
*/
virtual void initialize(const std::string &data){}
@@ -63,12 +128,39 @@ public:
static ClientActiveObject* create(u8 type);
protected:
+ // Used for creating objects based on type
+ typedef ClientActiveObject* (*Factory)();
+ static void registerType(u16 type, Factory f);
+private:
+ // Used for creating objects based on type
+ static core::map<u16, Factory> m_types;
+};
+
+struct DistanceSortedActiveObject
+{
+ ClientActiveObject *obj;
+ f32 d;
+
+ DistanceSortedActiveObject(ClientActiveObject *a_obj, f32 a_d)
+ {
+ obj = a_obj;
+ d = a_d;
+ }
+
+ bool operator < (DistanceSortedActiveObject &other)
+ {
+ return d < other.d;
+ }
};
+/*
+ TestCAO
+*/
+
class TestCAO : public ClientActiveObject
{
public:
- TestCAO(u16 id);
+ TestCAO();
virtual ~TestCAO();
u8 getType() const
@@ -76,13 +168,15 @@ public:
return ACTIVEOBJECT_TYPE_TEST;
}
+ static ClientActiveObject* create();
+
void addToScene(scene::ISceneManager *smgr);
void removeFromScene();
void updateLight(u8 light_at_pos);
v3s16 getLightPosition();
void updateNodePos();
- void step(float dtime);
+ void step(float dtime, ClientEnvironment *env);
void processMessage(const std::string &data);
@@ -91,30 +185,63 @@ private:
v3f m_position;
};
-extern "C"{
-#include "lua.h"
-#include "lualib.h"
-#include "lauxlib.h"
-}
+/*
+ ItemCAO
+*/
-class LuaCAO : public ClientActiveObject
+class ItemCAO : public ClientActiveObject
{
public:
- LuaCAO(u16 id);
- virtual ~LuaCAO();
+ ItemCAO();
+ virtual ~ItemCAO();
u8 getType() const
{
- return ACTIVEOBJECT_TYPE_LUA;
+ return ACTIVEOBJECT_TYPE_ITEM;
}
- void step(float dtime);
+ static ClientActiveObject* create();
+
+ void addToScene(scene::ISceneManager *smgr);
+ void removeFromScene();
+ void updateLight(u8 light_at_pos);
+ v3s16 getLightPosition();
+ void updateNodePos();
+
+ void step(float dtime, ClientEnvironment *env);
void processMessage(const std::string &data);
void initialize(const std::string &data);
+
+ core::aabbox3d<f32>* getSelectionBox()
+ {return &m_selection_box;}
+ v3f getPosition()
+ {return m_position;}
+
+private:
+ core::aabbox3d<f32> m_selection_box;
+ scene::IMeshSceneNode *m_node;
+ v3f m_position;
+ std::string m_inventorystring;
+};
+
+/*
+ RatCAO
+*/
- void loadScript(const std::string script);
+class RatCAO : public ClientActiveObject
+{
+public:
+ RatCAO();
+ virtual ~RatCAO();
+
+ u8 getType() const
+ {
+ return ACTIVEOBJECT_TYPE_RAT;
+ }
+
+ static ClientActiveObject* create();
void addToScene(scene::ISceneManager *smgr);
void removeFromScene();
@@ -122,26 +249,67 @@ public:
v3s16 getLightPosition();
void updateNodePos();
- void setPosition(v3f pos);
- v3f getPosition();
+ void step(float dtime, ClientEnvironment *env);
+
+ void processMessage(const std::string &data);
- void setRotation(v3f rot);
- v3f getRotation();
+ void initialize(const std::string &data);
- // image: eg. "rat.png"
- // corners: v3f corners[4]
- void addToMesh(const char *image, v3f *corners, bool backface_culling);
- void clearMesh();
+ core::aabbox3d<f32>* getSelectionBox()
+ {return &m_selection_box;}
+ v3f getPosition()
+ {return m_position;}
private:
- lua_State* L;
-
- scene::ISceneManager *m_smgr;
+ core::aabbox3d<f32> m_selection_box;
scene::IMeshSceneNode *m_node;
- scene::SMesh *m_mesh;
+ v3f m_position;
+ float m_yaw;
+ SmoothTranslator pos_translator;
+};
+
+/*
+ Oerkki1CAO
+*/
+class Oerkki1CAO : public ClientActiveObject
+{
+public:
+ Oerkki1CAO();
+ virtual ~Oerkki1CAO();
+
+ u8 getType() const
+ {
+ return ACTIVEOBJECT_TYPE_OERKKI1;
+ }
+
+ static ClientActiveObject* create();
+
+ void addToScene(scene::ISceneManager *smgr);
+ void removeFromScene();
+ void updateLight(u8 light_at_pos);
+ v3s16 getLightPosition();
+ void updateNodePos();
+
+ void step(float dtime, ClientEnvironment *env);
+
+ void processMessage(const std::string &data);
+
+ void initialize(const std::string &data);
+
+ core::aabbox3d<f32>* getSelectionBox()
+ {return &m_selection_box;}
+ v3f getPosition()
+ {return pos_translator.vect_show;}
+ //{return m_position;}
+
+private:
+ IntervalLimiter m_attack_interval;
+ core::aabbox3d<f32> m_selection_box;
+ scene::IMeshSceneNode *m_node;
v3f m_position;
- v3f m_rotation;
+ float m_yaw;
+ SmoothTranslator pos_translator;
};
#endif
diff --git a/src/clientserver.h b/src/clientserver.h
index 893bbc1e0..7972762c0 100644
--- a/src/clientserver.h
+++ b/src/clientserver.h
@@ -24,6 +24,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define PROTOCOL_ID 0x4f457403
+#define PASSWORD_SIZE 28 // Maximum password length. Allows for
+ // base64-encoded SHA-1 (27+\0).
+
enum ToClientCommand
{
TOCLIENT_INIT = 0x10,
@@ -33,14 +36,18 @@ enum ToClientCommand
[0] u16 TOSERVER_INIT
[2] u8 deployed version
- [3] v3s16 player's position + v3f(0,BS/2,0) floatToInt'd
+ [3] v3s16 player's position + v3f(0,BS/2,0) floatToInt'd
+ ([4] u64 map seed (new as of 2011-02-27))
+
+ NOTE: The position in here is deprecated; position is
+ explicitly sent afterwards
*/
TOCLIENT_BLOCKDATA = 0x20, //TODO: Multiple blocks
TOCLIENT_ADDNODE = 0x21,
TOCLIENT_REMOVENODE = 0x22,
- TOCLIENT_PLAYERPOS = 0x23,
+ TOCLIENT_PLAYERPOS = 0x23, // Obsolete
/*
[0] u16 command
// Followed by an arbitary number of these:
@@ -61,9 +68,9 @@ enum ToClientCommand
[N] char[20] name
*/
- TOCLIENT_OPT_BLOCK_NOT_FOUND = 0x25, // Not used
+ TOCLIENT_OPT_BLOCK_NOT_FOUND = 0x25, // Obsolete
- TOCLIENT_SECTORMETA = 0x26, // Not used
+ TOCLIENT_SECTORMETA = 0x26, // Obsolete
/*
[0] u16 command
[2] u8 sector count
@@ -133,6 +140,26 @@ enum ToClientCommand
}
*/
+ TOCLIENT_HP = 0x33,
+ /*
+ u16 command
+ u8 hp
+ */
+
+ TOCLIENT_MOVE_PLAYER = 0x34,
+ /*
+ u16 command
+ v3f1000 player position
+ f1000 player pitch
+ f1000 player yaw
+ */
+
+ TOCLIENT_ACCESS_DENIED = 0x35,
+ /*
+ u16 command
+ u16 reason_length
+ wstring reason
+ */
};
enum ToServerCommand
@@ -144,6 +171,7 @@ enum ToServerCommand
[0] u16 TOSERVER_INIT
[2] u8 SER_FMT_VER_HIGHEST
[3] u8[20] player_name
+ [23] u8[28] password
*/
TOSERVER_INIT2 = 0x11,
@@ -154,9 +182,9 @@ enum ToServerCommand
[0] u16 TOSERVER_INIT2
*/
- TOSERVER_GETBLOCK=0x20, // Not used
- TOSERVER_ADDNODE = 0x21, // Not used
- TOSERVER_REMOVENODE = 0x22, // deprecated
+ TOSERVER_GETBLOCK=0x20, // Obsolete
+ TOSERVER_ADDNODE = 0x21, // Obsolete
+ TOSERVER_REMOVENODE = 0x22, // Obsolete
TOSERVER_PLAYERPOS = 0x23,
/*
@@ -185,7 +213,7 @@ enum ToServerCommand
...
*/
- TOSERVER_ADDNODE_FROM_INVENTORY = 0x26, // deprecated
+ TOSERVER_ADDNODE_FROM_INVENTORY = 0x26, // Obsolete
/*
[0] u16 command
[2] v3s16 pos
@@ -217,9 +245,9 @@ enum ToServerCommand
3: digging completed
*/
- TOSERVER_RELEASE = 0x29, // Not used
+ TOSERVER_RELEASE = 0x29, // Obsolete
- TOSERVER_SIGNTEXT = 0x30,
+ TOSERVER_SIGNTEXT = 0x30, // Old signs
/*
u16 command
v3s16 blockpos
@@ -240,6 +268,38 @@ enum ToServerCommand
wstring message
*/
+ TOSERVER_SIGNNODETEXT = 0x33,
+ /*
+ u16 command
+ v3s16 p
+ u16 textlen
+ textdata
+ */
+
+ TOSERVER_CLICK_ACTIVEOBJECT = 0x34,
+ /*
+ length: 7
+ [0] u16 command
+ [2] u8 button (0=left, 1=right)
+ [3] u16 id
+ [5] u16 item
+ */
+
+ TOSERVER_DAMAGE = 0x35,
+ /*
+ u16 command
+ u8 amount
+ */
+
+ TOSERVER_PASSWORD=0x36,
+ /*
+ Sent to change password.
+
+ [0] u16 TOSERVER_PASSWORD
+ [2] u8[28] old password
+ [30] u8[28] new password
+ */
+
};
inline SharedBuffer<u8> makePacket_TOCLIENT_TIME_OF_DAY(u16 time)
diff --git a/src/clouds.cpp b/src/clouds.cpp
new file mode 100644
index 000000000..70d19fde5
--- /dev/null
+++ b/src/clouds.cpp
@@ -0,0 +1,157 @@
+/*
+Minetest-c55
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "clouds.h"
+#include "noise.h"
+#include "constants.h"
+#include "debug.h"
+
+Clouds::Clouds(
+ scene::ISceneNode* parent,
+ scene::ISceneManager* mgr,
+ s32 id,
+ float cloud_y,
+ u32 seed
+):
+ scene::ISceneNode(parent, mgr, id),
+ m_cloud_y(cloud_y),
+ m_seed(seed),
+ m_camera_pos(0,0),
+ m_time(0)
+{
+ dstream<<__FUNCTION_NAME<<std::endl;
+
+ m_material.setFlag(video::EMF_LIGHTING, false);
+ m_material.setFlag(video::EMF_BACK_FACE_CULLING, false);
+ m_material.setFlag(video::EMF_BILINEAR_FILTER, false);
+ m_material.setFlag(video::EMF_FOG_ENABLE, false);
+ //m_material.setFlag(video::EMF_ANTI_ALIASING, true);
+ //m_material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
+
+ m_box = core::aabbox3d<f32>(-BS*1000000,cloud_y-BS,-BS*1000000,
+ BS*1000000,cloud_y+BS,BS*1000000);
+
+}
+
+Clouds::~Clouds()
+{
+ dstream<<__FUNCTION_NAME<<std::endl;
+}
+
+void Clouds::OnRegisterSceneNode()
+{
+ if(IsVisible)
+ {
+ SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
+ //SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
+ }
+
+ ISceneNode::OnRegisterSceneNode();
+}
+
+#define MYROUND(x) (x > 0.0 ? (int)x : (int)x - 1)
+
+void Clouds::render()
+{
+ video::IVideoDriver* driver = SceneManager->getVideoDriver();
+
+ /*if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_TRANSPARENT)
+ return;*/
+ if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SOLID)
+ return;
+
+ driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
+ driver->setMaterial(m_material);
+
+ /*
+ Clouds move from X+ towards X-
+ */
+
+ const s16 cloud_radius_i = 12;
+ const float cloud_size = BS*50;
+ const v2f cloud_speed(-BS*2, 0);
+
+ // Position of cloud noise origin in world coordinates
+ v2f world_cloud_origin_pos_f = m_time*cloud_speed;
+ // Position of cloud noise origin from the camera
+ v2f cloud_origin_from_camera_f = world_cloud_origin_pos_f - m_camera_pos;
+ // The center point of drawing in the noise
+ v2f center_of_drawing_in_noise_f = -cloud_origin_from_camera_f;
+ // The integer center point of drawing in the noise
+ v2s16 center_of_drawing_in_noise_i(
+ MYROUND(center_of_drawing_in_noise_f.X / cloud_size),
+ MYROUND(center_of_drawing_in_noise_f.Y / cloud_size)
+ );
+ // The world position of the integer center point of drawing in the noise
+ v2f world_center_of_drawing_in_noise_f = v2f(
+ center_of_drawing_in_noise_i.X * cloud_size,
+ center_of_drawing_in_noise_i.Y * cloud_size
+ ) + world_cloud_origin_pos_f;
+
+ for(s16 zi=-cloud_radius_i; zi<cloud_radius_i; zi++)
+ for(s16 xi=-cloud_radius_i; xi<cloud_radius_i; xi++)
+ {
+ v2s16 p_in_noise_i(
+ xi+center_of_drawing_in_noise_i.X,
+ zi+center_of_drawing_in_noise_i.Y
+ );
+
+ /*if((p_in_noise_i.X + p_in_noise_i.Y)%2==0)
+ continue;*/
+ /*if((p_in_noise_i.X/2 + p_in_noise_i.Y/2)%2==0)
+ continue;*/
+
+ v2f p0 = v2f(xi,zi)*cloud_size + world_center_of_drawing_in_noise_f;
+
+ double noise = noise2d_perlin_abs(
+ (float)p_in_noise_i.X*cloud_size/BS/200,
+ (float)p_in_noise_i.Y*cloud_size/BS/200,
+ m_seed, 3, 0.4);
+ if(noise < 0.8)
+ continue;
+
+ v2f p1 = p0 + v2f(1,1)*cloud_size;
+
+ //video::SColor c(128,255,255,255);
+ float b = m_brightness;
+ video::SColor c(128,b*230,b*230,b*255);
+ video::S3DVertex vertices[4] =
+ {
+ video::S3DVertex(p0.X,m_cloud_y,p0.Y, 0,0,0, c, 0,1),
+ video::S3DVertex(p0.X,m_cloud_y,p1.Y, 0,0,0, c, 1,1),
+ video::S3DVertex(p1.X,m_cloud_y,p1.Y, 0,0,0, c, 1,0),
+ video::S3DVertex(p1.X,m_cloud_y,p0.Y, 0,0,0, c, 0,0),
+ };
+ u16 indices[] = {0,1,2,2,3,0};
+ driver->drawVertexPrimitiveList(vertices, 4, indices, 2,
+ video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
+ }
+}
+
+void Clouds::step(float dtime)
+{
+ m_time += dtime;
+}
+
+void Clouds::update(v2f camera_p, float brightness)
+{
+ m_camera_pos = camera_p;
+ m_brightness = brightness;
+}
+
diff --git a/src/clouds.h b/src/clouds.h
new file mode 100644
index 000000000..59861371e
--- /dev/null
+++ b/src/clouds.h
@@ -0,0 +1,83 @@
+/*
+Minetest-c55
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#ifndef CLOUDS_HEADER
+#define CLOUDS_HEADER
+
+#include "common_irrlicht.h"
+#include <iostream>
+
+class Clouds : public scene::ISceneNode
+{
+public:
+ Clouds(
+ scene::ISceneNode* parent,
+ scene::ISceneManager* mgr,
+ s32 id,
+ float cloud_y,
+ u32 seed
+ );
+
+ ~Clouds();
+
+ /*
+ ISceneNode methods
+ */
+
+ virtual void OnRegisterSceneNode();
+
+ virtual void render();
+
+ virtual const core::aabbox3d<f32>& getBoundingBox() const
+ {
+ return m_box;
+ }
+
+ virtual u32 getMaterialCount() const
+ {
+ return 1;
+ }
+
+ virtual video::SMaterial& getMaterial(u32 i)
+ {
+ return m_material;
+ }
+
+ /*
+ Other stuff
+ */
+
+ void step(float dtime);
+
+ void update(v2f camera_p, float brightness);
+
+private:
+ video::SMaterial m_material;
+ core::aabbox3d<f32> m_box;
+ float m_cloud_y;
+ float m_brightness;
+ u32 m_seed;
+ v2f m_camera_pos;
+ float m_time;
+};
+
+
+
+#endif
+
diff --git a/src/cmake_config.h.in b/src/cmake_config.h.in
index 8635b4f28..5e375191f 100644
--- a/src/cmake_config.h.in
+++ b/src/cmake_config.h.in
@@ -10,7 +10,7 @@
#else
#define BUILD_TYPE "Debug"
#endif
-#define BUILD_INFO "VER="VERSION_STRING" BUILD_DATE=@BUILD_DATE@ RUN_IN_PLACE=@RUN_IN_PLACE@ INSTALL_PREFIX=@CMAKE_INSTALL_PREFIX@ BUILD_TYPE="BUILD_TYPE
+#define BUILD_INFO "VER="VERSION_STRING" RUN_IN_PLACE=@RUN_IN_PLACE@ INSTALL_PREFIX=@CMAKE_INSTALL_PREFIX@ BUILD_TYPE="BUILD_TYPE
#endif
diff --git a/src/collision.cpp b/src/collision.cpp
new file mode 100644
index 000000000..63186a84a
--- /dev/null
+++ b/src/collision.cpp
@@ -0,0 +1,185 @@
+/*
+Minetest-c55
+Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "collision.h"
+#include "mapblock.h"
+#include "map.h"
+
+collisionMoveResult collisionMoveSimple(Map *map, f32 pos_max_d,
+ const core::aabbox3d<f32> &box_0,
+ f32 dtime, v3f &pos_f, v3f &speed_f)
+{
+ collisionMoveResult result;
+
+ v3f oldpos_f = pos_f;
+ v3s16 oldpos_i = floatToInt(oldpos_f, BS);
+
+ /*
+ Calculate new position
+ */
+ pos_f += speed_f * dtime;
+
+ /*
+ Collision detection
+ */
+
+ // position in nodes
+ v3s16 pos_i = floatToInt(pos_f, BS);
+
+ /*
+ Collision uncertainty radius
+ Make it a bit larger than the maximum distance of movement
+ */
+ f32 d = pos_max_d * 1.1;
+ // A fairly large value in here makes moving smoother
+ //f32 d = 0.15*BS;
+
+ // This should always apply, otherwise there are glitches
+ assert(d > pos_max_d);
+
+ /*
+ Calculate collision box
+ */
+ core::aabbox3d<f32> box = box_0;
+ box.MaxEdge += pos_f;
+ box.MinEdge += pos_f;
+ core::aabbox3d<f32> oldbox = box_0;
+ oldbox.MaxEdge += oldpos_f;
+ oldbox.MinEdge += oldpos_f;
+
+ /*
+ If the object lies on a walkable node, this is set to true.
+ */
+ result.touching_ground = false;
+
+ /*
+ Go through every node around the object
+ TODO: Calculate the range of nodes that need to be checked
+ */
+ for(s16 y = oldpos_i.Y - 1; y <= oldpos_i.Y + 2; y++)
+ for(s16 z = oldpos_i.Z - 1; z <= oldpos_i.Z + 1; z++)
+ for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++)
+ {
+ try{
+ // Object collides into walkable nodes
+ if(content_walkable(map->getNode(v3s16(x,y,z)).d) == false)
+ continue;
+ }
+ catch(InvalidPositionException &e)
+ {
+ // Doing nothing here will block the object from
+ // walking over map borders
+ }
+
+ core::aabbox3d<f32> nodebox = getNodeBox(v3s16(x,y,z), BS);
+
+ /*
+ See if the object is touching ground.
+
+ Object touches ground if object's minimum Y is near node's
+ maximum Y and object's X-Z-area overlaps with the node's
+ X-Z-area.
+
+ Use 0.15*BS so that it is easier to get on a node.
+ */
+ if(
+ //fabs(nodebox.MaxEdge.Y-box.MinEdge.Y) < d
+ fabs(nodebox.MaxEdge.Y-box.MinEdge.Y) < 0.15*BS
+ && nodebox.MaxEdge.X-d > box.MinEdge.X
+ && nodebox.MinEdge.X+d < box.MaxEdge.X
+ && nodebox.MaxEdge.Z-d > box.MinEdge.Z
+ && nodebox.MinEdge.Z+d < box.MaxEdge.Z
+ ){
+ result.touching_ground = true;
+ }
+
+ // If object doesn't intersect with node, ignore node.
+ if(box.intersectsWithBox(nodebox) == false)
+ continue;
+
+ /*
+ Go through every axis
+ */
+ v3f dirs[3] = {
+ v3f(0,0,1), // back-front
+ v3f(0,1,0), // top-bottom
+ v3f(1,0,0), // right-left
+ };
+ for(u16 i=0; i<3; i++)
+ {
+ /*
+ Calculate values along the axis
+ */
+ f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]);
+ f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]);
+ f32 objectmax = box.MaxEdge.dotProduct(dirs[i]);
+ f32 objectmin = box.MinEdge.dotProduct(dirs[i]);
+ f32 objectmax_old = oldbox.MaxEdge.dotProduct(dirs[i]);
+ f32 objectmin_old = oldbox.MinEdge.dotProduct(dirs[i]);
+
+ /*
+ Check collision for the axis.
+ Collision happens when object is going through a surface.
+ */
+ bool negative_axis_collides =
+ (nodemax > objectmin && nodemax <= objectmin_old + d
+ && speed_f.dotProduct(dirs[i]) < 0);
+ bool positive_axis_collides =
+ (nodemin < objectmax && nodemin >= objectmax_old - d
+ && speed_f.dotProduct(dirs[i]) > 0);
+ bool main_axis_collides =
+ negative_axis_collides || positive_axis_collides;
+
+ /*
+ Check overlap of object and node in other axes
+ */
+ bool other_axes_overlap = true;
+ for(u16 j=0; j<3; j++)
+ {
+ if(j == i)
+ continue;
+ f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]);
+ f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]);
+ f32 objectmax = box.MaxEdge.dotProduct(dirs[j]);
+ f32 objectmin = box.MinEdge.dotProduct(dirs[j]);
+ if(!(nodemax - d > objectmin && nodemin + d < objectmax))
+ {
+ other_axes_overlap = false;
+ break;
+ }
+ }
+
+ /*
+ If this is a collision, revert the pos_f in the main
+ direction.
+ */
+ if(other_axes_overlap && main_axis_collides)
+ {
+ speed_f -= speed_f.dotProduct(dirs[i]) * dirs[i];
+ pos_f -= pos_f.dotProduct(dirs[i]) * dirs[i];
+ pos_f += oldpos_f.dotProduct(dirs[i]) * dirs[i];
+ }
+
+ }
+ } // xyz
+
+ return result;
+}
+
+
diff --git a/src/iirrlichtwrapper.h b/src/collision.h
index 66f8a55c6..9c913c6a9 100644
--- a/src/iirrlichtwrapper.h
+++ b/src/collision.h
@@ -17,46 +17,36 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#ifndef IIRRLICHTWRAPPER_HEADER
-#define IIRRLICHTWRAPPER_HEADER
+#ifndef COLLISION_HEADER
+#define COLLISION_HEADER
#include "common_irrlicht.h"
-#include "texture.h"
-/*
- NOTE: This is deprecated and should be removed completely
-*/
+class Map;
-/*
- IrrlichtWrapper prototype.
+struct collisionMoveResult
+{
+ bool touching_ground;
- Server supplies this as a dummy wrapper.
-*/
+ collisionMoveResult():
+ touching_ground(false)
+ {}
+};
+
+collisionMoveResult collisionMoveSimple(Map *map, f32 pos_max_d,
+ const core::aabbox3d<f32> &box_0,
+ f32 dtime, v3f &pos_f, v3f &speed_f);
+//{return collisionMoveResult();}
+
+enum CollisionType
+{
+ COLLISION_FALL
+};
-class IIrrlichtWrapper
+struct CollisionInfo
{
-public:
- IIrrlichtWrapper()
- {
- }
- virtual ~IIrrlichtWrapper()
- {
- }
-
- // Should be called only from the main thread
- virtual IrrlichtDevice* getDevice(){ return NULL; }
-
- virtual u32 getTime()
- {
- return 0;
- }
-
- /*virtual textureid_t getTextureId(const std::string &name){ return 0; }
- virtual std::string getTextureName(textureid_t id){ return ""; }
- virtual video::ITexture* getTexture(const std::string &name){ return NULL; }
- virtual video::ITexture* getTexture(const TextureSpec &spec){ return NULL; }*/
-
-private:
+ CollisionType t;
+ f32 speed;
};
#endif
diff --git a/src/common_irrlicht.h b/src/common_irrlicht.h
index 88e74878b..785f4fec7 100644
--- a/src/common_irrlicht.h
+++ b/src/common_irrlicht.h
@@ -1,6 +1,6 @@
/*
Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -20,6 +20,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifndef COMMON_IRRLICHT_HEADER
#define COMMON_IRRLICHT_HEADER
+#define endSceneX(d){d->draw2DLine(v2s32(0,0),v2s32(1,0),\
+video::SColor(255,30,30,30));d->endScene();}
+
#include <irrlicht.h>
using namespace irr;
typedef core::vector3df v3f;
diff --git a/src/connection.cpp b/src/connection.cpp
index b07e0de90..548a7f532 100644
--- a/src/connection.cpp
+++ b/src/connection.cpp
@@ -320,7 +320,7 @@ IncomingSplitBuffer::~IncomingSplitBuffer()
This will throw a GotSplitPacketException when a full
split packet is constructed.
*/
-void IncomingSplitBuffer::insert(BufferedPacket &p, bool reliable)
+SharedBuffer<u8> IncomingSplitBuffer::insert(BufferedPacket &p, bool reliable)
{
u32 headersize = BASE_HEADER_SIZE + 7;
assert(p.data.getSize() >= headersize);
@@ -363,9 +363,9 @@ void IncomingSplitBuffer::insert(BufferedPacket &p, bool reliable)
// Set chunk data in buffer
sp->chunks[chunk_num] = chunkdata;
- // If not all chunks are received, return
+ // If not all chunks are received, return empty buffer
if(sp->allReceived() == false)
- return;
+ return SharedBuffer<u8>();
// Calculate total size
u32 totalsize = 0;
@@ -392,8 +392,8 @@ void IncomingSplitBuffer::insert(BufferedPacket &p, bool reliable)
// Remove sp from buffer
m_buf.remove(seqnum);
delete sp;
-
- throw GotSplitPacketException(fulldata);
+
+ return fulldata;
}
void IncomingSplitBuffer::removeUnreliableTimedOuts(float dtime, float timeout)
{
@@ -709,21 +709,17 @@ SharedBuffer<u8> Channel::ProcessPacket(
con->GetProtocolID(),
peer_id,
channelnum);
- try{
- // Buffer the packet
- incoming_splits.insert(packet, reliable);
- }
- // This exception happens when all the pieces of a packet
- // are collected.
- catch(GotSplitPacketException &e)
+ // Buffer the packet
+ SharedBuffer<u8> data = incoming_splits.insert(packet, reliable);
+ if(data.getSize() != 0)
{
con->PrintInfo();
dout_con<<"RETURNING TYPE_SPLIT: Constructed full data, "
- <<"size="<<e.getData().getSize()<<std::endl;
- return e.getData();
+ <<"size="<<data.getSize()<<std::endl;
+ return data;
}
con->PrintInfo();
- dout_con<<"BUFFERING TYPE_SPLIT"<<std::endl;
+ dout_con<<"BUFFERED TYPE_SPLIT"<<std::endl;
throw ProcessedSilentlyException("Buffered a split packet chunk");
}
else if(type == TYPE_RELIABLE)
diff --git a/src/connection.h b/src/connection.h
index 0b5d5e230..6eb2f2824 100644
--- a/src/connection.h
+++ b/src/connection.h
@@ -99,19 +99,6 @@ public:
{}
};
-class GotSplitPacketException
-{
- SharedBuffer<u8> m_data;
-public:
- GotSplitPacketException(SharedBuffer<u8> data):
- m_data(data)
- {}
- SharedBuffer<u8> getData()
- {
- return m_data;
- }
-};
-
inline u16 readPeerId(u8 *packetdata)
{
return readU16(&packetdata[4]);
@@ -314,10 +301,10 @@ class IncomingSplitBuffer
public:
~IncomingSplitBuffer();
/*
- This will throw a GotSplitPacketException when a full
- split packet is constructed.
+ Returns a reference counted buffer of length != 0 when a full split
+ packet is constructed. If not, returns one of length 0.
*/
- void insert(BufferedPacket &p, bool reliable);
+ SharedBuffer<u8> insert(BufferedPacket &p, bool reliable);
void removeUnreliableTimedOuts(float dtime, float timeout);
diff --git a/src/constants.h b/src/constants.h
index 3c37fca87..14ca58b43 100644
--- a/src/constants.h
+++ b/src/constants.h
@@ -33,6 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define DEBUGFILE "debug.txt"
#define WATER_ALPHA 160
+//#define WATER_ALPHA 190
// Define for simulating the quirks of sending through internet.
// Causes the socket class to deliberately drop random packets.
@@ -52,6 +53,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define FOV_ANGLE (PI/2.5)
// The absolute working limit is (2^15 - viewing_range).
+// I really don't want to make every algorithm to check if it's
+// going near the limit or not, so this is lower.
#define MAP_GENERATION_LIMIT (31000)
// Size of node in rendering units
diff --git a/src/debug.cpp b/src/debug.cpp
index d4d07375d..a19186232 100644
--- a/src/debug.cpp
+++ b/src/debug.cpp
@@ -90,6 +90,7 @@ DebugStack::DebugStack(threadid_t id)
threadid = id;
stack_i = 0;
stack_max_i = 0;
+ memset(stack, 0, DEBUG_STACK_SIZE*DEBUG_STACK_TEXT_SIZE);
}
void DebugStack::print(FILE *file, bool everything)
diff --git a/src/debug.h b/src/debug.h
index 5cc1e6c4d..234b7c74a 100644
--- a/src/debug.h
+++ b/src/debug.h
@@ -169,7 +169,10 @@ private:
bool m_overflowed;
};
-#define DSTACK(...)\
+#define DSTACK(msg)\
+ DebugStacker __debug_stacker(msg);
+
+#define DSTACKF(...)\
char __buf[DEBUG_STACK_TEXT_SIZE];\
snprintf(__buf,\
DEBUG_STACK_TEXT_SIZE, __VA_ARGS__);\
diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp
index 2a548d406..73f22a7f7 100644
--- a/src/defaultsettings.cpp
+++ b/src/defaultsettings.cpp
@@ -24,23 +24,39 @@ extern Settings g_settings;
void set_default_settings()
{
// Client and server
+
+ g_settings.setDefault("port", "");
+ g_settings.setDefault("name", "");
g_settings.setDefault("footprints", "false");
// Client stuff
+
+ g_settings.setDefault("keymap_forward", "KEY_KEY_W");
+ g_settings.setDefault("keymap_backward", "KEY_KEY_S");
+ g_settings.setDefault("keymap_left", "KEY_KEY_A");
+ g_settings.setDefault("keymap_right", "KEY_KEY_D");
+ g_settings.setDefault("keymap_jump", "KEY_SPACE");
+ g_settings.setDefault("keymap_sneak", "KEY_LSHIFT");
+ g_settings.setDefault("keymap_inventory", "KEY_KEY_I");
+ g_settings.setDefault("keymap_chat", "KEY_KEY_T");
+ g_settings.setDefault("keymap_rangeselect", "KEY_KEY_R");
+ // Some (temporary) keys for debugging
+ g_settings.setDefault("keymap_special1", "KEY_KEY_E");
+ g_settings.setDefault("keymap_print_debug_stacks", "KEY_KEY_P");
+
g_settings.setDefault("wanted_fps", "30");
g_settings.setDefault("fps_max", "60");
g_settings.setDefault("viewing_range_nodes_max", "300");
g_settings.setDefault("viewing_range_nodes_min", "35");
g_settings.setDefault("screenW", "800");
g_settings.setDefault("screenH", "600");
- g_settings.setDefault("port", "");
g_settings.setDefault("address", "");
- g_settings.setDefault("name", "");
g_settings.setDefault("random_input", "false");
g_settings.setDefault("client_delete_unused_sectors_timeout", "1200");
g_settings.setDefault("enable_fog", "true");
g_settings.setDefault("new_style_water", "false");
g_settings.setDefault("new_style_leaves", "true");
+ g_settings.setDefault("smooth_lighting", "true");
g_settings.setDefault("frametime_graph", "false");
g_settings.setDefault("enable_texture_atlas", "true");
g_settings.setDefault("texture_path", "");
@@ -49,20 +65,24 @@ void set_default_settings()
g_settings.setDefault("free_move", "false");
g_settings.setDefault("continuous_forward", "false");
g_settings.setDefault("fast_move", "false");
+ g_settings.setDefault("invert_mouse", "false");
// Server stuff
- g_settings.setDefault("fast_move", "false");
-
g_settings.setDefault("enable_experimental", "false");
g_settings.setDefault("creative_mode", "false");
+ g_settings.setDefault("enable_damage", "false"); //TODO: Set to true when healing is possible
+ g_settings.setDefault("give_initial_stuff", "false");
+ g_settings.setDefault("default_password", "");
+ g_settings.setDefault("default_privs", "build, shout");
+ g_settings.setDefault("profiler_print_interval", "0");
g_settings.setDefault("objectdata_interval", "0.2");
g_settings.setDefault("active_object_range", "2");
g_settings.setDefault("max_simultaneous_block_sends_per_client", "1");
//g_settings.setDefault("max_simultaneous_block_sends_per_client", "2");
- g_settings.setDefault("max_simultaneous_block_sends_server_total", "4");
- g_settings.setDefault("max_block_send_distance", "7");
- g_settings.setDefault("max_block_generate_distance", "7");
+ g_settings.setDefault("max_simultaneous_block_sends_server_total", "8");
+ g_settings.setDefault("max_block_send_distance", "8");
+ g_settings.setDefault("max_block_generate_distance", "8");
g_settings.setDefault("time_send_interval", "20");
g_settings.setDefault("time_speed", "96");
g_settings.setDefault("server_unload_unused_sectors_timeout", "60");
diff --git a/src/environment.cpp b/src/environment.cpp
index 7f3687c3c..f5f20d0e5 100644
--- a/src/environment.cpp
+++ b/src/environment.cpp
@@ -1,6 +1,6 @@
/*
Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -20,10 +20,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "environment.h"
#include "filesys.h"
#include "porting.h"
+#include "collision.h"
-Environment::Environment()
+
+Environment::Environment():
+ m_time_of_day(9000)
{
- m_daynight_ratio = 0.5;
}
Environment::~Environment()
@@ -172,14 +174,84 @@ void Environment::printPlayers(std::ostream &o)
}
}
-void Environment::setDayNightRatio(u32 r)
+/*void Environment::setDayNightRatio(u32 r)
{
- m_daynight_ratio = r;
-}
+ getDayNightRatio() = r;
+}*/
u32 Environment::getDayNightRatio()
{
- return m_daynight_ratio;
+ //return getDayNightRatio();
+ return time_to_daynight_ratio(m_time_of_day);
+}
+
+/*
+ ActiveBlockList
+*/
+
+void fillRadiusBlock(v3s16 p0, s16 r, core::map<v3s16, bool> &list)
+{
+ v3s16 p;
+ for(p.X=p0.X-r; p.X<=p0.X+r; p.X++)
+ for(p.Y=p0.Y-r; p.Y<=p0.Y+r; p.Y++)
+ for(p.Z=p0.Z-r; p.Z<=p0.Z+r; p.Z++)
+ {
+ // Set in list
+ list[p] = true;
+ }
+}
+
+void ActiveBlockList::update(core::list<v3s16> &active_positions,
+ s16 radius,
+ core::map<v3s16, bool> &blocks_removed,
+ core::map<v3s16, bool> &blocks_added)
+{
+ /*
+ Create the new list
+ */
+ core::map<v3s16, bool> newlist;
+ for(core::list<v3s16>::Iterator i = active_positions.begin();
+ i != active_positions.end(); i++)
+ {
+ fillRadiusBlock(*i, radius, newlist);
+ }
+
+ /*
+ Find out which blocks on the old list are not on the new list
+ */
+ // Go through old list
+ for(core::map<v3s16, bool>::Iterator i = m_list.getIterator();
+ i.atEnd()==false; i++)
+ {
+ v3s16 p = i.getNode()->getKey();
+ // If not on new list, it's been removed
+ if(newlist.find(p) == NULL)
+ blocks_removed.insert(p, true);
+ }
+
+ /*
+ Find out which blocks on the new list are not on the old list
+ */
+ // Go through new list
+ for(core::map<v3s16, bool>::Iterator i = newlist.getIterator();
+ i.atEnd()==false; i++)
+ {
+ v3s16 p = i.getNode()->getKey();
+ // If not on old list, it's been added
+ if(m_list.find(p) == NULL)
+ blocks_added.insert(p, true);
+ }
+
+ /*
+ Update m_list
+ */
+ m_list.clear();
+ for(core::map<v3s16, bool>::Iterator i = newlist.getIterator();
+ i.atEnd()==false; i++)
+ {
+ v3s16 p = i.getNode()->getKey();
+ m_list.insert(p, true);
+ }
}
/*
@@ -189,12 +261,22 @@ u32 Environment::getDayNightRatio()
ServerEnvironment::ServerEnvironment(ServerMap *map, Server *server):
m_map(map),
m_server(server),
- m_random_spawn_timer(0)
+ m_random_spawn_timer(3),
+ m_send_recommended_timer(0),
+ m_game_time(0),
+ m_game_time_fraction_counter(0)
{
}
ServerEnvironment::~ServerEnvironment()
{
+ // Clear active block list.
+ // This makes the next one delete all active objects.
+ m_active_blocks.clear();
+
+ // Convert all objects to static and delete the active objects
+ deactivateFarObjects(true);
+
// Drop/delete map
m_map->drop();
}
@@ -344,7 +426,14 @@ void ServerEnvironment::deSerializePlayers(const std::string &savedir)
testplayer.deSerialize(is);
}
- dstream<<"Loaded test player with name "<<testplayer.getName()<<std::endl;
+ if(!string_allowed(testplayer.getName(), PLAYERNAME_ALLOWED_CHARS))
+ {
+ dstream<<"Not loading player with invalid name: "
+ <<testplayer.getName()<<std::endl;
+ }
+
+ dstream<<"Loaded test player with name "<<testplayer.getName()
+ <<std::endl;
// Search for the player
std::string playername = testplayer.getName();
@@ -376,16 +465,143 @@ void ServerEnvironment::deSerializePlayers(const std::string &savedir)
}
}
+void ServerEnvironment::saveMeta(const std::string &savedir)
+{
+ std::string path = savedir + "/env_meta.txt";
+
+ // Open file and serialize
+ std::ofstream os(path.c_str(), std::ios_base::binary);
+ if(os.good() == false)
+ {
+ dstream<<"WARNING: ServerEnvironment::saveMeta(): Failed to open "
+ <<path<<std::endl;
+ throw SerializationError("Couldn't save env meta");
+ }
+
+ Settings args;
+ args.setU64("game_time", m_game_time);
+ args.setU64("time_of_day", getTimeOfDay());
+ args.writeLines(os);
+ os<<"EnvArgsEnd\n";
+}
+
+void ServerEnvironment::loadMeta(const std::string &savedir)
+{
+ std::string path = savedir + "/env_meta.txt";
+
+ // Open file and deserialize
+ std::ifstream is(path.c_str(), std::ios_base::binary);
+ if(is.good() == false)
+ {
+ dstream<<"WARNING: ServerEnvironment::loadMeta(): Failed to open "
+ <<path<<std::endl;
+ throw SerializationError("Couldn't load env meta");
+ }
+
+ Settings args;
+
+ for(;;)
+ {
+ if(is.eof())
+ throw SerializationError
+ ("ServerEnvironment::loadMeta(): EnvArgsEnd not found");
+ std::string line;
+ std::getline(is, line);
+ std::string trimmedline = trim(line);
+ if(trimmedline == "EnvArgsEnd")
+ break;
+ args.parseConfigLine(line);
+ }
+
+ try{
+ m_game_time = args.getU64("game_time");
+ }catch(SettingNotFoundException &e){
+ // Getting this is crucial, otherwise timestamps are useless
+ throw SerializationError("Couldn't load env meta game_time");
+ }
+
+ try{
+ m_time_of_day = args.getU64("time_of_day");
+ }catch(SettingNotFoundException &e){
+ // This is not as important
+ m_time_of_day = 9000;
+ }
+}
+
+#if 0
+// This is probably very useless
+void spawnRandomObjects(MapBlock *block)
+{
+ for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
+ for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
+ {
+ bool last_node_walkable = false;
+ for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
+ {
+ v3s16 p(x0,y0,z0);
+ MapNode n = block->getNodeNoEx(p);
+ if(n.d == CONTENT_IGNORE)
+ continue;
+ if(content_features(n.d).liquid_type != LIQUID_NONE)
+ continue;
+ if(content_features(n.d).walkable)
+ {
+ last_node_walkable = true;
+ continue;
+ }
+ if(last_node_walkable)
+ {
+ // If block contains light information
+ if(content_features(n.d).param_type == CPT_LIGHT)
+ {
+ if(n.getLight(LIGHTBANK_DAY) <= 5)
+ {
+ if(myrand() % 1000 == 0)
+ {
+ v3f pos_f = intToFloat(p+block->getPosRelative(), BS);
+ pos_f.Y -= BS*0.4;
+ ServerActiveObject *obj = new Oerkki1SAO(NULL,0,pos_f);
+ std::string data = obj->getStaticData();
+ StaticObject s_obj(obj->getType(),
+ obj->getBasePosition(), data);
+ // Add one
+ block->m_static_objects.insert(0, s_obj);
+ delete obj;
+ block->setChangedFlag();
+ }
+ }
+ }
+ }
+ last_node_walkable = false;
+ }
+ }
+}
+#endif
+
void ServerEnvironment::step(float dtime)
{
DSTACK(__FUNCTION_NAME);
+
+ //TimeTaker timer("ServerEnv step");
// Get some settings
- //bool free_move = g_settings.getBool("free_move");
bool footprints = g_settings.getBool("footprints");
+ /*
+ Increment game time
+ */
{
- //TimeTaker timer("Server m_map->timerUpdate()", g_device);
+ m_game_time_fraction_counter += dtime;
+ u32 inc_i = (u32)m_game_time_fraction_counter;
+ m_game_time += inc_i;
+ m_game_time_fraction_counter -= (float)inc_i;
+ }
+
+ /*
+ Let map update it's timers
+ */
+ {
+ //TimeTaker timer("Server m_map->timerUpdate()");
m_map->timerUpdate(dtime);
}
@@ -396,6 +612,11 @@ void ServerEnvironment::step(float dtime)
i != m_players.end(); i++)
{
Player *player = *i;
+
+ // Ignore disconnected players
+ if(player->peer_id == 0)
+ continue;
+
v3f playerpos = player->getPosition();
// Move
@@ -421,71 +642,282 @@ void ServerEnvironment::step(float dtime)
}
}
}
-
- if(g_settings.getBool("enable_experimental"))
- {
/*
- Step active objects
+ Manage active block list
*/
- for(core::map<u16, ServerActiveObject*>::Iterator
- i = m_active_objects.getIterator();
- i.atEnd()==false; i++)
+ if(m_active_blocks_management_interval.step(dtime, 2.0))
{
- ServerActiveObject* obj = i.getNode()->getValue();
- // Step object, putting messages directly to the queue
- obj->step(dtime, m_active_object_messages);
+ /*
+ Get player block positions
+ */
+ core::list<v3s16> players_blockpos;
+ for(core::list<Player*>::Iterator
+ i = m_players.begin();
+ i != m_players.end(); i++)
+ {
+ Player *player = *i;
+ // Ignore disconnected players
+ if(player->peer_id == 0)
+ continue;
+ v3s16 blockpos = getNodeBlockPos(
+ floatToInt(player->getPosition(), BS));
+ players_blockpos.push_back(blockpos);
+ }
+
+ /*
+ Update list of active blocks, collecting changes
+ */
+ const s16 active_block_range = 5;
+ core::map<v3s16, bool> blocks_removed;
+ core::map<v3s16, bool> blocks_added;
+ m_active_blocks.update(players_blockpos, active_block_range,
+ blocks_removed, blocks_added);
+
+ /*
+ Handle removed blocks
+ */
+
+ // Convert active objects that are no more in active blocks to static
+ deactivateFarObjects(false);
+
+ for(core::map<v3s16, bool>::Iterator
+ i = blocks_removed.getIterator();
+ i.atEnd()==false; i++)
+ {
+ v3s16 p = i.getNode()->getKey();
+
+ /*dstream<<"Server: Block ("<<p.X<<","<<p.Y<<","<<p.Z
+ <<") became inactive"<<std::endl;*/
+
+ MapBlock *block = m_map->getBlockNoCreateNoEx(p);
+ if(block==NULL)
+ continue;
+
+ // Set current time as timestamp
+ block->setTimestamp(m_game_time);
+ }
+
+ /*
+ Handle added blocks
+ */
+
+ for(core::map<v3s16, bool>::Iterator
+ i = blocks_added.getIterator();
+ i.atEnd()==false; i++)
+ {
+ v3s16 p = i.getNode()->getKey();
+
+ /*dstream<<"Server: Block ("<<p.X<<","<<p.Y<<","<<p.Z
+ <<") became active"<<std::endl;*/
+
+ MapBlock *block = m_map->getBlockNoCreateNoEx(p);
+ if(block==NULL)
+ continue;
+
+ // Get time difference
+ u32 dtime_s = 0;
+ u32 stamp = block->getTimestamp();
+ if(m_game_time > stamp && stamp != BLOCK_TIMESTAMP_UNDEFINED)
+ dtime_s = m_game_time - block->getTimestamp();
+
+ // Set current time as timestamp
+ block->setTimestamp(m_game_time);
+
+ //dstream<<"Block is "<<dtime_s<<" seconds old."<<std::endl;
+
+ // Activate stored objects
+ activateObjects(block);
+
+ // Run node metadata
+ bool changed = block->m_node_metadata.step((float)dtime_s);
+ if(changed)
+ {
+ MapEditEvent event;
+ event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
+ event.p = p;
+ m_map->dispatchEvent(&event);
+ }
+
+ // TODO: Do something
+ // TODO: Implement usage of ActiveBlockModifier
+
+ // Here's a quick demonstration
+ v3s16 p0;
+ for(p0.X=0; p0.X<MAP_BLOCKSIZE; p0.X++)
+ for(p0.Y=0; p0.Y<MAP_BLOCKSIZE; p0.Y++)
+ for(p0.Z=0; p0.Z<MAP_BLOCKSIZE; p0.Z++)
+ {
+ v3s16 p = p0 + block->getPosRelative();
+ MapNode n = block->getNodeNoEx(p0);
+ // Test something:
+ // Convert all mud under proper day lighting to grass
+ if(n.d == CONTENT_MUD)
+ {
+ if(dtime_s > 300)
+ {
+ MapNode n_top = block->getNodeNoEx(p0+v3s16(0,1,0));
+ if(content_features(n_top.d).air_equivalent &&
+ n_top.getLight(LIGHTBANK_DAY) >= 13)
+ {
+ n.d = CONTENT_GRASS;
+ m_map->addNodeWithEvent(p, n);
+ }
+ }
+ }
+ }
+ }
}
/*
- Remove (m_removed && m_known_by_count==0) objects
+ Mess around in active blocks
*/
+ if(m_active_blocks_nodemetadata_interval.step(dtime, 1.0))
{
- core::list<u16> objects_to_remove;
- for(core::map<u16, ServerActiveObject*>::Iterator
- i = m_active_objects.getIterator();
+ float dtime = 1.0;
+
+ for(core::map<v3s16, bool>::Iterator
+ i = m_active_blocks.m_list.getIterator();
i.atEnd()==false; i++)
{
- u16 id = i.getNode()->getKey();
- ServerActiveObject* obj = i.getNode()->getValue();
- // This shouldn't happen but check it
- if(obj == NULL)
- {
- dstream<<"WARNING: NULL object found in ServerEnvironment"
- <<" while finding removed objects. id="<<id<<std::endl;
- // Id to be removed from m_active_objects
- objects_to_remove.push_back(id);
+ v3s16 p = i.getNode()->getKey();
+
+ /*dstream<<"Server: Block ("<<p.X<<","<<p.Y<<","<<p.Z
+ <<") being handled"<<std::endl;*/
+
+ MapBlock *block = m_map->getBlockNoCreateNoEx(p);
+ if(block==NULL)
continue;
+
+ // Set current time as timestamp
+ block->setTimestamp(m_game_time);
+
+ // Run node metadata
+ bool changed = block->m_node_metadata.step(dtime);
+ if(changed)
+ {
+ MapEditEvent event;
+ event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
+ event.p = p;
+ m_map->dispatchEvent(&event);
}
- else
+ }
+ }
+ if(m_active_blocks_test_interval.step(dtime, 10.0))
+ {
+ //float dtime = 10.0;
+
+ for(core::map<v3s16, bool>::Iterator
+ i = m_active_blocks.m_list.getIterator();
+ i.atEnd()==false; i++)
+ {
+ v3s16 p = i.getNode()->getKey();
+
+ /*dstream<<"Server: Block ("<<p.X<<","<<p.Y<<","<<p.Z
+ <<") being handled"<<std::endl;*/
+
+ MapBlock *block = m_map->getBlockNoCreateNoEx(p);
+ if(block==NULL)
+ continue;
+
+ // Set current time as timestamp
+ block->setTimestamp(m_game_time);
+
+ /*
+ Do stuff!
+
+ Note that map modifications should be done using the event-
+ making map methods so that the server gets information
+ about them.
+
+ Reading can be done quickly directly from the block.
+
+ Everything should bind to inside this single content
+ searching loop to keep things fast.
+ */
+ // TODO: Implement usage of ActiveBlockModifier
+
+ v3s16 p0;
+ for(p0.X=0; p0.X<MAP_BLOCKSIZE; p0.X++)
+ for(p0.Y=0; p0.Y<MAP_BLOCKSIZE; p0.Y++)
+ for(p0.Z=0; p0.Z<MAP_BLOCKSIZE; p0.Z++)
{
- // If not m_removed, don't remove.
- if(obj->m_removed == false)
- continue;
- // Delete
- delete obj;
- // Id to be removed from m_active_objects
- objects_to_remove.push_back(id);
+ v3s16 p = p0 + block->getPosRelative();
+ MapNode n = block->getNodeNoEx(p0);
+
+ /*
+ Test something:
+ Convert mud under proper lighting to grass
+ */
+ if(n.d == CONTENT_MUD)
+ {
+ if(myrand()%20 == 0)
+ {
+ MapNode n_top = block->getNodeNoEx(p0+v3s16(0,1,0));
+ if(content_features(n_top.d).air_equivalent &&
+ n_top.getLightBlend(getDayNightRatio()) >= 13)
+ {
+ n.d = CONTENT_GRASS;
+ m_map->addNodeWithEvent(p, n);
+ }
+ }
+ }
}
}
- // Remove references from m_active_objects
- for(core::list<u16>::Iterator i = objects_to_remove.begin();
- i != objects_to_remove.end(); i++)
+ }
+
+ /*
+ Step active objects
+ */
+ {
+ //TimeTaker timer("Step active objects");
+
+ // This helps the objects to send data at the same time
+ bool send_recommended = false;
+ m_send_recommended_timer += dtime;
+ if(m_send_recommended_timer > 0.15)
+ {
+ m_send_recommended_timer = 0;
+ send_recommended = true;
+ }
+
+ for(core::map<u16, ServerActiveObject*>::Iterator
+ i = m_active_objects.getIterator();
+ i.atEnd()==false; i++)
{
- m_active_objects.remove(*i);
+ ServerActiveObject* obj = i.getNode()->getValue();
+ // Don't step if is to be removed or stored statically
+ if(obj->m_removed || obj->m_pending_deactivation)
+ continue;
+ // Step object, putting messages directly to the queue
+ obj->step(dtime, m_active_object_messages, send_recommended);
}
}
+
+ /*
+ Manage active objects
+ */
+ if(m_object_management_interval.step(dtime, 0.5))
+ {
+ /*
+ Remove objects that satisfy (m_removed && m_known_by_count==0)
+ */
+ removeRemovedObjects();
+ }
+
+ if(g_settings.getBool("enable_experimental"))
+ {
/*
TEST CODE
*/
+#if 1
m_random_spawn_timer -= dtime;
if(m_random_spawn_timer < 0)
{
- m_random_spawn_timer += myrand_range(2.0, 20.0);
-
- /*TestSAO *obj = new TestSAO(0,
- v3f(myrand_range(-2*BS,2*BS), BS*5, myrand_range(-2*BS,2*BS)));*/
+ //m_random_spawn_timer += myrand_range(2.0, 20.0);
+ //m_random_spawn_timer += 2.0;
+ m_random_spawn_timer += 200.0;
/*
Find some position
@@ -506,35 +938,16 @@ void ServerEnvironment::step(float dtime)
);
/*
- Create a LuaSAO (ServerActiveObject)
+ Create a ServerActiveObject
*/
- LuaSAO *obj = new LuaSAO(this, 0, pos);
-
- /*
- Select a random type for it
- */
- std::string objectdir = porting::getDataPath("scripts/objects");
- std::vector<fs::DirListNode> dirlist = fs::GetDirListing(objectdir);
- if(dirlist.size() > 0)
- {
- u32 selected_i = myrand_range(0, dirlist.size()-1);
- std::string selected_name = "";
- selected_name = dirlist[selected_i].name;
- /*for(u32 i=0; i<dirlist.size(); i++)*/
-
- dstream<<"ServerEnvironment: Selected script name \""<<selected_name
- <<"\" for new lua object"<<std::endl;
-
- /*
- Load the scripts for the type
- */
- obj->initializeFromNothing(selected_name.c_str());
-
- // Add the object to the environment
- addActiveObject(obj);
- }
+ //TestSAO *obj = new TestSAO(this, 0, pos);
+ //ServerActiveObject *obj = new ItemSAO(this, 0, pos, "CraftItem Stick 1");
+ //ServerActiveObject *obj = new RatSAO(this, 0, pos);
+ ServerActiveObject *obj = new Oerkki1SAO(this, 0, pos);
+ addActiveObject(obj);
}
+#endif
} // enable_experimental
}
@@ -602,9 +1015,29 @@ u16 ServerEnvironment::addActiveObject(ServerActiveObject *object)
delete object;
return 0;
}
- dstream<<"INGO: ServerEnvironment::addActiveObject(): "
- <<"added (id="<<object->getId()<<")"<<std::endl;
+ /*dstream<<"INGO: ServerEnvironment::addActiveObject(): "
+ <<"added (id="<<object->getId()<<")"<<std::endl;*/
+
m_active_objects.insert(object->getId(), object);
+
+ // Add static object to active static list of the block
+ v3f objectpos = object->getBasePosition();
+ std::string staticdata = object->getStaticData();
+ StaticObject s_obj(object->getType(), objectpos, staticdata);
+ // Add to the block where the object is located in
+ v3s16 blockpos = getNodeBlockPos(floatToInt(objectpos, BS));
+ MapBlock *block = m_map->getBlockNoCreateNoEx(blockpos);
+ if(block)
+ {
+ block->m_static_objects.m_active.insert(object->getId(), s_obj);
+ object->m_static_exists = true;
+ object->m_static_block = blockpos;
+ }
+ else{
+ dstream<<"WARNING: Server: Could not find a block for "
+ <<"storing newly added static active object"<<std::endl;
+ }
+
return object->getId();
}
@@ -704,6 +1137,226 @@ ActiveObjectMessage ServerEnvironment::getActiveObjectMessage()
return m_active_object_messages.pop_front();
}
+/*
+ ************ Private methods *************
+*/
+
+/*
+ Remove objects that satisfy (m_removed && m_known_by_count==0)
+*/
+void ServerEnvironment::removeRemovedObjects()
+{
+ core::list<u16> objects_to_remove;
+ for(core::map<u16, ServerActiveObject*>::Iterator
+ i = m_active_objects.getIterator();
+ i.atEnd()==false; i++)
+ {
+ u16 id = i.getNode()->getKey();
+ ServerActiveObject* obj = i.getNode()->getValue();
+ // This shouldn't happen but check it
+ if(obj == NULL)
+ {
+ dstream<<"WARNING: NULL object found in ServerEnvironment"
+ <<" while finding removed objects. id="<<id<<std::endl;
+ // Id to be removed from m_active_objects
+ objects_to_remove.push_back(id);
+ continue;
+ }
+
+ /*
+ We will delete objects that are marked as removed or thatare
+ waiting for deletion after deactivation
+ */
+ if(obj->m_removed == false && obj->m_pending_deactivation == false)
+ continue;
+
+ /*
+ Delete static data from block if is marked as removed
+ */
+ if(obj->m_static_exists && obj->m_removed)
+ {
+ MapBlock *block = m_map->getBlockNoCreateNoEx(obj->m_static_block);
+ if(block)
+ {
+ block->m_static_objects.remove(id);
+ block->setChangedFlag();
+ }
+ }
+
+ // If m_known_by_count > 0, don't actually remove.
+ if(obj->m_known_by_count > 0)
+ continue;
+
+ // Delete
+ delete obj;
+ // Id to be removed from m_active_objects
+ objects_to_remove.push_back(id);
+ }
+ // Remove references from m_active_objects
+ for(core::list<u16>::Iterator i = objects_to_remove.begin();
+ i != objects_to_remove.end(); i++)
+ {
+ m_active_objects.remove(*i);
+ }
+}
+
+/*
+ Convert stored objects from blocks near the players to active.
+*/
+void ServerEnvironment::activateObjects(MapBlock *block)
+{
+ if(block==NULL)
+ return;
+ // Ignore if no stored objects (to not set changed flag)
+ if(block->m_static_objects.m_stored.size() == 0)
+ return;
+ // A list for objects that couldn't be converted to static for some
+ // reason. They will be stored back.
+ core::list<StaticObject> new_stored;
+ // Loop through stored static objects
+ for(core::list<StaticObject>::Iterator
+ i = block->m_static_objects.m_stored.begin();
+ i != block->m_static_objects.m_stored.end(); i++)
+ {
+ /*dstream<<"INFO: Server: Creating an active object from "
+ <<"static data"<<std::endl;*/
+ StaticObject &s_obj = *i;
+ // Create an active object from the data
+ ServerActiveObject *obj = ServerActiveObject::create
+ (s_obj.type, this, 0, s_obj.pos, s_obj.data);
+ // If couldn't create object, store static data back.
+ if(obj==NULL)
+ {
+ new_stored.push_back(s_obj);
+ continue;
+ }
+ // This will also add the object to the active static list
+ addActiveObject(obj);
+ //u16 id = addActiveObject(obj);
+ }
+ // Clear stored list
+ block->m_static_objects.m_stored.clear();
+ // Add leftover failed stuff to stored list
+ for(core::list<StaticObject>::Iterator
+ i = new_stored.begin();
+ i != new_stored.end(); i++)
+ {
+ StaticObject &s_obj = *i;
+ block->m_static_objects.m_stored.push_back(s_obj);
+ }
+ // Block has been modified
+ block->setChangedFlag();
+}
+
+/*
+ Convert objects that are not in active blocks to static.
+
+ If m_known_by_count != 0, active object is not deleted, but static
+ data is still updated.
+
+ If force_delete is set, active object is deleted nevertheless. It
+ shall only be set so in the destructor of the environment.
+*/
+void ServerEnvironment::deactivateFarObjects(bool force_delete)
+{
+ core::list<u16> objects_to_remove;
+ for(core::map<u16, ServerActiveObject*>::Iterator
+ i = m_active_objects.getIterator();
+ i.atEnd()==false; i++)
+ {
+ ServerActiveObject* obj = i.getNode()->getValue();
+ u16 id = i.getNode()->getKey();
+ v3f objectpos = obj->getBasePosition();
+
+ // This shouldn't happen but check it
+ if(obj == NULL)
+ {
+ dstream<<"WARNING: NULL object found in ServerEnvironment"
+ <<std::endl;
+ assert(0);
+ continue;
+ }
+
+ // The block in which the object resides in
+ v3s16 blockpos_o = getNodeBlockPos(floatToInt(objectpos, BS));
+
+ // If block is active, don't remove
+ if(m_active_blocks.contains(blockpos_o))
+ continue;
+
+ /*
+ Update the static data
+ */
+
+ // Delete old static object
+ MapBlock *oldblock = NULL;
+ if(obj->m_static_exists)
+ {
+ MapBlock *block = m_map->getBlockNoCreateNoEx
+ (obj->m_static_block);
+ if(block)
+ {
+ block->m_static_objects.remove(id);
+ oldblock = block;
+ }
+ }
+ // Create new static object
+ std::string staticdata = obj->getStaticData();
+ StaticObject s_obj(obj->getType(), objectpos, staticdata);
+ // Add to the block where the object is located in
+ v3s16 blockpos = getNodeBlockPos(floatToInt(objectpos, BS));
+ MapBlock *block = m_map->getBlockNoCreateNoEx(blockpos);
+ if(block)
+ {
+ block->m_static_objects.insert(0, s_obj);
+ block->setChangedFlag();
+ obj->m_static_exists = true;
+ obj->m_static_block = block->getPos();
+ }
+ // If not possible, add back to previous block
+ else if(oldblock)
+ {
+ oldblock->m_static_objects.insert(0, s_obj);
+ oldblock->setChangedFlag();
+ obj->m_static_exists = true;
+ obj->m_static_block = oldblock->getPos();
+ }
+ else{
+ dstream<<"WARNING: Server: Could not find a block for "
+ <<"storing static object"<<std::endl;
+ obj->m_static_exists = false;
+ continue;
+ }
+
+ /*
+ Delete active object if not known by some client,
+ else set pending deactivation
+ */
+
+ // If known by some client, don't delete.
+ if(obj->m_known_by_count > 0 && force_delete == false)
+ {
+ obj->m_pending_deactivation = true;
+ continue;
+ }
+
+ /*dstream<<"INFO: Server: Stored static data. Deleting object."
+ <<std::endl;*/
+ // Delete active object
+ delete obj;
+ // Id to be removed from m_active_objects
+ objects_to_remove.push_back(id);
+ }
+
+ // Remove references from m_active_objects
+ for(core::list<u16>::Iterator i = objects_to_remove.begin();
+ i != objects_to_remove.end(); i++)
+ {
+ m_active_objects.remove(*i);
+ }
+}
+
+
#ifndef SERVER
/*
@@ -765,17 +1418,21 @@ void ClientEnvironment::step(float dtime)
bool footprints = g_settings.getBool("footprints");
{
- //TimeTaker timer("Client m_map->timerUpdate()", g_device);
+ //TimeTaker timer("Client m_map->timerUpdate()");
m_map->timerUpdate(dtime);
}
-
+
+ // Get local player
+ LocalPlayer *lplayer = getLocalPlayer();
+ assert(lplayer);
+ // collision info queue
+ core::list<CollisionInfo> player_collisions;
+
/*
Get the speed the player is going
*/
f32 player_speed = 0.001; // just some small value
- LocalPlayer *lplayer = getLocalPlayer();
- if(lplayer)
- player_speed = lplayer->getSpeed().getLength();
+ player_speed = lplayer->getSpeed().getLength();
/*
Maximum position increment
@@ -828,20 +1485,18 @@ void ClientEnvironment::step(float dtime)
*/
{
- Player *player = getLocalPlayer();
-
- v3f playerpos = player->getPosition();
+ v3f lplayerpos = lplayer->getPosition();
// Apply physics
if(free_move == false)
{
// Gravity
- v3f speed = player->getSpeed();
- if(player->swimming_up == false)
+ v3f speed = lplayer->getSpeed();
+ if(lplayer->swimming_up == false)
speed.Y -= 9.81 * BS * dtime_part * 2;
// Water resistance
- if(player->in_water_stable || player->in_water)
+ if(lplayer->in_water_stable || lplayer->in_water)
{
f32 max_down = 2.0*BS;
if(speed.Y < -max_down) speed.Y = -max_down;
@@ -853,19 +1508,47 @@ void ClientEnvironment::step(float dtime)
}
}
- player->setSpeed(speed);
+ lplayer->setSpeed(speed);
}
/*
- Move the player.
+ Move the lplayer.
This also does collision detection.
*/
- player->move(dtime_part, *m_map, position_max_increment);
+ lplayer->move(dtime_part, *m_map, position_max_increment,
+ &player_collisions);
}
}
while(dtime_downcount > 0.001);
//std::cout<<"Looped "<<loopcount<<" times."<<std::endl;
+
+ for(core::list<CollisionInfo>::Iterator
+ i = player_collisions.begin();
+ i != player_collisions.end(); i++)
+ {
+ CollisionInfo &info = *i;
+ if(info.t == COLLISION_FALL)
+ {
+ //f32 tolerance = BS*10; // 2 without damage
+ f32 tolerance = BS*12; // 3 without damage
+ f32 factor = 1;
+ if(info.speed > tolerance)
+ {
+ f32 damage_f = (info.speed - tolerance)/BS*factor;
+ u16 damage = (u16)(damage_f+0.5);
+ if(lplayer->hp > damage)
+ lplayer->hp -= damage;
+ else
+ lplayer->hp = 0;
+
+ ClientEnvEvent event;
+ event.type = CEE_PLAYER_DAMAGE;
+ event.player_damage.amount = damage;
+ m_client_event_queue.push_back(event);
+ }
+ }
+ }
/*
Stuff that can be done in an arbitarily large dtime
@@ -890,7 +1573,7 @@ void ClientEnvironment::step(float dtime)
// Get node at head
v3s16 p = floatToInt(playerpos + v3f(0,BS+BS/2,0), BS);
MapNode n = m_map->getNode(p);
- light = n.getLightBlend(m_daynight_ratio);
+ light = n.getLightBlend(getDayNightRatio());
}
catch(InvalidPositionException &e) {}
player->updateLight(light);
@@ -914,7 +1597,8 @@ void ClientEnvironment::step(float dtime)
{
v3s16 p_blocks = getNodeBlockPos(bottompos);
MapBlock *b = m_map->getBlockNoCreate(p_blocks);
- b->updateMesh(m_daynight_ratio);
+ //b->updateMesh(getDayNightRatio());
+ b->setMeshExpired(true);
}
}
}
@@ -925,21 +1609,33 @@ void ClientEnvironment::step(float dtime)
}
/*
- Step active objects
+ Step active objects and update lighting of them
*/
+
for(core::map<u16, ClientActiveObject*>::Iterator
i = m_active_objects.getIterator();
i.atEnd()==false; i++)
{
ClientActiveObject* obj = i.getNode()->getValue();
// Step object
- obj->step(dtime);
+ obj->step(dtime, this);
+ // Update lighting
+ //u8 light = LIGHT_MAX;
+ u8 light = 0;
+ try{
+ // Get node at head
+ v3s16 p = obj->getLightPosition();
+ MapNode n = m_map->getNode(p);
+ light = n.getLightBlend(getDayNightRatio());
+ }
+ catch(InvalidPositionException &e) {}
+ obj->updateLight(light);
}
}
void ClientEnvironment::updateMeshes(v3s16 blockpos)
{
- m_map->updateMeshes(blockpos, m_daynight_ratio);
+ m_map->updateMeshes(blockpos, getDayNightRatio());
}
void ClientEnvironment::expireMeshes(bool only_daynight_diffed)
@@ -1066,6 +1762,61 @@ void ClientEnvironment::processActiveObjectMessage(u16 id,
obj->processMessage(data);
}
+/*
+ Callbacks for activeobjects
+*/
+
+void ClientEnvironment::damageLocalPlayer(u8 damage)
+{
+ LocalPlayer *lplayer = getLocalPlayer();
+ assert(lplayer);
+
+ if(lplayer->hp > damage)
+ lplayer->hp -= damage;
+ else
+ lplayer->hp = 0;
+
+ ClientEnvEvent event;
+ event.type = CEE_PLAYER_DAMAGE;
+ event.player_damage.amount = damage;
+ m_client_event_queue.push_back(event);
+}
+
+/*
+ Client likes to call these
+*/
+
+void ClientEnvironment::getActiveObjects(v3f origin, f32 max_d,
+ core::array<DistanceSortedActiveObject> &dest)
+{
+ for(core::map<u16, ClientActiveObject*>::Iterator
+ i = m_active_objects.getIterator();
+ i.atEnd()==false; i++)
+ {
+ ClientActiveObject* obj = i.getNode()->getValue();
+
+ f32 d = (obj->getPosition() - origin).getLength();
+
+ if(d > max_d)
+ continue;
+
+ DistanceSortedActiveObject dso(obj, d);
+
+ dest.push_back(dso);
+ }
+}
+
+ClientEnvEvent ClientEnvironment::getClientEvent()
+{
+ if(m_client_event_queue.size() == 0)
+ {
+ ClientEnvEvent event;
+ event.type = CEE_NONE;
+ return event;
+ }
+ return m_client_event_queue.pop_front();
+}
+
#endif // #ifndef SERVER
diff --git a/src/environment.h b/src/environment.h
index b4159372a..b4f2a64ca 100644
--- a/src/environment.h
+++ b/src/environment.h
@@ -1,6 +1,6 @@
/*
Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -35,6 +35,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "player.h"
#include "map.h"
#include <ostream>
+#include "utility.h"
class Environment
{
@@ -63,14 +64,52 @@ public:
core::list<Player*> getPlayers(bool ignore_disconnected);
void printPlayers(std::ostream &o);
- void setDayNightRatio(u32 r);
+ //void setDayNightRatio(u32 r);
u32 getDayNightRatio();
+
+ // 0-23999
+ virtual void setTimeOfDay(u32 time)
+ {
+ m_time_of_day = time;
+ }
+
+ u32 getTimeOfDay()
+ {
+ return m_time_of_day;
+ }
protected:
// peer_ids in here should be unique, except that there may be many 0s
core::list<Player*> m_players;
// Brightness
- u32 m_daynight_ratio;
+ //u32 m_daynight_ratio;
+ // Time of day in milli-hours (0-23999); determines day and night
+ u32 m_time_of_day;
+};
+
+/*
+ List of active blocks, used by ServerEnvironment
+*/
+
+class ActiveBlockList
+{
+public:
+ void update(core::list<v3s16> &active_positions,
+ s16 radius,
+ core::map<v3s16, bool> &blocks_removed,
+ core::map<v3s16, bool> &blocks_added);
+
+ bool contains(v3s16 p){
+ return (m_list.find(p) != NULL);
+ }
+
+ void clear(){
+ m_list.clear();
+ }
+
+ core::map<v3s16, bool> m_list;
+
+private:
};
/*
@@ -82,6 +121,7 @@ protected:
#include "serverobject.h"
class Server;
+class ActiveBlockModifier;
class ServerEnvironment : public Environment
{
@@ -105,18 +145,28 @@ public:
}
void step(f32 dtime);
-
+
+ /*
+ Save players
+ */
void serializePlayers(const std::string &savedir);
void deSerializePlayers(const std::string &savedir);
/*
+ Save and load time of day and game timer
+ */
+ void saveMeta(const std::string &savedir);
+ void loadMeta(const std::string &savedir);
+
+ /*
ActiveObjects
+ -------------------------------------------
*/
ServerActiveObject* getActiveObject(u16 id);
/*
- Adds an active object to the environment.
+ Add an active object to the environment.
Environment handles deletion of object.
Object may be deleted by environment immediately.
If id of object is 0, assigns a free id to it.
@@ -126,7 +176,7 @@ public:
u16 addActiveObject(ServerActiveObject *object);
/*
- Finds out what new objects have been added to
+ Find out what new objects have been added to
inside a radius around a position
*/
void getAddedActiveObjects(v3s16 pos, s16 radius,
@@ -134,7 +184,7 @@ public:
core::map<u16, bool> &added_objects);
/*
- Finds out what new objects have been removed from
+ Find out what new objects have been removed from
inside a radius around a position
*/
void getRemovedActiveObjects(v3s16 pos, s16 radius,
@@ -142,17 +192,89 @@ public:
core::map<u16, bool> &removed_objects);
/*
- Gets the next message emitted by some active object.
+ Get the next message emitted by some active object.
Returns a message with id=0 if no messages are available.
*/
ActiveObjectMessage getActiveObjectMessage();
-
+
+ /*
+ ActiveBlockModifiers
+ -------------------------------------------
+ */
+
+ void addActiveBlockModifier(ActiveBlockModifier *abm);
+
private:
+ /*
+ Remove all objects that satisfy (m_removed && m_known_by_count==0)
+ */
+ void removeRemovedObjects();
+
+ /*
+ Convert stored objects from block to active
+ */
+ void activateObjects(MapBlock *block);
+
+ /*
+ Convert objects that are not in active blocks to static.
+
+ If m_known_by_count != 0, active object is not deleted, but static
+ data is still updated.
+
+ If force_delete is set, active object is deleted nevertheless. It
+ shall only be set so in the destructor of the environment.
+ */
+ void deactivateFarObjects(bool force_delete);
+
+ /*
+ Member variables
+ */
+
+ // The map
ServerMap *m_map;
+ // Pointer to server (which is handling this environment)
Server *m_server;
+ // Active object list
core::map<u16, ServerActiveObject*> m_active_objects;
+ // Outgoing network message buffer for active objects
Queue<ActiveObjectMessage> m_active_object_messages;
- float m_random_spawn_timer;
+ // Some timers
+ float m_random_spawn_timer; // used for experimental code
+ float m_send_recommended_timer;
+ IntervalLimiter m_object_management_interval;
+ // List of active blocks
+ ActiveBlockList m_active_blocks;
+ IntervalLimiter m_active_blocks_management_interval;
+ IntervalLimiter m_active_blocks_test_interval;
+ IntervalLimiter m_active_blocks_nodemetadata_interval;
+ // Time from the beginning of the game in seconds.
+ // Incremented in step().
+ u32 m_game_time;
+ // A helper variable for incrementing the latter
+ float m_game_time_fraction_counter;
+};
+
+/*
+ Active block modifier interface.
+
+ These are fed into ServerEnvironment at initialization time;
+ ServerEnvironment handles deleting them.
+*/
+
+class ActiveBlockModifier
+{
+public:
+ ActiveBlockModifier(){};
+ virtual ~ActiveBlockModifier(){};
+
+ //virtual core::list<u8> update(ServerEnvironment *env) = 0;
+ virtual u32 getTriggerContentCount(){ return 1;}
+ virtual u8 getTriggerContent(u32 i) = 0;
+ virtual float getActiveInterval() = 0;
+ // chance of (1 / return value), 0 is disallowed
+ virtual u32 getActiveChance() = 0;
+ // This is called usually at interval for 1/chance of the nodes
+ virtual void triggerEvent(ServerEnvironment *env, v3s16 p) = 0;
};
#ifndef SERVER
@@ -167,6 +289,24 @@ private:
Client uses an environment mutex.
*/
+enum ClientEnvEventType
+{
+ CEE_NONE,
+ CEE_PLAYER_DAMAGE
+};
+
+struct ClientEnvEvent
+{
+ ClientEnvEventType type;
+ union {
+ struct{
+ } none;
+ struct{
+ u8 amount;
+ } player_damage;
+ };
+};
+
class ClientEnvironment : public Environment
{
public:
@@ -191,6 +331,20 @@ public:
void updateMeshes(v3s16 blockpos);
void expireMeshes(bool only_daynight_diffed);
+ void setTimeOfDay(u32 time)
+ {
+ u32 old_dr = getDayNightRatio();
+
+ Environment::setTimeOfDay(time);
+
+ if(getDayNightRatio() != old_dr)
+ {
+ dout_client<<DTIME<<"ClientEnvironment: DayNightRatio changed"
+ <<" -> expiring meshes"<<std::endl;
+ expireMeshes(true);
+ }
+ }
+
/*
ActiveObjects
*/
@@ -212,10 +366,28 @@ public:
void processActiveObjectMessage(u16 id, const std::string &data);
+ /*
+ Callbacks for activeobjects
+ */
+
+ void damageLocalPlayer(u8 damage);
+
+ /*
+ Client likes to call these
+ */
+
+ // Get all nearby objects
+ void getActiveObjects(v3f origin, f32 max_d,
+ core::array<DistanceSortedActiveObject> &dest);
+
+ // Get event from queue. CEE_NONE is returned if queue is empty.
+ ClientEnvEvent getClientEvent();
+
private:
ClientMap *m_map;
scene::ISceneManager *m_smgr;
core::map<u16, ClientActiveObject*> m_active_objects;
+ Queue<ClientEnvEvent> m_client_event_queue;
};
#endif
diff --git a/src/filesys.cpp b/src/filesys.cpp
index 287090e8a..8248a13d4 100644
--- a/src/filesys.cpp
+++ b/src/filesys.cpp
@@ -290,5 +290,24 @@ bool RecursiveDeleteContent(std::string path)
return true;
}
+bool CreateAllDirs(std::string path)
+{
+
+ size_t pos;
+ std::vector<std::string> tocreate;
+ std::string basepath = path;
+ while(!PathExists(basepath))
+ {
+ tocreate.push_back(basepath);
+ pos = basepath.rfind('/');
+ if(pos == std::string::npos)
+ return false;
+ basepath = basepath.substr(0,pos);
+ }
+ for(int i=tocreate.size()-1;i>=0;i--)
+ CreateDir(tocreate[i]);
+ return true;
+}
+
} // namespace fs
diff --git a/src/filesys.h b/src/filesys.h
index 4dd90b84e..b74b34f3d 100644
--- a/src/filesys.h
+++ b/src/filesys.h
@@ -38,6 +38,9 @@ std::vector<DirListNode> GetDirListing(std::string path);
// Returns true if already exists
bool CreateDir(std::string path);
+// Create all directories on the given path that don't already exist.
+bool CreateAllDirs(std::string path);
+
bool PathExists(std::string path);
// Only pass full paths to this one. True on success.
diff --git a/src/game.cpp b/src/game.cpp
new file mode 100644
index 000000000..e8e6cb71f
--- /dev/null
+++ b/src/game.cpp
@@ -0,0 +1,2153 @@
+/*
+Minetest-c55
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "common_irrlicht.h"
+#include "game.h"
+#include "client.h"
+#include "server.h"
+#include "guiPauseMenu.h"
+#include "guiPasswordChange.h"
+#include "guiInventoryMenu.h"
+#include "guiTextInputMenu.h"
+#include "guiFurnaceMenu.h"
+#include "materials.h"
+#include "config.h"
+#include "clouds.h"
+#include "keycode.h"
+
+/*
+ Setting this to 1 enables a special camera mode that forces
+ the renderers to think that the camera statically points from
+ the starting place to a static direction.
+
+ This allows one to move around with the player and see what
+ is actually drawn behind solid things and behind the player.
+*/
+#define FIELD_OF_VIEW_TEST 0
+
+
+MapDrawControl draw_control;
+
+// Chat data
+struct ChatLine
+{
+ ChatLine():
+ age(0.0)
+ {
+ }
+ ChatLine(const std::wstring &a_text):
+ age(0.0),
+ text(a_text)
+ {
+ }
+ float age;
+ std::wstring text;
+};
+
+/*
+ Inventory stuff
+*/
+
+// Inventory actions from the menu are buffered here before sending
+Queue<InventoryAction*> inventory_action_queue;
+// This is a copy of the inventory that the client's environment has
+Inventory local_inventory;
+
+u16 g_selected_item = 0;
+
+/*
+ Text input system
+*/
+
+struct TextDestSign : public TextDest
+{
+ TextDestSign(v3s16 blockpos, s16 id, Client *client)
+ {
+ m_blockpos = blockpos;
+ m_id = id;
+ m_client = client;
+ }
+ void gotText(std::wstring text)
+ {
+ std::string ntext = wide_to_narrow(text);
+ dstream<<"Changing text of a sign object: "
+ <<ntext<<std::endl;
+ m_client->sendSignText(m_blockpos, m_id, ntext);
+ }
+
+ v3s16 m_blockpos;
+ s16 m_id;
+ Client *m_client;
+};
+
+struct TextDestChat : public TextDest
+{
+ TextDestChat(Client *client)
+ {
+ m_client = client;
+ }
+ void gotText(std::wstring text)
+ {
+ // Discard empty line
+ if(text == L"")
+ return;
+
+ // Parse command (server command starts with "/#")
+ if(text[0] == L'/' && text[1] != L'#')
+ {
+ std::wstring reply = L"Local: ";
+
+ reply += L"Local commands not yet supported. "
+ L"Server prefix is \"/#\".";
+
+ m_client->addChatMessage(reply);
+ return;
+ }
+
+ // Send to others
+ m_client->sendChatMessage(text);
+ // Show locally
+ m_client->addChatMessage(text);
+ }
+
+ Client *m_client;
+};
+
+struct TextDestSignNode : public TextDest
+{
+ TextDestSignNode(v3s16 p, Client *client)
+ {
+ m_p = p;
+ m_client = client;
+ }
+ void gotText(std::wstring text)
+ {
+ std::string ntext = wide_to_narrow(text);
+ dstream<<"Changing text of a sign node: "
+ <<ntext<<std::endl;
+ m_client->sendSignNodeText(m_p, ntext);
+ }
+
+ v3s16 m_p;
+ Client *m_client;
+};
+
+/*
+ Render distance feedback loop
+*/
+void updateViewingRange(f32 frametime_in, Client *client)
+{
+ if(draw_control.range_all == true)
+ return;
+
+ static f32 added_frametime = 0;
+ static s16 added_frames = 0;
+
+ added_frametime += frametime_in;
+ added_frames += 1;
+
+ // Actually this counter kind of sucks because frametime is busytime
+ static f32 counter = 0;
+ counter -= frametime_in;
+ if(counter > 0)
+ return;
+ //counter = 0.1;
+ counter = 0.2;
+
+ /*dstream<<__FUNCTION_NAME
+ <<": Collected "<<added_frames<<" frames, total of "
+ <<added_frametime<<"s."<<std::endl;*/
+
+ /*dstream<<"draw_control.blocks_drawn="
+ <<draw_control.blocks_drawn
+ <<", draw_control.blocks_would_have_drawn="
+ <<draw_control.blocks_would_have_drawn
+ <<std::endl;*/
+
+ float range_min = g_settings.getS16("viewing_range_nodes_min");
+ float range_max = g_settings.getS16("viewing_range_nodes_max");
+
+ // Limit minimum to keep the feedback loop stable
+ if(range_min < 5)
+ range_min = 5;
+
+ draw_control.wanted_min_range = range_min;
+ //draw_control.wanted_max_blocks = (1.5*draw_control.blocks_drawn)+1;
+ draw_control.wanted_max_blocks = (1.5*draw_control.blocks_would_have_drawn)+1;
+ if(draw_control.wanted_max_blocks < 10)
+ draw_control.wanted_max_blocks = 10;
+
+ float block_draw_ratio = 1.0;
+ if(draw_control.blocks_would_have_drawn != 0)
+ {
+ block_draw_ratio = (float)draw_control.blocks_drawn
+ / (float)draw_control.blocks_would_have_drawn;
+ }
+
+ // Calculate the average frametime in the case that all wanted
+ // blocks had been drawn
+ f32 frametime = added_frametime / added_frames / block_draw_ratio;
+
+ added_frametime = 0.0;
+ added_frames = 0;
+
+ float wanted_fps = g_settings.getFloat("wanted_fps");
+ float wanted_frametime = 1.0 / wanted_fps;
+
+ f32 wanted_frametime_change = wanted_frametime - frametime;
+ //dstream<<"wanted_frametime_change="<<wanted_frametime_change<<std::endl;
+
+ // If needed frametime change is small, just return
+ if(fabs(wanted_frametime_change) < wanted_frametime*0.4)
+ {
+ //dstream<<"ignoring small wanted_frametime_change"<<std::endl;
+ return;
+ }
+
+ float range = draw_control.wanted_range;
+ float new_range = range;
+
+ static s16 range_old = 0;
+ static f32 frametime_old = 0;
+
+ float d_range = range - range_old;
+ f32 d_frametime = frametime - frametime_old;
+ // A sane default of 30ms per 50 nodes of range
+ static f32 time_per_range = 30. / 50;
+ if(d_range != 0)
+ {
+ time_per_range = d_frametime / d_range;
+ }
+
+ // The minimum allowed calculated frametime-range derivative:
+ // Practically this sets the maximum speed of changing the range.
+ // The lower this value, the higher the maximum changing speed.
+ // A low value here results in wobbly range (0.001)
+ // A high value here results in slow changing range (0.0025)
+ // SUGG: This could be dynamically adjusted so that when
+ // the camera is turning, this is lower
+ //float min_time_per_range = 0.0015;
+ float min_time_per_range = 0.0010;
+ //float min_time_per_range = 0.05 / range;
+ if(time_per_range < min_time_per_range)
+ {
+ time_per_range = min_time_per_range;
+ //dstream<<"time_per_range="<<time_per_range<<" (min)"<<std::endl;
+ }
+ else
+ {
+ //dstream<<"time_per_range="<<time_per_range<<std::endl;
+ }
+
+ f32 wanted_range_change = wanted_frametime_change / time_per_range;
+ // Dampen the change a bit to kill oscillations
+ //wanted_range_change *= 0.9;
+ //wanted_range_change *= 0.75;
+ wanted_range_change *= 0.5;
+ //dstream<<"wanted_range_change="<<wanted_range_change<<std::endl;
+
+ // If needed range change is very small, just return
+ if(fabs(wanted_range_change) < 0.001)
+ {
+ //dstream<<"ignoring small wanted_range_change"<<std::endl;
+ return;
+ }
+
+ new_range += wanted_range_change;
+
+ //float new_range_unclamped = new_range;
+ if(new_range < range_min)
+ new_range = range_min;
+ if(new_range > range_max)
+ new_range = range_max;
+
+ /*dstream<<"new_range="<<new_range_unclamped
+ <<", clamped to "<<new_range<<std::endl;*/
+
+ draw_control.wanted_range = new_range;
+
+ range_old = new_range;
+ frametime_old = frametime;
+}
+
+/*
+ Hotbar draw routine
+*/
+void draw_hotbar(video::IVideoDriver *driver, gui::IGUIFont *font,
+ v2s32 centerlowerpos, s32 imgsize, s32 itemcount,
+ Inventory *inventory, s32 halfheartcount)
+{
+ InventoryList *mainlist = inventory->getList("main");
+ if(mainlist == NULL)
+ {
+ dstream<<"WARNING: draw_hotbar(): mainlist == NULL"<<std::endl;
+ return;
+ }
+
+ s32 padding = imgsize/12;
+ //s32 height = imgsize + padding*2;
+ s32 width = itemcount*(imgsize+padding*2);
+
+ // Position of upper left corner of bar
+ v2s32 pos = centerlowerpos - v2s32(width/2, imgsize+padding*2);
+
+ // Draw background color
+ /*core::rect<s32> barrect(0,0,width,height);
+ barrect += pos;
+ video::SColor bgcolor(255,128,128,128);
+ driver->draw2DRectangle(bgcolor, barrect, NULL);*/
+
+ core::rect<s32> imgrect(0,0,imgsize,imgsize);
+
+ for(s32 i=0; i<itemcount; i++)
+ {
+ InventoryItem *item = mainlist->getItem(i);
+
+ core::rect<s32> rect = imgrect + pos
+ + v2s32(padding+i*(imgsize+padding*2), padding);
+
+ if(g_selected_item == i)
+ {
+ driver->draw2DRectangle(video::SColor(255,255,0,0),
+ core::rect<s32>(rect.UpperLeftCorner - v2s32(1,1)*padding,
+ rect.LowerRightCorner + v2s32(1,1)*padding),
+ NULL);
+ }
+ else
+ {
+ video::SColor bgcolor2(128,0,0,0);
+ driver->draw2DRectangle(bgcolor2, rect, NULL);
+ }
+
+ if(item != NULL)
+ {
+ drawInventoryItem(driver, font, item, rect, NULL);
+ }
+ }
+
+ /*
+ Draw hearts
+ */
+ {
+ video::ITexture *heart_texture =
+ driver->getTexture(getTexturePath("heart.png").c_str());
+ v2s32 p = pos + v2s32(0, -20);
+ for(s32 i=0; i<halfheartcount/2; i++)
+ {
+ const video::SColor color(255,255,255,255);
+ const video::SColor colors[] = {color,color,color,color};
+ core::rect<s32> rect(0,0,16,16);
+ rect += p;
+ driver->draw2DImage(heart_texture, rect,
+ core::rect<s32>(core::position2d<s32>(0,0),
+ core::dimension2di(heart_texture->getOriginalSize())),
+ NULL, colors, true);
+ p += v2s32(20,0);
+ }
+ if(halfheartcount % 2 == 1)
+ {
+ const video::SColor color(255,255,255,255);
+ const video::SColor colors[] = {color,color,color,color};
+ core::rect<s32> rect(0,0,16/2,16);
+ rect += p;
+ core::dimension2di srcd(heart_texture->getOriginalSize());
+ srcd.Width /= 2;
+ driver->draw2DImage(heart_texture, rect,
+ core::rect<s32>(core::position2d<s32>(0,0), srcd),
+ NULL, colors, true);
+ p += v2s32(20,0);
+ }
+ }
+}
+
+/*
+ Find what the player is pointing at
+*/
+void getPointedNode(Client *client, v3f player_position,
+ v3f camera_direction, v3f camera_position,
+ bool &nodefound, core::line3d<f32> shootline,
+ v3s16 &nodepos, v3s16 &neighbourpos,
+ core::aabbox3d<f32> &nodehilightbox,
+ f32 d)
+{
+ f32 mindistance = BS * 1001;
+
+ v3s16 pos_i = floatToInt(player_position, BS);
+
+ /*std::cout<<"pos_i=("<<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z<<")"
+ <<std::endl;*/
+
+ s16 a = d;
+ s16 ystart = pos_i.Y + 0 - (camera_direction.Y<0 ? a : 1);
+ s16 zstart = pos_i.Z - (camera_direction.Z<0 ? a : 1);
+ s16 xstart = pos_i.X - (camera_direction.X<0 ? a : 1);
+ s16 yend = pos_i.Y + 1 + (camera_direction.Y>0 ? a : 1);
+ s16 zend = pos_i.Z + (camera_direction.Z>0 ? a : 1);
+ s16 xend = pos_i.X + (camera_direction.X>0 ? a : 1);
+
+ for(s16 y = ystart; y <= yend; y++)
+ for(s16 z = zstart; z <= zend; z++)
+ for(s16 x = xstart; x <= xend; x++)
+ {
+ MapNode n;
+ try
+ {
+ n = client->getNode(v3s16(x,y,z));
+ if(content_pointable(n.d) == false)
+ continue;
+ }
+ catch(InvalidPositionException &e)
+ {
+ continue;
+ }
+
+ v3s16 np(x,y,z);
+ v3f npf = intToFloat(np, BS);
+
+ f32 d = 0.01;
+
+ v3s16 dirs[6] = {
+ v3s16(0,0,1), // back
+ v3s16(0,1,0), // top
+ v3s16(1,0,0), // right
+ v3s16(0,0,-1), // front
+ v3s16(0,-1,0), // bottom
+ v3s16(-1,0,0), // left
+ };
+
+ /*
+ Meta-objects
+ */
+ if(n.d == CONTENT_TORCH)
+ {
+ v3s16 dir = unpackDir(n.dir);
+ v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
+ dir_f *= BS/2 - BS/6 - BS/20;
+ v3f cpf = npf + dir_f;
+ f32 distance = (cpf - camera_position).getLength();
+
+ core::aabbox3d<f32> box;
+
+ // bottom
+ if(dir == v3s16(0,-1,0))
+ {
+ box = core::aabbox3d<f32>(
+ npf - v3f(BS/6, BS/2, BS/6),
+ npf + v3f(BS/6, -BS/2+BS/3*2, BS/6)
+ );
+ }
+ // top
+ else if(dir == v3s16(0,1,0))
+ {
+ box = core::aabbox3d<f32>(
+ npf - v3f(BS/6, -BS/2+BS/3*2, BS/6),
+ npf + v3f(BS/6, BS/2, BS/6)
+ );
+ }
+ // side
+ else
+ {
+ box = core::aabbox3d<f32>(
+ cpf - v3f(BS/6, BS/3, BS/6),
+ cpf + v3f(BS/6, BS/3, BS/6)
+ );
+ }
+
+ if(distance < mindistance)
+ {
+ if(box.intersectsWithLine(shootline))
+ {
+ nodefound = true;
+ nodepos = np;
+ neighbourpos = np;
+ mindistance = distance;
+ nodehilightbox = box;
+ }
+ }
+ }
+ else if(n.d == CONTENT_SIGN_WALL)
+ {
+ v3s16 dir = unpackDir(n.dir);
+ v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
+ dir_f *= BS/2 - BS/6 - BS/20;
+ v3f cpf = npf + dir_f;
+ f32 distance = (cpf - camera_position).getLength();
+
+ v3f vertices[4] =
+ {
+ v3f(BS*0.42,-BS*0.35,-BS*0.4),
+ v3f(BS*0.49, BS*0.35, BS*0.4),
+ };
+
+ for(s32 i=0; i<2; i++)
+ {
+ if(dir == v3s16(1,0,0))
+ vertices[i].rotateXZBy(0);
+ if(dir == v3s16(-1,0,0))
+ vertices[i].rotateXZBy(180);
+ if(dir == v3s16(0,0,1))
+ vertices[i].rotateXZBy(90);
+ if(dir == v3s16(0,0,-1))
+ vertices[i].rotateXZBy(-90);
+ if(dir == v3s16(0,-1,0))
+ vertices[i].rotateXYBy(-90);
+ if(dir == v3s16(0,1,0))
+ vertices[i].rotateXYBy(90);
+
+ vertices[i] += npf;
+ }
+
+ core::aabbox3d<f32> box;
+
+ box = core::aabbox3d<f32>(vertices[0]);
+ box.addInternalPoint(vertices[1]);
+
+ if(distance < mindistance)
+ {
+ if(box.intersectsWithLine(shootline))
+ {
+ nodefound = true;
+ nodepos = np;
+ neighbourpos = np;
+ mindistance = distance;
+ nodehilightbox = box;
+ }
+ }
+ }
+ /*
+ Regular blocks
+ */
+ else
+ {
+ for(u16 i=0; i<6; i++)
+ {
+ v3f dir_f = v3f(dirs[i].X,
+ dirs[i].Y, dirs[i].Z);
+ v3f centerpoint = npf + dir_f * BS/2;
+ f32 distance =
+ (centerpoint - camera_position).getLength();
+
+ if(distance < mindistance)
+ {
+ core::CMatrix4<f32> m;
+ m.buildRotateFromTo(v3f(0,0,1), dir_f);
+
+ // This is the back face
+ v3f corners[2] = {
+ v3f(BS/2, BS/2, BS/2),
+ v3f(-BS/2, -BS/2, BS/2+d)
+ };
+
+ for(u16 j=0; j<2; j++)
+ {
+ m.rotateVect(corners[j]);
+ corners[j] += npf;
+ }
+
+ core::aabbox3d<f32> facebox(corners[0]);
+ facebox.addInternalPoint(corners[1]);
+
+ if(facebox.intersectsWithLine(shootline))
+ {
+ nodefound = true;
+ nodepos = np;
+ neighbourpos = np + dirs[i];
+ mindistance = distance;
+
+ //nodehilightbox = facebox;
+
+ const float d = 0.502;
+ core::aabbox3d<f32> nodebox
+ (-BS*d, -BS*d, -BS*d, BS*d, BS*d, BS*d);
+ v3f nodepos_f = intToFloat(nodepos, BS);
+ nodebox.MinEdge += nodepos_f;
+ nodebox.MaxEdge += nodepos_f;
+ nodehilightbox = nodebox;
+ }
+ } // if distance < mindistance
+ } // for dirs
+ } // regular block
+ } // for coords
+}
+
+void update_skybox(video::IVideoDriver* driver,
+ scene::ISceneManager* smgr, scene::ISceneNode* &skybox,
+ float brightness)
+{
+ if(skybox)
+ {
+ skybox->remove();
+ }
+
+ if(brightness >= 0.5)
+ {
+ skybox = smgr->addSkyBoxSceneNode(
+ driver->getTexture(getTexturePath("skybox2.png").c_str()),
+ driver->getTexture(getTexturePath("skybox3.png").c_str()),
+ driver->getTexture(getTexturePath("skybox1.png").c_str()),
+ driver->getTexture(getTexturePath("skybox1.png").c_str()),
+ driver->getTexture(getTexturePath("skybox1.png").c_str()),
+ driver->getTexture(getTexturePath("skybox1.png").c_str()));
+ }
+ else if(brightness >= 0.2)
+ {
+ skybox = smgr->addSkyBoxSceneNode(
+ driver->getTexture(getTexturePath("skybox2_dawn.png").c_str()),
+ driver->getTexture(getTexturePath("skybox3_dawn.png").c_str()),
+ driver->getTexture(getTexturePath("skybox1_dawn.png").c_str()),
+ driver->getTexture(getTexturePath("skybox1_dawn.png").c_str()),
+ driver->getTexture(getTexturePath("skybox1_dawn.png").c_str()),
+ driver->getTexture(getTexturePath("skybox1_dawn.png").c_str()));
+ }
+ else
+ {
+ skybox = smgr->addSkyBoxSceneNode(
+ driver->getTexture(getTexturePath("skybox2_night.png").c_str()),
+ driver->getTexture(getTexturePath("skybox3_night.png").c_str()),
+ driver->getTexture(getTexturePath("skybox1_night.png").c_str()),
+ driver->getTexture(getTexturePath("skybox1_night.png").c_str()),
+ driver->getTexture(getTexturePath("skybox1_night.png").c_str()),
+ driver->getTexture(getTexturePath("skybox1_night.png").c_str()));
+ }
+}
+
+void the_game(
+ bool &kill,
+ bool random_input,
+ InputHandler *input,
+ IrrlichtDevice *device,
+ gui::IGUIFont* font,
+ std::string map_dir,
+ std::string playername,
+ std::string password,
+ std::string address,
+ u16 port,
+ std::wstring &error_message
+)
+{
+ video::IVideoDriver* driver = device->getVideoDriver();
+ scene::ISceneManager* smgr = device->getSceneManager();
+
+ v2u32 screensize(0,0);
+ v2u32 last_screensize(0,0);
+ screensize = driver->getScreenSize();
+
+ const s32 hotbar_itemcount = 8;
+ const s32 hotbar_imagesize = 36;
+
+ // The color of the sky
+
+ //video::SColor skycolor = video::SColor(255,140,186,250);
+
+ video::SColor bgcolor_bright = video::SColor(255,170,200,230);
+
+ /*
+ Draw "Loading" screen
+ */
+ const wchar_t *loadingtext = L"Loading and connecting...";
+ u32 text_height = font->getDimension(loadingtext).Height;
+ core::vector2d<s32> center(screensize.X/2, screensize.Y/2);
+ core::vector2d<s32> textsize(300, text_height);
+ core::rect<s32> textrect(center - textsize/2, center + textsize/2);
+
+ gui::IGUIStaticText *gui_loadingtext = guienv->addStaticText(
+ loadingtext, textrect, false, false);
+ gui_loadingtext->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
+
+ driver->beginScene(true, true, video::SColor(255,0,0,0));
+ guienv->drawAll();
+ driver->endScene();
+
+
+ /*
+ Create server.
+ SharedPtr will delete it when it goes out of scope.
+ */
+ SharedPtr<Server> server;
+ if(address == ""){
+ std::cout<<DTIME<<"Creating server"<<std::endl;
+ server = new Server(map_dir);
+ server->start(port);
+ }
+
+ /*
+ Create client
+ */
+
+ std::cout<<DTIME<<"Creating client"<<std::endl;
+ Client client(device, playername.c_str(), password, draw_control);
+
+ Address connect_address(0,0,0,0, port);
+ try{
+ if(address == "")
+ //connect_address.Resolve("localhost");
+ connect_address.setAddress(127,0,0,1);
+ else
+ connect_address.Resolve(address.c_str());
+ }
+ catch(ResolveError &e)
+ {
+ std::cout<<DTIME<<"Couldn't resolve address"<<std::endl;
+ //return 0;
+ error_message = L"Couldn't resolve address";
+ gui_loadingtext->remove();
+ return;
+ }
+
+ /*
+ Attempt to connect to the server
+ */
+
+ dstream<<DTIME<<"Connecting to server at ";
+ connect_address.print(&dstream);
+ dstream<<std::endl;
+ client.connect(connect_address);
+
+ bool could_connect = false;
+
+ try{
+ float time_counter = 0.0;
+ for(;;)
+ {
+ if(client.connectedAndInitialized())
+ {
+ could_connect = true;
+ break;
+ }
+ if(client.accessDenied())
+ {
+ break;
+ }
+ // Wait for 10 seconds
+ if(time_counter >= 10.0)
+ {
+ break;
+ }
+
+ // Update screen
+ driver->beginScene(true, true, video::SColor(255,0,0,0));
+ guienv->drawAll();
+ driver->endScene();
+
+ // Update client and server
+
+ client.step(0.1);
+
+ if(server != NULL)
+ server->step(0.1);
+
+ // Delay a bit
+ sleep_ms(100);
+ time_counter += 0.1;
+ }
+ }
+ catch(con::PeerNotFoundException &e)
+ {}
+
+ if(could_connect == false)
+ {
+ if(client.accessDenied())
+ {
+ error_message = L"Access denied. Reason: "
+ +client.accessDeniedReason();
+ std::cout<<DTIME<<wide_to_narrow(error_message)<<std::endl;
+ }
+ else
+ {
+ error_message = L"Connection timed out.";
+ std::cout<<DTIME<<"Timed out."<<std::endl;
+ }
+ gui_loadingtext->remove();
+ return;
+ }
+
+ /*
+ Create skybox
+ */
+ float old_brightness = 1.0;
+ scene::ISceneNode* skybox = NULL;
+ update_skybox(driver, smgr, skybox, 1.0);
+
+ /*
+ Create the camera node
+ */
+
+ scene::ICameraSceneNode* camera = smgr->addCameraSceneNode(
+ 0, // Camera parent
+ v3f(BS*100, BS*2, BS*100), // Look from
+ v3f(BS*100+1, BS*2, BS*100), // Look to
+ -1 // Camera ID
+ );
+
+ if(camera == NULL)
+ {
+ error_message = L"Failed to create the camera node";
+ return;
+ }
+
+ camera->setFOV(FOV_ANGLE);
+
+ // Just so big a value that everything rendered is visible
+ camera->setFarValue(100000*BS);
+
+ f32 camera_yaw = 0; // "right/left"
+ f32 camera_pitch = 0; // "up/down"
+
+ /*
+ Clouds
+ */
+
+ float cloud_height = BS*100;
+ Clouds *clouds = NULL;
+ clouds = new Clouds(smgr->getRootSceneNode(), smgr, -1,
+ cloud_height, time(0));
+
+ /*
+ Move into game
+ */
+
+ gui_loadingtext->remove();
+
+ /*
+ Add some gui stuff
+ */
+
+ // First line of debug text
+ gui::IGUIStaticText *guitext = guienv->addStaticText(
+ L"Minetest-c55",
+ core::rect<s32>(5, 5, 795, 5+text_height),
+ false, false);
+ // Second line of debug text
+ gui::IGUIStaticText *guitext2 = guienv->addStaticText(
+ L"",
+ core::rect<s32>(5, 5+(text_height+5)*1, 795, (5+text_height)*2),
+ false, false);
+
+ // At the middle of the screen
+ // Object infos are shown in this
+ gui::IGUIStaticText *guitext_info = guienv->addStaticText(
+ L"",
+ core::rect<s32>(0,0,400,text_height+5) + v2s32(100,200),
+ false, false);
+
+ // Chat text
+ gui::IGUIStaticText *guitext_chat = guienv->addStaticText(
+ L"",
+ core::rect<s32>(0,0,0,0),
+ false, false); // Disable word wrap as of now
+ //false, true);
+ //guitext_chat->setBackgroundColor(video::SColor(96,0,0,0));
+ core::list<ChatLine> chat_lines;
+
+ /*GUIQuickInventory *quick_inventory = new GUIQuickInventory
+ (guienv, NULL, v2s32(10, 70), 5, &local_inventory);*/
+ /*GUIQuickInventory *quick_inventory = new GUIQuickInventory
+ (guienv, NULL, v2s32(0, 0), quickinv_itemcount, &local_inventory);*/
+
+ // Test the text input system
+ /*(new GUITextInputMenu(guienv, guiroot, -1, &g_menumgr,
+ NULL))->drop();*/
+ /*GUIMessageMenu *menu =
+ new GUIMessageMenu(guienv, guiroot, -1,
+ &g_menumgr,
+ L"Asd");
+ menu->drop();*/
+
+ // Launch pause menu
+ (new GUIPauseMenu(guienv, guiroot, -1, g_gamecallback,
+ &g_menumgr))->drop();
+
+ // Enable texts
+ /*guitext2->setVisible(true);
+ guitext_info->setVisible(true);
+ guitext_chat->setVisible(true);*/
+
+ //s32 guitext_chat_pad_bottom = 70;
+
+ /*
+ Some statistics are collected in these
+ */
+ u32 drawtime = 0;
+ u32 beginscenetime = 0;
+ u32 scenetime = 0;
+ u32 endscenetime = 0;
+
+ // A test
+ //throw con::PeerNotFoundException("lol");
+
+ core::list<float> frametime_log;
+
+ float damage_flash_timer = 0;
+
+ bool invert_mouse = g_settings.getBool("invert_mouse");
+
+ /*
+ Main loop
+ */
+
+ bool first_loop_after_window_activation = true;
+
+ // TODO: Convert the static interval timers to these
+ // Interval limiter for profiler
+ IntervalLimiter m_profiler_interval;
+
+ // Time is in milliseconds
+ // NOTE: getRealTime() causes strange problems in wine (imprecision?)
+ // NOTE: So we have to use getTime() and call run()s between them
+ u32 lasttime = device->getTimer()->getTime();
+
+ while(device->run() && kill == false)
+ {
+ if(g_gamecallback->disconnect_requested)
+ {
+ g_gamecallback->disconnect_requested = false;
+ break;
+ }
+
+ if(g_gamecallback->changepassword_requested)
+ {
+ (new GUIPasswordChange(guienv, guiroot, -1,
+ &g_menumgr, &client))->drop();
+ g_gamecallback->changepassword_requested = false;
+ }
+
+ /*
+ Process TextureSource's queue
+ */
+ ((TextureSource*)g_texturesource)->processQueue();
+
+ /*
+ Random calculations
+ */
+ last_screensize = screensize;
+ screensize = driver->getScreenSize();
+ v2s32 displaycenter(screensize.X/2,screensize.Y/2);
+ //bool screensize_changed = screensize != last_screensize;
+
+ // Hilight boxes collected during the loop and displayed
+ core::list< core::aabbox3d<f32> > hilightboxes;
+
+ // Info text
+ std::wstring infotext;
+
+ // When screen size changes, update positions and sizes of stuff
+ /*if(screensize_changed)
+ {
+ v2s32 pos(displaycenter.X-((quickinv_itemcount-1)*quickinv_spacing+quickinv_size)/2, screensize.Y-quickinv_spacing);
+ quick_inventory->updatePosition(pos);
+ }*/
+
+ //TimeTaker //timer1("//timer1");
+
+ // Time of frame without fps limit
+ float busytime;
+ u32 busytime_u32;
+ {
+ // not using getRealTime is necessary for wine
+ u32 time = device->getTimer()->getTime();
+ if(time > lasttime)
+ busytime_u32 = time - lasttime;
+ else
+ busytime_u32 = 0;
+ busytime = busytime_u32 / 1000.0;
+ }
+
+ //std::cout<<"busytime_u32="<<busytime_u32<<std::endl;
+
+ // Necessary for device->getTimer()->getTime()
+ device->run();
+
+ /*
+ Viewing range
+ */
+
+ updateViewingRange(busytime, &client);
+
+ /*
+ FPS limiter
+ */
+
+ {
+ float fps_max = g_settings.getFloat("fps_max");
+ u32 frametime_min = 1000./fps_max;
+
+ if(busytime_u32 < frametime_min)
+ {
+ u32 sleeptime = frametime_min - busytime_u32;
+ device->sleep(sleeptime);
+ }
+ }
+
+ // Necessary for device->getTimer()->getTime()
+ device->run();
+
+ /*
+ Time difference calculation
+ */
+ f32 dtime; // in seconds
+
+ u32 time = device->getTimer()->getTime();
+ if(time > lasttime)
+ dtime = (time - lasttime) / 1000.0;
+ else
+ dtime = 0;
+ lasttime = time;
+
+ /*
+ Log frametime for visualization
+ */
+ frametime_log.push_back(dtime);
+ if(frametime_log.size() > 100)
+ {
+ core::list<float>::Iterator i = frametime_log.begin();
+ frametime_log.erase(i);
+ }
+
+ /*
+ Visualize frametime in terminal
+ */
+ /*for(u32 i=0; i<dtime*400; i++)
+ std::cout<<"X";
+ std::cout<<std::endl;*/
+
+ /*
+ Time average and jitter calculation
+ */
+
+ static f32 dtime_avg1 = 0.0;
+ dtime_avg1 = dtime_avg1 * 0.98 + dtime * 0.02;
+ f32 dtime_jitter1 = dtime - dtime_avg1;
+
+ static f32 dtime_jitter1_max_sample = 0.0;
+ static f32 dtime_jitter1_max_fraction = 0.0;
+ {
+ static f32 jitter1_max = 0.0;
+ static f32 counter = 0.0;
+ if(dtime_jitter1 > jitter1_max)
+ jitter1_max = dtime_jitter1;
+ counter += dtime;
+ if(counter > 0.0)
+ {
+ counter -= 3.0;
+ dtime_jitter1_max_sample = jitter1_max;
+ dtime_jitter1_max_fraction
+ = dtime_jitter1_max_sample / (dtime_avg1+0.001);
+ jitter1_max = 0.0;
+ }
+ }
+
+ /*
+ Busytime average and jitter calculation
+ */
+
+ static f32 busytime_avg1 = 0.0;
+ busytime_avg1 = busytime_avg1 * 0.98 + busytime * 0.02;
+ f32 busytime_jitter1 = busytime - busytime_avg1;
+
+ static f32 busytime_jitter1_max_sample = 0.0;
+ static f32 busytime_jitter1_min_sample = 0.0;
+ {
+ static f32 jitter1_max = 0.0;
+ static f32 jitter1_min = 0.0;
+ static f32 counter = 0.0;
+ if(busytime_jitter1 > jitter1_max)
+ jitter1_max = busytime_jitter1;
+ if(busytime_jitter1 < jitter1_min)
+ jitter1_min = busytime_jitter1;
+ counter += dtime;
+ if(counter > 0.0){
+ counter -= 3.0;
+ busytime_jitter1_max_sample = jitter1_max;
+ busytime_jitter1_min_sample = jitter1_min;
+ jitter1_max = 0.0;
+ jitter1_min = 0.0;
+ }
+ }
+
+ /*
+ Debug info for client
+ */
+ {
+ static float counter = 0.0;
+ counter -= dtime;
+ if(counter < 0)
+ {
+ counter = 30.0;
+ client.printDebugInfo(std::cout);
+ }
+ }
+
+ /*
+ Profiler
+ */
+ float profiler_print_interval =
+ g_settings.getFloat("profiler_print_interval");
+ if(profiler_print_interval != 0)
+ {
+ if(m_profiler_interval.step(0.030, profiler_print_interval))
+ {
+ dstream<<"Profiler:"<<std::endl;
+ g_profiler.print(dstream);
+ g_profiler.clear();
+ }
+ }
+
+ /*
+ Direct handling of user input
+ */
+
+ // Reset input if window not active or some menu is active
+ if(device->isWindowActive() == false || noMenuActive() == false)
+ {
+ input->clear();
+ }
+
+ // Input handler step() (used by the random input generator)
+ input->step(dtime);
+
+ /*
+ Launch menus according to keys
+ */
+ if(input->wasKeyDown(getKeySetting("keymap_inventory")))
+ {
+ dstream<<DTIME<<"the_game: "
+ <<"Launching inventory"<<std::endl;
+
+ GUIInventoryMenu *menu =
+ new GUIInventoryMenu(guienv, guiroot, -1,
+ &g_menumgr, v2s16(8,7),
+ client.getInventoryContext(),
+ &client);
+
+ core::array<GUIInventoryMenu::DrawSpec> draw_spec;
+ draw_spec.push_back(GUIInventoryMenu::DrawSpec(
+ "list", "current_player", "main",
+ v2s32(0, 3), v2s32(8, 4)));
+ draw_spec.push_back(GUIInventoryMenu::DrawSpec(
+ "list", "current_player", "craft",
+ v2s32(3, 0), v2s32(3, 3)));
+ draw_spec.push_back(GUIInventoryMenu::DrawSpec(
+ "list", "current_player", "craftresult",
+ v2s32(7, 1), v2s32(1, 1)));
+
+ menu->setDrawSpec(draw_spec);
+
+ menu->drop();
+ }
+ else if(input->wasKeyDown(KEY_ESCAPE))
+ {
+ dstream<<DTIME<<"the_game: "
+ <<"Launching pause menu"<<std::endl;
+ // It will delete itself by itself
+ (new GUIPauseMenu(guienv, guiroot, -1, g_gamecallback,
+ &g_menumgr))->drop();
+
+ // Move mouse cursor on top of the disconnect button
+ input->setMousePos(displaycenter.X, displaycenter.Y+25);
+ }
+ else if(input->wasKeyDown(getKeySetting("keymap_chat")))
+ {
+ TextDest *dest = new TextDestChat(&client);
+
+ (new GUITextInputMenu(guienv, guiroot, -1,
+ &g_menumgr, dest,
+ L""))->drop();
+ }
+
+ // Item selection with mouse wheel
+ {
+ s32 wheel = input->getMouseWheel();
+ u16 max_item = MYMIN(PLAYER_INVENTORY_SIZE-1,
+ hotbar_itemcount-1);
+
+ if(wheel < 0)
+ {
+ if(g_selected_item < max_item)
+ g_selected_item++;
+ else
+ g_selected_item = 0;
+ }
+ else if(wheel > 0)
+ {
+ if(g_selected_item > 0)
+ g_selected_item--;
+ else
+ g_selected_item = max_item;
+ }
+ }
+
+ // Item selection
+ for(u16 i=0; i<10; i++)
+ {
+ s32 keycode = irr::KEY_KEY_1 + i;
+ if(i == 9)
+ keycode = irr::KEY_KEY_0;
+ if(input->wasKeyDown((irr::EKEY_CODE)keycode))
+ {
+ if(i < PLAYER_INVENTORY_SIZE && i < hotbar_itemcount)
+ {
+ g_selected_item = i;
+
+ dstream<<DTIME<<"Selected item: "
+ <<g_selected_item<<std::endl;
+ }
+ }
+ }
+
+ // Viewing range selection
+ if(input->wasKeyDown(getKeySetting("keymap_rangeselect")))
+ {
+ if(draw_control.range_all)
+ {
+ draw_control.range_all = false;
+ dstream<<DTIME<<"Disabled full viewing range"<<std::endl;
+ }
+ else
+ {
+ draw_control.range_all = true;
+ dstream<<DTIME<<"Enabled full viewing range"<<std::endl;
+ }
+ }
+
+ // Print debug stacks
+ if(input->wasKeyDown(getKeySetting("keymap_print_debug_stacks")))
+ {
+ dstream<<"-----------------------------------------"
+ <<std::endl;
+ dstream<<DTIME<<"Printing debug stacks:"<<std::endl;
+ dstream<<"-----------------------------------------"
+ <<std::endl;
+ debug_stacks_print();
+ }
+
+ /*
+ Player speed control
+ TODO: Cache the keycodes from getKeySetting
+ */
+
+ {
+ /*bool a_up,
+ bool a_down,
+ bool a_left,
+ bool a_right,
+ bool a_jump,
+ bool a_superspeed,
+ bool a_sneak,
+ float a_pitch,
+ float a_yaw*/
+ PlayerControl control(
+ input->isKeyDown(getKeySetting("keymap_forward")),
+ input->isKeyDown(getKeySetting("keymap_backward")),
+ input->isKeyDown(getKeySetting("keymap_left")),
+ input->isKeyDown(getKeySetting("keymap_right")),
+ input->isKeyDown(getKeySetting("keymap_jump")),
+ input->isKeyDown(getKeySetting("keymap_special1")),
+ input->isKeyDown(getKeySetting("keymap_sneak")),
+ camera_pitch,
+ camera_yaw
+ );
+ client.setPlayerControl(control);
+ }
+
+ /*
+ Run server
+ */
+
+ if(server != NULL)
+ {
+ //TimeTaker timer("server->step(dtime)");
+ server->step(dtime);
+ }
+
+ /*
+ Process environment
+ */
+
+ {
+ //TimeTaker timer("client.step(dtime)");
+ client.step(dtime);
+ //client.step(dtime_avg1);
+ }
+
+ // Read client events
+ for(;;)
+ {
+ ClientEvent event = client.getClientEvent();
+ if(event.type == CE_NONE)
+ {
+ break;
+ }
+ else if(event.type == CE_PLAYER_DAMAGE)
+ {
+ //u16 damage = event.player_damage.amount;
+ //dstream<<"Player damage: "<<damage<<std::endl;
+ damage_flash_timer = 0.05;
+ }
+ else if(event.type == CE_PLAYER_FORCE_MOVE)
+ {
+ camera_yaw = event.player_force_move.yaw;
+ camera_pitch = event.player_force_move.pitch;
+ }
+ }
+
+ // Get player position
+ v3f player_position = client.getPlayerPosition();
+
+ //TimeTaker //timer2("//timer2");
+
+ /*
+ Mouse and camera control
+ */
+
+ if((device->isWindowActive() && noMenuActive()) || random_input)
+ {
+ if(!random_input)
+ device->getCursorControl()->setVisible(false);
+
+ if(first_loop_after_window_activation){
+ //std::cout<<"window active, first loop"<<std::endl;
+ first_loop_after_window_activation = false;
+ }
+ else{
+ s32 dx = input->getMousePos().X - displaycenter.X;
+ s32 dy = input->getMousePos().Y - displaycenter.Y;
+ if(invert_mouse)
+ dy = -dy;
+ //std::cout<<"window active, pos difference "<<dx<<","<<dy<<std::endl;
+
+ /*const float keyspeed = 500;
+ if(input->isKeyDown(irr::KEY_UP))
+ dy -= dtime * keyspeed;
+ if(input->isKeyDown(irr::KEY_DOWN))
+ dy += dtime * keyspeed;
+ if(input->isKeyDown(irr::KEY_LEFT))
+ dx -= dtime * keyspeed;
+ if(input->isKeyDown(irr::KEY_RIGHT))
+ dx += dtime * keyspeed;*/
+
+ camera_yaw -= dx*0.2;
+ camera_pitch += dy*0.2;
+ if(camera_pitch < -89.5) camera_pitch = -89.5;
+ if(camera_pitch > 89.5) camera_pitch = 89.5;
+ }
+ input->setMousePos(displaycenter.X, displaycenter.Y);
+ }
+ else{
+ device->getCursorControl()->setVisible(true);
+
+ //std::cout<<"window inactive"<<std::endl;
+ first_loop_after_window_activation = true;
+ }
+
+ camera_yaw = wrapDegrees(camera_yaw);
+ camera_pitch = wrapDegrees(camera_pitch);
+
+ v3f camera_direction = v3f(0,0,1);
+ camera_direction.rotateYZBy(camera_pitch);
+ camera_direction.rotateXZBy(camera_yaw);
+
+ // This is at the height of the eyes of the current figure
+ //v3f camera_position = player_position + v3f(0, BS+BS/2, 0);
+ // This is more like in minecraft
+ v3f camera_position = player_position + v3f(0, BS+BS*0.625, 0);
+
+ camera->setPosition(camera_position);
+ // *100.0 helps in large map coordinates
+ camera->setTarget(camera_position + camera_direction * 100.0);
+
+ if(FIELD_OF_VIEW_TEST){
+ client.updateCamera(v3f(0,0,0), v3f(0,0,1));
+ }
+ else{
+ //TimeTaker timer("client.updateCamera");
+ client.updateCamera(camera_position, camera_direction);
+ }
+
+ //timer2.stop();
+ //TimeTaker //timer3("//timer3");
+
+ /*
+ Calculate what block is the crosshair pointing to
+ */
+
+ //u32 t1 = device->getTimer()->getRealTime();
+
+ //f32 d = 4; // max. distance
+ f32 d = 4; // max. distance
+ core::line3d<f32> shootline(camera_position,
+ camera_position + camera_direction * BS * (d+1));
+
+ MapBlockObject *selected_object = client.getSelectedObject
+ (d*BS, camera_position, shootline);
+
+ ClientActiveObject *selected_active_object
+ = client.getSelectedActiveObject
+ (d*BS, camera_position, shootline);
+
+ if(selected_object != NULL)
+ {
+ //dstream<<"Client returned selected_object != NULL"<<std::endl;
+
+ core::aabbox3d<f32> box_on_map
+ = selected_object->getSelectionBoxOnMap();
+
+ hilightboxes.push_back(box_on_map);
+
+ infotext = narrow_to_wide(selected_object->infoText());
+
+ if(input->getLeftClicked())
+ {
+ std::cout<<DTIME<<"Left-clicked object"<<std::endl;
+ client.clickObject(0, selected_object->getBlock()->getPos(),
+ selected_object->getId(), g_selected_item);
+ }
+ else if(input->getRightClicked())
+ {
+ std::cout<<DTIME<<"Right-clicked object"<<std::endl;
+ /*
+ Check if we want to modify the object ourselves
+ */
+ if(selected_object->getTypeId() == MAPBLOCKOBJECT_TYPE_SIGN)
+ {
+ dstream<<"Sign object right-clicked"<<std::endl;
+
+ if(random_input == false)
+ {
+ // Get a new text for it
+
+ TextDest *dest = new TextDestSign(
+ selected_object->getBlock()->getPos(),
+ selected_object->getId(),
+ &client);
+
+ SignObject *sign_object = (SignObject*)selected_object;
+
+ std::wstring wtext =
+ narrow_to_wide(sign_object->getText());
+
+ (new GUITextInputMenu(guienv, guiroot, -1,
+ &g_menumgr, dest,
+ wtext))->drop();
+ }
+ }
+ /*
+ Otherwise pass the event to the server as-is
+ */
+ else
+ {
+ client.clickObject(1, selected_object->getBlock()->getPos(),
+ selected_object->getId(), g_selected_item);
+ }
+ }
+ }
+ else if(selected_active_object != NULL)
+ {
+ //dstream<<"Client returned selected_active_object != NULL"<<std::endl;
+
+ core::aabbox3d<f32> *selection_box
+ = selected_active_object->getSelectionBox();
+ // Box should exist because object was returned in the
+ // first place
+ assert(selection_box);
+
+ v3f pos = selected_active_object->getPosition();
+
+ core::aabbox3d<f32> box_on_map(
+ selection_box->MinEdge + pos,
+ selection_box->MaxEdge + pos
+ );
+
+ hilightboxes.push_back(box_on_map);
+
+ //infotext = narrow_to_wide("A ClientActiveObject");
+ infotext = narrow_to_wide(selected_active_object->infoText());
+
+ if(input->getLeftClicked())
+ {
+ std::cout<<DTIME<<"Left-clicked object"<<std::endl;
+ client.clickActiveObject(0,
+ selected_active_object->getId(), g_selected_item);
+ }
+ else if(input->getRightClicked())
+ {
+ std::cout<<DTIME<<"Right-clicked object"<<std::endl;
+ }
+ }
+ else // selected_object == NULL
+ {
+
+ /*
+ Find out which node we are pointing at
+ */
+
+ bool nodefound = false;
+ v3s16 nodepos;
+ v3s16 neighbourpos;
+ core::aabbox3d<f32> nodehilightbox;
+
+ getPointedNode(&client, player_position,
+ camera_direction, camera_position,
+ nodefound, shootline,
+ nodepos, neighbourpos,
+ nodehilightbox, d);
+
+ static float nodig_delay_counter = 0.0;
+
+ if(nodefound)
+ {
+ static v3s16 nodepos_old(-32768,-32768,-32768);
+
+ static float dig_time = 0.0;
+ static u16 dig_index = 0;
+
+ /*
+ Visualize selection
+ */
+
+ hilightboxes.push_back(nodehilightbox);
+
+ /*
+ Check information text of node
+ */
+
+ NodeMetadata *meta = client.getNodeMetadata(nodepos);
+ if(meta)
+ {
+ infotext = narrow_to_wide(meta->infoText());
+ }
+
+ //MapNode node = client.getNode(nodepos);
+
+ /*
+ Handle digging
+ */
+
+ if(input->getLeftReleased())
+ {
+ client.clearTempMod(nodepos);
+ dig_time = 0.0;
+ }
+
+ if(nodig_delay_counter > 0.0)
+ {
+ nodig_delay_counter -= dtime;
+ }
+ else
+ {
+ if(nodepos != nodepos_old)
+ {
+ std::cout<<DTIME<<"Pointing at ("<<nodepos.X<<","
+ <<nodepos.Y<<","<<nodepos.Z<<")"<<std::endl;
+
+ if(nodepos_old != v3s16(-32768,-32768,-32768))
+ {
+ client.clearTempMod(nodepos_old);
+ dig_time = 0.0;
+ }
+ }
+
+ if(input->getLeftClicked() ||
+ (input->getLeftState() && nodepos != nodepos_old))
+ {
+ dstream<<DTIME<<"Started digging"<<std::endl;
+ client.groundAction(0, nodepos, neighbourpos, g_selected_item);
+ }
+ if(input->getLeftClicked())
+ {
+ client.setTempMod(nodepos, NodeMod(NODEMOD_CRACK, 0));
+ }
+ if(input->getLeftState())
+ {
+ MapNode n = client.getNode(nodepos);
+
+ // Get tool name. Default is "" = bare hands
+ std::string toolname = "";
+ InventoryList *mlist = local_inventory.getList("main");
+ if(mlist != NULL)
+ {
+ InventoryItem *item = mlist->getItem(g_selected_item);
+ if(item && (std::string)item->getName() == "ToolItem")
+ {
+ ToolItem *titem = (ToolItem*)item;
+ toolname = titem->getToolName();
+ }
+ }
+
+ // Get digging properties for material and tool
+ u8 material = n.d;
+ DiggingProperties prop =
+ getDiggingProperties(material, toolname);
+
+ float dig_time_complete = 0.0;
+
+ if(prop.diggable == false)
+ {
+ /*dstream<<"Material "<<(int)material
+ <<" not diggable with \""
+ <<toolname<<"\""<<std::endl;*/
+ // I guess nobody will wait for this long
+ dig_time_complete = 10000000.0;
+ }
+ else
+ {
+ dig_time_complete = prop.time;
+ }
+
+ if(dig_time_complete >= 0.001)
+ {
+ dig_index = (u16)((float)CRACK_ANIMATION_LENGTH
+ * dig_time/dig_time_complete);
+ }
+ // This is for torches
+ else
+ {
+ dig_index = CRACK_ANIMATION_LENGTH;
+ }
+
+ if(dig_index < CRACK_ANIMATION_LENGTH)
+ {
+ //TimeTaker timer("client.setTempMod");
+ //dstream<<"dig_index="<<dig_index<<std::endl;
+ client.setTempMod(nodepos, NodeMod(NODEMOD_CRACK, dig_index));
+ }
+ else
+ {
+ dstream<<DTIME<<"Digging completed"<<std::endl;
+ client.groundAction(3, nodepos, neighbourpos, g_selected_item);
+ client.clearTempMod(nodepos);
+ client.removeNode(nodepos);
+
+ dig_time = 0;
+
+ nodig_delay_counter = dig_time_complete
+ / (float)CRACK_ANIMATION_LENGTH;
+
+ // We don't want a corresponding delay to
+ // very time consuming nodes
+ if(nodig_delay_counter > 0.5)
+ {
+ nodig_delay_counter = 0.5;
+ }
+ // We want a slight delay to very little
+ // time consuming nodes
+ float mindelay = 0.15;
+ if(nodig_delay_counter < mindelay)
+ {
+ nodig_delay_counter = mindelay;
+ }
+ }
+
+ dig_time += dtime;
+ }
+ }
+
+ if(input->getRightClicked())
+ {
+ std::cout<<DTIME<<"Ground right-clicked"<<std::endl;
+
+ if(meta && meta->typeId() == CONTENT_SIGN_WALL && !random_input)
+ {
+ dstream<<"Sign node right-clicked"<<std::endl;
+
+ SignNodeMetadata *signmeta = (SignNodeMetadata*)meta;
+
+ // Get a new text for it
+
+ TextDest *dest = new TextDestSignNode(nodepos, &client);
+
+ std::wstring wtext =
+ narrow_to_wide(signmeta->getText());
+
+ (new GUITextInputMenu(guienv, guiroot, -1,
+ &g_menumgr, dest,
+ wtext))->drop();
+ }
+ else if(meta && meta->typeId() == CONTENT_CHEST && !random_input)
+ {
+ dstream<<"Chest node right-clicked"<<std::endl;
+
+ //ChestNodeMetadata *chestmeta = (ChestNodeMetadata*)meta;
+
+ std::string chest_inv_id;
+ chest_inv_id += "nodemeta:";
+ chest_inv_id += itos(nodepos.X);
+ chest_inv_id += ",";
+ chest_inv_id += itos(nodepos.Y);
+ chest_inv_id += ",";
+ chest_inv_id += itos(nodepos.Z);
+
+ GUIInventoryMenu *menu =
+ new GUIInventoryMenu(guienv, guiroot, -1,
+ &g_menumgr, v2s16(8,9),
+ client.getInventoryContext(),
+ &client);
+
+ core::array<GUIInventoryMenu::DrawSpec> draw_spec;
+
+ draw_spec.push_back(GUIInventoryMenu::DrawSpec(
+ "list", chest_inv_id, "0",
+ v2s32(0, 0), v2s32(8, 4)));
+ draw_spec.push_back(GUIInventoryMenu::DrawSpec(
+ "list", "current_player", "main",
+ v2s32(0, 5), v2s32(8, 4)));
+
+ menu->setDrawSpec(draw_spec);
+
+ menu->drop();
+
+ }
+ else if(meta && meta->typeId() == CONTENT_FURNACE && !random_input)
+ {
+ dstream<<"Furnace node right-clicked"<<std::endl;
+
+ GUIFurnaceMenu *menu =
+ new GUIFurnaceMenu(guienv, guiroot, -1,
+ &g_menumgr, nodepos, &client);
+
+ menu->drop();
+
+ }
+ else
+ {
+ client.groundAction(1, nodepos, neighbourpos, g_selected_item);
+ }
+ }
+
+ nodepos_old = nodepos;
+ }
+ else{
+ }
+
+ } // selected_object == NULL
+
+ input->resetLeftClicked();
+ input->resetRightClicked();
+
+ if(input->getLeftReleased())
+ {
+ std::cout<<DTIME<<"Left button released (stopped digging)"
+ <<std::endl;
+ client.groundAction(2, v3s16(0,0,0), v3s16(0,0,0), 0);
+ }
+ if(input->getRightReleased())
+ {
+ //std::cout<<DTIME<<"Right released"<<std::endl;
+ // Nothing here
+ }
+
+ input->resetLeftReleased();
+ input->resetRightReleased();
+
+ /*
+ Calculate stuff for drawing
+ */
+
+ camera->setAspectRatio((f32)screensize.X / (f32)screensize.Y);
+
+ u32 daynight_ratio = client.getDayNightRatio();
+ u8 l = decode_light((daynight_ratio * LIGHT_SUN) / 1000);
+ video::SColor bgcolor = video::SColor(
+ 255,
+ bgcolor_bright.getRed() * l / 255,
+ bgcolor_bright.getGreen() * l / 255,
+ bgcolor_bright.getBlue() * l / 255);
+ /*skycolor.getRed() * l / 255,
+ skycolor.getGreen() * l / 255,
+ skycolor.getBlue() * l / 255);*/
+
+ float brightness = (float)l/255.0;
+
+ /*
+ Update skybox
+ */
+ if(fabs(brightness - old_brightness) > 0.01)
+ update_skybox(driver, smgr, skybox, brightness);
+
+ /*
+ Update coulds
+ */
+ if(clouds)
+ {
+ clouds->step(dtime);
+ clouds->update(v2f(player_position.X, player_position.Z),
+ 0.05+brightness*0.95);
+ }
+
+ // Store brightness value
+ old_brightness = brightness;
+
+ /*
+ Fog
+ */
+
+ if(g_settings.getBool("enable_fog") == true)
+ {
+ f32 range = draw_control.wanted_range*BS + MAP_BLOCKSIZE*BS*1.5;
+ if(draw_control.range_all)
+ range = 100000*BS;
+ if(range < 50*BS)
+ range = range * 0.5 + 25*BS;
+
+ driver->setFog(
+ bgcolor,
+ video::EFT_FOG_LINEAR,
+ range*0.4,
+ range*1.0,
+ 0.01,
+ false, // pixel fog
+ false // range fog
+ );
+ }
+ else
+ {
+ driver->setFog(
+ bgcolor,
+ video::EFT_FOG_LINEAR,
+ 100000*BS,
+ 110000*BS,
+ 0.01,
+ false, // pixel fog
+ false // range fog
+ );
+ }
+
+
+ /*
+ Update gui stuff (0ms)
+ */
+
+ //TimeTaker guiupdatetimer("Gui updating");
+
+ {
+ static float drawtime_avg = 0;
+ drawtime_avg = drawtime_avg * 0.95 + (float)drawtime*0.05;
+ static float beginscenetime_avg = 0;
+ beginscenetime_avg = beginscenetime_avg * 0.95 + (float)beginscenetime*0.05;
+ static float scenetime_avg = 0;
+ scenetime_avg = scenetime_avg * 0.95 + (float)scenetime*0.05;
+ static float endscenetime_avg = 0;
+ endscenetime_avg = endscenetime_avg * 0.95 + (float)endscenetime*0.05;
+
+ char temptext[300];
+ snprintf(temptext, 300, "Minetest-c55 %s ("
+ "R: range_all=%i"
+ ")"
+ " drawtime=%.0f, beginscenetime=%.0f"
+ ", scenetime=%.0f, endscenetime=%.0f",
+ VERSION_STRING,
+ draw_control.range_all,
+ drawtime_avg,
+ beginscenetime_avg,
+ scenetime_avg,
+ endscenetime_avg
+ );
+
+ guitext->setText(narrow_to_wide(temptext).c_str());
+ }
+
+ {
+ char temptext[300];
+ snprintf(temptext, 300,
+ "(% .1f, % .1f, % .1f)"
+ " (% .3f < btime_jitter < % .3f"
+ ", dtime_jitter = % .1f %%"
+ ", v_range = %.1f)",
+ player_position.X/BS,
+ player_position.Y/BS,
+ player_position.Z/BS,
+ busytime_jitter1_min_sample,
+ busytime_jitter1_max_sample,
+ dtime_jitter1_max_fraction * 100.0,
+ draw_control.wanted_range
+ );
+
+ guitext2->setText(narrow_to_wide(temptext).c_str());
+ }
+
+ {
+ guitext_info->setText(infotext.c_str());
+ }
+
+ /*
+ Get chat messages from client
+ */
+ {
+ // Get new messages
+ std::wstring message;
+ while(client.getChatMessage(message))
+ {
+ chat_lines.push_back(ChatLine(message));
+ /*if(chat_lines.size() > 6)
+ {
+ core::list<ChatLine>::Iterator
+ i = chat_lines.begin();
+ chat_lines.erase(i);
+ }*/
+ }
+ // Append them to form the whole static text and throw
+ // it to the gui element
+ std::wstring whole;
+ // This will correspond to the line number counted from
+ // top to bottom, from size-1 to 0
+ s16 line_number = chat_lines.size();
+ // Count of messages to be removed from the top
+ u16 to_be_removed_count = 0;
+ for(core::list<ChatLine>::Iterator
+ i = chat_lines.begin();
+ i != chat_lines.end(); i++)
+ {
+ // After this, line number is valid for this loop
+ line_number--;
+ // Increment age
+ (*i).age += dtime;
+ /*
+ This results in a maximum age of 60*6 to the
+ lowermost line and a maximum of 6 lines
+ */
+ float allowed_age = (6-line_number) * 60.0;
+
+ if((*i).age > allowed_age)
+ {
+ to_be_removed_count++;
+ continue;
+ }
+ whole += (*i).text + L'\n';
+ }
+ for(u16 i=0; i<to_be_removed_count; i++)
+ {
+ core::list<ChatLine>::Iterator
+ it = chat_lines.begin();
+ chat_lines.erase(it);
+ }
+ guitext_chat->setText(whole.c_str());
+
+ // Update gui element size and position
+
+ /*core::rect<s32> rect(
+ 10,
+ screensize.Y - guitext_chat_pad_bottom
+ - text_height*chat_lines.size(),
+ screensize.X - 10,
+ screensize.Y - guitext_chat_pad_bottom
+ );*/
+ core::rect<s32> rect(
+ 10,
+ 50,
+ screensize.X - 10,
+ 50 + text_height*chat_lines.size()
+ );
+
+ guitext_chat->setRelativePosition(rect);
+
+ if(chat_lines.size() == 0)
+ guitext_chat->setVisible(false);
+ else
+ guitext_chat->setVisible(true);
+ }
+
+ /*
+ Inventory
+ */
+
+ static u16 old_selected_item = 65535;
+ if(client.getLocalInventoryUpdated()
+ || g_selected_item != old_selected_item)
+ {
+ old_selected_item = g_selected_item;
+ //std::cout<<"Updating local inventory"<<std::endl;
+ client.getLocalInventory(local_inventory);
+ }
+
+ /*
+ Send actions returned by the inventory menu
+ */
+ while(inventory_action_queue.size() != 0)
+ {
+ InventoryAction *a = inventory_action_queue.pop_front();
+
+ client.sendInventoryAction(a);
+ // Eat it
+ delete a;
+ }
+
+ /*
+ Drawing begins
+ */
+
+ TimeTaker drawtimer("Drawing");
+
+
+ {
+ TimeTaker timer("beginScene");
+ driver->beginScene(true, true, bgcolor);
+ //driver->beginScene(false, true, bgcolor);
+ beginscenetime = timer.stop(true);
+ }
+
+ //timer3.stop();
+
+ //std::cout<<DTIME<<"smgr->drawAll()"<<std::endl;
+
+ {
+ TimeTaker timer("smgr");
+ smgr->drawAll();
+ scenetime = timer.stop(true);
+ }
+
+ {
+ //TimeTaker timer9("auxiliary drawings");
+ // 0ms
+
+ //timer9.stop();
+ //TimeTaker //timer10("//timer10");
+
+ video::SMaterial m;
+ //m.Thickness = 10;
+ m.Thickness = 3;
+ m.Lighting = false;
+ driver->setMaterial(m);
+
+ driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
+
+ for(core::list< core::aabbox3d<f32> >::Iterator i=hilightboxes.begin();
+ i != hilightboxes.end(); i++)
+ {
+ /*std::cout<<"hilightbox min="
+ <<"("<<i->MinEdge.X<<","<<i->MinEdge.Y<<","<<i->MinEdge.Z<<")"
+ <<" max="
+ <<"("<<i->MaxEdge.X<<","<<i->MaxEdge.Y<<","<<i->MaxEdge.Z<<")"
+ <<std::endl;*/
+ driver->draw3DBox(*i, video::SColor(255,0,0,0));
+ }
+
+ /*
+ Frametime log
+ */
+ if(g_settings.getBool("frametime_graph") == true)
+ {
+ s32 x = 10;
+ for(core::list<float>::Iterator
+ i = frametime_log.begin();
+ i != frametime_log.end();
+ i++)
+ {
+ driver->draw2DLine(v2s32(x,50),
+ v2s32(x,50+(*i)*1000),
+ video::SColor(255,255,255,255));
+ x++;
+ }
+ }
+
+ /*
+ Draw crosshair
+ */
+ driver->draw2DLine(displaycenter - core::vector2d<s32>(10,0),
+ displaycenter + core::vector2d<s32>(10,0),
+ video::SColor(255,255,255,255));
+ driver->draw2DLine(displaycenter - core::vector2d<s32>(0,10),
+ displaycenter + core::vector2d<s32>(0,10),
+ video::SColor(255,255,255,255));
+
+ } // timer
+
+ //timer10.stop();
+ //TimeTaker //timer11("//timer11");
+
+ /*
+ Draw gui
+ */
+ // 0-1ms
+ guienv->drawAll();
+
+ /*
+ Draw hotbar
+ */
+ {
+ draw_hotbar(driver, font, v2s32(displaycenter.X, screensize.Y),
+ hotbar_imagesize, hotbar_itemcount, &local_inventory,
+ client.getHP());
+ }
+
+ /*
+ Damage flash
+ */
+ if(damage_flash_timer > 0.0)
+ {
+ damage_flash_timer -= dtime;
+
+ video::SColor color(128,255,0,0);
+ driver->draw2DRectangle(color,
+ core::rect<s32>(0,0,screensize.X,screensize.Y),
+ NULL);
+ }
+
+ /*
+ End scene
+ */
+ {
+ TimeTaker timer("endScene");
+ endSceneX(driver);
+ endscenetime = timer.stop(true);
+ }
+
+ drawtime = drawtimer.stop(true);
+
+ /*
+ End of drawing
+ */
+
+ static s16 lastFPS = 0;
+ //u16 fps = driver->getFPS();
+ u16 fps = (1.0/dtime_avg1);
+
+ if (lastFPS != fps)
+ {
+ core::stringw str = L"Minetest [";
+ str += driver->getName();
+ str += "] FPS=";
+ str += fps;
+
+ device->setWindowCaption(str.c_str());
+ lastFPS = fps;
+ }
+ }
+
+ /*
+ Drop stuff
+ */
+ clouds->drop();
+
+ /*
+ Draw a "shutting down" screen, which will be shown while the map
+ generator and other stuff quits
+ */
+ {
+ const wchar_t *shuttingdowntext = L"Shutting down stuff...";
+ gui::IGUIStaticText *gui_shuttingdowntext = guienv->addStaticText(
+ shuttingdowntext, textrect, false, false);
+ gui_shuttingdowntext->setTextAlignment(gui::EGUIA_CENTER,
+ gui::EGUIA_UPPERLEFT);
+ driver->beginScene(true, true, video::SColor(255,0,0,0));
+ guienv->drawAll();
+ driver->endScene();
+ gui_shuttingdowntext->remove();
+ }
+}
+
+
diff --git a/src/game.h b/src/game.h
new file mode 100644
index 000000000..eb289b8f2
--- /dev/null
+++ b/src/game.h
@@ -0,0 +1,77 @@
+/*
+Minetest-c55
+Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#ifndef GAME_HEADER
+#define GAME_HEADER
+
+#include "common_irrlicht.h"
+#include <string>
+
+class InputHandler
+{
+public:
+ InputHandler()
+ {
+ }
+ virtual ~InputHandler()
+ {
+ }
+
+ virtual bool isKeyDown(EKEY_CODE keyCode) = 0;
+ virtual bool wasKeyDown(EKEY_CODE keyCode) = 0;
+
+ virtual v2s32 getMousePos() = 0;
+ virtual void setMousePos(s32 x, s32 y) = 0;
+
+ virtual bool getLeftState() = 0;
+ virtual bool getRightState() = 0;
+
+ virtual bool getLeftClicked() = 0;
+ virtual bool getRightClicked() = 0;
+ virtual void resetLeftClicked() = 0;
+ virtual void resetRightClicked() = 0;
+
+ virtual bool getLeftReleased() = 0;
+ virtual bool getRightReleased() = 0;
+ virtual void resetLeftReleased() = 0;
+ virtual void resetRightReleased() = 0;
+
+ virtual s32 getMouseWheel() = 0;
+
+ virtual void step(float dtime) {};
+
+ virtual void clear() {};
+};
+
+void the_game(
+ bool &kill,
+ bool random_input,
+ InputHandler *input,
+ IrrlichtDevice *device,
+ gui::IGUIFont* font,
+ std::string map_dir,
+ std::string playername,
+ std::string password,
+ std::string address,
+ u16 port,
+ std::wstring &error_message
+);
+
+#endif
+
diff --git a/src/guiFurnaceMenu.cpp b/src/guiFurnaceMenu.cpp
new file mode 100644
index 000000000..9a998ce73
--- /dev/null
+++ b/src/guiFurnaceMenu.cpp
@@ -0,0 +1,58 @@
+/*
+Minetest-c55
+Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "guiFurnaceMenu.h"
+#include "client.h"
+
+GUIFurnaceMenu::GUIFurnaceMenu(
+ gui::IGUIEnvironment* env,
+ gui::IGUIElement* parent, s32 id,
+ IMenuManager *menumgr,
+ v3s16 nodepos,
+ Client *client
+ ):
+ GUIInventoryMenu(env, parent, id, menumgr, v2s16(8,9),
+ client->getInventoryContext(), client),
+ m_nodepos(nodepos),
+ m_client(client)
+{
+ std::string furnace_inv_id;
+ furnace_inv_id += "nodemeta:";
+ furnace_inv_id += itos(nodepos.X);
+ furnace_inv_id += ",";
+ furnace_inv_id += itos(nodepos.Y);
+ furnace_inv_id += ",";
+ furnace_inv_id += itos(nodepos.Z);
+
+ core::array<GUIInventoryMenu::DrawSpec> draw_spec;
+ draw_spec.push_back(GUIInventoryMenu::DrawSpec(
+ "list", furnace_inv_id, "fuel",
+ v2s32(2, 3), v2s32(1, 1)));
+ draw_spec.push_back(GUIInventoryMenu::DrawSpec(
+ "list", furnace_inv_id, "src",
+ v2s32(2, 1), v2s32(1, 1)));
+ draw_spec.push_back(GUIInventoryMenu::DrawSpec(
+ "list", furnace_inv_id, "dst",
+ v2s32(5, 1), v2s32(2, 2)));
+ draw_spec.push_back(GUIInventoryMenu::DrawSpec(
+ "list", "current_player", "main",
+ v2s32(0, 5), v2s32(8, 4)));
+ setDrawSpec(draw_spec);
+}
+
diff --git a/src/guiFurnaceMenu.h b/src/guiFurnaceMenu.h
new file mode 100644
index 000000000..ccc2e9dc0
--- /dev/null
+++ b/src/guiFurnaceMenu.h
@@ -0,0 +1,44 @@
+/*
+Minetest-c55
+Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#ifndef GUIFURNACEMENU_HEADER
+#define GUIFURNACEMENU_HEADER
+
+#include "guiInventoryMenu.h"
+
+class Client;
+
+class GUIFurnaceMenu : public GUIInventoryMenu
+{
+public:
+ GUIFurnaceMenu(
+ gui::IGUIEnvironment* env,
+ gui::IGUIElement* parent, s32 id,
+ IMenuManager *menumgr,
+ v3s16 nodepos,
+ Client *client
+ );
+private:
+
+ v3s16 m_nodepos;
+ Client *m_client;
+};
+
+#endif
+
diff --git a/src/guiInventoryMenu.cpp b/src/guiInventoryMenu.cpp
index ef795a5f4..a4ba472f8 100644
--- a/src/guiInventoryMenu.cpp
+++ b/src/guiInventoryMenu.cpp
@@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "guiInventoryMenu.h"
#include "constants.h"
+#include "keycode.h"
void drawInventoryItem(video::IVideoDriver *driver,
gui::IGUIFont *font,
@@ -78,18 +79,17 @@ void drawInventoryItem(video::IVideoDriver *driver,
GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env,
gui::IGUIElement* parent, s32 id,
- Inventory *inventory,
- Queue<InventoryAction*> *actions,
- IMenuManager *menumgr):
- GUIModalMenu(env, parent, id, menumgr)
+ IMenuManager *menumgr,
+ v2s16 menu_size,
+ InventoryContext *c,
+ InventoryManager *invmgr
+ ):
+ GUIModalMenu(env, parent, id, menumgr),
+ m_menu_size(menu_size),
+ m_c(c),
+ m_invmgr(invmgr)
{
- m_inventory = inventory;
m_selected_item = NULL;
- m_actions = actions;
-
- /*m_selected_item = new ItemSpec;
- m_selected_item->listname = "main";
- m_selected_item->i = 3;*/
}
GUIInventoryMenu::~GUIInventoryMenu()
@@ -102,11 +102,24 @@ GUIInventoryMenu::~GUIInventoryMenu()
void GUIInventoryMenu::removeChildren()
{
+ const core::list<gui::IGUIElement*> &children = getChildren();
+ core::list<gui::IGUIElement*> children_copy;
+ for(core::list<gui::IGUIElement*>::ConstIterator
+ i = children.begin(); i != children.end(); i++)
+ {
+ children_copy.push_back(*i);
+ }
+ for(core::list<gui::IGUIElement*>::Iterator
+ i = children_copy.begin();
+ i != children_copy.end(); i++)
{
+ (*i)->remove();
+ }
+ /*{
gui::IGUIElement *e = getElementFromId(256);
if(e != NULL)
e->remove();
- }
+ }*/
}
void GUIInventoryMenu::regenerateGui(v2u32 screensize)
@@ -114,15 +127,19 @@ void GUIInventoryMenu::regenerateGui(v2u32 screensize)
// Remove children
removeChildren();
- padding = v2s32(24,24);
+ /*padding = v2s32(24,24);
spacing = v2s32(60,56);
- imgsize = v2s32(48,48);
+ imgsize = v2s32(48,48);*/
+
+ padding = v2s32(screensize.Y/40, screensize.Y/40);
+ spacing = v2s32(screensize.Y/12, screensize.Y/13);
+ imgsize = v2s32(screensize.Y/15, screensize.Y/15);
s32 helptext_h = 15;
v2s32 size(
- padding.X*2+spacing.X*(8-1)+imgsize.X,
- padding.Y*2+spacing.Y*(7-1)+imgsize.Y + helptext_h
+ padding.X*2+spacing.X*(m_menu_size.X-1)+imgsize.X,
+ padding.Y*2+spacing.Y*(m_menu_size.Y-1)+imgsize.Y + helptext_h
);
core::rect<s32> rect(
@@ -137,13 +154,27 @@ void GUIInventoryMenu::regenerateGui(v2u32 screensize)
v2s32 basepos = getBasePos();
- m_draw_positions.clear();
- m_draw_positions.push_back(ListDrawSpec("main",
+ m_draw_spec.clear();
+ for(u16 i=0; i<m_init_draw_spec.size(); i++)
+ {
+ DrawSpec &s = m_init_draw_spec[i];
+ if(s.type == "list")
+ {
+ m_draw_spec.push_back(ListDrawSpec(s.name, s.subname,
+ basepos + v2s32(spacing.X*s.pos.X, spacing.Y*s.pos.Y),
+ s.geom));
+ }
+ }
+
+ /*
+ m_draw_spec.clear();
+ m_draw_spec.push_back(ListDrawSpec("main",
basepos + v2s32(spacing.X*0, spacing.Y*3), v2s32(8, 4)));
- m_draw_positions.push_back(ListDrawSpec("craft",
+ m_draw_spec.push_back(ListDrawSpec("craft",
basepos + v2s32(spacing.X*3, spacing.Y*0), v2s32(3, 3)));
- m_draw_positions.push_back(ListDrawSpec("craftresult",
+ m_draw_spec.push_back(ListDrawSpec("craftresult",
basepos + v2s32(spacing.X*7, spacing.Y*1), v2s32(1, 1)));
+ */
// Add children
{
@@ -160,9 +191,9 @@ GUIInventoryMenu::ItemSpec GUIInventoryMenu::getItemAtPos(v2s32 p) const
{
core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
- for(u32 i=0; i<m_draw_positions.size(); i++)
+ for(u32 i=0; i<m_draw_spec.size(); i++)
{
- const ListDrawSpec &s = m_draw_positions[i];
+ const ListDrawSpec &s = m_draw_spec[i];
for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
{
@@ -172,15 +203,14 @@ GUIInventoryMenu::ItemSpec GUIInventoryMenu::getItemAtPos(v2s32 p) const
core::rect<s32> rect = imgrect + s.pos + p0;
if(rect.isPointInside(p))
{
- return ItemSpec(s.listname, i);
+ return ItemSpec(s.inventoryname, s.listname, i);
}
}
}
- return ItemSpec("", -1);
+ return ItemSpec("", "", -1);
}
-//void GUIInventoryMenu::drawList(const std::string &name, v2s32 pos, v2s32 geom)
void GUIInventoryMenu::drawList(const ListDrawSpec &s)
{
video::IVideoDriver* driver = Environment->getVideoDriver();
@@ -191,7 +221,9 @@ void GUIInventoryMenu::drawList(const ListDrawSpec &s)
if (skin)
font = skin->getFont();
- InventoryList *ilist = m_inventory->getList(s.listname);
+ Inventory *inv = m_invmgr->getInventory(m_c, s.inventoryname);
+ assert(inv);
+ InventoryList *ilist = inv->getList(s.listname);
core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
@@ -241,9 +273,9 @@ void GUIInventoryMenu::drawMenu()
Draw items
*/
- for(u32 i=0; i<m_draw_positions.size(); i++)
+ for(u32 i=0; i<m_draw_spec.size(); i++)
{
- ListDrawSpec &s = m_draw_positions[i];
+ ListDrawSpec &s = m_draw_spec[i];
drawList(s);
}
@@ -262,7 +294,7 @@ bool GUIInventoryMenu::OnEvent(const SEvent& event)
quitMenu();
return true;
}
- if(event.KeyInput.Key==KEY_KEY_I && event.KeyInput.PressedDown)
+ if(event.KeyInput.Key==getKeySetting("keymap_inventory") && event.KeyInput.PressedDown)
{
quitMenu();
return true;
@@ -279,26 +311,40 @@ bool GUIInventoryMenu::OnEvent(const SEvent& event)
ItemSpec s = getItemAtPos(p);
if(s.isValid())
{
- //dstream<<"Mouse down on "<<s.listname<<" "<<s.i<<std::endl;
+ dstream<<"Mouse down on "<<s.inventoryname
+ <<"/"<<s.listname<<" "<<s.i<<std::endl;
if(m_selected_item)
{
+ Inventory *inv_from = m_invmgr->getInventory(m_c,
+ m_selected_item->inventoryname);
+ Inventory *inv_to = m_invmgr->getInventory(m_c,
+ s.inventoryname);
+ assert(inv_from);
+ assert(inv_to);
InventoryList *list_from =
- m_inventory->getList(m_selected_item->listname);
+ inv_from->getList(m_selected_item->listname);
InventoryList *list_to =
- m_inventory->getList(s.listname);
+ inv_to->getList(s.listname);
+ if(list_from == NULL)
+ dstream<<"from list doesn't exist"<<std::endl;
+ if(list_to == NULL)
+ dstream<<"to list doesn't exist"<<std::endl;
// Indicates whether source slot completely empties
bool source_empties = false;
if(list_from && list_to
&& list_from->getItem(m_selected_item->i) != NULL)
{
- dstream<<"Queueing IACTION_MOVE"<<std::endl;
+ dstream<<"Handing IACTION_MOVE to manager"<<std::endl;
IMoveAction *a = new IMoveAction();
a->count = right ? 1 : 0;
- a->from_name = m_selected_item->listname;
+ a->from_inv = m_selected_item->inventoryname;
+ a->from_list = m_selected_item->listname;
a->from_i = m_selected_item->i;
- a->to_name = s.listname;
+ a->to_inv = s.inventoryname;
+ a->to_list = s.listname;
a->to_i = s.i;
- m_actions->push_back(a);
+ //ispec.actions->push_back(a);
+ m_invmgr->inventoryAction(a);
if(list_from->getItem(m_selected_item->i)->getCount()==1)
source_empties = true;
@@ -316,7 +362,10 @@ bool GUIInventoryMenu::OnEvent(const SEvent& event)
/*
Select if non-NULL
*/
- InventoryList *list = m_inventory->getList(s.listname);
+ Inventory *inv = m_invmgr->getInventory(m_c,
+ s.inventoryname);
+ assert(inv);
+ InventoryList *list = inv->getList(s.listname);
if(list->getItem(s.i) != NULL)
{
m_selected_item = new ItemSpec(s);
diff --git a/src/guiInventoryMenu.h b/src/guiInventoryMenu.h
index 45a5e236a..10fb7a425 100644
--- a/src/guiInventoryMenu.h
+++ b/src/guiInventoryMenu.h
@@ -39,9 +39,12 @@ class GUIInventoryMenu : public GUIModalMenu
{
i = -1;
}
- ItemSpec(const std::string &a_name, s32 a_i)
+ ItemSpec(const std::string &a_inventoryname,
+ const std::string &a_listname,
+ s32 a_i)
{
- listname = a_name;
+ inventoryname = a_inventoryname;
+ listname = a_listname;
i = a_i;
}
bool isValid() const
@@ -49,6 +52,7 @@ class GUIInventoryMenu : public GUIModalMenu
return i != -1;
}
+ std::string inventoryname;
std::string listname;
s32 i;
};
@@ -58,26 +62,61 @@ class GUIInventoryMenu : public GUIModalMenu
ListDrawSpec()
{
}
- ListDrawSpec(const std::string &a_name, v2s32 a_pos, v2s32 a_geom)
+ ListDrawSpec(const std::string &a_inventoryname,
+ const std::string &a_listname,
+ v2s32 a_pos, v2s32 a_geom)
{
- listname = a_name;
+ inventoryname = a_inventoryname;
+ listname = a_listname;
pos = a_pos;
geom = a_geom;
}
+ std::string inventoryname;
std::string listname;
v2s32 pos;
v2s32 geom;
};
-
public:
+ struct DrawSpec
+ {
+ DrawSpec()
+ {
+ }
+ DrawSpec(const std::string &a_type,
+ const std::string &a_name,
+ const std::string &a_subname,
+ v2s32 a_pos,
+ v2s32 a_geom)
+ {
+ type = a_type;
+ name = a_name;
+ subname = a_subname;
+ pos = a_pos;
+ geom = a_geom;
+ }
+
+ std::string type;
+ std::string name;
+ std::string subname;
+ v2s32 pos;
+ v2s32 geom;
+ };
+
GUIInventoryMenu(gui::IGUIEnvironment* env,
gui::IGUIElement* parent, s32 id,
- Inventory *inventory,
- Queue<InventoryAction*> *actions,
- IMenuManager *menumgr);
+ IMenuManager *menumgr,
+ v2s16 menu_size,
+ InventoryContext *c,
+ InventoryManager *invmgr
+ );
~GUIInventoryMenu();
+ void setDrawSpec(core::array<DrawSpec> &init_draw_spec)
+ {
+ m_init_draw_spec = init_draw_spec;
+ }
+
void removeChildren();
/*
Remove and re-add (or reposition) stuff
@@ -90,22 +129,25 @@ public:
bool OnEvent(const SEvent& event);
-private:
+protected:
v2s32 getBasePos() const
{
return padding + AbsoluteRect.UpperLeftCorner;
}
+ v2s16 m_menu_size;
+
v2s32 padding;
v2s32 spacing;
v2s32 imgsize;
+
+ InventoryContext *m_c;
+ InventoryManager *m_invmgr;
- core::array<ListDrawSpec> m_draw_positions;
-
- Inventory *m_inventory;
+ core::array<DrawSpec> m_init_draw_spec;
+ core::array<ListDrawSpec> m_draw_spec;
ItemSpec *m_selected_item;
- Queue<InventoryAction*> *m_actions;
};
#endif
diff --git a/src/guiMainMenu.cpp b/src/guiMainMenu.cpp
index 6c5d6553c..ef0a013f1 100644
--- a/src/guiMainMenu.cpp
+++ b/src/guiMainMenu.cpp
@@ -64,7 +64,11 @@ void GUIMainMenu::regenerateGui(v2u32 screensize)
std::wstring text_address;
std::wstring text_port;
bool creative_mode;
-
+ bool enable_damage;
+ bool fancy_trees;
+ bool smooth_lighting;
+
+ // Client options
{
gui::IGUIElement *e = getElementFromId(258);
if(e != NULL)
@@ -87,12 +91,35 @@ void GUIMainMenu::regenerateGui(v2u32 screensize)
text_port = m_data->port;
}
{
+ gui::IGUIElement *e = getElementFromId(263);
+ if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
+ fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
+ else
+ fancy_trees = m_data->fancy_trees;
+ }
+ {
+ gui::IGUIElement *e = getElementFromId(262);
+ if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
+ smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
+ else
+ smooth_lighting = m_data->smooth_lighting;
+ }
+
+ // Server options
+ {
gui::IGUIElement *e = getElementFromId(259);
if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
else
creative_mode = m_data->creative_mode;
}
+ {
+ gui::IGUIElement *e = getElementFromId(261);
+ if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
+ enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
+ else
+ enable_damage = m_data->enable_damage;
+ }
/*
Remove stuff
@@ -102,86 +129,144 @@ void GUIMainMenu::regenerateGui(v2u32 screensize)
/*
Calculate new sizes and positions
*/
+
+ v2s32 size(620, 430);
+
core::rect<s32> rect(
- screensize.X/2 - 580/2,
- screensize.Y/2 - 300/2,
- screensize.X/2 + 580/2,
- screensize.Y/2 + 300/2
+ screensize.X/2 - size.X/2,
+ screensize.Y/2 - size.Y/2,
+ screensize.X/2 + size.X/2,
+ screensize.Y/2 + size.Y/2
);
-
+
DesiredRect = rect;
recalculateAbsolutePosition(false);
- v2s32 size = rect.getSize();
+ //v2s32 size = rect.getSize();
/*
Add stuff
*/
- // Nickname
+ /*
+ Client section
+ */
+
+ v2s32 topleft_client(40, 0);
+ v2s32 size_client = size - v2s32(40, 0);
+
{
- core::rect<s32> rect(0, 0, 100, 20);
- rect = rect + v2s32(size.X/2 - 250, size.Y/2 - 100 + 6);
- const wchar_t *text = L"Nickname";
+ core::rect<s32> rect(0, 0, 20, 125);
+ rect += topleft_client + v2s32(-15, 60);
+ const wchar_t *text = L"C\nL\nI\nE\nN\nT";
+ //gui::IGUIStaticText *t =
Environment->addStaticText(text, rect, false, true, this, -1);
+ //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
}
+
+ // Nickname + password
{
- core::rect<s32> rect(0, 0, 250, 30);
- rect = rect + v2s32(size.X/2 - 130, size.Y/2 - 100);
+ core::rect<s32> rect(0, 0, 110, 20);
+ rect += topleft_client + v2s32(35, 50+6);
+ const wchar_t *text = L"Name/Password";
+ Environment->addStaticText(text, rect, false, true, this, -1);
+ }
+ {
+ core::rect<s32> rect(0, 0, 230, 30);
+ rect += topleft_client + v2s32(160, 50);
gui::IGUIElement *e =
Environment->addEditBox(text_name.c_str(), rect, true, this, 258);
if(text_name == L"")
Environment->setFocus(e);
}
+ {
+ core::rect<s32> rect(0, 0, 120, 30);
+ rect += topleft_client + v2s32(size_client.X-60-100, 50);
+ gui::IGUIEditBox *e =
+ Environment->addEditBox(L"", rect, true, this, 264);
+ e->setPasswordBox(true);
+
+ }
// Address + port
{
- core::rect<s32> rect(0, 0, 100, 20);
- rect = rect + v2s32(size.X/2 - 250, size.Y/2 - 50 + 6);
- const wchar_t *text = L"Address + Port";
+ core::rect<s32> rect(0, 0, 110, 20);
+ rect += topleft_client + v2s32(35, 100+6);
+ const wchar_t *text = L"Address/Port";
Environment->addStaticText(text, rect, false, true, this, -1);
}
{
- core::rect<s32> rect(0, 0, 250, 30);
- rect = rect + v2s32(size.X/2 - 130, size.Y/2 - 50);
+ core::rect<s32> rect(0, 0, 230, 30);
+ rect += topleft_client + v2s32(160, 100);
gui::IGUIElement *e =
Environment->addEditBox(text_address.c_str(), rect, true, this, 256);
if(text_name != L"")
Environment->setFocus(e);
}
{
- core::rect<s32> rect(0, 0, 100, 30);
- rect = rect + v2s32(size.X/2 - 130 + 250 + 20, size.Y/2 - 50);
+ core::rect<s32> rect(0, 0, 120, 30);
+ //rect += topleft_client + v2s32(160+250+20, 125);
+ rect += topleft_client + v2s32(size_client.X-60-100, 100);
Environment->addEditBox(text_port.c_str(), rect, true, this, 257);
}
{
core::rect<s32> rect(0, 0, 400, 20);
- rect = rect + v2s32(size.X/2 - 130, size.Y/2 - 50 + 35);
+ rect += topleft_client + v2s32(160, 100+35);
const wchar_t *text = L"Leave address blank to start a local server.";
Environment->addStaticText(text, rect, false, true, this, -1);
}
- // Server parameters
{
- core::rect<s32> rect(0, 0, 100, 20);
- rect = rect + v2s32(size.X/2 - 250, size.Y/2 + 25 + 6);
- const wchar_t *text = L"Server params";
- Environment->addStaticText(text, rect, false, true, this, -1);
+ core::rect<s32> rect(0, 0, 250, 30);
+ rect += topleft_client + v2s32(35, 150);
+ Environment->addCheckBox(fancy_trees, rect, this, 263,
+ L"Fancy trees");
}
{
core::rect<s32> rect(0, 0, 250, 30);
- rect = rect + v2s32(size.X/2 - 130, size.Y/2 + 25);
- Environment->addCheckBox(creative_mode, rect, this, 259, L"Creative Mode");
+ rect += topleft_client + v2s32(35, 150+30);
+ Environment->addCheckBox(smooth_lighting, rect, this, 262,
+ L"Smooth Lighting");
}
// Start game button
{
core::rect<s32> rect(0, 0, 180, 30);
- rect = rect + v2s32(size.X/2-180/2, size.Y/2-30/2 + 100);
+ //rect += topleft_client + v2s32(size_client.X/2-180/2, 225-30/2);
+ rect += topleft_client + v2s32(size_client.X-180-40, 150+25);
Environment->addButton(rect, this, 257, L"Start Game / Connect");
}
+
+ /*
+ Server section
+ */
+
+ v2s32 topleft_server(40, 250);
+ v2s32 size_server = size - v2s32(40, 0);
+
+ {
+ core::rect<s32> rect(0, 0, 20, 125);
+ rect += topleft_server + v2s32(-15, 40);
+ const wchar_t *text = L"S\nE\nR\nV\nE\nR";
+ //gui::IGUIStaticText *t =
+ Environment->addStaticText(text, rect, false, true, this, -1);
+ //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
+ }
+
+ // Server parameters
+ {
+ core::rect<s32> rect(0, 0, 250, 30);
+ rect += topleft_server + v2s32(35, 30);
+ Environment->addCheckBox(creative_mode, rect, this, 259, L"Creative Mode");
+ }
+ {
+ core::rect<s32> rect(0, 0, 250, 30);
+ rect += topleft_server + v2s32(35, 60);
+ Environment->addCheckBox(enable_damage, rect, this, 261, L"Enable Damage");
+ }
// Map delete button
{
core::rect<s32> rect(0, 0, 130, 30);
- rect = rect + v2s32(size.X/2-130/2+200, size.Y/2-30/2 + 100);
- Environment->addButton(rect, this, 260, L"Delete map");
+ //rect += topleft_server + v2s32(size_server.X-40-130, 100+25);
+ rect += topleft_server + v2s32(40, 100+25);
+ Environment->addButton(rect, this, 260, L"Delete world");
}
}
@@ -192,8 +277,22 @@ void GUIMainMenu::drawMenu()
return;
video::IVideoDriver* driver = Environment->getVideoDriver();
+ /*video::SColor bgcolor(140,0,0,0);
+ driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);*/
+
video::SColor bgcolor(140,0,0,0);
- driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
+
+ {
+ core::rect<s32> rect(0, 0, 620, 230);
+ rect += AbsoluteRect.UpperLeftCorner;
+ driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
+ }
+
+ {
+ core::rect<s32> rect(0, 250, 620, 430);
+ rect += AbsoluteRect.UpperLeftCorner;
+ driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
+ }
gui::IGUIElement::draw();
}
@@ -206,6 +305,11 @@ void GUIMainMenu::acceptInput()
m_data->name = e->getText();
}
{
+ gui::IGUIElement *e = getElementFromId(264);
+ if(e != NULL)
+ m_data->password = e->getText();
+ }
+ {
gui::IGUIElement *e = getElementFromId(256);
if(e != NULL)
m_data->address = e->getText();
@@ -220,6 +324,21 @@ void GUIMainMenu::acceptInput()
if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
m_data->creative_mode = ((gui::IGUICheckBox*)e)->isChecked();
}
+ {
+ gui::IGUIElement *e = getElementFromId(261);
+ if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
+ m_data->enable_damage = ((gui::IGUICheckBox*)e)->isChecked();
+ }
+ {
+ gui::IGUIElement *e = getElementFromId(262);
+ if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
+ m_data->smooth_lighting = ((gui::IGUICheckBox*)e)->isChecked();
+ }
+ {
+ gui::IGUIElement *e = getElementFromId(263);
+ if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
+ m_data->fancy_trees = ((gui::IGUICheckBox*)e)->isChecked();
+ }
m_accepted = true;
}
@@ -274,7 +393,7 @@ bool GUIMainMenu::OnEvent(const SEvent& event)
{
switch(event.GUIEvent.Caller->getID())
{
- case 256: case 257: case 258:
+ case 256: case 257: case 258: case 264:
acceptInput();
quitMenu();
return true;
diff --git a/src/guiMainMenu.h b/src/guiMainMenu.h
index 010d0bf6d..edd519024 100644
--- a/src/guiMainMenu.h
+++ b/src/guiMainMenu.h
@@ -30,14 +30,28 @@ with this program; if not, write to the Free Software Foundation, Inc.,
struct MainMenuData
{
MainMenuData():
+ // Client opts
+ fancy_trees(false),
+ smooth_lighting(false),
+ // Server opts
creative_mode(false),
+ enable_damage(false),
+ // Actions
delete_map(false)
{}
+
// These are in the native format of the gui elements
+
+ // Client options
std::wstring address;
std::wstring port;
std::wstring name;
+ std::wstring password;
+ bool fancy_trees;
+ bool smooth_lighting;
+ // Server options
bool creative_mode;
+ bool enable_damage;
// If map deletion is requested, this is set to true
bool delete_map;
};
diff --git a/src/guiPasswordChange.cpp b/src/guiPasswordChange.cpp
new file mode 100644
index 000000000..ec1cd029a
--- /dev/null
+++ b/src/guiPasswordChange.cpp
@@ -0,0 +1,250 @@
+/*
+Part of Minetest-c55
+Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2011 Ciaran Gultnieks <ciaran@ciarang.com>
+
+Permission to use, copy, modify, and distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#include "guiPasswordChange.h"
+#include "debug.h"
+#include "serialization.h"
+#include <string>
+
+const int ID_oldPassword = 256;
+const int ID_newPassword1 = 257;
+const int ID_newPassword2 = 258;
+const int ID_change = 259;
+const int ID_message = 260;
+
+GUIPasswordChange::GUIPasswordChange(gui::IGUIEnvironment* env,
+ gui::IGUIElement* parent, s32 id,
+ IMenuManager *menumgr,
+ Client* client
+):
+ GUIModalMenu(env, parent, id, menumgr),
+ m_client(client)
+{
+}
+
+GUIPasswordChange::~GUIPasswordChange()
+{
+ removeChildren();
+}
+
+void GUIPasswordChange::removeChildren()
+{
+ {
+ gui::IGUIElement *e = getElementFromId(ID_oldPassword);
+ if(e != NULL)
+ e->remove();
+ }
+ {
+ gui::IGUIElement *e = getElementFromId(ID_newPassword1);
+ if(e != NULL)
+ e->remove();
+ }
+ {
+ gui::IGUIElement *e = getElementFromId(ID_newPassword2);
+ if(e != NULL)
+ e->remove();
+ }
+ {
+ gui::IGUIElement *e = getElementFromId(ID_change);
+ if(e != NULL)
+ e->remove();
+ }
+}
+
+void GUIPasswordChange::regenerateGui(v2u32 screensize)
+{
+ /*
+ Remove stuff
+ */
+ removeChildren();
+
+ /*
+ Calculate new sizes and positions
+ */
+ core::rect<s32> rect(
+ screensize.X/2 - 580/2,
+ screensize.Y/2 - 300/2,
+ screensize.X/2 + 580/2,
+ screensize.Y/2 + 300/2
+ );
+
+ DesiredRect = rect;
+ recalculateAbsolutePosition(false);
+
+ v2s32 size = rect.getSize();
+ v2s32 topleft_client(40, 0);
+ v2s32 size_client = size - v2s32(40, 0);
+
+ /*
+ Add stuff
+ */
+ s32 ypos = 50;
+ {
+ core::rect<s32> rect(0, 0, 110, 20);
+ rect += topleft_client + v2s32(35, ypos+6);
+ const wchar_t *text = L"Old Password";
+ Environment->addStaticText(text, rect, false, true, this, -1);
+ }
+ {
+ core::rect<s32> rect(0, 0, 230, 30);
+ rect += topleft_client + v2s32(160, ypos);
+ gui::IGUIEditBox *e =
+ Environment->addEditBox(L"", rect, true, this, ID_oldPassword);
+ Environment->setFocus(e);
+ e->setPasswordBox(true);
+ }
+ ypos += 50;
+ {
+ core::rect<s32> rect(0, 0, 110, 20);
+ rect += topleft_client + v2s32(35, ypos+6);
+ const wchar_t *text = L"New Password";
+ Environment->addStaticText(text, rect, false, true, this, -1);
+ }
+ {
+ core::rect<s32> rect(0, 0, 230, 30);
+ rect += topleft_client + v2s32(160, ypos);
+ gui::IGUIEditBox *e =
+ Environment->addEditBox(L"", rect, true, this, ID_newPassword1);
+ e->setPasswordBox(true);
+ }
+ ypos += 50;
+ {
+ core::rect<s32> rect(0, 0, 110, 20);
+ rect += topleft_client + v2s32(35, ypos+6);
+ const wchar_t *text = L"Confirm Password";
+ Environment->addStaticText(text, rect, false, true, this, -1);
+ }
+ {
+ core::rect<s32> rect(0, 0, 230, 30);
+ rect += topleft_client + v2s32(160, ypos);
+ gui::IGUIEditBox *e =
+ Environment->addEditBox(L"", rect, true, this, ID_newPassword2);
+ e->setPasswordBox(true);
+ }
+
+ ypos += 50;
+ {
+ core::rect<s32> rect(0, 0, 140, 30);
+ rect = rect + v2s32(size.X/2-140/2, ypos);
+ Environment->addButton(rect, this, ID_change, L"Change");
+ }
+
+ ypos += 50;
+ {
+ core::rect<s32> rect(0, 0, 300, 20);
+ rect += topleft_client + v2s32(35, ypos);
+ const wchar_t *text = L"Passwords do not match!";
+ IGUIElement *e =
+ Environment->addStaticText(text, rect, false, true, this, ID_message);
+ e->setVisible(false);
+ }
+
+}
+
+void GUIPasswordChange::drawMenu()
+{
+ gui::IGUISkin* skin = Environment->getSkin();
+ if (!skin)
+ return;
+ video::IVideoDriver* driver = Environment->getVideoDriver();
+
+ video::SColor bgcolor(140,0,0,0);
+ driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
+
+ gui::IGUIElement::draw();
+}
+
+bool GUIPasswordChange::acceptInput()
+{
+ std::wstring oldpass;
+ std::wstring newpass;
+ gui::IGUIElement *e;
+ e = getElementFromId(ID_oldPassword);
+ if(e != NULL)
+ oldpass = e->getText();
+ e = getElementFromId(ID_newPassword1);
+ if(e != NULL)
+ newpass = e->getText();
+ e = getElementFromId(ID_newPassword2);
+ if(e != NULL && newpass != e->getText())
+ {
+ e = getElementFromId(ID_message);
+ if(e != NULL)
+ e->setVisible(true);
+ return false;
+ }
+ m_client->sendChangePassword(oldpass, newpass);
+ return true;
+}
+
+bool GUIPasswordChange::OnEvent(const SEvent& event)
+{
+ if(event.EventType==EET_KEY_INPUT_EVENT)
+ {
+ if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
+ {
+ quitMenu();
+ return true;
+ }
+ if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
+ {
+ if(acceptInput())
+ quitMenu();
+ return true;
+ }
+ }
+ if(event.EventType==EET_GUI_EVENT)
+ {
+ if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
+ && isVisible())
+ {
+ if(!canTakeFocus(event.GUIEvent.Element))
+ {
+ dstream<<"GUIPasswordChange: Not allowing focus change."
+ <<std::endl;
+ // Returning true disables focus change
+ return true;
+ }
+ }
+ if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
+ {
+ switch(event.GUIEvent.Caller->getID())
+ {
+ case ID_change:
+ if(acceptInput())
+ quitMenu();
+ return true;
+ }
+ }
+ if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
+ {
+ switch(event.GUIEvent.Caller->getID())
+ {
+ case ID_oldPassword:
+ case ID_newPassword1:
+ case ID_newPassword2:
+ if(acceptInput())
+ quitMenu();
+ return true;
+ }
+ }
+ }
+
+ return Parent ? Parent->OnEvent(event) : false;
+}
+
diff --git a/src/guiPasswordChange.h b/src/guiPasswordChange.h
new file mode 100644
index 000000000..1748baade
--- /dev/null
+++ b/src/guiPasswordChange.h
@@ -0,0 +1,55 @@
+/*
+Part of Minetest-c55
+Copyright (C) 2010-11 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2011 Ciaran Gultnieks <ciaran@ciarang.com>
+
+Permission to use, copy, modify, and distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#ifndef GUIPASSWORDCHANGE_HEADER
+#define GUIPASSWORDCHANGE_HEADER
+
+#include "common_irrlicht.h"
+#include "modalMenu.h"
+#include "utility.h"
+#include "client.h"
+#include <string>
+
+class GUIPasswordChange : public GUIModalMenu
+{
+public:
+ GUIPasswordChange(gui::IGUIEnvironment* env,
+ gui::IGUIElement* parent, s32 id,
+ IMenuManager *menumgr,
+ Client* client);
+ ~GUIPasswordChange();
+
+ void removeChildren();
+ /*
+ Remove and re-add (or reposition) stuff
+ */
+ void regenerateGui(v2u32 screensize);
+
+ void drawMenu();
+
+ bool acceptInput();
+
+ bool OnEvent(const SEvent& event);
+
+private:
+ Client* m_client;
+
+};
+
+#endif
+
diff --git a/src/guiPauseMenu.cpp b/src/guiPauseMenu.cpp
index 2d42fdb77..d32d1a10b 100644
--- a/src/guiPauseMenu.cpp
+++ b/src/guiPauseMenu.cpp
@@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "serialization.h"
#include "porting.h"
#include "config.h"
+#include "main.h"
GUIPauseMenu::GUIPauseMenu(gui::IGUIEnvironment* env,
gui::IGUIElement* parent, s32 id,
@@ -64,6 +65,11 @@ void GUIPauseMenu::removeChildren()
if(e != NULL)
e->remove();
}
+ {
+ gui::IGUIElement *e = getElementFromId(261);
+ if(e != NULL)
+ e->remove();
+ }
}
void GUIPauseMenu::regenerateGui(v2u32 screensize)
@@ -91,21 +97,34 @@ void GUIPauseMenu::regenerateGui(v2u32 screensize)
/*
Add stuff
*/
+ const s32 btn_height = 30;
+ const s32 btn_gap = 20;
+ const s32 btn_num = 4;
+ s32 btn_y = size.Y/2-((btn_num*btn_height+(btn_num-1)*btn_gap))/2;
{
- core::rect<s32> rect(0, 0, 140, 30);
- rect = rect + v2s32(size.X/2-140/2, size.Y/2-30/2-50);
+ core::rect<s32> rect(0, 0, 140, btn_height);
+ rect = rect + v2s32(size.X/2-140/2, btn_y);
Environment->addButton(rect, this, 256, L"Continue");
}
+ btn_y += btn_height + btn_gap;
+ {
+ core::rect<s32> rect(0, 0, 140, btn_height);
+ rect = rect + v2s32(size.X/2-140/2, btn_y);
+ Environment->addButton(rect, this, 261, L"Change Password");
+ }
+ btn_y += btn_height + btn_gap;
{
- core::rect<s32> rect(0, 0, 140, 30);
- rect = rect + v2s32(size.X/2-140/2, size.Y/2-30/2+0);
+ core::rect<s32> rect(0, 0, 140, btn_height);
+ rect = rect + v2s32(size.X/2-140/2, btn_y);
Environment->addButton(rect, this, 260, L"Disconnect");
}
+ btn_y += btn_height + btn_gap;
{
- core::rect<s32> rect(0, 0, 140, 30);
- rect = rect + v2s32(size.X/2-140/2, size.Y/2-30/2+50);
+ core::rect<s32> rect(0, 0, 140, btn_height);
+ rect = rect + v2s32(size.X/2-140/2, btn_y);
Environment->addButton(rect, this, 257, L"Exit to OS");
}
+
{
core::rect<s32> rect(0, 0, 180, 240);
rect = rect + v2s32(size.X/2 + 90, size.Y/2-rect.getHeight()/2);
@@ -172,6 +191,7 @@ void GUIPauseMenu::drawMenu()
bool GUIPauseMenu::OnEvent(const SEvent& event)
{
+
if(event.EventType==EET_KEY_INPUT_EVENT)
{
if(event.KeyInput.PressedDown)
@@ -209,6 +229,10 @@ bool GUIPauseMenu::OnEvent(const SEvent& event)
quitMenu();
// ALWAYS return immediately after quitMenu()
return true;
+ case 261:
+ quitMenu();
+ m_gamecallback->changePassword();
+ return true;
case 260: // disconnect
m_gamecallback->disconnect();
quitMenu();
diff --git a/src/guiPauseMenu.h b/src/guiPauseMenu.h
index 22cb65b2c..64e3c71f1 100644
--- a/src/guiPauseMenu.h
+++ b/src/guiPauseMenu.h
@@ -28,6 +28,7 @@ class IGameCallback
public:
virtual void exitToOS() = 0;
virtual void disconnect() = 0;
+ virtual void changePassword() = 0;
};
class GUIPauseMenu : public GUIModalMenu
diff --git a/src/inventory.cpp b/src/inventory.cpp
index c29bb9470..47a8d4de9 100644
--- a/src/inventory.cpp
+++ b/src/inventory.cpp
@@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "debug.h"
#include <sstream>
#include "main.h"
+#include "serverobject.h"
/*
InventoryItem
@@ -90,23 +91,142 @@ InventoryItem* InventoryItem::deSerialize(std::istream &is)
}
}
+ServerActiveObject* InventoryItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
+{
+ /*
+ Create an ItemSAO
+ */
+ // Get item string
+ std::ostringstream os(std::ios_base::binary);
+ serialize(os);
+ // Create object
+ ServerActiveObject *obj = new ItemSAO(env, 0, pos, os.str());
+ return obj;
+}
+
+/*
+ MaterialItem
+*/
+
+bool MaterialItem::isCookable()
+{
+ if(m_content == CONTENT_TREE)
+ {
+ return true;
+ }
+ else if(m_content == CONTENT_COBBLE)
+ {
+ return true;
+ }
+ else if(m_content == CONTENT_SAND)
+ {
+ return true;
+ }
+ return false;
+}
+
+InventoryItem *MaterialItem::createCookResult()
+{
+ if(m_content == CONTENT_TREE)
+ {
+ return new CraftItem("lump_of_coal", 1);
+ }
+ else if(m_content == CONTENT_COBBLE)
+ {
+ return new MaterialItem(CONTENT_STONE, 1);
+ }
+ else if(m_content == CONTENT_SAND)
+ {
+ return new MaterialItem(CONTENT_GLASS, 1);
+ }
+ return NULL;
+}
+
+/*
+ CraftItem
+*/
+
+#ifndef SERVER
+video::ITexture * CraftItem::getImage()
+{
+ if(g_texturesource == NULL)
+ return NULL;
+
+ std::string name;
+
+ if(m_subname == "Stick")
+ name = "stick.png";
+ else if(m_subname == "lump_of_coal")
+ name = "lump_of_coal.png";
+ else if(m_subname == "lump_of_iron")
+ name = "lump_of_iron.png";
+ else if(m_subname == "steel_ingot")
+ name = "steel_ingot.png";
+ else if(m_subname == "rat")
+ name = "rat.png";
+ else
+ name = "cloud.png";
+
+ // Get such a texture
+ return g_texturesource->getTextureRaw(name);
+}
+#endif
+
+ServerActiveObject* CraftItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
+{
+ // Special cases
+ if(m_subname == "rat")
+ {
+ ServerActiveObject *obj = new RatSAO(env, id, pos);
+ return obj;
+ }
+ // Default
+ else
+ {
+ return InventoryItem::createSAO(env, id, pos);
+ }
+}
+
+u16 CraftItem::getDropCount()
+{
+ // Special cases
+ if(m_subname == "rat")
+ return 1;
+ // Default
+ else
+ return InventoryItem::getDropCount();
+}
+
+bool CraftItem::isCookable()
+{
+ if(m_subname == "lump_of_iron")
+ {
+ return true;
+ }
+ return false;
+}
+
+InventoryItem *CraftItem::createCookResult()
+{
+ if(m_subname == "lump_of_iron")
+ {
+ return new CraftItem("steel_ingot", 1);
+ }
+ return NULL;
+}
+
/*
MapBlockObjectItem
+ TODO: Remove
*/
#ifndef SERVER
video::ITexture * MapBlockObjectItem::getImage()
{
- //TODO
-
if(m_inventorystring.substr(0,3) == "Rat")
- //return g_device->getVideoDriver()->getTexture(porting::getDataPath("rat.png").c_str());
- //return g_irrlicht->getTexture("rat.png");
- return NULL;
+ return g_texturesource->getTextureRaw("rat.png");
if(m_inventorystring.substr(0,4) == "Sign")
- //return g_device->getVideoDriver()->getTexture(porting::getDataPath("sign.png").c_str());
- //return g_irrlicht->getTexture("sign.png");
- return NULL;
+ return g_texturesource->getTextureRaw("sign.png");
return NULL;
}
@@ -175,6 +295,7 @@ InventoryList::InventoryList(std::string name, u32 size)
m_name = name;
m_size = size;
clearItems();
+ //m_dirty = false;
}
InventoryList::~InventoryList()
@@ -200,6 +321,8 @@ void InventoryList::clearItems()
{
m_items.push_back(NULL);
}
+
+ //setDirty(true);
}
void InventoryList::serialize(std::ostream &os)
@@ -293,6 +416,7 @@ InventoryList & InventoryList::operator = (const InventoryList &other)
m_items[i] = item->clone();
}
}
+ //setDirty(true);
return *this;
}
@@ -319,6 +443,11 @@ u32 InventoryList::getUsedSlots()
return num;
}
+u32 InventoryList::getFreeSlots()
+{
+ return getSize() - getUsedSlots();
+}
+
InventoryItem * InventoryList::getItem(u32 i)
{
if(i > m_items.size() - 1)
@@ -332,6 +461,7 @@ InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
InventoryItem *olditem = m_items[i];
m_items[i] = newitem;
+ //setDirty(true);
return olditem;
}
@@ -345,6 +475,9 @@ void InventoryList::deleteItem(u32 i)
InventoryItem * InventoryList::addItem(InventoryItem *newitem)
{
+ if(newitem == NULL)
+ return NULL;
+
/*
First try to find if it could be added to some existing items
*/
@@ -379,6 +512,11 @@ InventoryItem * InventoryList::addItem(InventoryItem *newitem)
InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
{
+ if(newitem == NULL)
+ return NULL;
+
+ //setDirty(true);
+
// If it is an empty position, it's an easy job.
InventoryItem *to_item = m_items[i];
if(to_item == NULL)
@@ -409,10 +547,34 @@ InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
}
}
+bool InventoryList::itemFits(u32 i, InventoryItem *newitem)
+{
+ // If it is an empty position, it's an easy job.
+ InventoryItem *to_item = m_items[i];
+ if(to_item == NULL)
+ {
+ return true;
+ }
+
+ // If not addable, return the item
+ if(newitem->addableTo(to_item) == false)
+ return false;
+
+ // If the item fits fully in the slot, add counter and delete it
+ if(newitem->getCount() <= to_item->freeSpace())
+ {
+ return true;
+ }
+
+ return false;
+}
+
InventoryItem * InventoryList::takeItem(u32 i, u32 count)
{
if(count == 0)
return NULL;
+
+ //setDirty(true);
InventoryItem *item = m_items[i];
// If it is an empty position, return NULL
@@ -608,12 +770,27 @@ InventoryAction * InventoryAction::deSerialize(std::istream &is)
return a;
}
-void IMoveAction::apply(Inventory *inventory)
+void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
{
- /*dstream<<"from_name="<<from_name<<" to_name="<<to_name<<std::endl;
+#if 1
+
+ /*dstream<<"from_inv="<<from_inv<<" to_inv="<<to_inv<<std::endl;
+ dstream<<"from_list="<<from_list<<" to_list="<<to_list<<std::endl;
dstream<<"from_i="<<from_i<<" to_i="<<to_i<<std::endl;*/
- InventoryList *list_from = inventory->getList(from_name);
- InventoryList *list_to = inventory->getList(to_name);
+
+ Inventory *inv_from = mgr->getInventory(c, from_inv);
+ Inventory *inv_to = mgr->getInventory(c, to_inv);
+
+ if(!inv_from || !inv_to)
+ {
+ dstream<<__FUNCTION_NAME<<": Operation not allowed "
+ <<"(inventories not found)"<<std::endl;
+ return;
+ }
+
+ InventoryList *list_from = inv_from->getList(from_list);
+ InventoryList *list_to = inv_to->getList(to_list);
+
/*dstream<<"list_from="<<list_from<<" list_to="<<list_to
<<std::endl;*/
/*if(list_from)
@@ -625,12 +802,28 @@ void IMoveAction::apply(Inventory *inventory)
/*
If a list doesn't exist or the source item doesn't exist
- or the source and the destination slots are the same
*/
- if(!list_from || !list_to || list_from->getItem(from_i) == NULL
- || (list_from == list_to && from_i == to_i))
+ if(!list_from || !list_to)
+ {
+ dstream<<__FUNCTION_NAME<<": Operation not allowed "
+ <<"(a list doesn't exist)"
+ <<std::endl;
+ return;
+ }
+ if(list_from->getItem(from_i) == NULL)
+ {
+ dstream<<__FUNCTION_NAME<<": Operation not allowed "
+ <<"(the source item doesn't exist)"
+ <<std::endl;
+ return;
+ }
+ /*
+ If the source and the destination slots are the same
+ */
+ if(inv_from == inv_to && list_from == list_to && from_i == to_i)
{
- dstream<<__FUNCTION_NAME<<": Operation not allowed"<<std::endl;
+ dstream<<__FUNCTION_NAME<<": Operation not allowed "
+ <<"(source and the destination slots are the same)"<<std::endl;
return;
}
@@ -645,29 +838,160 @@ void IMoveAction::apply(Inventory *inventory)
InventoryItem *olditem = item1;
item1 = list_to->addItem(to_i, item1);
- // If nothing is returned, the item was fully added
- if(item1 == NULL)
- return;
+ // If something is returned, the item was not fully added
+ if(item1 != NULL)
+ {
+ // If olditem is returned, nothing was added.
+ bool nothing_added = (item1 == olditem);
+
+ // If something else is returned, part of the item was left unadded.
+ // Add the other part back to the source item
+ list_from->addItem(from_i, item1);
+
+ // If olditem is returned, nothing was added.
+ // Swap the items
+ if(nothing_added)
+ {
+ // Take item from source list
+ item1 = list_from->changeItem(from_i, NULL);
+ // Adding was not possible, swap the items.
+ InventoryItem *item2 = list_to->changeItem(to_i, item1);
+ // Put item from destination list to the source list
+ list_from->changeItem(from_i, item2);
+ }
+ }
+
+ mgr->inventoryModified(c, from_inv);
+ if(from_inv != to_inv)
+ mgr->inventoryModified(c, to_inv);
+#endif
+}
+
+/*
+ Craft checking system
+*/
+
+bool ItemSpec::checkItem(InventoryItem *item)
+{
+ if(type == ITEM_NONE)
+ {
+ // Has to be no item
+ if(item != NULL)
+ return false;
+ return true;
+ }
- // If olditem is returned, nothing was added.
- bool nothing_added = (item1 == olditem);
+ // There should be an item
+ if(item == NULL)
+ return false;
+
+ std::string itemname = item->getName();
+
+ if(type == ITEM_MATERIAL)
+ {
+ if(itemname != "MaterialItem")
+ return false;
+ MaterialItem *mitem = (MaterialItem*)item;
+ if(mitem->getMaterial() != num)
+ return false;
+ }
+ else if(type == ITEM_CRAFT)
+ {
+ if(itemname != "CraftItem")
+ return false;
+ CraftItem *mitem = (CraftItem*)item;
+ if(mitem->getSubName() != name)
+ return false;
+ }
+ else if(type == ITEM_TOOL)
+ {
+ // Not supported yet
+ assert(0);
+ }
+ else if(type == ITEM_MBO)
+ {
+ // Not supported yet
+ assert(0);
+ }
+ else
+ {
+ // Not supported yet
+ assert(0);
+ }
+ return true;
+}
+
+bool checkItemCombination(InventoryItem **items, ItemSpec *specs)
+{
+ u16 items_min_x = 100;
+ u16 items_max_x = 100;
+ u16 items_min_y = 100;
+ u16 items_max_y = 100;
+ for(u16 y=0; y<3; y++)
+ for(u16 x=0; x<3; x++)
+ {
+ if(items[y*3 + x] == NULL)
+ continue;
+ if(items_min_x == 100 || x < items_min_x)
+ items_min_x = x;
+ if(items_min_y == 100 || y < items_min_y)
+ items_min_y = y;
+ if(items_max_x == 100 || x > items_max_x)
+ items_max_x = x;
+ if(items_max_y == 100 || y > items_max_y)
+ items_max_y = y;
+ }
+ // No items at all, just return false
+ if(items_min_x == 100)
+ return false;
- // If something else is returned, part of the item was left unadded.
- // Add the other part back to the source item
- list_from->addItem(from_i, item1);
+ u16 items_w = items_max_x - items_min_x + 1;
+ u16 items_h = items_max_y - items_min_y + 1;
- // If olditem is returned, nothing was added.
- // Swap the items
- if(nothing_added)
+ u16 specs_min_x = 100;
+ u16 specs_max_x = 100;
+ u16 specs_min_y = 100;
+ u16 specs_max_y = 100;
+ for(u16 y=0; y<3; y++)
+ for(u16 x=0; x<3; x++)
{
- // Take item from source list
- item1 = list_from->changeItem(from_i, NULL);
- // Adding was not possible, swap the items.
- InventoryItem *item2 = list_to->changeItem(to_i, item1);
- // Put item from destination list to the source list
- list_from->changeItem(from_i, item2);
- return;
+ if(specs[y*3 + x].type == ITEM_NONE)
+ continue;
+ if(specs_min_x == 100 || x < specs_min_x)
+ specs_min_x = x;
+ if(specs_min_y == 100 || y < specs_min_y)
+ specs_min_y = y;
+ if(specs_max_x == 100 || x > specs_max_x)
+ specs_max_x = x;
+ if(specs_max_y == 100 || y > specs_max_y)
+ specs_max_y = y;
+ }
+ // No specs at all, just return false
+ if(specs_min_x == 100)
+ return false;
+
+ u16 specs_w = specs_max_x - specs_min_x + 1;
+ u16 specs_h = specs_max_y - specs_min_y + 1;
+
+ // Different sizes
+ if(items_w != specs_w || items_h != specs_h)
+ return false;
+
+ for(u16 y=0; y<specs_h; y++)
+ for(u16 x=0; x<specs_w; x++)
+ {
+ u16 items_x = items_min_x + x;
+ u16 items_y = items_min_y + y;
+ u16 specs_x = specs_min_x + x;
+ u16 specs_y = specs_min_y + y;
+ InventoryItem *item = items[items_y * 3 + items_x];
+ ItemSpec &spec = specs[specs_y * 3 + specs_x];
+
+ if(spec.checkItem(item) == false)
+ return false;
}
+
+ return true;
}
//END
diff --git a/src/inventory.h b/src/inventory.h
index 9155eb025..07d81a3f7 100644
--- a/src/inventory.h
+++ b/src/inventory.h
@@ -35,6 +35,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define QUANTITY_ITEM_MAX_COUNT 99
+class ServerActiveObject;
+class ServerEnvironment;
+
class InventoryItem
{
public:
@@ -54,6 +57,14 @@ public:
#endif
// Shall return a text to show in the GUI
virtual std::string getText() { return ""; }
+ // Creates an object from the item, to be placed in the world.
+ virtual ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
+ // Gets amount of items that dropping one SAO will decrement
+ virtual u16 getDropCount(){ return getCount(); }
+
+ /*
+ Quantity methods
+ */
// Shall return true if the item can be add()ed to the other
virtual bool addableTo(InventoryItem *other)
@@ -61,9 +72,6 @@ public:
return false;
}
- /*
- Quantity methods
- */
u16 getCount()
{
return m_count;
@@ -72,6 +80,7 @@ public:
{
m_count = count;
}
+ // This should return something else for stackable items
virtual u16 freeSpace()
{
return 0;
@@ -87,6 +96,16 @@ public:
m_count -= count;
}
+ /*
+ Other properties
+ */
+ // Whether it can be cooked
+ virtual bool isCookable(){return false;}
+ // Time of cooking
+ virtual float getCookTime(){return 3.0;}
+ // Result of cooking
+ virtual InventoryItem *createCookResult(){return NULL;}
+
protected:
u16 m_count;
};
@@ -149,6 +168,11 @@ public:
return QUANTITY_ITEM_MAX_COUNT - m_count;
}
/*
+ Other properties
+ */
+ bool isCookable();
+ InventoryItem *createCookResult();
+ /*
Special methods
*/
u8 getMaterial()
@@ -159,6 +183,7 @@ private:
u8 m_content;
};
+//TODO: Remove
class MapBlockObjectItem : public InventoryItem
{
public:
@@ -246,26 +271,7 @@ public:
return new CraftItem(m_subname, m_count);
}
#ifndef SERVER
- video::ITexture * getImage()
- {
- if(g_texturesource == NULL)
- return NULL;
-
- std::string name;
-
- if(m_subname == "Stick")
- name = "stick.png";
- else if(m_subname == "lump_of_coal")
- name = "lump_of_coal.png";
- else if(m_subname == "lump_of_iron")
- name = "lump_of_iron.png";
- else
- name = "cloud.png";
-
- // Get such a texture
- //return g_irrlicht->getTexture(name);
- return g_texturesource->getTextureRaw(name);
- }
+ video::ITexture * getImage();
#endif
std::string getText()
{
@@ -273,6 +279,10 @@ public:
os<<m_count;
return os.str();
}
+
+ ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
+ u16 getDropCount();
+
virtual bool addableTo(InventoryItem *other)
{
if(std::string(other->getName()) != "CraftItem")
@@ -289,6 +299,11 @@ public:
return QUANTITY_ITEM_MAX_COUNT - m_count;
}
/*
+ Other properties
+ */
+ bool isCookable();
+ InventoryItem *createCookResult();
+ /*
Special methods
*/
std::string getSubName()
@@ -335,11 +350,31 @@ public:
std::string basename;
if(m_toolname == "WPick")
- basename = "tool_wpick.png";
+ basename = "tool_woodpick.png";
else if(m_toolname == "STPick")
- basename = "tool_stpick.png";
+ basename = "tool_stonepick.png";
+ else if(m_toolname == "SteelPick")
+ basename = "tool_steelpick.png";
else if(m_toolname == "MesePick")
basename = "tool_mesepick.png";
+ else if(m_toolname == "WShovel")
+ basename = "tool_woodshovel.png";
+ else if(m_toolname == "STShovel")
+ basename = "tool_stoneshovel.png";
+ else if(m_toolname == "SteelShovel")
+ basename = "tool_steelshovel.png";
+ else if(m_toolname == "WAxe")
+ basename = "tool_woodaxe.png";
+ else if(m_toolname == "STAxe")
+ basename = "tool_stoneaxe.png";
+ else if(m_toolname == "SteelAxe")
+ basename = "tool_steelaxe.png";
+ else if(m_toolname == "WSword")
+ basename = "tool_woodsword.png";
+ else if(m_toolname == "STSword")
+ basename = "tool_stonesword.png";
+ else if(m_toolname == "SteelSword")
+ basename = "tool_steelsword.png";
else
basename = "cloud.png";
@@ -355,11 +390,6 @@ public:
os<<basename<<"^[progressbar"<<value_f;
return g_texturesource->getTextureRaw(os.str());
-
- /*TextureSpec spec;
- spec.addTid(g_irrlicht->getTextureId(basename));
- spec.addTid(g_irrlicht->getTextureId(os.str()));
- return g_irrlicht->getTexture(spec);*/
}
#endif
std::string getText()
@@ -428,6 +458,10 @@ public:
u32 getSize();
// Count used slots
u32 getUsedSlots();
+ u32 getFreeSlots();
+
+ /*bool getDirty(){ return m_dirty; }
+ void setDirty(bool dirty=true){ m_dirty = dirty; }*/
// Get pointer to item
InventoryItem * getItem(u32 i);
@@ -435,6 +469,7 @@ public:
InventoryItem * changeItem(u32 i, InventoryItem *newitem);
// Delete item
void deleteItem(u32 i);
+
// Adds an item to a suitable place. Returns leftover item.
// If all went into the list, returns NULL.
InventoryItem * addItem(InventoryItem *newitem);
@@ -445,6 +480,9 @@ public:
// If can be added fully, NULL is returned.
InventoryItem * addItem(u32 i, InventoryItem *newitem);
+ // Checks whether the item could be added to the given slot
+ bool itemFits(u32 i, InventoryItem *newitem);
+
// Takes some items from a slot.
// If there are not enough, takes as many as it can.
// Returns NULL if couldn't take any.
@@ -459,6 +497,7 @@ private:
core::array<InventoryItem*> m_items;
u32 m_size;
std::string m_name;
+ //bool m_dirty;
};
class Inventory
@@ -495,6 +534,41 @@ private:
core::array<InventoryList*> m_lists;
};
+class Player;
+
+struct InventoryContext
+{
+ Player *current_player;
+
+ InventoryContext():
+ current_player(NULL)
+ {}
+};
+
+class InventoryAction;
+
+class InventoryManager
+{
+public:
+ InventoryManager(){}
+ virtual ~InventoryManager(){}
+
+ /*
+ Get a pointer to an inventory specified by id.
+ id can be:
+ - "current_player"
+ - "nodemeta:X,Y,Z"
+ */
+ virtual Inventory* getInventory(InventoryContext *c, std::string id)
+ {return NULL;}
+ // Used on the server by InventoryAction::apply and other stuff
+ virtual void inventoryModified(InventoryContext *c, std::string id)
+ {}
+ // Used on the client
+ virtual void inventoryAction(InventoryAction *a)
+ {}
+};
+
#define IACTION_MOVE 0
struct InventoryAction
@@ -503,16 +577,18 @@ struct InventoryAction
virtual u16 getType() const = 0;
virtual void serialize(std::ostream &os) = 0;
- virtual void apply(Inventory *inventory) = 0;
+ virtual void apply(InventoryContext *c, InventoryManager *mgr) = 0;
};
struct IMoveAction : public InventoryAction
{
// count=0 means "everything"
u16 count;
- std::string from_name;
+ std::string from_inv;
+ std::string from_list;
s16 from_i;
- std::string to_name;
+ std::string to_inv;
+ std::string to_list;
s16 to_i;
IMoveAction()
@@ -528,12 +604,16 @@ struct IMoveAction : public InventoryAction
std::getline(is, ts, ' ');
count = stoi(ts);
- std::getline(is, from_name, ' ');
+ std::getline(is, from_inv, ' ');
+
+ std::getline(is, from_list, ' ');
std::getline(is, ts, ' ');
from_i = stoi(ts);
- std::getline(is, to_name, ' ');
+ std::getline(is, to_inv, ' ');
+
+ std::getline(is, to_list, ' ');
std::getline(is, ts, ' ');
to_i = stoi(ts);
@@ -548,14 +628,62 @@ struct IMoveAction : public InventoryAction
{
os<<"Move ";
os<<count<<" ";
- os<<from_name<<" ";
+ os<<from_inv<<" ";
+ os<<from_list<<" ";
os<<from_i<<" ";
- os<<to_name<<" ";
+ os<<to_inv<<" ";
+ os<<to_list<<" ";
os<<to_i;
}
- void apply(Inventory *inventory);
+ void apply(InventoryContext *c, InventoryManager *mgr);
+};
+
+/*
+ Craft checking system
+*/
+
+enum ItemSpecType
+{
+ ITEM_NONE,
+ ITEM_MATERIAL,
+ ITEM_CRAFT,
+ ITEM_TOOL,
+ ITEM_MBO
+};
+
+struct ItemSpec
+{
+ enum ItemSpecType type;
+ // Only other one of these is used
+ std::string name;
+ u16 num;
+
+ ItemSpec():
+ type(ITEM_NONE)
+ {
+ }
+ ItemSpec(enum ItemSpecType a_type, std::string a_name):
+ type(a_type),
+ name(a_name),
+ num(65535)
+ {
+ }
+ ItemSpec(enum ItemSpecType a_type, u16 a_num):
+ type(a_type),
+ name(""),
+ num(a_num)
+ {
+ }
+
+ bool checkItem(InventoryItem *item);
};
+/*
+ items: a pointer to an array of 9 pointers to items
+ specs: a pointer to an array of 9 ItemSpecs
+*/
+bool checkItemCombination(InventoryItem **items, ItemSpec *specs);
+
#endif
diff --git a/src/irrlichtwrapper.cpp b/src/irrlichtwrapper.cpp
deleted file mode 100644
index 4a1f2c413..000000000
--- a/src/irrlichtwrapper.cpp
+++ /dev/null
@@ -1,452 +0,0 @@
-#include "irrlichtwrapper.h"
-#include "constants.h"
-#include "string.h"
-#include "strfnd.h"
-
-IrrlichtWrapper::IrrlichtWrapper(IrrlichtDevice *device)
-{
- m_running = true;
- m_main_thread = get_current_thread_id();
- m_device_mutex.Init();
- m_device = device;
-}
-
-IrrlichtWrapper::~IrrlichtWrapper()
-{
-#if 0
- // Clear image cache
- for(core::map<std::string, video::IImage*>::Iterator
- i = m_imagecache.getIterator();
- i.atEnd() == false; i++)
- {
- i.getNode()->getValue()->drop();
- }
-#endif
-}
-
-void IrrlichtWrapper::Run()
-{
-#if 0
- /*
- Fetch textures
- */
- if(m_get_texture_queue.size() > 0)
- {
- GetRequest<TextureSpec, video::ITexture*, u8, u8>
- request = m_get_texture_queue.pop();
-
- dstream<<"got texture request with"
- <<" key.tids[0]="<<request.key.tids[0]
- <<" [1]="<<request.key.tids[1]
- <<std::endl;
-
- GetResult<TextureSpec, video::ITexture*, u8, u8>
- result;
- result.key = request.key;
- result.callers = request.callers;
- result.item = getTextureDirect(request.key);
-
- request.dest->push_back(result);
- }
-#endif
-}
-
-void IrrlichtWrapper::Shutdown(bool shutdown)
-{
- m_running = !shutdown;
-}
-
-IrrlichtDevice* IrrlichtWrapper::getDevice()
-{
- if(get_current_thread_id() != m_main_thread)
- {
- dstream<<"WARNING: IrrlichtWrapper::getDevice() called "
- "not from main thread"<<std::endl;
- return NULL;
- }
- return m_device;
-}
-
-#if 0
-textureid_t IrrlichtWrapper::getTextureId(const std::string &name)
-{
- u32 id = m_namecache.getId(name);
- return id;
-}
-
-std::string IrrlichtWrapper::getTextureName(textureid_t id)
-{
- std::string name("");
- m_namecache.getValue(id, name);
- // In case it was found, return the name; otherwise return an empty name.
- return name;
-}
-
-video::ITexture* IrrlichtWrapper::getTexture(const std::string &filename)
-{
- TextureSpec spec(getTextureId(filename));
- return getTexture(spec);
-}
-
-video::ITexture* IrrlichtWrapper::getTexture(const TextureSpec &spec)
-{
- if(spec.empty())
- return NULL;
-
- video::ITexture *t = m_texturecache.get(spec);
- if(t != NULL)
- return t;
-
- if(get_current_thread_id() == m_main_thread)
- {
- dstream<<"Getting texture directly: spec.tids[0]="
- <<spec.tids[0]<<std::endl;
-
- t = getTextureDirect(spec);
- }
- else
- {
- // If irrlicht has shut down, just return NULL
- if(m_running == false)
- return NULL;
-
- // We're gonna ask the result to be put into here
- ResultQueue<TextureSpec, video::ITexture*, u8, u8> result_queue;
-
- // Throw a request in
- m_get_texture_queue.add(spec, 0, 0, &result_queue);
-
- dstream<<"Waiting for texture from main thread: spec.tids[0]="
- <<spec.tids[0]<<std::endl;
-
- try
- {
- // Wait result for a second
- GetResult<TextureSpec, video::ITexture*, u8, u8>
- result = result_queue.pop_front(1000);
-
- // Check that at least something worked OK
- assert(result.key == spec);
-
- t = result.item;
- }
- catch(ItemNotFoundException &e)
- {
- dstream<<"Waiting for texture timed out."<<std::endl;
- t = NULL;
- }
- }
-
- // Add to cache and return
- m_texturecache.set(spec, t);
- return t;
-}
-
-// Draw a progress bar on the image
-void make_progressbar(float value, video::IImage *image);
-
-/*
- Texture fetcher/maker function, called always from the main thread
-*/
-
-video::ITexture* IrrlichtWrapper::getTextureDirect(const TextureSpec &spec)
-{
- // This would result in NULL image
- if(spec.empty())
- return NULL;
-
- // Don't generate existing stuff
- video::ITexture *t = m_texturecache.get(spec);
- if(t != NULL)
- {
- dstream<<"WARNING: Existing stuff requested from "
- "getTextureDirect()"<<std::endl;
- return t;
- }
-
- video::IVideoDriver* driver = m_device->getVideoDriver();
-
- /*
- An image will be built from files and then converted into a texture.
- */
- video::IImage *baseimg = NULL;
-
- /*
- Irrlicht requires a name for every texture, with which it
- will be stored internally in irrlicht.
- */
- std::string texture_name;
-
- for(u32 i=0; i<TEXTURE_SPEC_TEXTURE_COUNT; i++)
- {
- textureid_t tid = spec.tids[i];
- if(tid == 0)
- continue;
-
- std::string name = getTextureName(tid);
-
- // Add something to the name so that it is a unique identifier.
- texture_name += "[";
- texture_name += name;
- texture_name += "]";
-
- /*
- Try to get image from image cache
- */
- {
- core::map<std::string, video::IImage*>::Node *n;
- n = m_imagecache.find(texture_name);
- if(n != NULL)
- {
- video::IImage *image = n->getValue();
-
- core::dimension2d<u32> dim = image->getDimension();
- baseimg = driver->createImage(video::ECF_A8R8G8B8, dim);
- image->copyTo(baseimg);
-
- dstream<<"INFO: getTextureDirect(): Loaded \""
- <<texture_name<<"\" from image cache"
- <<std::endl;
-
- // Do not process any further.
- continue;
- }
- }
-
- // Stuff starting with [ are special commands
- if(name[0] != '[')
- {
- // A normal texture; load it from a file
- std::string path = porting::getDataPath(name.c_str());
- dstream<<"INFO: getTextureDirect(): Loading path \""<<path
- <<"\""<<std::endl;
-
- // DEBUG
- /*{
- dstream<<"DEBUG CODE: Loading base image "
- "directly to texture"<<std::endl;
- t = driver->getTexture(path.c_str());
- driver->renameTexture(t, texture_name.c_str());
- return t;
- }*/
-
- video::IImage *image = driver->createImageFromFile(path.c_str());
-
- if(image == NULL)
- {
- dstream<<"WARNING: Could not load image \""<<name
- <<"\" from path \""<<path<<"\""
- <<" while building texture"<<std::endl;
- continue;
- }
-
- // If base image is NULL, load as base.
- if(baseimg == NULL)
- {
- dstream<<"INFO: Setting "<<name<<" as base"<<std::endl;
- /*
- Copy it this way to get an alpha channel.
- Otherwise images with alpha cannot be blitted on
- images that don't have alpha in the original file.
- */
- // This is a deprecated method
- //baseimg = driver->createImage(video::ECF_A8R8G8B8, image);
- core::dimension2d<u32> dim = image->getDimension();
- baseimg = driver->createImage(video::ECF_A8R8G8B8, dim);
- image->copyTo(baseimg);
- image->drop();
- //baseimg = image;
- }
- // Else blit on base.
- else
- {
- dstream<<"INFO: Blitting "<<name<<" on base"<<std::endl;
- // Size of the copied area
- core::dimension2d<u32> dim = image->getDimension();
- //core::dimension2d<u32> dim(16,16);
- // Position to copy the blitted to in the base image
- core::position2d<s32> pos_to(0,0);
- // Position to copy the blitted from in the blitted image
- core::position2d<s32> pos_from(0,0);
- // Blit
- image->copyToWithAlpha(baseimg, pos_to,
- core::rect<s32>(pos_from, dim),
- video::SColor(255,255,255,255),
- NULL);
- // Drop image
- image->drop();
- }
- }
- else
- {
- // A special texture modification
- dstream<<"INFO: getTextureDirect(): generating \""<<name<<"\""
- <<std::endl;
- if(name.substr(0,6) == "[crack")
- {
- u16 progression = stoi(name.substr(6));
- // Size of the base image
- core::dimension2d<u32> dim_base = baseimg->getDimension();
- // Crack will be drawn at this size
- u32 cracksize = 16;
- // Size of the crack image
- core::dimension2d<u32> dim_crack(cracksize,cracksize);
- // Position to copy the crack from in the crack image
- core::position2d<s32> pos_other(0, 16 * progression);
-
- video::IImage *crackimage = driver->createImageFromFile(
- porting::getDataPath("crack.png").c_str());
-
- if(crackimage)
- {
- /*crackimage->copyToWithAlpha(baseimg, v2s32(0,0),
- core::rect<s32>(pos_other, dim_base),
- video::SColor(255,255,255,255),
- NULL);*/
-
- for(u32 y0=0; y0<dim_base.Height/dim_crack.Height; y0++)
- for(u32 x0=0; x0<dim_base.Width/dim_crack.Width; x0++)
- {
- // Position to copy the crack to in the base image
- core::position2d<s32> pos_base(x0*cracksize, y0*cracksize);
- crackimage->copyToWithAlpha(baseimg, pos_base,
- core::rect<s32>(pos_other, dim_crack),
- video::SColor(255,255,255,255),
- NULL);
- }
-
- crackimage->drop();
- }
- }
- else if(name.substr(0,8) == "[combine")
- {
- // "[combine:16x128:0,0=stone.png:0,16=grass.png"
- Strfnd sf(name);
- sf.next(":");
- u32 w0 = stoi(sf.next("x"));
- u32 h0 = stoi(sf.next(":"));
- dstream<<"INFO: combined w="<<w0<<" h="<<h0<<std::endl;
- core::dimension2d<u32> dim(w0,h0);
- baseimg = driver->createImage(video::ECF_A8R8G8B8, dim);
- while(sf.atend() == false)
- {
- u32 x = stoi(sf.next(","));
- u32 y = stoi(sf.next("="));
- std::string filename = sf.next(":");
- dstream<<"INFO: Adding \""<<filename
- <<"\" to combined ("<<x<<","<<y<<")"
- <<std::endl;
- video::IImage *img = driver->createImageFromFile(
- porting::getDataPath(filename.c_str()).c_str());
- if(img)
- {
- core::dimension2d<u32> dim = img->getDimension();
- dstream<<"INFO: Size "<<dim.Width
- <<"x"<<dim.Height<<std::endl;
- core::position2d<s32> pos_base(x, y);
- video::IImage *img2 =
- driver->createImage(video::ECF_A8R8G8B8, dim);
- img->copyTo(img2);
- img->drop();
- img2->copyToWithAlpha(baseimg, pos_base,
- core::rect<s32>(v2s32(0,0), dim),
- video::SColor(255,255,255,255),
- NULL);
- img2->drop();
- }
- else
- {
- dstream<<"WARNING: img==NULL"<<std::endl;
- }
- }
- }
- else if(name.substr(0,12) == "[progressbar")
- {
- float value = stof(name.substr(12));
- make_progressbar(value, baseimg);
- }
- else
- {
- dstream<<"WARNING: getTextureDirect(): Invalid "
- " texture: \""<<name<<"\""<<std::endl;
- }
- }
-
- /*
- Add to image cache
- */
- if(baseimg != NULL)
- {
- core::map<std::string, video::IImage*>::Node *n;
- n = m_imagecache.find(texture_name);
- if(n != NULL)
- {
- video::IImage *img = n->getValue();
- if(img != baseimg)
- {
- img->drop();
- }
- }
-
- m_imagecache[texture_name] = baseimg;
- }
- }
-
- // If no resulting image, return NULL
- if(baseimg == NULL)
- {
- dstream<<"WARNING: getTextureDirect(): baseimg is NULL (attempted to"
- " create texture \""<<texture_name<<"\""<<std::endl;
- return NULL;
- }
-
- /*// DEBUG: Paint some pixels
- video::SColor c(255,255,0,0);
- baseimg->setPixel(1,1, c);
- baseimg->setPixel(1,14, c);
- baseimg->setPixel(14,1, c);
- baseimg->setPixel(14,14, c);*/
-
- // Create texture from resulting image
- t = driver->addTexture(texture_name.c_str(), baseimg);
-
- dstream<<"INFO: getTextureDirect(): created texture \""<<texture_name
- <<"\""<<std::endl;
-
- return t;
-
-}
-
-void make_progressbar(float value, video::IImage *image)
-{
- if(image == NULL)
- return;
-
- core::dimension2d<u32> size = image->getDimension();
-
- u32 barheight = 1;
- u32 barpad_x = 1;
- u32 barpad_y = 1;
- u32 barwidth = size.Width - barpad_x*2;
- v2u32 barpos(barpad_x, size.Height - barheight - barpad_y);
-
- u32 barvalue_i = (u32)(((float)barwidth * value) + 0.5);
-
- video::SColor active(255,255,0,0);
- video::SColor inactive(255,0,0,0);
- for(u32 x0=0; x0<barwidth; x0++)
- {
- video::SColor *c;
- if(x0 < barvalue_i)
- c = &active;
- else
- c = &inactive;
- u32 x = x0 + barpos.X;
- for(u32 y=barpos.Y; y<barpos.Y+barheight; y++)
- {
- image->setPixel(x,y, *c);
- }
- }
-}
-#endif
-
diff --git a/src/irrlichtwrapper.h b/src/irrlichtwrapper.h
deleted file mode 100644
index 55e021bda..000000000
--- a/src/irrlichtwrapper.h
+++ /dev/null
@@ -1,187 +0,0 @@
-/*
-Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
-
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License along
-with this program; if not, write to the Free Software Foundation, Inc.,
-51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-*/
-
-#ifndef IRRLICHTWRAPPER_HEADER
-#define IRRLICHTWRAPPER_HEADER
-
-#include "threads.h"
-#include "common_irrlicht.h"
-#include "debug.h"
-#include "utility.h"
-#include "texture.h"
-#include "iirrlichtwrapper.h"
-
-#include <jmutex.h>
-#include <jmutexautolock.h>
-#include <string>
-
-/*
- NOTE: This is deprecated and should be removed completely
-*/
-
-/*
- A thread-safe texture pointer cache.
-
- This is used so that irrlicht doesn't get called from many
- threads, because texture pointers have to be handled in
- background threads.
-*/
-
-#if 0
-/*
- A thread-safe texture pointer cache
-*/
-class TextureCache
-{
-public:
- TextureCache()
- {
- m_mutex.Init();
- assert(m_mutex.IsInitialized());
- }
-
- void set(const TextureSpec &spec, video::ITexture *texture)
- {
- if(texture == NULL)
- return;
-
- JMutexAutoLock lock(m_mutex);
-
- m_textures[spec] = texture;
- }
-
- video::ITexture* get(const TextureSpec &spec)
- {
- JMutexAutoLock lock(m_mutex);
-
- core::map<TextureSpec, video::ITexture*>::Node *n;
- n = m_textures.find(spec);
-
- if(n != NULL)
- return n->getValue();
-
- return NULL;
- }
-
-private:
- core::map<TextureSpec, video::ITexture*> m_textures;
- JMutex m_mutex;
-};
-#endif
-
-/*
- A thread-safe wrapper for irrlicht, to be accessed from
- background worker threads.
-
- Queues tasks to be done in the main thread.
-
- Also caches texture specification strings to ids and textures.
-
- TODO: Remove this and move all texture functionality to TextureSource
-*/
-
-class IrrlichtWrapper : public IIrrlichtWrapper
-{
-public:
- /*
- These are called from the main thread
- */
-
- IrrlichtWrapper(IrrlichtDevice *device);
-
- ~IrrlichtWrapper();
-
- // Run queued tasks
- void Run();
-
- // Shutdown wrapper; this disables queued texture fetching
- void Shutdown(bool shutdown);
-
- IrrlichtDevice* getDevice();
-
- /*
- These are called from other threads
- */
-
- // Not exactly thread-safe but this needs to be fast.
- // getTimer()->getRealTime() only reads one variable anyway.
- u32 getTime()
- {
- return m_device->getTimer()->getRealTime();
- }
-
-#if 0
- /*
- Format of a texture name:
- "stone.png" (filename in image data directory)
- "[crack1" (a name starting with "[" is a special feature)
- "[progress1.0" (a name starting with "[" is a special feature)
- */
- /*
- Loads texture defined by "name" and assigns a texture id to it.
- If texture has to be generated, generates it.
- If the texture has already been loaded, returns existing id.
- */
- textureid_t getTextureId(const std::string &name);
- // The reverse of the above
- std::string getTextureName(textureid_t id);
- // Gets a texture based on a filename
- video::ITexture* getTexture(const std::string &filename);
- // Gets a texture based on a TextureSpec (a textureid_t is fine too)
- video::ITexture* getTexture(const TextureSpec &spec);
-#endif
-
-private:
- /*
- Non-thread-safe variants of stuff, for internal use
- */
-
- // Constructs a texture according to spec
- //video::ITexture* getTextureDirect(const TextureSpec &spec);
-
- /*
- Members
- */
-
- bool m_running;
-
- // The id of the thread that can (and has to) use irrlicht directly
- threadid_t m_main_thread;
-
- // The irrlicht device
- JMutex m_device_mutex;
- IrrlichtDevice *m_device;
-
-#if 0
- // Queued texture fetches (to be processed by the main thread)
- RequestQueue<TextureSpec, video::ITexture*, u8, u8> m_get_texture_queue;
-
- // Cache of textures by spec
- TextureCache m_texturecache;
-
- // Cached or generated source images by texture name
- core::map<std::string, video::IImage*> m_imagecache;
-
- // A mapping from texture id to string spec
- MutexedIdGenerator<std::string> m_namecache;
-#endif
-};
-
-#endif
-
diff --git a/src/keycode.cpp b/src/keycode.cpp
new file mode 100644
index 000000000..ad3c0b401
--- /dev/null
+++ b/src/keycode.cpp
@@ -0,0 +1,192 @@
+/*
+Minetest-c55
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "keycode.h"
+#include "main.h" // For g_settings
+
+#define CHECKKEY(x){if(strcmp(name, #x)==0) return irr::x;}
+
+irr::EKEY_CODE keyname_to_keycode(const char *name)
+{
+ CHECKKEY(KEY_LBUTTON)
+ CHECKKEY(KEY_RBUTTON)
+ CHECKKEY(KEY_CANCEL)
+ CHECKKEY(KEY_MBUTTON)
+ CHECKKEY(KEY_XBUTTON1)
+ CHECKKEY(KEY_XBUTTON2)
+ CHECKKEY(KEY_BACK)
+ CHECKKEY(KEY_TAB)
+ CHECKKEY(KEY_CLEAR)
+ CHECKKEY(KEY_RETURN)
+ CHECKKEY(KEY_SHIFT)
+ CHECKKEY(KEY_CONTROL)
+ CHECKKEY(KEY_MENU)
+ CHECKKEY(KEY_PAUSE)
+ CHECKKEY(KEY_CAPITAL)
+ CHECKKEY(KEY_KANA)
+ CHECKKEY(KEY_HANGUEL)
+ CHECKKEY(KEY_HANGUL)
+ CHECKKEY(KEY_JUNJA)
+ CHECKKEY(KEY_FINAL)
+ CHECKKEY(KEY_HANJA)
+ CHECKKEY(KEY_KANJI)
+ CHECKKEY(KEY_ESCAPE)
+ CHECKKEY(KEY_CONVERT)
+ CHECKKEY(KEY_NONCONVERT)
+ CHECKKEY(KEY_ACCEPT)
+ CHECKKEY(KEY_MODECHANGE)
+ CHECKKEY(KEY_SPACE)
+ CHECKKEY(KEY_PRIOR)
+ CHECKKEY(KEY_NEXT)
+ CHECKKEY(KEY_END)
+ CHECKKEY(KEY_HOME)
+ CHECKKEY(KEY_LEFT)
+ CHECKKEY(KEY_UP)
+ CHECKKEY(KEY_RIGHT)
+ CHECKKEY(KEY_DOWN)
+ CHECKKEY(KEY_SELECT)
+ CHECKKEY(KEY_PRINT)
+ CHECKKEY(KEY_EXECUT)
+ CHECKKEY(KEY_SNAPSHOT)
+ CHECKKEY(KEY_INSERT)
+ CHECKKEY(KEY_DELETE)
+ CHECKKEY(KEY_HELP)
+ CHECKKEY(KEY_KEY_0)
+ CHECKKEY(KEY_KEY_1)
+ CHECKKEY(KEY_KEY_2)
+ CHECKKEY(KEY_KEY_3)
+ CHECKKEY(KEY_KEY_4)
+ CHECKKEY(KEY_KEY_5)
+ CHECKKEY(KEY_KEY_6)
+ CHECKKEY(KEY_KEY_7)
+ CHECKKEY(KEY_KEY_8)
+ CHECKKEY(KEY_KEY_9)
+ CHECKKEY(KEY_KEY_A)
+ CHECKKEY(KEY_KEY_B)
+ CHECKKEY(KEY_KEY_C)
+ CHECKKEY(KEY_KEY_D)
+ CHECKKEY(KEY_KEY_E)
+ CHECKKEY(KEY_KEY_F)
+ CHECKKEY(KEY_KEY_G)
+ CHECKKEY(KEY_KEY_H)
+ CHECKKEY(KEY_KEY_I)
+ CHECKKEY(KEY_KEY_J)
+ CHECKKEY(KEY_KEY_K)
+ CHECKKEY(KEY_KEY_L)
+ CHECKKEY(KEY_KEY_M)
+ CHECKKEY(KEY_KEY_N)
+ CHECKKEY(KEY_KEY_O)
+ CHECKKEY(KEY_KEY_P)
+ CHECKKEY(KEY_KEY_Q)
+ CHECKKEY(KEY_KEY_R)
+ CHECKKEY(KEY_KEY_S)
+ CHECKKEY(KEY_KEY_T)
+ CHECKKEY(KEY_KEY_U)
+ CHECKKEY(KEY_KEY_V)
+ CHECKKEY(KEY_KEY_W)
+ CHECKKEY(KEY_KEY_X)
+ CHECKKEY(KEY_KEY_Y)
+ CHECKKEY(KEY_KEY_Z)
+ CHECKKEY(KEY_LWIN)
+ CHECKKEY(KEY_RWIN)
+ CHECKKEY(KEY_APPS)
+ CHECKKEY(KEY_SLEEP)
+ CHECKKEY(KEY_NUMPAD0)
+ CHECKKEY(KEY_NUMPAD1)
+ CHECKKEY(KEY_NUMPAD2)
+ CHECKKEY(KEY_NUMPAD3)
+ CHECKKEY(KEY_NUMPAD4)
+ CHECKKEY(KEY_NUMPAD5)
+ CHECKKEY(KEY_NUMPAD6)
+ CHECKKEY(KEY_NUMPAD7)
+ CHECKKEY(KEY_NUMPAD8)
+ CHECKKEY(KEY_NUMPAD9)
+ CHECKKEY(KEY_MULTIPLY)
+ CHECKKEY(KEY_ADD)
+ CHECKKEY(KEY_SEPARATOR)
+ CHECKKEY(KEY_SUBTRACT)
+ CHECKKEY(KEY_DECIMAL)
+ CHECKKEY(KEY_DIVIDE)
+ CHECKKEY(KEY_F1)
+ CHECKKEY(KEY_F2)
+ CHECKKEY(KEY_F3)
+ CHECKKEY(KEY_F4)
+ CHECKKEY(KEY_F5)
+ CHECKKEY(KEY_F6)
+ CHECKKEY(KEY_F7)
+ CHECKKEY(KEY_F8)
+ CHECKKEY(KEY_F9)
+ CHECKKEY(KEY_F10)
+ CHECKKEY(KEY_F11)
+ CHECKKEY(KEY_F12)
+ CHECKKEY(KEY_F13)
+ CHECKKEY(KEY_F14)
+ CHECKKEY(KEY_F15)
+ CHECKKEY(KEY_F16)
+ CHECKKEY(KEY_F17)
+ CHECKKEY(KEY_F18)
+ CHECKKEY(KEY_F19)
+ CHECKKEY(KEY_F20)
+ CHECKKEY(KEY_F21)
+ CHECKKEY(KEY_F22)
+ CHECKKEY(KEY_F23)
+ CHECKKEY(KEY_F24)
+ CHECKKEY(KEY_NUMLOCK)
+ CHECKKEY(KEY_SCROLL)
+ CHECKKEY(KEY_LSHIFT)
+ CHECKKEY(KEY_RSHIFT)
+ CHECKKEY(KEY_LCONTROL)
+ CHECKKEY(KEY_RCONTROL)
+ CHECKKEY(KEY_LMENU)
+ CHECKKEY(KEY_RMENU)
+ CHECKKEY(KEY_PLUS)
+ CHECKKEY(KEY_COMMA)
+ CHECKKEY(KEY_MINUS)
+ CHECKKEY(KEY_PERIOD)
+ CHECKKEY(KEY_ATTN)
+ CHECKKEY(KEY_CRSEL)
+ CHECKKEY(KEY_EXSEL)
+ CHECKKEY(KEY_EREOF)
+ CHECKKEY(KEY_PLAY)
+ CHECKKEY(KEY_ZOOM)
+ CHECKKEY(KEY_PA1)
+ CHECKKEY(KEY_OEM_CLEAR)
+
+ return irr::KEY_KEY_CODES_COUNT;
+}
+
+/*
+ Key config
+*/
+
+// A simple cache for quicker lookup
+core::map<std::string, irr::EKEY_CODE> g_key_setting_cache;
+
+irr::EKEY_CODE getKeySetting(const char *settingname)
+{
+ core::map<std::string, irr::EKEY_CODE>::Node *n;
+ n = g_key_setting_cache.find(settingname);
+ if(n)
+ return n->getValue();
+ irr::EKEY_CODE c = keyname_to_keycode(g_settings.get(settingname).c_str());
+ g_key_setting_cache.insert(settingname, c);
+ return c;
+}
+
+
diff --git a/src/keycode.h b/src/keycode.h
new file mode 100644
index 000000000..f19fe3442
--- /dev/null
+++ b/src/keycode.h
@@ -0,0 +1,31 @@
+/*
+Minetest-c55
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#ifndef KEYCODE_HEADER
+#define KEYCODE_HEADER
+
+#include "common_irrlicht.h"
+
+irr::EKEY_CODE keyname_to_keycode(const char *name);
+
+// Key configuration getter
+irr::EKEY_CODE getKeySetting(const char *settingname);
+
+#endif
+
diff --git a/src/light.cpp b/src/light.cpp
index 5dade2e16..f214d6ea0 100644
--- a/src/light.cpp
+++ b/src/light.cpp
@@ -19,27 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "light.h"
-// This is reasonable with classic lighting with a light source
-/*u8 light_decode_table[LIGHT_MAX+1] =
-{
-2,
-3,
-4,
-6,
-9,
-13,
-18,
-25,
-32,
-35,
-45,
-57,
-69,
-79,
-255
-};*/
-
-
+#if 1
// This is good
// a_n+1 = a_n * 0.786
// Length of LIGHT_MAX+1 means LIGHT_MAX is the last value.
@@ -62,6 +42,48 @@ u8 light_decode_table[LIGHT_MAX+1] =
200,
255,
};
+#else
+// Use for debugging in dark
+u8 light_decode_table[LIGHT_MAX+1] =
+{
+58,
+64,
+72,
+80,
+88,
+98,
+109,
+121,
+135,
+150,
+167,
+185,
+206,
+229,
+255,
+};
+#endif
+
+// This is reasonable with classic lighting with a light source
+/*u8 light_decode_table[LIGHT_MAX+1] =
+{
+2,
+3,
+4,
+6,
+9,
+13,
+18,
+25,
+32,
+35,
+45,
+57,
+69,
+79,
+255
+};*/
+
// As in minecraft, a_n+1 = a_n * 0.8
// NOTE: This doesn't really work that well because this defines
diff --git a/src/lua/CMakeLists.txt b/src/lua/CMakeLists.txt
deleted file mode 100644
index c43dc1737..000000000
--- a/src/lua/CMakeLists.txt
+++ /dev/null
@@ -1,109 +0,0 @@
-#
-# Lua 5.1.x
-#
-cmake_minimum_required(VERSION 2.4 FATAL_ERROR)
-
-project(lua C)
-
-set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
-
-include(CustomMacros)
-
-set(COMMON_CFLAGS)
-set(COMMON_LDFLAGS)
-set(LIBS)
-
-if(APPLE)
- set(DEFAULT_POSIX TRUE)
- set(DEFAULT_DLOPEN ON)
- # use this on Mac OS X 10.3-
- option(LUA_USE_MACOSX "Mac OS X 10.3-" OFF)
-elseif(CYGWIN)
- set(DEFAULT_POSIX TRUE)
-elseif(UNIX)
- set(DEFAULT_POSIX TRUE)
-elseif(WIN32)
- set(LUA_WIN TRUE)
- set(COMMON_CFLAGS "${COMMON_CFLAGS} -DLUA_BUILD_AS_DLL")
-else()
- set(DEFAULT_ANSI TRUE)
-endif()
-
-if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
- set(COMMON_LDFLAGS "${COMMON_LDFLAGS} -Wl,-E -lm")
- set(DEFAULT_DLOPEN ON)
-endif()
-
-if(WIN32)
- #set(BUILD_STATIC OFF)
- set(BUILD_STATIC ON)
-else()
- #option(BUILD_STATIC "build static library" ON)
- set(BUILD_STATIC ON)
-endif()
-
-if(DEFAULT_DLOPEN)
- option(LUA_USE_DLOPEN "Enable dlopen support." ON)
-else()
- option(LUA_USE_DLOPEN "Enable dlopen support." OFF)
-endif()
-
-if(DEFAULT_POSIX)
-else()
-endif()
-
-if(DEFAULT_ANSI)
- option(LUA_ANSI "Disable non-ansi features." ON)
-else()
- option(LUA_ANSI "Disable non-ansi features." OFF)
-endif()
-
-#
-# Lua version
-#
-set(LUA_VERSION_MAJOR 5)
-set(LUA_VERSION_MINOR 1)
-set(LUA_VERSION_PATCH 4)
-set(LUA_VERSION
- "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}")
-set(LUA_SOVERSION
- "${LUA_VERSION_MAJOR}")
-
-#
-# libs & cflags
-#
-set(COMMON_LDFLAGS "${COMMON_LDFLAGS}")
-
-# For "Mac OS X 10.3-"
-if(LUA_USE_MACOSX)
- set(COMMON_CFLAGS "${COMMON_CFLAGS} -DLUA_USE_MACOSX")
- set(LUA_USE_DLOPEN FALSE)
-endif(LUA_USE_MACOSX)
-
-if(LUA_USE_DLOPEN)
- set(COMMON_CFLAGS "${COMMON_CFLAGS} -DLUA_USE_DLOPEN")
- if(NOT APPLE)
- set(COMMON_LDFLAGS "${COMMON_LDFLAGS} -ldl ")
- endif(NOT APPLE)
-endif(LUA_USE_DLOPEN)
-
-if(LUA_ANSI)
- set(COMMON_CFLAGS "${COMMON_CFLAGS} -DLUA_ANSI")
-endif(LUA_ANSI)
-
-#
-# standard flags to use for each build type.
-#
-if(CMAKE_COMPILER_IS_GNUCC)
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pipe -Wall -Wextra -Wshadow -W -pedantic -std=gnu99")
- set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2")
- set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -g")
- set(CMAKE_C_FLAGS_PROFILE "${CMAKE_C_FLAGS_PROFILE} -O1 -g")
- set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_WITHDEBINFO} -O2 -g")
-endif(CMAKE_COMPILER_IS_GNUCC)
-
-#
-# sub-folders
-#
-ADD_SUBDIRECTORY(src build)
-
diff --git a/src/lua/COPYRIGHT b/src/lua/COPYRIGHT
deleted file mode 100644
index 3a53e741e..000000000
--- a/src/lua/COPYRIGHT
+++ /dev/null
@@ -1,34 +0,0 @@
-Lua License
------------
-
-Lua is licensed under the terms of the MIT license reproduced below.
-This means that Lua is free software and can be used for both academic
-and commercial purposes at absolutely no cost.
-
-For details and rationale, see http://www.lua.org/license.html .
-
-===============================================================================
-
-Copyright (C) 1994-2008 Lua.org, PUC-Rio.
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-THE SOFTWARE.
-
-===============================================================================
-
-(end of COPYRIGHT)
diff --git a/src/lua/cmake/CustomMacros.cmake b/src/lua/cmake/CustomMacros.cmake
deleted file mode 100644
index 9318ea4eb..000000000
--- a/src/lua/cmake/CustomMacros.cmake
+++ /dev/null
@@ -1,14 +0,0 @@
-
-macro(add_target_properties _target _name)
- set(_properties)
- foreach(_prop ${ARGN})
- set(_properties "${_properties} ${_prop}")
- endforeach(_prop)
- get_target_property(_old_properties ${_target} ${_name})
- if(NOT _old_properties)
- # in case it's NOTFOUND
- set(_old_properties)
- endif(NOT _old_properties)
- set_target_properties(${_target} PROPERTIES ${_name} "${_old_properties} ${_properties}")
-endmacro(add_target_properties)
-
diff --git a/src/lua/src/CMakeLists.txt b/src/lua/src/CMakeLists.txt
deleted file mode 100644
index f9bfda85e..000000000
--- a/src/lua/src/CMakeLists.txt
+++ /dev/null
@@ -1,65 +0,0 @@
-
-# Lua core source files.
-set(LUA_CORE_SRC
- lapi.c
- lauxlib.c
- lbaselib.c
- lcode.c
- ldblib.c
- ldebug.c
- ldo.c
- ldump.c
- lfunc.c
- lgc.c
- linit.c
- liolib.c
- llex.c
- lmathlib.c
- lmem.c
- loadlib.c
- lobject.c
- lopcodes.c
- loslib.c
- lparser.c
- lstate.c
- lstring.c
- lstrlib.c
- ltable.c
- ltablib.c
- ltm.c
- lundump.c
- lvm.c
- lzio.c
-)
-set(LUA_LIB_HEADERS
- lua.h
- lualib.h
- lauxlib.h
- luaconf.h
-)
-
-include_directories(${CMAKE_CURRENT_SOURCE_DIR}
- ${CMAKE_CURRENT_BINARY_DIR})
-
-#
-# Lua library.
-#
-add_library(lua STATIC ${LUA_CORE_SRC})
-add_target_properties(lua COMPILE_FLAGS "${COMMON_CFLAGS}")
-add_target_properties(lua LINK_FLAGS "${LD_FLAGS} ${COMMON_LDFLAGS}")
-target_link_libraries(lua ${LIBS})
-set(LUA_STATIC_LIB lua)
-set(LUA_LIBS lua)
-
-set_target_properties(${LUA_LIBS} PROPERTIES
- VERSION ${LUA_VERSION}
- SOVERSION ${LUA_SOVERSION}
- CLEAN_DIRECT_OUTPUT 1
-)
-
-# Install library
-install(TARGETS ${LUA_LIBS}
- RUNTIME DESTINATION bin
- LIBRARY DESTINATION lib
- ARCHIVE DESTINATION lib)
-
diff --git a/src/lua/src/lapi.c b/src/lua/src/lapi.c
deleted file mode 100644
index 5d5145d2e..000000000
--- a/src/lua/src/lapi.c
+++ /dev/null
@@ -1,1087 +0,0 @@
-/*
-** $Id: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $
-** Lua API
-** See Copyright Notice in lua.h
-*/
-
-
-#include <assert.h>
-#include <math.h>
-#include <stdarg.h>
-#include <string.h>
-
-#define lapi_c
-#define LUA_CORE
-
-#include "lua.h"
-
-#include "lapi.h"
-#include "ldebug.h"
-#include "ldo.h"
-#include "lfunc.h"
-#include "lgc.h"
-#include "lmem.h"
-#include "lobject.h"
-#include "lstate.h"
-#include "lstring.h"
-#include "ltable.h"
-#include "ltm.h"
-#include "lundump.h"
-#include "lvm.h"
-
-
-
-const char lua_ident[] =
- "$Lua: " LUA_RELEASE " " LUA_COPYRIGHT " $\n"
- "$Authors: " LUA_AUTHORS " $\n"
- "$URL: www.lua.org $\n";
-
-
-
-#define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
-
-#define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject)
-
-#define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;}
-
-
-
-static TValue *index2adr (lua_State *L, int idx) {
- if (idx > 0) {
- TValue *o = L->base + (idx - 1);
- api_check(L, idx <= L->ci->top - L->base);
- if (o >= L->top) return cast(TValue *, luaO_nilobject);
- else return o;
- }
- else if (idx > LUA_REGISTRYINDEX) {
- api_check(L, idx != 0 && -idx <= L->top - L->base);
- return L->top + idx;
- }
- else switch (idx) { /* pseudo-indices */
- case LUA_REGISTRYINDEX: return registry(L);
- case LUA_ENVIRONINDEX: {
- Closure *func = curr_func(L);
- sethvalue(L, &L->env, func->c.env);
- return &L->env;
- }
- case LUA_GLOBALSINDEX: return gt(L);
- default: {
- Closure *func = curr_func(L);
- idx = LUA_GLOBALSINDEX - idx;
- return (idx <= func->c.nupvalues)
- ? &func->c.upvalue[idx-1]
- : cast(TValue *, luaO_nilobject);
- }
- }
-}
-
-
-static Table *getcurrenv (lua_State *L) {
- if (L->ci == L->base_ci) /* no enclosing function? */
- return hvalue(gt(L)); /* use global table as environment */
- else {
- Closure *func = curr_func(L);
- return func->c.env;
- }
-}
-
-
-void luaA_pushobject (lua_State *L, const TValue *o) {
- setobj2s(L, L->top, o);
- api_incr_top(L);
-}
-
-
-LUA_API int lua_checkstack (lua_State *L, int size) {
- int res = 1;
- lua_lock(L);
- if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK)
- res = 0; /* stack overflow */
- else if (size > 0) {
- luaD_checkstack(L, size);
- if (L->ci->top < L->top + size)
- L->ci->top = L->top + size;
- }
- lua_unlock(L);
- return res;
-}
-
-
-LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
- int i;
- if (from == to) return;
- lua_lock(to);
- api_checknelems(from, n);
- api_check(from, G(from) == G(to));
- api_check(from, to->ci->top - to->top >= n);
- from->top -= n;
- for (i = 0; i < n; i++) {
- setobj2s(to, to->top++, from->top + i);
- }
- lua_unlock(to);
-}
-
-
-LUA_API void lua_setlevel (lua_State *from, lua_State *to) {
- to->nCcalls = from->nCcalls;
-}
-
-
-LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
- lua_CFunction old;
- lua_lock(L);
- old = G(L)->panic;
- G(L)->panic = panicf;
- lua_unlock(L);
- return old;
-}
-
-
-LUA_API lua_State *lua_newthread (lua_State *L) {
- lua_State *L1;
- lua_lock(L);
- luaC_checkGC(L);
- L1 = luaE_newthread(L);
- setthvalue(L, L->top, L1);
- api_incr_top(L);
- lua_unlock(L);
- luai_userstatethread(L, L1);
- return L1;
-}
-
-
-
-/*
-** basic stack manipulation
-*/
-
-
-LUA_API int lua_gettop (lua_State *L) {
- return cast_int(L->top - L->base);
-}
-
-
-LUA_API void lua_settop (lua_State *L, int idx) {
- lua_lock(L);
- if (idx >= 0) {
- api_check(L, idx <= L->stack_last - L->base);
- while (L->top < L->base + idx)
- setnilvalue(L->top++);
- L->top = L->base + idx;
- }
- else {
- api_check(L, -(idx+1) <= (L->top - L->base));
- L->top += idx+1; /* `subtract' index (index is negative) */
- }
- lua_unlock(L);
-}
-
-
-LUA_API void lua_remove (lua_State *L, int idx) {
- StkId p;
- lua_lock(L);
- p = index2adr(L, idx);
- api_checkvalidindex(L, p);
- while (++p < L->top) setobjs2s(L, p-1, p);
- L->top--;
- lua_unlock(L);
-}
-
-
-LUA_API void lua_insert (lua_State *L, int idx) {
- StkId p;
- StkId q;
- lua_lock(L);
- p = index2adr(L, idx);
- api_checkvalidindex(L, p);
- for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);
- setobjs2s(L, p, L->top);
- lua_unlock(L);
-}
-
-
-LUA_API void lua_replace (lua_State *L, int idx) {
- StkId o;
- lua_lock(L);
- /* explicit test for incompatible code */
- if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci)
- luaG_runerror(L, "no calling environment");
- api_checknelems(L, 1);
- o = index2adr(L, idx);
- api_checkvalidindex(L, o);
- if (idx == LUA_ENVIRONINDEX) {
- Closure *func = curr_func(L);
- api_check(L, ttistable(L->top - 1));
- func->c.env = hvalue(L->top - 1);
- luaC_barrier(L, func, L->top - 1);
- }
- else {
- setobj(L, o, L->top - 1);
- if (idx < LUA_GLOBALSINDEX) /* function upvalue? */
- luaC_barrier(L, curr_func(L), L->top - 1);
- }
- L->top--;
- lua_unlock(L);
-}
-
-
-LUA_API void lua_pushvalue (lua_State *L, int idx) {
- lua_lock(L);
- setobj2s(L, L->top, index2adr(L, idx));
- api_incr_top(L);
- lua_unlock(L);
-}
-
-
-
-/*
-** access functions (stack -> C)
-*/
-
-
-LUA_API int lua_type (lua_State *L, int idx) {
- StkId o = index2adr(L, idx);
- return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);
-}
-
-
-LUA_API const char *lua_typename (lua_State *L, int t) {
- UNUSED(L);
- return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
-}
-
-
-LUA_API int lua_iscfunction (lua_State *L, int idx) {
- StkId o = index2adr(L, idx);
- return iscfunction(o);
-}
-
-
-LUA_API int lua_isnumber (lua_State *L, int idx) {
- TValue n;
- const TValue *o = index2adr(L, idx);
- return tonumber(o, &n);
-}
-
-
-LUA_API int lua_isstring (lua_State *L, int idx) {
- int t = lua_type(L, idx);
- return (t == LUA_TSTRING || t == LUA_TNUMBER);
-}
-
-
-LUA_API int lua_isuserdata (lua_State *L, int idx) {
- const TValue *o = index2adr(L, idx);
- return (ttisuserdata(o) || ttislightuserdata(o));
-}
-
-
-LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
- StkId o1 = index2adr(L, index1);
- StkId o2 = index2adr(L, index2);
- return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
- : luaO_rawequalObj(o1, o2);
-}
-
-
-LUA_API int lua_equal (lua_State *L, int index1, int index2) {
- StkId o1, o2;
- int i;
- lua_lock(L); /* may call tag method */
- o1 = index2adr(L, index1);
- o2 = index2adr(L, index2);
- i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2);
- lua_unlock(L);
- return i;
-}
-
-
-LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
- StkId o1, o2;
- int i;
- lua_lock(L); /* may call tag method */
- o1 = index2adr(L, index1);
- o2 = index2adr(L, index2);
- i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
- : luaV_lessthan(L, o1, o2);
- lua_unlock(L);
- return i;
-}
-
-
-
-LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
- TValue n;
- const TValue *o = index2adr(L, idx);
- if (tonumber(o, &n))
- return nvalue(o);
- else
- return 0;
-}
-
-
-LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
- TValue n;
- const TValue *o = index2adr(L, idx);
- if (tonumber(o, &n)) {
- lua_Integer res;
- lua_Number num = nvalue(o);
- lua_number2integer(res, num);
- return res;
- }
- else
- return 0;
-}
-
-
-LUA_API int lua_toboolean (lua_State *L, int idx) {
- const TValue *o = index2adr(L, idx);
- return !l_isfalse(o);
-}
-
-
-LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
- StkId o = index2adr(L, idx);
- if (!ttisstring(o)) {
- lua_lock(L); /* `luaV_tostring' may create a new string */
- if (!luaV_tostring(L, o)) { /* conversion failed? */
- if (len != NULL) *len = 0;
- lua_unlock(L);
- return NULL;
- }
- luaC_checkGC(L);
- o = index2adr(L, idx); /* previous call may reallocate the stack */
- lua_unlock(L);
- }
- if (len != NULL) *len = tsvalue(o)->len;
- return svalue(o);
-}
-
-
-LUA_API size_t lua_objlen (lua_State *L, int idx) {
- StkId o = index2adr(L, idx);
- switch (ttype(o)) {
- case LUA_TSTRING: return tsvalue(o)->len;
- case LUA_TUSERDATA: return uvalue(o)->len;
- case LUA_TTABLE: return luaH_getn(hvalue(o));
- case LUA_TNUMBER: {
- size_t l;
- lua_lock(L); /* `luaV_tostring' may create a new string */
- l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0);
- lua_unlock(L);
- return l;
- }
- default: return 0;
- }
-}
-
-
-LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
- StkId o = index2adr(L, idx);
- return (!iscfunction(o)) ? NULL : clvalue(o)->c.f;
-}
-
-
-LUA_API void *lua_touserdata (lua_State *L, int idx) {
- StkId o = index2adr(L, idx);
- switch (ttype(o)) {
- case LUA_TUSERDATA: return (rawuvalue(o) + 1);
- case LUA_TLIGHTUSERDATA: return pvalue(o);
- default: return NULL;
- }
-}
-
-
-LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
- StkId o = index2adr(L, idx);
- return (!ttisthread(o)) ? NULL : thvalue(o);
-}
-
-
-LUA_API const void *lua_topointer (lua_State *L, int idx) {
- StkId o = index2adr(L, idx);
- switch (ttype(o)) {
- case LUA_TTABLE: return hvalue(o);
- case LUA_TFUNCTION: return clvalue(o);
- case LUA_TTHREAD: return thvalue(o);
- case LUA_TUSERDATA:
- case LUA_TLIGHTUSERDATA:
- return lua_touserdata(L, idx);
- default: return NULL;
- }
-}
-
-
-
-/*
-** push functions (C -> stack)
-*/
-
-
-LUA_API void lua_pushnil (lua_State *L) {
- lua_lock(L);
- setnilvalue(L->top);
- api_incr_top(L);
- lua_unlock(L);
-}
-
-
-LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
- lua_lock(L);
- setnvalue(L->top, n);
- api_incr_top(L);
- lua_unlock(L);
-}
-
-
-LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
- lua_lock(L);
- setnvalue(L->top, cast_num(n));
- api_incr_top(L);
- lua_unlock(L);
-}
-
-
-LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
- lua_lock(L);
- luaC_checkGC(L);
- setsvalue2s(L, L->top, luaS_newlstr(L, s, len));
- api_incr_top(L);
- lua_unlock(L);
-}
-
-
-LUA_API void lua_pushstring (lua_State *L, const char *s) {
- if (s == NULL)
- lua_pushnil(L);
- else
- lua_pushlstring(L, s, strlen(s));
-}
-
-
-LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
- va_list argp) {
- const char *ret;
- lua_lock(L);
- luaC_checkGC(L);
- ret = luaO_pushvfstring(L, fmt, argp);
- lua_unlock(L);
- return ret;
-}
-
-
-LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
- const char *ret;
- va_list argp;
- lua_lock(L);
- luaC_checkGC(L);
- va_start(argp, fmt);
- ret = luaO_pushvfstring(L, fmt, argp);
- va_end(argp);
- lua_unlock(L);
- return ret;
-}
-
-
-LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
- Closure *cl;
- lua_lock(L);
- luaC_checkGC(L);
- api_checknelems(L, n);
- cl = luaF_newCclosure(L, n, getcurrenv(L));
- cl->c.f = fn;
- L->top -= n;
- while (n--)
- setobj2n(L, &cl->c.upvalue[n], L->top+n);
- setclvalue(L, L->top, cl);
- lua_assert(iswhite(obj2gco(cl)));
- api_incr_top(L);
- lua_unlock(L);
-}
-
-
-LUA_API void lua_pushboolean (lua_State *L, int b) {
- lua_lock(L);
- setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
- api_incr_top(L);
- lua_unlock(L);
-}
-
-
-LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
- lua_lock(L);
- setpvalue(L->top, p);
- api_incr_top(L);
- lua_unlock(L);
-}
-
-
-LUA_API int lua_pushthread (lua_State *L) {
- lua_lock(L);
- setthvalue(L, L->top, L);
- api_incr_top(L);
- lua_unlock(L);
- return (G(L)->mainthread == L);
-}
-
-
-
-/*
-** get functions (Lua -> stack)
-*/
-
-
-LUA_API void lua_gettable (lua_State *L, int idx) {
- StkId t;
- lua_lock(L);
- t = index2adr(L, idx);
- api_checkvalidindex(L, t);
- luaV_gettable(L, t, L->top - 1, L->top - 1);
- lua_unlock(L);
-}
-
-
-LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
- StkId t;
- TValue key;
- lua_lock(L);
- t = index2adr(L, idx);
- api_checkvalidindex(L, t);
- setsvalue(L, &key, luaS_new(L, k));
- luaV_gettable(L, t, &key, L->top);
- api_incr_top(L);
- lua_unlock(L);
-}
-
-
-LUA_API void lua_rawget (lua_State *L, int idx) {
- StkId t;
- lua_lock(L);
- t = index2adr(L, idx);
- api_check(L, ttistable(t));
- setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
- lua_unlock(L);
-}
-
-
-LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
- StkId o;
- lua_lock(L);
- o = index2adr(L, idx);
- api_check(L, ttistable(o));
- setobj2s(L, L->top, luaH_getnum(hvalue(o), n));
- api_incr_top(L);
- lua_unlock(L);
-}
-
-
-LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
- lua_lock(L);
- luaC_checkGC(L);
- sethvalue(L, L->top, luaH_new(L, narray, nrec));
- api_incr_top(L);
- lua_unlock(L);
-}
-
-
-LUA_API int lua_getmetatable (lua_State *L, int objindex) {
- const TValue *obj;
- Table *mt = NULL;
- int res;
- lua_lock(L);
- obj = index2adr(L, objindex);
- switch (ttype(obj)) {
- case LUA_TTABLE:
- mt = hvalue(obj)->metatable;
- break;
- case LUA_TUSERDATA:
- mt = uvalue(obj)->metatable;
- break;
- default:
- mt = G(L)->mt[ttype(obj)];
- break;
- }
- if (mt == NULL)
- res = 0;
- else {
- sethvalue(L, L->top, mt);
- api_incr_top(L);
- res = 1;
- }
- lua_unlock(L);
- return res;
-}
-
-
-LUA_API void lua_getfenv (lua_State *L, int idx) {
- StkId o;
- lua_lock(L);
- o = index2adr(L, idx);
- api_checkvalidindex(L, o);
- switch (ttype(o)) {
- case LUA_TFUNCTION:
- sethvalue(L, L->top, clvalue(o)->c.env);
- break;
- case LUA_TUSERDATA:
- sethvalue(L, L->top, uvalue(o)->env);
- break;
- case LUA_TTHREAD:
- setobj2s(L, L->top, gt(thvalue(o)));
- break;
- default:
- setnilvalue(L->top);
- break;
- }
- api_incr_top(L);
- lua_unlock(L);
-}
-
-
-/*
-** set functions (stack -> Lua)
-*/
-
-
-LUA_API void lua_settable (lua_State *L, int idx) {
- StkId t;
- lua_lock(L);
- api_checknelems(L, 2);
- t = index2adr(L, idx);
- api_checkvalidindex(L, t);
- luaV_settable(L, t, L->top - 2, L->top - 1);
- L->top -= 2; /* pop index and value */
- lua_unlock(L);
-}
-
-
-LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
- StkId t;
- TValue key;
- lua_lock(L);
- api_checknelems(L, 1);
- t = index2adr(L, idx);
- api_checkvalidindex(L, t);
- setsvalue(L, &key, luaS_new(L, k));
- luaV_settable(L, t, &key, L->top - 1);
- L->top--; /* pop value */
- lua_unlock(L);
-}
-
-
-LUA_API void lua_rawset (lua_State *L, int idx) {
- StkId t;
- lua_lock(L);
- api_checknelems(L, 2);
- t = index2adr(L, idx);
- api_check(L, ttistable(t));
- setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
- luaC_barriert(L, hvalue(t), L->top-1);
- L->top -= 2;
- lua_unlock(L);
-}
-
-
-LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
- StkId o;
- lua_lock(L);
- api_checknelems(L, 1);
- o = index2adr(L, idx);
- api_check(L, ttistable(o));
- setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);
- luaC_barriert(L, hvalue(o), L->top-1);
- L->top--;
- lua_unlock(L);
-}
-
-
-LUA_API int lua_setmetatable (lua_State *L, int objindex) {
- TValue *obj;
- Table *mt;
- lua_lock(L);
- api_checknelems(L, 1);
- obj = index2adr(L, objindex);
- api_checkvalidindex(L, obj);
- if (ttisnil(L->top - 1))
- mt = NULL;
- else {
- api_check(L, ttistable(L->top - 1));
- mt = hvalue(L->top - 1);
- }
- switch (ttype(obj)) {
- case LUA_TTABLE: {
- hvalue(obj)->metatable = mt;
- if (mt)
- luaC_objbarriert(L, hvalue(obj), mt);
- break;
- }
- case LUA_TUSERDATA: {
- uvalue(obj)->metatable = mt;
- if (mt)
- luaC_objbarrier(L, rawuvalue(obj), mt);
- break;
- }
- default: {
- G(L)->mt[ttype(obj)] = mt;
- break;
- }
- }
- L->top--;
- lua_unlock(L);
- return 1;
-}
-
-
-LUA_API int lua_setfenv (lua_State *L, int idx) {
- StkId o;
- int res = 1;
- lua_lock(L);
- api_checknelems(L, 1);
- o = index2adr(L, idx);
- api_checkvalidindex(L, o);
- api_check(L, ttistable(L->top - 1));
- switch (ttype(o)) {
- case LUA_TFUNCTION:
- clvalue(o)->c.env = hvalue(L->top - 1);
- break;
- case LUA_TUSERDATA:
- uvalue(o)->env = hvalue(L->top - 1);
- break;
- case LUA_TTHREAD:
- sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1));
- break;
- default:
- res = 0;
- break;
- }
- if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
- L->top--;
- lua_unlock(L);
- return res;
-}
-
-
-/*
-** `load' and `call' functions (run Lua code)
-*/
-
-
-#define adjustresults(L,nres) \
- { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }
-
-
-#define checkresults(L,na,nr) \
- api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))
-
-
-LUA_API void lua_call (lua_State *L, int nargs, int nresults) {
- StkId func;
- lua_lock(L);
- api_checknelems(L, nargs+1);
- checkresults(L, nargs, nresults);
- func = L->top - (nargs+1);
- luaD_call(L, func, nresults);
- adjustresults(L, nresults);
- lua_unlock(L);
-}
-
-
-
-/*
-** Execute a protected call.
-*/
-struct CallS { /* data to `f_call' */
- StkId func;
- int nresults;
-};
-
-
-static void f_call (lua_State *L, void *ud) {
- struct CallS *c = cast(struct CallS *, ud);
- luaD_call(L, c->func, c->nresults);
-}
-
-
-
-LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
- struct CallS c;
- int status;
- ptrdiff_t func;
- lua_lock(L);
- api_checknelems(L, nargs+1);
- checkresults(L, nargs, nresults);
- if (errfunc == 0)
- func = 0;
- else {
- StkId o = index2adr(L, errfunc);
- api_checkvalidindex(L, o);
- func = savestack(L, o);
- }
- c.func = L->top - (nargs+1); /* function to be called */
- c.nresults = nresults;
- status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
- adjustresults(L, nresults);
- lua_unlock(L);
- return status;
-}
-
-
-/*
-** Execute a protected C call.
-*/
-struct CCallS { /* data to `f_Ccall' */
- lua_CFunction func;
- void *ud;
-};
-
-
-static void f_Ccall (lua_State *L, void *ud) {
- struct CCallS *c = cast(struct CCallS *, ud);
- Closure *cl;
- cl = luaF_newCclosure(L, 0, getcurrenv(L));
- cl->c.f = c->func;
- setclvalue(L, L->top, cl); /* push function */
- api_incr_top(L);
- setpvalue(L->top, c->ud); /* push only argument */
- api_incr_top(L);
- luaD_call(L, L->top - 2, 0);
-}
-
-
-LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
- struct CCallS c;
- int status;
- lua_lock(L);
- c.func = func;
- c.ud = ud;
- status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
- lua_unlock(L);
- return status;
-}
-
-
-LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
- const char *chunkname) {
- ZIO z;
- int status;
- lua_lock(L);
- if (!chunkname) chunkname = "?";
- luaZ_init(L, &z, reader, data);
- status = luaD_protectedparser(L, &z, chunkname);
- lua_unlock(L);
- return status;
-}
-
-
-LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
- int status;
- TValue *o;
- lua_lock(L);
- api_checknelems(L, 1);
- o = L->top - 1;
- if (isLfunction(o))
- status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0);
- else
- status = 1;
- lua_unlock(L);
- return status;
-}
-
-
-LUA_API int lua_status (lua_State *L) {
- return L->status;
-}
-
-
-/*
-** Garbage-collection function
-*/
-
-LUA_API int lua_gc (lua_State *L, int what, int data) {
- int res = 0;
- global_State *g;
- lua_lock(L);
- g = G(L);
- switch (what) {
- case LUA_GCSTOP: {
- g->GCthreshold = MAX_LUMEM;
- break;
- }
- case LUA_GCRESTART: {
- g->GCthreshold = g->totalbytes;
- break;
- }
- case LUA_GCCOLLECT: {
- luaC_fullgc(L);
- break;
- }
- case LUA_GCCOUNT: {
- /* GC values are expressed in Kbytes: #bytes/2^10 */
- res = cast_int(g->totalbytes >> 10);
- break;
- }
- case LUA_GCCOUNTB: {
- res = cast_int(g->totalbytes & 0x3ff);
- break;
- }
- case LUA_GCSTEP: {
- lu_mem a = (cast(lu_mem, data) << 10);
- if (a <= g->totalbytes)
- g->GCthreshold = g->totalbytes - a;
- else
- g->GCthreshold = 0;
- while (g->GCthreshold <= g->totalbytes) {
- luaC_step(L);
- if (g->gcstate == GCSpause) { /* end of cycle? */
- res = 1; /* signal it */
- break;
- }
- }
- break;
- }
- case LUA_GCSETPAUSE: {
- res = g->gcpause;
- g->gcpause = data;
- break;
- }
- case LUA_GCSETSTEPMUL: {
- res = g->gcstepmul;
- g->gcstepmul = data;
- break;
- }
- default: res = -1; /* invalid option */
- }
- lua_unlock(L);
- return res;
-}
-
-
-
-/*
-** miscellaneous functions
-*/
-
-
-LUA_API int lua_error (lua_State *L) {
- lua_lock(L);
- api_checknelems(L, 1);
- luaG_errormsg(L);
- lua_unlock(L);
- return 0; /* to avoid warnings */
-}
-
-
-LUA_API int lua_next (lua_State *L, int idx) {
- StkId t;
- int more;
- lua_lock(L);
- t = index2adr(L, idx);
- api_check(L, ttistable(t));
- more = luaH_next(L, hvalue(t), L->top - 1);
- if (more) {
- api_incr_top(L);
- }
- else /* no more elements */
- L->top -= 1; /* remove key */
- lua_unlock(L);
- return more;
-}
-
-
-LUA_API void lua_concat (lua_State *L, int n) {
- lua_lock(L);
- api_checknelems(L, n);
- if (n >= 2) {
- luaC_checkGC(L);
- luaV_concat(L, n, cast_int(L->top - L->base) - 1);
- L->top -= (n-1);
- }
- else if (n == 0) { /* push empty string */
- setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
- api_incr_top(L);
- }
- /* else n == 1; nothing to do */
- lua_unlock(L);
-}
-
-
-LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
- lua_Alloc f;
- lua_lock(L);
- if (ud) *ud = G(L)->ud;
- f = G(L)->frealloc;
- lua_unlock(L);
- return f;
-}
-
-
-LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
- lua_lock(L);
- G(L)->ud = ud;
- G(L)->frealloc = f;
- lua_unlock(L);
-}
-
-
-LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
- Udata *u;
- lua_lock(L);
- luaC_checkGC(L);
- u = luaS_newudata(L, size, getcurrenv(L));
- setuvalue(L, L->top, u);
- api_incr_top(L);
- lua_unlock(L);
- return u + 1;
-}
-
-
-
-
-static const char *aux_upvalue (StkId fi, int n, TValue **val) {
- Closure *f;
- if (!ttisfunction(fi)) return NULL;
- f = clvalue(fi);
- if (f->c.isC) {
- if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
- *val = &f->c.upvalue[n-1];
- return "";
- }
- else {
- Proto *p = f->l.p;
- if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
- *val = f->l.upvals[n-1]->v;
- return getstr(p->upvalues[n-1]);
- }
-}
-
-
-LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
- const char *name;
- TValue *val;
- lua_lock(L);
- name = aux_upvalue(index2adr(L, funcindex), n, &val);
- if (name) {
- setobj2s(L, L->top, val);
- api_incr_top(L);
- }
- lua_unlock(L);
- return name;
-}
-
-
-LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
- const char *name;
- TValue *val;
- StkId fi;
- lua_lock(L);
- fi = index2adr(L, funcindex);
- api_checknelems(L, 1);
- name = aux_upvalue(fi, n, &val);
- if (name) {
- L->top--;
- setobj(L, val, L->top);
- luaC_barrier(L, clvalue(fi), L->top);
- }
- lua_unlock(L);
- return name;
-}
-
diff --git a/src/lua/src/lapi.h b/src/lua/src/lapi.h
deleted file mode 100644
index 2c3fab244..000000000
--- a/src/lua/src/lapi.h
+++ /dev/null
@@ -1,16 +0,0 @@
-/*
-** $Id: lapi.h,v 2.2.1.1 2007/12/27 13:02:25 roberto Exp $
-** Auxiliary functions from Lua API
-** See Copyright Notice in lua.h
-*/
-
-#ifndef lapi_h
-#define lapi_h
-
-
-#include "lobject.h"
-
-
-LUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o);
-
-#endif
diff --git a/src/lua/src/lauxlib.c b/src/lua/src/lauxlib.c
deleted file mode 100644
index 10f14e2c0..000000000
--- a/src/lua/src/lauxlib.c
+++ /dev/null
@@ -1,652 +0,0 @@
-/*
-** $Id: lauxlib.c,v 1.159.1.3 2008/01/21 13:20:51 roberto Exp $
-** Auxiliary functions for building Lua libraries
-** See Copyright Notice in lua.h
-*/
-
-
-#include <ctype.h>
-#include <errno.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-
-/* This file uses only the official API of Lua.
-** Any function declared here could be written as an application function.
-*/
-
-#define lauxlib_c
-#define LUA_LIB
-
-#include "lua.h"
-
-#include "lauxlib.h"
-
-
-#define FREELIST_REF 0 /* free list of references */
-
-
-/* convert a stack index to positive */
-#define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
- lua_gettop(L) + (i) + 1)
-
-
-/*
-** {======================================================
-** Error-report functions
-** =======================================================
-*/
-
-
-LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
- lua_Debug ar;
- if (!lua_getstack(L, 0, &ar)) /* no stack frame? */
- return luaL_error(L, "bad argument #%d (%s)", narg, extramsg);
- lua_getinfo(L, "n", &ar);
- if (strcmp(ar.namewhat, "method") == 0) {
- narg--; /* do not count `self' */
- if (narg == 0) /* error is in the self argument itself? */
- return luaL_error(L, "calling " LUA_QS " on bad self (%s)",
- ar.name, extramsg);
- }
- if (ar.name == NULL)
- ar.name = "?";
- return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)",
- narg, ar.name, extramsg);
-}
-
-
-LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
- const char *msg = lua_pushfstring(L, "%s expected, got %s",
- tname, luaL_typename(L, narg));
- return luaL_argerror(L, narg, msg);
-}
-
-
-static void tag_error (lua_State *L, int narg, int tag) {
- luaL_typerror(L, narg, lua_typename(L, tag));
-}
-
-
-LUALIB_API void luaL_where (lua_State *L, int level) {
- lua_Debug ar;
- if (lua_getstack(L, level, &ar)) { /* check function at level */
- lua_getinfo(L, "Sl", &ar); /* get info about it */
- if (ar.currentline > 0) { /* is there info? */
- lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
- return;
- }
- }
- lua_pushliteral(L, ""); /* else, no information available... */
-}
-
-
-LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
- va_list argp;
- va_start(argp, fmt);
- luaL_where(L, 1);
- lua_pushvfstring(L, fmt, argp);
- va_end(argp);
- lua_concat(L, 2);
- return lua_error(L);
-}
-
-/* }====================================================== */
-
-
-LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,
- const char *const lst[]) {
- const char *name = (def) ? luaL_optstring(L, narg, def) :
- luaL_checkstring(L, narg);
- int i;
- for (i=0; lst[i]; i++)
- if (strcmp(lst[i], name) == 0)
- return i;
- return luaL_argerror(L, narg,
- lua_pushfstring(L, "invalid option " LUA_QS, name));
-}
-
-
-LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
- lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */
- if (!lua_isnil(L, -1)) /* name already in use? */
- return 0; /* leave previous value on top, but return 0 */
- lua_pop(L, 1);
- lua_newtable(L); /* create metatable */
- lua_pushvalue(L, -1);
- lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */
- return 1;
-}
-
-
-LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
- void *p = lua_touserdata(L, ud);
- if (p != NULL) { /* value is a userdata? */
- if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
- lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
- if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */
- lua_pop(L, 2); /* remove both metatables */
- return p;
- }
- }
- }
- luaL_typerror(L, ud, tname); /* else error */
- return NULL; /* to avoid warnings */
-}
-
-
-LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
- if (!lua_checkstack(L, space))
- luaL_error(L, "stack overflow (%s)", mes);
-}
-
-
-LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {
- if (lua_type(L, narg) != t)
- tag_error(L, narg, t);
-}
-
-
-LUALIB_API void luaL_checkany (lua_State *L, int narg) {
- if (lua_type(L, narg) == LUA_TNONE)
- luaL_argerror(L, narg, "value expected");
-}
-
-
-LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
- const char *s = lua_tolstring(L, narg, len);
- if (!s) tag_error(L, narg, LUA_TSTRING);
- return s;
-}
-
-
-LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
- const char *def, size_t *len) {
- if (lua_isnoneornil(L, narg)) {
- if (len)
- *len = (def ? strlen(def) : 0);
- return def;
- }
- else return luaL_checklstring(L, narg, len);
-}
-
-
-LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
- lua_Number d = lua_tonumber(L, narg);
- if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
- tag_error(L, narg, LUA_TNUMBER);
- return d;
-}
-
-
-LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
- return luaL_opt(L, luaL_checknumber, narg, def);
-}
-
-
-LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
- lua_Integer d = lua_tointeger(L, narg);
- if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
- tag_error(L, narg, LUA_TNUMBER);
- return d;
-}
-
-
-LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
- lua_Integer def) {
- return luaL_opt(L, luaL_checkinteger, narg, def);
-}
-
-
-LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
- if (!lua_getmetatable(L, obj)) /* no metatable? */
- return 0;
- lua_pushstring(L, event);
- lua_rawget(L, -2);
- if (lua_isnil(L, -1)) {
- lua_pop(L, 2); /* remove metatable and metafield */
- return 0;
- }
- else {
- lua_remove(L, -2); /* remove only metatable */
- return 1;
- }
-}
-
-
-LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
- obj = abs_index(L, obj);
- if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
- return 0;
- lua_pushvalue(L, obj);
- lua_call(L, 1, 1);
- return 1;
-}
-
-
-LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
- const luaL_Reg *l) {
- luaI_openlib(L, libname, l, 0);
-}
-
-
-static int libsize (const luaL_Reg *l) {
- int size = 0;
- for (; l->name; l++) size++;
- return size;
-}
-
-
-LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
- const luaL_Reg *l, int nup) {
- if (libname) {
- int size = libsize(l);
- /* check whether lib already exists */
- luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1);
- lua_getfield(L, -1, libname); /* get _LOADED[libname] */
- if (!lua_istable(L, -1)) { /* not found? */
- lua_pop(L, 1); /* remove previous result */
- /* try global variable (and create one if it does not exist) */
- if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL)
- luaL_error(L, "name conflict for module " LUA_QS, libname);
- lua_pushvalue(L, -1);
- lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */
- }
- lua_remove(L, -2); /* remove _LOADED table */
- lua_insert(L, -(nup+1)); /* move library table to below upvalues */
- }
- for (; l->name; l++) {
- int i;
- for (i=0; i<nup; i++) /* copy upvalues to the top */
- lua_pushvalue(L, -nup);
- lua_pushcclosure(L, l->func, nup);
- lua_setfield(L, -(nup+2), l->name);
- }
- lua_pop(L, nup); /* remove upvalues */
-}
-
-
-
-/*
-** {======================================================
-** getn-setn: size for arrays
-** =======================================================
-*/
-
-#if defined(LUA_COMPAT_GETN)
-
-static int checkint (lua_State *L, int topop) {
- int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1;
- lua_pop(L, topop);
- return n;
-}
-
-
-static void getsizes (lua_State *L) {
- lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES");
- if (lua_isnil(L, -1)) { /* no `size' table? */
- lua_pop(L, 1); /* remove nil */
- lua_newtable(L); /* create it */
- lua_pushvalue(L, -1); /* `size' will be its own metatable */
- lua_setmetatable(L, -2);
- lua_pushliteral(L, "kv");
- lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */
- lua_pushvalue(L, -1);
- lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); /* store in register */
- }
-}
-
-
-LUALIB_API void luaL_setn (lua_State *L, int t, int n) {
- t = abs_index(L, t);
- lua_pushliteral(L, "n");
- lua_rawget(L, t);
- if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */
- lua_pushliteral(L, "n"); /* use it */
- lua_pushinteger(L, n);
- lua_rawset(L, t);
- }
- else { /* use `sizes' */
- getsizes(L);
- lua_pushvalue(L, t);
- lua_pushinteger(L, n);
- lua_rawset(L, -3); /* sizes[t] = n */
- lua_pop(L, 1); /* remove `sizes' */
- }
-}
-
-
-LUALIB_API int luaL_getn (lua_State *L, int t) {
- int n;
- t = abs_index(L, t);
- lua_pushliteral(L, "n"); /* try t.n */
- lua_rawget(L, t);
- if ((n = checkint(L, 1)) >= 0) return n;
- getsizes(L); /* else try sizes[t] */
- lua_pushvalue(L, t);
- lua_rawget(L, -2);
- if ((n = checkint(L, 2)) >= 0) return n;
- return (int)lua_objlen(L, t);
-}
-
-#endif
-
-/* }====================================================== */
-
-
-
-LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
- const char *r) {
- const char *wild;
- size_t l = strlen(p);
- luaL_Buffer b;
- luaL_buffinit(L, &b);
- while ((wild = strstr(s, p)) != NULL) {
- luaL_addlstring(&b, s, wild - s); /* push prefix */
- luaL_addstring(&b, r); /* push replacement in place of pattern */
- s = wild + l; /* continue after `p' */
- }
- luaL_addstring(&b, s); /* push last suffix */
- luaL_pushresult(&b);
- return lua_tostring(L, -1);
-}
-
-
-LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
- const char *fname, int szhint) {
- const char *e;
- lua_pushvalue(L, idx);
- do {
- e = strchr(fname, '.');
- if (e == NULL) e = fname + strlen(fname);
- lua_pushlstring(L, fname, e - fname);
- lua_rawget(L, -2);
- if (lua_isnil(L, -1)) { /* no such field? */
- lua_pop(L, 1); /* remove this nil */
- lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
- lua_pushlstring(L, fname, e - fname);
- lua_pushvalue(L, -2);
- lua_settable(L, -4); /* set new table into field */
- }
- else if (!lua_istable(L, -1)) { /* field has a non-table value? */
- lua_pop(L, 2); /* remove table and value */
- return fname; /* return problematic part of the name */
- }
- lua_remove(L, -2); /* remove previous table */
- fname = e + 1;
- } while (*e == '.');
- return NULL;
-}
-
-
-
-/*
-** {======================================================
-** Generic Buffer manipulation
-** =======================================================
-*/
-
-
-#define bufflen(B) ((B)->p - (B)->buffer)
-#define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
-
-#define LIMIT (LUA_MINSTACK/2)
-
-
-static int emptybuffer (luaL_Buffer *B) {
- size_t l = bufflen(B);
- if (l == 0) return 0; /* put nothing on stack */
- else {
- lua_pushlstring(B->L, B->buffer, l);
- B->p = B->buffer;
- B->lvl++;
- return 1;
- }
-}
-
-
-static void adjuststack (luaL_Buffer *B) {
- if (B->lvl > 1) {
- lua_State *L = B->L;
- int toget = 1; /* number of levels to concat */
- size_t toplen = lua_strlen(L, -1);
- do {
- size_t l = lua_strlen(L, -(toget+1));
- if (B->lvl - toget + 1 >= LIMIT || toplen > l) {
- toplen += l;
- toget++;
- }
- else break;
- } while (toget < B->lvl);
- lua_concat(L, toget);
- B->lvl = B->lvl - toget + 1;
- }
-}
-
-
-LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
- if (emptybuffer(B))
- adjuststack(B);
- return B->buffer;
-}
-
-
-LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
- while (l--)
- luaL_addchar(B, *s++);
-}
-
-
-LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
- luaL_addlstring(B, s, strlen(s));
-}
-
-
-LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
- emptybuffer(B);
- lua_concat(B->L, B->lvl);
- B->lvl = 1;
-}
-
-
-LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
- lua_State *L = B->L;
- size_t vl;
- const char *s = lua_tolstring(L, -1, &vl);
- if (vl <= bufffree(B)) { /* fit into buffer? */
- memcpy(B->p, s, vl); /* put it there */
- B->p += vl;
- lua_pop(L, 1); /* remove from stack */
- }
- else {
- if (emptybuffer(B))
- lua_insert(L, -2); /* put buffer before new value */
- B->lvl++; /* add new value into B stack */
- adjuststack(B);
- }
-}
-
-
-LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
- B->L = L;
- B->p = B->buffer;
- B->lvl = 0;
-}
-
-/* }====================================================== */
-
-
-LUALIB_API int luaL_ref (lua_State *L, int t) {
- int ref;
- t = abs_index(L, t);
- if (lua_isnil(L, -1)) {
- lua_pop(L, 1); /* remove from stack */
- return LUA_REFNIL; /* `nil' has a unique fixed reference */
- }
- lua_rawgeti(L, t, FREELIST_REF); /* get first free element */
- ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */
- lua_pop(L, 1); /* remove it from stack */
- if (ref != 0) { /* any free element? */
- lua_rawgeti(L, t, ref); /* remove it from list */
- lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */
- }
- else { /* no free elements */
- ref = (int)lua_objlen(L, t);
- ref++; /* create new reference */
- }
- lua_rawseti(L, t, ref);
- return ref;
-}
-
-
-LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
- if (ref >= 0) {
- t = abs_index(L, t);
- lua_rawgeti(L, t, FREELIST_REF);
- lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */
- lua_pushinteger(L, ref);
- lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */
- }
-}
-
-
-
-/*
-** {======================================================
-** Load functions
-** =======================================================
-*/
-
-typedef struct LoadF {
- int extraline;
- FILE *f;
- char buff[LUAL_BUFFERSIZE];
-} LoadF;
-
-
-static const char *getF (lua_State *L, void *ud, size_t *size) {
- LoadF *lf = (LoadF *)ud;
- (void)L;
- if (lf->extraline) {
- lf->extraline = 0;
- *size = 1;
- return "\n";
- }
- if (feof(lf->f)) return NULL;
- *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);
- return (*size > 0) ? lf->buff : NULL;
-}
-
-
-static int errfile (lua_State *L, const char *what, int fnameindex) {
- const char *serr = strerror(errno);
- const char *filename = lua_tostring(L, fnameindex) + 1;
- lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
- lua_remove(L, fnameindex);
- return LUA_ERRFILE;
-}
-
-
-LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
- LoadF lf;
- int status, readstatus;
- int c;
- int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
- lf.extraline = 0;
- if (filename == NULL) {
- lua_pushliteral(L, "=stdin");
- lf.f = stdin;
- }
- else {
- lua_pushfstring(L, "@%s", filename);
- lf.f = fopen(filename, "r");
- if (lf.f == NULL) return errfile(L, "open", fnameindex);
- }
- c = getc(lf.f);
- if (c == '#') { /* Unix exec. file? */
- lf.extraline = 1;
- while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */
- if (c == '\n') c = getc(lf.f);
- }
- if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
- lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
- if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
- /* skip eventual `#!...' */
- while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
- lf.extraline = 0;
- }
- ungetc(c, lf.f);
- status = lua_load(L, getF, &lf, lua_tostring(L, -1));
- readstatus = ferror(lf.f);
- if (filename) fclose(lf.f); /* close file (even in case of errors) */
- if (readstatus) {
- lua_settop(L, fnameindex); /* ignore results from `lua_load' */
- return errfile(L, "read", fnameindex);
- }
- lua_remove(L, fnameindex);
- return status;
-}
-
-
-typedef struct LoadS {
- const char *s;
- size_t size;
-} LoadS;
-
-
-static const char *getS (lua_State *L, void *ud, size_t *size) {
- LoadS *ls = (LoadS *)ud;
- (void)L;
- if (ls->size == 0) return NULL;
- *size = ls->size;
- ls->size = 0;
- return ls->s;
-}
-
-
-LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
- const char *name) {
- LoadS ls;
- ls.s = buff;
- ls.size = size;
- return lua_load(L, getS, &ls, name);
-}
-
-
-LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) {
- return luaL_loadbuffer(L, s, strlen(s), s);
-}
-
-
-
-/* }====================================================== */
-
-
-static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
- (void)ud;
- (void)osize;
- if (nsize == 0) {
- free(ptr);
- return NULL;
- }
- else
- return realloc(ptr, nsize);
-}
-
-
-static int panic (lua_State *L) {
- (void)L; /* to avoid warnings */
- fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
- lua_tostring(L, -1));
- return 0;
-}
-
-
-LUALIB_API lua_State *luaL_newstate (void) {
- lua_State *L = lua_newstate(l_alloc, NULL);
- if (L) lua_atpanic(L, &panic);
- return L;
-}
-
diff --git a/src/lua/src/lauxlib.h b/src/lua/src/lauxlib.h
deleted file mode 100644
index 34258235d..000000000
--- a/src/lua/src/lauxlib.h
+++ /dev/null
@@ -1,174 +0,0 @@
-/*
-** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $
-** Auxiliary functions for building Lua libraries
-** See Copyright Notice in lua.h
-*/
-
-
-#ifndef lauxlib_h
-#define lauxlib_h
-
-
-#include <stddef.h>
-#include <stdio.h>
-
-#include "lua.h"
-
-
-#if defined(LUA_COMPAT_GETN)
-LUALIB_API int (luaL_getn) (lua_State *L, int t);
-LUALIB_API void (luaL_setn) (lua_State *L, int t, int n);
-#else
-#define luaL_getn(L,i) ((int)lua_objlen(L, i))
-#define luaL_setn(L,i,j) ((void)0) /* no op! */
-#endif
-
-#if defined(LUA_COMPAT_OPENLIB)
-#define luaI_openlib luaL_openlib
-#endif
-
-
-/* extra error code for `luaL_load' */
-#define LUA_ERRFILE (LUA_ERRERR+1)
-
-
-typedef struct luaL_Reg {
- const char *name;
- lua_CFunction func;
-} luaL_Reg;
-
-
-
-LUALIB_API void (luaI_openlib) (lua_State *L, const char *libname,
- const luaL_Reg *l, int nup);
-LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
- const luaL_Reg *l);
-LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e);
-LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e);
-LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname);
-LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
-LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg,
- size_t *l);
-LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg,
- const char *def, size_t *l);
-LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg);
-LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def);
-
-LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg);
-LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg,
- lua_Integer def);
-
-LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
-LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t);
-LUALIB_API void (luaL_checkany) (lua_State *L, int narg);
-
-LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname);
-LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname);
-
-LUALIB_API void (luaL_where) (lua_State *L, int lvl);
-LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
-
-LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def,
- const char *const lst[]);
-
-LUALIB_API int (luaL_ref) (lua_State *L, int t);
-LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);
-
-LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename);
-LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,
- const char *name);
-LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
-
-LUALIB_API lua_State *(luaL_newstate) (void);
-
-
-LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,
- const char *r);
-
-LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx,
- const char *fname, int szhint);
-
-
-
-
-/*
-** ===============================================================
-** some useful macros
-** ===============================================================
-*/
-
-#define luaL_argcheck(L, cond,numarg,extramsg) \
- ((void)((cond) || luaL_argerror(L, (numarg), (extramsg))))
-#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
-#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
-#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n)))
-#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
-#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n)))
-#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d)))
-
-#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i)))
-
-#define luaL_dofile(L, fn) \
- (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
-
-#define luaL_dostring(L, s) \
- (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
-
-#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
-
-#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
-
-/*
-** {======================================================
-** Generic Buffer manipulation
-** =======================================================
-*/
-
-
-
-typedef struct luaL_Buffer {
- char *p; /* current position in buffer */
- int lvl; /* number of strings in the stack (level) */
- lua_State *L;
- char buffer[LUAL_BUFFERSIZE];
-} luaL_Buffer;
-
-#define luaL_addchar(B,c) \
- ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \
- (*(B)->p++ = (char)(c)))
-
-/* compatibility only */
-#define luaL_putchar(B,c) luaL_addchar(B,c)
-
-#define luaL_addsize(B,n) ((B)->p += (n))
-
-LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);
-LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B);
-LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
-LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s);
-LUALIB_API void (luaL_addvalue) (luaL_Buffer *B);
-LUALIB_API void (luaL_pushresult) (luaL_Buffer *B);
-
-
-/* }====================================================== */
-
-
-/* compatibility with ref system */
-
-/* pre-defined references */
-#define LUA_NOREF (-2)
-#define LUA_REFNIL (-1)
-
-#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \
- (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0))
-
-#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref))
-
-#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref))
-
-
-#define luaL_reg luaL_Reg
-
-#endif
-
-
diff --git a/src/lua/src/lbaselib.c b/src/lua/src/lbaselib.c
deleted file mode 100644
index 2a4c079d3..000000000
--- a/src/lua/src/lbaselib.c
+++ /dev/null
@@ -1,653 +0,0 @@
-/*
-** $Id: lbaselib.c,v 1.191.1.6 2008/02/14 16:46:22 roberto Exp $
-** Basic library
-** See Copyright Notice in lua.h
-*/
-
-
-
-#include <ctype.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define lbaselib_c
-#define LUA_LIB
-
-#include "lua.h"
-
-#include "lauxlib.h"
-#include "lualib.h"
-
-
-
-
-/*
-** If your system does not support `stdout', you can just remove this function.
-** If you need, you can define your own `print' function, following this
-** model but changing `fputs' to put the strings at a proper place
-** (a console window or a log file, for instance).
-*/
-static int luaB_print (lua_State *L) {
- int n = lua_gettop(L); /* number of arguments */
- int i;
- lua_getglobal(L, "tostring");
- for (i=1; i<=n; i++) {
- const char *s;
- lua_pushvalue(L, -1); /* function to be called */
- lua_pushvalue(L, i); /* value to print */
- lua_call(L, 1, 1);
- s = lua_tostring(L, -1); /* get result */
- if (s == NULL)
- return luaL_error(L, LUA_QL("tostring") " must return a string to "
- LUA_QL("print"));
- if (i>1) fputs("\t", stdout);
- fputs(s, stdout);
- lua_pop(L, 1); /* pop result */
- }
- fputs("\n", stdout);
- return 0;
-}
-
-
-static int luaB_tonumber (lua_State *L) {
- int base = luaL_optint(L, 2, 10);
- if (base == 10) { /* standard conversion */
- luaL_checkany(L, 1);
- if (lua_isnumber(L, 1)) {
- lua_pushnumber(L, lua_tonumber(L, 1));
- return 1;
- }
- }
- else {
- const char *s1 = luaL_checkstring(L, 1);
- char *s2;
- unsigned long n;
- luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
- n = strtoul(s1, &s2, base);
- if (s1 != s2) { /* at least one valid digit? */
- while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
- if (*s2 == '\0') { /* no invalid trailing characters? */
- lua_pushnumber(L, (lua_Number)n);
- return 1;
- }
- }
- }
- lua_pushnil(L); /* else not a number */
- return 1;
-}
-
-
-static int luaB_error (lua_State *L) {
- int level = luaL_optint(L, 2, 1);
- lua_settop(L, 1);
- if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
- luaL_where(L, level);
- lua_pushvalue(L, 1);
- lua_concat(L, 2);
- }
- return lua_error(L);
-}
-
-
-static int luaB_getmetatable (lua_State *L) {
- luaL_checkany(L, 1);
- if (!lua_getmetatable(L, 1)) {
- lua_pushnil(L);
- return 1; /* no metatable */
- }
- luaL_getmetafield(L, 1, "__metatable");
- return 1; /* returns either __metatable field (if present) or metatable */
-}
-
-
-static int luaB_setmetatable (lua_State *L) {
- int t = lua_type(L, 2);
- luaL_checktype(L, 1, LUA_TTABLE);
- luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
- "nil or table expected");
- if (luaL_getmetafield(L, 1, "__metatable"))
- luaL_error(L, "cannot change a protected metatable");
- lua_settop(L, 2);
- lua_setmetatable(L, 1);
- return 1;
-}
-
-
-static void getfunc (lua_State *L, int opt) {
- if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
- else {
- lua_Debug ar;
- int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1);
- luaL_argcheck(L, level >= 0, 1, "level must be non-negative");
- if (lua_getstack(L, level, &ar) == 0)
- luaL_argerror(L, 1, "invalid level");
- lua_getinfo(L, "f", &ar);
- if (lua_isnil(L, -1))
- luaL_error(L, "no function environment for tail call at level %d",
- level);
- }
-}
-
-
-static int luaB_getfenv (lua_State *L) {
- getfunc(L, 1);
- if (lua_iscfunction(L, -1)) /* is a C function? */
- lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the thread's global env. */
- else
- lua_getfenv(L, -1);
- return 1;
-}
-
-
-static int luaB_setfenv (lua_State *L) {
- luaL_checktype(L, 2, LUA_TTABLE);
- getfunc(L, 0);
- lua_pushvalue(L, 2);
- if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {
- /* change environment of current thread */
- lua_pushthread(L);
- lua_insert(L, -2);
- lua_setfenv(L, -2);
- return 0;
- }
- else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0)
- luaL_error(L,
- LUA_QL("setfenv") " cannot change environment of given object");
- return 1;
-}
-
-
-static int luaB_rawequal (lua_State *L) {
- luaL_checkany(L, 1);
- luaL_checkany(L, 2);
- lua_pushboolean(L, lua_rawequal(L, 1, 2));
- return 1;
-}
-
-
-static int luaB_rawget (lua_State *L) {
- luaL_checktype(L, 1, LUA_TTABLE);
- luaL_checkany(L, 2);
- lua_settop(L, 2);
- lua_rawget(L, 1);
- return 1;
-}
-
-static int luaB_rawset (lua_State *L) {
- luaL_checktype(L, 1, LUA_TTABLE);
- luaL_checkany(L, 2);
- luaL_checkany(L, 3);
- lua_settop(L, 3);
- lua_rawset(L, 1);
- return 1;
-}
-
-
-static int luaB_gcinfo (lua_State *L) {
- lua_pushinteger(L, lua_getgccount(L));
- return 1;
-}
-
-
-static int luaB_collectgarbage (lua_State *L) {
- static const char *const opts[] = {"stop", "restart", "collect",
- "count", "step", "setpause", "setstepmul", NULL};
- static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
- LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL};
- int o = luaL_checkoption(L, 1, "collect", opts);
- int ex = luaL_optint(L, 2, 0);
- int res = lua_gc(L, optsnum[o], ex);
- switch (optsnum[o]) {
- case LUA_GCCOUNT: {
- int b = lua_gc(L, LUA_GCCOUNTB, 0);
- lua_pushnumber(L, res + ((lua_Number)b/1024));
- return 1;
- }
- case LUA_GCSTEP: {
- lua_pushboolean(L, res);
- return 1;
- }
- default: {
- lua_pushnumber(L, res);
- return 1;
- }
- }
-}
-
-
-static int luaB_type (lua_State *L) {
- luaL_checkany(L, 1);
- lua_pushstring(L, luaL_typename(L, 1));
- return 1;
-}
-
-
-static int luaB_next (lua_State *L) {
- luaL_checktype(L, 1, LUA_TTABLE);
- lua_settop(L, 2); /* create a 2nd argument if there isn't one */
- if (lua_next(L, 1))
- return 2;
- else {
- lua_pushnil(L);
- return 1;
- }
-}
-
-
-static int luaB_pairs (lua_State *L) {
- luaL_checktype(L, 1, LUA_TTABLE);
- lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
- lua_pushvalue(L, 1); /* state, */
- lua_pushnil(L); /* and initial value */
- return 3;
-}
-
-
-static int ipairsaux (lua_State *L) {
- int i = luaL_checkint(L, 2);
- luaL_checktype(L, 1, LUA_TTABLE);
- i++; /* next value */
- lua_pushinteger(L, i);
- lua_rawgeti(L, 1, i);
- return (lua_isnil(L, -1)) ? 0 : 2;
-}
-
-
-static int luaB_ipairs (lua_State *L) {
- luaL_checktype(L, 1, LUA_TTABLE);
- lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
- lua_pushvalue(L, 1); /* state, */
- lua_pushinteger(L, 0); /* and initial value */
- return 3;
-}
-
-
-static int load_aux (lua_State *L, int status) {
- if (status == 0) /* OK? */
- return 1;
- else {
- lua_pushnil(L);
- lua_insert(L, -2); /* put before error message */
- return 2; /* return nil plus error message */
- }
-}
-
-
-static int luaB_loadstring (lua_State *L) {
- size_t l;
- const char *s = luaL_checklstring(L, 1, &l);
- const char *chunkname = luaL_optstring(L, 2, s);
- return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));
-}
-
-
-static int luaB_loadfile (lua_State *L) {
- const char *fname = luaL_optstring(L, 1, NULL);
- return load_aux(L, luaL_loadfile(L, fname));
-}
-
-
-/*
-** Reader for generic `load' function: `lua_load' uses the
-** stack for internal stuff, so the reader cannot change the
-** stack top. Instead, it keeps its resulting string in a
-** reserved slot inside the stack.
-*/
-static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
- (void)ud; /* to avoid warnings */
- luaL_checkstack(L, 2, "too many nested functions");
- lua_pushvalue(L, 1); /* get function */
- lua_call(L, 0, 1); /* call it */
- if (lua_isnil(L, -1)) {
- *size = 0;
- return NULL;
- }
- else if (lua_isstring(L, -1)) {
- lua_replace(L, 3); /* save string in a reserved stack slot */
- return lua_tolstring(L, 3, size);
- }
- else luaL_error(L, "reader function must return a string");
- return NULL; /* to avoid warnings */
-}
-
-
-static int luaB_load (lua_State *L) {
- int status;
- const char *cname = luaL_optstring(L, 2, "=(load)");
- luaL_checktype(L, 1, LUA_TFUNCTION);
- lua_settop(L, 3); /* function, eventual name, plus one reserved slot */
- status = lua_load(L, generic_reader, NULL, cname);
- return load_aux(L, status);
-}
-
-
-static int luaB_dofile (lua_State *L) {
- const char *fname = luaL_optstring(L, 1, NULL);
- int n = lua_gettop(L);
- if (luaL_loadfile(L, fname) != 0) lua_error(L);
- lua_call(L, 0, LUA_MULTRET);
- return lua_gettop(L) - n;
-}
-
-
-static int luaB_assert (lua_State *L) {
- luaL_checkany(L, 1);
- if (!lua_toboolean(L, 1))
- return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
- return lua_gettop(L);
-}
-
-
-static int luaB_unpack (lua_State *L) {
- int i, e, n;
- luaL_checktype(L, 1, LUA_TTABLE);
- i = luaL_optint(L, 2, 1);
- e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1));
- if (i > e) return 0; /* empty range */
- n = e - i + 1; /* number of elements */
- if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */
- return luaL_error(L, "too many results to unpack");
- lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */
- while (i++ < e) /* push arg[i + 1...e] */
- lua_rawgeti(L, 1, i);
- return n;
-}
-
-
-static int luaB_select (lua_State *L) {
- int n = lua_gettop(L);
- if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
- lua_pushinteger(L, n-1);
- return 1;
- }
- else {
- int i = luaL_checkint(L, 1);
- if (i < 0) i = n + i;
- else if (i > n) i = n;
- luaL_argcheck(L, 1 <= i, 1, "index out of range");
- return n - i;
- }
-}
-
-
-static int luaB_pcall (lua_State *L) {
- int status;
- luaL_checkany(L, 1);
- status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);
- lua_pushboolean(L, (status == 0));
- lua_insert(L, 1);
- return lua_gettop(L); /* return status + all results */
-}
-
-
-static int luaB_xpcall (lua_State *L) {
- int status;
- luaL_checkany(L, 2);
- lua_settop(L, 2);
- lua_insert(L, 1); /* put error function under function to be called */
- status = lua_pcall(L, 0, LUA_MULTRET, 1);
- lua_pushboolean(L, (status == 0));
- lua_replace(L, 1);
- return lua_gettop(L); /* return status + all results */
-}
-
-
-static int luaB_tostring (lua_State *L) {
- luaL_checkany(L, 1);
- if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
- return 1; /* use its value */
- switch (lua_type(L, 1)) {
- case LUA_TNUMBER:
- lua_pushstring(L, lua_tostring(L, 1));
- break;
- case LUA_TSTRING:
- lua_pushvalue(L, 1);
- break;
- case LUA_TBOOLEAN:
- lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
- break;
- case LUA_TNIL:
- lua_pushliteral(L, "nil");
- break;
- default:
- lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1));
- break;
- }
- return 1;
-}
-
-
-static int luaB_newproxy (lua_State *L) {
- lua_settop(L, 1);
- lua_newuserdata(L, 0); /* create proxy */
- if (lua_toboolean(L, 1) == 0)
- return 1; /* no metatable */
- else if (lua_isboolean(L, 1)) {
- lua_newtable(L); /* create a new metatable `m' ... */
- lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
- lua_pushboolean(L, 1);
- lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
- }
- else {
- int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
- if (lua_getmetatable(L, 1)) {
- lua_rawget(L, lua_upvalueindex(1));
- validproxy = lua_toboolean(L, -1);
- lua_pop(L, 1); /* remove value */
- }
- luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
- lua_getmetatable(L, 1); /* metatable is valid; get it */
- }
- lua_setmetatable(L, 2);
- return 1;
-}
-
-
-static const luaL_Reg base_funcs[] = {
- {"assert", luaB_assert},
- {"collectgarbage", luaB_collectgarbage},
- {"dofile", luaB_dofile},
- {"error", luaB_error},
- {"gcinfo", luaB_gcinfo},
- {"getfenv", luaB_getfenv},
- {"getmetatable", luaB_getmetatable},
- {"loadfile", luaB_loadfile},
- {"load", luaB_load},
- {"loadstring", luaB_loadstring},
- {"next", luaB_next},
- {"pcall", luaB_pcall},
- {"print", luaB_print},
- {"rawequal", luaB_rawequal},
- {"rawget", luaB_rawget},
- {"rawset", luaB_rawset},
- {"select", luaB_select},
- {"setfenv", luaB_setfenv},
- {"setmetatable", luaB_setmetatable},
- {"tonumber", luaB_tonumber},
- {"tostring", luaB_tostring},
- {"type", luaB_type},
- {"unpack", luaB_unpack},
- {"xpcall", luaB_xpcall},
- {NULL, NULL}
-};
-
-
-/*
-** {======================================================
-** Coroutine library
-** =======================================================
-*/
-
-#define CO_RUN 0 /* running */
-#define CO_SUS 1 /* suspended */
-#define CO_NOR 2 /* 'normal' (it resumed another coroutine) */
-#define CO_DEAD 3
-
-static const char *const statnames[] =
- {"running", "suspended", "normal", "dead"};
-
-static int costatus (lua_State *L, lua_State *co) {
- if (L == co) return CO_RUN;
- switch (lua_status(co)) {
- case LUA_YIELD:
- return CO_SUS;
- case 0: {
- lua_Debug ar;
- if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
- return CO_NOR; /* it is running */
- else if (lua_gettop(co) == 0)
- return CO_DEAD;
- else
- return CO_SUS; /* initial state */
- }
- default: /* some error occured */
- return CO_DEAD;
- }
-}
-
-
-static int luaB_costatus (lua_State *L) {
- lua_State *co = lua_tothread(L, 1);
- luaL_argcheck(L, co, 1, "coroutine expected");
- lua_pushstring(L, statnames[costatus(L, co)]);
- return 1;
-}
-
-
-static int auxresume (lua_State *L, lua_State *co, int narg) {
- int status = costatus(L, co);
- if (!lua_checkstack(co, narg))
- luaL_error(L, "too many arguments to resume");
- if (status != CO_SUS) {
- lua_pushfstring(L, "cannot resume %s coroutine", statnames[status]);
- return -1; /* error flag */
- }
- lua_xmove(L, co, narg);
- lua_setlevel(L, co);
- status = lua_resume(co, narg);
- if (status == 0 || status == LUA_YIELD) {
- int nres = lua_gettop(co);
- if (!lua_checkstack(L, nres + 1))
- luaL_error(L, "too many results to resume");
- lua_xmove(co, L, nres); /* move yielded values */
- return nres;
- }
- else {
- lua_xmove(co, L, 1); /* move error message */
- return -1; /* error flag */
- }
-}
-
-
-static int luaB_coresume (lua_State *L) {
- lua_State *co = lua_tothread(L, 1);
- int r;
- luaL_argcheck(L, co, 1, "coroutine expected");
- r = auxresume(L, co, lua_gettop(L) - 1);
- if (r < 0) {
- lua_pushboolean(L, 0);
- lua_insert(L, -2);
- return 2; /* return false + error message */
- }
- else {
- lua_pushboolean(L, 1);
- lua_insert(L, -(r + 1));
- return r + 1; /* return true + `resume' returns */
- }
-}
-
-
-static int luaB_auxwrap (lua_State *L) {
- lua_State *co = lua_tothread(L, lua_upvalueindex(1));
- int r = auxresume(L, co, lua_gettop(L));
- if (r < 0) {
- if (lua_isstring(L, -1)) { /* error object is a string? */
- luaL_where(L, 1); /* add extra info */
- lua_insert(L, -2);
- lua_concat(L, 2);
- }
- lua_error(L); /* propagate error */
- }
- return r;
-}
-
-
-static int luaB_cocreate (lua_State *L) {
- lua_State *NL = lua_newthread(L);
- luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
- "Lua function expected");
- lua_pushvalue(L, 1); /* move function to top */
- lua_xmove(L, NL, 1); /* move function from L to NL */
- return 1;
-}
-
-
-static int luaB_cowrap (lua_State *L) {
- luaB_cocreate(L);
- lua_pushcclosure(L, luaB_auxwrap, 1);
- return 1;
-}
-
-
-static int luaB_yield (lua_State *L) {
- return lua_yield(L, lua_gettop(L));
-}
-
-
-static int luaB_corunning (lua_State *L) {
- if (lua_pushthread(L))
- lua_pushnil(L); /* main thread is not a coroutine */
- return 1;
-}
-
-
-static const luaL_Reg co_funcs[] = {
- {"create", luaB_cocreate},
- {"resume", luaB_coresume},
- {"running", luaB_corunning},
- {"status", luaB_costatus},
- {"wrap", luaB_cowrap},
- {"yield", luaB_yield},
- {NULL, NULL}
-};
-
-/* }====================================================== */
-
-
-static void auxopen (lua_State *L, const char *name,
- lua_CFunction f, lua_CFunction u) {
- lua_pushcfunction(L, u);
- lua_pushcclosure(L, f, 1);
- lua_setfield(L, -2, name);
-}
-
-
-static void base_open (lua_State *L) {
- /* set global _G */
- lua_pushvalue(L, LUA_GLOBALSINDEX);
- lua_setglobal(L, "_G");
- /* open lib into global table */
- luaL_register(L, "_G", base_funcs);
- lua_pushliteral(L, LUA_VERSION);
- lua_setglobal(L, "_VERSION"); /* set global _VERSION */
- /* `ipairs' and `pairs' need auxliliary functions as upvalues */
- auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
- auxopen(L, "pairs", luaB_pairs, luaB_next);
- /* `newproxy' needs a weaktable as upvalue */
- lua_createtable(L, 0, 1); /* new table `w' */
- lua_pushvalue(L, -1); /* `w' will be its own metatable */
- lua_setmetatable(L, -2);
- lua_pushliteral(L, "kv");
- lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */
- lua_pushcclosure(L, luaB_newproxy, 1);
- lua_setglobal(L, "newproxy"); /* set global `newproxy' */
-}
-
-
-LUALIB_API int luaopen_base (lua_State *L) {
- base_open(L);
- luaL_register(L, LUA_COLIBNAME, co_funcs);
- return 2;
-}
-
diff --git a/src/lua/src/lcode.c b/src/lua/src/lcode.c
deleted file mode 100644
index cff626b7f..000000000
--- a/src/lua/src/lcode.c
+++ /dev/null
@@ -1,839 +0,0 @@
-/*
-** $Id: lcode.c,v 2.25.1.3 2007/12/28 15:32:23 roberto Exp $
-** Code generator for Lua
-** See Copyright Notice in lua.h
-*/
-
-
-#include <stdlib.h>
-
-#define lcode_c
-#define LUA_CORE
-
-#include "lua.h"
-
-#include "lcode.h"
-#include "ldebug.h"
-#include "ldo.h"
-#include "lgc.h"
-#include "llex.h"
-#include "lmem.h"
-#include "lobject.h"
-#include "lopcodes.h"
-#include "lparser.h"
-#include "ltable.h"
-
-
-#define hasjumps(e) ((e)->t != (e)->f)
-
-
-static int isnumeral(expdesc *e) {
- return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);
-}
-
-
-void luaK_nil (FuncState *fs, int from, int n) {
- Instruction *previous;
- if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
- if (fs->pc == 0) { /* function start? */
- if (from >= fs->nactvar)
- return; /* positions are already clean */
- }
- else {
- previous = &fs->f->code[fs->pc-1];
- if (GET_OPCODE(*previous) == OP_LOADNIL) {
- int pfrom = GETARG_A(*previous);
- int pto = GETARG_B(*previous);
- if (pfrom <= from && from <= pto+1) { /* can connect both? */
- if (from+n-1 > pto)
- SETARG_B(*previous, from+n-1);
- return;
- }
- }
- }
- }
- luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */
-}
-
-
-int luaK_jump (FuncState *fs) {
- int jpc = fs->jpc; /* save list of jumps to here */
- int j;
- fs->jpc = NO_JUMP;
- j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
- luaK_concat(fs, &j, jpc); /* keep them on hold */
- return j;
-}
-
-
-void luaK_ret (FuncState *fs, int first, int nret) {
- luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
-}
-
-
-static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {
- luaK_codeABC(fs, op, A, B, C);
- return luaK_jump(fs);
-}
-
-
-static void fixjump (FuncState *fs, int pc, int dest) {
- Instruction *jmp = &fs->f->code[pc];
- int offset = dest-(pc+1);
- lua_assert(dest != NO_JUMP);
- if (abs(offset) > MAXARG_sBx)
- luaX_syntaxerror(fs->ls, "control structure too long");
- SETARG_sBx(*jmp, offset);
-}
-
-
-/*
-** returns current `pc' and marks it as a jump target (to avoid wrong
-** optimizations with consecutive instructions not in the same basic block).
-*/
-int luaK_getlabel (FuncState *fs) {
- fs->lasttarget = fs->pc;
- return fs->pc;
-}
-
-
-static int getjump (FuncState *fs, int pc) {
- int offset = GETARG_sBx(fs->f->code[pc]);
- if (offset == NO_JUMP) /* point to itself represents end of list */
- return NO_JUMP; /* end of list */
- else
- return (pc+1)+offset; /* turn offset into absolute position */
-}
-
-
-static Instruction *getjumpcontrol (FuncState *fs, int pc) {
- Instruction *pi = &fs->f->code[pc];
- if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
- return pi-1;
- else
- return pi;
-}
-
-
-/*
-** check whether list has any jump that do not produce a value
-** (or produce an inverted value)
-*/
-static int need_value (FuncState *fs, int list) {
- for (; list != NO_JUMP; list = getjump(fs, list)) {
- Instruction i = *getjumpcontrol(fs, list);
- if (GET_OPCODE(i) != OP_TESTSET) return 1;
- }
- return 0; /* not found */
-}
-
-
-static int patchtestreg (FuncState *fs, int node, int reg) {
- Instruction *i = getjumpcontrol(fs, node);
- if (GET_OPCODE(*i) != OP_TESTSET)
- return 0; /* cannot patch other instructions */
- if (reg != NO_REG && reg != GETARG_B(*i))
- SETARG_A(*i, reg);
- else /* no register to put value or register already has the value */
- *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));
-
- return 1;
-}
-
-
-static void removevalues (FuncState *fs, int list) {
- for (; list != NO_JUMP; list = getjump(fs, list))
- patchtestreg(fs, list, NO_REG);
-}
-
-
-static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
- int dtarget) {
- while (list != NO_JUMP) {
- int next = getjump(fs, list);
- if (patchtestreg(fs, list, reg))
- fixjump(fs, list, vtarget);
- else
- fixjump(fs, list, dtarget); /* jump to default target */
- list = next;
- }
-}
-
-
-static void dischargejpc (FuncState *fs) {
- patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);
- fs->jpc = NO_JUMP;
-}
-
-
-void luaK_patchlist (FuncState *fs, int list, int target) {
- if (target == fs->pc)
- luaK_patchtohere(fs, list);
- else {
- lua_assert(target < fs->pc);
- patchlistaux(fs, list, target, NO_REG, target);
- }
-}
-
-
-void luaK_patchtohere (FuncState *fs, int list) {
- luaK_getlabel(fs);
- luaK_concat(fs, &fs->jpc, list);
-}
-
-
-void luaK_concat (FuncState *fs, int *l1, int l2) {
- if (l2 == NO_JUMP) return;
- else if (*l1 == NO_JUMP)
- *l1 = l2;
- else {
- int list = *l1;
- int next;
- while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
- list = next;
- fixjump(fs, list, l2);
- }
-}
-
-
-void luaK_checkstack (FuncState *fs, int n) {
- int newstack = fs->freereg + n;
- if (newstack > fs->f->maxstacksize) {
- if (newstack >= MAXSTACK)
- luaX_syntaxerror(fs->ls, "function or expression too complex");
- fs->f->maxstacksize = cast_byte(newstack);
- }
-}
-
-
-void luaK_reserveregs (FuncState *fs, int n) {
- luaK_checkstack(fs, n);
- fs->freereg += n;
-}
-
-
-static void freereg (FuncState *fs, int reg) {
- if (!ISK(reg) && reg >= fs->nactvar) {
- fs->freereg--;
- lua_assert(reg == fs->freereg);
- }
-}
-
-
-static void freeexp (FuncState *fs, expdesc *e) {
- if (e->k == VNONRELOC)
- freereg(fs, e->u.s.info);
-}
-
-
-static int addk (FuncState *fs, TValue *k, TValue *v) {
- lua_State *L = fs->L;
- TValue *idx = luaH_set(L, fs->h, k);
- Proto *f = fs->f;
- int oldsize = f->sizek;
- if (ttisnumber(idx)) {
- lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v));
- return cast_int(nvalue(idx));
- }
- else { /* constant not found; create a new entry */
- setnvalue(idx, cast_num(fs->nk));
- luaM_growvector(L, f->k, fs->nk, f->sizek, TValue,
- MAXARG_Bx, "constant table overflow");
- while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
- setobj(L, &f->k[fs->nk], v);
- luaC_barrier(L, f, v);
- return fs->nk++;
- }
-}
-
-
-int luaK_stringK (FuncState *fs, TString *s) {
- TValue o;
- setsvalue(fs->L, &o, s);
- return addk(fs, &o, &o);
-}
-
-
-int luaK_numberK (FuncState *fs, lua_Number r) {
- TValue o;
- setnvalue(&o, r);
- return addk(fs, &o, &o);
-}
-
-
-static int boolK (FuncState *fs, int b) {
- TValue o;
- setbvalue(&o, b);
- return addk(fs, &o, &o);
-}
-
-
-static int nilK (FuncState *fs) {
- TValue k, v;
- setnilvalue(&v);
- /* cannot use nil as key; instead use table itself to represent nil */
- sethvalue(fs->L, &k, fs->h);
- return addk(fs, &k, &v);
-}
-
-
-void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
- if (e->k == VCALL) { /* expression is an open function call? */
- SETARG_C(getcode(fs, e), nresults+1);
- }
- else if (e->k == VVARARG) {
- SETARG_B(getcode(fs, e), nresults+1);
- SETARG_A(getcode(fs, e), fs->freereg);
- luaK_reserveregs(fs, 1);
- }
-}
-
-
-void luaK_setoneret (FuncState *fs, expdesc *e) {
- if (e->k == VCALL) { /* expression is an open function call? */
- e->k = VNONRELOC;
- e->u.s.info = GETARG_A(getcode(fs, e));
- }
- else if (e->k == VVARARG) {
- SETARG_B(getcode(fs, e), 2);
- e->k = VRELOCABLE; /* can relocate its simple result */
- }
-}
-
-
-void luaK_dischargevars (FuncState *fs, expdesc *e) {
- switch (e->k) {
- case VLOCAL: {
- e->k = VNONRELOC;
- break;
- }
- case VUPVAL: {
- e->u.s.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.s.info, 0);
- e->k = VRELOCABLE;
- break;
- }
- case VGLOBAL: {
- e->u.s.info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->u.s.info);
- e->k = VRELOCABLE;
- break;
- }
- case VINDEXED: {
- freereg(fs, e->u.s.aux);
- freereg(fs, e->u.s.info);
- e->u.s.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.s.info, e->u.s.aux);
- e->k = VRELOCABLE;
- break;
- }
- case VVARARG:
- case VCALL: {
- luaK_setoneret(fs, e);
- break;
- }
- default: break; /* there is one value available (somewhere) */
- }
-}
-
-
-static int code_label (FuncState *fs, int A, int b, int jump) {
- luaK_getlabel(fs); /* those instructions may be jump targets */
- return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
-}
-
-
-static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
- luaK_dischargevars(fs, e);
- switch (e->k) {
- case VNIL: {
- luaK_nil(fs, reg, 1);
- break;
- }
- case VFALSE: case VTRUE: {
- luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
- break;
- }
- case VK: {
- luaK_codeABx(fs, OP_LOADK, reg, e->u.s.info);
- break;
- }
- case VKNUM: {
- luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval));
- break;
- }
- case VRELOCABLE: {
- Instruction *pc = &getcode(fs, e);
- SETARG_A(*pc, reg);
- break;
- }
- case VNONRELOC: {
- if (reg != e->u.s.info)
- luaK_codeABC(fs, OP_MOVE, reg, e->u.s.info, 0);
- break;
- }
- default: {
- lua_assert(e->k == VVOID || e->k == VJMP);
- return; /* nothing to do... */
- }
- }
- e->u.s.info = reg;
- e->k = VNONRELOC;
-}
-
-
-static void discharge2anyreg (FuncState *fs, expdesc *e) {
- if (e->k != VNONRELOC) {
- luaK_reserveregs(fs, 1);
- discharge2reg(fs, e, fs->freereg-1);
- }
-}
-
-
-static void exp2reg (FuncState *fs, expdesc *e, int reg) {
- discharge2reg(fs, e, reg);
- if (e->k == VJMP)
- luaK_concat(fs, &e->t, e->u.s.info); /* put this jump in `t' list */
- if (hasjumps(e)) {
- int final; /* position after whole expression */
- int p_f = NO_JUMP; /* position of an eventual LOAD false */
- int p_t = NO_JUMP; /* position of an eventual LOAD true */
- if (need_value(fs, e->t) || need_value(fs, e->f)) {
- int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
- p_f = code_label(fs, reg, 0, 1);
- p_t = code_label(fs, reg, 1, 0);
- luaK_patchtohere(fs, fj);
- }
- final = luaK_getlabel(fs);
- patchlistaux(fs, e->f, final, reg, p_f);
- patchlistaux(fs, e->t, final, reg, p_t);
- }
- e->f = e->t = NO_JUMP;
- e->u.s.info = reg;
- e->k = VNONRELOC;
-}
-
-
-void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
- luaK_dischargevars(fs, e);
- freeexp(fs, e);
- luaK_reserveregs(fs, 1);
- exp2reg(fs, e, fs->freereg - 1);
-}
-
-
-int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
- luaK_dischargevars(fs, e);
- if (e->k == VNONRELOC) {
- if (!hasjumps(e)) return e->u.s.info; /* exp is already in a register */
- if (e->u.s.info >= fs->nactvar) { /* reg. is not a local? */
- exp2reg(fs, e, e->u.s.info); /* put value on it */
- return e->u.s.info;
- }
- }
- luaK_exp2nextreg(fs, e); /* default */
- return e->u.s.info;
-}
-
-
-void luaK_exp2val (FuncState *fs, expdesc *e) {
- if (hasjumps(e))
- luaK_exp2anyreg(fs, e);
- else
- luaK_dischargevars(fs, e);
-}
-
-
-int luaK_exp2RK (FuncState *fs, expdesc *e) {
- luaK_exp2val(fs, e);
- switch (e->k) {
- case VKNUM:
- case VTRUE:
- case VFALSE:
- case VNIL: {
- if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */
- e->u.s.info = (e->k == VNIL) ? nilK(fs) :
- (e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) :
- boolK(fs, (e->k == VTRUE));
- e->k = VK;
- return RKASK(e->u.s.info);
- }
- else break;
- }
- case VK: {
- if (e->u.s.info <= MAXINDEXRK) /* constant fit in argC? */
- return RKASK(e->u.s.info);
- else break;
- }
- default: break;
- }
- /* not a constant in the right range: put it in a register */
- return luaK_exp2anyreg(fs, e);
-}
-
-
-void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
- switch (var->k) {
- case VLOCAL: {
- freeexp(fs, ex);
- exp2reg(fs, ex, var->u.s.info);
- return;
- }
- case VUPVAL: {
- int e = luaK_exp2anyreg(fs, ex);
- luaK_codeABC(fs, OP_SETUPVAL, e, var->u.s.info, 0);
- break;
- }
- case VGLOBAL: {
- int e = luaK_exp2anyreg(fs, ex);
- luaK_codeABx(fs, OP_SETGLOBAL, e, var->u.s.info);
- break;
- }
- case VINDEXED: {
- int e = luaK_exp2RK(fs, ex);
- luaK_codeABC(fs, OP_SETTABLE, var->u.s.info, var->u.s.aux, e);
- break;
- }
- default: {
- lua_assert(0); /* invalid var kind to store */
- break;
- }
- }
- freeexp(fs, ex);
-}
-
-
-void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
- int func;
- luaK_exp2anyreg(fs, e);
- freeexp(fs, e);
- func = fs->freereg;
- luaK_reserveregs(fs, 2);
- luaK_codeABC(fs, OP_SELF, func, e->u.s.info, luaK_exp2RK(fs, key));
- freeexp(fs, key);
- e->u.s.info = func;
- e->k = VNONRELOC;
-}
-
-
-static void invertjump (FuncState *fs, expdesc *e) {
- Instruction *pc = getjumpcontrol(fs, e->u.s.info);
- lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
- GET_OPCODE(*pc) != OP_TEST);
- SETARG_A(*pc, !(GETARG_A(*pc)));
-}
-
-
-static int jumponcond (FuncState *fs, expdesc *e, int cond) {
- if (e->k == VRELOCABLE) {
- Instruction ie = getcode(fs, e);
- if (GET_OPCODE(ie) == OP_NOT) {
- fs->pc--; /* remove previous OP_NOT */
- return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);
- }
- /* else go through */
- }
- discharge2anyreg(fs, e);
- freeexp(fs, e);
- return condjump(fs, OP_TESTSET, NO_REG, e->u.s.info, cond);
-}
-
-
-void luaK_goiftrue (FuncState *fs, expdesc *e) {
- int pc; /* pc of last jump */
- luaK_dischargevars(fs, e);
- switch (e->k) {
- case VK: case VKNUM: case VTRUE: {
- pc = NO_JUMP; /* always true; do nothing */
- break;
- }
- case VFALSE: {
- pc = luaK_jump(fs); /* always jump */
- break;
- }
- case VJMP: {
- invertjump(fs, e);
- pc = e->u.s.info;
- break;
- }
- default: {
- pc = jumponcond(fs, e, 0);
- break;
- }
- }
- luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
- luaK_patchtohere(fs, e->t);
- e->t = NO_JUMP;
-}
-
-
-static void luaK_goiffalse (FuncState *fs, expdesc *e) {
- int pc; /* pc of last jump */
- luaK_dischargevars(fs, e);
- switch (e->k) {
- case VNIL: case VFALSE: {
- pc = NO_JUMP; /* always false; do nothing */
- break;
- }
- case VTRUE: {
- pc = luaK_jump(fs); /* always jump */
- break;
- }
- case VJMP: {
- pc = e->u.s.info;
- break;
- }
- default: {
- pc = jumponcond(fs, e, 1);
- break;
- }
- }
- luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
- luaK_patchtohere(fs, e->f);
- e->f = NO_JUMP;
-}
-
-
-static void codenot (FuncState *fs, expdesc *e) {
- luaK_dischargevars(fs, e);
- switch (e->k) {
- case VNIL: case VFALSE: {
- e->k = VTRUE;
- break;
- }
- case VK: case VKNUM: case VTRUE: {
- e->k = VFALSE;
- break;
- }
- case VJMP: {
- invertjump(fs, e);
- break;
- }
- case VRELOCABLE:
- case VNONRELOC: {
- discharge2anyreg(fs, e);
- freeexp(fs, e);
- e->u.s.info = luaK_codeABC(fs, OP_NOT, 0, e->u.s.info, 0);
- e->k = VRELOCABLE;
- break;
- }
- default: {
- lua_assert(0); /* cannot happen */
- break;
- }
- }
- /* interchange true and false lists */
- { int temp = e->f; e->f = e->t; e->t = temp; }
- removevalues(fs, e->f);
- removevalues(fs, e->t);
-}
-
-
-void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
- t->u.s.aux = luaK_exp2RK(fs, k);
- t->k = VINDEXED;
-}
-
-
-static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
- lua_Number v1, v2, r;
- if (!isnumeral(e1) || !isnumeral(e2)) return 0;
- v1 = e1->u.nval;
- v2 = e2->u.nval;
- switch (op) {
- case OP_ADD: r = luai_numadd(v1, v2); break;
- case OP_SUB: r = luai_numsub(v1, v2); break;
- case OP_MUL: r = luai_nummul(v1, v2); break;
- case OP_DIV:
- if (v2 == 0) return 0; /* do not attempt to divide by 0 */
- r = luai_numdiv(v1, v2); break;
- case OP_MOD:
- if (v2 == 0) return 0; /* do not attempt to divide by 0 */
- r = luai_nummod(v1, v2); break;
- case OP_POW: r = luai_numpow(v1, v2); break;
- case OP_UNM: r = luai_numunm(v1); break;
- case OP_LEN: return 0; /* no constant folding for 'len' */
- default: lua_assert(0); r = 0; break;
- }
- if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */
- e1->u.nval = r;
- return 1;
-}
-
-
-static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
- if (constfolding(op, e1, e2))
- return;
- else {
- int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0;
- int o1 = luaK_exp2RK(fs, e1);
- if (o1 > o2) {
- freeexp(fs, e1);
- freeexp(fs, e2);
- }
- else {
- freeexp(fs, e2);
- freeexp(fs, e1);
- }
- e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2);
- e1->k = VRELOCABLE;
- }
-}
-
-
-static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
- expdesc *e2) {
- int o1 = luaK_exp2RK(fs, e1);
- int o2 = luaK_exp2RK(fs, e2);
- freeexp(fs, e2);
- freeexp(fs, e1);
- if (cond == 0 && op != OP_EQ) {
- int temp; /* exchange args to replace by `<' or `<=' */
- temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
- cond = 1;
- }
- e1->u.s.info = condjump(fs, op, cond, o1, o2);
- e1->k = VJMP;
-}
-
-
-void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
- expdesc e2;
- e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;
- switch (op) {
- case OPR_MINUS: {
- if (!isnumeral(e))
- luaK_exp2anyreg(fs, e); /* cannot operate on non-numeric constants */
- codearith(fs, OP_UNM, e, &e2);
- break;
- }
- case OPR_NOT: codenot(fs, e); break;
- case OPR_LEN: {
- luaK_exp2anyreg(fs, e); /* cannot operate on constants */
- codearith(fs, OP_LEN, e, &e2);
- break;
- }
- default: lua_assert(0);
- }
-}
-
-
-void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
- switch (op) {
- case OPR_AND: {
- luaK_goiftrue(fs, v);
- break;
- }
- case OPR_OR: {
- luaK_goiffalse(fs, v);
- break;
- }
- case OPR_CONCAT: {
- luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
- break;
- }
- case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
- case OPR_MOD: case OPR_POW: {
- if (!isnumeral(v)) luaK_exp2RK(fs, v);
- break;
- }
- default: {
- luaK_exp2RK(fs, v);
- break;
- }
- }
-}
-
-
-void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
- switch (op) {
- case OPR_AND: {
- lua_assert(e1->t == NO_JUMP); /* list must be closed */
- luaK_dischargevars(fs, e2);
- luaK_concat(fs, &e2->f, e1->f);
- *e1 = *e2;
- break;
- }
- case OPR_OR: {
- lua_assert(e1->f == NO_JUMP); /* list must be closed */
- luaK_dischargevars(fs, e2);
- luaK_concat(fs, &e2->t, e1->t);
- *e1 = *e2;
- break;
- }
- case OPR_CONCAT: {
- luaK_exp2val(fs, e2);
- if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
- lua_assert(e1->u.s.info == GETARG_B(getcode(fs, e2))-1);
- freeexp(fs, e1);
- SETARG_B(getcode(fs, e2), e1->u.s.info);
- e1->k = VRELOCABLE; e1->u.s.info = e2->u.s.info;
- }
- else {
- luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
- codearith(fs, OP_CONCAT, e1, e2);
- }
- break;
- }
- case OPR_ADD: codearith(fs, OP_ADD, e1, e2); break;
- case OPR_SUB: codearith(fs, OP_SUB, e1, e2); break;
- case OPR_MUL: codearith(fs, OP_MUL, e1, e2); break;
- case OPR_DIV: codearith(fs, OP_DIV, e1, e2); break;
- case OPR_MOD: codearith(fs, OP_MOD, e1, e2); break;
- case OPR_POW: codearith(fs, OP_POW, e1, e2); break;
- case OPR_EQ: codecomp(fs, OP_EQ, 1, e1, e2); break;
- case OPR_NE: codecomp(fs, OP_EQ, 0, e1, e2); break;
- case OPR_LT: codecomp(fs, OP_LT, 1, e1, e2); break;
- case OPR_LE: codecomp(fs, OP_LE, 1, e1, e2); break;
- case OPR_GT: codecomp(fs, OP_LT, 0, e1, e2); break;
- case OPR_GE: codecomp(fs, OP_LE, 0, e1, e2); break;
- default: lua_assert(0);
- }
-}
-
-
-void luaK_fixline (FuncState *fs, int line) {
- fs->f->lineinfo[fs->pc - 1] = line;
-}
-
-
-static int luaK_code (FuncState *fs, Instruction i, int line) {
- Proto *f = fs->f;
- dischargejpc(fs); /* `pc' will change */
- /* put new instruction in code array */
- luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
- MAX_INT, "code size overflow");
- f->code[fs->pc] = i;
- /* save corresponding line information */
- luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
- MAX_INT, "code size overflow");
- f->lineinfo[fs->pc] = line;
- return fs->pc++;
-}
-
-
-int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
- lua_assert(getOpMode(o) == iABC);
- lua_assert(getBMode(o) != OpArgN || b == 0);
- lua_assert(getCMode(o) != OpArgN || c == 0);
- return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline);
-}
-
-
-int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
- lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
- lua_assert(getCMode(o) == OpArgN);
- return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline);
-}
-
-
-void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
- int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
- int b = (tostore == LUA_MULTRET) ? 0 : tostore;
- lua_assert(tostore != 0);
- if (c <= MAXARG_C)
- luaK_codeABC(fs, OP_SETLIST, base, b, c);
- else {
- luaK_codeABC(fs, OP_SETLIST, base, b, 0);
- luaK_code(fs, cast(Instruction, c), fs->ls->lastline);
- }
- fs->freereg = base + 1; /* free registers with list values */
-}
-
diff --git a/src/lua/src/lcode.h b/src/lua/src/lcode.h
deleted file mode 100644
index b941c6072..000000000
--- a/src/lua/src/lcode.h
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
-** $Id: lcode.h,v 1.48.1.1 2007/12/27 13:02:25 roberto Exp $
-** Code generator for Lua
-** See Copyright Notice in lua.h
-*/
-
-#ifndef lcode_h
-#define lcode_h
-
-#include "llex.h"
-#include "lobject.h"
-#include "lopcodes.h"
-#include "lparser.h"
-
-
-/*
-** Marks the end of a patch list. It is an invalid value both as an absolute
-** address, and as a list link (would link an element to itself).
-*/
-#define NO_JUMP (-1)
-
-
-/*
-** grep "ORDER OPR" if you change these enums
-*/
-typedef enum BinOpr {
- OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW,
- OPR_CONCAT,
- OPR_NE, OPR_EQ,
- OPR_LT, OPR_LE, OPR_GT, OPR_GE,
- OPR_AND, OPR_OR,
- OPR_NOBINOPR
-} BinOpr;
-
-
-typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
-
-
-#define getcode(fs,e) ((fs)->f->code[(e)->u.s.info])
-
-#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx)
-
-#define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET)
-
-LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
-LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C);
-LUAI_FUNC void luaK_fixline (FuncState *fs, int line);
-LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n);
-LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n);
-LUAI_FUNC void luaK_checkstack (FuncState *fs, int n);
-LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s);
-LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r);
-LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e);
-LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e);
-LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e);
-LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e);
-LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e);
-LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key);
-LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
-LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e);
-LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e);
-LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults);
-LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e);
-LUAI_FUNC int luaK_jump (FuncState *fs);
-LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret);
-LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target);
-LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list);
-LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2);
-LUAI_FUNC int luaK_getlabel (FuncState *fs);
-LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v);
-LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
-LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2);
-LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore);
-
-
-#endif
diff --git a/src/lua/src/ldblib.c b/src/lua/src/ldblib.c
deleted file mode 100644
index 67de1222a..000000000
--- a/src/lua/src/ldblib.c
+++ /dev/null
@@ -1,397 +0,0 @@
-/*
-** $Id: ldblib.c,v 1.104.1.3 2008/01/21 13:11:21 roberto Exp $
-** Interface from Lua to its debug API
-** See Copyright Notice in lua.h
-*/
-
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define ldblib_c
-#define LUA_LIB
-
-#include "lua.h"
-
-#include "lauxlib.h"
-#include "lualib.h"
-
-
-
-static int db_getregistry (lua_State *L) {
- lua_pushvalue(L, LUA_REGISTRYINDEX);
- return 1;
-}
-
-
-static int db_getmetatable (lua_State *L) {
- luaL_checkany(L, 1);
- if (!lua_getmetatable(L, 1)) {
- lua_pushnil(L); /* no metatable */
- }
- return 1;
-}
-
-
-static int db_setmetatable (lua_State *L) {
- int t = lua_type(L, 2);
- luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
- "nil or table expected");
- lua_settop(L, 2);
- lua_pushboolean(L, lua_setmetatable(L, 1));
- return 1;
-}
-
-
-static int db_getfenv (lua_State *L) {
- lua_getfenv(L, 1);
- return 1;
-}
-
-
-static int db_setfenv (lua_State *L) {
- luaL_checktype(L, 2, LUA_TTABLE);
- lua_settop(L, 2);
- if (lua_setfenv(L, 1) == 0)
- luaL_error(L, LUA_QL("setfenv")
- " cannot change environment of given object");
- return 1;
-}
-
-
-static void settabss (lua_State *L, const char *i, const char *v) {
- lua_pushstring(L, v);
- lua_setfield(L, -2, i);
-}
-
-
-static void settabsi (lua_State *L, const char *i, int v) {
- lua_pushinteger(L, v);
- lua_setfield(L, -2, i);
-}
-
-
-static lua_State *getthread (lua_State *L, int *arg) {
- if (lua_isthread(L, 1)) {
- *arg = 1;
- return lua_tothread(L, 1);
- }
- else {
- *arg = 0;
- return L;
- }
-}
-
-
-static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
- if (L == L1) {
- lua_pushvalue(L, -2);
- lua_remove(L, -3);
- }
- else
- lua_xmove(L1, L, 1);
- lua_setfield(L, -2, fname);
-}
-
-
-static int db_getinfo (lua_State *L) {
- lua_Debug ar;
- int arg;
- lua_State *L1 = getthread(L, &arg);
- const char *options = luaL_optstring(L, arg+2, "flnSu");
- if (lua_isnumber(L, arg+1)) {
- if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) {
- lua_pushnil(L); /* level out of range */
- return 1;
- }
- }
- else if (lua_isfunction(L, arg+1)) {
- lua_pushfstring(L, ">%s", options);
- options = lua_tostring(L, -1);
- lua_pushvalue(L, arg+1);
- lua_xmove(L, L1, 1);
- }
- else
- return luaL_argerror(L, arg+1, "function or level expected");
- if (!lua_getinfo(L1, options, &ar))
- return luaL_argerror(L, arg+2, "invalid option");
- lua_createtable(L, 0, 2);
- if (strchr(options, 'S')) {
- settabss(L, "source", ar.source);
- settabss(L, "short_src", ar.short_src);
- settabsi(L, "linedefined", ar.linedefined);
- settabsi(L, "lastlinedefined", ar.lastlinedefined);
- settabss(L, "what", ar.what);
- }
- if (strchr(options, 'l'))
- settabsi(L, "currentline", ar.currentline);
- if (strchr(options, 'u'))
- settabsi(L, "nups", ar.nups);
- if (strchr(options, 'n')) {
- settabss(L, "name", ar.name);
- settabss(L, "namewhat", ar.namewhat);
- }
- if (strchr(options, 'L'))
- treatstackoption(L, L1, "activelines");
- if (strchr(options, 'f'))
- treatstackoption(L, L1, "func");
- return 1; /* return table */
-}
-
-
-static int db_getlocal (lua_State *L) {
- int arg;
- lua_State *L1 = getthread(L, &arg);
- lua_Debug ar;
- const char *name;
- if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
- return luaL_argerror(L, arg+1, "level out of range");
- name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2));
- if (name) {
- lua_xmove(L1, L, 1);
- lua_pushstring(L, name);
- lua_pushvalue(L, -2);
- return 2;
- }
- else {
- lua_pushnil(L);
- return 1;
- }
-}
-
-
-static int db_setlocal (lua_State *L) {
- int arg;
- lua_State *L1 = getthread(L, &arg);
- lua_Debug ar;
- if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
- return luaL_argerror(L, arg+1, "level out of range");
- luaL_checkany(L, arg+3);
- lua_settop(L, arg+3);
- lua_xmove(L, L1, 1);
- lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2)));
- return 1;
-}
-
-
-static int auxupvalue (lua_State *L, int get) {
- const char *name;
- int n = luaL_checkint(L, 2);
- luaL_checktype(L, 1, LUA_TFUNCTION);
- if (lua_iscfunction(L, 1)) return 0; /* cannot touch C upvalues from Lua */
- name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
- if (name == NULL) return 0;
- lua_pushstring(L, name);
- lua_insert(L, -(get+1));
- return get + 1;
-}
-
-
-static int db_getupvalue (lua_State *L) {
- return auxupvalue(L, 1);
-}
-
-
-static int db_setupvalue (lua_State *L) {
- luaL_checkany(L, 3);
- return auxupvalue(L, 0);
-}
-
-
-
-static const char KEY_HOOK = 'h';
-
-
-static void hookf (lua_State *L, lua_Debug *ar) {
- static const char *const hooknames[] =
- {"call", "return", "line", "count", "tail return"};
- lua_pushlightuserdata(L, (void *)&KEY_HOOK);
- lua_rawget(L, LUA_REGISTRYINDEX);
- lua_pushlightuserdata(L, L);
- lua_rawget(L, -2);
- if (lua_isfunction(L, -1)) {
- lua_pushstring(L, hooknames[(int)ar->event]);
- if (ar->currentline >= 0)
- lua_pushinteger(L, ar->currentline);
- else lua_pushnil(L);
- lua_assert(lua_getinfo(L, "lS", ar));
- lua_call(L, 2, 0);
- }
-}
-
-
-static int makemask (const char *smask, int count) {
- int mask = 0;
- if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
- if (strchr(smask, 'r')) mask |= LUA_MASKRET;
- if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
- if (count > 0) mask |= LUA_MASKCOUNT;
- return mask;
-}
-
-
-static char *unmakemask (int mask, char *smask) {
- int i = 0;
- if (mask & LUA_MASKCALL) smask[i++] = 'c';
- if (mask & LUA_MASKRET) smask[i++] = 'r';
- if (mask & LUA_MASKLINE) smask[i++] = 'l';
- smask[i] = '\0';
- return smask;
-}
-
-
-static void gethooktable (lua_State *L) {
- lua_pushlightuserdata(L, (void *)&KEY_HOOK);
- lua_rawget(L, LUA_REGISTRYINDEX);
- if (!lua_istable(L, -1)) {
- lua_pop(L, 1);
- lua_createtable(L, 0, 1);
- lua_pushlightuserdata(L, (void *)&KEY_HOOK);
- lua_pushvalue(L, -2);
- lua_rawset(L, LUA_REGISTRYINDEX);
- }
-}
-
-
-static int db_sethook (lua_State *L) {
- int arg, mask, count;
- lua_Hook func;
- lua_State *L1 = getthread(L, &arg);
- if (lua_isnoneornil(L, arg+1)) {
- lua_settop(L, arg+1);
- func = NULL; mask = 0; count = 0; /* turn off hooks */
- }
- else {
- const char *smask = luaL_checkstring(L, arg+2);
- luaL_checktype(L, arg+1, LUA_TFUNCTION);
- count = luaL_optint(L, arg+3, 0);
- func = hookf; mask = makemask(smask, count);
- }
- gethooktable(L);
- lua_pushlightuserdata(L, L1);
- lua_pushvalue(L, arg+1);
- lua_rawset(L, -3); /* set new hook */
- lua_pop(L, 1); /* remove hook table */
- lua_sethook(L1, func, mask, count); /* set hooks */
- return 0;
-}
-
-
-static int db_gethook (lua_State *L) {
- int arg;
- lua_State *L1 = getthread(L, &arg);
- char buff[5];
- int mask = lua_gethookmask(L1);
- lua_Hook hook = lua_gethook(L1);
- if (hook != NULL && hook != hookf) /* external hook? */
- lua_pushliteral(L, "external hook");
- else {
- gethooktable(L);
- lua_pushlightuserdata(L, L1);
- lua_rawget(L, -2); /* get hook */
- lua_remove(L, -2); /* remove hook table */
- }
- lua_pushstring(L, unmakemask(mask, buff));
- lua_pushinteger(L, lua_gethookcount(L1));
- return 3;
-}
-
-
-static int db_debug (lua_State *L) {
- for (;;) {
- char buffer[250];
- fputs("lua_debug> ", stderr);
- if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
- strcmp(buffer, "cont\n") == 0)
- return 0;
- if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
- lua_pcall(L, 0, 0, 0)) {
- fputs(lua_tostring(L, -1), stderr);
- fputs("\n", stderr);
- }
- lua_settop(L, 0); /* remove eventual returns */
- }
-}
-
-
-#define LEVELS1 12 /* size of the first part of the stack */
-#define LEVELS2 10 /* size of the second part of the stack */
-
-static int db_errorfb (lua_State *L) {
- int level;
- int firstpart = 1; /* still before eventual `...' */
- int arg;
- lua_State *L1 = getthread(L, &arg);
- lua_Debug ar;
- if (lua_isnumber(L, arg+2)) {
- level = (int)lua_tointeger(L, arg+2);
- lua_pop(L, 1);
- }
- else
- level = (L == L1) ? 1 : 0; /* level 0 may be this own function */
- if (lua_gettop(L) == arg)
- lua_pushliteral(L, "");
- else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */
- else lua_pushliteral(L, "\n");
- lua_pushliteral(L, "stack traceback:");
- while (lua_getstack(L1, level++, &ar)) {
- if (level > LEVELS1 && firstpart) {
- /* no more than `LEVELS2' more levels? */
- if (!lua_getstack(L1, level+LEVELS2, &ar))
- level--; /* keep going */
- else {
- lua_pushliteral(L, "\n\t..."); /* too many levels */
- while (lua_getstack(L1, level+LEVELS2, &ar)) /* find last levels */
- level++;
- }
- firstpart = 0;
- continue;
- }
- lua_pushliteral(L, "\n\t");
- lua_getinfo(L1, "Snl", &ar);
- lua_pushfstring(L, "%s:", ar.short_src);
- if (ar.currentline > 0)
- lua_pushfstring(L, "%d:", ar.currentline);
- if (*ar.namewhat != '\0') /* is there a name? */
- lua_pushfstring(L, " in function " LUA_QS, ar.name);
- else {
- if (*ar.what == 'm') /* main? */
- lua_pushfstring(L, " in main chunk");
- else if (*ar.what == 'C' || *ar.what == 't')
- lua_pushliteral(L, " ?"); /* C function or tail call */
- else
- lua_pushfstring(L, " in function <%s:%d>",
- ar.short_src, ar.linedefined);
- }
- lua_concat(L, lua_gettop(L) - arg);
- }
- lua_concat(L, lua_gettop(L) - arg);
- return 1;
-}
-
-
-static const luaL_Reg dblib[] = {
- {"debug", db_debug},
- {"getfenv", db_getfenv},
- {"gethook", db_gethook},
- {"getinfo", db_getinfo},
- {"getlocal", db_getlocal},
- {"getregistry", db_getregistry},
- {"getmetatable", db_getmetatable},
- {"getupvalue", db_getupvalue},
- {"setfenv", db_setfenv},
- {"sethook", db_sethook},
- {"setlocal", db_setlocal},
- {"setmetatable", db_setmetatable},
- {"setupvalue", db_setupvalue},
- {"traceback", db_errorfb},
- {NULL, NULL}
-};
-
-
-LUALIB_API int luaopen_debug (lua_State *L) {
- luaL_register(L, LUA_DBLIBNAME, dblib);
- return 1;
-}
-
diff --git a/src/lua/src/ldebug.c b/src/lua/src/ldebug.c
deleted file mode 100644
index 50ad3d380..000000000
--- a/src/lua/src/ldebug.c
+++ /dev/null
@@ -1,638 +0,0 @@
-/*
-** $Id: ldebug.c,v 2.29.1.6 2008/05/08 16:56:26 roberto Exp $
-** Debug Interface
-** See Copyright Notice in lua.h
-*/
-
-
-#include <stdarg.h>
-#include <stddef.h>
-#include <string.h>
-
-
-#define ldebug_c
-#define LUA_CORE
-
-#include "lua.h"
-
-#include "lapi.h"
-#include "lcode.h"
-#include "ldebug.h"
-#include "ldo.h"
-#include "lfunc.h"
-#include "lobject.h"
-#include "lopcodes.h"
-#include "lstate.h"
-#include "lstring.h"
-#include "ltable.h"
-#include "ltm.h"
-#include "lvm.h"
-
-
-
-static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
-
-
-static int currentpc (lua_State *L, CallInfo *ci) {
- if (!isLua(ci)) return -1; /* function is not a Lua function? */
- if (ci == L->ci)
- ci->savedpc = L->savedpc;
- return pcRel(ci->savedpc, ci_func(ci)->l.p);
-}
-
-
-static int currentline (lua_State *L, CallInfo *ci) {
- int pc = currentpc(L, ci);
- if (pc < 0)
- return -1; /* only active lua functions have current-line information */
- else
- return getline(ci_func(ci)->l.p, pc);
-}
-
-
-/*
-** this function can be called asynchronous (e.g. during a signal)
-*/
-LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
- if (func == NULL || mask == 0) { /* turn off hooks? */
- mask = 0;
- func = NULL;
- }
- L->hook = func;
- L->basehookcount = count;
- resethookcount(L);
- L->hookmask = cast_byte(mask);
- return 1;
-}
-
-
-LUA_API lua_Hook lua_gethook (lua_State *L) {
- return L->hook;
-}
-
-
-LUA_API int lua_gethookmask (lua_State *L) {
- return L->hookmask;
-}
-
-
-LUA_API int lua_gethookcount (lua_State *L) {
- return L->basehookcount;
-}
-
-
-LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
- int status;
- CallInfo *ci;
- lua_lock(L);
- for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) {
- level--;
- if (f_isLua(ci)) /* Lua function? */
- level -= ci->tailcalls; /* skip lost tail calls */
- }
- if (level == 0 && ci > L->base_ci) { /* level found? */
- status = 1;
- ar->i_ci = cast_int(ci - L->base_ci);
- }
- else if (level < 0) { /* level is of a lost tail call? */
- status = 1;
- ar->i_ci = 0;
- }
- else status = 0; /* no such level */
- lua_unlock(L);
- return status;
-}
-
-
-static Proto *getluaproto (CallInfo *ci) {
- return (isLua(ci) ? ci_func(ci)->l.p : NULL);
-}
-
-
-static const char *findlocal (lua_State *L, CallInfo *ci, int n) {
- const char *name;
- Proto *fp = getluaproto(ci);
- if (fp && (name = luaF_getlocalname(fp, n, currentpc(L, ci))) != NULL)
- return name; /* is a local variable in a Lua function */
- else {
- StkId limit = (ci == L->ci) ? L->top : (ci+1)->func;
- if (limit - ci->base >= n && n > 0) /* is 'n' inside 'ci' stack? */
- return "(*temporary)";
- else
- return NULL;
- }
-}
-
-
-LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
- CallInfo *ci = L->base_ci + ar->i_ci;
- const char *name = findlocal(L, ci, n);
- lua_lock(L);
- if (name)
- luaA_pushobject(L, ci->base + (n - 1));
- lua_unlock(L);
- return name;
-}
-
-
-LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
- CallInfo *ci = L->base_ci + ar->i_ci;
- const char *name = findlocal(L, ci, n);
- lua_lock(L);
- if (name)
- setobjs2s(L, ci->base + (n - 1), L->top - 1);
- L->top--; /* pop value */
- lua_unlock(L);
- return name;
-}
-
-
-static void funcinfo (lua_Debug *ar, Closure *cl) {
- if (cl->c.isC) {
- ar->source = "=[C]";
- ar->linedefined = -1;
- ar->lastlinedefined = -1;
- ar->what = "C";
- }
- else {
- ar->source = getstr(cl->l.p->source);
- ar->linedefined = cl->l.p->linedefined;
- ar->lastlinedefined = cl->l.p->lastlinedefined;
- ar->what = (ar->linedefined == 0) ? "main" : "Lua";
- }
- luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
-}
-
-
-static void info_tailcall (lua_Debug *ar) {
- ar->name = ar->namewhat = "";
- ar->what = "tail";
- ar->lastlinedefined = ar->linedefined = ar->currentline = -1;
- ar->source = "=(tail call)";
- luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
- ar->nups = 0;
-}
-
-
-static void collectvalidlines (lua_State *L, Closure *f) {
- if (f == NULL || f->c.isC) {
- setnilvalue(L->top);
- }
- else {
- Table *t = luaH_new(L, 0, 0);
- int *lineinfo = f->l.p->lineinfo;
- int i;
- for (i=0; i<f->l.p->sizelineinfo; i++)
- setbvalue(luaH_setnum(L, t, lineinfo[i]), 1);
- sethvalue(L, L->top, t);
- }
- incr_top(L);
-}
-
-
-static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
- Closure *f, CallInfo *ci) {
- int status = 1;
- if (f == NULL) {
- info_tailcall(ar);
- return status;
- }
- for (; *what; what++) {
- switch (*what) {
- case 'S': {
- funcinfo(ar, f);
- break;
- }
- case 'l': {
- ar->currentline = (ci) ? currentline(L, ci) : -1;
- break;
- }
- case 'u': {
- ar->nups = f->c.nupvalues;
- break;
- }
- case 'n': {
- ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
- if (ar->namewhat == NULL) {
- ar->namewhat = ""; /* not found */
- ar->name = NULL;
- }
- break;
- }
- case 'L':
- case 'f': /* handled by lua_getinfo */
- break;
- default: status = 0; /* invalid option */
- }
- }
- return status;
-}
-
-
-LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
- int status;
- Closure *f = NULL;
- CallInfo *ci = NULL;
- lua_lock(L);
- if (*what == '>') {
- StkId func = L->top - 1;
- luai_apicheck(L, ttisfunction(func));
- what++; /* skip the '>' */
- f = clvalue(func);
- L->top--; /* pop function */
- }
- else if (ar->i_ci != 0) { /* no tail call? */
- ci = L->base_ci + ar->i_ci;
- lua_assert(ttisfunction(ci->func));
- f = clvalue(ci->func);
- }
- status = auxgetinfo(L, what, ar, f, ci);
- if (strchr(what, 'f')) {
- if (f == NULL) setnilvalue(L->top);
- else setclvalue(L, L->top, f);
- incr_top(L);
- }
- if (strchr(what, 'L'))
- collectvalidlines(L, f);
- lua_unlock(L);
- return status;
-}
-
-
-/*
-** {======================================================
-** Symbolic Execution and code checker
-** =======================================================
-*/
-
-#define check(x) if (!(x)) return 0;
-
-#define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
-
-#define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
-
-
-
-static int precheck (const Proto *pt) {
- check(pt->maxstacksize <= MAXSTACK);
- check(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize);
- check(!(pt->is_vararg & VARARG_NEEDSARG) ||
- (pt->is_vararg & VARARG_HASARG));
- check(pt->sizeupvalues <= pt->nups);
- check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0);
- check(pt->sizecode > 0 && GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
- return 1;
-}
-
-
-#define checkopenop(pt,pc) luaG_checkopenop((pt)->code[(pc)+1])
-
-int luaG_checkopenop (Instruction i) {
- switch (GET_OPCODE(i)) {
- case OP_CALL:
- case OP_TAILCALL:
- case OP_RETURN:
- case OP_SETLIST: {
- check(GETARG_B(i) == 0);
- return 1;
- }
- default: return 0; /* invalid instruction after an open call */
- }
-}
-
-
-static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) {
- switch (mode) {
- case OpArgN: check(r == 0); break;
- case OpArgU: break;
- case OpArgR: checkreg(pt, r); break;
- case OpArgK:
- check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize);
- break;
- }
- return 1;
-}
-
-
-static Instruction symbexec (const Proto *pt, int lastpc, int reg) {
- int pc;
- int last; /* stores position of last instruction that changed `reg' */
- last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */
- check(precheck(pt));
- for (pc = 0; pc < lastpc; pc++) {
- Instruction i = pt->code[pc];
- OpCode op = GET_OPCODE(i);
- int a = GETARG_A(i);
- int b = 0;
- int c = 0;
- check(op < NUM_OPCODES);
- checkreg(pt, a);
- switch (getOpMode(op)) {
- case iABC: {
- b = GETARG_B(i);
- c = GETARG_C(i);
- check(checkArgMode(pt, b, getBMode(op)));
- check(checkArgMode(pt, c, getCMode(op)));
- break;
- }
- case iABx: {
- b = GETARG_Bx(i);
- if (getBMode(op) == OpArgK) check(b < pt->sizek);
- break;
- }
- case iAsBx: {
- b = GETARG_sBx(i);
- if (getBMode(op) == OpArgR) {
- int dest = pc+1+b;
- check(0 <= dest && dest < pt->sizecode);
- if (dest > 0) {
- int j;
- /* check that it does not jump to a setlist count; this
- is tricky, because the count from a previous setlist may
- have the same value of an invalid setlist; so, we must
- go all the way back to the first of them (if any) */
- for (j = 0; j < dest; j++) {
- Instruction d = pt->code[dest-1-j];
- if (!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)) break;
- }
- /* if 'j' is even, previous value is not a setlist (even if
- it looks like one) */
- check((j&1) == 0);
- }
- }
- break;
- }
- }
- if (testAMode(op)) {
- if (a == reg) last = pc; /* change register `a' */
- }
- if (testTMode(op)) {
- check(pc+2 < pt->sizecode); /* check skip */
- check(GET_OPCODE(pt->code[pc+1]) == OP_JMP);
- }
- switch (op) {
- case OP_LOADBOOL: {
- if (c == 1) { /* does it jump? */
- check(pc+2 < pt->sizecode); /* check its jump */
- check(GET_OPCODE(pt->code[pc+1]) != OP_SETLIST ||
- GETARG_C(pt->code[pc+1]) != 0);
- }
- break;
- }
- case OP_LOADNIL: {
- if (a <= reg && reg <= b)
- last = pc; /* set registers from `a' to `b' */
- break;
- }
- case OP_GETUPVAL:
- case OP_SETUPVAL: {
- check(b < pt->nups);
- break;
- }
- case OP_GETGLOBAL:
- case OP_SETGLOBAL: {
- check(ttisstring(&pt->k[b]));
- break;
- }
- case OP_SELF: {
- checkreg(pt, a+1);
- if (reg == a+1) last = pc;
- break;
- }
- case OP_CONCAT: {
- check(b < c); /* at least two operands */
- break;
- }
- case OP_TFORLOOP: {
- check(c >= 1); /* at least one result (control variable) */
- checkreg(pt, a+2+c); /* space for results */
- if (reg >= a+2) last = pc; /* affect all regs above its base */
- break;
- }
- case OP_FORLOOP:
- case OP_FORPREP:
- checkreg(pt, a+3);
- /* go through */
- case OP_JMP: {
- int dest = pc+1+b;
- /* not full check and jump is forward and do not skip `lastpc'? */
- if (reg != NO_REG && pc < dest && dest <= lastpc)
- pc += b; /* do the jump */
- break;
- }
- case OP_CALL:
- case OP_TAILCALL: {
- if (b != 0) {
- checkreg(pt, a+b-1);
- }
- c--; /* c = num. returns */
- if (c == LUA_MULTRET) {
- check(checkopenop(pt, pc));
- }
- else if (c != 0)
- checkreg(pt, a+c-1);
- if (reg >= a) last = pc; /* affect all registers above base */
- break;
- }
- case OP_RETURN: {
- b--; /* b = num. returns */
- if (b > 0) checkreg(pt, a+b-1);
- break;
- }
- case OP_SETLIST: {
- if (b > 0) checkreg(pt, a + b);
- if (c == 0) {
- pc++;
- check(pc < pt->sizecode - 1);
- }
- break;
- }
- case OP_CLOSURE: {
- int nup, j;
- check(b < pt->sizep);
- nup = pt->p[b]->nups;
- check(pc + nup < pt->sizecode);
- for (j = 1; j <= nup; j++) {
- OpCode op1 = GET_OPCODE(pt->code[pc + j]);
- check(op1 == OP_GETUPVAL || op1 == OP_MOVE);
- }
- if (reg != NO_REG) /* tracing? */
- pc += nup; /* do not 'execute' these pseudo-instructions */
- break;
- }
- case OP_VARARG: {
- check((pt->is_vararg & VARARG_ISVARARG) &&
- !(pt->is_vararg & VARARG_NEEDSARG));
- b--;
- if (b == LUA_MULTRET) check(checkopenop(pt, pc));
- checkreg(pt, a+b-1);
- break;
- }
- default: break;
- }
- }
- return pt->code[last];
-}
-
-#undef check
-#undef checkjump
-#undef checkreg
-
-/* }====================================================== */
-
-
-int luaG_checkcode (const Proto *pt) {
- return (symbexec(pt, pt->sizecode, NO_REG) != 0);
-}
-
-
-static const char *kname (Proto *p, int c) {
- if (ISK(c) && ttisstring(&p->k[INDEXK(c)]))
- return svalue(&p->k[INDEXK(c)]);
- else
- return "?";
-}
-
-
-static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos,
- const char **name) {
- if (isLua(ci)) { /* a Lua function? */
- Proto *p = ci_func(ci)->l.p;
- int pc = currentpc(L, ci);
- Instruction i;
- *name = luaF_getlocalname(p, stackpos+1, pc);
- if (*name) /* is a local? */
- return "local";
- i = symbexec(p, pc, stackpos); /* try symbolic execution */
- lua_assert(pc != -1);
- switch (GET_OPCODE(i)) {
- case OP_GETGLOBAL: {
- int g = GETARG_Bx(i); /* global index */
- lua_assert(ttisstring(&p->k[g]));
- *name = svalue(&p->k[g]);
- return "global";
- }
- case OP_MOVE: {
- int a = GETARG_A(i);
- int b = GETARG_B(i); /* move from `b' to `a' */
- if (b < a)
- return getobjname(L, ci, b, name); /* get name for `b' */
- break;
- }
- case OP_GETTABLE: {
- int k = GETARG_C(i); /* key index */
- *name = kname(p, k);
- return "field";
- }
- case OP_GETUPVAL: {
- int u = GETARG_B(i); /* upvalue index */
- *name = p->upvalues ? getstr(p->upvalues[u]) : "?";
- return "upvalue";
- }
- case OP_SELF: {
- int k = GETARG_C(i); /* key index */
- *name = kname(p, k);
- return "method";
- }
- default: break;
- }
- }
- return NULL; /* no useful name found */
-}
-
-
-static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
- Instruction i;
- if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1))
- return NULL; /* calling function is not Lua (or is unknown) */
- ci--; /* calling function */
- i = ci_func(ci)->l.p->code[currentpc(L, ci)];
- if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||
- GET_OPCODE(i) == OP_TFORLOOP)
- return getobjname(L, ci, GETARG_A(i), name);
- else
- return NULL; /* no useful name can be found */
-}
-
-
-/* only ANSI way to check whether a pointer points to an array */
-static int isinstack (CallInfo *ci, const TValue *o) {
- StkId p;
- for (p = ci->base; p < ci->top; p++)
- if (o == p) return 1;
- return 0;
-}
-
-
-void luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
- const char *name = NULL;
- const char *t = luaT_typenames[ttype(o)];
- const char *kind = (isinstack(L->ci, o)) ?
- getobjname(L, L->ci, cast_int(o - L->base), &name) :
- NULL;
- if (kind)
- luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)",
- op, kind, name, t);
- else
- luaG_runerror(L, "attempt to %s a %s value", op, t);
-}
-
-
-void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
- if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
- lua_assert(!ttisstring(p1) && !ttisnumber(p1));
- luaG_typeerror(L, p1, "concatenate");
-}
-
-
-void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
- TValue temp;
- if (luaV_tonumber(p1, &temp) == NULL)
- p2 = p1; /* first operand is wrong */
- luaG_typeerror(L, p2, "perform arithmetic on");
-}
-
-
-int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
- const char *t1 = luaT_typenames[ttype(p1)];
- const char *t2 = luaT_typenames[ttype(p2)];
- if (t1[2] == t2[2])
- luaG_runerror(L, "attempt to compare two %s values", t1);
- else
- luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
- return 0;
-}
-
-
-static void addinfo (lua_State *L, const char *msg) {
- CallInfo *ci = L->ci;
- if (isLua(ci)) { /* is Lua code? */
- char buff[LUA_IDSIZE]; /* add file:line information */
- int line = currentline(L, ci);
- luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);
- luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
- }
-}
-
-
-void luaG_errormsg (lua_State *L) {
- if (L->errfunc != 0) { /* is there an error handling function? */
- StkId errfunc = restorestack(L, L->errfunc);
- if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
- setobjs2s(L, L->top, L->top - 1); /* move argument */
- setobjs2s(L, L->top - 1, errfunc); /* push function */
- incr_top(L);
- luaD_call(L, L->top - 2, 1); /* call it */
- }
- luaD_throw(L, LUA_ERRRUN);
-}
-
-
-void luaG_runerror (lua_State *L, const char *fmt, ...) {
- va_list argp;
- va_start(argp, fmt);
- addinfo(L, luaO_pushvfstring(L, fmt, argp));
- va_end(argp);
- luaG_errormsg(L);
-}
-
diff --git a/src/lua/src/ldebug.h b/src/lua/src/ldebug.h
deleted file mode 100644
index ba28a9724..000000000
--- a/src/lua/src/ldebug.h
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
-** $Id: ldebug.h,v 2.3.1.1 2007/12/27 13:02:25 roberto Exp $
-** Auxiliary functions from Debug Interface module
-** See Copyright Notice in lua.h
-*/
-
-#ifndef ldebug_h
-#define ldebug_h
-
-
-#include "lstate.h"
-
-
-#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1)
-
-#define getline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0)
-
-#define resethookcount(L) (L->hookcount = L->basehookcount)
-
-
-LUAI_FUNC void luaG_typeerror (lua_State *L, const TValue *o,
- const char *opname);
-LUAI_FUNC void luaG_concaterror (lua_State *L, StkId p1, StkId p2);
-LUAI_FUNC void luaG_aritherror (lua_State *L, const TValue *p1,
- const TValue *p2);
-LUAI_FUNC int luaG_ordererror (lua_State *L, const TValue *p1,
- const TValue *p2);
-LUAI_FUNC void luaG_runerror (lua_State *L, const char *fmt, ...);
-LUAI_FUNC void luaG_errormsg (lua_State *L);
-LUAI_FUNC int luaG_checkcode (const Proto *pt);
-LUAI_FUNC int luaG_checkopenop (Instruction i);
-
-#endif
diff --git a/src/lua/src/ldo.c b/src/lua/src/ldo.c
deleted file mode 100644
index 8de05f728..000000000
--- a/src/lua/src/ldo.c
+++ /dev/null
@@ -1,518 +0,0 @@
-/*
-** $Id: ldo.c,v 2.38.1.3 2008/01/18 22:31:22 roberto Exp $
-** Stack and Call structure of Lua
-** See Copyright Notice in lua.h
-*/
-
-
-#include <setjmp.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define ldo_c
-#define LUA_CORE
-
-#include "lua.h"
-
-#include "ldebug.h"
-#include "ldo.h"
-#include "lfunc.h"
-#include "lgc.h"
-#include "lmem.h"
-#include "lobject.h"
-#include "lopcodes.h"
-#include "lparser.h"
-#include "lstate.h"
-#include "lstring.h"
-#include "ltable.h"
-#include "ltm.h"
-#include "lundump.h"
-#include "lvm.h"
-#include "lzio.h"
-
-
-
-
-/*
-** {======================================================
-** Error-recovery functions
-** =======================================================
-*/
-
-
-/* chain list of long jump buffers */
-struct lua_longjmp {
- struct lua_longjmp *previous;
- luai_jmpbuf b;
- volatile int status; /* error code */
-};
-
-
-void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) {
- switch (errcode) {
- case LUA_ERRMEM: {
- setsvalue2s(L, oldtop, luaS_newliteral(L, MEMERRMSG));
- break;
- }
- case LUA_ERRERR: {
- setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));
- break;
- }
- case LUA_ERRSYNTAX:
- case LUA_ERRRUN: {
- setobjs2s(L, oldtop, L->top - 1); /* error message on current top */
- break;
- }
- }
- L->top = oldtop + 1;
-}
-
-
-static void restore_stack_limit (lua_State *L) {
- lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);
- if (L->size_ci > LUAI_MAXCALLS) { /* there was an overflow? */
- int inuse = cast_int(L->ci - L->base_ci);
- if (inuse + 1 < LUAI_MAXCALLS) /* can `undo' overflow? */
- luaD_reallocCI(L, LUAI_MAXCALLS);
- }
-}
-
-
-static void resetstack (lua_State *L, int status) {
- L->ci = L->base_ci;
- L->base = L->ci->base;
- luaF_close(L, L->base); /* close eventual pending closures */
- luaD_seterrorobj(L, status, L->base);
- L->nCcalls = L->baseCcalls;
- L->allowhook = 1;
- restore_stack_limit(L);
- L->errfunc = 0;
- L->errorJmp = NULL;
-}
-
-
-void luaD_throw (lua_State *L, int errcode) {
- if (L->errorJmp) {
- L->errorJmp->status = errcode;
- LUAI_THROW(L, L->errorJmp);
- }
- else {
- L->status = cast_byte(errcode);
- if (G(L)->panic) {
- resetstack(L, errcode);
- lua_unlock(L);
- G(L)->panic(L);
- }
- exit(EXIT_FAILURE);
- }
-}
-
-
-int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
- struct lua_longjmp lj;
- lj.status = 0;
- lj.previous = L->errorJmp; /* chain new error handler */
- L->errorJmp = &lj;
- LUAI_TRY(L, &lj,
- (*f)(L, ud);
- );
- L->errorJmp = lj.previous; /* restore old error handler */
- return lj.status;
-}
-
-/* }====================================================== */
-
-
-static void correctstack (lua_State *L, TValue *oldstack) {
- CallInfo *ci;
- GCObject *up;
- L->top = (L->top - oldstack) + L->stack;
- for (up = L->openupval; up != NULL; up = up->gch.next)
- gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack;
- for (ci = L->base_ci; ci <= L->ci; ci++) {
- ci->top = (ci->top - oldstack) + L->stack;
- ci->base = (ci->base - oldstack) + L->stack;
- ci->func = (ci->func - oldstack) + L->stack;
- }
- L->base = (L->base - oldstack) + L->stack;
-}
-
-
-void luaD_reallocstack (lua_State *L, int newsize) {
- TValue *oldstack = L->stack;
- int realsize = newsize + 1 + EXTRA_STACK;
- lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);
- luaM_reallocvector(L, L->stack, L->stacksize, realsize, TValue);
- L->stacksize = realsize;
- L->stack_last = L->stack+newsize;
- correctstack(L, oldstack);
-}
-
-
-void luaD_reallocCI (lua_State *L, int newsize) {
- CallInfo *oldci = L->base_ci;
- luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo);
- L->size_ci = newsize;
- L->ci = (L->ci - oldci) + L->base_ci;
- L->end_ci = L->base_ci + L->size_ci - 1;
-}
-
-
-void luaD_growstack (lua_State *L, int n) {
- if (n <= L->stacksize) /* double size is enough? */
- luaD_reallocstack(L, 2*L->stacksize);
- else
- luaD_reallocstack(L, L->stacksize + n);
-}
-
-
-static CallInfo *growCI (lua_State *L) {
- if (L->size_ci > LUAI_MAXCALLS) /* overflow while handling overflow? */
- luaD_throw(L, LUA_ERRERR);
- else {
- luaD_reallocCI(L, 2*L->size_ci);
- if (L->size_ci > LUAI_MAXCALLS)
- luaG_runerror(L, "stack overflow");
- }
- return ++L->ci;
-}
-
-
-void luaD_callhook (lua_State *L, int event, int line) {
- lua_Hook hook = L->hook;
- if (hook && L->allowhook) {
- ptrdiff_t top = savestack(L, L->top);
- ptrdiff_t ci_top = savestack(L, L->ci->top);
- lua_Debug ar;
- ar.event = event;
- ar.currentline = line;
- if (event == LUA_HOOKTAILRET)
- ar.i_ci = 0; /* tail call; no debug information about it */
- else
- ar.i_ci = cast_int(L->ci - L->base_ci);
- luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
- L->ci->top = L->top + LUA_MINSTACK;
- lua_assert(L->ci->top <= L->stack_last);
- L->allowhook = 0; /* cannot call hooks inside a hook */
- lua_unlock(L);
- (*hook)(L, &ar);
- lua_lock(L);
- lua_assert(!L->allowhook);
- L->allowhook = 1;
- L->ci->top = restorestack(L, ci_top);
- L->top = restorestack(L, top);
- }
-}
-
-
-static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
- int i;
- int nfixargs = p->numparams;
- Table *htab = NULL;
- StkId base, fixed;
- for (; actual < nfixargs; ++actual)
- setnilvalue(L->top++);
-#if defined(LUA_COMPAT_VARARG)
- if (p->is_vararg & VARARG_NEEDSARG) { /* compat. with old-style vararg? */
- int nvar = actual - nfixargs; /* number of extra arguments */
- lua_assert(p->is_vararg & VARARG_HASARG);
- luaC_checkGC(L);
- htab = luaH_new(L, nvar, 1); /* create `arg' table */
- for (i=0; i<nvar; i++) /* put extra arguments into `arg' table */
- setobj2n(L, luaH_setnum(L, htab, i+1), L->top - nvar + i);
- /* store counter in field `n' */
- setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar));
- }
-#endif
- /* move fixed parameters to final position */
- fixed = L->top - actual; /* first fixed argument */
- base = L->top; /* final position of first argument */
- for (i=0; i<nfixargs; i++) {
- setobjs2s(L, L->top++, fixed+i);
- setnilvalue(fixed+i);
- }
- /* add `arg' parameter */
- if (htab) {
- sethvalue(L, L->top++, htab);
- lua_assert(iswhite(obj2gco(htab)));
- }
- return base;
-}
-
-
-static StkId tryfuncTM (lua_State *L, StkId func) {
- const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
- StkId p;
- ptrdiff_t funcr = savestack(L, func);
- if (!ttisfunction(tm))
- luaG_typeerror(L, func, "call");
- /* Open a hole inside the stack at `func' */
- for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
- incr_top(L);
- func = restorestack(L, funcr); /* previous call may change stack */
- setobj2s(L, func, tm); /* tag method is the new function to be called */
- return func;
-}
-
-
-
-#define inc_ci(L) \
- ((L->ci == L->end_ci) ? growCI(L) : \
- (condhardstacktests(luaD_reallocCI(L, L->size_ci)), ++L->ci))
-
-
-int luaD_precall (lua_State *L, StkId func, int nresults) {
- LClosure *cl;
- ptrdiff_t funcr;
- if (!ttisfunction(func)) /* `func' is not a function? */
- func = tryfuncTM(L, func); /* check the `function' tag method */
- funcr = savestack(L, func);
- cl = &clvalue(func)->l;
- L->ci->savedpc = L->savedpc;
- if (!cl->isC) { /* Lua function? prepare its call */
- CallInfo *ci;
- StkId st, base;
- Proto *p = cl->p;
- luaD_checkstack(L, p->maxstacksize);
- func = restorestack(L, funcr);
- if (!p->is_vararg) { /* no varargs? */
- base = func + 1;
- if (L->top > base + p->numparams)
- L->top = base + p->numparams;
- }
- else { /* vararg function */
- int nargs = cast_int(L->top - func) - 1;
- base = adjust_varargs(L, p, nargs);
- func = restorestack(L, funcr); /* previous call may change the stack */
- }
- ci = inc_ci(L); /* now `enter' new function */
- ci->func = func;
- L->base = ci->base = base;
- ci->top = L->base + p->maxstacksize;
- lua_assert(ci->top <= L->stack_last);
- L->savedpc = p->code; /* starting point */
- ci->tailcalls = 0;
- ci->nresults = nresults;
- for (st = L->top; st < ci->top; st++)
- setnilvalue(st);
- L->top = ci->top;
- if (L->hookmask & LUA_MASKCALL) {
- L->savedpc++; /* hooks assume 'pc' is already incremented */
- luaD_callhook(L, LUA_HOOKCALL, -1);
- L->savedpc--; /* correct 'pc' */
- }
- return PCRLUA;
- }
- else { /* if is a C function, call it */
- CallInfo *ci;
- int n;
- luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
- ci = inc_ci(L); /* now `enter' new function */
- ci->func = restorestack(L, funcr);
- L->base = ci->base = ci->func + 1;
- ci->top = L->top + LUA_MINSTACK;
- lua_assert(ci->top <= L->stack_last);
- ci->nresults = nresults;
- if (L->hookmask & LUA_MASKCALL)
- luaD_callhook(L, LUA_HOOKCALL, -1);
- lua_unlock(L);
- n = (*curr_func(L)->c.f)(L); /* do the actual call */
- lua_lock(L);
- if (n < 0) /* yielding? */
- return PCRYIELD;
- else {
- luaD_poscall(L, L->top - n);
- return PCRC;
- }
- }
-}
-
-
-static StkId callrethooks (lua_State *L, StkId firstResult) {
- ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
- luaD_callhook(L, LUA_HOOKRET, -1);
- if (f_isLua(L->ci)) { /* Lua function? */
- while ((L->hookmask & LUA_MASKRET) && L->ci->tailcalls--) /* tail calls */
- luaD_callhook(L, LUA_HOOKTAILRET, -1);
- }
- return restorestack(L, fr);
-}
-
-
-int luaD_poscall (lua_State *L, StkId firstResult) {
- StkId res;
- int wanted, i;
- CallInfo *ci;
- if (L->hookmask & LUA_MASKRET)
- firstResult = callrethooks(L, firstResult);
- ci = L->ci--;
- res = ci->func; /* res == final position of 1st result */
- wanted = ci->nresults;
- L->base = (ci - 1)->base; /* restore base */
- L->savedpc = (ci - 1)->savedpc; /* restore savedpc */
- /* move results to correct place */
- for (i = wanted; i != 0 && firstResult < L->top; i--)
- setobjs2s(L, res++, firstResult++);
- while (i-- > 0)
- setnilvalue(res++);
- L->top = res;
- return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */
-}
-
-
-/*
-** Call a function (C or Lua). The function to be called is at *func.
-** The arguments are on the stack, right after the function.
-** When returns, all the results are on the stack, starting at the original
-** function position.
-*/
-void luaD_call (lua_State *L, StkId func, int nResults) {
- if (++L->nCcalls >= LUAI_MAXCCALLS) {
- if (L->nCcalls == LUAI_MAXCCALLS)
- luaG_runerror(L, "C stack overflow");
- else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
- luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
- }
- if (luaD_precall(L, func, nResults) == PCRLUA) /* is a Lua function? */
- luaV_execute(L, 1); /* call it */
- L->nCcalls--;
- luaC_checkGC(L);
-}
-
-
-static void resume (lua_State *L, void *ud) {
- StkId firstArg = cast(StkId, ud);
- CallInfo *ci = L->ci;
- if (L->status == 0) { /* start coroutine? */
- lua_assert(ci == L->base_ci && firstArg > L->base);
- if (luaD_precall(L, firstArg - 1, LUA_MULTRET) != PCRLUA)
- return;
- }
- else { /* resuming from previous yield */
- lua_assert(L->status == LUA_YIELD);
- L->status = 0;
- if (!f_isLua(ci)) { /* `common' yield? */
- /* finish interrupted execution of `OP_CALL' */
- lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL ||
- GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_TAILCALL);
- if (luaD_poscall(L, firstArg)) /* complete it... */
- L->top = L->ci->top; /* and correct top if not multiple results */
- }
- else /* yielded inside a hook: just continue its execution */
- L->base = L->ci->base;
- }
- luaV_execute(L, cast_int(L->ci - L->base_ci));
-}
-
-
-static int resume_error (lua_State *L, const char *msg) {
- L->top = L->ci->base;
- setsvalue2s(L, L->top, luaS_new(L, msg));
- incr_top(L);
- lua_unlock(L);
- return LUA_ERRRUN;
-}
-
-
-LUA_API int lua_resume (lua_State *L, int nargs) {
- int status;
- lua_lock(L);
- if (L->status != LUA_YIELD && (L->status != 0 || L->ci != L->base_ci))
- return resume_error(L, "cannot resume non-suspended coroutine");
- if (L->nCcalls >= LUAI_MAXCCALLS)
- return resume_error(L, "C stack overflow");
- luai_userstateresume(L, nargs);
- lua_assert(L->errfunc == 0);
- L->baseCcalls = ++L->nCcalls;
- status = luaD_rawrunprotected(L, resume, L->top - nargs);
- if (status != 0) { /* error? */
- L->status = cast_byte(status); /* mark thread as `dead' */
- luaD_seterrorobj(L, status, L->top);
- L->ci->top = L->top;
- }
- else {
- lua_assert(L->nCcalls == L->baseCcalls);
- status = L->status;
- }
- --L->nCcalls;
- lua_unlock(L);
- return status;
-}
-
-
-LUA_API int lua_yield (lua_State *L, int nresults) {
- luai_userstateyield(L, nresults);
- lua_lock(L);
- if (L->nCcalls > L->baseCcalls)
- luaG_runerror(L, "attempt to yield across metamethod/C-call boundary");
- L->base = L->top - nresults; /* protect stack slots below */
- L->status = LUA_YIELD;
- lua_unlock(L);
- return -1;
-}
-
-
-int luaD_pcall (lua_State *L, Pfunc func, void *u,
- ptrdiff_t old_top, ptrdiff_t ef) {
- int status;
- unsigned short oldnCcalls = L->nCcalls;
- ptrdiff_t old_ci = saveci(L, L->ci);
- lu_byte old_allowhooks = L->allowhook;
- ptrdiff_t old_errfunc = L->errfunc;
- L->errfunc = ef;
- status = luaD_rawrunprotected(L, func, u);
- if (status != 0) { /* an error occurred? */
- StkId oldtop = restorestack(L, old_top);
- luaF_close(L, oldtop); /* close eventual pending closures */
- luaD_seterrorobj(L, status, oldtop);
- L->nCcalls = oldnCcalls;
- L->ci = restoreci(L, old_ci);
- L->base = L->ci->base;
- L->savedpc = L->ci->savedpc;
- L->allowhook = old_allowhooks;
- restore_stack_limit(L);
- }
- L->errfunc = old_errfunc;
- return status;
-}
-
-
-
-/*
-** Execute a protected parser.
-*/
-struct SParser { /* data to `f_parser' */
- ZIO *z;
- Mbuffer buff; /* buffer to be used by the scanner */
- const char *name;
-};
-
-static void f_parser (lua_State *L, void *ud) {
- int i;
- Proto *tf;
- Closure *cl;
- struct SParser *p = cast(struct SParser *, ud);
- int c = luaZ_lookahead(p->z);
- luaC_checkGC(L);
- tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z,
- &p->buff, p->name);
- cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L)));
- cl->l.p = tf;
- for (i = 0; i < tf->nups; i++) /* initialize eventual upvalues */
- cl->l.upvals[i] = luaF_newupval(L);
- setclvalue(L, L->top, cl);
- incr_top(L);
-}
-
-
-int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) {
- struct SParser p;
- int status;
- p.z = z; p.name = name;
- luaZ_initbuffer(L, &p.buff);
- status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
- luaZ_freebuffer(L, &p.buff);
- return status;
-}
-
-
diff --git a/src/lua/src/ldo.h b/src/lua/src/ldo.h
deleted file mode 100644
index 98fddac59..000000000
--- a/src/lua/src/ldo.h
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
-** $Id: ldo.h,v 2.7.1.1 2007/12/27 13:02:25 roberto Exp $
-** Stack and Call structure of Lua
-** See Copyright Notice in lua.h
-*/
-
-#ifndef ldo_h
-#define ldo_h
-
-
-#include "lobject.h"
-#include "lstate.h"
-#include "lzio.h"
-
-
-#define luaD_checkstack(L,n) \
- if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TValue)) \
- luaD_growstack(L, n); \
- else condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1));
-
-
-#define incr_top(L) {luaD_checkstack(L,1); L->top++;}
-
-#define savestack(L,p) ((char *)(p) - (char *)L->stack)
-#define restorestack(L,n) ((TValue *)((char *)L->stack + (n)))
-
-#define saveci(L,p) ((char *)(p) - (char *)L->base_ci)
-#define restoreci(L,n) ((CallInfo *)((char *)L->base_ci + (n)))
-
-
-/* results from luaD_precall */
-#define PCRLUA 0 /* initiated a call to a Lua function */
-#define PCRC 1 /* did a call to a C function */
-#define PCRYIELD 2 /* C funtion yielded */
-
-
-/* type of protected functions, to be ran by `runprotected' */
-typedef void (*Pfunc) (lua_State *L, void *ud);
-
-LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name);
-LUAI_FUNC void luaD_callhook (lua_State *L, int event, int line);
-LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults);
-LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
-LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
- ptrdiff_t oldtop, ptrdiff_t ef);
-LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult);
-LUAI_FUNC void luaD_reallocCI (lua_State *L, int newsize);
-LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize);
-LUAI_FUNC void luaD_growstack (lua_State *L, int n);
-
-LUAI_FUNC void luaD_throw (lua_State *L, int errcode);
-LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
-
-LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop);
-
-#endif
-
diff --git a/src/lua/src/ldump.c b/src/lua/src/ldump.c
deleted file mode 100644
index c9d3d4870..000000000
--- a/src/lua/src/ldump.c
+++ /dev/null
@@ -1,164 +0,0 @@
-/*
-** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
-** save precompiled Lua chunks
-** See Copyright Notice in lua.h
-*/
-
-#include <stddef.h>
-
-#define ldump_c
-#define LUA_CORE
-
-#include "lua.h"
-
-#include "lobject.h"
-#include "lstate.h"
-#include "lundump.h"
-
-typedef struct {
- lua_State* L;
- lua_Writer writer;
- void* data;
- int strip;
- int status;
-} DumpState;
-
-#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D)
-#define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D)
-
-static void DumpBlock(const void* b, size_t size, DumpState* D)
-{
- if (D->status==0)
- {
- lua_unlock(D->L);
- D->status=(*D->writer)(D->L,b,size,D->data);
- lua_lock(D->L);
- }
-}
-
-static void DumpChar(int y, DumpState* D)
-{
- char x=(char)y;
- DumpVar(x,D);
-}
-
-static void DumpInt(int x, DumpState* D)
-{
- DumpVar(x,D);
-}
-
-static void DumpNumber(lua_Number x, DumpState* D)
-{
- DumpVar(x,D);
-}
-
-static void DumpVector(const void* b, int n, size_t size, DumpState* D)
-{
- DumpInt(n,D);
- DumpMem(b,n,size,D);
-}
-
-static void DumpString(const TString* s, DumpState* D)
-{
- if (s==NULL || getstr(s)==NULL)
- {
- size_t size=0;
- DumpVar(size,D);
- }
- else
- {
- size_t size=s->tsv.len+1; /* include trailing '\0' */
- DumpVar(size,D);
- DumpBlock(getstr(s),size,D);
- }
-}
-
-#define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
-
-static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
-
-static void DumpConstants(const Proto* f, DumpState* D)
-{
- int i,n=f->sizek;
- DumpInt(n,D);
- for (i=0; i<n; i++)
- {
- const TValue* o=&f->k[i];
- DumpChar(ttype(o),D);
- switch (ttype(o))
- {
- case LUA_TNIL:
- break;
- case LUA_TBOOLEAN:
- DumpChar(bvalue(o),D);
- break;
- case LUA_TNUMBER:
- DumpNumber(nvalue(o),D);
- break;
- case LUA_TSTRING:
- DumpString(rawtsvalue(o),D);
- break;
- default:
- lua_assert(0); /* cannot happen */
- break;
- }
- }
- n=f->sizep;
- DumpInt(n,D);
- for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
-}
-
-static void DumpDebug(const Proto* f, DumpState* D)
-{
- int i,n;
- n= (D->strip) ? 0 : f->sizelineinfo;
- DumpVector(f->lineinfo,n,sizeof(int),D);
- n= (D->strip) ? 0 : f->sizelocvars;
- DumpInt(n,D);
- for (i=0; i<n; i++)
- {
- DumpString(f->locvars[i].varname,D);
- DumpInt(f->locvars[i].startpc,D);
- DumpInt(f->locvars[i].endpc,D);
- }
- n= (D->strip) ? 0 : f->sizeupvalues;
- DumpInt(n,D);
- for (i=0; i<n; i++) DumpString(f->upvalues[i],D);
-}
-
-static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
-{
- DumpString((f->source==p || D->strip) ? NULL : f->source,D);
- DumpInt(f->linedefined,D);
- DumpInt(f->lastlinedefined,D);
- DumpChar(f->nups,D);
- DumpChar(f->numparams,D);
- DumpChar(f->is_vararg,D);
- DumpChar(f->maxstacksize,D);
- DumpCode(f,D);
- DumpConstants(f,D);
- DumpDebug(f,D);
-}
-
-static void DumpHeader(DumpState* D)
-{
- char h[LUAC_HEADERSIZE];
- luaU_header(h);
- DumpBlock(h,LUAC_HEADERSIZE,D);
-}
-
-/*
-** dump Lua function as precompiled chunk
-*/
-int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
-{
- DumpState D;
- D.L=L;
- D.writer=w;
- D.data=data;
- D.strip=strip;
- D.status=0;
- DumpHeader(&D);
- DumpFunction(f,NULL,&D);
- return D.status;
-}
diff --git a/src/lua/src/lfunc.c b/src/lua/src/lfunc.c
deleted file mode 100644
index 813e88f58..000000000
--- a/src/lua/src/lfunc.c
+++ /dev/null
@@ -1,174 +0,0 @@
-/*
-** $Id: lfunc.c,v 2.12.1.2 2007/12/28 14:58:43 roberto Exp $
-** Auxiliary functions to manipulate prototypes and closures
-** See Copyright Notice in lua.h
-*/
-
-
-#include <stddef.h>
-
-#define lfunc_c
-#define LUA_CORE
-
-#include "lua.h"
-
-#include "lfunc.h"
-#include "lgc.h"
-#include "lmem.h"
-#include "lobject.h"
-#include "lstate.h"
-
-
-
-Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) {
- Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems)));
- luaC_link(L, obj2gco(c), LUA_TFUNCTION);
- c->c.isC = 1;
- c->c.env = e;
- c->c.nupvalues = cast_byte(nelems);
- return c;
-}
-
-
-Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) {
- Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));
- luaC_link(L, obj2gco(c), LUA_TFUNCTION);
- c->l.isC = 0;
- c->l.env = e;
- c->l.nupvalues = cast_byte(nelems);
- while (nelems--) c->l.upvals[nelems] = NULL;
- return c;
-}
-
-
-UpVal *luaF_newupval (lua_State *L) {
- UpVal *uv = luaM_new(L, UpVal);
- luaC_link(L, obj2gco(uv), LUA_TUPVAL);
- uv->v = &uv->u.value;
- setnilvalue(uv->v);
- return uv;
-}
-
-
-UpVal *luaF_findupval (lua_State *L, StkId level) {
- global_State *g = G(L);
- GCObject **pp = &L->openupval;
- UpVal *p;
- UpVal *uv;
- while (*pp != NULL && (p = ngcotouv(*pp))->v >= level) {
- lua_assert(p->v != &p->u.value);
- if (p->v == level) { /* found a corresponding upvalue? */
- if (isdead(g, obj2gco(p))) /* is it dead? */
- changewhite(obj2gco(p)); /* ressurect it */
- return p;
- }
- pp = &p->next;
- }
- uv = luaM_new(L, UpVal); /* not found: create a new one */
- uv->tt = LUA_TUPVAL;
- uv->marked = luaC_white(g);
- uv->v = level; /* current value lives in the stack */
- uv->next = *pp; /* chain it in the proper position */
- *pp = obj2gco(uv);
- uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */
- uv->u.l.next = g->uvhead.u.l.next;
- uv->u.l.next->u.l.prev = uv;
- g->uvhead.u.l.next = uv;
- lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
- return uv;
-}
-
-
-static void unlinkupval (UpVal *uv) {
- lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
- uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */
- uv->u.l.prev->u.l.next = uv->u.l.next;
-}
-
-
-void luaF_freeupval (lua_State *L, UpVal *uv) {
- if (uv->v != &uv->u.value) /* is it open? */
- unlinkupval(uv); /* remove from open list */
- luaM_free(L, uv); /* free upvalue */
-}
-
-
-void luaF_close (lua_State *L, StkId level) {
- UpVal *uv;
- global_State *g = G(L);
- while (L->openupval != NULL && (uv = ngcotouv(L->openupval))->v >= level) {
- GCObject *o = obj2gco(uv);
- lua_assert(!isblack(o) && uv->v != &uv->u.value);
- L->openupval = uv->next; /* remove from `open' list */
- if (isdead(g, o))
- luaF_freeupval(L, uv); /* free upvalue */
- else {
- unlinkupval(uv);
- setobj(L, &uv->u.value, uv->v);
- uv->v = &uv->u.value; /* now current value lives here */
- luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */
- }
- }
-}
-
-
-Proto *luaF_newproto (lua_State *L) {
- Proto *f = luaM_new(L, Proto);
- luaC_link(L, obj2gco(f), LUA_TPROTO);
- f->k = NULL;
- f->sizek = 0;
- f->p = NULL;
- f->sizep = 0;
- f->code = NULL;
- f->sizecode = 0;
- f->sizelineinfo = 0;
- f->sizeupvalues = 0;
- f->nups = 0;
- f->upvalues = NULL;
- f->numparams = 0;
- f->is_vararg = 0;
- f->maxstacksize = 0;
- f->lineinfo = NULL;
- f->sizelocvars = 0;
- f->locvars = NULL;
- f->linedefined = 0;
- f->lastlinedefined = 0;
- f->source = NULL;
- return f;
-}
-
-
-void luaF_freeproto (lua_State *L, Proto *f) {
- luaM_freearray(L, f->code, f->sizecode, Instruction);
- luaM_freearray(L, f->p, f->sizep, Proto *);
- luaM_freearray(L, f->k, f->sizek, TValue);
- luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);
- luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
- luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *);
- luaM_free(L, f);
-}
-
-
-void luaF_freeclosure (lua_State *L, Closure *c) {
- int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) :
- sizeLclosure(c->l.nupvalues);
- luaM_freemem(L, c, size);
-}
-
-
-/*
-** Look for n-th local variable at line `line' in function `func'.
-** Returns NULL if not found.
-*/
-const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
- int i;
- for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
- if (pc < f->locvars[i].endpc) { /* is variable active? */
- local_number--;
- if (local_number == 0)
- return getstr(f->locvars[i].varname);
- }
- }
- return NULL; /* not found */
-}
-
diff --git a/src/lua/src/lfunc.h b/src/lua/src/lfunc.h
deleted file mode 100644
index a68cf5151..000000000
--- a/src/lua/src/lfunc.h
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
-** $Id: lfunc.h,v 2.4.1.1 2007/12/27 13:02:25 roberto Exp $
-** Auxiliary functions to manipulate prototypes and closures
-** See Copyright Notice in lua.h
-*/
-
-#ifndef lfunc_h
-#define lfunc_h
-
-
-#include "lobject.h"
-
-
-#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \
- cast(int, sizeof(TValue)*((n)-1)))
-
-#define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \
- cast(int, sizeof(TValue *)*((n)-1)))
-
-
-LUAI_FUNC Proto *luaF_newproto (lua_State *L);
-LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e);
-LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e);
-LUAI_FUNC UpVal *luaF_newupval (lua_State *L);
-LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);
-LUAI_FUNC void luaF_close (lua_State *L, StkId level);
-LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f);
-LUAI_FUNC void luaF_freeclosure (lua_State *L, Closure *c);
-LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv);
-LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number,
- int pc);
-
-
-#endif
diff --git a/src/lua/src/lgc.c b/src/lua/src/lgc.c
deleted file mode 100644
index d9e0b7829..000000000
--- a/src/lua/src/lgc.c
+++ /dev/null
@@ -1,711 +0,0 @@
-/*
-** $Id: lgc.c,v 2.38.1.1 2007/12/27 13:02:25 roberto Exp $
-** Garbage Collector
-** See Copyright Notice in lua.h
-*/
-
-#include <string.h>
-
-#define lgc_c
-#define LUA_CORE
-
-#include "lua.h"
-
-#include "ldebug.h"
-#include "ldo.h"
-#include "lfunc.h"
-#include "lgc.h"
-#include "lmem.h"
-#include "lobject.h"
-#include "lstate.h"
-#include "lstring.h"
-#include "ltable.h"
-#include "ltm.h"
-
-
-#define GCSTEPSIZE 1024u
-#define GCSWEEPMAX 40
-#define GCSWEEPCOST 10
-#define GCFINALIZECOST 100
-
-
-#define maskmarks cast_byte(~(bitmask(BLACKBIT)|WHITEBITS))
-
-#define makewhite(g,x) \
- ((x)->gch.marked = cast_byte(((x)->gch.marked & maskmarks) | luaC_white(g)))
-
-#define white2gray(x) reset2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)
-#define black2gray(x) resetbit((x)->gch.marked, BLACKBIT)
-
-#define stringmark(s) reset2bits((s)->tsv.marked, WHITE0BIT, WHITE1BIT)
-
-
-#define isfinalized(u) testbit((u)->marked, FINALIZEDBIT)
-#define markfinalized(u) l_setbit((u)->marked, FINALIZEDBIT)
-
-
-#define KEYWEAK bitmask(KEYWEAKBIT)
-#define VALUEWEAK bitmask(VALUEWEAKBIT)
-
-
-
-#define markvalue(g,o) { checkconsistency(o); \
- if (iscollectable(o) && iswhite(gcvalue(o))) reallymarkobject(g,gcvalue(o)); }
-
-#define markobject(g,t) { if (iswhite(obj2gco(t))) \
- reallymarkobject(g, obj2gco(t)); }
-
-
-#define setthreshold(g) (g->GCthreshold = (g->estimate/100) * g->gcpause)
-
-
-static void removeentry (Node *n) {
- lua_assert(ttisnil(gval(n)));
- if (iscollectable(gkey(n)))
- setttype(gkey(n), LUA_TDEADKEY); /* dead key; remove it */
-}
-
-
-static void reallymarkobject (global_State *g, GCObject *o) {
- lua_assert(iswhite(o) && !isdead(g, o));
- white2gray(o);
- switch (o->gch.tt) {
- case LUA_TSTRING: {
- return;
- }
- case LUA_TUSERDATA: {
- Table *mt = gco2u(o)->metatable;
- gray2black(o); /* udata are never gray */
- if (mt) markobject(g, mt);
- markobject(g, gco2u(o)->env);
- return;
- }
- case LUA_TUPVAL: {
- UpVal *uv = gco2uv(o);
- markvalue(g, uv->v);
- if (uv->v == &uv->u.value) /* closed? */
- gray2black(o); /* open upvalues are never black */
- return;
- }
- case LUA_TFUNCTION: {
- gco2cl(o)->c.gclist = g->gray;
- g->gray = o;
- break;
- }
- case LUA_TTABLE: {
- gco2h(o)->gclist = g->gray;
- g->gray = o;
- break;
- }
- case LUA_TTHREAD: {
- gco2th(o)->gclist = g->gray;
- g->gray = o;
- break;
- }
- case LUA_TPROTO: {
- gco2p(o)->gclist = g->gray;
- g->gray = o;
- break;
- }
- default: lua_assert(0);
- }
-}
-
-
-static void marktmu (global_State *g) {
- GCObject *u = g->tmudata;
- if (u) {
- do {
- u = u->gch.next;
- makewhite(g, u); /* may be marked, if left from previous GC */
- reallymarkobject(g, u);
- } while (u != g->tmudata);
- }
-}
-
-
-/* move `dead' udata that need finalization to list `tmudata' */
-size_t luaC_separateudata (lua_State *L, int all) {
- global_State *g = G(L);
- size_t deadmem = 0;
- GCObject **p = &g->mainthread->next;
- GCObject *curr;
- while ((curr = *p) != NULL) {
- if (!(iswhite(curr) || all) || isfinalized(gco2u(curr)))
- p = &curr->gch.next; /* don't bother with them */
- else if (fasttm(L, gco2u(curr)->metatable, TM_GC) == NULL) {
- markfinalized(gco2u(curr)); /* don't need finalization */
- p = &curr->gch.next;
- }
- else { /* must call its gc method */
- deadmem += sizeudata(gco2u(curr));
- markfinalized(gco2u(curr));
- *p = curr->gch.next;
- /* link `curr' at the end of `tmudata' list */
- if (g->tmudata == NULL) /* list is empty? */
- g->tmudata = curr->gch.next = curr; /* creates a circular list */
- else {
- curr->gch.next = g->tmudata->gch.next;
- g->tmudata->gch.next = curr;
- g->tmudata = curr;
- }
- }
- }
- return deadmem;
-}
-
-
-static int traversetable (global_State *g, Table *h) {
- int i;
- int weakkey = 0;
- int weakvalue = 0;
- const TValue *mode;
- if (h->metatable)
- markobject(g, h->metatable);
- mode = gfasttm(g, h->metatable, TM_MODE);
- if (mode && ttisstring(mode)) { /* is there a weak mode? */
- weakkey = (strchr(svalue(mode), 'k') != NULL);
- weakvalue = (strchr(svalue(mode), 'v') != NULL);
- if (weakkey || weakvalue) { /* is really weak? */
- h->marked &= ~(KEYWEAK | VALUEWEAK); /* clear bits */
- h->marked |= cast_byte((weakkey << KEYWEAKBIT) |
- (weakvalue << VALUEWEAKBIT));
- h->gclist = g->weak; /* must be cleared after GC, ... */
- g->weak = obj2gco(h); /* ... so put in the appropriate list */
- }
- }
- if (weakkey && weakvalue) return 1;
- if (!weakvalue) {
- i = h->sizearray;
- while (i--)
- markvalue(g, &h->array[i]);
- }
- i = sizenode(h);
- while (i--) {
- Node *n = gnode(h, i);
- lua_assert(ttype(gkey(n)) != LUA_TDEADKEY || ttisnil(gval(n)));
- if (ttisnil(gval(n)))
- removeentry(n); /* remove empty entries */
- else {
- lua_assert(!ttisnil(gkey(n)));
- if (!weakkey) markvalue(g, gkey(n));
- if (!weakvalue) markvalue(g, gval(n));
- }
- }
- return weakkey || weakvalue;
-}
-
-
-/*
-** All marks are conditional because a GC may happen while the
-** prototype is still being created
-*/
-static void traverseproto (global_State *g, Proto *f) {
- int i;
- if (f->source) stringmark(f->source);
- for (i=0; i<f->sizek; i++) /* mark literals */
- markvalue(g, &f->k[i]);
- for (i=0; i<f->sizeupvalues; i++) { /* mark upvalue names */
- if (f->upvalues[i])
- stringmark(f->upvalues[i]);
- }
- for (i=0; i<f->sizep; i++) { /* mark nested protos */
- if (f->p[i])
- markobject(g, f->p[i]);
- }
- for (i=0; i<f->sizelocvars; i++) { /* mark local-variable names */
- if (f->locvars[i].varname)
- stringmark(f->locvars[i].varname);
- }
-}
-
-
-
-static void traverseclosure (global_State *g, Closure *cl) {
- markobject(g, cl->c.env);
- if (cl->c.isC) {
- int i;
- for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */
- markvalue(g, &cl->c.upvalue[i]);
- }
- else {
- int i;
- lua_assert(cl->l.nupvalues == cl->l.p->nups);
- markobject(g, cl->l.p);
- for (i=0; i<cl->l.nupvalues; i++) /* mark its upvalues */
- markobject(g, cl->l.upvals[i]);
- }
-}
-
-
-static void checkstacksizes (lua_State *L, StkId max) {
- int ci_used = cast_int(L->ci - L->base_ci); /* number of `ci' in use */
- int s_used = cast_int(max - L->stack); /* part of stack in use */
- if (L->size_ci > LUAI_MAXCALLS) /* handling overflow? */
- return; /* do not touch the stacks */
- if (4*ci_used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci)
- luaD_reallocCI(L, L->size_ci/2); /* still big enough... */
- condhardstacktests(luaD_reallocCI(L, ci_used + 1));
- if (4*s_used < L->stacksize &&
- 2*(BASIC_STACK_SIZE+EXTRA_STACK) < L->stacksize)
- luaD_reallocstack(L, L->stacksize/2); /* still big enough... */
- condhardstacktests(luaD_reallocstack(L, s_used));
-}
-
-
-static void traversestack (global_State *g, lua_State *l) {
- StkId o, lim;
- CallInfo *ci;
- markvalue(g, gt(l));
- lim = l->top;
- for (ci = l->base_ci; ci <= l->ci; ci++) {
- lua_assert(ci->top <= l->stack_last);
- if (lim < ci->top) lim = ci->top;
- }
- for (o = l->stack; o < l->top; o++)
- markvalue(g, o);
- for (; o <= lim; o++)
- setnilvalue(o);
- checkstacksizes(l, lim);
-}
-
-
-/*
-** traverse one gray object, turning it to black.
-** Returns `quantity' traversed.
-*/
-static l_mem propagatemark (global_State *g) {
- GCObject *o = g->gray;
- lua_assert(isgray(o));
- gray2black(o);
- switch (o->gch.tt) {
- case LUA_TTABLE: {
- Table *h = gco2h(o);
- g->gray = h->gclist;
- if (traversetable(g, h)) /* table is weak? */
- black2gray(o); /* keep it gray */
- return sizeof(Table) + sizeof(TValue) * h->sizearray +
- sizeof(Node) * sizenode(h);
- }
- case LUA_TFUNCTION: {
- Closure *cl = gco2cl(o);
- g->gray = cl->c.gclist;
- traverseclosure(g, cl);
- return (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) :
- sizeLclosure(cl->l.nupvalues);
- }
- case LUA_TTHREAD: {
- lua_State *th = gco2th(o);
- g->gray = th->gclist;
- th->gclist = g->grayagain;
- g->grayagain = o;
- black2gray(o);
- traversestack(g, th);
- return sizeof(lua_State) + sizeof(TValue) * th->stacksize +
- sizeof(CallInfo) * th->size_ci;
- }
- case LUA_TPROTO: {
- Proto *p = gco2p(o);
- g->gray = p->gclist;
- traverseproto(g, p);
- return sizeof(Proto) + sizeof(Instruction) * p->sizecode +
- sizeof(Proto *) * p->sizep +
- sizeof(TValue) * p->sizek +
- sizeof(int) * p->sizelineinfo +
- sizeof(LocVar) * p->sizelocvars +
- sizeof(TString *) * p->sizeupvalues;
- }
- default: lua_assert(0); return 0;
- }
-}
-
-
-static size_t propagateall (global_State *g) {
- size_t m = 0;
- while (g->gray) m += propagatemark(g);
- return m;
-}
-
-
-/*
-** The next function tells whether a key or value can be cleared from
-** a weak table. Non-collectable objects are never removed from weak
-** tables. Strings behave as `values', so are never removed too. for
-** other objects: if really collected, cannot keep them; for userdata
-** being finalized, keep them in keys, but not in values
-*/
-static int iscleared (const TValue *o, int iskey) {
- if (!iscollectable(o)) return 0;
- if (ttisstring(o)) {
- stringmark(rawtsvalue(o)); /* strings are `values', so are never weak */
- return 0;
- }
- return iswhite(gcvalue(o)) ||
- (ttisuserdata(o) && (!iskey && isfinalized(uvalue(o))));
-}
-
-
-/*
-** clear collected entries from weaktables
-*/
-static void cleartable (GCObject *l) {
- while (l) {
- Table *h = gco2h(l);
- int i = h->sizearray;
- lua_assert(testbit(h->marked, VALUEWEAKBIT) ||
- testbit(h->marked, KEYWEAKBIT));
- if (testbit(h->marked, VALUEWEAKBIT)) {
- while (i--) {
- TValue *o = &h->array[i];
- if (iscleared(o, 0)) /* value was collected? */
- setnilvalue(o); /* remove value */
- }
- }
- i = sizenode(h);
- while (i--) {
- Node *n = gnode(h, i);
- if (!ttisnil(gval(n)) && /* non-empty entry? */
- (iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) {
- setnilvalue(gval(n)); /* remove value ... */
- removeentry(n); /* remove entry from table */
- }
- }
- l = h->gclist;
- }
-}
-
-
-static void freeobj (lua_State *L, GCObject *o) {
- switch (o->gch.tt) {
- case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break;
- case LUA_TFUNCTION: luaF_freeclosure(L, gco2cl(o)); break;
- case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break;
- case LUA_TTABLE: luaH_free(L, gco2h(o)); break;
- case LUA_TTHREAD: {
- lua_assert(gco2th(o) != L && gco2th(o) != G(L)->mainthread);
- luaE_freethread(L, gco2th(o));
- break;
- }
- case LUA_TSTRING: {
- G(L)->strt.nuse--;
- luaM_freemem(L, o, sizestring(gco2ts(o)));
- break;
- }
- case LUA_TUSERDATA: {
- luaM_freemem(L, o, sizeudata(gco2u(o)));
- break;
- }
- default: lua_assert(0);
- }
-}
-
-
-
-#define sweepwholelist(L,p) sweeplist(L,p,MAX_LUMEM)
-
-
-static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
- GCObject *curr;
- global_State *g = G(L);
- int deadmask = otherwhite(g);
- while ((curr = *p) != NULL && count-- > 0) {
- if (curr->gch.tt == LUA_TTHREAD) /* sweep open upvalues of each thread */
- sweepwholelist(L, &gco2th(curr)->openupval);
- if ((curr->gch.marked ^ WHITEBITS) & deadmask) { /* not dead? */
- lua_assert(!isdead(g, curr) || testbit(curr->gch.marked, FIXEDBIT));
- makewhite(g, curr); /* make it white (for next cycle) */
- p = &curr->gch.next;
- }
- else { /* must erase `curr' */
- lua_assert(isdead(g, curr) || deadmask == bitmask(SFIXEDBIT));
- *p = curr->gch.next;
- if (curr == g->rootgc) /* is the first element of the list? */
- g->rootgc = curr->gch.next; /* adjust first */
- freeobj(L, curr);
- }
- }
- return p;
-}
-
-
-static void checkSizes (lua_State *L) {
- global_State *g = G(L);
- /* check size of string hash */
- if (g->strt.nuse < cast(lu_int32, g->strt.size/4) &&
- g->strt.size > MINSTRTABSIZE*2)
- luaS_resize(L, g->strt.size/2); /* table is too big */
- /* check size of buffer */
- if (luaZ_sizebuffer(&g->buff) > LUA_MINBUFFER*2) { /* buffer too big? */
- size_t newsize = luaZ_sizebuffer(&g->buff) / 2;
- luaZ_resizebuffer(L, &g->buff, newsize);
- }
-}
-
-
-static void GCTM (lua_State *L) {
- global_State *g = G(L);
- GCObject *o = g->tmudata->gch.next; /* get first element */
- Udata *udata = rawgco2u(o);
- const TValue *tm;
- /* remove udata from `tmudata' */
- if (o == g->tmudata) /* last element? */
- g->tmudata = NULL;
- else
- g->tmudata->gch.next = udata->uv.next;
- udata->uv.next = g->mainthread->next; /* return it to `root' list */
- g->mainthread->next = o;
- makewhite(g, o);
- tm = fasttm(L, udata->uv.metatable, TM_GC);
- if (tm != NULL) {
- lu_byte oldah = L->allowhook;
- lu_mem oldt = g->GCthreshold;
- L->allowhook = 0; /* stop debug hooks during GC tag method */
- g->GCthreshold = 2*g->totalbytes; /* avoid GC steps */
- setobj2s(L, L->top, tm);
- setuvalue(L, L->top+1, udata);
- L->top += 2;
- luaD_call(L, L->top - 2, 0);
- L->allowhook = oldah; /* restore hooks */
- g->GCthreshold = oldt; /* restore threshold */
- }
-}
-
-
-/*
-** Call all GC tag methods
-*/
-void luaC_callGCTM (lua_State *L) {
- while (G(L)->tmudata)
- GCTM(L);
-}
-
-
-void luaC_freeall (lua_State *L) {
- global_State *g = G(L);
- int i;
- g->currentwhite = WHITEBITS | bitmask(SFIXEDBIT); /* mask to collect all elements */
- sweepwholelist(L, &g->rootgc);
- for (i = 0; i < g->strt.size; i++) /* free all string lists */
- sweepwholelist(L, &g->strt.hash[i]);
-}
-
-
-static void markmt (global_State *g) {
- int i;
- for (i=0; i<NUM_TAGS; i++)
- if (g->mt[i]) markobject(g, g->mt[i]);
-}
-
-
-/* mark root set */
-static void markroot (lua_State *L) {
- global_State *g = G(L);
- g->gray = NULL;
- g->grayagain = NULL;
- g->weak = NULL;
- markobject(g, g->mainthread);
- /* make global table be traversed before main stack */
- markvalue(g, gt(g->mainthread));
- markvalue(g, registry(L));
- markmt(g);
- g->gcstate = GCSpropagate;
-}
-
-
-static void remarkupvals (global_State *g) {
- UpVal *uv;
- for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) {
- lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
- if (isgray(obj2gco(uv)))
- markvalue(g, uv->v);
- }
-}
-
-
-static void atomic (lua_State *L) {
- global_State *g = G(L);
- size_t udsize; /* total size of userdata to be finalized */
- /* remark occasional upvalues of (maybe) dead threads */
- remarkupvals(g);
- /* traverse objects cautch by write barrier and by 'remarkupvals' */
- propagateall(g);
- /* remark weak tables */
- g->gray = g->weak;
- g->weak = NULL;
- lua_assert(!iswhite(obj2gco(g->mainthread)));
- markobject(g, L); /* mark running thread */
- markmt(g); /* mark basic metatables (again) */
- propagateall(g);
- /* remark gray again */
- g->gray = g->grayagain;
- g->grayagain = NULL;
- propagateall(g);
- udsize = luaC_separateudata(L, 0); /* separate userdata to be finalized */
- marktmu(g); /* mark `preserved' userdata */
- udsize += propagateall(g); /* remark, to propagate `preserveness' */
- cleartable(g->weak); /* remove collected objects from weak tables */
- /* flip current white */
- g->currentwhite = cast_byte(otherwhite(g));
- g->sweepstrgc = 0;
- g->sweepgc = &g->rootgc;
- g->gcstate = GCSsweepstring;
- g->estimate = g->totalbytes - udsize; /* first estimate */
-}
-
-
-static l_mem singlestep (lua_State *L) {
- global_State *g = G(L);
- /*lua_checkmemory(L);*/
- switch (g->gcstate) {
- case GCSpause: {
- markroot(L); /* start a new collection */
- return 0;
- }
- case GCSpropagate: {
- if (g->gray)
- return propagatemark(g);
- else { /* no more `gray' objects */
- atomic(L); /* finish mark phase */
- return 0;
- }
- }
- case GCSsweepstring: {
- lu_mem old = g->totalbytes;
- sweepwholelist(L, &g->strt.hash[g->sweepstrgc++]);
- if (g->sweepstrgc >= g->strt.size) /* nothing more to sweep? */
- g->gcstate = GCSsweep; /* end sweep-string phase */
- lua_assert(old >= g->totalbytes);
- g->estimate -= old - g->totalbytes;
- return GCSWEEPCOST;
- }
- case GCSsweep: {
- lu_mem old = g->totalbytes;
- g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);
- if (*g->sweepgc == NULL) { /* nothing more to sweep? */
- checkSizes(L);
- g->gcstate = GCSfinalize; /* end sweep phase */
- }
- lua_assert(old >= g->totalbytes);
- g->estimate -= old - g->totalbytes;
- return GCSWEEPMAX*GCSWEEPCOST;
- }
- case GCSfinalize: {
- if (g->tmudata) {
- GCTM(L);
- if (g->estimate > GCFINALIZECOST)
- g->estimate -= GCFINALIZECOST;
- return GCFINALIZECOST;
- }
- else {
- g->gcstate = GCSpause; /* end collection */
- g->gcdept = 0;
- return 0;
- }
- }
- default: lua_assert(0); return 0;
- }
-}
-
-
-void luaC_step (lua_State *L) {
- global_State *g = G(L);
- l_mem lim = (GCSTEPSIZE/100) * g->gcstepmul;
- if (lim == 0)
- lim = (MAX_LUMEM-1)/2; /* no limit */
- g->gcdept += g->totalbytes - g->GCthreshold;
- do {
- lim -= singlestep(L);
- if (g->gcstate == GCSpause)
- break;
- } while (lim > 0);
- if (g->gcstate != GCSpause) {
- if (g->gcdept < GCSTEPSIZE)
- g->GCthreshold = g->totalbytes + GCSTEPSIZE; /* - lim/g->gcstepmul;*/
- else {
- g->gcdept -= GCSTEPSIZE;
- g->GCthreshold = g->totalbytes;
- }
- }
- else {
- lua_assert(g->totalbytes >= g->estimate);
- setthreshold(g);
- }
-}
-
-
-void luaC_fullgc (lua_State *L) {
- global_State *g = G(L);
- if (g->gcstate <= GCSpropagate) {
- /* reset sweep marks to sweep all elements (returning them to white) */
- g->sweepstrgc = 0;
- g->sweepgc = &g->rootgc;
- /* reset other collector lists */
- g->gray = NULL;
- g->grayagain = NULL;
- g->weak = NULL;
- g->gcstate = GCSsweepstring;
- }
- lua_assert(g->gcstate != GCSpause && g->gcstate != GCSpropagate);
- /* finish any pending sweep phase */
- while (g->gcstate != GCSfinalize) {
- lua_assert(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep);
- singlestep(L);
- }
- markroot(L);
- while (g->gcstate != GCSpause) {
- singlestep(L);
- }
- setthreshold(g);
-}
-
-
-void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) {
- global_State *g = G(L);
- lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));
- lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
- lua_assert(ttype(&o->gch) != LUA_TTABLE);
- /* must keep invariant? */
- if (g->gcstate == GCSpropagate)
- reallymarkobject(g, v); /* restore invariant */
- else /* don't mind */
- makewhite(g, o); /* mark as white just to avoid other barriers */
-}
-
-
-void luaC_barrierback (lua_State *L, Table *t) {
- global_State *g = G(L);
- GCObject *o = obj2gco(t);
- lua_assert(isblack(o) && !isdead(g, o));
- lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
- black2gray(o); /* make table gray (again) */
- t->gclist = g->grayagain;
- g->grayagain = o;
-}
-
-
-void luaC_link (lua_State *L, GCObject *o, lu_byte tt) {
- global_State *g = G(L);
- o->gch.next = g->rootgc;
- g->rootgc = o;
- o->gch.marked = luaC_white(g);
- o->gch.tt = tt;
-}
-
-
-void luaC_linkupval (lua_State *L, UpVal *uv) {
- global_State *g = G(L);
- GCObject *o = obj2gco(uv);
- o->gch.next = g->rootgc; /* link upvalue into `rootgc' list */
- g->rootgc = o;
- if (isgray(o)) {
- if (g->gcstate == GCSpropagate) {
- gray2black(o); /* closed upvalues need barrier */
- luaC_barrier(L, uv, uv->v);
- }
- else { /* sweep phase: sweep it (turning it into white) */
- makewhite(g, o);
- lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
- }
- }
-}
-
diff --git a/src/lua/src/lgc.h b/src/lua/src/lgc.h
deleted file mode 100644
index 5a8dc605b..000000000
--- a/src/lua/src/lgc.h
+++ /dev/null
@@ -1,110 +0,0 @@
-/*
-** $Id: lgc.h,v 2.15.1.1 2007/12/27 13:02:25 roberto Exp $
-** Garbage Collector
-** See Copyright Notice in lua.h
-*/
-
-#ifndef lgc_h
-#define lgc_h
-
-
-#include "lobject.h"
-
-
-/*
-** Possible states of the Garbage Collector
-*/
-#define GCSpause 0
-#define GCSpropagate 1
-#define GCSsweepstring 2
-#define GCSsweep 3
-#define GCSfinalize 4
-
-
-/*
-** some userful bit tricks
-*/
-#define resetbits(x,m) ((x) &= cast(lu_byte, ~(m)))
-#define setbits(x,m) ((x) |= (m))
-#define testbits(x,m) ((x) & (m))
-#define bitmask(b) (1<<(b))
-#define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
-#define l_setbit(x,b) setbits(x, bitmask(b))
-#define resetbit(x,b) resetbits(x, bitmask(b))
-#define testbit(x,b) testbits(x, bitmask(b))
-#define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2)))
-#define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2)))
-#define test2bits(x,b1,b2) testbits(x, (bit2mask(b1, b2)))
-
-
-
-/*
-** Layout for bit use in `marked' field:
-** bit 0 - object is white (type 0)
-** bit 1 - object is white (type 1)
-** bit 2 - object is black
-** bit 3 - for userdata: has been finalized
-** bit 3 - for tables: has weak keys
-** bit 4 - for tables: has weak values
-** bit 5 - object is fixed (should not be collected)
-** bit 6 - object is "super" fixed (only the main thread)
-*/
-
-
-#define WHITE0BIT 0
-#define WHITE1BIT 1
-#define BLACKBIT 2
-#define FINALIZEDBIT 3
-#define KEYWEAKBIT 3
-#define VALUEWEAKBIT 4
-#define FIXEDBIT 5
-#define SFIXEDBIT 6
-#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
-
-
-#define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)
-#define isblack(x) testbit((x)->gch.marked, BLACKBIT)
-#define isgray(x) (!isblack(x) && !iswhite(x))
-
-#define otherwhite(g) (g->currentwhite ^ WHITEBITS)
-#define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS)
-
-#define changewhite(x) ((x)->gch.marked ^= WHITEBITS)
-#define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT)
-
-#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x)))
-
-#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
-
-
-#define luaC_checkGC(L) { \
- condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \
- if (G(L)->totalbytes >= G(L)->GCthreshold) \
- luaC_step(L); }
-
-
-#define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \
- luaC_barrierf(L,obj2gco(p),gcvalue(v)); }
-
-#define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \
- luaC_barrierback(L,t); }
-
-#define luaC_objbarrier(L,p,o) \
- { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \
- luaC_barrierf(L,obj2gco(p),obj2gco(o)); }
-
-#define luaC_objbarriert(L,t,o) \
- { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); }
-
-LUAI_FUNC size_t luaC_separateudata (lua_State *L, int all);
-LUAI_FUNC void luaC_callGCTM (lua_State *L);
-LUAI_FUNC void luaC_freeall (lua_State *L);
-LUAI_FUNC void luaC_step (lua_State *L);
-LUAI_FUNC void luaC_fullgc (lua_State *L);
-LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt);
-LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv);
-LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v);
-LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t);
-
-
-#endif
diff --git a/src/lua/src/linit.c b/src/lua/src/linit.c
deleted file mode 100644
index c1f90dfab..000000000
--- a/src/lua/src/linit.c
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
-** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $
-** Initialization of libraries for lua.c
-** See Copyright Notice in lua.h
-*/
-
-
-#define linit_c
-#define LUA_LIB
-
-#include "lua.h"
-
-#include "lualib.h"
-#include "lauxlib.h"
-
-
-static const luaL_Reg lualibs[] = {
- {"", luaopen_base},
- {LUA_LOADLIBNAME, luaopen_package},
- {LUA_TABLIBNAME, luaopen_table},
- {LUA_IOLIBNAME, luaopen_io},
- {LUA_OSLIBNAME, luaopen_os},
- {LUA_STRLIBNAME, luaopen_string},
- {LUA_MATHLIBNAME, luaopen_math},
- {LUA_DBLIBNAME, luaopen_debug},
- {NULL, NULL}
-};
-
-
-LUALIB_API void luaL_openlibs (lua_State *L) {
- const luaL_Reg *lib = lualibs;
- for (; lib->func; lib++) {
- lua_pushcfunction(L, lib->func);
- lua_pushstring(L, lib->name);
- lua_call(L, 1, 0);
- }
-}
-
diff --git a/src/lua/src/liolib.c b/src/lua/src/liolib.c
deleted file mode 100644
index e79ed1cb2..000000000
--- a/src/lua/src/liolib.c
+++ /dev/null
@@ -1,553 +0,0 @@
-/*
-** $Id: liolib.c,v 2.73.1.3 2008/01/18 17:47:43 roberto Exp $
-** Standard I/O (and system) library
-** See Copyright Notice in lua.h
-*/
-
-
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define liolib_c
-#define LUA_LIB
-
-#include "lua.h"
-
-#include "lauxlib.h"
-#include "lualib.h"
-
-
-
-#define IO_INPUT 1
-#define IO_OUTPUT 2
-
-
-static const char *const fnames[] = {"input", "output"};
-
-
-static int pushresult (lua_State *L, int i, const char *filename) {
- int en = errno; /* calls to Lua API may change this value */
- if (i) {
- lua_pushboolean(L, 1);
- return 1;
- }
- else {
- lua_pushnil(L);
- if (filename)
- lua_pushfstring(L, "%s: %s", filename, strerror(en));
- else
- lua_pushfstring(L, "%s", strerror(en));
- lua_pushinteger(L, en);
- return 3;
- }
-}
-
-
-static void fileerror (lua_State *L, int arg, const char *filename) {
- lua_pushfstring(L, "%s: %s", filename, strerror(errno));
- luaL_argerror(L, arg, lua_tostring(L, -1));
-}
-
-
-#define tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
-
-
-static int io_type (lua_State *L) {
- void *ud;
- luaL_checkany(L, 1);
- ud = lua_touserdata(L, 1);
- lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE);
- if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1))
- lua_pushnil(L); /* not a file */
- else if (*((FILE **)ud) == NULL)
- lua_pushliteral(L, "closed file");
- else
- lua_pushliteral(L, "file");
- return 1;
-}
-
-
-static FILE *tofile (lua_State *L) {
- FILE **f = tofilep(L);
- if (*f == NULL)
- luaL_error(L, "attempt to use a closed file");
- return *f;
-}
-
-
-
-/*
-** When creating file handles, always creates a `closed' file handle
-** before opening the actual file; so, if there is a memory error, the
-** file is not left opened.
-*/
-static FILE **newfile (lua_State *L) {
- FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
- *pf = NULL; /* file handle is currently `closed' */
- luaL_getmetatable(L, LUA_FILEHANDLE);
- lua_setmetatable(L, -2);
- return pf;
-}
-
-
-/*
-** function to (not) close the standard files stdin, stdout, and stderr
-*/
-static int io_noclose (lua_State *L) {
- lua_pushnil(L);
- lua_pushliteral(L, "cannot close standard file");
- return 2;
-}
-
-
-/*
-** function to close 'popen' files
-*/
-static int io_pclose (lua_State *L) {
- FILE **p = tofilep(L);
- int ok = lua_pclose(L, *p);
- *p = NULL;
- return pushresult(L, ok, NULL);
-}
-
-
-/*
-** function to close regular files
-*/
-static int io_fclose (lua_State *L) {
- FILE **p = tofilep(L);
- int ok = (fclose(*p) == 0);
- *p = NULL;
- return pushresult(L, ok, NULL);
-}
-
-
-static int aux_close (lua_State *L) {
- lua_getfenv(L, 1);
- lua_getfield(L, -1, "__close");
- return (lua_tocfunction(L, -1))(L);
-}
-
-
-static int io_close (lua_State *L) {
- if (lua_isnone(L, 1))
- lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT);
- tofile(L); /* make sure argument is a file */
- return aux_close(L);
-}
-
-
-static int io_gc (lua_State *L) {
- FILE *f = *tofilep(L);
- /* ignore closed files */
- if (f != NULL)
- aux_close(L);
- return 0;
-}
-
-
-static int io_tostring (lua_State *L) {
- FILE *f = *tofilep(L);
- if (f == NULL)
- lua_pushliteral(L, "file (closed)");
- else
- lua_pushfstring(L, "file (%p)", f);
- return 1;
-}
-
-
-static int io_open (lua_State *L) {
- const char *filename = luaL_checkstring(L, 1);
- const char *mode = luaL_optstring(L, 2, "r");
- FILE **pf = newfile(L);
- *pf = fopen(filename, mode);
- return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
-}
-
-
-/*
-** this function has a separated environment, which defines the
-** correct __close for 'popen' files
-*/
-static int io_popen (lua_State *L) {
- const char *filename = luaL_checkstring(L, 1);
- const char *mode = luaL_optstring(L, 2, "r");
- FILE **pf = newfile(L);
- *pf = lua_popen(L, filename, mode);
- return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
-}
-
-
-static int io_tmpfile (lua_State *L) {
- FILE **pf = newfile(L);
- *pf = tmpfile();
- return (*pf == NULL) ? pushresult(L, 0, NULL) : 1;
-}
-
-
-static FILE *getiofile (lua_State *L, int findex) {
- FILE *f;
- lua_rawgeti(L, LUA_ENVIRONINDEX, findex);
- f = *(FILE **)lua_touserdata(L, -1);
- if (f == NULL)
- luaL_error(L, "standard %s file is closed", fnames[findex - 1]);
- return f;
-}
-
-
-static int g_iofile (lua_State *L, int f, const char *mode) {
- if (!lua_isnoneornil(L, 1)) {
- const char *filename = lua_tostring(L, 1);
- if (filename) {
- FILE **pf = newfile(L);
- *pf = fopen(filename, mode);
- if (*pf == NULL)
- fileerror(L, 1, filename);
- }
- else {
- tofile(L); /* check that it's a valid file handle */
- lua_pushvalue(L, 1);
- }
- lua_rawseti(L, LUA_ENVIRONINDEX, f);
- }
- /* return current value */
- lua_rawgeti(L, LUA_ENVIRONINDEX, f);
- return 1;
-}
-
-
-static int io_input (lua_State *L) {
- return g_iofile(L, IO_INPUT, "r");
-}
-
-
-static int io_output (lua_State *L) {
- return g_iofile(L, IO_OUTPUT, "w");
-}
-
-
-static int io_readline (lua_State *L);
-
-
-static void aux_lines (lua_State *L, int idx, int toclose) {
- lua_pushvalue(L, idx);
- lua_pushboolean(L, toclose); /* close/not close file when finished */
- lua_pushcclosure(L, io_readline, 2);
-}
-
-
-static int f_lines (lua_State *L) {
- tofile(L); /* check that it's a valid file handle */
- aux_lines(L, 1, 0);
- return 1;
-}
-
-
-static int io_lines (lua_State *L) {
- if (lua_isnoneornil(L, 1)) { /* no arguments? */
- /* will iterate over default input */
- lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT);
- return f_lines(L);
- }
- else {
- const char *filename = luaL_checkstring(L, 1);
- FILE **pf = newfile(L);
- *pf = fopen(filename, "r");
- if (*pf == NULL)
- fileerror(L, 1, filename);
- aux_lines(L, lua_gettop(L), 1);
- return 1;
- }
-}
-
-
-/*
-** {======================================================
-** READ
-** =======================================================
-*/
-
-
-static int read_number (lua_State *L, FILE *f) {
- lua_Number d;
- if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
- lua_pushnumber(L, d);
- return 1;
- }
- else return 0; /* read fails */
-}
-
-
-static int test_eof (lua_State *L, FILE *f) {
- int c = getc(f);
- ungetc(c, f);
- lua_pushlstring(L, NULL, 0);
- return (c != EOF);
-}
-
-
-static int read_line (lua_State *L, FILE *f) {
- luaL_Buffer b;
- luaL_buffinit(L, &b);
- for (;;) {
- size_t l;
- char *p = luaL_prepbuffer(&b);
- if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
- luaL_pushresult(&b); /* close buffer */
- return (lua_objlen(L, -1) > 0); /* check whether read something */
- }
- l = strlen(p);
- if (l == 0 || p[l-1] != '\n')
- luaL_addsize(&b, l);
- else {
- luaL_addsize(&b, l - 1); /* do not include `eol' */
- luaL_pushresult(&b); /* close buffer */
- return 1; /* read at least an `eol' */
- }
- }
-}
-
-
-static int read_chars (lua_State *L, FILE *f, size_t n) {
- size_t rlen; /* how much to read */
- size_t nr; /* number of chars actually read */
- luaL_Buffer b;
- luaL_buffinit(L, &b);
- rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
- do {
- char *p = luaL_prepbuffer(&b);
- if (rlen > n) rlen = n; /* cannot read more than asked */
- nr = fread(p, sizeof(char), rlen, f);
- luaL_addsize(&b, nr);
- n -= nr; /* still have to read `n' chars */
- } while (n > 0 && nr == rlen); /* until end of count or eof */
- luaL_pushresult(&b); /* close buffer */
- return (n == 0 || lua_objlen(L, -1) > 0);
-}
-
-
-static int g_read (lua_State *L, FILE *f, int first) {
- int nargs = lua_gettop(L) - 1;
- int success;
- int n;
- clearerr(f);
- if (nargs == 0) { /* no arguments? */
- success = read_line(L, f);
- n = first+1; /* to return 1 result */
- }
- else { /* ensure stack space for all results and for auxlib's buffer */
- luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
- success = 1;
- for (n = first; nargs-- && success; n++) {
- if (lua_type(L, n) == LUA_TNUMBER) {
- size_t l = (size_t)lua_tointeger(L, n);
- success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
- }
- else {
- const char *p = lua_tostring(L, n);
- luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
- switch (p[1]) {
- case 'n': /* number */
- success = read_number(L, f);
- break;
- case 'l': /* line */
- success = read_line(L, f);
- break;
- case 'a': /* file */
- read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
- success = 1; /* always success */
- break;
- default:
- return luaL_argerror(L, n, "invalid format");
- }
- }
- }
- }
- if (ferror(f))
- return pushresult(L, 0, NULL);
- if (!success) {
- lua_pop(L, 1); /* remove last result */
- lua_pushnil(L); /* push nil instead */
- }
- return n - first;
-}
-
-
-static int io_read (lua_State *L) {
- return g_read(L, getiofile(L, IO_INPUT), 1);
-}
-
-
-static int f_read (lua_State *L) {
- return g_read(L, tofile(L), 2);
-}
-
-
-static int io_readline (lua_State *L) {
- FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1));
- int sucess;
- if (f == NULL) /* file is already closed? */
- luaL_error(L, "file is already closed");
- sucess = read_line(L, f);
- if (ferror(f))
- return luaL_error(L, "%s", strerror(errno));
- if (sucess) return 1;
- else { /* EOF */
- if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */
- lua_settop(L, 0);
- lua_pushvalue(L, lua_upvalueindex(1));
- aux_close(L); /* close it */
- }
- return 0;
- }
-}
-
-/* }====================================================== */
-
-
-static int g_write (lua_State *L, FILE *f, int arg) {
- int nargs = lua_gettop(L) - 1;
- int status = 1;
- for (; nargs--; arg++) {
- if (lua_type(L, arg) == LUA_TNUMBER) {
- /* optimization: could be done exactly as for strings */
- status = status &&
- fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
- }
- else {
- size_t l;
- const char *s = luaL_checklstring(L, arg, &l);
- status = status && (fwrite(s, sizeof(char), l, f) == l);
- }
- }
- return pushresult(L, status, NULL);
-}
-
-
-static int io_write (lua_State *L) {
- return g_write(L, getiofile(L, IO_OUTPUT), 1);
-}
-
-
-static int f_write (lua_State *L) {
- return g_write(L, tofile(L), 2);
-}
-
-
-static int f_seek (lua_State *L) {
- static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
- static const char *const modenames[] = {"set", "cur", "end", NULL};
- FILE *f = tofile(L);
- int op = luaL_checkoption(L, 2, "cur", modenames);
- long offset = luaL_optlong(L, 3, 0);
- op = fseek(f, offset, mode[op]);
- if (op)
- return pushresult(L, 0, NULL); /* error */
- else {
- lua_pushinteger(L, ftell(f));
- return 1;
- }
-}
-
-
-static int f_setvbuf (lua_State *L) {
- static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
- static const char *const modenames[] = {"no", "full", "line", NULL};
- FILE *f = tofile(L);
- int op = luaL_checkoption(L, 2, NULL, modenames);
- lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
- int res = setvbuf(f, NULL, mode[op], sz);
- return pushresult(L, res == 0, NULL);
-}
-
-
-
-static int io_flush (lua_State *L) {
- return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
-}
-
-
-static int f_flush (lua_State *L) {
- return pushresult(L, fflush(tofile(L)) == 0, NULL);
-}
-
-
-static const luaL_Reg iolib[] = {
- {"close", io_close},
- {"flush", io_flush},
- {"input", io_input},
- {"lines", io_lines},
- {"open", io_open},
- {"output", io_output},
- {"popen", io_popen},
- {"read", io_read},
- {"tmpfile", io_tmpfile},
- {"type", io_type},
- {"write", io_write},
- {NULL, NULL}
-};
-
-
-static const luaL_Reg flib[] = {
- {"close", io_close},
- {"flush", f_flush},
- {"lines", f_lines},
- {"read", f_read},
- {"seek", f_seek},
- {"setvbuf", f_setvbuf},
- {"write", f_write},
- {"__gc", io_gc},
- {"__tostring", io_tostring},
- {NULL, NULL}
-};
-
-
-static void createmeta (lua_State *L) {
- luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
- lua_pushvalue(L, -1); /* push metatable */
- lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
- luaL_register(L, NULL, flib); /* file methods */
-}
-
-
-static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) {
- *newfile(L) = f;
- if (k > 0) {
- lua_pushvalue(L, -1);
- lua_rawseti(L, LUA_ENVIRONINDEX, k);
- }
- lua_pushvalue(L, -2); /* copy environment */
- lua_setfenv(L, -2); /* set it */
- lua_setfield(L, -3, fname);
-}
-
-
-static void newfenv (lua_State *L, lua_CFunction cls) {
- lua_createtable(L, 0, 1);
- lua_pushcfunction(L, cls);
- lua_setfield(L, -2, "__close");
-}
-
-
-LUALIB_API int luaopen_io (lua_State *L) {
- createmeta(L);
- /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */
- newfenv(L, io_fclose);
- lua_replace(L, LUA_ENVIRONINDEX);
- /* open library */
- luaL_register(L, LUA_IOLIBNAME, iolib);
- /* create (and set) default files */
- newfenv(L, io_noclose); /* close function for default files */
- createstdfile(L, stdin, IO_INPUT, "stdin");
- createstdfile(L, stdout, IO_OUTPUT, "stdout");
- createstdfile(L, stderr, 0, "stderr");
- lua_pop(L, 1); /* pop environment for default files */
- lua_getfield(L, -1, "popen");
- newfenv(L, io_pclose); /* create environment for 'popen' */
- lua_setfenv(L, -2); /* set fenv for 'popen' */
- lua_pop(L, 1); /* pop 'popen' */
- return 1;
-}
-
diff --git a/src/lua/src/llex.c b/src/lua/src/llex.c
deleted file mode 100644
index 6dc319358..000000000
--- a/src/lua/src/llex.c
+++ /dev/null
@@ -1,461 +0,0 @@
-/*
-** $Id: llex.c,v 2.20.1.1 2007/12/27 13:02:25 roberto Exp $
-** Lexical Analyzer
-** See Copyright Notice in lua.h
-*/
-
-
-#include <ctype.h>
-#include <locale.h>
-#include <string.h>
-
-#define llex_c
-#define LUA_CORE
-
-#include "lua.h"
-
-#include "ldo.h"
-#include "llex.h"
-#include "lobject.h"
-#include "lparser.h"
-#include "lstate.h"
-#include "lstring.h"
-#include "ltable.h"
-#include "lzio.h"
-
-
-
-#define next(ls) (ls->current = zgetc(ls->z))
-
-
-
-
-#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
-
-
-/* ORDER RESERVED */
-const char *const luaX_tokens [] = {
- "and", "break", "do", "else", "elseif",
- "end", "false", "for", "function", "if",
- "in", "local", "nil", "not", "or", "repeat",
- "return", "then", "true", "until", "while",
- "..", "...", "==", ">=", "<=", "~=",
- "<number>", "<name>", "<string>", "<eof>",
- NULL
-};
-
-
-#define save_and_next(ls) (save(ls, ls->current), next(ls))
-
-
-static void save (LexState *ls, int c) {
- Mbuffer *b = ls->buff;
- if (b->n + 1 > b->buffsize) {
- size_t newsize;
- if (b->buffsize >= MAX_SIZET/2)
- luaX_lexerror(ls, "lexical element too long", 0);
- newsize = b->buffsize * 2;
- luaZ_resizebuffer(ls->L, b, newsize);
- }
- b->buffer[b->n++] = cast(char, c);
-}
-
-
-void luaX_init (lua_State *L) {
- int i;
- for (i=0; i<NUM_RESERVED; i++) {
- TString *ts = luaS_new(L, luaX_tokens[i]);
- luaS_fix(ts); /* reserved words are never collected */
- lua_assert(strlen(luaX_tokens[i])+1 <= TOKEN_LEN);
- ts->tsv.reserved = cast_byte(i+1); /* reserved word */
- }
-}
-
-
-#define MAXSRC 80
-
-
-const char *luaX_token2str (LexState *ls, int token) {
- if (token < FIRST_RESERVED) {
- lua_assert(token == cast(unsigned char, token));
- return (iscntrl(token)) ? luaO_pushfstring(ls->L, "char(%d)", token) :
- luaO_pushfstring(ls->L, "%c", token);
- }
- else
- return luaX_tokens[token-FIRST_RESERVED];
-}
-
-
-static const char *txtToken (LexState *ls, int token) {
- switch (token) {
- case TK_NAME:
- case TK_STRING:
- case TK_NUMBER:
- save(ls, '\0');
- return luaZ_buffer(ls->buff);
- default:
- return luaX_token2str(ls, token);
- }
-}
-
-
-void luaX_lexerror (LexState *ls, const char *msg, int token) {
- char buff[MAXSRC];
- luaO_chunkid(buff, getstr(ls->source), MAXSRC);
- msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg);
- if (token)
- luaO_pushfstring(ls->L, "%s near " LUA_QS, msg, txtToken(ls, token));
- luaD_throw(ls->L, LUA_ERRSYNTAX);
-}
-
-
-void luaX_syntaxerror (LexState *ls, const char *msg) {
- luaX_lexerror(ls, msg, ls->t.token);
-}
-
-
-TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
- lua_State *L = ls->L;
- TString *ts = luaS_newlstr(L, str, l);
- TValue *o = luaH_setstr(L, ls->fs->h, ts); /* entry for `str' */
- if (ttisnil(o))
- setbvalue(o, 1); /* make sure `str' will not be collected */
- return ts;
-}
-
-
-static void inclinenumber (LexState *ls) {
- int old = ls->current;
- lua_assert(currIsNewline(ls));
- next(ls); /* skip `\n' or `\r' */
- if (currIsNewline(ls) && ls->current != old)
- next(ls); /* skip `\n\r' or `\r\n' */
- if (++ls->linenumber >= MAX_INT)
- luaX_syntaxerror(ls, "chunk has too many lines");
-}
-
-
-void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) {
- ls->decpoint = '.';
- ls->L = L;
- ls->lookahead.token = TK_EOS; /* no look-ahead token */
- ls->z = z;
- ls->fs = NULL;
- ls->linenumber = 1;
- ls->lastline = 1;
- ls->source = source;
- luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
- next(ls); /* read first char */
-}
-
-
-
-/*
-** =======================================================
-** LEXICAL ANALYZER
-** =======================================================
-*/
-
-
-
-static int check_next (LexState *ls, const char *set) {
- if (!strchr(set, ls->current))
- return 0;
- save_and_next(ls);
- return 1;
-}
-
-
-static void buffreplace (LexState *ls, char from, char to) {
- size_t n = luaZ_bufflen(ls->buff);
- char *p = luaZ_buffer(ls->buff);
- while (n--)
- if (p[n] == from) p[n] = to;
-}
-
-
-static void trydecpoint (LexState *ls, SemInfo *seminfo) {
- /* format error: try to update decimal point separator */
- struct lconv *cv = localeconv();
- char old = ls->decpoint;
- ls->decpoint = (cv ? cv->decimal_point[0] : '.');
- buffreplace(ls, old, ls->decpoint); /* try updated decimal separator */
- if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) {
- /* format error with correct decimal point: no more options */
- buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
- luaX_lexerror(ls, "malformed number", TK_NUMBER);
- }
-}
-
-
-/* LUA_NUMBER */
-static void read_numeral (LexState *ls, SemInfo *seminfo) {
- lua_assert(isdigit(ls->current));
- do {
- save_and_next(ls);
- } while (isdigit(ls->current) || ls->current == '.');
- if (check_next(ls, "Ee")) /* `E'? */
- check_next(ls, "+-"); /* optional exponent sign */
- while (isalnum(ls->current) || ls->current == '_')
- save_and_next(ls);
- save(ls, '\0');
- buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
- if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */
- trydecpoint(ls, seminfo); /* try to update decimal point separator */
-}
-
-
-static int skip_sep (LexState *ls) {
- int count = 0;
- int s = ls->current;
- lua_assert(s == '[' || s == ']');
- save_and_next(ls);
- while (ls->current == '=') {
- save_and_next(ls);
- count++;
- }
- return (ls->current == s) ? count : (-count) - 1;
-}
-
-
-static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
- int cont = 0;
- (void)(cont); /* avoid warnings when `cont' is not used */
- save_and_next(ls); /* skip 2nd `[' */
- if (currIsNewline(ls)) /* string starts with a newline? */
- inclinenumber(ls); /* skip it */
- for (;;) {
- switch (ls->current) {
- case EOZ:
- luaX_lexerror(ls, (seminfo) ? "unfinished long string" :
- "unfinished long comment", TK_EOS);
- break; /* to avoid warnings */
-#if defined(LUA_COMPAT_LSTR)
- case '[': {
- if (skip_sep(ls) == sep) {
- save_and_next(ls); /* skip 2nd `[' */
- cont++;
-#if LUA_COMPAT_LSTR == 1
- if (sep == 0)
- luaX_lexerror(ls, "nesting of [[...]] is deprecated", '[');
-#endif
- }
- break;
- }
-#endif
- case ']': {
- if (skip_sep(ls) == sep) {
- save_and_next(ls); /* skip 2nd `]' */
-#if defined(LUA_COMPAT_LSTR) && LUA_COMPAT_LSTR == 2
- cont--;
- if (sep == 0 && cont >= 0) break;
-#endif
- goto endloop;
- }
- break;
- }
- case '\n':
- case '\r': {
- save(ls, '\n');
- inclinenumber(ls);
- if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */
- break;
- }
- default: {
- if (seminfo) save_and_next(ls);
- else next(ls);
- }
- }
- } endloop:
- if (seminfo)
- seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep),
- luaZ_bufflen(ls->buff) - 2*(2 + sep));
-}
-
-
-static void read_string (LexState *ls, int del, SemInfo *seminfo) {
- save_and_next(ls);
- while (ls->current != del) {
- switch (ls->current) {
- case EOZ:
- luaX_lexerror(ls, "unfinished string", TK_EOS);
- continue; /* to avoid warnings */
- case '\n':
- case '\r':
- luaX_lexerror(ls, "unfinished string", TK_STRING);
- continue; /* to avoid warnings */
- case '\\': {
- int c;
- next(ls); /* do not save the `\' */
- switch (ls->current) {
- case 'a': c = '\a'; break;
- case 'b': c = '\b'; break;
- case 'f': c = '\f'; break;
- case 'n': c = '\n'; break;
- case 'r': c = '\r'; break;
- case 't': c = '\t'; break;
- case 'v': c = '\v'; break;
- case '\n': /* go through */
- case '\r': save(ls, '\n'); inclinenumber(ls); continue;
- case EOZ: continue; /* will raise an error next loop */
- default: {
- if (!isdigit(ls->current))
- save_and_next(ls); /* handles \\, \", \', and \? */
- else { /* \xxx */
- int i = 0;
- c = 0;
- do {
- c = 10*c + (ls->current-'0');
- next(ls);
- } while (++i<3 && isdigit(ls->current));
- if (c > UCHAR_MAX)
- luaX_lexerror(ls, "escape sequence too large", TK_STRING);
- save(ls, c);
- }
- continue;
- }
- }
- save(ls, c);
- next(ls);
- continue;
- }
- default:
- save_and_next(ls);
- }
- }
- save_and_next(ls); /* skip delimiter */
- seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1,
- luaZ_bufflen(ls->buff) - 2);
-}
-
-
-static int llex (LexState *ls, SemInfo *seminfo) {
- luaZ_resetbuffer(ls->buff);
- for (;;) {
- switch (ls->current) {
- case '\n':
- case '\r': {
- inclinenumber(ls);
- continue;
- }
- case '-': {
- next(ls);
- if (ls->current != '-') return '-';
- /* else is a comment */
- next(ls);
- if (ls->current == '[') {
- int sep = skip_sep(ls);
- luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */
- if (sep >= 0) {
- read_long_string(ls, NULL, sep); /* long comment */
- luaZ_resetbuffer(ls->buff);
- continue;
- }
- }
- /* else short comment */
- while (!currIsNewline(ls) && ls->current != EOZ)
- next(ls);
- continue;
- }
- case '[': {
- int sep = skip_sep(ls);
- if (sep >= 0) {
- read_long_string(ls, seminfo, sep);
- return TK_STRING;
- }
- else if (sep == -1) return '[';
- else luaX_lexerror(ls, "invalid long string delimiter", TK_STRING);
- }
- case '=': {
- next(ls);
- if (ls->current != '=') return '=';
- else { next(ls); return TK_EQ; }
- }
- case '<': {
- next(ls);
- if (ls->current != '=') return '<';
- else { next(ls); return TK_LE; }
- }
- case '>': {
- next(ls);
- if (ls->current != '=') return '>';
- else { next(ls); return TK_GE; }
- }
- case '~': {
- next(ls);
- if (ls->current != '=') return '~';
- else { next(ls); return TK_NE; }
- }
- case '"':
- case '\'': {
- read_string(ls, ls->current, seminfo);
- return TK_STRING;
- }
- case '.': {
- save_and_next(ls);
- if (check_next(ls, ".")) {
- if (check_next(ls, "."))
- return TK_DOTS; /* ... */
- else return TK_CONCAT; /* .. */
- }
- else if (!isdigit(ls->current)) return '.';
- else {
- read_numeral(ls, seminfo);
- return TK_NUMBER;
- }
- }
- case EOZ: {
- return TK_EOS;
- }
- default: {
- if (isspace(ls->current)) {
- lua_assert(!currIsNewline(ls));
- next(ls);
- continue;
- }
- else if (isdigit(ls->current)) {
- read_numeral(ls, seminfo);
- return TK_NUMBER;
- }
- else if (isalpha(ls->current) || ls->current == '_') {
- /* identifier or reserved word */
- TString *ts;
- do {
- save_and_next(ls);
- } while (isalnum(ls->current) || ls->current == '_');
- ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
- luaZ_bufflen(ls->buff));
- if (ts->tsv.reserved > 0) /* reserved word? */
- return ts->tsv.reserved - 1 + FIRST_RESERVED;
- else {
- seminfo->ts = ts;
- return TK_NAME;
- }
- }
- else {
- int c = ls->current;
- next(ls);
- return c; /* single-char tokens (+ - / ...) */
- }
- }
- }
- }
-}
-
-
-void luaX_next (LexState *ls) {
- ls->lastline = ls->linenumber;
- if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
- ls->t = ls->lookahead; /* use this one */
- ls->lookahead.token = TK_EOS; /* and discharge it */
- }
- else
- ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */
-}
-
-
-void luaX_lookahead (LexState *ls) {
- lua_assert(ls->lookahead.token == TK_EOS);
- ls->lookahead.token = llex(ls, &ls->lookahead.seminfo);
-}
-
diff --git a/src/lua/src/llex.h b/src/lua/src/llex.h
deleted file mode 100644
index a9201cee4..000000000
--- a/src/lua/src/llex.h
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
-** $Id: llex.h,v 1.58.1.1 2007/12/27 13:02:25 roberto Exp $
-** Lexical Analyzer
-** See Copyright Notice in lua.h
-*/
-
-#ifndef llex_h
-#define llex_h
-
-#include "lobject.h"
-#include "lzio.h"
-
-
-#define FIRST_RESERVED 257
-
-/* maximum length of a reserved word */
-#define TOKEN_LEN (sizeof("function")/sizeof(char))
-
-
-/*
-* WARNING: if you change the order of this enumeration,
-* grep "ORDER RESERVED"
-*/
-enum RESERVED {
- /* terminal symbols denoted by reserved words */
- TK_AND = FIRST_RESERVED, TK_BREAK,
- TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION,
- TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,
- TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
- /* other terminal symbols */
- TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER,
- TK_NAME, TK_STRING, TK_EOS
-};
-
-/* number of reserved words */
-#define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1))
-
-
-/* array with token `names' */
-LUAI_DATA const char *const luaX_tokens [];
-
-
-typedef union {
- lua_Number r;
- TString *ts;
-} SemInfo; /* semantics information */
-
-
-typedef struct Token {
- int token;
- SemInfo seminfo;
-} Token;
-
-
-typedef struct LexState {
- int current; /* current character (charint) */
- int linenumber; /* input line counter */
- int lastline; /* line of last token `consumed' */
- Token t; /* current token */
- Token lookahead; /* look ahead token */
- struct FuncState *fs; /* `FuncState' is private to the parser */
- struct lua_State *L;
- ZIO *z; /* input stream */
- Mbuffer *buff; /* buffer for tokens */
- TString *source; /* current source name */
- char decpoint; /* locale decimal point */
-} LexState;
-
-
-LUAI_FUNC void luaX_init (lua_State *L);
-LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z,
- TString *source);
-LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l);
-LUAI_FUNC void luaX_next (LexState *ls);
-LUAI_FUNC void luaX_lookahead (LexState *ls);
-LUAI_FUNC void luaX_lexerror (LexState *ls, const char *msg, int token);
-LUAI_FUNC void luaX_syntaxerror (LexState *ls, const char *s);
-LUAI_FUNC const char *luaX_token2str (LexState *ls, int token);
-
-
-#endif
diff --git a/src/lua/src/llimits.h b/src/lua/src/llimits.h
deleted file mode 100644
index ca8dcb722..000000000
--- a/src/lua/src/llimits.h
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
-** $Id: llimits.h,v 1.69.1.1 2007/12/27 13:02:25 roberto Exp $
-** Limits, basic types, and some other `installation-dependent' definitions
-** See Copyright Notice in lua.h
-*/
-
-#ifndef llimits_h
-#define llimits_h
-
-
-#include <limits.h>
-#include <stddef.h>
-
-
-#include "lua.h"
-
-
-typedef LUAI_UINT32 lu_int32;
-
-typedef LUAI_UMEM lu_mem;
-
-typedef LUAI_MEM l_mem;
-
-
-
-/* chars used as small naturals (so that `char' is reserved for characters) */
-typedef unsigned char lu_byte;
-
-
-#define MAX_SIZET ((size_t)(~(size_t)0)-2)
-
-#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2)
-
-
-#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
-
-/*
-** conversion of pointer to integer
-** this is for hashing only; there is no problem if the integer
-** cannot hold the whole pointer value
-*/
-#define IntPoint(p) ((unsigned int)(lu_mem)(p))
-
-
-
-/* type to ensure maximum alignment */
-typedef LUAI_USER_ALIGNMENT_T L_Umaxalign;
-
-
-/* result of a `usual argument conversion' over lua_Number */
-typedef LUAI_UACNUMBER l_uacNumber;
-
-
-/* internal assertions for in-house debugging */
-#ifdef lua_assert
-
-#define check_exp(c,e) (lua_assert(c), (e))
-#define api_check(l,e) lua_assert(e)
-
-#else
-
-#define lua_assert(c) ((void)0)
-#define check_exp(c,e) (e)
-#define api_check luai_apicheck
-
-#endif
-
-
-#ifndef UNUSED
-#define UNUSED(x) ((void)(x)) /* to avoid warnings */
-#endif
-
-
-#ifndef cast
-#define cast(t, exp) ((t)(exp))
-#endif
-
-#define cast_byte(i) cast(lu_byte, (i))
-#define cast_num(i) cast(lua_Number, (i))
-#define cast_int(i) cast(int, (i))
-
-
-
-/*
-** type for virtual-machine instructions
-** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
-*/
-typedef lu_int32 Instruction;
-
-
-
-/* maximum stack for a Lua function */
-#define MAXSTACK 250
-
-
-
-/* minimum size for the string table (must be power of 2) */
-#ifndef MINSTRTABSIZE
-#define MINSTRTABSIZE 32
-#endif
-
-
-/* minimum size for string buffer */
-#ifndef LUA_MINBUFFER
-#define LUA_MINBUFFER 32
-#endif
-
-
-#ifndef lua_lock
-#define lua_lock(L) ((void) 0)
-#define lua_unlock(L) ((void) 0)
-#endif
-
-#ifndef luai_threadyield
-#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
-#endif
-
-
-/*
-** macro to control inclusion of some hard tests on stack reallocation
-*/
-#ifndef HARDSTACKTESTS
-#define condhardstacktests(x) ((void)0)
-#else
-#define condhardstacktests(x) x
-#endif
-
-#endif
diff --git a/src/lua/src/lmathlib.c b/src/lua/src/lmathlib.c
deleted file mode 100644
index 441fbf736..000000000
--- a/src/lua/src/lmathlib.c
+++ /dev/null
@@ -1,263 +0,0 @@
-/*
-** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $
-** Standard mathematical library
-** See Copyright Notice in lua.h
-*/
-
-
-#include <stdlib.h>
-#include <math.h>
-
-#define lmathlib_c
-#define LUA_LIB
-
-#include "lua.h"
-
-#include "lauxlib.h"
-#include "lualib.h"
-
-
-#undef PI
-#define PI (3.14159265358979323846)
-#define RADIANS_PER_DEGREE (PI/180.0)
-
-
-
-static int math_abs (lua_State *L) {
- lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
- return 1;
-}
-
-static int math_sin (lua_State *L) {
- lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
- return 1;
-}
-
-static int math_sinh (lua_State *L) {
- lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
- return 1;
-}
-
-static int math_cos (lua_State *L) {
- lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
- return 1;
-}
-
-static int math_cosh (lua_State *L) {
- lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
- return 1;
-}
-
-static int math_tan (lua_State *L) {
- lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
- return 1;
-}
-
-static int math_tanh (lua_State *L) {
- lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
- return 1;
-}
-
-static int math_asin (lua_State *L) {
- lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
- return 1;
-}
-
-static int math_acos (lua_State *L) {
- lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
- return 1;
-}
-
-static int math_atan (lua_State *L) {
- lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
- return 1;
-}
-
-static int math_atan2 (lua_State *L) {
- lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
- return 1;
-}
-
-static int math_ceil (lua_State *L) {
- lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
- return 1;
-}
-
-static int math_floor (lua_State *L) {
- lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
- return 1;
-}
-
-static int math_fmod (lua_State *L) {
- lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
- return 1;
-}
-
-static int math_modf (lua_State *L) {
- double ip;
- double fp = modf(luaL_checknumber(L, 1), &ip);
- lua_pushnumber(L, ip);
- lua_pushnumber(L, fp);
- return 2;
-}
-
-static int math_sqrt (lua_State *L) {
- lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
- return 1;
-}
-
-static int math_pow (lua_State *L) {
- lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
- return 1;
-}
-
-static int math_log (lua_State *L) {
- lua_pushnumber(L, log(luaL_checknumber(L, 1)));
- return 1;
-}
-
-static int math_log10 (lua_State *L) {
- lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
- return 1;
-}
-
-static int math_exp (lua_State *L) {
- lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
- return 1;
-}
-
-static int math_deg (lua_State *L) {
- lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);
- return 1;
-}
-
-static int math_rad (lua_State *L) {
- lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
- return 1;
-}
-
-static int math_frexp (lua_State *L) {
- int e;
- lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
- lua_pushinteger(L, e);
- return 2;
-}
-
-static int math_ldexp (lua_State *L) {
- lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
- return 1;
-}
-
-
-
-static int math_min (lua_State *L) {
- int n = lua_gettop(L); /* number of arguments */
- lua_Number dmin = luaL_checknumber(L, 1);
- int i;
- for (i=2; i<=n; i++) {
- lua_Number d = luaL_checknumber(L, i);
- if (d < dmin)
- dmin = d;
- }
- lua_pushnumber(L, dmin);
- return 1;
-}
-
-
-static int math_max (lua_State *L) {
- int n = lua_gettop(L); /* number of arguments */
- lua_Number dmax = luaL_checknumber(L, 1);
- int i;
- for (i=2; i<=n; i++) {
- lua_Number d = luaL_checknumber(L, i);
- if (d > dmax)
- dmax = d;
- }
- lua_pushnumber(L, dmax);
- return 1;
-}
-
-
-static int math_random (lua_State *L) {
- /* the `%' avoids the (rare) case of r==1, and is needed also because on
- some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
- lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
- switch (lua_gettop(L)) { /* check number of arguments */
- case 0: { /* no arguments */
- lua_pushnumber(L, r); /* Number between 0 and 1 */
- break;
- }
- case 1: { /* only upper limit */
- int u = luaL_checkint(L, 1);
- luaL_argcheck(L, 1<=u, 1, "interval is empty");
- lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */
- break;
- }
- case 2: { /* lower and upper limits */
- int l = luaL_checkint(L, 1);
- int u = luaL_checkint(L, 2);
- luaL_argcheck(L, l<=u, 2, "interval is empty");
- lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */
- break;
- }
- default: return luaL_error(L, "wrong number of arguments");
- }
- return 1;
-}
-
-
-static int math_randomseed (lua_State *L) {
- srand(luaL_checkint(L, 1));
- return 0;
-}
-
-
-static const luaL_Reg mathlib[] = {
- {"abs", math_abs},
- {"acos", math_acos},
- {"asin", math_asin},
- {"atan2", math_atan2},
- {"atan", math_atan},
- {"ceil", math_ceil},
- {"cosh", math_cosh},
- {"cos", math_cos},
- {"deg", math_deg},
- {"exp", math_exp},
- {"floor", math_floor},
- {"fmod", math_fmod},
- {"frexp", math_frexp},
- {"ldexp", math_ldexp},
- {"log10", math_log10},
- {"log", math_log},
- {"max", math_max},
- {"min", math_min},
- {"modf", math_modf},
- {"pow", math_pow},
- {"rad", math_rad},
- {"random", math_random},
- {"randomseed", math_randomseed},
- {"sinh", math_sinh},
- {"sin", math_sin},
- {"sqrt", math_sqrt},
- {"tanh", math_tanh},
- {"tan", math_tan},
- {NULL, NULL}
-};
-
-
-/*
-** Open math library
-*/
-LUALIB_API int luaopen_math (lua_State *L) {
- luaL_register(L, LUA_MATHLIBNAME, mathlib);
- lua_pushnumber(L, PI);
- lua_setfield(L, -2, "pi");
- lua_pushnumber(L, HUGE_VAL);
- lua_setfield(L, -2, "huge");
-#if defined(LUA_COMPAT_MOD)
- lua_getfield(L, -1, "fmod");
- lua_setfield(L, -2, "mod");
-#endif
- return 1;
-}
-
diff --git a/src/lua/src/lmem.c b/src/lua/src/lmem.c
deleted file mode 100644
index ae7d8c965..000000000
--- a/src/lua/src/lmem.c
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
-** $Id: lmem.c,v 1.70.1.1 2007/12/27 13:02:25 roberto Exp $
-** Interface to Memory Manager
-** See Copyright Notice in lua.h
-*/
-
-
-#include <stddef.h>
-
-#define lmem_c
-#define LUA_CORE
-
-#include "lua.h"
-
-#include "ldebug.h"
-#include "ldo.h"
-#include "lmem.h"
-#include "lobject.h"
-#include "lstate.h"
-
-
-
-/*
-** About the realloc function:
-** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
-** (`osize' is the old size, `nsize' is the new size)
-**
-** Lua ensures that (ptr == NULL) iff (osize == 0).
-**
-** * frealloc(ud, NULL, 0, x) creates a new block of size `x'
-**
-** * frealloc(ud, p, x, 0) frees the block `p'
-** (in this specific case, frealloc must return NULL).
-** particularly, frealloc(ud, NULL, 0, 0) does nothing
-** (which is equivalent to free(NULL) in ANSI C)
-**
-** frealloc returns NULL if it cannot create or reallocate the area
-** (any reallocation to an equal or smaller size cannot fail!)
-*/
-
-
-
-#define MINSIZEARRAY 4
-
-
-void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
- int limit, const char *errormsg) {
- void *newblock;
- int newsize;
- if (*size >= limit/2) { /* cannot double it? */
- if (*size >= limit) /* cannot grow even a little? */
- luaG_runerror(L, errormsg);
- newsize = limit; /* still have at least one free place */
- }
- else {
- newsize = (*size)*2;
- if (newsize < MINSIZEARRAY)
- newsize = MINSIZEARRAY; /* minimum size */
- }
- newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
- *size = newsize; /* update only when everything else is OK */
- return newblock;
-}
-
-
-void *luaM_toobig (lua_State *L) {
- luaG_runerror(L, "memory allocation error: block too big");
- return NULL; /* to avoid warnings */
-}
-
-
-
-/*
-** generic allocation routine.
-*/
-void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
- global_State *g = G(L);
- lua_assert((osize == 0) == (block == NULL));
- block = (*g->frealloc)(g->ud, block, osize, nsize);
- if (block == NULL && nsize > 0)
- luaD_throw(L, LUA_ERRMEM);
- lua_assert((nsize == 0) == (block == NULL));
- g->totalbytes = (g->totalbytes - osize) + nsize;
- return block;
-}
-
diff --git a/src/lua/src/lmem.h b/src/lua/src/lmem.h
deleted file mode 100644
index 7c2dcb322..000000000
--- a/src/lua/src/lmem.h
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
-** $Id: lmem.h,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $
-** Interface to Memory Manager
-** See Copyright Notice in lua.h
-*/
-
-#ifndef lmem_h
-#define lmem_h
-
-
-#include <stddef.h>
-
-#include "llimits.h"
-#include "lua.h"
-
-#define MEMERRMSG "not enough memory"
-
-
-#define luaM_reallocv(L,b,on,n,e) \
- ((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \
- luaM_realloc_(L, (b), (on)*(e), (n)*(e)) : \
- luaM_toobig(L))
-
-#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0)
-#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0)
-#define luaM_freearray(L, b, n, t) luaM_reallocv(L, (b), n, 0, sizeof(t))
-
-#define luaM_malloc(L,t) luaM_realloc_(L, NULL, 0, (t))
-#define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t)))
-#define luaM_newvector(L,n,t) \
- cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t)))
-
-#define luaM_growvector(L,v,nelems,size,t,limit,e) \
- if ((nelems)+1 > (size)) \
- ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e)))
-
-#define luaM_reallocvector(L, v,oldn,n,t) \
- ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t))))
-
-
-LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,
- size_t size);
-LUAI_FUNC void *luaM_toobig (lua_State *L);
-LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size,
- size_t size_elem, int limit,
- const char *errormsg);
-
-#endif
-
diff --git a/src/lua/src/loadlib.c b/src/lua/src/loadlib.c
deleted file mode 100644
index 0d401eba1..000000000
--- a/src/lua/src/loadlib.c
+++ /dev/null
@@ -1,666 +0,0 @@
-/*
-** $Id: loadlib.c,v 1.52.1.3 2008/08/06 13:29:28 roberto Exp $
-** Dynamic library loader for Lua
-** See Copyright Notice in lua.h
-**
-** This module contains an implementation of loadlib for Unix systems
-** that have dlfcn, an implementation for Darwin (Mac OS X), an
-** implementation for Windows, and a stub for other systems.
-*/
-
-
-#include <stdlib.h>
-#include <string.h>
-
-
-#define loadlib_c
-#define LUA_LIB
-
-#include "lua.h"
-
-#include "lauxlib.h"
-#include "lualib.h"
-
-
-/* prefix for open functions in C libraries */
-#define LUA_POF "luaopen_"
-
-/* separator for open functions in C libraries */
-#define LUA_OFSEP "_"
-
-
-#define LIBPREFIX "LOADLIB: "
-
-#define POF LUA_POF
-#define LIB_FAIL "open"
-
-
-/* error codes for ll_loadfunc */
-#define ERRLIB 1
-#define ERRFUNC 2
-
-#define setprogdir(L) ((void)0)
-
-
-static void ll_unloadlib (void *lib);
-static void *ll_load (lua_State *L, const char *path);
-static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym);
-
-
-
-#if defined(LUA_DL_DLOPEN)
-/*
-** {========================================================================
-** This is an implementation of loadlib based on the dlfcn interface.
-** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
-** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
-** as an emulation layer on top of native functions.
-** =========================================================================
-*/
-
-#include <dlfcn.h>
-
-static void ll_unloadlib (void *lib) {
- dlclose(lib);
-}
-
-
-static void *ll_load (lua_State *L, const char *path) {
- void *lib = dlopen(path, RTLD_NOW);
- if (lib == NULL) lua_pushstring(L, dlerror());
- return lib;
-}
-
-
-static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
- lua_CFunction f = (lua_CFunction)dlsym(lib, sym);
- if (f == NULL) lua_pushstring(L, dlerror());
- return f;
-}
-
-/* }====================================================== */
-
-
-
-#elif defined(LUA_DL_DLL)
-/*
-** {======================================================================
-** This is an implementation of loadlib for Windows using native functions.
-** =======================================================================
-*/
-
-#include <windows.h>
-
-
-#undef setprogdir
-
-static void setprogdir (lua_State *L) {
- char buff[MAX_PATH + 1];
- char *lb;
- DWORD nsize = sizeof(buff)/sizeof(char);
- DWORD n = GetModuleFileNameA(NULL, buff, nsize);
- if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL)
- luaL_error(L, "unable to get ModuleFileName");
- else {
- *lb = '\0';
- luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff);
- lua_remove(L, -2); /* remove original string */
- }
-}
-
-
-static void pusherror (lua_State *L) {
- int error = GetLastError();
- char buffer[128];
- if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
- NULL, error, 0, buffer, sizeof(buffer), NULL))
- lua_pushstring(L, buffer);
- else
- lua_pushfstring(L, "system error %d\n", error);
-}
-
-static void ll_unloadlib (void *lib) {
- FreeLibrary((HINSTANCE)lib);
-}
-
-
-static void *ll_load (lua_State *L, const char *path) {
- HINSTANCE lib = LoadLibraryA(path);
- if (lib == NULL) pusherror(L);
- return lib;
-}
-
-
-static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
- lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym);
- if (f == NULL) pusherror(L);
- return f;
-}
-
-/* }====================================================== */
-
-
-
-#elif defined(LUA_DL_DYLD)
-/*
-** {======================================================================
-** Native Mac OS X / Darwin Implementation
-** =======================================================================
-*/
-
-#include <mach-o/dyld.h>
-
-
-/* Mac appends a `_' before C function names */
-#undef POF
-#define POF "_" LUA_POF
-
-
-static void pusherror (lua_State *L) {
- const char *err_str;
- const char *err_file;
- NSLinkEditErrors err;
- int err_num;
- NSLinkEditError(&err, &err_num, &err_file, &err_str);
- lua_pushstring(L, err_str);
-}
-
-
-static const char *errorfromcode (NSObjectFileImageReturnCode ret) {
- switch (ret) {
- case NSObjectFileImageInappropriateFile:
- return "file is not a bundle";
- case NSObjectFileImageArch:
- return "library is for wrong CPU type";
- case NSObjectFileImageFormat:
- return "bad format";
- case NSObjectFileImageAccess:
- return "cannot access file";
- case NSObjectFileImageFailure:
- default:
- return "unable to load library";
- }
-}
-
-
-static void ll_unloadlib (void *lib) {
- NSUnLinkModule((NSModule)lib, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES);
-}
-
-
-static void *ll_load (lua_State *L, const char *path) {
- NSObjectFileImage img;
- NSObjectFileImageReturnCode ret;
- /* this would be a rare case, but prevents crashing if it happens */
- if(!_dyld_present()) {
- lua_pushliteral(L, "dyld not present");
- return NULL;
- }
- ret = NSCreateObjectFileImageFromFile(path, &img);
- if (ret == NSObjectFileImageSuccess) {
- NSModule mod = NSLinkModule(img, path, NSLINKMODULE_OPTION_PRIVATE |
- NSLINKMODULE_OPTION_RETURN_ON_ERROR);
- NSDestroyObjectFileImage(img);
- if (mod == NULL) pusherror(L);
- return mod;
- }
- lua_pushstring(L, errorfromcode(ret));
- return NULL;
-}
-
-
-static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
- NSSymbol nss = NSLookupSymbolInModule((NSModule)lib, sym);
- if (nss == NULL) {
- lua_pushfstring(L, "symbol " LUA_QS " not found", sym);
- return NULL;
- }
- return (lua_CFunction)NSAddressOfSymbol(nss);
-}
-
-/* }====================================================== */
-
-
-
-#else
-/*
-** {======================================================
-** Fallback for other systems
-** =======================================================
-*/
-
-#undef LIB_FAIL
-#define LIB_FAIL "absent"
-
-
-#define DLMSG "dynamic libraries not enabled; check your Lua installation"
-
-
-static void ll_unloadlib (void *lib) {
- (void)lib; /* to avoid warnings */
-}
-
-
-static void *ll_load (lua_State *L, const char *path) {
- (void)path; /* to avoid warnings */
- lua_pushliteral(L, DLMSG);
- return NULL;
-}
-
-
-static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
- (void)lib; (void)sym; /* to avoid warnings */
- lua_pushliteral(L, DLMSG);
- return NULL;
-}
-
-/* }====================================================== */
-#endif
-
-
-
-static void **ll_register (lua_State *L, const char *path) {
- void **plib;
- lua_pushfstring(L, "%s%s", LIBPREFIX, path);
- lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */
- if (!lua_isnil(L, -1)) /* is there an entry? */
- plib = (void **)lua_touserdata(L, -1);
- else { /* no entry yet; create one */
- lua_pop(L, 1);
- plib = (void **)lua_newuserdata(L, sizeof(const void *));
- *plib = NULL;
- luaL_getmetatable(L, "_LOADLIB");
- lua_setmetatable(L, -2);
- lua_pushfstring(L, "%s%s", LIBPREFIX, path);
- lua_pushvalue(L, -2);
- lua_settable(L, LUA_REGISTRYINDEX);
- }
- return plib;
-}
-
-
-/*
-** __gc tag method: calls library's `ll_unloadlib' function with the lib
-** handle
-*/
-static int gctm (lua_State *L) {
- void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB");
- if (*lib) ll_unloadlib(*lib);
- *lib = NULL; /* mark library as closed */
- return 0;
-}
-
-
-static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {
- void **reg = ll_register(L, path);
- if (*reg == NULL) *reg = ll_load(L, path);
- if (*reg == NULL)
- return ERRLIB; /* unable to load library */
- else {
- lua_CFunction f = ll_sym(L, *reg, sym);
- if (f == NULL)
- return ERRFUNC; /* unable to find function */
- lua_pushcfunction(L, f);
- return 0; /* return function */
- }
-}
-
-
-static int ll_loadlib (lua_State *L) {
- const char *path = luaL_checkstring(L, 1);
- const char *init = luaL_checkstring(L, 2);
- int stat = ll_loadfunc(L, path, init);
- if (stat == 0) /* no errors? */
- return 1; /* return the loaded function */
- else { /* error; error message is on stack top */
- lua_pushnil(L);
- lua_insert(L, -2);
- lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
- return 3; /* return nil, error message, and where */
- }
-}
-
-
-
-/*
-** {======================================================
-** 'require' function
-** =======================================================
-*/
-
-
-static int readable (const char *filename) {
- FILE *f = fopen(filename, "r"); /* try to open file */
- if (f == NULL) return 0; /* open failed */
- fclose(f);
- return 1;
-}
-
-
-static const char *pushnexttemplate (lua_State *L, const char *path) {
- const char *l;
- while (*path == *LUA_PATHSEP) path++; /* skip separators */
- if (*path == '\0') return NULL; /* no more templates */
- l = strchr(path, *LUA_PATHSEP); /* find next separator */
- if (l == NULL) l = path + strlen(path);
- lua_pushlstring(L, path, l - path); /* template */
- return l;
-}
-
-
-static const char *findfile (lua_State *L, const char *name,
- const char *pname) {
- const char *path;
- name = luaL_gsub(L, name, ".", LUA_DIRSEP);
- lua_getfield(L, LUA_ENVIRONINDEX, pname);
- path = lua_tostring(L, -1);
- if (path == NULL)
- luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
- lua_pushliteral(L, ""); /* error accumulator */
- while ((path = pushnexttemplate(L, path)) != NULL) {
- const char *filename;
- filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
- lua_remove(L, -2); /* remove path template */
- if (readable(filename)) /* does file exist and is readable? */
- return filename; /* return that file name */
- lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
- lua_remove(L, -2); /* remove file name */
- lua_concat(L, 2); /* add entry to possible error message */
- }
- return NULL; /* not found */
-}
-
-
-static void loaderror (lua_State *L, const char *filename) {
- luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s",
- lua_tostring(L, 1), filename, lua_tostring(L, -1));
-}
-
-
-static int loader_Lua (lua_State *L) {
- const char *filename;
- const char *name = luaL_checkstring(L, 1);
- filename = findfile(L, name, "path");
- if (filename == NULL) return 1; /* library not found in this path */
- if (luaL_loadfile(L, filename) != 0)
- loaderror(L, filename);
- return 1; /* library loaded successfully */
-}
-
-
-static const char *mkfuncname (lua_State *L, const char *modname) {
- const char *funcname;
- const char *mark = strchr(modname, *LUA_IGMARK);
- if (mark) modname = mark + 1;
- funcname = luaL_gsub(L, modname, ".", LUA_OFSEP);
- funcname = lua_pushfstring(L, POF"%s", funcname);
- lua_remove(L, -2); /* remove 'gsub' result */
- return funcname;
-}
-
-
-static int loader_C (lua_State *L) {
- const char *funcname;
- const char *name = luaL_checkstring(L, 1);
- const char *filename = findfile(L, name, "cpath");
- if (filename == NULL) return 1; /* library not found in this path */
- funcname = mkfuncname(L, name);
- if (ll_loadfunc(L, filename, funcname) != 0)
- loaderror(L, filename);
- return 1; /* library loaded successfully */
-}
-
-
-static int loader_Croot (lua_State *L) {
- const char *funcname;
- const char *filename;
- const char *name = luaL_checkstring(L, 1);
- const char *p = strchr(name, '.');
- int stat;
- if (p == NULL) return 0; /* is root */
- lua_pushlstring(L, name, p - name);
- filename = findfile(L, lua_tostring(L, -1), "cpath");
- if (filename == NULL) return 1; /* root not found */
- funcname = mkfuncname(L, name);
- if ((stat = ll_loadfunc(L, filename, funcname)) != 0) {
- if (stat != ERRFUNC) loaderror(L, filename); /* real error */
- lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
- name, filename);
- return 1; /* function not found */
- }
- return 1;
-}
-
-
-static int loader_preload (lua_State *L) {
- const char *name = luaL_checkstring(L, 1);
- lua_getfield(L, LUA_ENVIRONINDEX, "preload");
- if (!lua_istable(L, -1))
- luaL_error(L, LUA_QL("package.preload") " must be a table");
- lua_getfield(L, -1, name);
- if (lua_isnil(L, -1)) /* not found? */
- lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
- return 1;
-}
-
-
-static const int sentinel_ = 0;
-#define sentinel ((void *)&sentinel_)
-
-
-static int ll_require (lua_State *L) {
- const char *name = luaL_checkstring(L, 1);
- int i;
- lua_settop(L, 1); /* _LOADED table will be at index 2 */
- lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
- lua_getfield(L, 2, name);
- if (lua_toboolean(L, -1)) { /* is it there? */
- if (lua_touserdata(L, -1) == sentinel) /* check loops */
- luaL_error(L, "loop or previous error loading module " LUA_QS, name);
- return 1; /* package is already loaded */
- }
- /* else must load it; iterate over available loaders */
- lua_getfield(L, LUA_ENVIRONINDEX, "loaders");
- if (!lua_istable(L, -1))
- luaL_error(L, LUA_QL("package.loaders") " must be a table");
- lua_pushliteral(L, ""); /* error message accumulator */
- for (i=1; ; i++) {
- lua_rawgeti(L, -2, i); /* get a loader */
- if (lua_isnil(L, -1))
- luaL_error(L, "module " LUA_QS " not found:%s",
- name, lua_tostring(L, -2));
- lua_pushstring(L, name);
- lua_call(L, 1, 1); /* call it */
- if (lua_isfunction(L, -1)) /* did it find module? */
- break; /* module loaded successfully */
- else if (lua_isstring(L, -1)) /* loader returned error message? */
- lua_concat(L, 2); /* accumulate it */
- else
- lua_pop(L, 1);
- }
- lua_pushlightuserdata(L, sentinel);
- lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */
- lua_pushstring(L, name); /* pass name as argument to module */
- lua_call(L, 1, 1); /* run loaded module */
- if (!lua_isnil(L, -1)) /* non-nil return? */
- lua_setfield(L, 2, name); /* _LOADED[name] = returned value */
- lua_getfield(L, 2, name);
- if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */
- lua_pushboolean(L, 1); /* use true as result */
- lua_pushvalue(L, -1); /* extra copy to be returned */
- lua_setfield(L, 2, name); /* _LOADED[name] = true */
- }
- return 1;
-}
-
-/* }====================================================== */
-
-
-
-/*
-** {======================================================
-** 'module' function
-** =======================================================
-*/
-
-
-static void setfenv (lua_State *L) {
- lua_Debug ar;
- if (lua_getstack(L, 1, &ar) == 0 ||
- lua_getinfo(L, "f", &ar) == 0 || /* get calling function */
- lua_iscfunction(L, -1))
- luaL_error(L, LUA_QL("module") " not called from a Lua function");
- lua_pushvalue(L, -2);
- lua_setfenv(L, -2);
- lua_pop(L, 1);
-}
-
-
-static void dooptions (lua_State *L, int n) {
- int i;
- for (i = 2; i <= n; i++) {
- lua_pushvalue(L, i); /* get option (a function) */
- lua_pushvalue(L, -2); /* module */
- lua_call(L, 1, 0);
- }
-}
-
-
-static void modinit (lua_State *L, const char *modname) {
- const char *dot;
- lua_pushvalue(L, -1);
- lua_setfield(L, -2, "_M"); /* module._M = module */
- lua_pushstring(L, modname);
- lua_setfield(L, -2, "_NAME");
- dot = strrchr(modname, '.'); /* look for last dot in module name */
- if (dot == NULL) dot = modname;
- else dot++;
- /* set _PACKAGE as package name (full module name minus last part) */
- lua_pushlstring(L, modname, dot - modname);
- lua_setfield(L, -2, "_PACKAGE");
-}
-
-
-static int ll_module (lua_State *L) {
- const char *modname = luaL_checkstring(L, 1);
- int loaded = lua_gettop(L) + 1; /* index of _LOADED table */
- lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
- lua_getfield(L, loaded, modname); /* get _LOADED[modname] */
- if (!lua_istable(L, -1)) { /* not found? */
- lua_pop(L, 1); /* remove previous result */
- /* try global variable (and create one if it does not exist) */
- if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL)
- return luaL_error(L, "name conflict for module " LUA_QS, modname);
- lua_pushvalue(L, -1);
- lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */
- }
- /* check whether table already has a _NAME field */
- lua_getfield(L, -1, "_NAME");
- if (!lua_isnil(L, -1)) /* is table an initialized module? */
- lua_pop(L, 1);
- else { /* no; initialize it */
- lua_pop(L, 1);
- modinit(L, modname);
- }
- lua_pushvalue(L, -1);
- setfenv(L);
- dooptions(L, loaded - 1);
- return 0;
-}
-
-
-static int ll_seeall (lua_State *L) {
- luaL_checktype(L, 1, LUA_TTABLE);
- if (!lua_getmetatable(L, 1)) {
- lua_createtable(L, 0, 1); /* create new metatable */
- lua_pushvalue(L, -1);
- lua_setmetatable(L, 1);
- }
- lua_pushvalue(L, LUA_GLOBALSINDEX);
- lua_setfield(L, -2, "__index"); /* mt.__index = _G */
- return 0;
-}
-
-
-/* }====================================================== */
-
-
-
-/* auxiliary mark (for internal use) */
-#define AUXMARK "\1"
-
-static void setpath (lua_State *L, const char *fieldname, const char *envname,
- const char *def) {
- const char *path = getenv(envname);
- if (path == NULL) /* no environment variable? */
- lua_pushstring(L, def); /* use default */
- else {
- /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
- path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP,
- LUA_PATHSEP AUXMARK LUA_PATHSEP);
- luaL_gsub(L, path, AUXMARK, def);
- lua_remove(L, -2);
- }
- setprogdir(L);
- lua_setfield(L, -2, fieldname);
-}
-
-
-static const luaL_Reg pk_funcs[] = {
- {"loadlib", ll_loadlib},
- {"seeall", ll_seeall},
- {NULL, NULL}
-};
-
-
-static const luaL_Reg ll_funcs[] = {
- {"module", ll_module},
- {"require", ll_require},
- {NULL, NULL}
-};
-
-
-static const lua_CFunction loaders[] =
- {loader_preload, loader_Lua, loader_C, loader_Croot, NULL};
-
-
-LUALIB_API int luaopen_package (lua_State *L) {
- int i;
- /* create new type _LOADLIB */
- luaL_newmetatable(L, "_LOADLIB");
- lua_pushcfunction(L, gctm);
- lua_setfield(L, -2, "__gc");
- /* create `package' table */
- luaL_register(L, LUA_LOADLIBNAME, pk_funcs);
-#if defined(LUA_COMPAT_LOADLIB)
- lua_getfield(L, -1, "loadlib");
- lua_setfield(L, LUA_GLOBALSINDEX, "loadlib");
-#endif
- lua_pushvalue(L, -1);
- lua_replace(L, LUA_ENVIRONINDEX);
- /* create `loaders' table */
- lua_createtable(L, 0, sizeof(loaders)/sizeof(loaders[0]) - 1);
- /* fill it with pre-defined loaders */
- for (i=0; loaders[i] != NULL; i++) {
- lua_pushcfunction(L, loaders[i]);
- lua_rawseti(L, -2, i+1);
- }
- lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */
- setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */
- setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */
- /* store config information */
- lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n"
- LUA_EXECDIR "\n" LUA_IGMARK);
- lua_setfield(L, -2, "config");
- /* set field `loaded' */
- luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2);
- lua_setfield(L, -2, "loaded");
- /* set field `preload' */
- lua_newtable(L);
- lua_setfield(L, -2, "preload");
- lua_pushvalue(L, LUA_GLOBALSINDEX);
- luaL_register(L, NULL, ll_funcs); /* open lib into global table */
- lua_pop(L, 1);
- return 1; /* return 'package' table */
-}
-
diff --git a/src/lua/src/lobject.c b/src/lua/src/lobject.c
deleted file mode 100644
index 4ff50732a..000000000
--- a/src/lua/src/lobject.c
+++ /dev/null
@@ -1,214 +0,0 @@
-/*
-** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $
-** Some generic functions over Lua objects
-** See Copyright Notice in lua.h
-*/
-
-#include <ctype.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define lobject_c
-#define LUA_CORE
-
-#include "lua.h"
-
-#include "ldo.h"
-#include "lmem.h"
-#include "lobject.h"
-#include "lstate.h"
-#include "lstring.h"
-#include "lvm.h"
-
-
-
-const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL};
-
-
-/*
-** converts an integer to a "floating point byte", represented as
-** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
-** eeeee != 0 and (xxx) otherwise.
-*/
-int luaO_int2fb (unsigned int x) {
- int e = 0; /* expoent */
- while (x >= 16) {
- x = (x+1) >> 1;
- e++;
- }
- if (x < 8) return x;
- else return ((e+1) << 3) | (cast_int(x) - 8);
-}
-
-
-/* converts back */
-int luaO_fb2int (int x) {
- int e = (x >> 3) & 31;
- if (e == 0) return x;
- else return ((x & 7)+8) << (e - 1);
-}
-
-
-int luaO_log2 (unsigned int x) {
- static const lu_byte log_2[256] = {
- 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
- 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
- 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
- 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
- 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
- 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
- 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
- 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
- };
- int l = -1;
- while (x >= 256) { l += 8; x >>= 8; }
- return l + log_2[x];
-
-}
-
-
-int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
- if (ttype(t1) != ttype(t2)) return 0;
- else switch (ttype(t1)) {
- case LUA_TNIL:
- return 1;
- case LUA_TNUMBER:
- return luai_numeq(nvalue(t1), nvalue(t2));
- case LUA_TBOOLEAN:
- return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
- case LUA_TLIGHTUSERDATA:
- return pvalue(t1) == pvalue(t2);
- default:
- lua_assert(iscollectable(t1));
- return gcvalue(t1) == gcvalue(t2);
- }
-}
-
-
-int luaO_str2d (const char *s, lua_Number *result) {
- char *endptr;
- *result = lua_str2number(s, &endptr);
- if (endptr == s) return 0; /* conversion failed */
- if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */
- *result = cast_num(strtoul(s, &endptr, 16));
- if (*endptr == '\0') return 1; /* most common case */
- while (isspace(cast(unsigned char, *endptr))) endptr++;
- if (*endptr != '\0') return 0; /* invalid trailing characters? */
- return 1;
-}
-
-
-
-static void pushstr (lua_State *L, const char *str) {
- setsvalue2s(L, L->top, luaS_new(L, str));
- incr_top(L);
-}
-
-
-/* this function handles only `%d', `%c', %f, %p, and `%s' formats */
-const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
- int n = 1;
- pushstr(L, "");
- for (;;) {
- const char *e = strchr(fmt, '%');
- if (e == NULL) break;
- setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
- incr_top(L);
- switch (*(e+1)) {
- case 's': {
- const char *s = va_arg(argp, char *);
- if (s == NULL) s = "(null)";
- pushstr(L, s);
- break;
- }
- case 'c': {
- char buff[2];
- buff[0] = cast(char, va_arg(argp, int));
- buff[1] = '\0';
- pushstr(L, buff);
- break;
- }
- case 'd': {
- setnvalue(L->top, cast_num(va_arg(argp, int)));
- incr_top(L);
- break;
- }
- case 'f': {
- setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
- incr_top(L);
- break;
- }
- case 'p': {
- char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
- sprintf(buff, "%p", va_arg(argp, void *));
- pushstr(L, buff);
- break;
- }
- case '%': {
- pushstr(L, "%");
- break;
- }
- default: {
- char buff[3];
- buff[0] = '%';
- buff[1] = *(e+1);
- buff[2] = '\0';
- pushstr(L, buff);
- break;
- }
- }
- n += 2;
- fmt = e+2;
- }
- pushstr(L, fmt);
- luaV_concat(L, n+1, cast_int(L->top - L->base) - 1);
- L->top -= n;
- return svalue(L->top - 1);
-}
-
-
-const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
- const char *msg;
- va_list argp;
- va_start(argp, fmt);
- msg = luaO_pushvfstring(L, fmt, argp);
- va_end(argp);
- return msg;
-}
-
-
-void luaO_chunkid (char *out, const char *source, size_t bufflen) {
- if (*source == '=') {
- strncpy(out, source+1, bufflen); /* remove first char */
- out[bufflen-1] = '\0'; /* ensures null termination */
- }
- else { /* out = "source", or "...source" */
- if (*source == '@') {
- size_t l;
- source++; /* skip the `@' */
- bufflen -= sizeof(" '...' ");
- l = strlen(source);
- strcpy(out, "");
- if (l > bufflen) {
- source += (l-bufflen); /* get last part of file name */
- strcat(out, "...");
- }
- strcat(out, source);
- }
- else { /* out = [string "string"] */
- size_t len = strcspn(source, "\n\r"); /* stop at first newline */
- bufflen -= sizeof(" [string \"...\"] ");
- if (len > bufflen) len = bufflen;
- strcpy(out, "[string \"");
- if (source[len] != '\0') { /* must truncate? */
- strncat(out, source, len);
- strcat(out, "...");
- }
- else
- strcat(out, source);
- strcat(out, "\"]");
- }
- }
-}
diff --git a/src/lua/src/lobject.h b/src/lua/src/lobject.h
deleted file mode 100644
index f1e447ef3..000000000
--- a/src/lua/src/lobject.h
+++ /dev/null
@@ -1,381 +0,0 @@
-/*
-** $Id: lobject.h,v 2.20.1.2 2008/08/06 13:29:48 roberto Exp $
-** Type definitions for Lua objects
-** See Copyright Notice in lua.h
-*/
-
-
-#ifndef lobject_h
-#define lobject_h
-
-
-#include <stdarg.h>
-
-
-#include "llimits.h"
-#include "lua.h"
-
-
-/* tags for values visible from Lua */
-#define LAST_TAG LUA_TTHREAD
-
-#define NUM_TAGS (LAST_TAG+1)
-
-
-/*
-** Extra tags for non-values
-*/
-#define LUA_TPROTO (LAST_TAG+1)
-#define LUA_TUPVAL (LAST_TAG+2)
-#define LUA_TDEADKEY (LAST_TAG+3)
-
-
-/*
-** Union of all collectable objects
-*/
-typedef union GCObject GCObject;
-
-
-/*
-** Common Header for all collectable objects (in macro form, to be
-** included in other objects)
-*/
-#define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
-
-
-/*
-** Common header in struct form
-*/
-typedef struct GCheader {
- CommonHeader;
-} GCheader;
-
-
-
-
-/*
-** Union of all Lua values
-*/
-typedef union {
- GCObject *gc;
- void *p;
- lua_Number n;
- int b;
-} Value;
-
-
-/*
-** Tagged Values
-*/
-
-#define TValuefields Value value; int tt
-
-typedef struct lua_TValue {
- TValuefields;
-} TValue;
-
-
-/* Macros to test type */
-#define ttisnil(o) (ttype(o) == LUA_TNIL)
-#define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
-#define ttisstring(o) (ttype(o) == LUA_TSTRING)
-#define ttistable(o) (ttype(o) == LUA_TTABLE)
-#define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
-#define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN)
-#define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
-#define ttisthread(o) (ttype(o) == LUA_TTHREAD)
-#define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
-
-/* Macros to access values */
-#define ttype(o) ((o)->tt)
-#define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
-#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
-#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
-#define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
-#define tsvalue(o) (&rawtsvalue(o)->tsv)
-#define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
-#define uvalue(o) (&rawuvalue(o)->uv)
-#define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl)
-#define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)
-#define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
-#define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th)
-
-#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
-
-/*
-** for internal debug only
-*/
-#define checkconsistency(obj) \
- lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
-
-#define checkliveness(g,obj) \
- lua_assert(!iscollectable(obj) || \
- ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
-
-
-/* Macros to set values */
-#define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
-
-#define setnvalue(obj,x) \
- { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
-
-#define setpvalue(obj,x) \
- { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
-
-#define setbvalue(obj,x) \
- { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; }
-
-#define setsvalue(L,obj,x) \
- { TValue *i_o=(obj); \
- i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \
- checkliveness(G(L),i_o); }
-
-#define setuvalue(L,obj,x) \
- { TValue *i_o=(obj); \
- i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \
- checkliveness(G(L),i_o); }
-
-#define setthvalue(L,obj,x) \
- { TValue *i_o=(obj); \
- i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \
- checkliveness(G(L),i_o); }
-
-#define setclvalue(L,obj,x) \
- { TValue *i_o=(obj); \
- i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \
- checkliveness(G(L),i_o); }
-
-#define sethvalue(L,obj,x) \
- { TValue *i_o=(obj); \
- i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \
- checkliveness(G(L),i_o); }
-
-#define setptvalue(L,obj,x) \
- { TValue *i_o=(obj); \
- i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
- checkliveness(G(L),i_o); }
-
-
-
-
-#define setobj(L,obj1,obj2) \
- { const TValue *o2=(obj2); TValue *o1=(obj1); \
- o1->value = o2->value; o1->tt=o2->tt; \
- checkliveness(G(L),o1); }
-
-
-/*
-** different types of sets, according to destination
-*/
-
-/* from stack to (same) stack */
-#define setobjs2s setobj
-/* to stack (not from same stack) */
-#define setobj2s setobj
-#define setsvalue2s setsvalue
-#define sethvalue2s sethvalue
-#define setptvalue2s setptvalue
-/* from table to same table */
-#define setobjt2t setobj
-/* to table */
-#define setobj2t setobj
-/* to new object */
-#define setobj2n setobj
-#define setsvalue2n setsvalue
-
-#define setttype(obj, tt) (ttype(obj) = (tt))
-
-
-#define iscollectable(o) (ttype(o) >= LUA_TSTRING)
-
-
-
-typedef TValue *StkId; /* index to stack elements */
-
-
-/*
-** String headers for string table
-*/
-typedef union TString {
- L_Umaxalign dummy; /* ensures maximum alignment for strings */
- struct {
- CommonHeader;
- lu_byte reserved;
- unsigned int hash;
- size_t len;
- } tsv;
-} TString;
-
-
-#define getstr(ts) cast(const char *, (ts) + 1)
-#define svalue(o) getstr(rawtsvalue(o))
-
-
-
-typedef union Udata {
- L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
- struct {
- CommonHeader;
- struct Table *metatable;
- struct Table *env;
- size_t len;
- } uv;
-} Udata;
-
-
-
-
-/*
-** Function Prototypes
-*/
-typedef struct Proto {
- CommonHeader;
- TValue *k; /* constants used by the function */
- Instruction *code;
- struct Proto **p; /* functions defined inside the function */
- int *lineinfo; /* map from opcodes to source lines */
- struct LocVar *locvars; /* information about local variables */
- TString **upvalues; /* upvalue names */
- TString *source;
- int sizeupvalues;
- int sizek; /* size of `k' */
- int sizecode;
- int sizelineinfo;
- int sizep; /* size of `p' */
- int sizelocvars;
- int linedefined;
- int lastlinedefined;
- GCObject *gclist;
- lu_byte nups; /* number of upvalues */
- lu_byte numparams;
- lu_byte is_vararg;
- lu_byte maxstacksize;
-} Proto;
-
-
-/* masks for new-style vararg */
-#define VARARG_HASARG 1
-#define VARARG_ISVARARG 2
-#define VARARG_NEEDSARG 4
-
-
-typedef struct LocVar {
- TString *varname;
- int startpc; /* first point where variable is active */
- int endpc; /* first point where variable is dead */
-} LocVar;
-
-
-
-/*
-** Upvalues
-*/
-
-typedef struct UpVal {
- CommonHeader;
- TValue *v; /* points to stack or to its own value */
- union {
- TValue value; /* the value (when closed) */
- struct { /* double linked list (when open) */
- struct UpVal *prev;
- struct UpVal *next;
- } l;
- } u;
-} UpVal;
-
-
-/*
-** Closures
-*/
-
-#define ClosureHeader \
- CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \
- struct Table *env
-
-typedef struct CClosure {
- ClosureHeader;
- lua_CFunction f;
- TValue upvalue[1];
-} CClosure;
-
-
-typedef struct LClosure {
- ClosureHeader;
- struct Proto *p;
- UpVal *upvals[1];
-} LClosure;
-
-
-typedef union Closure {
- CClosure c;
- LClosure l;
-} Closure;
-
-
-#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
-#define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
-
-
-/*
-** Tables
-*/
-
-typedef union TKey {
- struct {
- TValuefields;
- struct Node *next; /* for chaining */
- } nk;
- TValue tvk;
-} TKey;
-
-
-typedef struct Node {
- TValue i_val;
- TKey i_key;
-} Node;
-
-
-typedef struct Table {
- CommonHeader;
- lu_byte flags; /* 1<<p means tagmethod(p) is not present */
- lu_byte lsizenode; /* log2 of size of `node' array */
- struct Table *metatable;
- TValue *array; /* array part */
- Node *node;
- Node *lastfree; /* any free position is before this position */
- GCObject *gclist;
- int sizearray; /* size of `array' array */
-} Table;
-
-
-
-/*
-** `module' operation for hashing (size is always a power of 2)
-*/
-#define lmod(s,size) \
- (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
-
-
-#define twoto(x) (1<<(x))
-#define sizenode(t) (twoto((t)->lsizenode))
-
-
-#define luaO_nilobject (&luaO_nilobject_)
-
-LUAI_DATA const TValue luaO_nilobject_;
-
-#define ceillog2(x) (luaO_log2((x)-1) + 1)
-
-LUAI_FUNC int luaO_log2 (unsigned int x);
-LUAI_FUNC int luaO_int2fb (unsigned int x);
-LUAI_FUNC int luaO_fb2int (int x);
-LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
-LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
-LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
- va_list argp);
-LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
-LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
-
-
-#endif
-
diff --git a/src/lua/src/lopcodes.c b/src/lua/src/lopcodes.c
deleted file mode 100644
index 4cc745230..000000000
--- a/src/lua/src/lopcodes.c
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
-** $Id: lopcodes.c,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $
-** See Copyright Notice in lua.h
-*/
-
-
-#define lopcodes_c
-#define LUA_CORE
-
-
-#include "lopcodes.h"
-
-
-/* ORDER OP */
-
-const char *const luaP_opnames[NUM_OPCODES+1] = {
- "MOVE",
- "LOADK",
- "LOADBOOL",
- "LOADNIL",
- "GETUPVAL",
- "GETGLOBAL",
- "GETTABLE",
- "SETGLOBAL",
- "SETUPVAL",
- "SETTABLE",
- "NEWTABLE",
- "SELF",
- "ADD",
- "SUB",
- "MUL",
- "DIV",
- "MOD",
- "POW",
- "UNM",
- "NOT",
- "LEN",
- "CONCAT",
- "JMP",
- "EQ",
- "LT",
- "LE",
- "TEST",
- "TESTSET",
- "CALL",
- "TAILCALL",
- "RETURN",
- "FORLOOP",
- "FORPREP",
- "TFORLOOP",
- "SETLIST",
- "CLOSE",
- "CLOSURE",
- "VARARG",
- NULL
-};
-
-
-#define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m))
-
-const lu_byte luaP_opmodes[NUM_OPCODES] = {
-/* T A B C mode opcode */
- opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */
- ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */
- ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */
- ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LOADNIL */
- ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */
- ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_GETGLOBAL */
- ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */
- ,opmode(0, 0, OpArgK, OpArgN, iABx) /* OP_SETGLOBAL */
- ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */
- ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */
- ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */
- ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */
- ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */
- ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */
- ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */
- ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */
- ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */
- ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */
- ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */
- ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */
- ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */
- ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */
- ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */
- ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */
- ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */
- ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */
- ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TEST */
- ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */
- ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */
- ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */
- ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */
- ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */
- ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */
- ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TFORLOOP */
- ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */
- ,opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */
- ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */
- ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */
-};
-
diff --git a/src/lua/src/lopcodes.h b/src/lua/src/lopcodes.h
deleted file mode 100644
index 41224d6ee..000000000
--- a/src/lua/src/lopcodes.h
+++ /dev/null
@@ -1,268 +0,0 @@
-/*
-** $Id: lopcodes.h,v 1.125.1.1 2007/12/27 13:02:25 roberto Exp $
-** Opcodes for Lua virtual machine
-** See Copyright Notice in lua.h
-*/
-
-#ifndef lopcodes_h
-#define lopcodes_h
-
-#include "llimits.h"
-
-
-/*===========================================================================
- We assume that instructions are unsigned numbers.
- All instructions have an opcode in the first 6 bits.
- Instructions can have the following fields:
- `A' : 8 bits
- `B' : 9 bits
- `C' : 9 bits
- `Bx' : 18 bits (`B' and `C' together)
- `sBx' : signed Bx
-
- A signed argument is represented in excess K; that is, the number
- value is the unsigned value minus K. K is exactly the maximum value
- for that argument (so that -max is represented by 0, and +max is
- represented by 2*max), which is half the maximum for the corresponding
- unsigned argument.
-===========================================================================*/
-
-
-enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */
-
-
-/*
-** size and position of opcode arguments.
-*/
-#define SIZE_C 9
-#define SIZE_B 9
-#define SIZE_Bx (SIZE_C + SIZE_B)
-#define SIZE_A 8
-
-#define SIZE_OP 6
-
-#define POS_OP 0
-#define POS_A (POS_OP + SIZE_OP)
-#define POS_C (POS_A + SIZE_A)
-#define POS_B (POS_C + SIZE_C)
-#define POS_Bx POS_C
-
-
-/*
-** limits for opcode arguments.
-** we use (signed) int to manipulate most arguments,
-** so they must fit in LUAI_BITSINT-1 bits (-1 for sign)
-*/
-#if SIZE_Bx < LUAI_BITSINT-1
-#define MAXARG_Bx ((1<<SIZE_Bx)-1)
-#define MAXARG_sBx (MAXARG_Bx>>1) /* `sBx' is signed */
-#else
-#define MAXARG_Bx MAX_INT
-#define MAXARG_sBx MAX_INT
-#endif
-
-
-#define MAXARG_A ((1<<SIZE_A)-1)
-#define MAXARG_B ((1<<SIZE_B)-1)
-#define MAXARG_C ((1<<SIZE_C)-1)
-
-
-/* creates a mask with `n' 1 bits at position `p' */
-#define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p)
-
-/* creates a mask with `n' 0 bits at position `p' */
-#define MASK0(n,p) (~MASK1(n,p))
-
-/*
-** the following macros help to manipulate instructions
-*/
-
-#define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
-#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
- ((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
-
-#define GETARG_A(i) (cast(int, ((i)>>POS_A) & MASK1(SIZE_A,0)))
-#define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
- ((cast(Instruction, u)<<POS_A)&MASK1(SIZE_A,POS_A))))
-
-#define GETARG_B(i) (cast(int, ((i)>>POS_B) & MASK1(SIZE_B,0)))
-#define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
- ((cast(Instruction, b)<<POS_B)&MASK1(SIZE_B,POS_B))))
-
-#define GETARG_C(i) (cast(int, ((i)>>POS_C) & MASK1(SIZE_C,0)))
-#define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \
- ((cast(Instruction, b)<<POS_C)&MASK1(SIZE_C,POS_C))))
-
-#define GETARG_Bx(i) (cast(int, ((i)>>POS_Bx) & MASK1(SIZE_Bx,0)))
-#define SETARG_Bx(i,b) ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \
- ((cast(Instruction, b)<<POS_Bx)&MASK1(SIZE_Bx,POS_Bx))))
-
-#define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx)
-#define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))
-
-
-#define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)<<POS_OP) \
- | (cast(Instruction, a)<<POS_A) \
- | (cast(Instruction, b)<<POS_B) \
- | (cast(Instruction, c)<<POS_C))
-
-#define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \
- | (cast(Instruction, a)<<POS_A) \
- | (cast(Instruction, bc)<<POS_Bx))
-
-
-/*
-** Macros to operate RK indices
-*/
-
-/* this bit 1 means constant (0 means register) */
-#define BITRK (1 << (SIZE_B - 1))
-
-/* test whether value is a constant */
-#define ISK(x) ((x) & BITRK)
-
-/* gets the index of the constant */
-#define INDEXK(r) ((int)(r) & ~BITRK)
-
-#define MAXINDEXRK (BITRK - 1)
-
-/* code a constant index as a RK value */
-#define RKASK(x) ((x) | BITRK)
-
-
-/*
-** invalid register that fits in 8 bits
-*/
-#define NO_REG MAXARG_A
-
-
-/*
-** R(x) - register
-** Kst(x) - constant (in constant table)
-** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)
-*/
-
-
-/*
-** grep "ORDER OP" if you change these enums
-*/
-
-typedef enum {
-/*----------------------------------------------------------------------
-name args description
-------------------------------------------------------------------------*/
-OP_MOVE,/* A B R(A) := R(B) */
-OP_LOADK,/* A Bx R(A) := Kst(Bx) */
-OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */
-OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */
-OP_GETUPVAL,/* A B R(A) := UpValue[B] */
-
-OP_GETGLOBAL,/* A Bx R(A) := Gbl[Kst(Bx)] */
-OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */
-
-OP_SETGLOBAL,/* A Bx Gbl[Kst(Bx)] := R(A) */
-OP_SETUPVAL,/* A B UpValue[B] := R(A) */
-OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */
-
-OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */
-
-OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */
-
-OP_ADD,/* A B C R(A) := RK(B) + RK(C) */
-OP_SUB,/* A B C R(A) := RK(B) - RK(C) */
-OP_MUL,/* A B C R(A) := RK(B) * RK(C) */
-OP_DIV,/* A B C R(A) := RK(B) / RK(C) */
-OP_MOD,/* A B C R(A) := RK(B) % RK(C) */
-OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */
-OP_UNM,/* A B R(A) := -R(B) */
-OP_NOT,/* A B R(A) := not R(B) */
-OP_LEN,/* A B R(A) := length of R(B) */
-
-OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
-
-OP_JMP,/* sBx pc+=sBx */
-
-OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */
-OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */
-OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
-
-OP_TEST,/* A C if not (R(A) <=> C) then pc++ */
-OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */
-
-OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
-OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */
-OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */
-
-OP_FORLOOP,/* A sBx R(A)+=R(A+2);
- if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
-OP_FORPREP,/* A sBx R(A)-=R(A+2); pc+=sBx */
-
-OP_TFORLOOP,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));
- if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++ */
-OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
-
-OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/
-OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */
-
-OP_VARARG/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */
-} OpCode;
-
-
-#define NUM_OPCODES (cast(int, OP_VARARG) + 1)
-
-
-
-/*===========================================================================
- Notes:
- (*) In OP_CALL, if (B == 0) then B = top. C is the number of returns - 1,
- and can be 0: OP_CALL then sets `top' to last_result+1, so
- next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'.
-
- (*) In OP_VARARG, if (B == 0) then use actual number of varargs and
- set top (like in OP_CALL with C == 0).
-
- (*) In OP_RETURN, if (B == 0) then return up to `top'
-
- (*) In OP_SETLIST, if (B == 0) then B = `top';
- if (C == 0) then next `instruction' is real C
-
- (*) For comparisons, A specifies what condition the test should accept
- (true or false).
-
- (*) All `skips' (pc++) assume that next instruction is a jump
-===========================================================================*/
-
-
-/*
-** masks for instruction properties. The format is:
-** bits 0-1: op mode
-** bits 2-3: C arg mode
-** bits 4-5: B arg mode
-** bit 6: instruction set register A
-** bit 7: operator is a test
-*/
-
-enum OpArgMask {
- OpArgN, /* argument is not used */
- OpArgU, /* argument is used */
- OpArgR, /* argument is a register or a jump offset */
- OpArgK /* argument is a constant or register/constant */
-};
-
-LUAI_DATA const lu_byte luaP_opmodes[NUM_OPCODES];
-
-#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3))
-#define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3))
-#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3))
-#define testAMode(m) (luaP_opmodes[m] & (1 << 6))
-#define testTMode(m) (luaP_opmodes[m] & (1 << 7))
-
-
-LUAI_DATA const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */
-
-
-/* number of list items to accumulate before a SETLIST instruction */
-#define LFIELDS_PER_FLUSH 50
-
-
-#endif
diff --git a/src/lua/src/loslib.c b/src/lua/src/loslib.c
deleted file mode 100644
index da06a572a..000000000
--- a/src/lua/src/loslib.c
+++ /dev/null
@@ -1,243 +0,0 @@
-/*
-** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $
-** Standard Operating System library
-** See Copyright Notice in lua.h
-*/
-
-
-#include <errno.h>
-#include <locale.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-
-#define loslib_c
-#define LUA_LIB
-
-#include "lua.h"
-
-#include "lauxlib.h"
-#include "lualib.h"
-
-
-static int os_pushresult (lua_State *L, int i, const char *filename) {
- int en = errno; /* calls to Lua API may change this value */
- if (i) {
- lua_pushboolean(L, 1);
- return 1;
- }
- else {
- lua_pushnil(L);
- lua_pushfstring(L, "%s: %s", filename, strerror(en));
- lua_pushinteger(L, en);
- return 3;
- }
-}
-
-
-static int os_execute (lua_State *L) {
- lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
- return 1;
-}
-
-
-static int os_remove (lua_State *L) {
- const char *filename = luaL_checkstring(L, 1);
- return os_pushresult(L, remove(filename) == 0, filename);
-}
-
-
-static int os_rename (lua_State *L) {
- const char *fromname = luaL_checkstring(L, 1);
- const char *toname = luaL_checkstring(L, 2);
- return os_pushresult(L, rename(fromname, toname) == 0, fromname);
-}
-
-
-static int os_tmpname (lua_State *L) {
- char buff[LUA_TMPNAMBUFSIZE];
- int err;
- lua_tmpnam(buff, err);
- if (err)
- return luaL_error(L, "unable to generate a unique filename");
- lua_pushstring(L, buff);
- return 1;
-}
-
-
-static int os_getenv (lua_State *L) {
- lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
- return 1;
-}
-
-
-static int os_clock (lua_State *L) {
- lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
- return 1;
-}
-
-
-/*
-** {======================================================
-** Time/Date operations
-** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
-** wday=%w+1, yday=%j, isdst=? }
-** =======================================================
-*/
-
-static void setfield (lua_State *L, const char *key, int value) {
- lua_pushinteger(L, value);
- lua_setfield(L, -2, key);
-}
-
-static void setboolfield (lua_State *L, const char *key, int value) {
- if (value < 0) /* undefined? */
- return; /* does not set field */
- lua_pushboolean(L, value);
- lua_setfield(L, -2, key);
-}
-
-static int getboolfield (lua_State *L, const char *key) {
- int res;
- lua_getfield(L, -1, key);
- res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
- lua_pop(L, 1);
- return res;
-}
-
-
-static int getfield (lua_State *L, const char *key, int d) {
- int res;
- lua_getfield(L, -1, key);
- if (lua_isnumber(L, -1))
- res = (int)lua_tointeger(L, -1);
- else {
- if (d < 0)
- return luaL_error(L, "field " LUA_QS " missing in date table", key);
- res = d;
- }
- lua_pop(L, 1);
- return res;
-}
-
-
-static int os_date (lua_State *L) {
- const char *s = luaL_optstring(L, 1, "%c");
- time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
- struct tm *stm;
- if (*s == '!') { /* UTC? */
- stm = gmtime(&t);
- s++; /* skip `!' */
- }
- else
- stm = localtime(&t);
- if (stm == NULL) /* invalid date? */
- lua_pushnil(L);
- else if (strcmp(s, "*t") == 0) {
- lua_createtable(L, 0, 9); /* 9 = number of fields */
- setfield(L, "sec", stm->tm_sec);
- setfield(L, "min", stm->tm_min);
- setfield(L, "hour", stm->tm_hour);
- setfield(L, "day", stm->tm_mday);
- setfield(L, "month", stm->tm_mon+1);
- setfield(L, "year", stm->tm_year+1900);
- setfield(L, "wday", stm->tm_wday+1);
- setfield(L, "yday", stm->tm_yday+1);
- setboolfield(L, "isdst", stm->tm_isdst);
- }
- else {
- char cc[3];
- luaL_Buffer b;
- cc[0] = '%'; cc[2] = '\0';
- luaL_buffinit(L, &b);
- for (; *s; s++) {
- if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */
- luaL_addchar(&b, *s);
- else {
- size_t reslen;
- char buff[200]; /* should be big enough for any conversion result */
- cc[1] = *(++s);
- reslen = strftime(buff, sizeof(buff), cc, stm);
- luaL_addlstring(&b, buff, reslen);
- }
- }
- luaL_pushresult(&b);
- }
- return 1;
-}
-
-
-static int os_time (lua_State *L) {
- time_t t;
- if (lua_isnoneornil(L, 1)) /* called without args? */
- t = time(NULL); /* get current time */
- else {
- struct tm ts;
- luaL_checktype(L, 1, LUA_TTABLE);
- lua_settop(L, 1); /* make sure table is at the top */
- ts.tm_sec = getfield(L, "sec", 0);
- ts.tm_min = getfield(L, "min", 0);
- ts.tm_hour = getfield(L, "hour", 12);
- ts.tm_mday = getfield(L, "day", -1);
- ts.tm_mon = getfield(L, "month", -1) - 1;
- ts.tm_year = getfield(L, "year", -1) - 1900;
- ts.tm_isdst = getboolfield(L, "isdst");
- t = mktime(&ts);
- }
- if (t == (time_t)(-1))
- lua_pushnil(L);
- else
- lua_pushnumber(L, (lua_Number)t);
- return 1;
-}
-
-
-static int os_difftime (lua_State *L) {
- lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
- (time_t)(luaL_optnumber(L, 2, 0))));
- return 1;
-}
-
-/* }====================================================== */
-
-
-static int os_setlocale (lua_State *L) {
- static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
- LC_NUMERIC, LC_TIME};
- static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
- "numeric", "time", NULL};
- const char *l = luaL_optstring(L, 1, NULL);
- int op = luaL_checkoption(L, 2, "all", catnames);
- lua_pushstring(L, setlocale(cat[op], l));
- return 1;
-}
-
-
-static int os_exit (lua_State *L) {
- exit(luaL_optint(L, 1, EXIT_SUCCESS));
-}
-
-static const luaL_Reg syslib[] = {
- {"clock", os_clock},
- {"date", os_date},
- {"difftime", os_difftime},
- {"execute", os_execute},
- {"exit", os_exit},
- {"getenv", os_getenv},
- {"remove", os_remove},
- {"rename", os_rename},
- {"setlocale", os_setlocale},
- {"time", os_time},
- {"tmpname", os_tmpname},
- {NULL, NULL}
-};
-
-/* }====================================================== */
-
-
-
-LUALIB_API int luaopen_os (lua_State *L) {
- luaL_register(L, LUA_OSLIBNAME, syslib);
- return 1;
-}
-
diff --git a/src/lua/src/lparser.c b/src/lua/src/lparser.c
deleted file mode 100644
index 1e2a9a88b..000000000
--- a/src/lua/src/lparser.c
+++ /dev/null
@@ -1,1339 +0,0 @@
-/*
-** $Id: lparser.c,v 2.42.1.3 2007/12/28 15:32:23 roberto Exp $
-** Lua Parser
-** See Copyright Notice in lua.h
-*/
-
-
-#include <string.h>
-
-#define lparser_c
-#define LUA_CORE
-
-#include "lua.h"
-
-#include "lcode.h"
-#include "ldebug.h"
-#include "ldo.h"
-#include "lfunc.h"
-#include "llex.h"
-#include "lmem.h"
-#include "lobject.h"
-#include "lopcodes.h"
-#include "lparser.h"
-#include "lstate.h"
-#include "lstring.h"
-#include "ltable.h"
-
-
-
-#define hasmultret(k) ((k) == VCALL || (k) == VVARARG)
-
-#define getlocvar(fs, i) ((fs)->f->locvars[(fs)->actvar[i]])
-
-#define luaY_checklimit(fs,v,l,m) if ((v)>(l)) errorlimit(fs,l,m)
-
-
-/*
-** nodes for block list (list of active blocks)
-*/
-typedef struct BlockCnt {
- struct BlockCnt *previous; /* chain */
- int breaklist; /* list of jumps out of this loop */
- lu_byte nactvar; /* # active locals outside the breakable structure */
- lu_byte upval; /* true if some variable in the block is an upvalue */
- lu_byte isbreakable; /* true if `block' is a loop */
-} BlockCnt;
-
-
-
-/*
-** prototypes for recursive non-terminal functions
-*/
-static void chunk (LexState *ls);
-static void expr (LexState *ls, expdesc *v);
-
-
-static void anchor_token (LexState *ls) {
- if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) {
- TString *ts = ls->t.seminfo.ts;
- luaX_newstring(ls, getstr(ts), ts->tsv.len);
- }
-}
-
-
-static void error_expected (LexState *ls, int token) {
- luaX_syntaxerror(ls,
- luaO_pushfstring(ls->L, LUA_QS " expected", luaX_token2str(ls, token)));
-}
-
-
-static void errorlimit (FuncState *fs, int limit, const char *what) {
- const char *msg = (fs->f->linedefined == 0) ?
- luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) :
- luaO_pushfstring(fs->L, "function at line %d has more than %d %s",
- fs->f->linedefined, limit, what);
- luaX_lexerror(fs->ls, msg, 0);
-}
-
-
-static int testnext (LexState *ls, int c) {
- if (ls->t.token == c) {
- luaX_next(ls);
- return 1;
- }
- else return 0;
-}
-
-
-static void check (LexState *ls, int c) {
- if (ls->t.token != c)
- error_expected(ls, c);
-}
-
-static void checknext (LexState *ls, int c) {
- check(ls, c);
- luaX_next(ls);
-}
-
-
-#define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); }
-
-
-
-static void check_match (LexState *ls, int what, int who, int where) {
- if (!testnext(ls, what)) {
- if (where == ls->linenumber)
- error_expected(ls, what);
- else {
- luaX_syntaxerror(ls, luaO_pushfstring(ls->L,
- LUA_QS " expected (to close " LUA_QS " at line %d)",
- luaX_token2str(ls, what), luaX_token2str(ls, who), where));
- }
- }
-}
-
-
-static TString *str_checkname (LexState *ls) {
- TString *ts;
- check(ls, TK_NAME);
- ts = ls->t.seminfo.ts;
- luaX_next(ls);
- return ts;
-}
-
-
-static void init_exp (expdesc *e, expkind k, int i) {
- e->f = e->t = NO_JUMP;
- e->k = k;
- e->u.s.info = i;
-}
-
-
-static void codestring (LexState *ls, expdesc *e, TString *s) {
- init_exp(e, VK, luaK_stringK(ls->fs, s));
-}
-
-
-static void checkname(LexState *ls, expdesc *e) {
- codestring(ls, e, str_checkname(ls));
-}
-
-
-static int registerlocalvar (LexState *ls, TString *varname) {
- FuncState *fs = ls->fs;
- Proto *f = fs->f;
- int oldsize = f->sizelocvars;
- luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars,
- LocVar, SHRT_MAX, "too many local variables");
- while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL;
- f->locvars[fs->nlocvars].varname = varname;
- luaC_objbarrier(ls->L, f, varname);
- return fs->nlocvars++;
-}
-
-
-#define new_localvarliteral(ls,v,n) \
- new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char))-1), n)
-
-
-static void new_localvar (LexState *ls, TString *name, int n) {
- FuncState *fs = ls->fs;
- luaY_checklimit(fs, fs->nactvar+n+1, LUAI_MAXVARS, "local variables");
- fs->actvar[fs->nactvar+n] = cast(unsigned short, registerlocalvar(ls, name));
-}
-
-
-static void adjustlocalvars (LexState *ls, int nvars) {
- FuncState *fs = ls->fs;
- fs->nactvar = cast_byte(fs->nactvar + nvars);
- for (; nvars; nvars--) {
- getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc;
- }
-}
-
-
-static void removevars (LexState *ls, int tolevel) {
- FuncState *fs = ls->fs;
- while (fs->nactvar > tolevel)
- getlocvar(fs, --fs->nactvar).endpc = fs->pc;
-}
-
-
-static int indexupvalue (FuncState *fs, TString *name, expdesc *v) {
- int i;
- Proto *f = fs->f;
- int oldsize = f->sizeupvalues;
- for (i=0; i<f->nups; i++) {
- if (fs->upvalues[i].k == v->k && fs->upvalues[i].info == v->u.s.info) {
- lua_assert(f->upvalues[i] == name);
- return i;
- }
- }
- /* new one */
- luaY_checklimit(fs, f->nups + 1, LUAI_MAXUPVALUES, "upvalues");
- luaM_growvector(fs->L, f->upvalues, f->nups, f->sizeupvalues,
- TString *, MAX_INT, "");
- while (oldsize < f->sizeupvalues) f->upvalues[oldsize++] = NULL;
- f->upvalues[f->nups] = name;
- luaC_objbarrier(fs->L, f, name);
- lua_assert(v->k == VLOCAL || v->k == VUPVAL);
- fs->upvalues[f->nups].k = cast_byte(v->k);
- fs->upvalues[f->nups].info = cast_byte(v->u.s.info);
- return f->nups++;
-}
-
-
-static int searchvar (FuncState *fs, TString *n) {
- int i;
- for (i=fs->nactvar-1; i >= 0; i--) {
- if (n == getlocvar(fs, i).varname)
- return i;
- }
- return -1; /* not found */
-}
-
-
-static void markupval (FuncState *fs, int level) {
- BlockCnt *bl = fs->bl;
- while (bl && bl->nactvar > level) bl = bl->previous;
- if (bl) bl->upval = 1;
-}
-
-
-static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
- if (fs == NULL) { /* no more levels? */
- init_exp(var, VGLOBAL, NO_REG); /* default is global variable */
- return VGLOBAL;
- }
- else {
- int v = searchvar(fs, n); /* look up at current level */
- if (v >= 0) {
- init_exp(var, VLOCAL, v);
- if (!base)
- markupval(fs, v); /* local will be used as an upval */
- return VLOCAL;
- }
- else { /* not found at current level; try upper one */
- if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL)
- return VGLOBAL;
- var->u.s.info = indexupvalue(fs, n, var); /* else was LOCAL or UPVAL */
- var->k = VUPVAL; /* upvalue in this level */
- return VUPVAL;
- }
- }
-}
-
-
-static void singlevar (LexState *ls, expdesc *var) {
- TString *varname = str_checkname(ls);
- FuncState *fs = ls->fs;
- if (singlevaraux(fs, varname, var, 1) == VGLOBAL)
- var->u.s.info = luaK_stringK(fs, varname); /* info points to global name */
-}
-
-
-static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) {
- FuncState *fs = ls->fs;
- int extra = nvars - nexps;
- if (hasmultret(e->k)) {
- extra++; /* includes call itself */
- if (extra < 0) extra = 0;
- luaK_setreturns(fs, e, extra); /* last exp. provides the difference */
- if (extra > 1) luaK_reserveregs(fs, extra-1);
- }
- else {
- if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */
- if (extra > 0) {
- int reg = fs->freereg;
- luaK_reserveregs(fs, extra);
- luaK_nil(fs, reg, extra);
- }
- }
-}
-
-
-static void enterlevel (LexState *ls) {
- if (++ls->L->nCcalls > LUAI_MAXCCALLS)
- luaX_lexerror(ls, "chunk has too many syntax levels", 0);
-}
-
-
-#define leavelevel(ls) ((ls)->L->nCcalls--)
-
-
-static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isbreakable) {
- bl->breaklist = NO_JUMP;
- bl->isbreakable = isbreakable;
- bl->nactvar = fs->nactvar;
- bl->upval = 0;
- bl->previous = fs->bl;
- fs->bl = bl;
- lua_assert(fs->freereg == fs->nactvar);
-}
-
-
-static void leaveblock (FuncState *fs) {
- BlockCnt *bl = fs->bl;
- fs->bl = bl->previous;
- removevars(fs->ls, bl->nactvar);
- if (bl->upval)
- luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
- /* a block either controls scope or breaks (never both) */
- lua_assert(!bl->isbreakable || !bl->upval);
- lua_assert(bl->nactvar == fs->nactvar);
- fs->freereg = fs->nactvar; /* free registers */
- luaK_patchtohere(fs, bl->breaklist);
-}
-
-
-static void pushclosure (LexState *ls, FuncState *func, expdesc *v) {
- FuncState *fs = ls->fs;
- Proto *f = fs->f;
- int oldsize = f->sizep;
- int i;
- luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *,
- MAXARG_Bx, "constant table overflow");
- while (oldsize < f->sizep) f->p[oldsize++] = NULL;
- f->p[fs->np++] = func->f;
- luaC_objbarrier(ls->L, f, func->f);
- init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1));
- for (i=0; i<func->f->nups; i++) {
- OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;
- luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0);
- }
-}
-
-
-static void open_func (LexState *ls, FuncState *fs) {
- lua_State *L = ls->L;
- Proto *f = luaF_newproto(L);
- fs->f = f;
- fs->prev = ls->fs; /* linked list of funcstates */
- fs->ls = ls;
- fs->L = L;
- ls->fs = fs;
- fs->pc = 0;
- fs->lasttarget = -1;
- fs->jpc = NO_JUMP;
- fs->freereg = 0;
- fs->nk = 0;
- fs->np = 0;
- fs->nlocvars = 0;
- fs->nactvar = 0;
- fs->bl = NULL;
- f->source = ls->source;
- f->maxstacksize = 2; /* registers 0/1 are always valid */
- fs->h = luaH_new(L, 0, 0);
- /* anchor table of constants and prototype (to avoid being collected) */
- sethvalue2s(L, L->top, fs->h);
- incr_top(L);
- setptvalue2s(L, L->top, f);
- incr_top(L);
-}
-
-
-static void close_func (LexState *ls) {
- lua_State *L = ls->L;
- FuncState *fs = ls->fs;
- Proto *f = fs->f;
- removevars(ls, 0);
- luaK_ret(fs, 0, 0); /* final return */
- luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);
- f->sizecode = fs->pc;
- luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int);
- f->sizelineinfo = fs->pc;
- luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue);
- f->sizek = fs->nk;
- luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *);
- f->sizep = fs->np;
- luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar);
- f->sizelocvars = fs->nlocvars;
- luaM_reallocvector(L, f->upvalues, f->sizeupvalues, f->nups, TString *);
- f->sizeupvalues = f->nups;
- lua_assert(luaG_checkcode(f));
- lua_assert(fs->bl == NULL);
- ls->fs = fs->prev;
- L->top -= 2; /* remove table and prototype from the stack */
- /* last token read was anchored in defunct function; must reanchor it */
- if (fs) anchor_token(ls);
-}
-
-
-Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) {
- struct LexState lexstate;
- struct FuncState funcstate;
- lexstate.buff = buff;
- luaX_setinput(L, &lexstate, z, luaS_new(L, name));
- open_func(&lexstate, &funcstate);
- funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */
- luaX_next(&lexstate); /* read first token */
- chunk(&lexstate);
- check(&lexstate, TK_EOS);
- close_func(&lexstate);
- lua_assert(funcstate.prev == NULL);
- lua_assert(funcstate.f->nups == 0);
- lua_assert(lexstate.fs == NULL);
- return funcstate.f;
-}
-
-
-
-/*============================================================*/
-/* GRAMMAR RULES */
-/*============================================================*/
-
-
-static void field (LexState *ls, expdesc *v) {
- /* field -> ['.' | ':'] NAME */
- FuncState *fs = ls->fs;
- expdesc key;
- luaK_exp2anyreg(fs, v);
- luaX_next(ls); /* skip the dot or colon */
- checkname(ls, &key);
- luaK_indexed(fs, v, &key);
-}
-
-
-static void yindex (LexState *ls, expdesc *v) {
- /* index -> '[' expr ']' */
- luaX_next(ls); /* skip the '[' */
- expr(ls, v);
- luaK_exp2val(ls->fs, v);
- checknext(ls, ']');
-}
-
-
-/*
-** {======================================================================
-** Rules for Constructors
-** =======================================================================
-*/
-
-
-struct ConsControl {
- expdesc v; /* last list item read */
- expdesc *t; /* table descriptor */
- int nh; /* total number of `record' elements */
- int na; /* total number of array elements */
- int tostore; /* number of array elements pending to be stored */
-};
-
-
-static void recfield (LexState *ls, struct ConsControl *cc) {
- /* recfield -> (NAME | `['exp1`]') = exp1 */
- FuncState *fs = ls->fs;
- int reg = ls->fs->freereg;
- expdesc key, val;
- int rkkey;
- if (ls->t.token == TK_NAME) {
- luaY_checklimit(fs, cc->nh, MAX_INT, "items in a constructor");
- checkname(ls, &key);
- }
- else /* ls->t.token == '[' */
- yindex(ls, &key);
- cc->nh++;
- checknext(ls, '=');
- rkkey = luaK_exp2RK(fs, &key);
- expr(ls, &val);
- luaK_codeABC(fs, OP_SETTABLE, cc->t->u.s.info, rkkey, luaK_exp2RK(fs, &val));
- fs->freereg = reg; /* free registers */
-}
-
-
-static void closelistfield (FuncState *fs, struct ConsControl *cc) {
- if (cc->v.k == VVOID) return; /* there is no list item */
- luaK_exp2nextreg(fs, &cc->v);
- cc->v.k = VVOID;
- if (cc->tostore == LFIELDS_PER_FLUSH) {
- luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); /* flush */
- cc->tostore = 0; /* no more items pending */
- }
-}
-
-
-static void lastlistfield (FuncState *fs, struct ConsControl *cc) {
- if (cc->tostore == 0) return;
- if (hasmultret(cc->v.k)) {
- luaK_setmultret(fs, &cc->v);
- luaK_setlist(fs, cc->t->u.s.info, cc->na, LUA_MULTRET);
- cc->na--; /* do not count last expression (unknown number of elements) */
- }
- else {
- if (cc->v.k != VVOID)
- luaK_exp2nextreg(fs, &cc->v);
- luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore);
- }
-}
-
-
-static void listfield (LexState *ls, struct ConsControl *cc) {
- expr(ls, &cc->v);
- luaY_checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor");
- cc->na++;
- cc->tostore++;
-}
-
-
-static void constructor (LexState *ls, expdesc *t) {
- /* constructor -> ?? */
- FuncState *fs = ls->fs;
- int line = ls->linenumber;
- int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);
- struct ConsControl cc;
- cc.na = cc.nh = cc.tostore = 0;
- cc.t = t;
- init_exp(t, VRELOCABLE, pc);
- init_exp(&cc.v, VVOID, 0); /* no value (yet) */
- luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */
- checknext(ls, '{');
- do {
- lua_assert(cc.v.k == VVOID || cc.tostore > 0);
- if (ls->t.token == '}') break;
- closelistfield(fs, &cc);
- switch(ls->t.token) {
- case TK_NAME: { /* may be listfields or recfields */
- luaX_lookahead(ls);
- if (ls->lookahead.token != '=') /* expression? */
- listfield(ls, &cc);
- else
- recfield(ls, &cc);
- break;
- }
- case '[': { /* constructor_item -> recfield */
- recfield(ls, &cc);
- break;
- }
- default: { /* constructor_part -> listfield */
- listfield(ls, &cc);
- break;
- }
- }
- } while (testnext(ls, ',') || testnext(ls, ';'));
- check_match(ls, '}', '{', line);
- lastlistfield(fs, &cc);
- SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */
- SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */
-}
-
-/* }====================================================================== */
-
-
-
-static void parlist (LexState *ls) {
- /* parlist -> [ param { `,' param } ] */
- FuncState *fs = ls->fs;
- Proto *f = fs->f;
- int nparams = 0;
- f->is_vararg = 0;
- if (ls->t.token != ')') { /* is `parlist' not empty? */
- do {
- switch (ls->t.token) {
- case TK_NAME: { /* param -> NAME */
- new_localvar(ls, str_checkname(ls), nparams++);
- break;
- }
- case TK_DOTS: { /* param -> `...' */
- luaX_next(ls);
-#if defined(LUA_COMPAT_VARARG)
- /* use `arg' as default name */
- new_localvarliteral(ls, "arg", nparams++);
- f->is_vararg = VARARG_HASARG | VARARG_NEEDSARG;
-#endif
- f->is_vararg |= VARARG_ISVARARG;
- break;
- }
- default: luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected");
- }
- } while (!f->is_vararg && testnext(ls, ','));
- }
- adjustlocalvars(ls, nparams);
- f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG));
- luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */
-}
-
-
-static void body (LexState *ls, expdesc *e, int needself, int line) {
- /* body -> `(' parlist `)' chunk END */
- FuncState new_fs;
- open_func(ls, &new_fs);
- new_fs.f->linedefined = line;
- checknext(ls, '(');
- if (needself) {
- new_localvarliteral(ls, "self", 0);
- adjustlocalvars(ls, 1);
- }
- parlist(ls);
- checknext(ls, ')');
- chunk(ls);
- new_fs.f->lastlinedefined = ls->linenumber;
- check_match(ls, TK_END, TK_FUNCTION, line);
- close_func(ls);
- pushclosure(ls, &new_fs, e);
-}
-
-
-static int explist1 (LexState *ls, expdesc *v) {
- /* explist1 -> expr { `,' expr } */
- int n = 1; /* at least one expression */
- expr(ls, v);
- while (testnext(ls, ',')) {
- luaK_exp2nextreg(ls->fs, v);
- expr(ls, v);
- n++;
- }
- return n;
-}
-
-
-static void funcargs (LexState *ls, expdesc *f) {
- FuncState *fs = ls->fs;
- expdesc args;
- int base, nparams;
- int line = ls->linenumber;
- switch (ls->t.token) {
- case '(': { /* funcargs -> `(' [ explist1 ] `)' */
- if (line != ls->lastline)
- luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)");
- luaX_next(ls);
- if (ls->t.token == ')') /* arg list is empty? */
- args.k = VVOID;
- else {
- explist1(ls, &args);
- luaK_setmultret(fs, &args);
- }
- check_match(ls, ')', '(', line);
- break;
- }
- case '{': { /* funcargs -> constructor */
- constructor(ls, &args);
- break;
- }
- case TK_STRING: { /* funcargs -> STRING */
- codestring(ls, &args, ls->t.seminfo.ts);
- luaX_next(ls); /* must use `seminfo' before `next' */
- break;
- }
- default: {
- luaX_syntaxerror(ls, "function arguments expected");
- return;
- }
- }
- lua_assert(f->k == VNONRELOC);
- base = f->u.s.info; /* base register for call */
- if (hasmultret(args.k))
- nparams = LUA_MULTRET; /* open call */
- else {
- if (args.k != VVOID)
- luaK_exp2nextreg(fs, &args); /* close last argument */
- nparams = fs->freereg - (base+1);
- }
- init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2));
- luaK_fixline(fs, line);
- fs->freereg = base+1; /* call remove function and arguments and leaves
- (unless changed) one result */
-}
-
-
-
-
-/*
-** {======================================================================
-** Expression parsing
-** =======================================================================
-*/
-
-
-static void prefixexp (LexState *ls, expdesc *v) {
- /* prefixexp -> NAME | '(' expr ')' */
- switch (ls->t.token) {
- case '(': {
- int line = ls->linenumber;
- luaX_next(ls);
- expr(ls, v);
- check_match(ls, ')', '(', line);
- luaK_dischargevars(ls->fs, v);
- return;
- }
- case TK_NAME: {
- singlevar(ls, v);
- return;
- }
- default: {
- luaX_syntaxerror(ls, "unexpected symbol");
- return;
- }
- }
-}
-
-
-static void primaryexp (LexState *ls, expdesc *v) {
- /* primaryexp ->
- prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */
- FuncState *fs = ls->fs;
- prefixexp(ls, v);
- for (;;) {
- switch (ls->t.token) {
- case '.': { /* field */
- field(ls, v);
- break;
- }
- case '[': { /* `[' exp1 `]' */
- expdesc key;
- luaK_exp2anyreg(fs, v);
- yindex(ls, &key);
- luaK_indexed(fs, v, &key);
- break;
- }
- case ':': { /* `:' NAME funcargs */
- expdesc key;
- luaX_next(ls);
- checkname(ls, &key);
- luaK_self(fs, v, &key);
- funcargs(ls, v);
- break;
- }
- case '(': case TK_STRING: case '{': { /* funcargs */
- luaK_exp2nextreg(fs, v);
- funcargs(ls, v);
- break;
- }
- default: return;
- }
- }
-}
-
-
-static void simpleexp (LexState *ls, expdesc *v) {
- /* simpleexp -> NUMBER | STRING | NIL | true | false | ... |
- constructor | FUNCTION body | primaryexp */
- switch (ls->t.token) {
- case TK_NUMBER: {
- init_exp(v, VKNUM, 0);
- v->u.nval = ls->t.seminfo.r;
- break;
- }
- case TK_STRING: {
- codestring(ls, v, ls->t.seminfo.ts);
- break;
- }
- case TK_NIL: {
- init_exp(v, VNIL, 0);
- break;
- }
- case TK_TRUE: {
- init_exp(v, VTRUE, 0);
- break;
- }
- case TK_FALSE: {
- init_exp(v, VFALSE, 0);
- break;
- }
- case TK_DOTS: { /* vararg */
- FuncState *fs = ls->fs;
- check_condition(ls, fs->f->is_vararg,
- "cannot use " LUA_QL("...") " outside a vararg function");
- fs->f->is_vararg &= ~VARARG_NEEDSARG; /* don't need 'arg' */
- init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0));
- break;
- }
- case '{': { /* constructor */
- constructor(ls, v);
- return;
- }
- case TK_FUNCTION: {
- luaX_next(ls);
- body(ls, v, 0, ls->linenumber);
- return;
- }
- default: {
- primaryexp(ls, v);
- return;
- }
- }
- luaX_next(ls);
-}
-
-
-static UnOpr getunopr (int op) {
- switch (op) {
- case TK_NOT: return OPR_NOT;
- case '-': return OPR_MINUS;
- case '#': return OPR_LEN;
- default: return OPR_NOUNOPR;
- }
-}
-
-
-static BinOpr getbinopr (int op) {
- switch (op) {
- case '+': return OPR_ADD;
- case '-': return OPR_SUB;
- case '*': return OPR_MUL;
- case '/': return OPR_DIV;
- case '%': return OPR_MOD;
- case '^': return OPR_POW;
- case TK_CONCAT: return OPR_CONCAT;
- case TK_NE: return OPR_NE;
- case TK_EQ: return OPR_EQ;
- case '<': return OPR_LT;
- case TK_LE: return OPR_LE;
- case '>': return OPR_GT;
- case TK_GE: return OPR_GE;
- case TK_AND: return OPR_AND;
- case TK_OR: return OPR_OR;
- default: return OPR_NOBINOPR;
- }
-}
-
-
-static const struct {
- lu_byte left; /* left priority for each binary operator */
- lu_byte right; /* right priority */
-} priority[] = { /* ORDER OPR */
- {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, /* `+' `-' `/' `%' */
- {10, 9}, {5, 4}, /* power and concat (right associative) */
- {3, 3}, {3, 3}, /* equality and inequality */
- {3, 3}, {3, 3}, {3, 3}, {3, 3}, /* order */
- {2, 2}, {1, 1} /* logical (and/or) */
-};
-
-#define UNARY_PRIORITY 8 /* priority for unary operators */
-
-
-/*
-** subexpr -> (simpleexp | unop subexpr) { binop subexpr }
-** where `binop' is any binary operator with a priority higher than `limit'
-*/
-static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) {
- BinOpr op;
- UnOpr uop;
- enterlevel(ls);
- uop = getunopr(ls->t.token);
- if (uop != OPR_NOUNOPR) {
- luaX_next(ls);
- subexpr(ls, v, UNARY_PRIORITY);
- luaK_prefix(ls->fs, uop, v);
- }
- else simpleexp(ls, v);
- /* expand while operators have priorities higher than `limit' */
- op = getbinopr(ls->t.token);
- while (op != OPR_NOBINOPR && priority[op].left > limit) {
- expdesc v2;
- BinOpr nextop;
- luaX_next(ls);
- luaK_infix(ls->fs, op, v);
- /* read sub-expression with higher priority */
- nextop = subexpr(ls, &v2, priority[op].right);
- luaK_posfix(ls->fs, op, v, &v2);
- op = nextop;
- }
- leavelevel(ls);
- return op; /* return first untreated operator */
-}
-
-
-static void expr (LexState *ls, expdesc *v) {
- subexpr(ls, v, 0);
-}
-
-/* }==================================================================== */
-
-
-
-/*
-** {======================================================================
-** Rules for Statements
-** =======================================================================
-*/
-
-
-static int block_follow (int token) {
- switch (token) {
- case TK_ELSE: case TK_ELSEIF: case TK_END:
- case TK_UNTIL: case TK_EOS:
- return 1;
- default: return 0;
- }
-}
-
-
-static void block (LexState *ls) {
- /* block -> chunk */
- FuncState *fs = ls->fs;
- BlockCnt bl;
- enterblock(fs, &bl, 0);
- chunk(ls);
- lua_assert(bl.breaklist == NO_JUMP);
- leaveblock(fs);
-}
-
-
-/*
-** structure to chain all variables in the left-hand side of an
-** assignment
-*/
-struct LHS_assign {
- struct LHS_assign *prev;
- expdesc v; /* variable (global, local, upvalue, or indexed) */
-};
-
-
-/*
-** check whether, in an assignment to a local variable, the local variable
-** is needed in a previous assignment (to a table). If so, save original
-** local value in a safe place and use this safe copy in the previous
-** assignment.
-*/
-static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
- FuncState *fs = ls->fs;
- int extra = fs->freereg; /* eventual position to save local variable */
- int conflict = 0;
- for (; lh; lh = lh->prev) {
- if (lh->v.k == VINDEXED) {
- if (lh->v.u.s.info == v->u.s.info) { /* conflict? */
- conflict = 1;
- lh->v.u.s.info = extra; /* previous assignment will use safe copy */
- }
- if (lh->v.u.s.aux == v->u.s.info) { /* conflict? */
- conflict = 1;
- lh->v.u.s.aux = extra; /* previous assignment will use safe copy */
- }
- }
- }
- if (conflict) {
- luaK_codeABC(fs, OP_MOVE, fs->freereg, v->u.s.info, 0); /* make copy */
- luaK_reserveregs(fs, 1);
- }
-}
-
-
-static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
- expdesc e;
- check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED,
- "syntax error");
- if (testnext(ls, ',')) { /* assignment -> `,' primaryexp assignment */
- struct LHS_assign nv;
- nv.prev = lh;
- primaryexp(ls, &nv.v);
- if (nv.v.k == VLOCAL)
- check_conflict(ls, lh, &nv.v);
- luaY_checklimit(ls->fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls,
- "variables in assignment");
- assignment(ls, &nv, nvars+1);
- }
- else { /* assignment -> `=' explist1 */
- int nexps;
- checknext(ls, '=');
- nexps = explist1(ls, &e);
- if (nexps != nvars) {
- adjust_assign(ls, nvars, nexps, &e);
- if (nexps > nvars)
- ls->fs->freereg -= nexps - nvars; /* remove extra values */
- }
- else {
- luaK_setoneret(ls->fs, &e); /* close last expression */
- luaK_storevar(ls->fs, &lh->v, &e);
- return; /* avoid default */
- }
- }
- init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */
- luaK_storevar(ls->fs, &lh->v, &e);
-}
-
-
-static int cond (LexState *ls) {
- /* cond -> exp */
- expdesc v;
- expr(ls, &v); /* read condition */
- if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */
- luaK_goiftrue(ls->fs, &v);
- return v.f;
-}
-
-
-static void breakstat (LexState *ls) {
- FuncState *fs = ls->fs;
- BlockCnt *bl = fs->bl;
- int upval = 0;
- while (bl && !bl->isbreakable) {
- upval |= bl->upval;
- bl = bl->previous;
- }
- if (!bl)
- luaX_syntaxerror(ls, "no loop to break");
- if (upval)
- luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
- luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
-}
-
-
-static void whilestat (LexState *ls, int line) {
- /* whilestat -> WHILE cond DO block END */
- FuncState *fs = ls->fs;
- int whileinit;
- int condexit;
- BlockCnt bl;
- luaX_next(ls); /* skip WHILE */
- whileinit = luaK_getlabel(fs);
- condexit = cond(ls);
- enterblock(fs, &bl, 1);
- checknext(ls, TK_DO);
- block(ls);
- luaK_patchlist(fs, luaK_jump(fs), whileinit);
- check_match(ls, TK_END, TK_WHILE, line);
- leaveblock(fs);
- luaK_patchtohere(fs, condexit); /* false conditions finish the loop */
-}
-
-
-static void repeatstat (LexState *ls, int line) {
- /* repeatstat -> REPEAT block UNTIL cond */
- int condexit;
- FuncState *fs = ls->fs;
- int repeat_init = luaK_getlabel(fs);
- BlockCnt bl1, bl2;
- enterblock(fs, &bl1, 1); /* loop block */
- enterblock(fs, &bl2, 0); /* scope block */
- luaX_next(ls); /* skip REPEAT */
- chunk(ls);
- check_match(ls, TK_UNTIL, TK_REPEAT, line);
- condexit = cond(ls); /* read condition (inside scope block) */
- if (!bl2.upval) { /* no upvalues? */
- leaveblock(fs); /* finish scope */
- luaK_patchlist(ls->fs, condexit, repeat_init); /* close the loop */
- }
- else { /* complete semantics when there are upvalues */
- breakstat(ls); /* if condition then break */
- luaK_patchtohere(ls->fs, condexit); /* else... */
- leaveblock(fs); /* finish scope... */
- luaK_patchlist(ls->fs, luaK_jump(fs), repeat_init); /* and repeat */
- }
- leaveblock(fs); /* finish loop */
-}
-
-
-static int exp1 (LexState *ls) {
- expdesc e;
- int k;
- expr(ls, &e);
- k = e.k;
- luaK_exp2nextreg(ls->fs, &e);
- return k;
-}
-
-
-static void forbody (LexState *ls, int base, int line, int nvars, int isnum) {
- /* forbody -> DO block */
- BlockCnt bl;
- FuncState *fs = ls->fs;
- int prep, endfor;
- adjustlocalvars(ls, 3); /* control variables */
- checknext(ls, TK_DO);
- prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs);
- enterblock(fs, &bl, 0); /* scope for declared variables */
- adjustlocalvars(ls, nvars);
- luaK_reserveregs(fs, nvars);
- block(ls);
- leaveblock(fs); /* end of scope for declared variables */
- luaK_patchtohere(fs, prep);
- endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) :
- luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars);
- luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */
- luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1);
-}
-
-
-static void fornum (LexState *ls, TString *varname, int line) {
- /* fornum -> NAME = exp1,exp1[,exp1] forbody */
- FuncState *fs = ls->fs;
- int base = fs->freereg;
- new_localvarliteral(ls, "(for index)", 0);
- new_localvarliteral(ls, "(for limit)", 1);
- new_localvarliteral(ls, "(for step)", 2);
- new_localvar(ls, varname, 3);
- checknext(ls, '=');
- exp1(ls); /* initial value */
- checknext(ls, ',');
- exp1(ls); /* limit */
- if (testnext(ls, ','))
- exp1(ls); /* optional step */
- else { /* default step = 1 */
- luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
- luaK_reserveregs(fs, 1);
- }
- forbody(ls, base, line, 1, 1);
-}
-
-
-static void forlist (LexState *ls, TString *indexname) {
- /* forlist -> NAME {,NAME} IN explist1 forbody */
- FuncState *fs = ls->fs;
- expdesc e;
- int nvars = 0;
- int line;
- int base = fs->freereg;
- /* create control variables */
- new_localvarliteral(ls, "(for generator)", nvars++);
- new_localvarliteral(ls, "(for state)", nvars++);
- new_localvarliteral(ls, "(for control)", nvars++);
- /* create declared variables */
- new_localvar(ls, indexname, nvars++);
- while (testnext(ls, ','))
- new_localvar(ls, str_checkname(ls), nvars++);
- checknext(ls, TK_IN);
- line = ls->linenumber;
- adjust_assign(ls, 3, explist1(ls, &e), &e);
- luaK_checkstack(fs, 3); /* extra space to call generator */
- forbody(ls, base, line, nvars - 3, 0);
-}
-
-
-static void forstat (LexState *ls, int line) {
- /* forstat -> FOR (fornum | forlist) END */
- FuncState *fs = ls->fs;
- TString *varname;
- BlockCnt bl;
- enterblock(fs, &bl, 1); /* scope for loop and control variables */
- luaX_next(ls); /* skip `for' */
- varname = str_checkname(ls); /* first variable name */
- switch (ls->t.token) {
- case '=': fornum(ls, varname, line); break;
- case ',': case TK_IN: forlist(ls, varname); break;
- default: luaX_syntaxerror(ls, LUA_QL("=") " or " LUA_QL("in") " expected");
- }
- check_match(ls, TK_END, TK_FOR, line);
- leaveblock(fs); /* loop scope (`break' jumps to this point) */
-}
-
-
-static int test_then_block (LexState *ls) {
- /* test_then_block -> [IF | ELSEIF] cond THEN block */
- int condexit;
- luaX_next(ls); /* skip IF or ELSEIF */
- condexit = cond(ls);
- checknext(ls, TK_THEN);
- block(ls); /* `then' part */
- return condexit;
-}
-
-
-static void ifstat (LexState *ls, int line) {
- /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
- FuncState *fs = ls->fs;
- int flist;
- int escapelist = NO_JUMP;
- flist = test_then_block(ls); /* IF cond THEN block */
- while (ls->t.token == TK_ELSEIF) {
- luaK_concat(fs, &escapelist, luaK_jump(fs));
- luaK_patchtohere(fs, flist);
- flist = test_then_block(ls); /* ELSEIF cond THEN block */
- }
- if (ls->t.token == TK_ELSE) {
- luaK_concat(fs, &escapelist, luaK_jump(fs));
- luaK_patchtohere(fs, flist);
- luaX_next(ls); /* skip ELSE (after patch, for correct line info) */
- block(ls); /* `else' part */
- }
- else
- luaK_concat(fs, &escapelist, flist);
- luaK_patchtohere(fs, escapelist);
- check_match(ls, TK_END, TK_IF, line);
-}
-
-
-static void localfunc (LexState *ls) {
- expdesc v, b;
- FuncState *fs = ls->fs;
- new_localvar(ls, str_checkname(ls), 0);
- init_exp(&v, VLOCAL, fs->freereg);
- luaK_reserveregs(fs, 1);
- adjustlocalvars(ls, 1);
- body(ls, &b, 0, ls->linenumber);
- luaK_storevar(fs, &v, &b);
- /* debug information will only see the variable after this point! */
- getlocvar(fs, fs->nactvar - 1).startpc = fs->pc;
-}
-
-
-static void localstat (LexState *ls) {
- /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */
- int nvars = 0;
- int nexps;
- expdesc e;
- do {
- new_localvar(ls, str_checkname(ls), nvars++);
- } while (testnext(ls, ','));
- if (testnext(ls, '='))
- nexps = explist1(ls, &e);
- else {
- e.k = VVOID;
- nexps = 0;
- }
- adjust_assign(ls, nvars, nexps, &e);
- adjustlocalvars(ls, nvars);
-}
-
-
-static int funcname (LexState *ls, expdesc *v) {
- /* funcname -> NAME {field} [`:' NAME] */
- int needself = 0;
- singlevar(ls, v);
- while (ls->t.token == '.')
- field(ls, v);
- if (ls->t.token == ':') {
- needself = 1;
- field(ls, v);
- }
- return needself;
-}
-
-
-static void funcstat (LexState *ls, int line) {
- /* funcstat -> FUNCTION funcname body */
- int needself;
- expdesc v, b;
- luaX_next(ls); /* skip FUNCTION */
- needself = funcname(ls, &v);
- body(ls, &b, needself, line);
- luaK_storevar(ls->fs, &v, &b);
- luaK_fixline(ls->fs, line); /* definition `happens' in the first line */
-}
-
-
-static void exprstat (LexState *ls) {
- /* stat -> func | assignment */
- FuncState *fs = ls->fs;
- struct LHS_assign v;
- primaryexp(ls, &v.v);
- if (v.v.k == VCALL) /* stat -> func */
- SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */
- else { /* stat -> assignment */
- v.prev = NULL;
- assignment(ls, &v, 1);
- }
-}
-
-
-static void retstat (LexState *ls) {
- /* stat -> RETURN explist */
- FuncState *fs = ls->fs;
- expdesc e;
- int first, nret; /* registers with returned values */
- luaX_next(ls); /* skip RETURN */
- if (block_follow(ls->t.token) || ls->t.token == ';')
- first = nret = 0; /* return no values */
- else {
- nret = explist1(ls, &e); /* optional return values */
- if (hasmultret(e.k)) {
- luaK_setmultret(fs, &e);
- if (e.k == VCALL && nret == 1) { /* tail call? */
- SET_OPCODE(getcode(fs,&e), OP_TAILCALL);
- lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar);
- }
- first = fs->nactvar;
- nret = LUA_MULTRET; /* return all values */
- }
- else {
- if (nret == 1) /* only one single value? */
- first = luaK_exp2anyreg(fs, &e);
- else {
- luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */
- first = fs->nactvar; /* return all `active' values */
- lua_assert(nret == fs->freereg - first);
- }
- }
- }
- luaK_ret(fs, first, nret);
-}
-
-
-static int statement (LexState *ls) {
- int line = ls->linenumber; /* may be needed for error messages */
- switch (ls->t.token) {
- case TK_IF: { /* stat -> ifstat */
- ifstat(ls, line);
- return 0;
- }
- case TK_WHILE: { /* stat -> whilestat */
- whilestat(ls, line);
- return 0;
- }
- case TK_DO: { /* stat -> DO block END */
- luaX_next(ls); /* skip DO */
- block(ls);
- check_match(ls, TK_END, TK_DO, line);
- return 0;
- }
- case TK_FOR: { /* stat -> forstat */
- forstat(ls, line);
- return 0;
- }
- case TK_REPEAT: { /* stat -> repeatstat */
- repeatstat(ls, line);
- return 0;
- }
- case TK_FUNCTION: {
- funcstat(ls, line); /* stat -> funcstat */
- return 0;
- }
- case TK_LOCAL: { /* stat -> localstat */
- luaX_next(ls); /* skip LOCAL */
- if (testnext(ls, TK_FUNCTION)) /* local function? */
- localfunc(ls);
- else
- localstat(ls);
- return 0;
- }
- case TK_RETURN: { /* stat -> retstat */
- retstat(ls);
- return 1; /* must be last statement */
- }
- case TK_BREAK: { /* stat -> breakstat */
- luaX_next(ls); /* skip BREAK */
- breakstat(ls);
- return 1; /* must be last statement */
- }
- default: {
- exprstat(ls);
- return 0; /* to avoid warnings */
- }
- }
-}
-
-
-static void chunk (LexState *ls) {
- /* chunk -> { stat [`;'] } */
- int islast = 0;
- enterlevel(ls);
- while (!islast && !block_follow(ls->t.token)) {
- islast = statement(ls);
- testnext(ls, ';');
- lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg &&
- ls->fs->freereg >= ls->fs->nactvar);
- ls->fs->freereg = ls->fs->nactvar; /* free registers */
- }
- leavelevel(ls);
-}
-
-/* }====================================================================== */
diff --git a/src/lua/src/lparser.h b/src/lua/src/lparser.h
deleted file mode 100644
index 18836afd1..000000000
--- a/src/lua/src/lparser.h
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
-** $Id: lparser.h,v 1.57.1.1 2007/12/27 13:02:25 roberto Exp $
-** Lua Parser
-** See Copyright Notice in lua.h
-*/
-
-#ifndef lparser_h
-#define lparser_h
-
-#include "llimits.h"
-#include "lobject.h"
-#include "lzio.h"
-
-
-/*
-** Expression descriptor
-*/
-
-typedef enum {
- VVOID, /* no value */
- VNIL,
- VTRUE,
- VFALSE,
- VK, /* info = index of constant in `k' */
- VKNUM, /* nval = numerical value */
- VLOCAL, /* info = local register */
- VUPVAL, /* info = index of upvalue in `upvalues' */
- VGLOBAL, /* info = index of table; aux = index of global name in `k' */
- VINDEXED, /* info = table register; aux = index register (or `k') */
- VJMP, /* info = instruction pc */
- VRELOCABLE, /* info = instruction pc */
- VNONRELOC, /* info = result register */
- VCALL, /* info = instruction pc */
- VVARARG /* info = instruction pc */
-} expkind;
-
-typedef struct expdesc {
- expkind k;
- union {
- struct { int info, aux; } s;
- lua_Number nval;
- } u;
- int t; /* patch list of `exit when true' */
- int f; /* patch list of `exit when false' */
-} expdesc;
-
-
-typedef struct upvaldesc {
- lu_byte k;
- lu_byte info;
-} upvaldesc;
-
-
-struct BlockCnt; /* defined in lparser.c */
-
-
-/* state needed to generate code for a given function */
-typedef struct FuncState {
- Proto *f; /* current function header */
- Table *h; /* table to find (and reuse) elements in `k' */
- struct FuncState *prev; /* enclosing function */
- struct LexState *ls; /* lexical state */
- struct lua_State *L; /* copy of the Lua state */
- struct BlockCnt *bl; /* chain of current blocks */
- int pc; /* next position to code (equivalent to `ncode') */
- int lasttarget; /* `pc' of last `jump target' */
- int jpc; /* list of pending jumps to `pc' */
- int freereg; /* first free register */
- int nk; /* number of elements in `k' */
- int np; /* number of elements in `p' */
- short nlocvars; /* number of elements in `locvars' */
- lu_byte nactvar; /* number of active local variables */
- upvaldesc upvalues[LUAI_MAXUPVALUES]; /* upvalues */
- unsigned short actvar[LUAI_MAXVARS]; /* declared-variable stack */
-} FuncState;
-
-
-LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
- const char *name);
-
-
-#endif
diff --git a/src/lua/src/lstate.c b/src/lua/src/lstate.c
deleted file mode 100644
index 4313b83a0..000000000
--- a/src/lua/src/lstate.c
+++ /dev/null
@@ -1,214 +0,0 @@
-/*
-** $Id: lstate.c,v 2.36.1.2 2008/01/03 15:20:39 roberto Exp $
-** Global State
-** See Copyright Notice in lua.h
-*/
-
-
-#include <stddef.h>
-
-#define lstate_c
-#define LUA_CORE
-
-#include "lua.h"
-
-#include "ldebug.h"
-#include "ldo.h"
-#include "lfunc.h"
-#include "lgc.h"
-#include "llex.h"
-#include "lmem.h"
-#include "lstate.h"
-#include "lstring.h"
-#include "ltable.h"
-#include "ltm.h"
-
-
-#define state_size(x) (sizeof(x) + LUAI_EXTRASPACE)
-#define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE)
-#define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE))
-
-
-/*
-** Main thread combines a thread state and the global state
-*/
-typedef struct LG {
- lua_State l;
- global_State g;
-} LG;
-
-
-
-static void stack_init (lua_State *L1, lua_State *L) {
- /* initialize CallInfo array */
- L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo);
- L1->ci = L1->base_ci;
- L1->size_ci = BASIC_CI_SIZE;
- L1->end_ci = L1->base_ci + L1->size_ci - 1;
- /* initialize stack array */
- L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue);
- L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK;
- L1->top = L1->stack;
- L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1;
- /* initialize first ci */
- L1->ci->func = L1->top;
- setnilvalue(L1->top++); /* `function' entry for this `ci' */
- L1->base = L1->ci->base = L1->top;
- L1->ci->top = L1->top + LUA_MINSTACK;
-}
-
-
-static void freestack (lua_State *L, lua_State *L1) {
- luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo);
- luaM_freearray(L, L1->stack, L1->stacksize, TValue);
-}
-
-
-/*
-** open parts that may cause memory-allocation errors
-*/
-static void f_luaopen (lua_State *L, void *ud) {
- global_State *g = G(L);
- UNUSED(ud);
- stack_init(L, L); /* init stack */
- sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */
- sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */
- luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
- luaT_init(L);
- luaX_init(L);
- luaS_fix(luaS_newliteral(L, MEMERRMSG));
- g->GCthreshold = 4*g->totalbytes;
-}
-
-
-static void preinit_state (lua_State *L, global_State *g) {
- G(L) = g;
- L->stack = NULL;
- L->stacksize = 0;
- L->errorJmp = NULL;
- L->hook = NULL;
- L->hookmask = 0;
- L->basehookcount = 0;
- L->allowhook = 1;
- resethookcount(L);
- L->openupval = NULL;
- L->size_ci = 0;
- L->nCcalls = L->baseCcalls = 0;
- L->status = 0;
- L->base_ci = L->ci = NULL;
- L->savedpc = NULL;
- L->errfunc = 0;
- setnilvalue(gt(L));
-}
-
-
-static void close_state (lua_State *L) {
- global_State *g = G(L);
- luaF_close(L, L->stack); /* close all upvalues for this thread */
- luaC_freeall(L); /* collect all objects */
- lua_assert(g->rootgc == obj2gco(L));
- lua_assert(g->strt.nuse == 0);
- luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
- luaZ_freebuffer(L, &g->buff);
- freestack(L, L);
- lua_assert(g->totalbytes == sizeof(LG));
- (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0);
-}
-
-
-lua_State *luaE_newthread (lua_State *L) {
- lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State)));
- luaC_link(L, obj2gco(L1), LUA_TTHREAD);
- preinit_state(L1, G(L));
- stack_init(L1, L); /* init stack */
- setobj2n(L, gt(L1), gt(L)); /* share table of globals */
- L1->hookmask = L->hookmask;
- L1->basehookcount = L->basehookcount;
- L1->hook = L->hook;
- resethookcount(L1);
- lua_assert(iswhite(obj2gco(L1)));
- return L1;
-}
-
-
-void luaE_freethread (lua_State *L, lua_State *L1) {
- luaF_close(L1, L1->stack); /* close all upvalues for this thread */
- lua_assert(L1->openupval == NULL);
- luai_userstatefree(L1);
- freestack(L, L1);
- luaM_freemem(L, fromstate(L1), state_size(lua_State));
-}
-
-
-LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
- int i;
- lua_State *L;
- global_State *g;
- void *l = (*f)(ud, NULL, 0, state_size(LG));
- if (l == NULL) return NULL;
- L = tostate(l);
- g = &((LG *)L)->g;
- L->next = NULL;
- L->tt = LUA_TTHREAD;
- g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
- L->marked = luaC_white(g);
- set2bits(L->marked, FIXEDBIT, SFIXEDBIT);
- preinit_state(L, g);
- g->frealloc = f;
- g->ud = ud;
- g->mainthread = L;
- g->uvhead.u.l.prev = &g->uvhead;
- g->uvhead.u.l.next = &g->uvhead;
- g->GCthreshold = 0; /* mark it as unfinished state */
- g->strt.size = 0;
- g->strt.nuse = 0;
- g->strt.hash = NULL;
- setnilvalue(registry(L));
- luaZ_initbuffer(L, &g->buff);
- g->panic = NULL;
- g->gcstate = GCSpause;
- g->rootgc = obj2gco(L);
- g->sweepstrgc = 0;
- g->sweepgc = &g->rootgc;
- g->gray = NULL;
- g->grayagain = NULL;
- g->weak = NULL;
- g->tmudata = NULL;
- g->totalbytes = sizeof(LG);
- g->gcpause = LUAI_GCPAUSE;
- g->gcstepmul = LUAI_GCMUL;
- g->gcdept = 0;
- for (i=0; i<NUM_TAGS; i++) g->mt[i] = NULL;
- if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
- /* memory allocation error: free partial state */
- close_state(L);
- L = NULL;
- }
- else
- luai_userstateopen(L);
- return L;
-}
-
-
-static void callallgcTM (lua_State *L, void *ud) {
- UNUSED(ud);
- luaC_callGCTM(L); /* call GC metamethods for all udata */
-}
-
-
-LUA_API void lua_close (lua_State *L) {
- L = G(L)->mainthread; /* only the main thread can be closed */
- lua_lock(L);
- luaF_close(L, L->stack); /* close all upvalues for this thread */
- luaC_separateudata(L, 1); /* separate udata that have GC metamethods */
- L->errfunc = 0; /* no error function during GC metamethods */
- do { /* repeat until no more errors */
- L->ci = L->base_ci;
- L->base = L->top = L->ci->base;
- L->nCcalls = L->baseCcalls = 0;
- } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0);
- lua_assert(G(L)->tmudata == NULL);
- luai_userstateclose(L);
- close_state(L);
-}
-
diff --git a/src/lua/src/lstate.h b/src/lua/src/lstate.h
deleted file mode 100644
index 3bc575b6b..000000000
--- a/src/lua/src/lstate.h
+++ /dev/null
@@ -1,169 +0,0 @@
-/*
-** $Id: lstate.h,v 2.24.1.2 2008/01/03 15:20:39 roberto Exp $
-** Global State
-** See Copyright Notice in lua.h
-*/
-
-#ifndef lstate_h
-#define lstate_h
-
-#include "lua.h"
-
-#include "lobject.h"
-#include "ltm.h"
-#include "lzio.h"
-
-
-
-struct lua_longjmp; /* defined in ldo.c */
-
-
-/* table of globals */
-#define gt(L) (&L->l_gt)
-
-/* registry */
-#define registry(L) (&G(L)->l_registry)
-
-
-/* extra stack space to handle TM calls and some other extras */
-#define EXTRA_STACK 5
-
-
-#define BASIC_CI_SIZE 8
-
-#define BASIC_STACK_SIZE (2*LUA_MINSTACK)
-
-
-
-typedef struct stringtable {
- GCObject **hash;
- lu_int32 nuse; /* number of elements */
- int size;
-} stringtable;
-
-
-/*
-** informations about a call
-*/
-typedef struct CallInfo {
- StkId base; /* base for this function */
- StkId func; /* function index in the stack */
- StkId top; /* top for this function */
- const Instruction *savedpc;
- int nresults; /* expected number of results from this function */
- int tailcalls; /* number of tail calls lost under this entry */
-} CallInfo;
-
-
-
-#define curr_func(L) (clvalue(L->ci->func))
-#define ci_func(ci) (clvalue((ci)->func))
-#define f_isLua(ci) (!ci_func(ci)->c.isC)
-#define isLua(ci) (ttisfunction((ci)->func) && f_isLua(ci))
-
-
-/*
-** `global state', shared by all threads of this state
-*/
-typedef struct global_State {
- stringtable strt; /* hash table for strings */
- lua_Alloc frealloc; /* function to reallocate memory */
- void *ud; /* auxiliary data to `frealloc' */
- lu_byte currentwhite;
- lu_byte gcstate; /* state of garbage collector */
- int sweepstrgc; /* position of sweep in `strt' */
- GCObject *rootgc; /* list of all collectable objects */
- GCObject **sweepgc; /* position of sweep in `rootgc' */
- GCObject *gray; /* list of gray objects */
- GCObject *grayagain; /* list of objects to be traversed atomically */
- GCObject *weak; /* list of weak tables (to be cleared) */
- GCObject *tmudata; /* last element of list of userdata to be GC */
- Mbuffer buff; /* temporary buffer for string concatentation */
- lu_mem GCthreshold;
- lu_mem totalbytes; /* number of bytes currently allocated */
- lu_mem estimate; /* an estimate of number of bytes actually in use */
- lu_mem gcdept; /* how much GC is `behind schedule' */
- int gcpause; /* size of pause between successive GCs */
- int gcstepmul; /* GC `granularity' */
- lua_CFunction panic; /* to be called in unprotected errors */
- TValue l_registry;
- struct lua_State *mainthread;
- UpVal uvhead; /* head of double-linked list of all open upvalues */
- struct Table *mt[NUM_TAGS]; /* metatables for basic types */
- TString *tmname[TM_N]; /* array with tag-method names */
-} global_State;
-
-
-/*
-** `per thread' state
-*/
-struct lua_State {
- CommonHeader;
- lu_byte status;
- StkId top; /* first free slot in the stack */
- StkId base; /* base of current function */
- global_State *l_G;
- CallInfo *ci; /* call info for current function */
- const Instruction *savedpc; /* `savedpc' of current function */
- StkId stack_last; /* last free slot in the stack */
- StkId stack; /* stack base */
- CallInfo *end_ci; /* points after end of ci array*/
- CallInfo *base_ci; /* array of CallInfo's */
- int stacksize;
- int size_ci; /* size of array `base_ci' */
- unsigned short nCcalls; /* number of nested C calls */
- unsigned short baseCcalls; /* nested C calls when resuming coroutine */
- lu_byte hookmask;
- lu_byte allowhook;
- int basehookcount;
- int hookcount;
- lua_Hook hook;
- TValue l_gt; /* table of globals */
- TValue env; /* temporary place for environments */
- GCObject *openupval; /* list of open upvalues in this stack */
- GCObject *gclist;
- struct lua_longjmp *errorJmp; /* current error recover point */
- ptrdiff_t errfunc; /* current error handling function (stack index) */
-};
-
-
-#define G(L) (L->l_G)
-
-
-/*
-** Union of all collectable objects
-*/
-union GCObject {
- GCheader gch;
- union TString ts;
- union Udata u;
- union Closure cl;
- struct Table h;
- struct Proto p;
- struct UpVal uv;
- struct lua_State th; /* thread */
-};
-
-
-/* macros to convert a GCObject into a specific value */
-#define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
-#define gco2ts(o) (&rawgco2ts(o)->tsv)
-#define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
-#define gco2u(o) (&rawgco2u(o)->uv)
-#define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
-#define gco2h(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
-#define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
-#define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
-#define ngcotouv(o) \
- check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))
-#define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
-
-/* macro to convert any Lua object into a GCObject */
-#define obj2gco(v) (cast(GCObject *, (v)))
-
-
-LUAI_FUNC lua_State *luaE_newthread (lua_State *L);
-LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
-
-#endif
-
diff --git a/src/lua/src/lstring.c b/src/lua/src/lstring.c
deleted file mode 100644
index 49113151c..000000000
--- a/src/lua/src/lstring.c
+++ /dev/null
@@ -1,111 +0,0 @@
-/*
-** $Id: lstring.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
-** String table (keeps all strings handled by Lua)
-** See Copyright Notice in lua.h
-*/
-
-
-#include <string.h>
-
-#define lstring_c
-#define LUA_CORE
-
-#include "lua.h"
-
-#include "lmem.h"
-#include "lobject.h"
-#include "lstate.h"
-#include "lstring.h"
-
-
-
-void luaS_resize (lua_State *L, int newsize) {
- GCObject **newhash;
- stringtable *tb;
- int i;
- if (G(L)->gcstate == GCSsweepstring)
- return; /* cannot resize during GC traverse */
- newhash = luaM_newvector(L, newsize, GCObject *);
- tb = &G(L)->strt;
- for (i=0; i<newsize; i++) newhash[i] = NULL;
- /* rehash */
- for (i=0; i<tb->size; i++) {
- GCObject *p = tb->hash[i];
- while (p) { /* for each node in the list */
- GCObject *next = p->gch.next; /* save next */
- unsigned int h = gco2ts(p)->hash;
- int h1 = lmod(h, newsize); /* new position */
- lua_assert(cast_int(h%newsize) == lmod(h, newsize));
- p->gch.next = newhash[h1]; /* chain it */
- newhash[h1] = p;
- p = next;
- }
- }
- luaM_freearray(L, tb->hash, tb->size, TString *);
- tb->size = newsize;
- tb->hash = newhash;
-}
-
-
-static TString *newlstr (lua_State *L, const char *str, size_t l,
- unsigned int h) {
- TString *ts;
- stringtable *tb;
- if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
- luaM_toobig(L);
- ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString)));
- ts->tsv.len = l;
- ts->tsv.hash = h;
- ts->tsv.marked = luaC_white(G(L));
- ts->tsv.tt = LUA_TSTRING;
- ts->tsv.reserved = 0;
- memcpy(ts+1, str, l*sizeof(char));
- ((char *)(ts+1))[l] = '\0'; /* ending 0 */
- tb = &G(L)->strt;
- h = lmod(h, tb->size);
- ts->tsv.next = tb->hash[h]; /* chain new entry */
- tb->hash[h] = obj2gco(ts);
- tb->nuse++;
- if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
- luaS_resize(L, tb->size*2); /* too crowded */
- return ts;
-}
-
-
-TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
- GCObject *o;
- unsigned int h = cast(unsigned int, l); /* seed */
- size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */
- size_t l1;
- for (l1=l; l1>=step; l1-=step) /* compute hash */
- h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1]));
- for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
- o != NULL;
- o = o->gch.next) {
- TString *ts = rawgco2ts(o);
- if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {
- /* string may be dead */
- if (isdead(G(L), o)) changewhite(o);
- return ts;
- }
- }
- return newlstr(L, str, l, h); /* not found */
-}
-
-
-Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
- Udata *u;
- if (s > MAX_SIZET - sizeof(Udata))
- luaM_toobig(L);
- u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata)));
- u->uv.marked = luaC_white(G(L)); /* is not finalized */
- u->uv.tt = LUA_TUSERDATA;
- u->uv.len = s;
- u->uv.metatable = NULL;
- u->uv.env = e;
- /* chain it on udata list (after main thread) */
- u->uv.next = G(L)->mainthread->next;
- G(L)->mainthread->next = obj2gco(u);
- return u;
-}
-
diff --git a/src/lua/src/lstring.h b/src/lua/src/lstring.h
deleted file mode 100644
index 73a2ff8b3..000000000
--- a/src/lua/src/lstring.h
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
-** $Id: lstring.h,v 1.43.1.1 2007/12/27 13:02:25 roberto Exp $
-** String table (keep all strings handled by Lua)
-** See Copyright Notice in lua.h
-*/
-
-#ifndef lstring_h
-#define lstring_h
-
-
-#include "lgc.h"
-#include "lobject.h"
-#include "lstate.h"
-
-
-#define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char))
-
-#define sizeudata(u) (sizeof(union Udata)+(u)->len)
-
-#define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s)))
-#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \
- (sizeof(s)/sizeof(char))-1))
-
-#define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT)
-
-LUAI_FUNC void luaS_resize (lua_State *L, int newsize);
-LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e);
-LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
-
-
-#endif
diff --git a/src/lua/src/lstrlib.c b/src/lua/src/lstrlib.c
deleted file mode 100644
index 1b4763d4e..000000000
--- a/src/lua/src/lstrlib.c
+++ /dev/null
@@ -1,869 +0,0 @@
-/*
-** $Id: lstrlib.c,v 1.132.1.4 2008/07/11 17:27:21 roberto Exp $
-** Standard library for string operations and pattern-matching
-** See Copyright Notice in lua.h
-*/
-
-
-#include <ctype.h>
-#include <stddef.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define lstrlib_c
-#define LUA_LIB
-
-#include "lua.h"
-
-#include "lauxlib.h"
-#include "lualib.h"
-
-
-/* macro to `unsign' a character */
-#define uchar(c) ((unsigned char)(c))
-
-
-
-static int str_len (lua_State *L) {
- size_t l;
- luaL_checklstring(L, 1, &l);
- lua_pushinteger(L, l);
- return 1;
-}
-
-
-static ptrdiff_t posrelat (ptrdiff_t pos, size_t len) {
- /* relative string position: negative means back from end */
- if (pos < 0) pos += (ptrdiff_t)len + 1;
- return (pos >= 0) ? pos : 0;
-}
-
-
-static int str_sub (lua_State *L) {
- size_t l;
- const char *s = luaL_checklstring(L, 1, &l);
- ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l);
- ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l);
- if (start < 1) start = 1;
- if (end > (ptrdiff_t)l) end = (ptrdiff_t)l;
- if (start <= end)
- lua_pushlstring(L, s+start-1, end-start+1);
- else lua_pushliteral(L, "");
- return 1;
-}
-
-
-static int str_reverse (lua_State *L) {
- size_t l;
- luaL_Buffer b;
- const char *s = luaL_checklstring(L, 1, &l);
- luaL_buffinit(L, &b);
- while (l--) luaL_addchar(&b, s[l]);
- luaL_pushresult(&b);
- return 1;
-}
-
-
-static int str_lower (lua_State *L) {
- size_t l;
- size_t i;
- luaL_Buffer b;
- const char *s = luaL_checklstring(L, 1, &l);
- luaL_buffinit(L, &b);
- for (i=0; i<l; i++)
- luaL_addchar(&b, tolower(uchar(s[i])));
- luaL_pushresult(&b);
- return 1;
-}
-
-
-static int str_upper (lua_State *L) {
- size_t l;
- size_t i;
- luaL_Buffer b;
- const char *s = luaL_checklstring(L, 1, &l);
- luaL_buffinit(L, &b);
- for (i=0; i<l; i++)
- luaL_addchar(&b, toupper(uchar(s[i])));
- luaL_pushresult(&b);
- return 1;
-}
-
-static int str_rep (lua_State *L) {
- size_t l;
- luaL_Buffer b;
- const char *s = luaL_checklstring(L, 1, &l);
- int n = luaL_checkint(L, 2);
- luaL_buffinit(L, &b);
- while (n-- > 0)
- luaL_addlstring(&b, s, l);
- luaL_pushresult(&b);
- return 1;
-}
-
-
-static int str_byte (lua_State *L) {
- size_t l;
- const char *s = luaL_checklstring(L, 1, &l);
- ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
- ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l);
- int n, i;
- if (posi <= 0) posi = 1;
- if ((size_t)pose > l) pose = l;
- if (posi > pose) return 0; /* empty interval; return no values */
- n = (int)(pose - posi + 1);
- if (posi + n <= pose) /* overflow? */
- luaL_error(L, "string slice too long");
- luaL_checkstack(L, n, "string slice too long");
- for (i=0; i<n; i++)
- lua_pushinteger(L, uchar(s[posi+i-1]));
- return n;
-}
-
-
-static int str_char (lua_State *L) {
- int n = lua_gettop(L); /* number of arguments */
- int i;
- luaL_Buffer b;
- luaL_buffinit(L, &b);
- for (i=1; i<=n; i++) {
- int c = luaL_checkint(L, i);
- luaL_argcheck(L, uchar(c) == c, i, "invalid value");
- luaL_addchar(&b, uchar(c));
- }
- luaL_pushresult(&b);
- return 1;
-}
-
-
-static int writer (lua_State *L, const void* b, size_t size, void* B) {
- (void)L;
- luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);
- return 0;
-}
-
-
-static int str_dump (lua_State *L) {
- luaL_Buffer b;
- luaL_checktype(L, 1, LUA_TFUNCTION);
- lua_settop(L, 1);
- luaL_buffinit(L,&b);
- if (lua_dump(L, writer, &b) != 0)
- luaL_error(L, "unable to dump given function");
- luaL_pushresult(&b);
- return 1;
-}
-
-
-
-/*
-** {======================================================
-** PATTERN MATCHING
-** =======================================================
-*/
-
-
-#define CAP_UNFINISHED (-1)
-#define CAP_POSITION (-2)
-
-typedef struct MatchState {
- const char *src_init; /* init of source string */
- const char *src_end; /* end (`\0') of source string */
- lua_State *L;
- int level; /* total number of captures (finished or unfinished) */
- struct {
- const char *init;
- ptrdiff_t len;
- } capture[LUA_MAXCAPTURES];
-} MatchState;
-
-
-#define L_ESC '%'
-#define SPECIALS "^$*+?.([%-"
-
-
-static int check_capture (MatchState *ms, int l) {
- l -= '1';
- if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
- return luaL_error(ms->L, "invalid capture index");
- return l;
-}
-
-
-static int capture_to_close (MatchState *ms) {
- int level = ms->level;
- for (level--; level>=0; level--)
- if (ms->capture[level].len == CAP_UNFINISHED) return level;
- return luaL_error(ms->L, "invalid pattern capture");
-}
-
-
-static const char *classend (MatchState *ms, const char *p) {
- switch (*p++) {
- case L_ESC: {
- if (*p == '\0')
- luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")");
- return p+1;
- }
- case '[': {
- if (*p == '^') p++;
- do { /* look for a `]' */
- if (*p == '\0')
- luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")");
- if (*(p++) == L_ESC && *p != '\0')
- p++; /* skip escapes (e.g. `%]') */
- } while (*p != ']');
- return p+1;
- }
- default: {
- return p;
- }
- }
-}
-
-
-static int match_class (int c, int cl) {
- int res;
- switch (tolower(cl)) {
- case 'a' : res = isalpha(c); break;
- case 'c' : res = iscntrl(c); break;
- case 'd' : res = isdigit(c); break;
- case 'l' : res = islower(c); break;
- case 'p' : res = ispunct(c); break;
- case 's' : res = isspace(c); break;
- case 'u' : res = isupper(c); break;
- case 'w' : res = isalnum(c); break;
- case 'x' : res = isxdigit(c); break;
- case 'z' : res = (c == 0); break;
- default: return (cl == c);
- }
- return (islower(cl) ? res : !res);
-}
-
-
-static int matchbracketclass (int c, const char *p, const char *ec) {
- int sig = 1;
- if (*(p+1) == '^') {
- sig = 0;
- p++; /* skip the `^' */
- }
- while (++p < ec) {
- if (*p == L_ESC) {
- p++;
- if (match_class(c, uchar(*p)))
- return sig;
- }
- else if ((*(p+1) == '-') && (p+2 < ec)) {
- p+=2;
- if (uchar(*(p-2)) <= c && c <= uchar(*p))
- return sig;
- }
- else if (uchar(*p) == c) return sig;
- }
- return !sig;
-}
-
-
-static int singlematch (int c, const char *p, const char *ep) {
- switch (*p) {
- case '.': return 1; /* matches any char */
- case L_ESC: return match_class(c, uchar(*(p+1)));
- case '[': return matchbracketclass(c, p, ep-1);
- default: return (uchar(*p) == c);
- }
-}
-
-
-static const char *match (MatchState *ms, const char *s, const char *p);
-
-
-static const char *matchbalance (MatchState *ms, const char *s,
- const char *p) {
- if (*p == 0 || *(p+1) == 0)
- luaL_error(ms->L, "unbalanced pattern");
- if (*s != *p) return NULL;
- else {
- int b = *p;
- int e = *(p+1);
- int cont = 1;
- while (++s < ms->src_end) {
- if (*s == e) {
- if (--cont == 0) return s+1;
- }
- else if (*s == b) cont++;
- }
- }
- return NULL; /* string ends out of balance */
-}
-
-
-static const char *max_expand (MatchState *ms, const char *s,
- const char *p, const char *ep) {
- ptrdiff_t i = 0; /* counts maximum expand for item */
- while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep))
- i++;
- /* keeps trying to match with the maximum repetitions */
- while (i>=0) {
- const char *res = match(ms, (s+i), ep+1);
- if (res) return res;
- i--; /* else didn't match; reduce 1 repetition to try again */
- }
- return NULL;
-}
-
-
-static const char *min_expand (MatchState *ms, const char *s,
- const char *p, const char *ep) {
- for (;;) {
- const char *res = match(ms, s, ep+1);
- if (res != NULL)
- return res;
- else if (s<ms->src_end && singlematch(uchar(*s), p, ep))
- s++; /* try with one more repetition */
- else return NULL;
- }
-}
-
-
-static const char *start_capture (MatchState *ms, const char *s,
- const char *p, int what) {
- const char *res;
- int level = ms->level;
- if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");
- ms->capture[level].init = s;
- ms->capture[level].len = what;
- ms->level = level+1;
- if ((res=match(ms, s, p)) == NULL) /* match failed? */
- ms->level--; /* undo capture */
- return res;
-}
-
-
-static const char *end_capture (MatchState *ms, const char *s,
- const char *p) {
- int l = capture_to_close(ms);
- const char *res;
- ms->capture[l].len = s - ms->capture[l].init; /* close capture */
- if ((res = match(ms, s, p)) == NULL) /* match failed? */
- ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
- return res;
-}
-
-
-static const char *match_capture (MatchState *ms, const char *s, int l) {
- size_t len;
- l = check_capture(ms, l);
- len = ms->capture[l].len;
- if ((size_t)(ms->src_end-s) >= len &&
- memcmp(ms->capture[l].init, s, len) == 0)
- return s+len;
- else return NULL;
-}
-
-
-static const char *match (MatchState *ms, const char *s, const char *p) {
- init: /* using goto's to optimize tail recursion */
- switch (*p) {
- case '(': { /* start capture */
- if (*(p+1) == ')') /* position capture? */
- return start_capture(ms, s, p+2, CAP_POSITION);
- else
- return start_capture(ms, s, p+1, CAP_UNFINISHED);
- }
- case ')': { /* end capture */
- return end_capture(ms, s, p+1);
- }
- case L_ESC: {
- switch (*(p+1)) {
- case 'b': { /* balanced string? */
- s = matchbalance(ms, s, p+2);
- if (s == NULL) return NULL;
- p+=4; goto init; /* else return match(ms, s, p+4); */
- }
- case 'f': { /* frontier? */
- const char *ep; char previous;
- p += 2;
- if (*p != '[')
- luaL_error(ms->L, "missing " LUA_QL("[") " after "
- LUA_QL("%%f") " in pattern");
- ep = classend(ms, p); /* points to what is next */
- previous = (s == ms->src_init) ? '\0' : *(s-1);
- if (matchbracketclass(uchar(previous), p, ep-1) ||
- !matchbracketclass(uchar(*s), p, ep-1)) return NULL;
- p=ep; goto init; /* else return match(ms, s, ep); */
- }
- default: {
- if (isdigit(uchar(*(p+1)))) { /* capture results (%0-%9)? */
- s = match_capture(ms, s, uchar(*(p+1)));
- if (s == NULL) return NULL;
- p+=2; goto init; /* else return match(ms, s, p+2) */
- }
- goto dflt; /* case default */
- }
- }
- }
- case '\0': { /* end of pattern */
- return s; /* match succeeded */
- }
- case '$': {
- if (*(p+1) == '\0') /* is the `$' the last char in pattern? */
- return (s == ms->src_end) ? s : NULL; /* check end of string */
- else goto dflt;
- }
- default: dflt: { /* it is a pattern item */
- const char *ep = classend(ms, p); /* points to what is next */
- int m = s<ms->src_end && singlematch(uchar(*s), p, ep);
- switch (*ep) {
- case '?': { /* optional */
- const char *res;
- if (m && ((res=match(ms, s+1, ep+1)) != NULL))
- return res;
- p=ep+1; goto init; /* else return match(ms, s, ep+1); */
- }
- case '*': { /* 0 or more repetitions */
- return max_expand(ms, s, p, ep);
- }
- case '+': { /* 1 or more repetitions */
- return (m ? max_expand(ms, s+1, p, ep) : NULL);
- }
- case '-': { /* 0 or more repetitions (minimum) */
- return min_expand(ms, s, p, ep);
- }
- default: {
- if (!m) return NULL;
- s++; p=ep; goto init; /* else return match(ms, s+1, ep); */
- }
- }
- }
- }
-}
-
-
-
-static const char *lmemfind (const char *s1, size_t l1,
- const char *s2, size_t l2) {
- if (l2 == 0) return s1; /* empty strings are everywhere */
- else if (l2 > l1) return NULL; /* avoids a negative `l1' */
- else {
- const char *init; /* to search for a `*s2' inside `s1' */
- l2--; /* 1st char will be checked by `memchr' */
- l1 = l1-l2; /* `s2' cannot be found after that */
- while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
- init++; /* 1st char is already checked */
- if (memcmp(init, s2+1, l2) == 0)
- return init-1;
- else { /* correct `l1' and `s1' to try again */
- l1 -= init-s1;
- s1 = init;
- }
- }
- return NULL; /* not found */
- }
-}
-
-
-static void push_onecapture (MatchState *ms, int i, const char *s,
- const char *e) {
- if (i >= ms->level) {
- if (i == 0) /* ms->level == 0, too */
- lua_pushlstring(ms->L, s, e - s); /* add whole match */
- else
- luaL_error(ms->L, "invalid capture index");
- }
- else {
- ptrdiff_t l = ms->capture[i].len;
- if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
- if (l == CAP_POSITION)
- lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1);
- else
- lua_pushlstring(ms->L, ms->capture[i].init, l);
- }
-}
-
-
-static int push_captures (MatchState *ms, const char *s, const char *e) {
- int i;
- int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
- luaL_checkstack(ms->L, nlevels, "too many captures");
- for (i = 0; i < nlevels; i++)
- push_onecapture(ms, i, s, e);
- return nlevels; /* number of strings pushed */
-}
-
-
-static int str_find_aux (lua_State *L, int find) {
- size_t l1, l2;
- const char *s = luaL_checklstring(L, 1, &l1);
- const char *p = luaL_checklstring(L, 2, &l2);
- ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1;
- if (init < 0) init = 0;
- else if ((size_t)(init) > l1) init = (ptrdiff_t)l1;
- if (find && (lua_toboolean(L, 4) || /* explicit request? */
- strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */
- /* do a plain search */
- const char *s2 = lmemfind(s+init, l1-init, p, l2);
- if (s2) {
- lua_pushinteger(L, s2-s+1);
- lua_pushinteger(L, s2-s+l2);
- return 2;
- }
- }
- else {
- MatchState ms;
- int anchor = (*p == '^') ? (p++, 1) : 0;
- const char *s1=s+init;
- ms.L = L;
- ms.src_init = s;
- ms.src_end = s+l1;
- do {
- const char *res;
- ms.level = 0;
- if ((res=match(&ms, s1, p)) != NULL) {
- if (find) {
- lua_pushinteger(L, s1-s+1); /* start */
- lua_pushinteger(L, res-s); /* end */
- return push_captures(&ms, NULL, 0) + 2;
- }
- else
- return push_captures(&ms, s1, res);
- }
- } while (s1++ < ms.src_end && !anchor);
- }
- lua_pushnil(L); /* not found */
- return 1;
-}
-
-
-static int str_find (lua_State *L) {
- return str_find_aux(L, 1);
-}
-
-
-static int str_match (lua_State *L) {
- return str_find_aux(L, 0);
-}
-
-
-static int gmatch_aux (lua_State *L) {
- MatchState ms;
- size_t ls;
- const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
- const char *p = lua_tostring(L, lua_upvalueindex(2));
- const char *src;
- ms.L = L;
- ms.src_init = s;
- ms.src_end = s+ls;
- for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3));
- src <= ms.src_end;
- src++) {
- const char *e;
- ms.level = 0;
- if ((e = match(&ms, src, p)) != NULL) {
- lua_Integer newstart = e-s;
- if (e == src) newstart++; /* empty match? go at least one position */
- lua_pushinteger(L, newstart);
- lua_replace(L, lua_upvalueindex(3));
- return push_captures(&ms, src, e);
- }
- }
- return 0; /* not found */
-}
-
-
-static int gmatch (lua_State *L) {
- luaL_checkstring(L, 1);
- luaL_checkstring(L, 2);
- lua_settop(L, 2);
- lua_pushinteger(L, 0);
- lua_pushcclosure(L, gmatch_aux, 3);
- return 1;
-}
-
-
-static int gfind_nodef (lua_State *L) {
- return luaL_error(L, LUA_QL("string.gfind") " was renamed to "
- LUA_QL("string.gmatch"));
-}
-
-
-static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
- const char *e) {
- size_t l, i;
- const char *news = lua_tolstring(ms->L, 3, &l);
- for (i = 0; i < l; i++) {
- if (news[i] != L_ESC)
- luaL_addchar(b, news[i]);
- else {
- i++; /* skip ESC */
- if (!isdigit(uchar(news[i])))
- luaL_addchar(b, news[i]);
- else if (news[i] == '0')
- luaL_addlstring(b, s, e - s);
- else {
- push_onecapture(ms, news[i] - '1', s, e);
- luaL_addvalue(b); /* add capture to accumulated result */
- }
- }
- }
-}
-
-
-static void add_value (MatchState *ms, luaL_Buffer *b, const char *s,
- const char *e) {
- lua_State *L = ms->L;
- switch (lua_type(L, 3)) {
- case LUA_TNUMBER:
- case LUA_TSTRING: {
- add_s(ms, b, s, e);
- return;
- }
- case LUA_TFUNCTION: {
- int n;
- lua_pushvalue(L, 3);
- n = push_captures(ms, s, e);
- lua_call(L, n, 1);
- break;
- }
- case LUA_TTABLE: {
- push_onecapture(ms, 0, s, e);
- lua_gettable(L, 3);
- break;
- }
- }
- if (!lua_toboolean(L, -1)) { /* nil or false? */
- lua_pop(L, 1);
- lua_pushlstring(L, s, e - s); /* keep original text */
- }
- else if (!lua_isstring(L, -1))
- luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1));
- luaL_addvalue(b); /* add result to accumulator */
-}
-
-
-static int str_gsub (lua_State *L) {
- size_t srcl;
- const char *src = luaL_checklstring(L, 1, &srcl);
- const char *p = luaL_checkstring(L, 2);
- int tr = lua_type(L, 3);
- int max_s = luaL_optint(L, 4, srcl+1);
- int anchor = (*p == '^') ? (p++, 1) : 0;
- int n = 0;
- MatchState ms;
- luaL_Buffer b;
- luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
- tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3,
- "string/function/table expected");
- luaL_buffinit(L, &b);
- ms.L = L;
- ms.src_init = src;
- ms.src_end = src+srcl;
- while (n < max_s) {
- const char *e;
- ms.level = 0;
- e = match(&ms, src, p);
- if (e) {
- n++;
- add_value(&ms, &b, src, e);
- }
- if (e && e>src) /* non empty match? */
- src = e; /* skip it */
- else if (src < ms.src_end)
- luaL_addchar(&b, *src++);
- else break;
- if (anchor) break;
- }
- luaL_addlstring(&b, src, ms.src_end-src);
- luaL_pushresult(&b);
- lua_pushinteger(L, n); /* number of substitutions */
- return 2;
-}
-
-/* }====================================================== */
-
-
-/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
-#define MAX_ITEM 512
-/* valid flags in a format specification */
-#define FLAGS "-+ #0"
-/*
-** maximum size of each format specification (such as '%-099.99d')
-** (+10 accounts for %99.99x plus margin of error)
-*/
-#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
-
-
-static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
- size_t l;
- const char *s = luaL_checklstring(L, arg, &l);
- luaL_addchar(b, '"');
- while (l--) {
- switch (*s) {
- case '"': case '\\': case '\n': {
- luaL_addchar(b, '\\');
- luaL_addchar(b, *s);
- break;
- }
- case '\r': {
- luaL_addlstring(b, "\\r", 2);
- break;
- }
- case '\0': {
- luaL_addlstring(b, "\\000", 4);
- break;
- }
- default: {
- luaL_addchar(b, *s);
- break;
- }
- }
- s++;
- }
- luaL_addchar(b, '"');
-}
-
-static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
- const char *p = strfrmt;
- while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
- if ((size_t)(p - strfrmt) >= sizeof(FLAGS))
- luaL_error(L, "invalid format (repeated flags)");
- if (isdigit(uchar(*p))) p++; /* skip width */
- if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
- if (*p == '.') {
- p++;
- if (isdigit(uchar(*p))) p++; /* skip precision */
- if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
- }
- if (isdigit(uchar(*p)))
- luaL_error(L, "invalid format (width or precision too long)");
- *(form++) = '%';
- strncpy(form, strfrmt, p - strfrmt + 1);
- form += p - strfrmt + 1;
- *form = '\0';
- return p;
-}
-
-
-static void addintlen (char *form) {
- size_t l = strlen(form);
- char spec = form[l - 1];
- strcpy(form + l - 1, LUA_INTFRMLEN);
- form[l + sizeof(LUA_INTFRMLEN) - 2] = spec;
- form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0';
-}
-
-
-static int str_format (lua_State *L) {
- int arg = 1;
- size_t sfl;
- const char *strfrmt = luaL_checklstring(L, arg, &sfl);
- const char *strfrmt_end = strfrmt+sfl;
- luaL_Buffer b;
- luaL_buffinit(L, &b);
- while (strfrmt < strfrmt_end) {
- if (*strfrmt != L_ESC)
- luaL_addchar(&b, *strfrmt++);
- else if (*++strfrmt == L_ESC)
- luaL_addchar(&b, *strfrmt++); /* %% */
- else { /* format item */
- char form[MAX_FORMAT]; /* to store the format (`%...') */
- char buff[MAX_ITEM]; /* to store the formatted item */
- arg++;
- strfrmt = scanformat(L, strfrmt, form);
- switch (*strfrmt++) {
- case 'c': {
- sprintf(buff, form, (int)luaL_checknumber(L, arg));
- break;
- }
- case 'd': case 'i': {
- addintlen(form);
- sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg));
- break;
- }
- case 'o': case 'u': case 'x': case 'X': {
- addintlen(form);
- sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg));
- break;
- }
- case 'e': case 'E': case 'f':
- case 'g': case 'G': {
- sprintf(buff, form, (double)luaL_checknumber(L, arg));
- break;
- }
- case 'q': {
- addquoted(L, &b, arg);
- continue; /* skip the 'addsize' at the end */
- }
- case 's': {
- size_t l;
- const char *s = luaL_checklstring(L, arg, &l);
- if (!strchr(form, '.') && l >= 100) {
- /* no precision and string is too long to be formatted;
- keep original string */
- lua_pushvalue(L, arg);
- luaL_addvalue(&b);
- continue; /* skip the `addsize' at the end */
- }
- else {
- sprintf(buff, form, s);
- break;
- }
- }
- default: { /* also treat cases `pnLlh' */
- return luaL_error(L, "invalid option " LUA_QL("%%%c") " to "
- LUA_QL("format"), *(strfrmt - 1));
- }
- }
- luaL_addlstring(&b, buff, strlen(buff));
- }
- }
- luaL_pushresult(&b);
- return 1;
-}
-
-
-static const luaL_Reg strlib[] = {
- {"byte", str_byte},
- {"char", str_char},
- {"dump", str_dump},
- {"find", str_find},
- {"format", str_format},
- {"gfind", gfind_nodef},
- {"gmatch", gmatch},
- {"gsub", str_gsub},
- {"len", str_len},
- {"lower", str_lower},
- {"match", str_match},
- {"rep", str_rep},
- {"reverse", str_reverse},
- {"sub", str_sub},
- {"upper", str_upper},
- {NULL, NULL}
-};
-
-
-static void createmetatable (lua_State *L) {
- lua_createtable(L, 0, 1); /* create metatable for strings */
- lua_pushliteral(L, ""); /* dummy string */
- lua_pushvalue(L, -2);
- lua_setmetatable(L, -2); /* set string metatable */
- lua_pop(L, 1); /* pop dummy string */
- lua_pushvalue(L, -2); /* string library... */
- lua_setfield(L, -2, "__index"); /* ...is the __index metamethod */
- lua_pop(L, 1); /* pop metatable */
-}
-
-
-/*
-** Open string library
-*/
-LUALIB_API int luaopen_string (lua_State *L) {
- luaL_register(L, LUA_STRLIBNAME, strlib);
-#if defined(LUA_COMPAT_GFIND)
- lua_getfield(L, -1, "gmatch");
- lua_setfield(L, -2, "gfind");
-#endif
- createmetatable(L);
- return 1;
-}
-
diff --git a/src/lua/src/ltable.c b/src/lua/src/ltable.c
deleted file mode 100644
index ec84f4fab..000000000
--- a/src/lua/src/ltable.c
+++ /dev/null
@@ -1,588 +0,0 @@
-/*
-** $Id: ltable.c,v 2.32.1.2 2007/12/28 15:32:23 roberto Exp $
-** Lua tables (hash)
-** See Copyright Notice in lua.h
-*/
-
-
-/*
-** Implementation of tables (aka arrays, objects, or hash tables).
-** Tables keep its elements in two parts: an array part and a hash part.
-** Non-negative integer keys are all candidates to be kept in the array
-** part. The actual size of the array is the largest `n' such that at
-** least half the slots between 0 and n are in use.
-** Hash uses a mix of chained scatter table with Brent's variation.
-** A main invariant of these tables is that, if an element is not
-** in its main position (i.e. the `original' position that its hash gives
-** to it), then the colliding element is in its own main position.
-** Hence even when the load factor reaches 100%, performance remains good.
-*/
-
-#include <math.h>
-#include <string.h>
-
-#define ltable_c
-#define LUA_CORE
-
-#include "lua.h"
-
-#include "ldebug.h"
-#include "ldo.h"
-#include "lgc.h"
-#include "lmem.h"
-#include "lobject.h"
-#include "lstate.h"
-#include "ltable.h"
-
-
-/*
-** max size of array part is 2^MAXBITS
-*/
-#if LUAI_BITSINT > 26
-#define MAXBITS 26
-#else
-#define MAXBITS (LUAI_BITSINT-2)
-#endif
-
-#define MAXASIZE (1 << MAXBITS)
-
-
-#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
-
-#define hashstr(t,str) hashpow2(t, (str)->tsv.hash)
-#define hashboolean(t,p) hashpow2(t, p)
-
-
-/*
-** for some types, it is better to avoid modulus by power of 2, as
-** they tend to have many 2 factors.
-*/
-#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
-
-
-#define hashpointer(t,p) hashmod(t, IntPoint(p))
-
-
-/*
-** number of ints inside a lua_Number
-*/
-#define numints cast_int(sizeof(lua_Number)/sizeof(int))
-
-
-
-#define dummynode (&dummynode_)
-
-static const Node dummynode_ = {
- {{NULL}, LUA_TNIL}, /* value */
- {{{NULL}, LUA_TNIL, NULL}} /* key */
-};
-
-
-/*
-** hash for lua_Numbers
-*/
-static Node *hashnum (const Table *t, lua_Number n) {
- unsigned int a[numints];
- int i;
- if (luai_numeq(n, 0)) /* avoid problems with -0 */
- return gnode(t, 0);
- memcpy(a, &n, sizeof(a));
- for (i = 1; i < numints; i++) a[0] += a[i];
- return hashmod(t, a[0]);
-}
-
-
-
-/*
-** returns the `main' position of an element in a table (that is, the index
-** of its hash value)
-*/
-static Node *mainposition (const Table *t, const TValue *key) {
- switch (ttype(key)) {
- case LUA_TNUMBER:
- return hashnum(t, nvalue(key));
- case LUA_TSTRING:
- return hashstr(t, rawtsvalue(key));
- case LUA_TBOOLEAN:
- return hashboolean(t, bvalue(key));
- case LUA_TLIGHTUSERDATA:
- return hashpointer(t, pvalue(key));
- default:
- return hashpointer(t, gcvalue(key));
- }
-}
-
-
-/*
-** returns the index for `key' if `key' is an appropriate key to live in
-** the array part of the table, -1 otherwise.
-*/
-static int arrayindex (const TValue *key) {
- if (ttisnumber(key)) {
- lua_Number n = nvalue(key);
- int k;
- lua_number2int(k, n);
- if (luai_numeq(cast_num(k), n))
- return k;
- }
- return -1; /* `key' did not match some condition */
-}
-
-
-/*
-** returns the index of a `key' for table traversals. First goes all
-** elements in the array part, then elements in the hash part. The
-** beginning of a traversal is signalled by -1.
-*/
-static int findindex (lua_State *L, Table *t, StkId key) {
- int i;
- if (ttisnil(key)) return -1; /* first iteration */
- i = arrayindex(key);
- if (0 < i && i <= t->sizearray) /* is `key' inside array part? */
- return i-1; /* yes; that's the index (corrected to C) */
- else {
- Node *n = mainposition(t, key);
- do { /* check whether `key' is somewhere in the chain */
- /* key may be dead already, but it is ok to use it in `next' */
- if (luaO_rawequalObj(key2tval(n), key) ||
- (ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) &&
- gcvalue(gkey(n)) == gcvalue(key))) {
- i = cast_int(n - gnode(t, 0)); /* key index in hash table */
- /* hash elements are numbered after array ones */
- return i + t->sizearray;
- }
- else n = gnext(n);
- } while (n);
- luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */
- return 0; /* to avoid warnings */
- }
-}
-
-
-int luaH_next (lua_State *L, Table *t, StkId key) {
- int i = findindex(L, t, key); /* find original element */
- for (i++; i < t->sizearray; i++) { /* try first array part */
- if (!ttisnil(&t->array[i])) { /* a non-nil value? */
- setnvalue(key, cast_num(i+1));
- setobj2s(L, key+1, &t->array[i]);
- return 1;
- }
- }
- for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */
- if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */
- setobj2s(L, key, key2tval(gnode(t, i)));
- setobj2s(L, key+1, gval(gnode(t, i)));
- return 1;
- }
- }
- return 0; /* no more elements */
-}
-
-
-/*
-** {=============================================================
-** Rehash
-** ==============================================================
-*/
-
-
-static int computesizes (int nums[], int *narray) {
- int i;
- int twotoi; /* 2^i */
- int a = 0; /* number of elements smaller than 2^i */
- int na = 0; /* number of elements to go to array part */
- int n = 0; /* optimal size for array part */
- for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) {
- if (nums[i] > 0) {
- a += nums[i];
- if (a > twotoi/2) { /* more than half elements present? */
- n = twotoi; /* optimal size (till now) */
- na = a; /* all elements smaller than n will go to array part */
- }
- }
- if (a == *narray) break; /* all elements already counted */
- }
- *narray = n;
- lua_assert(*narray/2 <= na && na <= *narray);
- return na;
-}
-
-
-static int countint (const TValue *key, int *nums) {
- int k = arrayindex(key);
- if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */
- nums[ceillog2(k)]++; /* count as such */
- return 1;
- }
- else
- return 0;
-}
-
-
-static int numusearray (const Table *t, int *nums) {
- int lg;
- int ttlg; /* 2^lg */
- int ause = 0; /* summation of `nums' */
- int i = 1; /* count to traverse all array keys */
- for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */
- int lc = 0; /* counter */
- int lim = ttlg;
- if (lim > t->sizearray) {
- lim = t->sizearray; /* adjust upper limit */
- if (i > lim)
- break; /* no more elements to count */
- }
- /* count elements in range (2^(lg-1), 2^lg] */
- for (; i <= lim; i++) {
- if (!ttisnil(&t->array[i-1]))
- lc++;
- }
- nums[lg] += lc;
- ause += lc;
- }
- return ause;
-}
-
-
-static int numusehash (const Table *t, int *nums, int *pnasize) {
- int totaluse = 0; /* total number of elements */
- int ause = 0; /* summation of `nums' */
- int i = sizenode(t);
- while (i--) {
- Node *n = &t->node[i];
- if (!ttisnil(gval(n))) {
- ause += countint(key2tval(n), nums);
- totaluse++;
- }
- }
- *pnasize += ause;
- return totaluse;
-}
-
-
-static void setarrayvector (lua_State *L, Table *t, int size) {
- int i;
- luaM_reallocvector(L, t->array, t->sizearray, size, TValue);
- for (i=t->sizearray; i<size; i++)
- setnilvalue(&t->array[i]);
- t->sizearray = size;
-}
-
-
-static void setnodevector (lua_State *L, Table *t, int size) {
- int lsize;
- if (size == 0) { /* no elements to hash part? */
- t->node = cast(Node *, dummynode); /* use common `dummynode' */
- lsize = 0;
- }
- else {
- int i;
- lsize = ceillog2(size);
- if (lsize > MAXBITS)
- luaG_runerror(L, "table overflow");
- size = twoto(lsize);
- t->node = luaM_newvector(L, size, Node);
- for (i=0; i<size; i++) {
- Node *n = gnode(t, i);
- gnext(n) = NULL;
- setnilvalue(gkey(n));
- setnilvalue(gval(n));
- }
- }
- t->lsizenode = cast_byte(lsize);
- t->lastfree = gnode(t, size); /* all positions are free */
-}
-
-
-static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
- int i;
- int oldasize = t->sizearray;
- int oldhsize = t->lsizenode;
- Node *nold = t->node; /* save old hash ... */
- if (nasize > oldasize) /* array part must grow? */
- setarrayvector(L, t, nasize);
- /* create new hash part with appropriate size */
- setnodevector(L, t, nhsize);
- if (nasize < oldasize) { /* array part must shrink? */
- t->sizearray = nasize;
- /* re-insert elements from vanishing slice */
- for (i=nasize; i<oldasize; i++) {
- if (!ttisnil(&t->array[i]))
- setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]);
- }
- /* shrink array */
- luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
- }
- /* re-insert elements from hash part */
- for (i = twoto(oldhsize) - 1; i >= 0; i--) {
- Node *old = nold+i;
- if (!ttisnil(gval(old)))
- setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old));
- }
- if (nold != dummynode)
- luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */
-}
-
-
-void luaH_resizearray (lua_State *L, Table *t, int nasize) {
- int nsize = (t->node == dummynode) ? 0 : sizenode(t);
- resize(L, t, nasize, nsize);
-}
-
-
-static void rehash (lua_State *L, Table *t, const TValue *ek) {
- int nasize, na;
- int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */
- int i;
- int totaluse;
- for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */
- nasize = numusearray(t, nums); /* count keys in array part */
- totaluse = nasize; /* all those keys are integer keys */
- totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */
- /* count extra key */
- nasize += countint(ek, nums);
- totaluse++;
- /* compute new size for array part */
- na = computesizes(nums, &nasize);
- /* resize the table to new computed sizes */
- resize(L, t, nasize, totaluse - na);
-}
-
-
-
-/*
-** }=============================================================
-*/
-
-
-Table *luaH_new (lua_State *L, int narray, int nhash) {
- Table *t = luaM_new(L, Table);
- luaC_link(L, obj2gco(t), LUA_TTABLE);
- t->metatable = NULL;
- t->flags = cast_byte(~0);
- /* temporary values (kept only if some malloc fails) */
- t->array = NULL;
- t->sizearray = 0;
- t->lsizenode = 0;
- t->node = cast(Node *, dummynode);
- setarrayvector(L, t, narray);
- setnodevector(L, t, nhash);
- return t;
-}
-
-
-void luaH_free (lua_State *L, Table *t) {
- if (t->node != dummynode)
- luaM_freearray(L, t->node, sizenode(t), Node);
- luaM_freearray(L, t->array, t->sizearray, TValue);
- luaM_free(L, t);
-}
-
-
-static Node *getfreepos (Table *t) {
- while (t->lastfree-- > t->node) {
- if (ttisnil(gkey(t->lastfree)))
- return t->lastfree;
- }
- return NULL; /* could not find a free place */
-}
-
-
-
-/*
-** inserts a new key into a hash table; first, check whether key's main
-** position is free. If not, check whether colliding node is in its main
-** position or not: if it is not, move colliding node to an empty place and
-** put new key in its main position; otherwise (colliding node is in its main
-** position), new key goes to an empty position.
-*/
-static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
- Node *mp = mainposition(t, key);
- if (!ttisnil(gval(mp)) || mp == dummynode) {
- Node *othern;
- Node *n = getfreepos(t); /* get a free place */
- if (n == NULL) { /* cannot find a free place? */
- rehash(L, t, key); /* grow table */
- return luaH_set(L, t, key); /* re-insert key into grown table */
- }
- lua_assert(n != dummynode);
- othern = mainposition(t, key2tval(mp));
- if (othern != mp) { /* is colliding node out of its main position? */
- /* yes; move colliding node into free position */
- while (gnext(othern) != mp) othern = gnext(othern); /* find previous */
- gnext(othern) = n; /* redo the chain with `n' in place of `mp' */
- *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
- gnext(mp) = NULL; /* now `mp' is free */
- setnilvalue(gval(mp));
- }
- else { /* colliding node is in its own main position */
- /* new node will go into free position */
- gnext(n) = gnext(mp); /* chain new position */
- gnext(mp) = n;
- mp = n;
- }
- }
- gkey(mp)->value = key->value; gkey(mp)->tt = key->tt;
- luaC_barriert(L, t, key);
- lua_assert(ttisnil(gval(mp)));
- return gval(mp);
-}
-
-
-/*
-** search function for integers
-*/
-const TValue *luaH_getnum (Table *t, int key) {
- /* (1 <= key && key <= t->sizearray) */
- if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray))
- return &t->array[key-1];
- else {
- lua_Number nk = cast_num(key);
- Node *n = hashnum(t, nk);
- do { /* check whether `key' is somewhere in the chain */
- if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk))
- return gval(n); /* that's it */
- else n = gnext(n);
- } while (n);
- return luaO_nilobject;
- }
-}
-
-
-/*
-** search function for strings
-*/
-const TValue *luaH_getstr (Table *t, TString *key) {
- Node *n = hashstr(t, key);
- do { /* check whether `key' is somewhere in the chain */
- if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key)
- return gval(n); /* that's it */
- else n = gnext(n);
- } while (n);
- return luaO_nilobject;
-}
-
-
-/*
-** main search function
-*/
-const TValue *luaH_get (Table *t, const TValue *key) {
- switch (ttype(key)) {
- case LUA_TNIL: return luaO_nilobject;
- case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
- case LUA_TNUMBER: {
- int k;
- lua_Number n = nvalue(key);
- lua_number2int(k, n);
- if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */
- return luaH_getnum(t, k); /* use specialized version */
- /* else go through */
- }
- default: {
- Node *n = mainposition(t, key);
- do { /* check whether `key' is somewhere in the chain */
- if (luaO_rawequalObj(key2tval(n), key))
- return gval(n); /* that's it */
- else n = gnext(n);
- } while (n);
- return luaO_nilobject;
- }
- }
-}
-
-
-TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
- const TValue *p = luaH_get(t, key);
- t->flags = 0;
- if (p != luaO_nilobject)
- return cast(TValue *, p);
- else {
- if (ttisnil(key)) luaG_runerror(L, "table index is nil");
- else if (ttisnumber(key) && luai_numisnan(nvalue(key)))
- luaG_runerror(L, "table index is NaN");
- return newkey(L, t, key);
- }
-}
-
-
-TValue *luaH_setnum (lua_State *L, Table *t, int key) {
- const TValue *p = luaH_getnum(t, key);
- if (p != luaO_nilobject)
- return cast(TValue *, p);
- else {
- TValue k;
- setnvalue(&k, cast_num(key));
- return newkey(L, t, &k);
- }
-}
-
-
-TValue *luaH_setstr (lua_State *L, Table *t, TString *key) {
- const TValue *p = luaH_getstr(t, key);
- if (p != luaO_nilobject)
- return cast(TValue *, p);
- else {
- TValue k;
- setsvalue(L, &k, key);
- return newkey(L, t, &k);
- }
-}
-
-
-static int unbound_search (Table *t, unsigned int j) {
- unsigned int i = j; /* i is zero or a present index */
- j++;
- /* find `i' and `j' such that i is present and j is not */
- while (!ttisnil(luaH_getnum(t, j))) {
- i = j;
- j *= 2;
- if (j > cast(unsigned int, MAX_INT)) { /* overflow? */
- /* table was built with bad purposes: resort to linear search */
- i = 1;
- while (!ttisnil(luaH_getnum(t, i))) i++;
- return i - 1;
- }
- }
- /* now do a binary search between them */
- while (j - i > 1) {
- unsigned int m = (i+j)/2;
- if (ttisnil(luaH_getnum(t, m))) j = m;
- else i = m;
- }
- return i;
-}
-
-
-/*
-** Try to find a boundary in table `t'. A `boundary' is an integer index
-** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
-*/
-int luaH_getn (Table *t) {
- unsigned int j = t->sizearray;
- if (j > 0 && ttisnil(&t->array[j - 1])) {
- /* there is a boundary in the array part: (binary) search for it */
- unsigned int i = 0;
- while (j - i > 1) {
- unsigned int m = (i+j)/2;
- if (ttisnil(&t->array[m - 1])) j = m;
- else i = m;
- }
- return i;
- }
- /* else must find a boundary in hash part */
- else if (t->node == dummynode) /* hash part is empty? */
- return j; /* that is easy... */
- else return unbound_search(t, j);
-}
-
-
-
-#if defined(LUA_DEBUG)
-
-Node *luaH_mainposition (const Table *t, const TValue *key) {
- return mainposition(t, key);
-}
-
-int luaH_isdummy (Node *n) { return n == dummynode; }
-
-#endif
diff --git a/src/lua/src/ltable.h b/src/lua/src/ltable.h
deleted file mode 100644
index f5b9d5ead..000000000
--- a/src/lua/src/ltable.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
-** $Id: ltable.h,v 2.10.1.1 2007/12/27 13:02:25 roberto Exp $
-** Lua tables (hash)
-** See Copyright Notice in lua.h
-*/
-
-#ifndef ltable_h
-#define ltable_h
-
-#include "lobject.h"
-
-
-#define gnode(t,i) (&(t)->node[i])
-#define gkey(n) (&(n)->i_key.nk)
-#define gval(n) (&(n)->i_val)
-#define gnext(n) ((n)->i_key.nk.next)
-
-#define key2tval(n) (&(n)->i_key.tvk)
-
-
-LUAI_FUNC const TValue *luaH_getnum (Table *t, int key);
-LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key);
-LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
-LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key);
-LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
-LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key);
-LUAI_FUNC Table *luaH_new (lua_State *L, int narray, int lnhash);
-LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize);
-LUAI_FUNC void luaH_free (lua_State *L, Table *t);
-LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key);
-LUAI_FUNC int luaH_getn (Table *t);
-
-
-#if defined(LUA_DEBUG)
-LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key);
-LUAI_FUNC int luaH_isdummy (Node *n);
-#endif
-
-
-#endif
diff --git a/src/lua/src/ltablib.c b/src/lua/src/ltablib.c
deleted file mode 100644
index b6d9cb4ac..000000000
--- a/src/lua/src/ltablib.c
+++ /dev/null
@@ -1,287 +0,0 @@
-/*
-** $Id: ltablib.c,v 1.38.1.3 2008/02/14 16:46:58 roberto Exp $
-** Library for Table Manipulation
-** See Copyright Notice in lua.h
-*/
-
-
-#include <stddef.h>
-
-#define ltablib_c
-#define LUA_LIB
-
-#include "lua.h"
-
-#include "lauxlib.h"
-#include "lualib.h"
-
-
-#define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_getn(L, n))
-
-
-static int foreachi (lua_State *L) {
- int i;
- int n = aux_getn(L, 1);
- luaL_checktype(L, 2, LUA_TFUNCTION);
- for (i=1; i <= n; i++) {
- lua_pushvalue(L, 2); /* function */
- lua_pushinteger(L, i); /* 1st argument */
- lua_rawgeti(L, 1, i); /* 2nd argument */
- lua_call(L, 2, 1);
- if (!lua_isnil(L, -1))
- return 1;
- lua_pop(L, 1); /* remove nil result */
- }
- return 0;
-}
-
-
-static int foreach (lua_State *L) {
- luaL_checktype(L, 1, LUA_TTABLE);
- luaL_checktype(L, 2, LUA_TFUNCTION);
- lua_pushnil(L); /* first key */
- while (lua_next(L, 1)) {
- lua_pushvalue(L, 2); /* function */
- lua_pushvalue(L, -3); /* key */
- lua_pushvalue(L, -3); /* value */
- lua_call(L, 2, 1);
- if (!lua_isnil(L, -1))
- return 1;
- lua_pop(L, 2); /* remove value and result */
- }
- return 0;
-}
-
-
-static int maxn (lua_State *L) {
- lua_Number max = 0;
- luaL_checktype(L, 1, LUA_TTABLE);
- lua_pushnil(L); /* first key */
- while (lua_next(L, 1)) {
- lua_pop(L, 1); /* remove value */
- if (lua_type(L, -1) == LUA_TNUMBER) {
- lua_Number v = lua_tonumber(L, -1);
- if (v > max) max = v;
- }
- }
- lua_pushnumber(L, max);
- return 1;
-}
-
-
-static int getn (lua_State *L) {
- lua_pushinteger(L, aux_getn(L, 1));
- return 1;
-}
-
-
-static int setn (lua_State *L) {
- luaL_checktype(L, 1, LUA_TTABLE);
-#ifndef luaL_setn
- luaL_setn(L, 1, luaL_checkint(L, 2));
-#else
- luaL_error(L, LUA_QL("setn") " is obsolete");
-#endif
- lua_pushvalue(L, 1);
- return 1;
-}
-
-
-static int tinsert (lua_State *L) {
- int e = aux_getn(L, 1) + 1; /* first empty element */
- int pos; /* where to insert new element */
- switch (lua_gettop(L)) {
- case 2: { /* called with only 2 arguments */
- pos = e; /* insert new element at the end */
- break;
- }
- case 3: {
- int i;
- pos = luaL_checkint(L, 2); /* 2nd argument is the position */
- if (pos > e) e = pos; /* `grow' array if necessary */
- for (i = e; i > pos; i--) { /* move up elements */
- lua_rawgeti(L, 1, i-1);
- lua_rawseti(L, 1, i); /* t[i] = t[i-1] */
- }
- break;
- }
- default: {
- return luaL_error(L, "wrong number of arguments to " LUA_QL("insert"));
- }
- }
- luaL_setn(L, 1, e); /* new size */
- lua_rawseti(L, 1, pos); /* t[pos] = v */
- return 0;
-}
-
-
-static int tremove (lua_State *L) {
- int e = aux_getn(L, 1);
- int pos = luaL_optint(L, 2, e);
- if (!(1 <= pos && pos <= e)) /* position is outside bounds? */
- return 0; /* nothing to remove */
- luaL_setn(L, 1, e - 1); /* t.n = n-1 */
- lua_rawgeti(L, 1, pos); /* result = t[pos] */
- for ( ;pos<e; pos++) {
- lua_rawgeti(L, 1, pos+1);
- lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */
- }
- lua_pushnil(L);
- lua_rawseti(L, 1, e); /* t[e] = nil */
- return 1;
-}
-
-
-static void addfield (lua_State *L, luaL_Buffer *b, int i) {
- lua_rawgeti(L, 1, i);
- if (!lua_isstring(L, -1))
- luaL_error(L, "invalid value (%s) at index %d in table for "
- LUA_QL("concat"), luaL_typename(L, -1), i);
- luaL_addvalue(b);
-}
-
-
-static int tconcat (lua_State *L) {
- luaL_Buffer b;
- size_t lsep;
- int i, last;
- const char *sep = luaL_optlstring(L, 2, "", &lsep);
- luaL_checktype(L, 1, LUA_TTABLE);
- i = luaL_optint(L, 3, 1);
- last = luaL_opt(L, luaL_checkint, 4, luaL_getn(L, 1));
- luaL_buffinit(L, &b);
- for (; i < last; i++) {
- addfield(L, &b, i);
- luaL_addlstring(&b, sep, lsep);
- }
- if (i == last) /* add last value (if interval was not empty) */
- addfield(L, &b, i);
- luaL_pushresult(&b);
- return 1;
-}
-
-
-
-/*
-** {======================================================
-** Quicksort
-** (based on `Algorithms in MODULA-3', Robert Sedgewick;
-** Addison-Wesley, 1993.)
-*/
-
-
-static void set2 (lua_State *L, int i, int j) {
- lua_rawseti(L, 1, i);
- lua_rawseti(L, 1, j);
-}
-
-static int sort_comp (lua_State *L, int a, int b) {
- if (!lua_isnil(L, 2)) { /* function? */
- int res;
- lua_pushvalue(L, 2);
- lua_pushvalue(L, a-1); /* -1 to compensate function */
- lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
- lua_call(L, 2, 1);
- res = lua_toboolean(L, -1);
- lua_pop(L, 1);
- return res;
- }
- else /* a < b? */
- return lua_lessthan(L, a, b);
-}
-
-static void auxsort (lua_State *L, int l, int u) {
- while (l < u) { /* for tail recursion */
- int i, j;
- /* sort elements a[l], a[(l+u)/2] and a[u] */
- lua_rawgeti(L, 1, l);
- lua_rawgeti(L, 1, u);
- if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
- set2(L, l, u); /* swap a[l] - a[u] */
- else
- lua_pop(L, 2);
- if (u-l == 1) break; /* only 2 elements */
- i = (l+u)/2;
- lua_rawgeti(L, 1, i);
- lua_rawgeti(L, 1, l);
- if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
- set2(L, i, l);
- else {
- lua_pop(L, 1); /* remove a[l] */
- lua_rawgeti(L, 1, u);
- if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
- set2(L, i, u);
- else
- lua_pop(L, 2);
- }
- if (u-l == 2) break; /* only 3 elements */
- lua_rawgeti(L, 1, i); /* Pivot */
- lua_pushvalue(L, -1);
- lua_rawgeti(L, 1, u-1);
- set2(L, i, u-1);
- /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
- i = l; j = u-1;
- for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
- /* repeat ++i until a[i] >= P */
- while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
- if (i>u) luaL_error(L, "invalid order function for sorting");
- lua_pop(L, 1); /* remove a[i] */
- }
- /* repeat --j until a[j] <= P */
- while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
- if (j<l) luaL_error(L, "invalid order function for sorting");
- lua_pop(L, 1); /* remove a[j] */
- }
- if (j<i) {
- lua_pop(L, 3); /* pop pivot, a[i], a[j] */
- break;
- }
- set2(L, i, j);
- }
- lua_rawgeti(L, 1, u-1);
- lua_rawgeti(L, 1, i);
- set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
- /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
- /* adjust so that smaller half is in [j..i] and larger one in [l..u] */
- if (i-l < u-i) {
- j=l; i=i-1; l=i+2;
- }
- else {
- j=i+1; i=u; u=j-2;
- }
- auxsort(L, j, i); /* call recursively the smaller one */
- } /* repeat the routine for the larger one */
-}
-
-static int sort (lua_State *L) {
- int n = aux_getn(L, 1);
- luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */
- if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
- luaL_checktype(L, 2, LUA_TFUNCTION);
- lua_settop(L, 2); /* make sure there is two arguments */
- auxsort(L, 1, n);
- return 0;
-}
-
-/* }====================================================== */
-
-
-static const luaL_Reg tab_funcs[] = {
- {"concat", tconcat},
- {"foreach", foreach},
- {"foreachi", foreachi},
- {"getn", getn},
- {"maxn", maxn},
- {"insert", tinsert},
- {"remove", tremove},
- {"setn", setn},
- {"sort", sort},
- {NULL, NULL}
-};
-
-
-LUALIB_API int luaopen_table (lua_State *L) {
- luaL_register(L, LUA_TABLIBNAME, tab_funcs);
- return 1;
-}
-
diff --git a/src/lua/src/ltm.c b/src/lua/src/ltm.c
deleted file mode 100644
index c27f0f6fa..000000000
--- a/src/lua/src/ltm.c
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
-** $Id: ltm.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
-** Tag methods
-** See Copyright Notice in lua.h
-*/
-
-
-#include <string.h>
-
-#define ltm_c
-#define LUA_CORE
-
-#include "lua.h"
-
-#include "lobject.h"
-#include "lstate.h"
-#include "lstring.h"
-#include "ltable.h"
-#include "ltm.h"
-
-
-
-const char *const luaT_typenames[] = {
- "nil", "boolean", "userdata", "number",
- "string", "table", "function", "userdata", "thread",
- "proto", "upval"
-};
-
-
-void luaT_init (lua_State *L) {
- static const char *const luaT_eventname[] = { /* ORDER TM */
- "__index", "__newindex",
- "__gc", "__mode", "__eq",
- "__add", "__sub", "__mul", "__div", "__mod",
- "__pow", "__unm", "__len", "__lt", "__le",
- "__concat", "__call"
- };
- int i;
- for (i=0; i<TM_N; i++) {
- G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
- luaS_fix(G(L)->tmname[i]); /* never collect these names */
- }
-}
-
-
-/*
-** function to be used with macro "fasttm": optimized for absence of
-** tag methods
-*/
-const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
- const TValue *tm = luaH_getstr(events, ename);
- lua_assert(event <= TM_EQ);
- if (ttisnil(tm)) { /* no tag method? */
- events->flags |= cast_byte(1u<<event); /* cache this fact */
- return NULL;
- }
- else return tm;
-}
-
-
-const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
- Table *mt;
- switch (ttype(o)) {
- case LUA_TTABLE:
- mt = hvalue(o)->metatable;
- break;
- case LUA_TUSERDATA:
- mt = uvalue(o)->metatable;
- break;
- default:
- mt = G(L)->mt[ttype(o)];
- }
- return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);
-}
-
diff --git a/src/lua/src/ltm.h b/src/lua/src/ltm.h
deleted file mode 100644
index 64343b781..000000000
--- a/src/lua/src/ltm.h
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
-** $Id: ltm.h,v 2.6.1.1 2007/12/27 13:02:25 roberto Exp $
-** Tag methods
-** See Copyright Notice in lua.h
-*/
-
-#ifndef ltm_h
-#define ltm_h
-
-
-#include "lobject.h"
-
-
-/*
-* WARNING: if you change the order of this enumeration,
-* grep "ORDER TM"
-*/
-typedef enum {
- TM_INDEX,
- TM_NEWINDEX,
- TM_GC,
- TM_MODE,
- TM_EQ, /* last tag method with `fast' access */
- TM_ADD,
- TM_SUB,
- TM_MUL,
- TM_DIV,
- TM_MOD,
- TM_POW,
- TM_UNM,
- TM_LEN,
- TM_LT,
- TM_LE,
- TM_CONCAT,
- TM_CALL,
- TM_N /* number of elements in the enum */
-} TMS;
-
-
-
-#define gfasttm(g,et,e) ((et) == NULL ? NULL : \
- ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e]))
-
-#define fasttm(l,et,e) gfasttm(G(l), et, e)
-
-LUAI_DATA const char *const luaT_typenames[];
-
-
-LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename);
-LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o,
- TMS event);
-LUAI_FUNC void luaT_init (lua_State *L);
-
-#endif
diff --git a/src/lua/src/lua.c b/src/lua/src/lua.c
deleted file mode 100644
index 3a4660932..000000000
--- a/src/lua/src/lua.c
+++ /dev/null
@@ -1,392 +0,0 @@
-/*
-** $Id: lua.c,v 1.160.1.2 2007/12/28 15:32:23 roberto Exp $
-** Lua stand-alone interpreter
-** See Copyright Notice in lua.h
-*/
-
-
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define lua_c
-
-#include "lua.h"
-
-#include "lauxlib.h"
-#include "lualib.h"
-
-
-
-static lua_State *globalL = NULL;
-
-static const char *progname = LUA_PROGNAME;
-
-
-
-static void lstop (lua_State *L, lua_Debug *ar) {
- (void)ar; /* unused arg. */
- lua_sethook(L, NULL, 0, 0);
- luaL_error(L, "interrupted!");
-}
-
-
-static void laction (int i) {
- signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
- terminate process (default action) */
- lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
-}
-
-
-static void print_usage (void) {
- fprintf(stderr,
- "usage: %s [options] [script [args]].\n"
- "Available options are:\n"
- " -e stat execute string " LUA_QL("stat") "\n"
- " -l name require library " LUA_QL("name") "\n"
- " -i enter interactive mode after executing " LUA_QL("script") "\n"
- " -v show version information\n"
- " -- stop handling options\n"
- " - execute stdin and stop handling options\n"
- ,
- progname);
- fflush(stderr);
-}
-
-
-static void l_message (const char *pname, const char *msg) {
- if (pname) fprintf(stderr, "%s: ", pname);
- fprintf(stderr, "%s\n", msg);
- fflush(stderr);
-}
-
-
-static int report (lua_State *L, int status) {
- if (status && !lua_isnil(L, -1)) {
- const char *msg = lua_tostring(L, -1);
- if (msg == NULL) msg = "(error object is not a string)";
- l_message(progname, msg);
- lua_pop(L, 1);
- }
- return status;
-}
-
-
-static int traceback (lua_State *L) {
- if (!lua_isstring(L, 1)) /* 'message' not a string? */
- return 1; /* keep it intact */
- lua_getfield(L, LUA_GLOBALSINDEX, "debug");
- if (!lua_istable(L, -1)) {
- lua_pop(L, 1);
- return 1;
- }
- lua_getfield(L, -1, "traceback");
- if (!lua_isfunction(L, -1)) {
- lua_pop(L, 2);
- return 1;
- }
- lua_pushvalue(L, 1); /* pass error message */
- lua_pushinteger(L, 2); /* skip this function and traceback */
- lua_call(L, 2, 1); /* call debug.traceback */
- return 1;
-}
-
-
-static int docall (lua_State *L, int narg, int clear) {
- int status;
- int base = lua_gettop(L) - narg; /* function index */
- lua_pushcfunction(L, traceback); /* push traceback function */
- lua_insert(L, base); /* put it under chunk and args */
- signal(SIGINT, laction);
- status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
- signal(SIGINT, SIG_DFL);
- lua_remove(L, base); /* remove traceback function */
- /* force a complete garbage collection in case of errors */
- if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
- return status;
-}
-
-
-static void print_version (void) {
- l_message(NULL, LUA_RELEASE " " LUA_COPYRIGHT);
-}
-
-
-static int getargs (lua_State *L, char **argv, int n) {
- int narg;
- int i;
- int argc = 0;
- while (argv[argc]) argc++; /* count total number of arguments */
- narg = argc - (n + 1); /* number of arguments to the script */
- luaL_checkstack(L, narg + 3, "too many arguments to script");
- for (i=n+1; i < argc; i++)
- lua_pushstring(L, argv[i]);
- lua_createtable(L, narg, n + 1);
- for (i=0; i < argc; i++) {
- lua_pushstring(L, argv[i]);
- lua_rawseti(L, -2, i - n);
- }
- return narg;
-}
-
-
-static int dofile (lua_State *L, const char *name) {
- int status = luaL_loadfile(L, name) || docall(L, 0, 1);
- return report(L, status);
-}
-
-
-static int dostring (lua_State *L, const char *s, const char *name) {
- int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
- return report(L, status);
-}
-
-
-static int dolibrary (lua_State *L, const char *name) {
- lua_getglobal(L, "require");
- lua_pushstring(L, name);
- return report(L, docall(L, 1, 1));
-}
-
-
-static const char *get_prompt (lua_State *L, int firstline) {
- const char *p;
- lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
- p = lua_tostring(L, -1);
- if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
- lua_pop(L, 1); /* remove global */
- return p;
-}
-
-
-static int incomplete (lua_State *L, int status) {
- if (status == LUA_ERRSYNTAX) {
- size_t lmsg;
- const char *msg = lua_tolstring(L, -1, &lmsg);
- const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
- if (strstr(msg, LUA_QL("<eof>")) == tp) {
- lua_pop(L, 1);
- return 1;
- }
- }
- return 0; /* else... */
-}
-
-
-static int pushline (lua_State *L, int firstline) {
- char buffer[LUA_MAXINPUT];
- char *b = buffer;
- size_t l;
- const char *prmt = get_prompt(L, firstline);
- if (lua_readline(L, b, prmt) == 0)
- return 0; /* no input */
- l = strlen(b);
- if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
- b[l-1] = '\0'; /* remove it */
- if (firstline && b[0] == '=') /* first line starts with `=' ? */
- lua_pushfstring(L, "return %s", b+1); /* change it to `return' */
- else
- lua_pushstring(L, b);
- lua_freeline(L, b);
- return 1;
-}
-
-
-static int loadline (lua_State *L) {
- int status;
- lua_settop(L, 0);
- if (!pushline(L, 1))
- return -1; /* no input */
- for (;;) { /* repeat until gets a complete line */
- status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
- if (!incomplete(L, status)) break; /* cannot try to add lines? */
- if (!pushline(L, 0)) /* no more input? */
- return -1;
- lua_pushliteral(L, "\n"); /* add a new line... */
- lua_insert(L, -2); /* ...between the two lines */
- lua_concat(L, 3); /* join them */
- }
- lua_saveline(L, 1);
- lua_remove(L, 1); /* remove line */
- return status;
-}
-
-
-static void dotty (lua_State *L) {
- int status;
- const char *oldprogname = progname;
- progname = NULL;
- while ((status = loadline(L)) != -1) {
- if (status == 0) status = docall(L, 0, 0);
- report(L, status);
- if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
- lua_getglobal(L, "print");
- lua_insert(L, 1);
- if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
- l_message(progname, lua_pushfstring(L,
- "error calling " LUA_QL("print") " (%s)",
- lua_tostring(L, -1)));
- }
- }
- lua_settop(L, 0); /* clear stack */
- fputs("\n", stdout);
- fflush(stdout);
- progname = oldprogname;
-}
-
-
-static int handle_script (lua_State *L, char **argv, int n) {
- int status;
- const char *fname;
- int narg = getargs(L, argv, n); /* collect arguments */
- lua_setglobal(L, "arg");
- fname = argv[n];
- if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0)
- fname = NULL; /* stdin */
- status = luaL_loadfile(L, fname);
- lua_insert(L, -(narg+1));
- if (status == 0)
- status = docall(L, narg, 0);
- else
- lua_pop(L, narg);
- return report(L, status);
-}
-
-
-/* check that argument has no extra characters at the end */
-#define notail(x) {if ((x)[2] != '\0') return -1;}
-
-
-static int collectargs (char **argv, int *pi, int *pv, int *pe) {
- int i;
- for (i = 1; argv[i] != NULL; i++) {
- if (argv[i][0] != '-') /* not an option? */
- return i;
- switch (argv[i][1]) { /* option */
- case '-':
- notail(argv[i]);
- return (argv[i+1] != NULL ? i+1 : 0);
- case '\0':
- return i;
- case 'i':
- notail(argv[i]);
- *pi = 1; /* go through */
- case 'v':
- notail(argv[i]);
- *pv = 1;
- break;
- case 'e':
- *pe = 1; /* go through */
- case 'l':
- if (argv[i][2] == '\0') {
- i++;
- if (argv[i] == NULL) return -1;
- }
- break;
- default: return -1; /* invalid option */
- }
- }
- return 0;
-}
-
-
-static int runargs (lua_State *L, char **argv, int n) {
- int i;
- for (i = 1; i < n; i++) {
- if (argv[i] == NULL) continue;
- lua_assert(argv[i][0] == '-');
- switch (argv[i][1]) { /* option */
- case 'e': {
- const char *chunk = argv[i] + 2;
- if (*chunk == '\0') chunk = argv[++i];
- lua_assert(chunk != NULL);
- if (dostring(L, chunk, "=(command line)") != 0)
- return 1;
- break;
- }
- case 'l': {
- const char *filename = argv[i] + 2;
- if (*filename == '\0') filename = argv[++i];
- lua_assert(filename != NULL);
- if (dolibrary(L, filename))
- return 1; /* stop if file fails */
- break;
- }
- default: break;
- }
- }
- return 0;
-}
-
-
-static int handle_luainit (lua_State *L) {
- const char *init = getenv(LUA_INIT);
- if (init == NULL) return 0; /* status OK */
- else if (init[0] == '@')
- return dofile(L, init+1);
- else
- return dostring(L, init, "=" LUA_INIT);
-}
-
-
-struct Smain {
- int argc;
- char **argv;
- int status;
-};
-
-
-static int pmain (lua_State *L) {
- struct Smain *s = (struct Smain *)lua_touserdata(L, 1);
- char **argv = s->argv;
- int script;
- int has_i = 0, has_v = 0, has_e = 0;
- globalL = L;
- if (argv[0] && argv[0][0]) progname = argv[0];
- lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
- luaL_openlibs(L); /* open libraries */
- lua_gc(L, LUA_GCRESTART, 0);
- s->status = handle_luainit(L);
- if (s->status != 0) return 0;
- script = collectargs(argv, &has_i, &has_v, &has_e);
- if (script < 0) { /* invalid args? */
- print_usage();
- s->status = 1;
- return 0;
- }
- if (has_v) print_version();
- s->status = runargs(L, argv, (script > 0) ? script : s->argc);
- if (s->status != 0) return 0;
- if (script)
- s->status = handle_script(L, argv, script);
- if (s->status != 0) return 0;
- if (has_i)
- dotty(L);
- else if (script == 0 && !has_e && !has_v) {
- if (lua_stdin_is_tty()) {
- print_version();
- dotty(L);
- }
- else dofile(L, NULL); /* executes stdin as a file */
- }
- return 0;
-}
-
-
-int main (int argc, char **argv) {
- int status;
- struct Smain s;
- lua_State *L = lua_open(); /* create state */
- if (L == NULL) {
- l_message(argv[0], "cannot create state: not enough memory");
- return EXIT_FAILURE;
- }
- s.argc = argc;
- s.argv = argv;
- status = lua_cpcall(L, &pmain, &s);
- report(L, status);
- lua_close(L);
- return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
-}
-
diff --git a/src/lua/src/lua.h b/src/lua/src/lua.h
deleted file mode 100644
index e4bdfd3b9..000000000
--- a/src/lua/src/lua.h
+++ /dev/null
@@ -1,388 +0,0 @@
-/*
-** $Id: lua.h,v 1.218.1.5 2008/08/06 13:30:12 roberto Exp $
-** Lua - An Extensible Extension Language
-** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
-** See Copyright Notice at the end of this file
-*/
-
-
-#ifndef lua_h
-#define lua_h
-
-#include <stdarg.h>
-#include <stddef.h>
-
-
-#include "luaconf.h"
-
-
-#define LUA_VERSION "Lua 5.1"
-#define LUA_RELEASE "Lua 5.1.4"
-#define LUA_VERSION_NUM 501
-#define LUA_COPYRIGHT "Copyright (C) 1994-2008 Lua.org, PUC-Rio"
-#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"
-
-
-/* mark for precompiled code (`<esc>Lua') */
-#define LUA_SIGNATURE "\033Lua"
-
-/* option for multiple returns in `lua_pcall' and `lua_call' */
-#define LUA_MULTRET (-1)
-
-
-/*
-** pseudo-indices
-*/
-#define LUA_REGISTRYINDEX (-10000)
-#define LUA_ENVIRONINDEX (-10001)
-#define LUA_GLOBALSINDEX (-10002)
-#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i))
-
-
-/* thread status; 0 is OK */
-#define LUA_YIELD 1
-#define LUA_ERRRUN 2
-#define LUA_ERRSYNTAX 3
-#define LUA_ERRMEM 4
-#define LUA_ERRERR 5
-
-
-typedef struct lua_State lua_State;
-
-typedef int (*lua_CFunction) (lua_State *L);
-
-
-/*
-** functions that read/write blocks when loading/dumping Lua chunks
-*/
-typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz);
-
-typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud);
-
-
-/*
-** prototype for memory-allocation functions
-*/
-typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
-
-
-/*
-** basic types
-*/
-#define LUA_TNONE (-1)
-
-#define LUA_TNIL 0
-#define LUA_TBOOLEAN 1
-#define LUA_TLIGHTUSERDATA 2
-#define LUA_TNUMBER 3
-#define LUA_TSTRING 4
-#define LUA_TTABLE 5
-#define LUA_TFUNCTION 6
-#define LUA_TUSERDATA 7
-#define LUA_TTHREAD 8
-
-
-
-/* minimum Lua stack available to a C function */
-#define LUA_MINSTACK 20
-
-
-/*
-** generic extra include file
-*/
-#if defined(LUA_USER_H)
-#include LUA_USER_H
-#endif
-
-
-/* type of numbers in Lua */
-typedef LUA_NUMBER lua_Number;
-
-
-/* type for integer functions */
-typedef LUA_INTEGER lua_Integer;
-
-
-
-/*
-** state manipulation
-*/
-LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud);
-LUA_API void (lua_close) (lua_State *L);
-LUA_API lua_State *(lua_newthread) (lua_State *L);
-
-LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);
-
-
-/*
-** basic stack manipulation
-*/
-LUA_API int (lua_gettop) (lua_State *L);
-LUA_API void (lua_settop) (lua_State *L, int idx);
-LUA_API void (lua_pushvalue) (lua_State *L, int idx);
-LUA_API void (lua_remove) (lua_State *L, int idx);
-LUA_API void (lua_insert) (lua_State *L, int idx);
-LUA_API void (lua_replace) (lua_State *L, int idx);
-LUA_API int (lua_checkstack) (lua_State *L, int sz);
-
-LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n);
-
-
-/*
-** access functions (stack -> C)
-*/
-
-LUA_API int (lua_isnumber) (lua_State *L, int idx);
-LUA_API int (lua_isstring) (lua_State *L, int idx);
-LUA_API int (lua_iscfunction) (lua_State *L, int idx);
-LUA_API int (lua_isuserdata) (lua_State *L, int idx);
-LUA_API int (lua_type) (lua_State *L, int idx);
-LUA_API const char *(lua_typename) (lua_State *L, int tp);
-
-LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2);
-LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2);
-LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2);
-
-LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx);
-LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx);
-LUA_API int (lua_toboolean) (lua_State *L, int idx);
-LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len);
-LUA_API size_t (lua_objlen) (lua_State *L, int idx);
-LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx);
-LUA_API void *(lua_touserdata) (lua_State *L, int idx);
-LUA_API lua_State *(lua_tothread) (lua_State *L, int idx);
-LUA_API const void *(lua_topointer) (lua_State *L, int idx);
-
-
-/*
-** push functions (C -> stack)
-*/
-LUA_API void (lua_pushnil) (lua_State *L);
-LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n);
-LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n);
-LUA_API void (lua_pushlstring) (lua_State *L, const char *s, size_t l);
-LUA_API void (lua_pushstring) (lua_State *L, const char *s);
-LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
- va_list argp);
-LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...);
-LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
-LUA_API void (lua_pushboolean) (lua_State *L, int b);
-LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p);
-LUA_API int (lua_pushthread) (lua_State *L);
-
-
-/*
-** get functions (Lua -> stack)
-*/
-LUA_API void (lua_gettable) (lua_State *L, int idx);
-LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k);
-LUA_API void (lua_rawget) (lua_State *L, int idx);
-LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n);
-LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec);
-LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz);
-LUA_API int (lua_getmetatable) (lua_State *L, int objindex);
-LUA_API void (lua_getfenv) (lua_State *L, int idx);
-
-
-/*
-** set functions (stack -> Lua)
-*/
-LUA_API void (lua_settable) (lua_State *L, int idx);
-LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k);
-LUA_API void (lua_rawset) (lua_State *L, int idx);
-LUA_API void (lua_rawseti) (lua_State *L, int idx, int n);
-LUA_API int (lua_setmetatable) (lua_State *L, int objindex);
-LUA_API int (lua_setfenv) (lua_State *L, int idx);
-
-
-/*
-** `load' and `call' functions (load and run Lua code)
-*/
-LUA_API void (lua_call) (lua_State *L, int nargs, int nresults);
-LUA_API int (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
-LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud);
-LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
- const char *chunkname);
-
-LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data);
-
-
-/*
-** coroutine functions
-*/
-LUA_API int (lua_yield) (lua_State *L, int nresults);
-LUA_API int (lua_resume) (lua_State *L, int narg);
-LUA_API int (lua_status) (lua_State *L);
-
-/*
-** garbage-collection function and options
-*/
-
-#define LUA_GCSTOP 0
-#define LUA_GCRESTART 1
-#define LUA_GCCOLLECT 2
-#define LUA_GCCOUNT 3
-#define LUA_GCCOUNTB 4
-#define LUA_GCSTEP 5
-#define LUA_GCSETPAUSE 6
-#define LUA_GCSETSTEPMUL 7
-
-LUA_API int (lua_gc) (lua_State *L, int what, int data);
-
-
-/*
-** miscellaneous functions
-*/
-
-LUA_API int (lua_error) (lua_State *L);
-
-LUA_API int (lua_next) (lua_State *L, int idx);
-
-LUA_API void (lua_concat) (lua_State *L, int n);
-
-LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
-LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);
-
-
-
-/*
-** ===============================================================
-** some useful macros
-** ===============================================================
-*/
-
-#define lua_pop(L,n) lua_settop(L, -(n)-1)
-
-#define lua_newtable(L) lua_createtable(L, 0, 0)
-
-#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
-
-#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0)
-
-#define lua_strlen(L,i) lua_objlen(L, (i))
-
-#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION)
-#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE)
-#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA)
-#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL)
-#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN)
-#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD)
-#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
-#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
-
-#define lua_pushliteral(L, s) \
- lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
-
-#define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, (s))
-#define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, (s))
-
-#define lua_tostring(L,i) lua_tolstring(L, (i), NULL)
-
-
-
-/*
-** compatibility macros and functions
-*/
-
-#define lua_open() luaL_newstate()
-
-#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX)
-
-#define lua_getgccount(L) lua_gc(L, LUA_GCCOUNT, 0)
-
-#define lua_Chunkreader lua_Reader
-#define lua_Chunkwriter lua_Writer
-
-
-/* hack */
-LUA_API void lua_setlevel (lua_State *from, lua_State *to);
-
-
-/*
-** {======================================================================
-** Debug API
-** =======================================================================
-*/
-
-
-/*
-** Event codes
-*/
-#define LUA_HOOKCALL 0
-#define LUA_HOOKRET 1
-#define LUA_HOOKLINE 2
-#define LUA_HOOKCOUNT 3
-#define LUA_HOOKTAILRET 4
-
-
-/*
-** Event masks
-*/
-#define LUA_MASKCALL (1 << LUA_HOOKCALL)
-#define LUA_MASKRET (1 << LUA_HOOKRET)
-#define LUA_MASKLINE (1 << LUA_HOOKLINE)
-#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT)
-
-typedef struct lua_Debug lua_Debug; /* activation record */
-
-
-/* Functions to be called by the debuger in specific events */
-typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
-
-
-LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar);
-LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
-LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);
-LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);
-LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n);
-LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n);
-
-LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count);
-LUA_API lua_Hook lua_gethook (lua_State *L);
-LUA_API int lua_gethookmask (lua_State *L);
-LUA_API int lua_gethookcount (lua_State *L);
-
-
-struct lua_Debug {
- int event;
- const char *name; /* (n) */
- const char *namewhat; /* (n) `global', `local', `field', `method' */
- const char *what; /* (S) `Lua', `C', `main', `tail' */
- const char *source; /* (S) */
- int currentline; /* (l) */
- int nups; /* (u) number of upvalues */
- int linedefined; /* (S) */
- int lastlinedefined; /* (S) */
- char short_src[LUA_IDSIZE]; /* (S) */
- /* private part */
- int i_ci; /* active function */
-};
-
-/* }====================================================================== */
-
-
-/******************************************************************************
-* Copyright (C) 1994-2008 Lua.org, PUC-Rio. All rights reserved.
-*
-* Permission is hereby granted, free of charge, to any person obtaining
-* a copy of this software and associated documentation files (the
-* "Software"), to deal in the Software without restriction, including
-* without limitation the rights to use, copy, modify, merge, publish,
-* distribute, sublicense, and/or sell copies of the Software, and to
-* permit persons to whom the Software is furnished to do so, subject to
-* the following conditions:
-*
-* The above copyright notice and this permission notice shall be
-* included in all copies or substantial portions of the Software.
-*
-* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-******************************************************************************/
-
-
-#endif
diff --git a/src/lua/src/luac.c b/src/lua/src/luac.c
deleted file mode 100644
index d07017391..000000000
--- a/src/lua/src/luac.c
+++ /dev/null
@@ -1,200 +0,0 @@
-/*
-** $Id: luac.c,v 1.54 2006/06/02 17:37:11 lhf Exp $
-** Lua compiler (saves bytecodes to files; also list bytecodes)
-** See Copyright Notice in lua.h
-*/
-
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define luac_c
-#define LUA_CORE
-
-#include "lua.h"
-#include "lauxlib.h"
-
-#include "ldo.h"
-#include "lfunc.h"
-#include "lmem.h"
-#include "lobject.h"
-#include "lopcodes.h"
-#include "lstring.h"
-#include "lundump.h"
-
-#define PROGNAME "luac" /* default program name */
-#define OUTPUT PROGNAME ".out" /* default output file */
-
-static int listing=0; /* list bytecodes? */
-static int dumping=1; /* dump bytecodes? */
-static int stripping=0; /* strip debug information? */
-static char Output[]={ OUTPUT }; /* default output file name */
-static const char* output=Output; /* actual output file name */
-static const char* progname=PROGNAME; /* actual program name */
-
-static void fatal(const char* message)
-{
- fprintf(stderr,"%s: %s\n",progname,message);
- exit(EXIT_FAILURE);
-}
-
-static void cannot(const char* what)
-{
- fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno));
- exit(EXIT_FAILURE);
-}
-
-static void usage(const char* message)
-{
- if (*message=='-')
- fprintf(stderr,"%s: unrecognized option " LUA_QS "\n",progname,message);
- else
- fprintf(stderr,"%s: %s\n",progname,message);
- fprintf(stderr,
- "usage: %s [options] [filenames].\n"
- "Available options are:\n"
- " - process stdin\n"
- " -l list\n"
- " -o name output to file " LUA_QL("name") " (default is \"%s\")\n"
- " -p parse only\n"
- " -s strip debug information\n"
- " -v show version information\n"
- " -- stop handling options\n",
- progname,Output);
- exit(EXIT_FAILURE);
-}
-
-#define IS(s) (strcmp(argv[i],s)==0)
-
-static int doargs(int argc, char* argv[])
-{
- int i;
- int version=0;
- if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0];
- for (i=1; i<argc; i++)
- {
- if (*argv[i]!='-') /* end of options; keep it */
- break;
- else if (IS("--")) /* end of options; skip it */
- {
- ++i;
- if (version) ++version;
- break;
- }
- else if (IS("-")) /* end of options; use stdin */
- break;
- else if (IS("-l")) /* list */
- ++listing;
- else if (IS("-o")) /* output file */
- {
- output=argv[++i];
- if (output==NULL || *output==0) usage(LUA_QL("-o") " needs argument");
- if (IS("-")) output=NULL;
- }
- else if (IS("-p")) /* parse only */
- dumping=0;
- else if (IS("-s")) /* strip debug information */
- stripping=1;
- else if (IS("-v")) /* show version */
- ++version;
- else /* unknown option */
- usage(argv[i]);
- }
- if (i==argc && (listing || !dumping))
- {
- dumping=0;
- argv[--i]=Output;
- }
- if (version)
- {
- printf("%s %s\n",LUA_RELEASE,LUA_COPYRIGHT);
- if (version==argc-1) exit(EXIT_SUCCESS);
- }
- return i;
-}
-
-#define toproto(L,i) (clvalue(L->top+(i))->l.p)
-
-static const Proto* combine(lua_State* L, int n)
-{
- if (n==1)
- return toproto(L,-1);
- else
- {
- int i,pc;
- Proto* f=luaF_newproto(L);
- setptvalue2s(L,L->top,f); incr_top(L);
- f->source=luaS_newliteral(L,"=(" PROGNAME ")");
- f->maxstacksize=1;
- pc=2*n+1;
- f->code=luaM_newvector(L,pc,Instruction);
- f->sizecode=pc;
- f->p=luaM_newvector(L,n,Proto*);
- f->sizep=n;
- pc=0;
- for (i=0; i<n; i++)
- {
- f->p[i]=toproto(L,i-n-1);
- f->code[pc++]=CREATE_ABx(OP_CLOSURE,0,i);
- f->code[pc++]=CREATE_ABC(OP_CALL,0,1,1);
- }
- f->code[pc++]=CREATE_ABC(OP_RETURN,0,1,0);
- return f;
- }
-}
-
-static int writer(lua_State* L, const void* p, size_t size, void* u)
-{
- UNUSED(L);
- return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0);
-}
-
-struct Smain {
- int argc;
- char** argv;
-};
-
-static int pmain(lua_State* L)
-{
- struct Smain* s = (struct Smain*)lua_touserdata(L, 1);
- int argc=s->argc;
- char** argv=s->argv;
- const Proto* f;
- int i;
- if (!lua_checkstack(L,argc)) fatal("too many input files");
- for (i=0; i<argc; i++)
- {
- const char* filename=IS("-") ? NULL : argv[i];
- if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1));
- }
- f=combine(L,argc);
- if (listing) luaU_print(f,listing>1);
- if (dumping)
- {
- FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
- if (D==NULL) cannot("open");
- lua_lock(L);
- luaU_dump(L,f,writer,D,stripping);
- lua_unlock(L);
- if (ferror(D)) cannot("write");
- if (fclose(D)) cannot("close");
- }
- return 0;
-}
-
-int main(int argc, char* argv[])
-{
- lua_State* L;
- struct Smain s;
- int i=doargs(argc,argv);
- argc-=i; argv+=i;
- if (argc<=0) usage("no input files given");
- L=lua_open();
- if (L==NULL) fatal("not enough memory for state");
- s.argc=argc;
- s.argv=argv;
- if (lua_cpcall(L,pmain,&s)!=0) fatal(lua_tostring(L,-1));
- lua_close(L);
- return EXIT_SUCCESS;
-}
diff --git a/src/lua/src/luaconf.h b/src/lua/src/luaconf.h
deleted file mode 100644
index e2cb26163..000000000
--- a/src/lua/src/luaconf.h
+++ /dev/null
@@ -1,763 +0,0 @@
-/*
-** $Id: luaconf.h,v 1.82.1.7 2008/02/11 16:25:08 roberto Exp $
-** Configuration file for Lua
-** See Copyright Notice in lua.h
-*/
-
-
-#ifndef lconfig_h
-#define lconfig_h
-
-#include <limits.h>
-#include <stddef.h>
-
-
-/*
-** ==================================================================
-** Search for "@@" to find all configurable definitions.
-** ===================================================================
-*/
-
-
-/*
-@@ LUA_ANSI controls the use of non-ansi features.
-** CHANGE it (define it) if you want Lua to avoid the use of any
-** non-ansi feature or library.
-*/
-#if defined(__STRICT_ANSI__)
-#define LUA_ANSI
-#endif
-
-
-#if !defined(LUA_ANSI) && defined(_WIN32)
-#define LUA_WIN
-#endif
-
-#if defined(LUA_USE_LINUX)
-#define LUA_USE_POSIX
-#define LUA_USE_DLOPEN /* needs an extra library: -ldl */
-#define LUA_USE_READLINE /* needs some extra libraries */
-#endif
-
-#if defined(LUA_USE_MACOSX)
-#define LUA_USE_POSIX
-#define LUA_DL_DYLD /* does not need extra library */
-#endif
-
-
-
-/*
-@@ LUA_USE_POSIX includes all functionallity listed as X/Open System
-@* Interfaces Extension (XSI).
-** CHANGE it (define it) if your system is XSI compatible.
-*/
-#if defined(LUA_USE_POSIX)
-#define LUA_USE_MKSTEMP
-#define LUA_USE_ISATTY
-#define LUA_USE_POPEN
-#define LUA_USE_ULONGJMP
-#endif
-
-
-/*
-@@ LUA_PATH and LUA_CPATH are the names of the environment variables that
-@* Lua check to set its paths.
-@@ LUA_INIT is the name of the environment variable that Lua
-@* checks for initialization code.
-** CHANGE them if you want different names.
-*/
-#define LUA_PATH "LUA_PATH"
-#define LUA_CPATH "LUA_CPATH"
-#define LUA_INIT "LUA_INIT"
-
-
-/*
-@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for
-@* Lua libraries.
-@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for
-@* C libraries.
-** CHANGE them if your machine has a non-conventional directory
-** hierarchy or if you want to install your libraries in
-** non-conventional directories.
-*/
-#if defined(_WIN32)
-/*
-** In Windows, any exclamation mark ('!') in the path is replaced by the
-** path of the directory of the executable file of the current process.
-*/
-#define LUA_LDIR "!\\lua\\"
-#define LUA_CDIR "!\\"
-#define LUA_PATH_DEFAULT \
- ".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \
- LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua"
-#define LUA_CPATH_DEFAULT \
- ".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll"
-
-#else
-#define LUA_ROOT "/usr/local/"
-#define LUA_LDIR LUA_ROOT "share/lua/5.1/"
-#define LUA_CDIR LUA_ROOT "lib/lua/5.1/"
-#define LUA_PATH_DEFAULT \
- "./?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \
- LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua"
-#define LUA_CPATH_DEFAULT \
- "./?.so;" LUA_CDIR"?.so;" LUA_CDIR"loadall.so"
-#endif
-
-
-/*
-@@ LUA_DIRSEP is the directory separator (for submodules).
-** CHANGE it if your machine does not use "/" as the directory separator
-** and is not Windows. (On Windows Lua automatically uses "\".)
-*/
-#if defined(_WIN32)
-#define LUA_DIRSEP "\\"
-#else
-#define LUA_DIRSEP "/"
-#endif
-
-
-/*
-@@ LUA_PATHSEP is the character that separates templates in a path.
-@@ LUA_PATH_MARK is the string that marks the substitution points in a
-@* template.
-@@ LUA_EXECDIR in a Windows path is replaced by the executable's
-@* directory.
-@@ LUA_IGMARK is a mark to ignore all before it when bulding the
-@* luaopen_ function name.
-** CHANGE them if for some reason your system cannot use those
-** characters. (E.g., if one of those characters is a common character
-** in file/directory names.) Probably you do not need to change them.
-*/
-#define LUA_PATHSEP ";"
-#define LUA_PATH_MARK "?"
-#define LUA_EXECDIR "!"
-#define LUA_IGMARK "-"
-
-
-/*
-@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger.
-** CHANGE that if ptrdiff_t is not adequate on your machine. (On most
-** machines, ptrdiff_t gives a good choice between int or long.)
-*/
-#define LUA_INTEGER ptrdiff_t
-
-
-/*
-@@ LUA_API is a mark for all core API functions.
-@@ LUALIB_API is a mark for all standard library functions.
-** CHANGE them if you need to define those functions in some special way.
-** For instance, if you want to create one Windows DLL with the core and
-** the libraries, you may want to use the following definition (define
-** LUA_BUILD_AS_DLL to get it).
-*/
-#if defined(LUA_BUILD_AS_DLL)
-
-#if defined(LUA_CORE) || defined(LUA_LIB)
-#define LUA_API __declspec(dllexport)
-#else
-#define LUA_API __declspec(dllimport)
-#endif
-
-#else
-
-#define LUA_API extern
-
-#endif
-
-/* more often than not the libs go together with the core */
-#define LUALIB_API LUA_API
-
-
-/*
-@@ LUAI_FUNC is a mark for all extern functions that are not to be
-@* exported to outside modules.
-@@ LUAI_DATA is a mark for all extern (const) variables that are not to
-@* be exported to outside modules.
-** CHANGE them if you need to mark them in some special way. Elf/gcc
-** (versions 3.2 and later) mark them as "hidden" to optimize access
-** when Lua is compiled as a shared library.
-*/
-#if defined(luaall_c)
-#define LUAI_FUNC static
-#define LUAI_DATA /* empty */
-
-#elif defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \
- defined(__ELF__)
-#define LUAI_FUNC __attribute__((visibility("hidden"))) extern
-#define LUAI_DATA LUAI_FUNC
-
-#else
-#define LUAI_FUNC extern
-#define LUAI_DATA extern
-#endif
-
-
-
-/*
-@@ LUA_QL describes how error messages quote program elements.
-** CHANGE it if you want a different appearance.
-*/
-#define LUA_QL(x) "'" x "'"
-#define LUA_QS LUA_QL("%s")
-
-
-/*
-@@ LUA_IDSIZE gives the maximum size for the description of the source
-@* of a function in debug information.
-** CHANGE it if you want a different size.
-*/
-#define LUA_IDSIZE 60
-
-
-/*
-** {==================================================================
-** Stand-alone configuration
-** ===================================================================
-*/
-
-#if defined(lua_c) || defined(luaall_c)
-
-/*
-@@ lua_stdin_is_tty detects whether the standard input is a 'tty' (that
-@* is, whether we're running lua interactively).
-** CHANGE it if you have a better definition for non-POSIX/non-Windows
-** systems.
-*/
-#if defined(LUA_USE_ISATTY)
-#include <unistd.h>
-#define lua_stdin_is_tty() isatty(0)
-#elif defined(LUA_WIN)
-#include <io.h>
-#include <stdio.h>
-#define lua_stdin_is_tty() _isatty(_fileno(stdin))
-#else
-#define lua_stdin_is_tty() 1 /* assume stdin is a tty */
-#endif
-
-
-/*
-@@ LUA_PROMPT is the default prompt used by stand-alone Lua.
-@@ LUA_PROMPT2 is the default continuation prompt used by stand-alone Lua.
-** CHANGE them if you want different prompts. (You can also change the
-** prompts dynamically, assigning to globals _PROMPT/_PROMPT2.)
-*/
-#define LUA_PROMPT "> "
-#define LUA_PROMPT2 ">> "
-
-
-/*
-@@ LUA_PROGNAME is the default name for the stand-alone Lua program.
-** CHANGE it if your stand-alone interpreter has a different name and
-** your system is not able to detect that name automatically.
-*/
-#define LUA_PROGNAME "lua"
-
-
-/*
-@@ LUA_MAXINPUT is the maximum length for an input line in the
-@* stand-alone interpreter.
-** CHANGE it if you need longer lines.
-*/
-#define LUA_MAXINPUT 512
-
-
-/*
-@@ lua_readline defines how to show a prompt and then read a line from
-@* the standard input.
-@@ lua_saveline defines how to "save" a read line in a "history".
-@@ lua_freeline defines how to free a line read by lua_readline.
-** CHANGE them if you want to improve this functionality (e.g., by using
-** GNU readline and history facilities).
-*/
-#if defined(LUA_USE_READLINE)
-#include <stdio.h>
-#include <readline/readline.h>
-#include <readline/history.h>
-#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL)
-#define lua_saveline(L,idx) \
- if (lua_strlen(L,idx) > 0) /* non-empty line? */ \
- add_history(lua_tostring(L, idx)); /* add it to history */
-#define lua_freeline(L,b) ((void)L, free(b))
-#else
-#define lua_readline(L,b,p) \
- ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \
- fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */
-#define lua_saveline(L,idx) { (void)L; (void)idx; }
-#define lua_freeline(L,b) { (void)L; (void)b; }
-#endif
-
-#endif
-
-/* }================================================================== */
-
-
-/*
-@@ LUAI_GCPAUSE defines the default pause between garbage-collector cycles
-@* as a percentage.
-** CHANGE it if you want the GC to run faster or slower (higher values
-** mean larger pauses which mean slower collection.) You can also change
-** this value dynamically.
-*/
-#define LUAI_GCPAUSE 200 /* 200% (wait memory to double before next GC) */
-
-
-/*
-@@ LUAI_GCMUL defines the default speed of garbage collection relative to
-@* memory allocation as a percentage.
-** CHANGE it if you want to change the granularity of the garbage
-** collection. (Higher values mean coarser collections. 0 represents
-** infinity, where each step performs a full collection.) You can also
-** change this value dynamically.
-*/
-#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
-
-
-
-/*
-@@ LUA_COMPAT_GETN controls compatibility with old getn behavior.
-** CHANGE it (define it) if you want exact compatibility with the
-** behavior of setn/getn in Lua 5.0.
-*/
-#undef LUA_COMPAT_GETN
-
-/*
-@@ LUA_COMPAT_LOADLIB controls compatibility about global loadlib.
-** CHANGE it to undefined as soon as you do not need a global 'loadlib'
-** function (the function is still available as 'package.loadlib').
-*/
-#undef LUA_COMPAT_LOADLIB
-
-/*
-@@ LUA_COMPAT_VARARG controls compatibility with old vararg feature.
-** CHANGE it to undefined as soon as your programs use only '...' to
-** access vararg parameters (instead of the old 'arg' table).
-*/
-#define LUA_COMPAT_VARARG
-
-/*
-@@ LUA_COMPAT_MOD controls compatibility with old math.mod function.
-** CHANGE it to undefined as soon as your programs use 'math.fmod' or
-** the new '%' operator instead of 'math.mod'.
-*/
-#define LUA_COMPAT_MOD
-
-/*
-@@ LUA_COMPAT_LSTR controls compatibility with old long string nesting
-@* facility.
-** CHANGE it to 2 if you want the old behaviour, or undefine it to turn
-** off the advisory error when nesting [[...]].
-*/
-#define LUA_COMPAT_LSTR 1
-
-/*
-@@ LUA_COMPAT_GFIND controls compatibility with old 'string.gfind' name.
-** CHANGE it to undefined as soon as you rename 'string.gfind' to
-** 'string.gmatch'.
-*/
-#define LUA_COMPAT_GFIND
-
-/*
-@@ LUA_COMPAT_OPENLIB controls compatibility with old 'luaL_openlib'
-@* behavior.
-** CHANGE it to undefined as soon as you replace to 'luaL_register'
-** your uses of 'luaL_openlib'
-*/
-#define LUA_COMPAT_OPENLIB
-
-
-
-/*
-@@ luai_apicheck is the assert macro used by the Lua-C API.
-** CHANGE luai_apicheck if you want Lua to perform some checks in the
-** parameters it gets from API calls. This may slow down the interpreter
-** a bit, but may be quite useful when debugging C code that interfaces
-** with Lua. A useful redefinition is to use assert.h.
-*/
-#if defined(LUA_USE_APICHECK)
-#include <assert.h>
-#define luai_apicheck(L,o) { (void)L; assert(o); }
-#else
-#define luai_apicheck(L,o) { (void)L; }
-#endif
-
-
-/*
-@@ LUAI_BITSINT defines the number of bits in an int.
-** CHANGE here if Lua cannot automatically detect the number of bits of
-** your machine. Probably you do not need to change this.
-*/
-/* avoid overflows in comparison */
-#if INT_MAX-20 < 32760
-#define LUAI_BITSINT 16
-#elif INT_MAX > 2147483640L
-/* int has at least 32 bits */
-#define LUAI_BITSINT 32
-#else
-#error "you must define LUA_BITSINT with number of bits in an integer"
-#endif
-
-
-/*
-@@ LUAI_UINT32 is an unsigned integer with at least 32 bits.
-@@ LUAI_INT32 is an signed integer with at least 32 bits.
-@@ LUAI_UMEM is an unsigned integer big enough to count the total
-@* memory used by Lua.
-@@ LUAI_MEM is a signed integer big enough to count the total memory
-@* used by Lua.
-** CHANGE here if for some weird reason the default definitions are not
-** good enough for your machine. (The definitions in the 'else'
-** part always works, but may waste space on machines with 64-bit
-** longs.) Probably you do not need to change this.
-*/
-#if LUAI_BITSINT >= 32
-#define LUAI_UINT32 unsigned int
-#define LUAI_INT32 int
-#define LUAI_MAXINT32 INT_MAX
-#define LUAI_UMEM size_t
-#define LUAI_MEM ptrdiff_t
-#else
-/* 16-bit ints */
-#define LUAI_UINT32 unsigned long
-#define LUAI_INT32 long
-#define LUAI_MAXINT32 LONG_MAX
-#define LUAI_UMEM unsigned long
-#define LUAI_MEM long
-#endif
-
-
-/*
-@@ LUAI_MAXCALLS limits the number of nested calls.
-** CHANGE it if you need really deep recursive calls. This limit is
-** arbitrary; its only purpose is to stop infinite recursion before
-** exhausting memory.
-*/
-#define LUAI_MAXCALLS 20000
-
-
-/*
-@@ LUAI_MAXCSTACK limits the number of Lua stack slots that a C function
-@* can use.
-** CHANGE it if you need lots of (Lua) stack space for your C
-** functions. This limit is arbitrary; its only purpose is to stop C
-** functions to consume unlimited stack space. (must be smaller than
-** -LUA_REGISTRYINDEX)
-*/
-#define LUAI_MAXCSTACK 8000
-
-
-
-/*
-** {==================================================================
-** CHANGE (to smaller values) the following definitions if your system
-** has a small C stack. (Or you may want to change them to larger
-** values if your system has a large C stack and these limits are
-** too rigid for you.) Some of these constants control the size of
-** stack-allocated arrays used by the compiler or the interpreter, while
-** others limit the maximum number of recursive calls that the compiler
-** or the interpreter can perform. Values too large may cause a C stack
-** overflow for some forms of deep constructs.
-** ===================================================================
-*/
-
-
-/*
-@@ LUAI_MAXCCALLS is the maximum depth for nested C calls (short) and
-@* syntactical nested non-terminals in a program.
-*/
-#define LUAI_MAXCCALLS 200
-
-
-/*
-@@ LUAI_MAXVARS is the maximum number of local variables per function
-@* (must be smaller than 250).
-*/
-#define LUAI_MAXVARS 200
-
-
-/*
-@@ LUAI_MAXUPVALUES is the maximum number of upvalues per function
-@* (must be smaller than 250).
-*/
-#define LUAI_MAXUPVALUES 60
-
-
-/*
-@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.
-*/
-#define LUAL_BUFFERSIZE BUFSIZ
-
-/* }================================================================== */
-
-
-
-
-/*
-** {==================================================================
-@@ LUA_NUMBER is the type of numbers in Lua.
-** CHANGE the following definitions only if you want to build Lua
-** with a number type different from double. You may also need to
-** change lua_number2int & lua_number2integer.
-** ===================================================================
-*/
-
-#define LUA_NUMBER_DOUBLE
-#define LUA_NUMBER double
-
-/*
-@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
-@* over a number.
-*/
-#define LUAI_UACNUMBER double
-
-
-/*
-@@ LUA_NUMBER_SCAN is the format for reading numbers.
-@@ LUA_NUMBER_FMT is the format for writing numbers.
-@@ lua_number2str converts a number to a string.
-@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion.
-@@ lua_str2number converts a string to a number.
-*/
-#define LUA_NUMBER_SCAN "%lf"
-#define LUA_NUMBER_FMT "%.14g"
-#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
-#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */
-#define lua_str2number(s,p) strtod((s), (p))
-
-
-/*
-@@ The luai_num* macros define the primitive operations over numbers.
-*/
-#if defined(LUA_CORE)
-#include <math.h>
-#define luai_numadd(a,b) ((a)+(b))
-#define luai_numsub(a,b) ((a)-(b))
-#define luai_nummul(a,b) ((a)*(b))
-#define luai_numdiv(a,b) ((a)/(b))
-#define luai_nummod(a,b) ((a) - floor((a)/(b))*(b))
-#define luai_numpow(a,b) (pow(a,b))
-#define luai_numunm(a) (-(a))
-#define luai_numeq(a,b) ((a)==(b))
-#define luai_numlt(a,b) ((a)<(b))
-#define luai_numle(a,b) ((a)<=(b))
-#define luai_numisnan(a) (!luai_numeq((a), (a)))
-#endif
-
-
-/*
-@@ lua_number2int is a macro to convert lua_Number to int.
-@@ lua_number2integer is a macro to convert lua_Number to lua_Integer.
-** CHANGE them if you know a faster way to convert a lua_Number to
-** int (with any rounding method and without throwing errors) in your
-** system. In Pentium machines, a naive typecast from double to int
-** in C is extremely slow, so any alternative is worth trying.
-*/
-
-/* On a Pentium, resort to a trick */
-#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \
- (defined(__i386) || defined (_M_IX86) || defined(__i386__))
-
-/* On a Microsoft compiler, use assembler */
-#if defined(_MSC_VER)
-
-#define lua_number2int(i,d) __asm fld d __asm fistp i
-#define lua_number2integer(i,n) lua_number2int(i, n)
-
-/* the next trick should work on any Pentium, but sometimes clashes
- with a DirectX idiosyncrasy */
-#else
-
-union luai_Cast { double l_d; long l_l; };
-#define lua_number2int(i,d) \
- { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
-#define lua_number2integer(i,n) lua_number2int(i, n)
-
-#endif
-
-
-/* this option always works, but may be slow */
-#else
-#define lua_number2int(i,d) ((i)=(int)(d))
-#define lua_number2integer(i,d) ((i)=(lua_Integer)(d))
-
-#endif
-
-/* }================================================================== */
-
-
-/*
-@@ LUAI_USER_ALIGNMENT_T is a type that requires maximum alignment.
-** CHANGE it if your system requires alignments larger than double. (For
-** instance, if your system supports long doubles and they must be
-** aligned in 16-byte boundaries, then you should add long double in the
-** union.) Probably you do not need to change this.
-*/
-#define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; }
-
-
-/*
-@@ LUAI_THROW/LUAI_TRY define how Lua does exception handling.
-** CHANGE them if you prefer to use longjmp/setjmp even with C++
-** or if want/don't to use _longjmp/_setjmp instead of regular
-** longjmp/setjmp. By default, Lua handles errors with exceptions when
-** compiling as C++ code, with _longjmp/_setjmp when asked to use them,
-** and with longjmp/setjmp otherwise.
-*/
-#if defined(__cplusplus)
-/* C++ exceptions */
-#define LUAI_THROW(L,c) throw(c)
-#define LUAI_TRY(L,c,a) try { a } catch(...) \
- { if ((c)->status == 0) (c)->status = -1; }
-#define luai_jmpbuf int /* dummy variable */
-
-#elif defined(LUA_USE_ULONGJMP)
-/* in Unix, try _longjmp/_setjmp (more efficient) */
-#define LUAI_THROW(L,c) _longjmp((c)->b, 1)
-#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a }
-#define luai_jmpbuf jmp_buf
-
-#else
-/* default handling with long jumps */
-#define LUAI_THROW(L,c) longjmp((c)->b, 1)
-#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a }
-#define luai_jmpbuf jmp_buf
-
-#endif
-
-
-/*
-@@ LUA_MAXCAPTURES is the maximum number of captures that a pattern
-@* can do during pattern-matching.
-** CHANGE it if you need more captures. This limit is arbitrary.
-*/
-#define LUA_MAXCAPTURES 32
-
-
-/*
-@@ lua_tmpnam is the function that the OS library uses to create a
-@* temporary name.
-@@ LUA_TMPNAMBUFSIZE is the maximum size of a name created by lua_tmpnam.
-** CHANGE them if you have an alternative to tmpnam (which is considered
-** insecure) or if you want the original tmpnam anyway. By default, Lua
-** uses tmpnam except when POSIX is available, where it uses mkstemp.
-*/
-#if defined(loslib_c) || defined(luaall_c)
-
-#if defined(LUA_USE_MKSTEMP)
-#include <unistd.h>
-#define LUA_TMPNAMBUFSIZE 32
-#define lua_tmpnam(b,e) { \
- strcpy(b, "/tmp/lua_XXXXXX"); \
- e = mkstemp(b); \
- if (e != -1) close(e); \
- e = (e == -1); }
-
-#else
-#define LUA_TMPNAMBUFSIZE L_tmpnam
-#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
-#endif
-
-#endif
-
-
-/*
-@@ lua_popen spawns a new process connected to the current one through
-@* the file streams.
-** CHANGE it if you have a way to implement it in your system.
-*/
-#if defined(LUA_USE_POPEN)
-
-#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m))
-#define lua_pclose(L,file) ((void)L, (pclose(file) != -1))
-
-#elif defined(LUA_WIN)
-
-#define lua_popen(L,c,m) ((void)L, _popen(c,m))
-#define lua_pclose(L,file) ((void)L, (_pclose(file) != -1))
-
-#else
-
-#define lua_popen(L,c,m) ((void)((void)c, m), \
- luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0)
-#define lua_pclose(L,file) ((void)((void)L, file), 0)
-
-#endif
-
-/*
-@@ LUA_DL_* define which dynamic-library system Lua should use.
-** CHANGE here if Lua has problems choosing the appropriate
-** dynamic-library system for your platform (either Windows' DLL, Mac's
-** dyld, or Unix's dlopen). If your system is some kind of Unix, there
-** is a good chance that it has dlopen, so LUA_DL_DLOPEN will work for
-** it. To use dlopen you also need to adapt the src/Makefile (probably
-** adding -ldl to the linker options), so Lua does not select it
-** automatically. (When you change the makefile to add -ldl, you must
-** also add -DLUA_USE_DLOPEN.)
-** If you do not want any kind of dynamic library, undefine all these
-** options.
-** By default, _WIN32 gets LUA_DL_DLL and MAC OS X gets LUA_DL_DYLD.
-*/
-#if defined(LUA_USE_DLOPEN)
-#define LUA_DL_DLOPEN
-#endif
-
-#if defined(LUA_WIN)
-#define LUA_DL_DLL
-#endif
-
-
-/*
-@@ LUAI_EXTRASPACE allows you to add user-specific data in a lua_State
-@* (the data goes just *before* the lua_State pointer).
-** CHANGE (define) this if you really need that. This value must be
-** a multiple of the maximum alignment required for your machine.
-*/
-#define LUAI_EXTRASPACE 0
-
-
-/*
-@@ luai_userstate* allow user-specific actions on threads.
-** CHANGE them if you defined LUAI_EXTRASPACE and need to do something
-** extra when a thread is created/deleted/resumed/yielded.
-*/
-#define luai_userstateopen(L) ((void)L)
-#define luai_userstateclose(L) ((void)L)
-#define luai_userstatethread(L,L1) ((void)L)
-#define luai_userstatefree(L) ((void)L)
-#define luai_userstateresume(L,n) ((void)L)
-#define luai_userstateyield(L,n) ((void)L)
-
-
-/*
-@@ LUA_INTFRMLEN is the length modifier for integer conversions
-@* in 'string.format'.
-@@ LUA_INTFRM_T is the integer type correspoding to the previous length
-@* modifier.
-** CHANGE them if your system supports long long or does not support long.
-*/
-
-#if defined(LUA_USELONGLONG)
-
-#define LUA_INTFRMLEN "ll"
-#define LUA_INTFRM_T long long
-
-#else
-
-#define LUA_INTFRMLEN "l"
-#define LUA_INTFRM_T long
-
-#endif
-
-
-
-/* =================================================================== */
-
-/*
-** Local configuration. You can use this space to add your redefinitions
-** without modifying the main part of the file.
-*/
-
-
-
-#endif
-
diff --git a/src/lua/src/lualib.h b/src/lua/src/lualib.h
deleted file mode 100644
index 469417f67..000000000
--- a/src/lua/src/lualib.h
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
-** $Id: lualib.h,v 1.36.1.1 2007/12/27 13:02:25 roberto Exp $
-** Lua standard libraries
-** See Copyright Notice in lua.h
-*/
-
-
-#ifndef lualib_h
-#define lualib_h
-
-#include "lua.h"
-
-
-/* Key to file-handle type */
-#define LUA_FILEHANDLE "FILE*"
-
-
-#define LUA_COLIBNAME "coroutine"
-LUALIB_API int (luaopen_base) (lua_State *L);
-
-#define LUA_TABLIBNAME "table"
-LUALIB_API int (luaopen_table) (lua_State *L);
-
-#define LUA_IOLIBNAME "io"
-LUALIB_API int (luaopen_io) (lua_State *L);
-
-#define LUA_OSLIBNAME "os"
-LUALIB_API int (luaopen_os) (lua_State *L);
-
-#define LUA_STRLIBNAME "string"
-LUALIB_API int (luaopen_string) (lua_State *L);
-
-#define LUA_MATHLIBNAME "math"
-LUALIB_API int (luaopen_math) (lua_State *L);
-
-#define LUA_DBLIBNAME "debug"
-LUALIB_API int (luaopen_debug) (lua_State *L);
-
-#define LUA_LOADLIBNAME "package"
-LUALIB_API int (luaopen_package) (lua_State *L);
-
-
-/* open all previous libraries */
-LUALIB_API void (luaL_openlibs) (lua_State *L);
-
-
-
-#ifndef lua_assert
-#define lua_assert(x) ((void)0)
-#endif
-
-
-#endif
diff --git a/src/lua/src/lundump.c b/src/lua/src/lundump.c
deleted file mode 100644
index 8010a4579..000000000
--- a/src/lua/src/lundump.c
+++ /dev/null
@@ -1,227 +0,0 @@
-/*
-** $Id: lundump.c,v 2.7.1.4 2008/04/04 19:51:41 roberto Exp $
-** load precompiled Lua chunks
-** See Copyright Notice in lua.h
-*/
-
-#include <string.h>
-
-#define lundump_c
-#define LUA_CORE
-
-#include "lua.h"
-
-#include "ldebug.h"
-#include "ldo.h"
-#include "lfunc.h"
-#include "lmem.h"
-#include "lobject.h"
-#include "lstring.h"
-#include "lundump.h"
-#include "lzio.h"
-
-typedef struct {
- lua_State* L;
- ZIO* Z;
- Mbuffer* b;
- const char* name;
-} LoadState;
-
-#ifdef LUAC_TRUST_BINARIES
-#define IF(c,s)
-#define error(S,s)
-#else
-#define IF(c,s) if (c) error(S,s)
-
-static void error(LoadState* S, const char* why)
-{
- luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
- luaD_throw(S->L,LUA_ERRSYNTAX);
-}
-#endif
-
-#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
-#define LoadByte(S) (lu_byte)LoadChar(S)
-#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
-#define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
-
-static void LoadBlock(LoadState* S, void* b, size_t size)
-{
- size_t r=luaZ_read(S->Z,b,size);
- IF (r!=0, "unexpected end");
-}
-
-static int LoadChar(LoadState* S)
-{
- char x;
- LoadVar(S,x);
- return x;
-}
-
-static int LoadInt(LoadState* S)
-{
- int x;
- LoadVar(S,x);
- IF (x<0, "bad integer");
- return x;
-}
-
-static lua_Number LoadNumber(LoadState* S)
-{
- lua_Number x;
- LoadVar(S,x);
- return x;
-}
-
-static TString* LoadString(LoadState* S)
-{
- size_t size;
- LoadVar(S,size);
- if (size==0)
- return NULL;
- else
- {
- char* s=luaZ_openspace(S->L,S->b,size);
- LoadBlock(S,s,size);
- return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
- }
-}
-
-static void LoadCode(LoadState* S, Proto* f)
-{
- int n=LoadInt(S);
- f->code=luaM_newvector(S->L,n,Instruction);
- f->sizecode=n;
- LoadVector(S,f->code,n,sizeof(Instruction));
-}
-
-static Proto* LoadFunction(LoadState* S, TString* p);
-
-static void LoadConstants(LoadState* S, Proto* f)
-{
- int i,n;
- n=LoadInt(S);
- f->k=luaM_newvector(S->L,n,TValue);
- f->sizek=n;
- for (i=0; i<n; i++) setnilvalue(&f->k[i]);
- for (i=0; i<n; i++)
- {
- TValue* o=&f->k[i];
- int t=LoadChar(S);
- switch (t)
- {
- case LUA_TNIL:
- setnilvalue(o);
- break;
- case LUA_TBOOLEAN:
- setbvalue(o,LoadChar(S)!=0);
- break;
- case LUA_TNUMBER:
- setnvalue(o,LoadNumber(S));
- break;
- case LUA_TSTRING:
- setsvalue2n(S->L,o,LoadString(S));
- break;
- default:
- error(S,"bad constant");
- break;
- }
- }
- n=LoadInt(S);
- f->p=luaM_newvector(S->L,n,Proto*);
- f->sizep=n;
- for (i=0; i<n; i++) f->p[i]=NULL;
- for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
-}
-
-static void LoadDebug(LoadState* S, Proto* f)
-{
- int i,n;
- n=LoadInt(S);
- f->lineinfo=luaM_newvector(S->L,n,int);
- f->sizelineinfo=n;
- LoadVector(S,f->lineinfo,n,sizeof(int));
- n=LoadInt(S);
- f->locvars=luaM_newvector(S->L,n,LocVar);
- f->sizelocvars=n;
- for (i=0; i<n; i++) f->locvars[i].varname=NULL;
- for (i=0; i<n; i++)
- {
- f->locvars[i].varname=LoadString(S);
- f->locvars[i].startpc=LoadInt(S);
- f->locvars[i].endpc=LoadInt(S);
- }
- n=LoadInt(S);
- f->upvalues=luaM_newvector(S->L,n,TString*);
- f->sizeupvalues=n;
- for (i=0; i<n; i++) f->upvalues[i]=NULL;
- for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
-}
-
-static Proto* LoadFunction(LoadState* S, TString* p)
-{
- Proto* f;
- if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep");
- f=luaF_newproto(S->L);
- setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
- f->source=LoadString(S); if (f->source==NULL) f->source=p;
- f->linedefined=LoadInt(S);
- f->lastlinedefined=LoadInt(S);
- f->nups=LoadByte(S);
- f->numparams=LoadByte(S);
- f->is_vararg=LoadByte(S);
- f->maxstacksize=LoadByte(S);
- LoadCode(S,f);
- LoadConstants(S,f);
- LoadDebug(S,f);
- IF (!luaG_checkcode(f), "bad code");
- S->L->top--;
- S->L->nCcalls--;
- return f;
-}
-
-static void LoadHeader(LoadState* S)
-{
- char h[LUAC_HEADERSIZE];
- char s[LUAC_HEADERSIZE];
- luaU_header(h);
- LoadBlock(S,s,LUAC_HEADERSIZE);
- IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
-}
-
-/*
-** load precompiled chunk
-*/
-Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
-{
- LoadState S;
- if (*name=='@' || *name=='=')
- S.name=name+1;
- else if (*name==LUA_SIGNATURE[0])
- S.name="binary string";
- else
- S.name=name;
- S.L=L;
- S.Z=Z;
- S.b=buff;
- LoadHeader(&S);
- return LoadFunction(&S,luaS_newliteral(L,"=?"));
-}
-
-/*
-* make header
-*/
-void luaU_header (char* h)
-{
- int x=1;
- memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
- h+=sizeof(LUA_SIGNATURE)-1;
- *h++=(char)LUAC_VERSION;
- *h++=(char)LUAC_FORMAT;
- *h++=(char)*(char*)&x; /* endianness */
- *h++=(char)sizeof(int);
- *h++=(char)sizeof(size_t);
- *h++=(char)sizeof(Instruction);
- *h++=(char)sizeof(lua_Number);
- *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
-}
diff --git a/src/lua/src/lundump.h b/src/lua/src/lundump.h
deleted file mode 100644
index c80189dbf..000000000
--- a/src/lua/src/lundump.h
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
-** $Id: lundump.h,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $
-** load precompiled Lua chunks
-** See Copyright Notice in lua.h
-*/
-
-#ifndef lundump_h
-#define lundump_h
-
-#include "lobject.h"
-#include "lzio.h"
-
-/* load one chunk; from lundump.c */
-LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name);
-
-/* make header; from lundump.c */
-LUAI_FUNC void luaU_header (char* h);
-
-/* dump one chunk; from ldump.c */
-LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip);
-
-#ifdef luac_c
-/* print one chunk; from print.c */
-LUAI_FUNC void luaU_print (const Proto* f, int full);
-#endif
-
-/* for header of binary files -- this is Lua 5.1 */
-#define LUAC_VERSION 0x51
-
-/* for header of binary files -- this is the official format */
-#define LUAC_FORMAT 0
-
-/* size of header of binary files */
-#define LUAC_HEADERSIZE 12
-
-#endif
diff --git a/src/lua/src/lvm.c b/src/lua/src/lvm.c
deleted file mode 100644
index ee3256ab9..000000000
--- a/src/lua/src/lvm.c
+++ /dev/null
@@ -1,763 +0,0 @@
-/*
-** $Id: lvm.c,v 2.63.1.3 2007/12/28 15:32:23 roberto Exp $
-** Lua virtual machine
-** See Copyright Notice in lua.h
-*/
-
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define lvm_c
-#define LUA_CORE
-
-#include "lua.h"
-
-#include "ldebug.h"
-#include "ldo.h"
-#include "lfunc.h"
-#include "lgc.h"
-#include "lobject.h"
-#include "lopcodes.h"
-#include "lstate.h"
-#include "lstring.h"
-#include "ltable.h"
-#include "ltm.h"
-#include "lvm.h"
-
-
-
-/* limit for table tag-method chains (to avoid loops) */
-#define MAXTAGLOOP 100
-
-
-const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
- lua_Number num;
- if (ttisnumber(obj)) return obj;
- if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
- setnvalue(n, num);
- return n;
- }
- else
- return NULL;
-}
-
-
-int luaV_tostring (lua_State *L, StkId obj) {
- if (!ttisnumber(obj))
- return 0;
- else {
- char s[LUAI_MAXNUMBER2STR];
- lua_Number n = nvalue(obj);
- lua_number2str(s, n);
- setsvalue2s(L, obj, luaS_new(L, s));
- return 1;
- }
-}
-
-
-static void traceexec (lua_State *L, const Instruction *pc) {
- lu_byte mask = L->hookmask;
- const Instruction *oldpc = L->savedpc;
- L->savedpc = pc;
- if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) {
- resethookcount(L);
- luaD_callhook(L, LUA_HOOKCOUNT, -1);
- }
- if (mask & LUA_MASKLINE) {
- Proto *p = ci_func(L->ci)->l.p;
- int npc = pcRel(pc, p);
- int newline = getline(p, npc);
- /* call linehook when enter a new function, when jump back (loop),
- or when enter a new line */
- if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p)))
- luaD_callhook(L, LUA_HOOKLINE, newline);
- }
-}
-
-
-static void callTMres (lua_State *L, StkId res, const TValue *f,
- const TValue *p1, const TValue *p2) {
- ptrdiff_t result = savestack(L, res);
- setobj2s(L, L->top, f); /* push function */
- setobj2s(L, L->top+1, p1); /* 1st argument */
- setobj2s(L, L->top+2, p2); /* 2nd argument */
- luaD_checkstack(L, 3);
- L->top += 3;
- luaD_call(L, L->top - 3, 1);
- res = restorestack(L, result);
- L->top--;
- setobjs2s(L, res, L->top);
-}
-
-
-
-static void callTM (lua_State *L, const TValue *f, const TValue *p1,
- const TValue *p2, const TValue *p3) {
- setobj2s(L, L->top, f); /* push function */
- setobj2s(L, L->top+1, p1); /* 1st argument */
- setobj2s(L, L->top+2, p2); /* 2nd argument */
- setobj2s(L, L->top+3, p3); /* 3th argument */
- luaD_checkstack(L, 4);
- L->top += 4;
- luaD_call(L, L->top - 4, 0);
-}
-
-
-void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
- int loop;
- for (loop = 0; loop < MAXTAGLOOP; loop++) {
- const TValue *tm;
- if (ttistable(t)) { /* `t' is a table? */
- Table *h = hvalue(t);
- const TValue *res = luaH_get(h, key); /* do a primitive get */
- if (!ttisnil(res) || /* result is no nil? */
- (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
- setobj2s(L, val, res);
- return;
- }
- /* else will try the tag method */
- }
- else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
- luaG_typeerror(L, t, "index");
- if (ttisfunction(tm)) {
- callTMres(L, val, tm, t, key);
- return;
- }
- t = tm; /* else repeat with `tm' */
- }
- luaG_runerror(L, "loop in gettable");
-}
-
-
-void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
- int loop;
- for (loop = 0; loop < MAXTAGLOOP; loop++) {
- const TValue *tm;
- if (ttistable(t)) { /* `t' is a table? */
- Table *h = hvalue(t);
- TValue *oldval = luaH_set(L, h, key); /* do a primitive set */
- if (!ttisnil(oldval) || /* result is no nil? */
- (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
- setobj2t(L, oldval, val);
- luaC_barriert(L, h, val);
- return;
- }
- /* else will try the tag method */
- }
- else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
- luaG_typeerror(L, t, "index");
- if (ttisfunction(tm)) {
- callTM(L, tm, t, key, val);
- return;
- }
- t = tm; /* else repeat with `tm' */
- }
- luaG_runerror(L, "loop in settable");
-}
-
-
-static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
- StkId res, TMS event) {
- const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
- if (ttisnil(tm))
- tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
- if (ttisnil(tm)) return 0;
- callTMres(L, res, tm, p1, p2);
- return 1;
-}
-
-
-static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2,
- TMS event) {
- const TValue *tm1 = fasttm(L, mt1, event);
- const TValue *tm2;
- if (tm1 == NULL) return NULL; /* no metamethod */
- if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
- tm2 = fasttm(L, mt2, event);
- if (tm2 == NULL) return NULL; /* no metamethod */
- if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */
- return tm1;
- return NULL;
-}
-
-
-static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
- TMS event) {
- const TValue *tm1 = luaT_gettmbyobj(L, p1, event);
- const TValue *tm2;
- if (ttisnil(tm1)) return -1; /* no metamethod? */
- tm2 = luaT_gettmbyobj(L, p2, event);
- if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */
- return -1;
- callTMres(L, L->top, tm1, p1, p2);
- return !l_isfalse(L->top);
-}
-
-
-static int l_strcmp (const TString *ls, const TString *rs) {
- const char *l = getstr(ls);
- size_t ll = ls->tsv.len;
- const char *r = getstr(rs);
- size_t lr = rs->tsv.len;
- for (;;) {
- int temp = strcoll(l, r);
- if (temp != 0) return temp;
- else { /* strings are equal up to a `\0' */
- size_t len = strlen(l); /* index of first `\0' in both strings */
- if (len == lr) /* r is finished? */
- return (len == ll) ? 0 : 1;
- else if (len == ll) /* l is finished? */
- return -1; /* l is smaller than r (because r is not finished) */
- /* both strings longer than `len'; go on comparing (after the `\0') */
- len++;
- l += len; ll -= len; r += len; lr -= len;
- }
- }
-}
-
-
-int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
- int res;
- if (ttype(l) != ttype(r))
- return luaG_ordererror(L, l, r);
- else if (ttisnumber(l))
- return luai_numlt(nvalue(l), nvalue(r));
- else if (ttisstring(l))
- return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
- else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
- return res;
- return luaG_ordererror(L, l, r);
-}
-
-
-static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
- int res;
- if (ttype(l) != ttype(r))
- return luaG_ordererror(L, l, r);
- else if (ttisnumber(l))
- return luai_numle(nvalue(l), nvalue(r));
- else if (ttisstring(l))
- return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
- else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
- return res;
- else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */
- return !res;
- return luaG_ordererror(L, l, r);
-}
-
-
-int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
- const TValue *tm;
- lua_assert(ttype(t1) == ttype(t2));
- switch (ttype(t1)) {
- case LUA_TNIL: return 1;
- case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
- case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
- case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
- case LUA_TUSERDATA: {
- if (uvalue(t1) == uvalue(t2)) return 1;
- tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable,
- TM_EQ);
- break; /* will try TM */
- }
- case LUA_TTABLE: {
- if (hvalue(t1) == hvalue(t2)) return 1;
- tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
- break; /* will try TM */
- }
- default: return gcvalue(t1) == gcvalue(t2);
- }
- if (tm == NULL) return 0; /* no TM? */
- callTMres(L, L->top, tm, t1, t2); /* call TM */
- return !l_isfalse(L->top);
-}
-
-
-void luaV_concat (lua_State *L, int total, int last) {
- do {
- StkId top = L->base + last + 1;
- int n = 2; /* number of elements handled in this pass (at least 2) */
- if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
- if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
- luaG_concaterror(L, top-2, top-1);
- } else if (tsvalue(top-1)->len == 0) /* second op is empty? */
- (void)tostring(L, top - 2); /* result is first op (as string) */
- else {
- /* at least two string values; get as many as possible */
- size_t tl = tsvalue(top-1)->len;
- char *buffer;
- int i;
- /* collect total length */
- for (n = 1; n < total && tostring(L, top-n-1); n++) {
- size_t l = tsvalue(top-n-1)->len;
- if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
- tl += l;
- }
- buffer = luaZ_openspace(L, &G(L)->buff, tl);
- tl = 0;
- for (i=n; i>0; i--) { /* concat all strings */
- size_t l = tsvalue(top-i)->len;
- memcpy(buffer+tl, svalue(top-i), l);
- tl += l;
- }
- setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
- }
- total -= n-1; /* got `n' strings to create 1 new */
- last -= n-1;
- } while (total > 1); /* repeat until only 1 result left */
-}
-
-
-static void Arith (lua_State *L, StkId ra, const TValue *rb,
- const TValue *rc, TMS op) {
- TValue tempb, tempc;
- const TValue *b, *c;
- if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
- (c = luaV_tonumber(rc, &tempc)) != NULL) {
- lua_Number nb = nvalue(b), nc = nvalue(c);
- switch (op) {
- case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;
- case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;
- case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;
- case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;
- case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;
- case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;
- case TM_UNM: setnvalue(ra, luai_numunm(nb)); break;
- default: lua_assert(0); break;
- }
- }
- else if (!call_binTM(L, rb, rc, ra, op))
- luaG_aritherror(L, rb, rc);
-}
-
-
-
-/*
-** some macros for common tasks in `luaV_execute'
-*/
-
-#define runtime_check(L, c) { if (!(c)) break; }
-
-#define RA(i) (base+GETARG_A(i))
-/* to be used after possible stack reallocation */
-#define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
-#define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
-#define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
- ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
-#define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
- ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
-#define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
-
-
-#define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);}
-
-
-#define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
-
-
-#define arith_op(op,tm) { \
- TValue *rb = RKB(i); \
- TValue *rc = RKC(i); \
- if (ttisnumber(rb) && ttisnumber(rc)) { \
- lua_Number nb = nvalue(rb), nc = nvalue(rc); \
- setnvalue(ra, op(nb, nc)); \
- } \
- else \
- Protect(Arith(L, ra, rb, rc, tm)); \
- }
-
-
-
-void luaV_execute (lua_State *L, int nexeccalls) {
- LClosure *cl;
- StkId base;
- TValue *k;
- const Instruction *pc;
- reentry: /* entry point */
- lua_assert(isLua(L->ci));
- pc = L->savedpc;
- cl = &clvalue(L->ci->func)->l;
- base = L->base;
- k = cl->p->k;
- /* main loop of interpreter */
- for (;;) {
- const Instruction i = *pc++;
- StkId ra;
- if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
- (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
- traceexec(L, pc);
- if (L->status == LUA_YIELD) { /* did hook yield? */
- L->savedpc = pc - 1;
- return;
- }
- base = L->base;
- }
- /* warning!! several calls may realloc the stack and invalidate `ra' */
- ra = RA(i);
- lua_assert(base == L->base && L->base == L->ci->base);
- lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
- lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
- switch (GET_OPCODE(i)) {
- case OP_MOVE: {
- setobjs2s(L, ra, RB(i));
- continue;
- }
- case OP_LOADK: {
- setobj2s(L, ra, KBx(i));
- continue;
- }
- case OP_LOADBOOL: {
- setbvalue(ra, GETARG_B(i));
- if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
- continue;
- }
- case OP_LOADNIL: {
- TValue *rb = RB(i);
- do {
- setnilvalue(rb--);
- } while (rb >= ra);
- continue;
- }
- case OP_GETUPVAL: {
- int b = GETARG_B(i);
- setobj2s(L, ra, cl->upvals[b]->v);
- continue;
- }
- case OP_GETGLOBAL: {
- TValue g;
- TValue *rb = KBx(i);
- sethvalue(L, &g, cl->env);
- lua_assert(ttisstring(rb));
- Protect(luaV_gettable(L, &g, rb, ra));
- continue;
- }
- case OP_GETTABLE: {
- Protect(luaV_gettable(L, RB(i), RKC(i), ra));
- continue;
- }
- case OP_SETGLOBAL: {
- TValue g;
- sethvalue(L, &g, cl->env);
- lua_assert(ttisstring(KBx(i)));
- Protect(luaV_settable(L, &g, KBx(i), ra));
- continue;
- }
- case OP_SETUPVAL: {
- UpVal *uv = cl->upvals[GETARG_B(i)];
- setobj(L, uv->v, ra);
- luaC_barrier(L, uv, ra);
- continue;
- }
- case OP_SETTABLE: {
- Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
- continue;
- }
- case OP_NEWTABLE: {
- int b = GETARG_B(i);
- int c = GETARG_C(i);
- sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
- Protect(luaC_checkGC(L));
- continue;
- }
- case OP_SELF: {
- StkId rb = RB(i);
- setobjs2s(L, ra+1, rb);
- Protect(luaV_gettable(L, rb, RKC(i), ra));
- continue;
- }
- case OP_ADD: {
- arith_op(luai_numadd, TM_ADD);
- continue;
- }
- case OP_SUB: {
- arith_op(luai_numsub, TM_SUB);
- continue;
- }
- case OP_MUL: {
- arith_op(luai_nummul, TM_MUL);
- continue;
- }
- case OP_DIV: {
- arith_op(luai_numdiv, TM_DIV);
- continue;
- }
- case OP_MOD: {
- arith_op(luai_nummod, TM_MOD);
- continue;
- }
- case OP_POW: {
- arith_op(luai_numpow, TM_POW);
- continue;
- }
- case OP_UNM: {
- TValue *rb = RB(i);
- if (ttisnumber(rb)) {
- lua_Number nb = nvalue(rb);
- setnvalue(ra, luai_numunm(nb));
- }
- else {
- Protect(Arith(L, ra, rb, rb, TM_UNM));
- }
- continue;
- }
- case OP_NOT: {
- int res = l_isfalse(RB(i)); /* next assignment may change this value */
- setbvalue(ra, res);
- continue;
- }
- case OP_LEN: {
- const TValue *rb = RB(i);
- switch (ttype(rb)) {
- case LUA_TTABLE: {
- setnvalue(ra, cast_num(luaH_getn(hvalue(rb))));
- break;
- }
- case LUA_TSTRING: {
- setnvalue(ra, cast_num(tsvalue(rb)->len));
- break;
- }
- default: { /* try metamethod */
- Protect(
- if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN))
- luaG_typeerror(L, rb, "get length of");
- )
- }
- }
- continue;
- }
- case OP_CONCAT: {
- int b = GETARG_B(i);
- int c = GETARG_C(i);
- Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L));
- setobjs2s(L, RA(i), base+b);
- continue;
- }
- case OP_JMP: {
- dojump(L, pc, GETARG_sBx(i));
- continue;
- }
- case OP_EQ: {
- TValue *rb = RKB(i);
- TValue *rc = RKC(i);
- Protect(
- if (equalobj(L, rb, rc) == GETARG_A(i))
- dojump(L, pc, GETARG_sBx(*pc));
- )
- pc++;
- continue;
- }
- case OP_LT: {
- Protect(
- if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
- dojump(L, pc, GETARG_sBx(*pc));
- )
- pc++;
- continue;
- }
- case OP_LE: {
- Protect(
- if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
- dojump(L, pc, GETARG_sBx(*pc));
- )
- pc++;
- continue;
- }
- case OP_TEST: {
- if (l_isfalse(ra) != GETARG_C(i))
- dojump(L, pc, GETARG_sBx(*pc));
- pc++;
- continue;
- }
- case OP_TESTSET: {
- TValue *rb = RB(i);
- if (l_isfalse(rb) != GETARG_C(i)) {
- setobjs2s(L, ra, rb);
- dojump(L, pc, GETARG_sBx(*pc));
- }
- pc++;
- continue;
- }
- case OP_CALL: {
- int b = GETARG_B(i);
- int nresults = GETARG_C(i) - 1;
- if (b != 0) L->top = ra+b; /* else previous instruction set top */
- L->savedpc = pc;
- switch (luaD_precall(L, ra, nresults)) {
- case PCRLUA: {
- nexeccalls++;
- goto reentry; /* restart luaV_execute over new Lua function */
- }
- case PCRC: {
- /* it was a C function (`precall' called it); adjust results */
- if (nresults >= 0) L->top = L->ci->top;
- base = L->base;
- continue;
- }
- default: {
- return; /* yield */
- }
- }
- }
- case OP_TAILCALL: {
- int b = GETARG_B(i);
- if (b != 0) L->top = ra+b; /* else previous instruction set top */
- L->savedpc = pc;
- lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
- switch (luaD_precall(L, ra, LUA_MULTRET)) {
- case PCRLUA: {
- /* tail call: put new frame in place of previous one */
- CallInfo *ci = L->ci - 1; /* previous frame */
- int aux;
- StkId func = ci->func;
- StkId pfunc = (ci+1)->func; /* previous function index */
- if (L->openupval) luaF_close(L, ci->base);
- L->base = ci->base = ci->func + ((ci+1)->base - pfunc);
- for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */
- setobjs2s(L, func+aux, pfunc+aux);
- ci->top = L->top = func+aux; /* correct top */
- lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
- ci->savedpc = L->savedpc;
- ci->tailcalls++; /* one more call lost */
- L->ci--; /* remove new frame */
- goto reentry;
- }
- case PCRC: { /* it was a C function (`precall' called it) */
- base = L->base;
- continue;
- }
- default: {
- return; /* yield */
- }
- }
- }
- case OP_RETURN: {
- int b = GETARG_B(i);
- if (b != 0) L->top = ra+b-1;
- if (L->openupval) luaF_close(L, base);
- L->savedpc = pc;
- b = luaD_poscall(L, ra);
- if (--nexeccalls == 0) /* was previous function running `here'? */
- return; /* no: return */
- else { /* yes: continue its execution */
- if (b) L->top = L->ci->top;
- lua_assert(isLua(L->ci));
- lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL);
- goto reentry;
- }
- }
- case OP_FORLOOP: {
- lua_Number step = nvalue(ra+2);
- lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
- lua_Number limit = nvalue(ra+1);
- if (luai_numlt(0, step) ? luai_numle(idx, limit)
- : luai_numle(limit, idx)) {
- dojump(L, pc, GETARG_sBx(i)); /* jump back */
- setnvalue(ra, idx); /* update internal index... */
- setnvalue(ra+3, idx); /* ...and external index */
- }
- continue;
- }
- case OP_FORPREP: {
- const TValue *init = ra;
- const TValue *plimit = ra+1;
- const TValue *pstep = ra+2;
- L->savedpc = pc; /* next steps may throw errors */
- if (!tonumber(init, ra))
- luaG_runerror(L, LUA_QL("for") " initial value must be a number");
- else if (!tonumber(plimit, ra+1))
- luaG_runerror(L, LUA_QL("for") " limit must be a number");
- else if (!tonumber(pstep, ra+2))
- luaG_runerror(L, LUA_QL("for") " step must be a number");
- setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
- dojump(L, pc, GETARG_sBx(i));
- continue;
- }
- case OP_TFORLOOP: {
- StkId cb = ra + 3; /* call base */
- setobjs2s(L, cb+2, ra+2);
- setobjs2s(L, cb+1, ra+1);
- setobjs2s(L, cb, ra);
- L->top = cb+3; /* func. + 2 args (state and index) */
- Protect(luaD_call(L, cb, GETARG_C(i)));
- L->top = L->ci->top;
- cb = RA(i) + 3; /* previous call may change the stack */
- if (!ttisnil(cb)) { /* continue loop? */
- setobjs2s(L, cb-1, cb); /* save control variable */
- dojump(L, pc, GETARG_sBx(*pc)); /* jump back */
- }
- pc++;
- continue;
- }
- case OP_SETLIST: {
- int n = GETARG_B(i);
- int c = GETARG_C(i);
- int last;
- Table *h;
- if (n == 0) {
- n = cast_int(L->top - ra) - 1;
- L->top = L->ci->top;
- }
- if (c == 0) c = cast_int(*pc++);
- runtime_check(L, ttistable(ra));
- h = hvalue(ra);
- last = ((c-1)*LFIELDS_PER_FLUSH) + n;
- if (last > h->sizearray) /* needs more space? */
- luaH_resizearray(L, h, last); /* pre-alloc it at once */
- for (; n > 0; n--) {
- TValue *val = ra+n;
- setobj2t(L, luaH_setnum(L, h, last--), val);
- luaC_barriert(L, h, val);
- }
- continue;
- }
- case OP_CLOSE: {
- luaF_close(L, ra);
- continue;
- }
- case OP_CLOSURE: {
- Proto *p;
- Closure *ncl;
- int nup, j;
- p = cl->p->p[GETARG_Bx(i)];
- nup = p->nups;
- ncl = luaF_newLclosure(L, nup, cl->env);
- ncl->l.p = p;
- for (j=0; j<nup; j++, pc++) {
- if (GET_OPCODE(*pc) == OP_GETUPVAL)
- ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
- else {
- lua_assert(GET_OPCODE(*pc) == OP_MOVE);
- ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
- }
- }
- setclvalue(L, ra, ncl);
- Protect(luaC_checkGC(L));
- continue;
- }
- case OP_VARARG: {
- int b = GETARG_B(i) - 1;
- int j;
- CallInfo *ci = L->ci;
- int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1;
- if (b == LUA_MULTRET) {
- Protect(luaD_checkstack(L, n));
- ra = RA(i); /* previous call may change the stack */
- b = n;
- L->top = ra + n;
- }
- for (j = 0; j < b; j++) {
- if (j < n) {
- setobjs2s(L, ra + j, ci->base - n + j);
- }
- else {
- setnilvalue(ra + j);
- }
- }
- continue;
- }
- }
- }
-}
-
diff --git a/src/lua/src/lvm.h b/src/lua/src/lvm.h
deleted file mode 100644
index bfe4f5678..000000000
--- a/src/lua/src/lvm.h
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
-** $Id: lvm.h,v 2.5.1.1 2007/12/27 13:02:25 roberto Exp $
-** Lua virtual machine
-** See Copyright Notice in lua.h
-*/
-
-#ifndef lvm_h
-#define lvm_h
-
-
-#include "ldo.h"
-#include "lobject.h"
-#include "ltm.h"
-
-
-#define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o)))
-
-#define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \
- (((o) = luaV_tonumber(o,n)) != NULL))
-
-#define equalobj(L,o1,o2) \
- (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2))
-
-
-LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
-LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2);
-LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n);
-LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj);
-LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key,
- StkId val);
-LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key,
- StkId val);
-LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls);
-LUAI_FUNC void luaV_concat (lua_State *L, int total, int last);
-
-#endif
diff --git a/src/lua/src/lzio.c b/src/lua/src/lzio.c
deleted file mode 100644
index 293edd59b..000000000
--- a/src/lua/src/lzio.c
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
-** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $
-** a generic input stream interface
-** See Copyright Notice in lua.h
-*/
-
-
-#include <string.h>
-
-#define lzio_c
-#define LUA_CORE
-
-#include "lua.h"
-
-#include "llimits.h"
-#include "lmem.h"
-#include "lstate.h"
-#include "lzio.h"
-
-
-int luaZ_fill (ZIO *z) {
- size_t size;
- lua_State *L = z->L;
- const char *buff;
- lua_unlock(L);
- buff = z->reader(L, z->data, &size);
- lua_lock(L);
- if (buff == NULL || size == 0) return EOZ;
- z->n = size - 1;
- z->p = buff;
- return char2int(*(z->p++));
-}
-
-
-int luaZ_lookahead (ZIO *z) {
- if (z->n == 0) {
- if (luaZ_fill(z) == EOZ)
- return EOZ;
- else {
- z->n++; /* luaZ_fill removed first byte; put back it */
- z->p--;
- }
- }
- return char2int(*z->p);
-}
-
-
-void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
- z->L = L;
- z->reader = reader;
- z->data = data;
- z->n = 0;
- z->p = NULL;
-}
-
-
-/* --------------------------------------------------------------- read --- */
-size_t luaZ_read (ZIO *z, void *b, size_t n) {
- while (n) {
- size_t m;
- if (luaZ_lookahead(z) == EOZ)
- return n; /* return number of missing bytes */
- m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
- memcpy(b, z->p, m);
- z->n -= m;
- z->p += m;
- b = (char *)b + m;
- n -= m;
- }
- return 0;
-}
-
-/* ------------------------------------------------------------------------ */
-char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
- if (n > buff->buffsize) {
- if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
- luaZ_resizebuffer(L, buff, n);
- }
- return buff->buffer;
-}
-
-
diff --git a/src/lua/src/lzio.h b/src/lua/src/lzio.h
deleted file mode 100644
index 51d695d8c..000000000
--- a/src/lua/src/lzio.h
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
-** $Id: lzio.h,v 1.21.1.1 2007/12/27 13:02:25 roberto Exp $
-** Buffered streams
-** See Copyright Notice in lua.h
-*/
-
-
-#ifndef lzio_h
-#define lzio_h
-
-#include "lua.h"
-
-#include "lmem.h"
-
-
-#define EOZ (-1) /* end of stream */
-
-typedef struct Zio ZIO;
-
-#define char2int(c) cast(int, cast(unsigned char, (c)))
-
-#define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z))
-
-typedef struct Mbuffer {
- char *buffer;
- size_t n;
- size_t buffsize;
-} Mbuffer;
-
-#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0)
-
-#define luaZ_buffer(buff) ((buff)->buffer)
-#define luaZ_sizebuffer(buff) ((buff)->buffsize)
-#define luaZ_bufflen(buff) ((buff)->n)
-
-#define luaZ_resetbuffer(buff) ((buff)->n = 0)
-
-
-#define luaZ_resizebuffer(L, buff, size) \
- (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \
- (buff)->buffsize = size)
-
-#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)
-
-
-LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n);
-LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
- void *data);
-LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */
-LUAI_FUNC int luaZ_lookahead (ZIO *z);
-
-
-
-/* --------- Private Part ------------------ */
-
-struct Zio {
- size_t n; /* bytes still unread */
- const char *p; /* current position in buffer */
- lua_Reader reader;
- void* data; /* additional data */
- lua_State *L; /* Lua state (for reader) */
-};
-
-
-LUAI_FUNC int luaZ_fill (ZIO *z);
-
-#endif
diff --git a/src/lua/src/print.c b/src/lua/src/print.c
deleted file mode 100644
index e240cfc3c..000000000
--- a/src/lua/src/print.c
+++ /dev/null
@@ -1,227 +0,0 @@
-/*
-** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $
-** print bytecodes
-** See Copyright Notice in lua.h
-*/
-
-#include <ctype.h>
-#include <stdio.h>
-
-#define luac_c
-#define LUA_CORE
-
-#include "ldebug.h"
-#include "lobject.h"
-#include "lopcodes.h"
-#include "lundump.h"
-
-#define PrintFunction luaU_print
-
-#define Sizeof(x) ((int)sizeof(x))
-#define VOID(p) ((const void*)(p))
-
-static void PrintString(const TString* ts)
-{
- const char* s=getstr(ts);
- size_t i,n=ts->tsv.len;
- putchar('"');
- for (i=0; i<n; i++)
- {
- int c=s[i];
- switch (c)
- {
- case '"': printf("\\\""); break;
- case '\\': printf("\\\\"); break;
- case '\a': printf("\\a"); break;
- case '\b': printf("\\b"); break;
- case '\f': printf("\\f"); break;
- case '\n': printf("\\n"); break;
- case '\r': printf("\\r"); break;
- case '\t': printf("\\t"); break;
- case '\v': printf("\\v"); break;
- default: if (isprint((unsigned char)c))
- putchar(c);
- else
- printf("\\%03u",(unsigned char)c);
- }
- }
- putchar('"');
-}
-
-static void PrintConstant(const Proto* f, int i)
-{
- const TValue* o=&f->k[i];
- switch (ttype(o))
- {
- case LUA_TNIL:
- printf("nil");
- break;
- case LUA_TBOOLEAN:
- printf(bvalue(o) ? "true" : "false");
- break;
- case LUA_TNUMBER:
- printf(LUA_NUMBER_FMT,nvalue(o));
- break;
- case LUA_TSTRING:
- PrintString(rawtsvalue(o));
- break;
- default: /* cannot happen */
- printf("? type=%d",ttype(o));
- break;
- }
-}
-
-static void PrintCode(const Proto* f)
-{
- const Instruction* code=f->code;
- int pc,n=f->sizecode;
- for (pc=0; pc<n; pc++)
- {
- Instruction i=code[pc];
- OpCode o=GET_OPCODE(i);
- int a=GETARG_A(i);
- int b=GETARG_B(i);
- int c=GETARG_C(i);
- int bx=GETARG_Bx(i);
- int sbx=GETARG_sBx(i);
- int line=getline(f,pc);
- printf("\t%d\t",pc+1);
- if (line>0) printf("[%d]\t",line); else printf("[-]\t");
- printf("%-9s\t",luaP_opnames[o]);
- switch (getOpMode(o))
- {
- case iABC:
- printf("%d",a);
- if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b);
- if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c);
- break;
- case iABx:
- if (getBMode(o)==OpArgK) printf("%d %d",a,-1-bx); else printf("%d %d",a,bx);
- break;
- case iAsBx:
- if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx);
- break;
- }
- switch (o)
- {
- case OP_LOADK:
- printf("\t; "); PrintConstant(f,bx);
- break;
- case OP_GETUPVAL:
- case OP_SETUPVAL:
- printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-");
- break;
- case OP_GETGLOBAL:
- case OP_SETGLOBAL:
- printf("\t; %s",svalue(&f->k[bx]));
- break;
- case OP_GETTABLE:
- case OP_SELF:
- if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); }
- break;
- case OP_SETTABLE:
- case OP_ADD:
- case OP_SUB:
- case OP_MUL:
- case OP_DIV:
- case OP_POW:
- case OP_EQ:
- case OP_LT:
- case OP_LE:
- if (ISK(b) || ISK(c))
- {
- printf("\t; ");
- if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-");
- printf(" ");
- if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-");
- }
- break;
- case OP_JMP:
- case OP_FORLOOP:
- case OP_FORPREP:
- printf("\t; to %d",sbx+pc+2);
- break;
- case OP_CLOSURE:
- printf("\t; %p",VOID(f->p[bx]));
- break;
- case OP_SETLIST:
- if (c==0) printf("\t; %d",(int)code[++pc]);
- else printf("\t; %d",c);
- break;
- default:
- break;
- }
- printf("\n");
- }
-}
-
-#define SS(x) (x==1)?"":"s"
-#define S(x) x,SS(x)
-
-static void PrintHeader(const Proto* f)
-{
- const char* s=getstr(f->source);
- if (*s=='@' || *s=='=')
- s++;
- else if (*s==LUA_SIGNATURE[0])
- s="(bstring)";
- else
- s="(string)";
- printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n",
- (f->linedefined==0)?"main":"function",s,
- f->linedefined,f->lastlinedefined,
- S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f));
- printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
- f->numparams,f->is_vararg?"+":"",SS(f->numparams),
- S(f->maxstacksize),S(f->nups));
- printf("%d local%s, %d constant%s, %d function%s\n",
- S(f->sizelocvars),S(f->sizek),S(f->sizep));
-}
-
-static void PrintConstants(const Proto* f)
-{
- int i,n=f->sizek;
- printf("constants (%d) for %p:\n",n,VOID(f));
- for (i=0; i<n; i++)
- {
- printf("\t%d\t",i+1);
- PrintConstant(f,i);
- printf("\n");
- }
-}
-
-static void PrintLocals(const Proto* f)
-{
- int i,n=f->sizelocvars;
- printf("locals (%d) for %p:\n",n,VOID(f));
- for (i=0; i<n; i++)
- {
- printf("\t%d\t%s\t%d\t%d\n",
- i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);
- }
-}
-
-static void PrintUpvalues(const Proto* f)
-{
- int i,n=f->sizeupvalues;
- printf("upvalues (%d) for %p:\n",n,VOID(f));
- if (f->upvalues==NULL) return;
- for (i=0; i<n; i++)
- {
- printf("\t%d\t%s\n",i,getstr(f->upvalues[i]));
- }
-}
-
-void PrintFunction(const Proto* f, int full)
-{
- int i,n=f->sizep;
- PrintHeader(f);
- PrintCode(f);
- if (full)
- {
- PrintConstants(f);
- PrintLocals(f);
- PrintUpvalues(f);
- }
- for (i=0; i<n; i++) PrintFunction(f->p[i],full);
-}
diff --git a/src/main.cpp b/src/main.cpp
index 2c059840e..2cde3b302 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,6 +1,6 @@
/*
Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -21,30 +21,16 @@ with this program; if not, write to the Free Software Foundation, Inc.,
=============================== NOTES ==============================
NOTE: Things starting with TODO are sometimes only suggestions.
-NOTE: VBO cannot be turned on for fast-changing stuff because there
- is an apparanet memory leak in irrlicht when using it (not sure)
- - It is not a memory leak but some kind of a buffer.
-
NOTE: iostream.imbue(std::locale("C")) is very slow
NOTE: Global locale is now set at initialization
-SUGG: Fix address to be ipv6 compatible
-
-NOTE: When a new sector is generated, it may change the ground level
- of it's and it's neighbors border that two blocks that are
- above and below each other and that are generated before and
- after the sector heightmap generation (order doesn't matter),
- can have a small gap between each other at the border.
-SUGG: Use same technique for sector heightmaps as what we're
- using for UnlimitedHeightmap? (getting all neighbors
- when generating)
+NOTE: If VBO (EHM_STATIC) is used, remember to explicitly free the
+ hardware buffer (it is not freed automatically)
-SUGG: Transfer more blocks in a single packet
-SUGG: A blockdata combiner class, to which blocks are added and at
- destruction it sends all the stuff in as few packets as possible.
+Old, wild and random suggestions that probably won't be done:
+-------------------------------------------------------------
SUGG: If player is on ground, mainly fetch ground-level blocks
-SUGG: Fetch stuff mainly from the viewing direction
SUGG: Expose Connection's seqnums and ACKs to server and client.
- This enables saving many packets and making a faster connection
@@ -58,27 +44,16 @@ SUGG: More fine-grained control of client's dumping of blocks from
SUGG: A map editing mode (similar to dedicated server mode)
-SUGG: Add a time value to the param of footstepped grass and check it
- against a global timer when a block is accessed, to make old
- steps fade away.
-
-SUGG: Make a copy of close-range environment on client for showing
- on screen, with minimal mutexes to slow down the main loop
-
+SUGG: Transfer more blocks in a single packet
+SUGG: A blockdata combiner class, to which blocks are added and at
+ destruction it sends all the stuff in as few packets as possible.
SUGG: Make a PACKET_COMBINED which contains many subpackets. Utilize
it by sending more stuff in a single packet.
- Add a packet queue to RemoteClient, from which packets will be
combined with object data packets
- This is not exactly trivial: the object data packets are
sometimes very big by themselves
-
-SUGG: Split MapBlockObject serialization to to-client and to-disk
- - This will allow saving ages of rats on disk but not sending
- them to clients
-
-SUGG: MovingObject::move and Player::move are basically the same.
- combine them.
- - NOTE: Player::move is more up-to-date.
+ - This might not give much network performance gain though.
SUGG: Precalculate lighting translation table at runtime (at startup)
- This is not doable because it is currently hand-made and not
@@ -88,7 +63,7 @@ SUGG: Precalculate lighting translation table at runtime (at startup)
SUGG: A version number to blocks, which increments when the block is
modified (node add/remove, water update, lighting update)
- This can then be used to make sure the most recent version of
- a block has been sent to client
+ a block has been sent to client, for example
SUGG: Make the amount of blocks sending to client and the total
amount of blocks dynamically limited. Transferring blocks is the
@@ -101,65 +76,104 @@ SUGG: Meshes of blocks could be split into 6 meshes facing into
SUGG: Calculate lighting per vertex to get a lighting effect like in
bartwe's game
+SUGG: Background music based on cellular automata?
+ http://www.earslap.com/projectslab/otomata
+
+SUGG: Simple light color information to air
+
+SUGG: Server-side objects could be moved based on nodes to enable very
+ lightweight operation and simple AI
+ - Not practical; client would still need to show smooth movement.
+
+SUGG: Make a system for pregenerating quick information for mapblocks, so
+ that the client can show them as cubes before they are actually sent
+ or even generated.
+
Gaming ideas:
-------------
- Aim for something like controlling a single dwarf in Dwarf Fortress
-
- The player could go faster by a crafting a boat, or riding an animal
-
- Random NPC traders. what else?
+Game content:
+-------------
+
+- When furnace is destroyed, move items to player's inventory
+- Add lots of stuff
+- Glass blocks
+- Growing grass, decaying leaves
+ - This can be done in the active blocks I guess.
+ - Lots of stuff can be done in the active blocks.
+ - Uh, is there an active block list somewhere? I think not. Add it.
+- Breaking weak structures
+ - This can probably be accomplished in the same way as grass
+- Player health points
+ - When player dies, throw items on map (needs better item-on-map
+ implementation)
+- Cobble to get mossy if near water
+- More slots in furnace source list, so that multiple ingredients
+ are possible.
+- Keys to chests?
+
+- The Treasure Guard; a big monster with a hammer
+ - The hammer does great damage, shakes the ground and removes a block
+ - You can drop on top of it, and have some time to attack there
+ before he shakes you off
+
+- Maybe the difficulty could come from monsters getting tougher in
+ far-away places, and the player starting to need something from
+ there when time goes by.
+ - The player would have some of that stuff at the beginning, and
+ would need new supplies of it when it runs out
+
+- A bomb
+- A spread-items-on-map routine for the bomb, and for dying players
+
+- Fighting:
+ - Proper sword swing simulation
+ - Player should get damage from colliding to a wall at high speed
+
Documentation:
--------------
Build system / running:
-----------------------
-FIXME: Some network errors on Windows that cause local game to not work
- - See siggjen's emails.
- - Is this the famous "windows 7 problem"?
- - Apparently there might be other errors too
-
Networking and serialization:
-----------------------------
-TODO: Get rid of GotSplitPacketException
-
-GUI:
-----
-
-TODO: Add gui option to remove map
+SUGG: Fix address to be ipv6 compatible
-TODO: Configuration menu, at least for keys
+User Interface:
+---------------
Graphics:
---------
-TODO: Optimize day/night mesh updating somehow
- - create copies of all textures for all lighting values and only
- change texture for material?
- - Umm... the collecting of the faces is the slow part
- -> what about just changing the color values of the existing
- meshbuffers? It should go quite fast.
- - This is not easy; There'd need to be a buffer somewhere
- that would contain the night and day lighting values.
- - Actually if FastFaces would be stored, they could
- hold both values
-
-FEATURE: Combine MapBlock's face caches to so big pieces that VBO
- gets used
+SUGG: Combine MapBlock's face caches to so big pieces that VBO
+ can be used
- That is >500 vertices
- This is not easy; all the MapBlocks close to the player would
still need to be drawn separately and combining the blocks
would have to happen in a background thread
-TODO: Make fetching sector's blocks more efficient when rendering
+SUGG: Make fetching sector's blocks more efficient when rendering
sectors that have very large amounts of blocks (on client)
- Is this necessary at all?
TODO: Flowing water animation
+SUGG: Draw cubes in inventory directly with 3D drawing commands, so that
+ animating them is easier.
+
+SUGG: Option for enabling proper alpha channel for textures
+TODO: A setting for enabling bilinear filtering for textures
+
+TODO: Better control of draw_control.wanted_max_blocks
+
+TODO: Block mesh generator to tile properly on smooth lighting
+
Configuration:
--------------
@@ -168,58 +182,88 @@ Client:
TODO: Untie client network operations from framerate
- Needs some input queues or something
+ - This won't give much performance boost because calculating block
+ meshes takes so long
SUGG: Make morning and evening transition more smooth and maybe shorter
-SUGG: Don't update all meshes always on single node changes, but
+TODO: Don't update all meshes always on single node changes, but
check which ones should be updated
- - implement Map::updateNodeMeshes()
+ - implement Map::updateNodeMeshes() and the usage of it
+ - It will give almost always a 4x boost in mesh update performance.
-TODO: Remove IrrlichtWrapper
+- A weapon engine
+
+- Tool/weapon visualization
+
+FIXME: When disconnected to the menu, memory is not freed properly
Server:
-------
-TODO: When player dies, throw items on map
-
-TODO: Make an option to the server to disable building and digging near
+SUGG: Make an option to the server to disable building and digging near
the starting position
-TODO: Copy the text of the last picked sign to inventory in creative
- mode
+FIXME: Server sometimes goes into some infinite PeerNotFoundException loop
-TODO: Check what goes wrong with caching map to disk (Kray)
- - Nothing?
-
-TODO: When server sees that client is removing an inexistent block in
- an existent position, resend the MapBlock.
+* Fix the problem with the server constantly saving one or a few
+ blocks? List the first saved block, maybe it explains.
+ - It is probably caused by oscillating water
+* Make a small history check to transformLiquids to detect and log
+ continuous oscillations, in such detail that they can be fixed.
-FIXME: Server went into some infinite PeerNotFoundException loop
+FIXME: The new optimized map sending doesn't sometimes send enough blocks
+ from big caves and such
+
+* Take player's walking direction into account in GetNextBlocks
+
+Environment:
+------------
+
+TODO: A list of "active blocks" in which stuff happens.
+ + Add a never-resetted game timer to the server
+ + Add a timestamp value to blocks
+ + The simple rule: All blocks near some player are "active"
+ - Do stuff in real time in active blocks
+ + Handle objects
+ TODO: Make proper hooks in here
+ - Grow grass, delete leaves without a tree
+ - Spawn some mobs based on some rules
+ - Transform cobble to mossy cobble near water
+ - Run a custom script
+ - ...And all kinds of other dynamic stuff
+ + Keep track of when a block becomes active and becomes inactive
+ + When a block goes inactive:
+ + Store objects statically to block
+ + Store timer value as the timestamp
+ + When a block goes active:
+ + Create active objects out of static objects
+ TODO: Make proper hooks in here
+ - Simulate the results of what would have happened if it would have
+ been active for all the time
+ - Grow a lot of grass and so on
+ + Initially it is fine to send information about every active object
+ to every player. Eventually it should be modified to only send info
+ about the nearest ones.
+ + This was left to be done by the old system and it sends only the
+ nearest ones.
Objects:
--------
-TODO: There has to be some better way to handle static objects than to
- send them all the time. This affects signs and item objects.
-SUGG: Signs could be done in the same way as torches. For this, blocks
- need an additional metadata field for the texts
- - This is also needed for item container chests
-
-Block object server side:
- - A "near blocks" buffer, in which some nearby blocks are stored.
- - For all blocks in the buffer, objects are stepped(). This
- means they are active.
- - TODO: A global active buffer is needed for the server
- - TODO: A timestamp to blocks
- - TODO: All blocks going in and out of the buffer are recorded.
- - TODO: For outgoing blocks, timestamp is written.
- - TODO: For incoming blocks, time difference is calculated and
- objects are stepped according to it.
-
-- When an active object goes far from a player, either delete
- it or store it statically.
-- When a statically stored active object comes near a player,
- recreate the active object
+TODO: Get rid of MapBlockObjects and use only ActiveObjects
+ - Skipping the MapBlockObject data is nasty - there is no "total
+ length" stored; have to make a SkipMBOs function which contains
+ enough of the current code to skip them properly.
+
+SUGG: MovingObject::move and Player::move are basically the same.
+ combine them.
+ - NOTE: Player::move is more up-to-date.
+ - NOTE: There is a simple move implementation now in collision.{h,cpp}
+ - NOTE: MovingObject will be deleted (MapBlockObject)
+
+TODO: Add a long step function to objects that is called with the time
+ difference when block activates
Map:
----
@@ -229,88 +273,63 @@ TODO: Mineral and ground material properties
some formula, as well as tool strengths
TODO: Flowing water to actually contain flow direction information
-
-TODO: Remove duplicate lighting implementation from Map (leave
- VoxelManipulator, which is faster)
-
-FEATURE: Create a system that allows a huge amount of different "map
- generator modules/filters"
-
-FEATURE: Erosion simulation at map generation time
- - Simulate water flows, which would carve out dirt fast and
- then turn stone into gravel and sand and relocate it.
- - How about relocating minerals, too? Coal and gold in
- downstream sand and gravel would be kind of cool
- - This would need a better way of handling minerals, mainly
- to have mineral content as a separate field. the first
- parameter field is free for this.
- - Simulate rock falling from cliffs when water has removed
- enough solid rock from the bottom
-
-Doing now (most important at the top):
---------------------------------------
-# maybe done
-* not done
-
-=== Next
-* Continue making the scripting system:
- * Make updateNodeMesh for a less verbose mesh update on add/removenode
- * Switch to using a safe way for the self and env pointers
- * Make some global environment hooks, like node placed and general
- on_step()
-
-=== Fixmes
-* Check the fixmes in the list above
-* Make server find the spawning place from the real map data, not from
- the heightmap
- - But the changing borders of chunk have to be avoided, because
- there is time to generate only one chunk.
-* Make the generator to run in background and not blocking block
- placement and transfer
-* only_from_disk might not work anymore - check and fix it.
-
-=== Making it more portable
-* Some MSVC: std::sto* are defined without a namespace and collide
- with the ones in utility.h
-
-=== Features
-* Map should make the appropriate MapEditEvents
-* Add a global Lua spawn handler and such
-* Get rid of MapBlockObjects
-* Other players could be sent to clients as LuaCAOs
-* Add mud underground
-* Make an "environment metafile" to store at least time of day
-* Move digging property stuff from material.{h,cpp} to mapnode.cpp...
- - Or maybe move content_features to material.{h,cpp}?
-* Add some kind of erosion and other stuff that now is possible
-* Make client to fetch stuff asynchronously
- - Needs method SyncProcessData
+ - There is a space for this - it just has to be implemented.
+
+SUGG: Erosion simulation at map generation time
+ - Simulate water flows, which would carve out dirt fast and
+ then turn stone into gravel and sand and relocate it.
+ - How about relocating minerals, too? Coal and gold in
+ downstream sand and gravel would be kind of cool
+ - This would need a better way of handling minerals, mainly
+ to have mineral content as a separate field. the first
+ parameter field is free for this.
+ - Simulate rock falling from cliffs when water has removed
+ enough solid rock from the bottom
+
+SUGG: Try out the notch way of generating maps, that is, make bunches
+ of low-res 3d noise and interpolate linearly.
+
+Mapgen v2:
+* Possibly add some kind of erosion and other stuff
* Better water generation (spread it to underwater caverns but don't
fill dungeons that don't touch big water masses)
* When generating a chunk and the neighboring chunk doesn't have mud
and stuff yet and the ground is fairly flat, the mud will flow to
the other chunk making nasty straight walls when the other chunk
- is generated. Fix it.
-* Fix the problem with the server constantly saving one or a few
- blocks? List the first saved block, maybe it explains.
- - It is probably caused by oscillating water
-* Make a small history check to transformLiquids to detect and log
- continuous oscillations, in such detail that they can be fixed.
-* Combine meshes to bigger ones in ClientMap and set them EHM_STATIC
+ is generated. Fix it. Maybe just a special case if the ground is
+ flat?
-======================================================================
+Misc. stuff:
+------------
+* Move digging property stuff from material.{h,cpp} to mapnode.cpp
+ - ...Or maybe move content_features to material.{h,cpp}?
-*/
+Making it more portable:
+------------------------
+
+Stuff to do before release:
+---------------------------
-/*
- Setting this to 1 enables a special camera mode that forces
- the renderers to think that the camera statically points from
- the starting place to a static direction.
+Fixes to the current release:
+-----------------------------
+
+Stuff to do after release:
+---------------------------
+- Make sure server handles removing grass when a block is placed (etc)
+ - The client should not do it by itself
+- Block cube placement around player's head
+- Protocol version field
+- Consider getting some textures from cisoun's texture pack
+ - Ask from Cisoun
+- Make sure the fence implementation and data format is good
+ - Think about using same bits for material for fences and doors, for
+ example
+- Finish the ActiveBlockModifier stuff and use it for something
+- Move mineral to param2, increment map serialization version, add conversion
+
+======================================================================
- This allows one to move around with the player and see what
- is actually drawn behind solid things and behind the player.
*/
-#define FIELD_OF_VIEW_TEST 0
#ifdef NDEBUG
#ifdef _WIN32
@@ -336,69 +355,49 @@ Doing now (most important at the top):
#include <iostream>
#include <fstream>
-#include <jmutexautolock.h>
+//#include <jmutexautolock.h>
#include <locale.h>
#include "main.h"
#include "common_irrlicht.h"
#include "debug.h"
-#include "map.h"
-#include "player.h"
+//#include "map.h"
+//#include "player.h"
#include "test.h"
-//#include "environment.h"
#include "server.h"
-#include "client.h"
-//#include "serialization.h"
+//#include "client.h"
#include "constants.h"
-//#include "strfnd.h"
#include "porting.h"
-#include "irrlichtwrapper.h"
#include "gettime.h"
-#include "porting.h"
-#include "guiPauseMenu.h"
-#include "guiInventoryMenu.h"
-#include "guiTextInputMenu.h"
-#include "materials.h"
#include "guiMessageMenu.h"
#include "filesys.h"
#include "config.h"
#include "guiMainMenu.h"
#include "mineral.h"
-#include "noise.h"
-#include "tile.h"
-
-// TODO: Remove this
-IrrlichtWrapper *g_irrlicht = NULL;
+//#include "noise.h"
+//#include "tile.h"
+#include "materials.h"
+#include "game.h"
+#include "keycode.h"
// This makes textures
ITextureSource *g_texturesource = NULL;
-MapDrawControl draw_control;
-
/*
Settings.
These are loaded from the config file.
*/
Settings g_settings;
-
+// This is located in defaultsettings.cpp
extern void set_default_settings();
+// Global profiler
+Profiler g_profiler;
+
/*
Random stuff
*/
-IrrlichtDevice *g_device = NULL;
-Client *g_client = NULL;
-
-/*const s16 quickinv_size = 40;
-const s16 quickinv_padding = 8;
-const s16 quickinv_spacing = quickinv_size + quickinv_padding;
-const s16 quickinv_outer_padding = 4;
-const s16 quickinv_itemcount = 8;*/
-
-const s32 hotbar_itemcount = 8;
-const s32 hotbar_imagesize = 36;
-
/*
GUI Stuff
*/
@@ -406,58 +405,6 @@ const s32 hotbar_imagesize = 36;
gui::IGUIEnvironment* guienv = NULL;
gui::IGUIStaticText *guiroot = NULL;
-class MainMenuManager : public IMenuManager
-{
-public:
- virtual void createdMenu(GUIModalMenu *menu)
- {
- for(core::list<GUIModalMenu*>::Iterator
- i = m_stack.begin();
- i != m_stack.end(); i++)
- {
- assert(*i != menu);
- }
-
- if(m_stack.size() != 0)
- (*m_stack.getLast())->setVisible(false);
- m_stack.push_back(menu);
- }
-
- virtual void deletingMenu(GUIModalMenu *menu)
- {
- // Remove all entries if there are duplicates
- bool removed_entry;
- do{
- removed_entry = false;
- for(core::list<GUIModalMenu*>::Iterator
- i = m_stack.begin();
- i != m_stack.end(); i++)
- {
- if(*i == menu)
- {
- m_stack.erase(i);
- removed_entry = true;
- break;
- }
- }
- }while(removed_entry);
-
- /*core::list<GUIModalMenu*>::Iterator i = m_stack.getLast();
- assert(*i == menu);
- m_stack.erase(i);*/
-
- if(m_stack.size() != 0)
- (*m_stack.getLast())->setVisible(true);
- }
-
- u32 menuCount()
- {
- return m_stack.size();
- }
-
- core::list<GUIModalMenu*> m_stack;
-};
-
MainMenuManager g_menumgr;
bool noMenuActive()
@@ -465,30 +412,9 @@ bool noMenuActive()
return (g_menumgr.menuCount() == 0);
}
-bool g_disconnect_requested = false;
+// Passed to menus to allow disconnecting and exiting
-class MainGameCallback : public IGameCallback
-{
-public:
- virtual void exitToOS()
- {
- g_device->closeDevice();
- }
-
- virtual void disconnect()
- {
- g_disconnect_requested = true;
- }
-};
-
-MainGameCallback g_gamecallback;
-
-// Inventory actions from the menu are buffered here before sending
-Queue<InventoryAction*> inventory_action_queue;
-// This is a copy of the inventory that the client's environment has
-Inventory local_inventory;
-
-u16 g_selected_item = 0;
+MainGameCallback *g_gamecallback = NULL;
/*
Debug streams
@@ -514,74 +440,56 @@ std::ostream *derr_client_ptr = &dstream;
gettime.h implementation
*/
-u32 getTimeMs()
+// A small helper class
+class TimeGetter
{
- /*
- Use irrlicht because it is more precise than porting.h's
- getTimeMs()
- */
- if(g_irrlicht == NULL)
- return 0;
- return g_irrlicht->getTime();
-}
-
-/*
- Text input system
-*/
+public:
+ virtual u32 getTime() = 0;
+};
-struct TextDestSign : public TextDest
+// A precise irrlicht one
+class IrrlichtTimeGetter: public TimeGetter
{
- TextDestSign(v3s16 blockpos, s16 id, Client *client)
- {
- m_blockpos = blockpos;
- m_id = id;
- m_client = client;
- }
- void gotText(std::wstring text)
+public:
+ IrrlichtTimeGetter(IrrlichtDevice *device):
+ m_device(device)
+ {}
+ u32 getTime()
{
- std::string ntext = wide_to_narrow(text);
- dstream<<"Changing text of a sign object: "
- <<ntext<<std::endl;
- m_client->sendSignText(m_blockpos, m_id, ntext);
+ if(m_device == NULL)
+ return 0;
+ return m_device->getTimer()->getRealTime();
}
-
- v3s16 m_blockpos;
- s16 m_id;
- Client *m_client;
+private:
+ IrrlichtDevice *m_device;
};
-
-struct TextDestChat : public TextDest
+// Not so precise one which works without irrlicht
+class SimpleTimeGetter: public TimeGetter
{
- TextDestChat(Client *client)
+public:
+ u32 getTime()
{
- m_client = client;
+ return porting::getTimeMs();
}
- void gotText(std::wstring text)
- {
- // Discard empty line
- if(text == L"")
- return;
-
- // Parse command (server command starts with "/#")
- if(text[0] == L'/' && text[1] != L'#')
- {
- std::wstring reply = L"Local: ";
+};
- reply += L"Local commands not yet supported. "
- L"Server prefix is \"/#\".";
-
- m_client->addChatMessage(reply);
- return;
- }
+// A pointer to a global instance of the time getter
+// TODO: why?
+TimeGetter *g_timegetter = NULL;
- // Send to others
- m_client->sendChatMessage(text);
- // Show locally
- m_client->addChatMessage(text);
- }
+u32 getTimeMs()
+{
+ if(g_timegetter == NULL)
+ return 0;
+ return g_timegetter->getTime();
+}
- Client *m_client;
-};
+/*
+ Event handler for Irrlicht
+
+ NOTE: Everything possible should be moved out from here,
+ probably to InputHandler and the_game
+*/
class MyEventReceiver : public IEventReceiver
{
@@ -594,7 +502,6 @@ public:
*/
if(noMenuActive() == false)
{
- clearInput();
return false;
}
@@ -604,82 +511,7 @@ public:
keyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
if(event.KeyInput.PressedDown)
- {
- //dstream<<"Pressed key: "<<(char)event.KeyInput.Key<<std::endl;
-
- /*
- Launch menus
- */
-
- if(guienv != NULL && guiroot != NULL && g_device != NULL)
- {
- if(event.KeyInput.Key == irr::KEY_ESCAPE)
- {
- dstream<<DTIME<<"MyEventReceiver: "
- <<"Launching pause menu"<<std::endl;
- // It will delete itself by itself
- (new GUIPauseMenu(guienv, guiroot, -1, &g_gamecallback,
- &g_menumgr))->drop();
- return true;
- }
- if(event.KeyInput.Key == irr::KEY_KEY_I)
- {
- dstream<<DTIME<<"MyEventReceiver: "
- <<"Launching inventory"<<std::endl;
- (new GUIInventoryMenu(guienv, guiroot, -1,
- &local_inventory, &inventory_action_queue,
- &g_menumgr))->drop();
- return true;
- }
- if(event.KeyInput.Key == irr::KEY_KEY_T)
- {
- TextDest *dest = new TextDestChat(g_client);
-
- (new GUITextInputMenu(guienv, guiroot, -1,
- &g_menumgr, dest,
- L""))->drop();
- }
- }
-
- // Item selection
- if(event.KeyInput.Key >= irr::KEY_KEY_0
- && event.KeyInput.Key <= irr::KEY_KEY_9)
- {
- u16 s1 = event.KeyInput.Key - irr::KEY_KEY_0;
- if(event.KeyInput.Key == irr::KEY_KEY_0)
- s1 = 10;
- if(s1 < PLAYER_INVENTORY_SIZE && s1 < hotbar_itemcount)
- g_selected_item = s1-1;
- dstream<<DTIME<<"Selected item: "
- <<g_selected_item<<std::endl;
- }
-
- // Viewing range selection
- if(event.KeyInput.Key == irr::KEY_KEY_R)
- {
- if(draw_control.range_all)
- {
- draw_control.range_all = false;
- dstream<<DTIME<<"Disabled full viewing range"<<std::endl;
- }
- else
- {
- draw_control.range_all = true;
- dstream<<DTIME<<"Enabled full viewing range"<<std::endl;
- }
- }
-
- // Print debug stacks
- if(event.KeyInput.Key == irr::KEY_KEY_P)
- {
- dstream<<"-----------------------------------------"
- <<std::endl;
- dstream<<DTIME<<"Printing debug stacks:"<<std::endl;
- dstream<<"-----------------------------------------"
- <<std::endl;
- debug_stacks_print();
- }
- }
+ keyWasDown[event.KeyInput.Key] = true;
}
if(event.EventType == irr::EET_MOUSE_INPUT_EVENT)
@@ -715,25 +547,7 @@ public:
}
if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
{
- /*dstream<<"event.MouseInput.Wheel="
- <<event.MouseInput.Wheel<<std::endl;*/
-
- u16 max_item = MYMIN(PLAYER_INVENTORY_SIZE-1,
- hotbar_itemcount-1);
- if(event.MouseInput.Wheel < 0)
- {
- if(g_selected_item < max_item)
- g_selected_item++;
- else
- g_selected_item = 0;
- }
- else if(event.MouseInput.Wheel > 0)
- {
- if(g_selected_item > 0)
- g_selected_item--;
- else
- g_selected_item = max_item;
- }
+ mouse_wheel += event.MouseInput.Wheel;
}
}
}
@@ -741,16 +555,33 @@ public:
return false;
}
- // This is used to check whether a key is being held down
- virtual bool IsKeyDown(EKEY_CODE keyCode) const
+ bool IsKeyDown(EKEY_CODE keyCode) const
{
return keyIsDown[keyCode];
}
+
+ // Checks whether a key was down and resets the state
+ bool WasKeyDown(EKEY_CODE keyCode)
+ {
+ bool b = keyWasDown[keyCode];
+ keyWasDown[keyCode] = false;
+ return b;
+ }
+
+ s32 getMouseWheel()
+ {
+ s32 a = mouse_wheel;
+ mouse_wheel = 0;
+ return a;
+ }
void clearInput()
{
- for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
- keyIsDown[i] = false;
+ for(u32 i=0; i<KEY_KEY_CODES_COUNT; i++)
+ {
+ keyIsDown[i] = false;
+ keyWasDown[i] = false;
+ }
leftclicked = false;
rightclicked = false;
@@ -760,6 +591,8 @@ public:
left_active = false;
middle_active = false;
right_active = false;
+
+ mouse_wheel = 0;
}
MyEventReceiver()
@@ -776,48 +609,20 @@ public:
bool middle_active;
bool right_active;
+ s32 mouse_wheel;
+
private:
- // We use this array to store the current state of each key
- bool keyIsDown[KEY_KEY_CODES_COUNT];
- //s32 mouseX;
- //s32 mouseY;
IrrlichtDevice *m_device;
-};
-
-class InputHandler
-{
-public:
- InputHandler()
- {
- }
- virtual ~InputHandler()
- {
- }
-
- virtual bool isKeyDown(EKEY_CODE keyCode) = 0;
-
- virtual v2s32 getMousePos() = 0;
- virtual void setMousePos(s32 x, s32 y) = 0;
-
- virtual bool getLeftState() = 0;
- virtual bool getRightState() = 0;
-
- virtual bool getLeftClicked() = 0;
- virtual bool getRightClicked() = 0;
- virtual void resetLeftClicked() = 0;
- virtual void resetRightClicked() = 0;
-
- virtual bool getLeftReleased() = 0;
- virtual bool getRightReleased() = 0;
- virtual void resetLeftReleased() = 0;
- virtual void resetRightReleased() = 0;
- virtual void step(float dtime) {};
-
- virtual void clear() {};
+ // The current state of keys
+ bool keyIsDown[KEY_KEY_CODES_COUNT];
+ // Whether a key has been pressed or not
+ bool keyWasDown[KEY_KEY_CODES_COUNT];
};
-InputHandler *g_input = NULL;
+/*
+ Separated input handler
+*/
class RealInputHandler : public InputHandler
{
@@ -831,6 +636,10 @@ public:
{
return m_receiver->IsKeyDown(keyCode);
}
+ virtual bool wasKeyDown(EKEY_CODE keyCode)
+ {
+ return m_receiver->WasKeyDown(keyCode);
+ }
virtual v2s32 getMousePos()
{
return m_device->getCursorControl()->getPosition();
@@ -883,10 +692,14 @@ public:
m_receiver->rightreleased = false;
}
+ virtual s32 getMouseWheel()
+ {
+ return m_receiver->getMouseWheel();
+ }
+
void clear()
{
- resetRightClicked();
- resetLeftClicked();
+ m_receiver->clearInput();
}
private:
IrrlichtDevice *m_device;
@@ -911,6 +724,10 @@ public:
{
return keydown[keyCode];
}
+ virtual bool wasKeyDown(EKEY_CODE keyCode)
+ {
+ return false;
+ }
virtual v2s32 getMousePos()
{
return mousepos;
@@ -963,6 +780,11 @@ public:
rightreleased = false;
}
+ virtual s32 getMouseWheel()
+ {
+ return 0;
+ }
+
virtual void step(float dtime)
{
{
@@ -971,7 +793,8 @@ public:
if(counter1 < 0.0)
{
counter1 = 0.1*Rand(1, 40);
- keydown[irr::KEY_SPACE] = !keydown[irr::KEY_SPACE];
+ keydown[getKeySetting("keymap_jump")] =
+ !keydown[getKeySetting("keymap_jump")];
}
}
{
@@ -980,7 +803,8 @@ public:
if(counter1 < 0.0)
{
counter1 = 0.1*Rand(1, 40);
- keydown[irr::KEY_KEY_E] = !keydown[irr::KEY_KEY_E];
+ keydown[getKeySetting("keymap_special1")] =
+ !keydown[getKeySetting("keymap_special1")];
}
}
{
@@ -989,7 +813,8 @@ public:
if(counter1 < 0.0)
{
counter1 = 0.1*Rand(1, 40);
- keydown[irr::KEY_KEY_W] = !keydown[irr::KEY_KEY_W];
+ keydown[getKeySetting("keymap_forward")] =
+ !keydown[getKeySetting("keymap_forward")];
}
}
{
@@ -998,7 +823,8 @@ public:
if(counter1 < 0.0)
{
counter1 = 0.1*Rand(1, 40);
- keydown[irr::KEY_KEY_A] = !keydown[irr::KEY_KEY_A];
+ keydown[getKeySetting("keymap_left")] =
+ !keydown[getKeySetting("keymap_left")];
}
}
{
@@ -1055,206 +881,6 @@ private:
bool rightreleased;
};
-void updateViewingRange(f32 frametime_in, Client *client)
-{
- if(draw_control.range_all == true)
- return;
-
- static f32 added_frametime = 0;
- static s16 added_frames = 0;
-
- added_frametime += frametime_in;
- added_frames += 1;
-
- // Actually this counter kind of sucks because frametime is busytime
- static f32 counter = 0;
- counter -= frametime_in;
- if(counter > 0)
- return;
- //counter = 0.1;
- counter = 0.2;
-
- /*dstream<<__FUNCTION_NAME
- <<": Collected "<<added_frames<<" frames, total of "
- <<added_frametime<<"s."<<std::endl;*/
-
- /*dstream<<"draw_control.blocks_drawn="
- <<draw_control.blocks_drawn
- <<", draw_control.blocks_would_have_drawn="
- <<draw_control.blocks_would_have_drawn
- <<std::endl;*/
-
- float range_min = g_settings.getS16("viewing_range_nodes_min");
- float range_max = g_settings.getS16("viewing_range_nodes_max");
-
- draw_control.wanted_min_range = range_min;
- draw_control.wanted_max_blocks = (1.2*draw_control.blocks_drawn)+1;
-
- float block_draw_ratio = 1.0;
- if(draw_control.blocks_would_have_drawn != 0)
- {
- block_draw_ratio = (float)draw_control.blocks_drawn
- / (float)draw_control.blocks_would_have_drawn;
- }
-
- // Calculate the average frametime in the case that all wanted
- // blocks had been drawn
- f32 frametime = added_frametime / added_frames / block_draw_ratio;
-
- added_frametime = 0.0;
- added_frames = 0;
-
- float wanted_fps = g_settings.getFloat("wanted_fps");
- float wanted_frametime = 1.0 / wanted_fps;
-
- f32 wanted_frametime_change = wanted_frametime - frametime;
- //dstream<<"wanted_frametime_change="<<wanted_frametime_change<<std::endl;
-
- // If needed frametime change is very small, just return
- if(fabs(wanted_frametime_change) < wanted_frametime*0.2)
- {
- //dstream<<"ignoring small wanted_frametime_change"<<std::endl;
- return;
- }
-
- float range = draw_control.wanted_range;
- float new_range = range;
-
- static s16 range_old = 0;
- static f32 frametime_old = 0;
-
- float d_range = range - range_old;
- f32 d_frametime = frametime - frametime_old;
- // A sane default of 30ms per 50 nodes of range
- static f32 time_per_range = 30. / 50;
- if(d_range != 0)
- {
- time_per_range = d_frametime / d_range;
- }
-
- // The minimum allowed calculated frametime-range derivative:
- // Practically this sets the maximum speed of changing the range.
- // The lower this value, the higher the maximum changing speed.
- // A low value here results in wobbly range (0.001)
- // A high value here results in slow changing range (0.0025)
- // SUGG: This could be dynamically adjusted so that when
- // the camera is turning, this is lower
- //float min_time_per_range = 0.0015;
- float min_time_per_range = 0.0010;
- //float min_time_per_range = 0.05 / range;
- if(time_per_range < min_time_per_range)
- {
- time_per_range = min_time_per_range;
- //dstream<<"time_per_range="<<time_per_range<<" (min)"<<std::endl;
- }
- else
- {
- //dstream<<"time_per_range="<<time_per_range<<std::endl;
- }
-
- f32 wanted_range_change = wanted_frametime_change / time_per_range;
- // Dampen the change a bit to kill oscillations
- //wanted_range_change *= 0.9;
- //wanted_range_change *= 0.75;
- wanted_range_change *= 0.5;
- //dstream<<"wanted_range_change="<<wanted_range_change<<std::endl;
-
- // If needed range change is very small, just return
- if(fabs(wanted_range_change) < 0.001)
- {
- //dstream<<"ignoring small wanted_range_change"<<std::endl;
- return;
- }
-
- new_range += wanted_range_change;
- //dstream<<"new_range="<<new_range/*<<std::endl*/;
-
- //float new_range_unclamped = new_range;
- if(new_range < range_min)
- new_range = range_min;
- if(new_range > range_max)
- new_range = range_max;
-
- /*if(new_range != new_range_unclamped)
- dstream<<", clamped to "<<new_range<<std::endl;
- else
- dstream<<std::endl;*/
-
- draw_control.wanted_range = new_range;
-
- range_old = new_range;
- frametime_old = frametime;
-}
-
-void draw_hotbar(video::IVideoDriver *driver, gui::IGUIFont *font,
- v2s32 centerlowerpos, s32 imgsize, s32 itemcount,
- Inventory *inventory)
-{
- InventoryList *mainlist = inventory->getList("main");
- if(mainlist == NULL)
- {
- dstream<<"WARNING: draw_hotbar(): mainlist == NULL"<<std::endl;
- return;
- }
-
- s32 padding = imgsize/12;
- //s32 height = imgsize + padding*2;
- s32 width = itemcount*(imgsize+padding*2);
-
- // Position of upper left corner of bar
- v2s32 pos = centerlowerpos - v2s32(width/2, imgsize+padding*2);
-
- // Draw background color
- /*core::rect<s32> barrect(0,0,width,height);
- barrect += pos;
- video::SColor bgcolor(255,128,128,128);
- driver->draw2DRectangle(bgcolor, barrect, NULL);*/
-
- core::rect<s32> imgrect(0,0,imgsize,imgsize);
-
- for(s32 i=0; i<itemcount; i++)
- {
- InventoryItem *item = mainlist->getItem(i);
-
- core::rect<s32> rect = imgrect + pos
- + v2s32(padding+i*(imgsize+padding*2), padding);
-
- if(g_selected_item == i)
- {
- driver->draw2DRectangle(video::SColor(255,255,0,0),
- core::rect<s32>(rect.UpperLeftCorner - v2s32(1,1)*padding,
- rect.LowerRightCorner + v2s32(1,1)*padding),
- NULL);
- }
- else
- {
- video::SColor bgcolor2(128,0,0,0);
- driver->draw2DRectangle(bgcolor2, rect, NULL);
- }
-
- if(item != NULL)
- {
- drawInventoryItem(driver, font, item, rect, NULL);
- }
- }
-}
-
-// Chat data
-struct ChatLine
-{
- ChatLine():
- age(0.0)
- {
- }
- ChatLine(const std::wstring &a_text):
- age(0.0),
- text(a_text)
- {
- }
- float age;
- std::wstring text;
-};
-
// These are defined global so that they're not optimized too much.
// Can't change them to volatile.
s16 temp16;
@@ -1353,6 +979,53 @@ void SpeedTests()
}
}
+void drawMenuBackground(video::IVideoDriver* driver)
+{
+ core::dimension2d<u32> screensize = driver->getScreenSize();
+
+ video::ITexture *bgtexture =
+ driver->getTexture(getTexturePath("mud.png").c_str());
+ if(bgtexture)
+ {
+ s32 texturesize = 128;
+ s32 tiled_y = screensize.Height / texturesize + 1;
+ s32 tiled_x = screensize.Width / texturesize + 1;
+
+ for(s32 y=0; y<tiled_y; y++)
+ for(s32 x=0; x<tiled_x; x++)
+ {
+ core::rect<s32> rect(0,0,texturesize,texturesize);
+ rect += v2s32(x*texturesize, y*texturesize);
+ driver->draw2DImage(bgtexture, rect,
+ core::rect<s32>(core::position2d<s32>(0,0),
+ core::dimension2di(bgtexture->getSize())),
+ NULL, NULL, true);
+ }
+ }
+
+ video::ITexture *logotexture =
+ driver->getTexture(getTexturePath("menulogo.png").c_str());
+ if(logotexture)
+ {
+ v2s32 logosize(logotexture->getOriginalSize().Width,
+ logotexture->getOriginalSize().Height);
+ logosize *= 4;
+
+ video::SColor bgcolor(255,50,50,50);
+ core::rect<s32> bgrect(0, screensize.Height-logosize.Y-20,
+ screensize.Width, screensize.Height);
+ driver->draw2DRectangle(bgcolor, bgrect, NULL);
+
+ core::rect<s32> rect(0,0,logosize.X,logosize.Y);
+ rect += v2s32(screensize.Width/2,screensize.Height-10-logosize.Y);
+ rect -= v2s32(logosize.X/2, 0);
+ driver->draw2DImage(logotexture, rect,
+ core::rect<s32>(core::position2d<s32>(0,0),
+ core::dimension2di(logotexture->getSize())),
+ NULL, NULL, true);
+ }
+}
+
int main(int argc, char *argv[])
{
/*
@@ -1536,18 +1209,20 @@ int main(int argc, char *argv[])
return 0;*/
/*
- Some parameters
+ Game parameters
*/
// Port
u16 port = 30000;
if(cmd_args.exists("port"))
port = cmd_args.getU16("port");
- else if(cmd_args.exists("port"))
+ else if(g_settings.exists("port"))
port = g_settings.getU16("port");
+ if(port == 0)
+ port = 30000;
// Map directory
- std::string map_dir = porting::path_userdata+"/map";
+ std::string map_dir = porting::path_userdata+"/world";
if(cmd_args.exists("map-dir"))
map_dir = cmd_args.get("map-dir");
else if(g_settings.exists("map-dir"))
@@ -1558,6 +1233,9 @@ int main(int argc, char *argv[])
{
DSTACK("Dedicated server branch");
+ // Create time getter
+ g_timegetter = new SimpleTimeGetter();
+
// Create server
Server server(map_dir.c_str());
server.start(port);
@@ -1568,6 +1246,7 @@ int main(int argc, char *argv[])
return 0;
}
+
/*
More parameters
*/
@@ -1586,6 +1265,10 @@ int main(int argc, char *argv[])
std::string playername = g_settings.get("name");
+ /*
+ Device initialization
+ */
+
// Resolution selection
bool fullscreen = false;
@@ -1617,7 +1300,9 @@ int main(int argc, char *argv[])
driverType = video::EDT_OPENGL;
}
- // create device and exit if creation failed
+ /*
+ Create device and exit if creation failed
+ */
MyEventReceiver receiver;
@@ -1629,10 +1314,17 @@ int main(int argc, char *argv[])
if (device == 0)
return 1; // could not create selected driver.
- g_device = device;
- g_irrlicht = new IrrlichtWrapper(device);
- TextureSource *texturesource = new TextureSource(device);
- g_texturesource = texturesource;
+ // Set device in game parameters
+ device = device;
+
+ // Create time getter
+ g_timegetter = new IrrlichtTimeGetter(device);
+
+ // Create game callback for menus
+ g_gamecallback = new MainGameCallback(device);
+
+ // Create texture source
+ g_texturesource = new TextureSource(device);
/*
Speed tests (done after irrlicht is loaded to get timer)
@@ -1648,16 +1340,17 @@ int main(int argc, char *argv[])
bool random_input = g_settings.getBool("random_input")
|| cmd_args.getFlag("random-input");
+ InputHandler *input = NULL;
if(random_input)
- g_input = new RandomInputHandler();
+ input = new RandomInputHandler();
else
- g_input = new RealInputHandler(device, &receiver);
+ input = new RealInputHandler(device, &receiver);
/*
Continue initialization
*/
- video::IVideoDriver* driver = device->getVideoDriver();
+ //video::IVideoDriver* driver = device->getVideoDriver();
/*
This changes the minimum allowed number of vertices in a VBO.
@@ -1669,7 +1362,7 @@ int main(int argc, char *argv[])
guienv = device->getGUIEnvironment();
gui::IGUISkin* skin = guienv->getSkin();
- gui::IGUIFont* font = guienv->getFont(porting::getDataPath("fontlucida.png").c_str());
+ gui::IGUIFont* font = guienv->getFont(getTexturePath("fontlucida.png").c_str());
if(font)
skin->setFont(font);
else
@@ -1678,7 +1371,7 @@ int main(int argc, char *argv[])
// If font was not found, this will get us one
font = skin->getFont();
assert(font);
-
+
u32 text_height = font->getDimension(L"Hello, world!").Height;
dstream<<"text_height="<<text_height<<std::endl;
@@ -1702,1440 +1395,219 @@ int main(int argc, char *argv[])
*/
/*
- We need some kind of a root node to be able to add
- custom gui elements directly on the screen.
- Otherwise they won't be automatically drawn.
- */
- guiroot = guienv->addStaticText(L"",
- core::rect<s32>(0, 0, 10000, 10000));
-
- // First line of debug text
- gui::IGUIStaticText *guitext = guienv->addStaticText(
- L"",
- core::rect<s32>(5, 5, 795, 5+text_height),
- false, false);
- // Second line of debug text
- gui::IGUIStaticText *guitext2 = guienv->addStaticText(
- L"",
- core::rect<s32>(5, 5+(text_height+5)*1, 795, (5+text_height)*2),
- false, false);
-
- // At the middle of the screen
- // Object infos are shown in this
- gui::IGUIStaticText *guitext_info = guienv->addStaticText(
- L"",
- core::rect<s32>(0,0,400,text_height+5) + v2s32(100,200),
- false, false);
-
- // Chat text
- gui::IGUIStaticText *guitext_chat = guienv->addStaticText(
- L"",
- core::rect<s32>(0,0,0,0),
- false, false); // Disable word wrap as of now
- //false, true);
- //guitext_chat->setBackgroundColor(video::SColor(96,0,0,0));
- core::list<ChatLine> chat_lines;
-
- /*
If an error occurs, this is set to something and the
menu-game loop is restarted. It is then displayed before
the menu.
*/
std::wstring error_message = L"";
-
- /*
- Menu-game loop
- */
- while(g_device->run() && kill == false)
- {
-
- // This is used for catching disconnects
- try
- {
-
- /*
- Out-of-game menu loop.
-
- Loop quits when menu returns proper parameters.
- */
- while(kill == false)
- {
- // Cursor can be non-visible when coming from the game
- device->getCursorControl()->setVisible(true);
- // Some stuff are left to scene manager when coming from the game
- // (map at least?)
- smgr->clear();
- // Reset or hide the debug gui texts
- guitext->setText(L"Minetest-c55");
- guitext2->setVisible(false);
- guitext_info->setVisible(false);
- guitext_chat->setVisible(false);
-
- // Initialize menu data
- MainMenuData menudata;
- menudata.address = narrow_to_wide(address);
- menudata.name = narrow_to_wide(playername);
- menudata.port = narrow_to_wide(itos(port));
- menudata.creative_mode = g_settings.getBool("creative_mode");
-
- GUIMainMenu *menu =
- new GUIMainMenu(guienv, guiroot, -1,
- &g_menumgr, &menudata, &g_gamecallback);
- menu->allowFocusRemoval(true);
-
- if(error_message != L"")
- {
- GUIMessageMenu *menu2 =
- new GUIMessageMenu(guienv, guiroot, -1,
- &g_menumgr, error_message.c_str());
- menu2->drop();
- error_message = L"";
- }
-
- video::IVideoDriver* driver = g_device->getVideoDriver();
-
- dstream<<"Created main menu"<<std::endl;
-
- while(g_device->run() && kill == false)
- {
- // Run global IrrlichtWrapper's main thread processing stuff
- g_irrlicht->Run();
-
- if(menu->getStatus() == true)
- break;
-
- //driver->beginScene(true, true, video::SColor(255,0,0,0));
- driver->beginScene(true, true, video::SColor(255,128,128,128));
- guienv->drawAll();
- driver->endScene();
- }
-
- // Break out of menu-game loop to shut down cleanly
- if(g_device->run() == false || kill == true)
- break;
-
- dstream<<"Dropping main menu"<<std::endl;
-
- menu->drop();
-
- // Delete map if requested
- if(menudata.delete_map)
- {
- bool r = fs::RecursiveDeleteContent(map_dir);
- if(r == false)
- error_message = L"Delete failed";
- continue;
- }
-
- playername = wide_to_narrow(menudata.name);
- address = wide_to_narrow(menudata.address);
- port = stoi(wide_to_narrow(menudata.port));
- g_settings.set("creative_mode", itos(menudata.creative_mode));
-
- // Check for valid parameters, restart menu if invalid.
- if(playername == "")
- {
- error_message = L"Name required.";
- continue;
- }
-
- // Save settings
- g_settings.set("name", playername);
- g_settings.set("address", address);
- g_settings.set("port", itos(port));
- // Update configuration file
- if(configpath != "")
- g_settings.updateConfigFile(configpath.c_str());
-
- // Continue to game
- break;
- }
-
- // Break out of menu-game loop to shut down cleanly
- if(g_device->run() == false)
- break;
-
- /*
- Make a scope here so that the client and the server and other
- stuff gets removed when disconnected or the irrlicht device
- is removed.
- */
- {
-
- // This is set to true at the end of the scope
- g_irrlicht->Shutdown(false);
-
- /*
- Draw "Loading" screen
- */
- const wchar_t *text = L"Loading and connecting...";
- core::vector2d<s32> center(screenW/2, screenH/2);
- core::vector2d<s32> textsize(300, text_height);
- core::rect<s32> textrect(center - textsize/2, center + textsize/2);
-
- gui::IGUIStaticText *gui_loadingtext = guienv->addStaticText(
- text, textrect, false, false);
- gui_loadingtext->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
-
- driver->beginScene(true, true, video::SColor(255,0,0,0));
- guienv->drawAll();
- driver->endScene();
-
- std::cout<<DTIME<<"Creating server and client"<<std::endl;
-
- /*
- Create server.
- SharedPtr will delete it when it goes out of scope.
- */
- SharedPtr<Server> server;
- if(address == ""){
- server = new Server(map_dir);
- server->start(port);
- }
-
- /*
- Create client
- */
-
- Client client(device, playername.c_str(), draw_control);
-
- g_client = &client;
-
- Address connect_address(0,0,0,0, port);
- try{
- if(address == "")
- //connect_address.Resolve("localhost");
- connect_address.setAddress(127,0,0,1);
- else
- connect_address.Resolve(address.c_str());
- }
- catch(ResolveError &e)
- {
- std::cout<<DTIME<<"Couldn't resolve address"<<std::endl;
- //return 0;
- error_message = L"Couldn't resolve address";
- gui_loadingtext->remove();
- continue;
- }
-
- dstream<<DTIME<<"Connecting to server at ";
- connect_address.print(&dstream);
- dstream<<std::endl;
- client.connect(connect_address);
-
- try{
- while(client.connectedAndInitialized() == false)
- {
- // Update screen
- driver->beginScene(true, true, video::SColor(255,0,0,0));
- guienv->drawAll();
- driver->endScene();
-
- // Update client and server
-
- client.step(0.1);
-
- if(server != NULL)
- server->step(0.1);
-
- // Delay a bit
- sleep_ms(100);
- }
- }
- catch(con::PeerNotFoundException &e)
- {
- std::cout<<DTIME<<"Timed out."<<std::endl;
- //return 0;
- error_message = L"Connection timed out.";
- gui_loadingtext->remove();
- continue;
- }
-
- /*
- Create skybox
- */
- /*scene::ISceneNode* skybox;
- skybox = smgr->addSkyBoxSceneNode(
- driver->getTexture(porting::getDataPath("skybox2.png").c_str()),
- driver->getTexture(porting::getDataPath("skybox3.png").c_str()),
- driver->getTexture(porting::getDataPath("skybox1.png").c_str()),
- driver->getTexture(porting::getDataPath("skybox1.png").c_str()),
- driver->getTexture(porting::getDataPath("skybox1.png").c_str()),
- driver->getTexture(porting::getDataPath("skybox1.png").c_str()));*/
-
- /*
- Create the camera node
- */
-
- scene::ICameraSceneNode* camera = smgr->addCameraSceneNode(
- 0, // Camera parent
- v3f(BS*100, BS*2, BS*100), // Look from
- v3f(BS*100+1, BS*2, BS*100), // Look to
- -1 // Camera ID
- );
-
- if(camera == NULL)
- return 1;
-
- //video::SColor skycolor = video::SColor(255,90,140,200);
- //video::SColor skycolor = video::SColor(255,166,202,244);
- video::SColor skycolor = video::SColor(255,120,185,244);
-
- camera->setFOV(FOV_ANGLE);
- // Just so big a value that everything rendered is visible
- camera->setFarValue(100000*BS);
-
- /*
- Lighting test code. Doesn't quite work this way.
- The CPU-computed lighting is good.
- */
-
- /*
- smgr->addLightSceneNode(NULL,
- v3f(0, BS*1000000, 0),
- video::SColorf(0.3,0.3,0.3),
- BS*10000000);
-
- smgr->setAmbientLight(video::SColorf(0.0, 0.0, 0.0));
-
- scene::ILightSceneNode *light = smgr->addLightSceneNode(camera,
- v3f(0, 0, 0), video::SColorf(0.5,0.5,0.5), BS*4);
- */
-
- f32 camera_yaw = 0; // "right/left"
- f32 camera_pitch = 0; // "up/down"
-
- /*
- Move into game
- */
-
- gui_loadingtext->remove();
-
- /*
- Add some gui stuff
- */
-
- /*GUIQuickInventory *quick_inventory = new GUIQuickInventory
- (guienv, NULL, v2s32(10, 70), 5, &local_inventory);*/
- /*GUIQuickInventory *quick_inventory = new GUIQuickInventory
- (guienv, NULL, v2s32(0, 0), quickinv_itemcount, &local_inventory);*/
-
- // Test the text input system
- /*(new GUITextInputMenu(guienv, guiroot, -1, &g_menumgr,
- NULL))->drop();*/
- /*GUIMessageMenu *menu =
- new GUIMessageMenu(guienv, guiroot, -1,
- &g_menumgr,
- L"Asd");
- menu->drop();*/
-
- // Launch pause menu
- (new GUIPauseMenu(guienv, guiroot, -1, &g_gamecallback,
- &g_menumgr))->drop();
-
- // Enable texts
- guitext2->setVisible(true);
- guitext_info->setVisible(true);
- guitext_chat->setVisible(true);
-
- //s32 guitext_chat_pad_bottom = 70;
+ // The password entered during the menu screen,
+ std::string password;
- v2u32 screensize(0,0);
- v2u32 last_screensize(0,0);
-
/*
- Some statistics are collected in these
- */
- u32 drawtime = 0;
- u32 beginscenetime = 0;
- u32 scenetime = 0;
- u32 endscenetime = 0;
-
- // A test
- //throw con::PeerNotFoundException("lol");
-
- core::list<float> frametime_log;
-
- /*
- Main loop
+ Menu-game loop
*/
-
- bool first_loop_after_window_activation = true;
-
- // Time is in milliseconds
- // NOTE: getRealTime() causes strange problems in wine (imprecision?)
- // NOTE: So we have to use getTime() and call run()s between them
- u32 lasttime = device->getTimer()->getTime();
-
while(device->run() && kill == false)
{
- if(g_disconnect_requested)
- {
- g_disconnect_requested = false;
- break;
- }
-
- /*
- Run global IrrlichtWrapper's main thread processing stuff
- */
- g_irrlicht->Run();
-
- /*
- Process TextureSource's queue
- */
- texturesource->processQueue();
-
- /*
- Random calculations
- */
- last_screensize = screensize;
- screensize = driver->getScreenSize();
- v2s32 displaycenter(screensize.X/2,screensize.Y/2);
- //bool screensize_changed = screensize != last_screensize;
-
- // Hilight boxes collected during the loop and displayed
- core::list< core::aabbox3d<f32> > hilightboxes;
-
- // Info text
- std::wstring infotext;
-
- // When screen size changes, update positions and sizes of stuff
- /*if(screensize_changed)
- {
- v2s32 pos(displaycenter.X-((quickinv_itemcount-1)*quickinv_spacing+quickinv_size)/2, screensize.Y-quickinv_spacing);
- quick_inventory->updatePosition(pos);
- }*/
-
- //TimeTaker //timer1("//timer1");
-
- // Time of frame without fps limit
- float busytime;
- u32 busytime_u32;
- {
- // not using getRealTime is necessary for wine
- u32 time = device->getTimer()->getTime();
- if(time > lasttime)
- busytime_u32 = time - lasttime;
- else
- busytime_u32 = 0;
- busytime = busytime_u32 / 1000.0;
- }
-
- //std::cout<<"busytime_u32="<<busytime_u32<<std::endl;
-
- // Absolutelu necessary for wine!
- device->run();
-
- /*
- Viewing range
- */
-
- updateViewingRange(busytime, &client);
-
- /*
- FPS limiter
- */
+ // This is used for catching disconnects
+ try
{
- float fps_max = g_settings.getFloat("fps_max");
- u32 frametime_min = 1000./fps_max;
-
- if(busytime_u32 < frametime_min)
- {
- u32 sleeptime = frametime_min - busytime_u32;
- device->sleep(sleeptime);
- }
- }
-
- // Absolutelu necessary for wine!
- device->run();
- /*
- Time difference calculation
- */
- f32 dtime; // in seconds
-
- u32 time = device->getTimer()->getTime();
- if(time > lasttime)
- dtime = (time - lasttime) / 1000.0;
- else
- dtime = 0;
- lasttime = time;
-
- /*
- Log frametime for visualization
- */
- frametime_log.push_back(dtime);
- if(frametime_log.size() > 100)
- {
- core::list<float>::Iterator i = frametime_log.begin();
- frametime_log.erase(i);
- }
-
- /*
- Visualize frametime in terminal
- */
- /*for(u32 i=0; i<dtime*400; i++)
- std::cout<<"X";
- std::cout<<std::endl;*/
-
- /*
- Time average and jitter calculation
- */
-
- static f32 dtime_avg1 = 0.0;
- dtime_avg1 = dtime_avg1 * 0.98 + dtime * 0.02;
- f32 dtime_jitter1 = dtime - dtime_avg1;
-
- static f32 dtime_jitter1_max_sample = 0.0;
- static f32 dtime_jitter1_max_fraction = 0.0;
- {
- static f32 jitter1_max = 0.0;
- static f32 counter = 0.0;
- if(dtime_jitter1 > jitter1_max)
- jitter1_max = dtime_jitter1;
- counter += dtime;
- if(counter > 0.0)
- {
- counter -= 3.0;
- dtime_jitter1_max_sample = jitter1_max;
- dtime_jitter1_max_fraction
- = dtime_jitter1_max_sample / (dtime_avg1+0.001);
- jitter1_max = 0.0;
- }
- }
-
- /*
- Busytime average and jitter calculation
- */
-
- static f32 busytime_avg1 = 0.0;
- busytime_avg1 = busytime_avg1 * 0.98 + busytime * 0.02;
- f32 busytime_jitter1 = busytime - busytime_avg1;
-
- static f32 busytime_jitter1_max_sample = 0.0;
- static f32 busytime_jitter1_min_sample = 0.0;
- {
- static f32 jitter1_max = 0.0;
- static f32 jitter1_min = 0.0;
- static f32 counter = 0.0;
- if(busytime_jitter1 > jitter1_max)
- jitter1_max = busytime_jitter1;
- if(busytime_jitter1 < jitter1_min)
- jitter1_min = busytime_jitter1;
- counter += dtime;
- if(counter > 0.0){
- counter -= 3.0;
- busytime_jitter1_max_sample = jitter1_max;
- busytime_jitter1_min_sample = jitter1_min;
- jitter1_max = 0.0;
- jitter1_min = 0.0;
- }
- }
-
- /*
- Debug info for client
- */
- {
- static float counter = 0.0;
- counter -= dtime;
- if(counter < 0)
- {
- counter = 30.0;
- client.printDebugInfo(std::cout);
- }
- }
-
- /*
- Input handler step()
- */
- g_input->step(dtime);
-
- /*
- Player speed control
- */
-
- {
- /*bool a_up,
- bool a_down,
- bool a_left,
- bool a_right,
- bool a_jump,
- bool a_superspeed,
- bool a_sneak,
- float a_pitch,
- float a_yaw*/
- PlayerControl control(
- g_input->isKeyDown(irr::KEY_KEY_W),
- g_input->isKeyDown(irr::KEY_KEY_S),
- g_input->isKeyDown(irr::KEY_KEY_A),
- g_input->isKeyDown(irr::KEY_KEY_D),
- g_input->isKeyDown(irr::KEY_SPACE),
- g_input->isKeyDown(irr::KEY_KEY_E),
- g_input->isKeyDown(irr::KEY_LSHIFT)
- || g_input->isKeyDown(irr::KEY_RSHIFT),
- camera_pitch,
- camera_yaw
- );
- client.setPlayerControl(control);
- }
-
- /*
- Process environment
- */
-
- {
- //TimeTaker timer("client.step(dtime)");
- client.step(dtime);
- //client.step(dtime_avg1);
- }
-
- if(server != NULL)
- {
- //TimeTaker timer("server->step(dtime)");
- server->step(dtime);
- }
-
- v3f player_position = client.getPlayerPosition();
-
- //TimeTaker //timer2("//timer2");
-
- /*
- Mouse and camera control
- */
-
- if((device->isWindowActive() && noMenuActive()) || random_input)
- {
- if(!random_input)
- device->getCursorControl()->setVisible(false);
-
- if(first_loop_after_window_activation){
- //std::cout<<"window active, first loop"<<std::endl;
- first_loop_after_window_activation = false;
- }
- else{
- s32 dx = g_input->getMousePos().X - displaycenter.X;
- s32 dy = g_input->getMousePos().Y - displaycenter.Y;
- //std::cout<<"window active, pos difference "<<dx<<","<<dy<<std::endl;
- camera_yaw -= dx*0.2;
- camera_pitch += dy*0.2;
- if(camera_pitch < -89.5) camera_pitch = -89.5;
- if(camera_pitch > 89.5) camera_pitch = 89.5;
- }
- g_input->setMousePos(displaycenter.X, displaycenter.Y);
- }
- else{
- device->getCursorControl()->setVisible(true);
-
- //std::cout<<"window inactive"<<std::endl;
- first_loop_after_window_activation = true;
- }
-
- camera_yaw = wrapDegrees(camera_yaw);
- camera_pitch = wrapDegrees(camera_pitch);
-
- v3f camera_direction = v3f(0,0,1);
- camera_direction.rotateYZBy(camera_pitch);
- camera_direction.rotateXZBy(camera_yaw);
-
- // This is at the height of the eyes of the current figure
- //v3f camera_position = player_position + v3f(0, BS+BS/2, 0);
- // This is more like in minecraft
- v3f camera_position = player_position + v3f(0, BS+BS*0.625, 0);
-
- camera->setPosition(camera_position);
- // *100.0 helps in large map coordinates
- camera->setTarget(camera_position + camera_direction * 100.0);
-
- if(FIELD_OF_VIEW_TEST){
- client.updateCamera(v3f(0,0,0), v3f(0,0,1));
- }
- else{
- //TimeTaker timer("client.updateCamera");
- client.updateCamera(camera_position, camera_direction);
- }
-
- //timer2.stop();
- //TimeTaker //timer3("//timer3");
-
- /*
- Calculate what block is the crosshair pointing to
- */
-
- //u32 t1 = device->getTimer()->getRealTime();
-
- //f32 d = 4; // max. distance
- f32 d = 4; // max. distance
- core::line3d<f32> shootline(camera_position,
- camera_position + camera_direction * BS * (d+1));
-
- MapBlockObject *selected_object = client.getSelectedObject
- (d*BS, camera_position, shootline);
-
- /*
- If it's pointing to a MapBlockObject
- */
-
- if(selected_object != NULL)
- {
- //dstream<<"Client returned selected_object != NULL"<<std::endl;
-
- core::aabbox3d<f32> box_on_map
- = selected_object->getSelectionBoxOnMap();
-
- hilightboxes.push_back(box_on_map);
-
- infotext = narrow_to_wide(selected_object->infoText());
-
- if(g_input->getLeftClicked())
- {
- std::cout<<DTIME<<"Left-clicked object"<<std::endl;
- client.clickObject(0, selected_object->getBlock()->getPos(),
- selected_object->getId(), g_selected_item);
- }
- else if(g_input->getRightClicked())
- {
- std::cout<<DTIME<<"Right-clicked object"<<std::endl;
- /*
- Check if we want to modify the object ourselves
- */
- if(selected_object->getTypeId() == MAPBLOCKOBJECT_TYPE_SIGN)
- {
- dstream<<"Sign object right-clicked"<<std::endl;
-
- if(random_input == false)
- {
- // Get a new text for it
-
- TextDest *dest = new TextDestSign(
- selected_object->getBlock()->getPos(),
- selected_object->getId(),
- &client);
-
- SignObject *sign_object = (SignObject*)selected_object;
-
- std::wstring wtext =
- narrow_to_wide(sign_object->getText());
-
- (new GUITextInputMenu(guienv, guiroot, -1,
- &g_menumgr, dest,
- wtext))->drop();
- }
- }
- /*
- Otherwise pass the event to the server as-is
- */
- else
- {
- client.clickObject(1, selected_object->getBlock()->getPos(),
- selected_object->getId(), g_selected_item);
- }
- }
- }
- else // selected_object == NULL
- {
-
- /*
- Find out which node we are pointing at
- */
-
- bool nodefound = false;
- v3s16 nodepos;
- v3s16 neighbourpos;
- core::aabbox3d<f32> nodehilightbox;
- f32 mindistance = BS * 1001;
-
- v3s16 pos_i = floatToInt(player_position, BS);
-
- /*std::cout<<"pos_i=("<<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z<<")"
- <<std::endl;*/
-
- s16 a = d;
- s16 ystart = pos_i.Y + 0 - (camera_direction.Y<0 ? a : 1);
- s16 zstart = pos_i.Z - (camera_direction.Z<0 ? a : 1);
- s16 xstart = pos_i.X - (camera_direction.X<0 ? a : 1);
- s16 yend = pos_i.Y + 1 + (camera_direction.Y>0 ? a : 1);
- s16 zend = pos_i.Z + (camera_direction.Z>0 ? a : 1);
- s16 xend = pos_i.X + (camera_direction.X>0 ? a : 1);
-
- for(s16 y = ystart; y <= yend; y++)
- for(s16 z = zstart; z <= zend; z++)
- for(s16 x = xstart; x <= xend; x++)
- {
- MapNode n;
- try
- {
- n = client.getNode(v3s16(x,y,z));
- if(content_pointable(n.d) == false)
- continue;
- }
- catch(InvalidPositionException &e)
- {
- continue;
- }
-
- v3s16 np(x,y,z);
- v3f npf = intToFloat(np, BS);
-
- f32 d = 0.01;
+ /*
+ Clear everything from the GUIEnvironment
+ */
+ guienv->clear();
- v3s16 dirs[6] = {
- v3s16(0,0,1), // back
- v3s16(0,1,0), // top
- v3s16(1,0,0), // right
- v3s16(0,0,-1), // front
- v3s16(0,-1,0), // bottom
- v3s16(-1,0,0), // left
- };
+ /*
+ We need some kind of a root node to be able to add
+ custom gui elements directly on the screen.
+ Otherwise they won't be automatically drawn.
+ */
+ guiroot = guienv->addStaticText(L"",
+ core::rect<s32>(0, 0, 10000, 10000));
/*
- Meta-objects
+ Out-of-game menu loop.
+
+ Loop quits when menu returns proper parameters.
*/
- if(n.d == CONTENT_TORCH)
+ while(kill == false)
{
- v3s16 dir = unpackDir(n.dir);
- v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
- dir_f *= BS/2 - BS/6 - BS/20;
- v3f cpf = npf + dir_f;
- f32 distance = (cpf - camera_position).getLength();
-
- core::aabbox3d<f32> box;
+ // Cursor can be non-visible when coming from the game
+ device->getCursorControl()->setVisible(true);
+ // Some stuff are left to scene manager when coming from the game
+ // (map at least?)
+ smgr->clear();
+ // Reset or hide the debug gui texts
+ /*guitext->setText(L"Minetest-c55");
+ guitext2->setVisible(false);
+ guitext_info->setVisible(false);
+ guitext_chat->setVisible(false);*/
- // bottom
- if(dir == v3s16(0,-1,0))
- {
- box = core::aabbox3d<f32>(
- npf - v3f(BS/6, BS/2, BS/6),
- npf + v3f(BS/6, -BS/2+BS/3*2, BS/6)
- );
- }
- // top
- else if(dir == v3s16(0,1,0))
- {
- box = core::aabbox3d<f32>(
- npf - v3f(BS/6, -BS/2+BS/3*2, BS/6),
- npf + v3f(BS/6, BS/2, BS/6)
- );
- }
- // side
- else
+ // Initialize menu data
+ MainMenuData menudata;
+ menudata.address = narrow_to_wide(address);
+ menudata.name = narrow_to_wide(playername);
+ menudata.port = narrow_to_wide(itos(port));
+ menudata.fancy_trees = g_settings.getBool("new_style_leaves");
+ menudata.smooth_lighting = g_settings.getBool("smooth_lighting");
+ menudata.creative_mode = g_settings.getBool("creative_mode");
+ menudata.enable_damage = g_settings.getBool("enable_damage");
+
+ GUIMainMenu *menu =
+ new GUIMainMenu(guienv, guiroot, -1,
+ &g_menumgr, &menudata, g_gamecallback);
+ menu->allowFocusRemoval(true);
+
+ if(error_message != L"")
{
- box = core::aabbox3d<f32>(
- cpf - v3f(BS/6, BS/3, BS/6),
- cpf + v3f(BS/6, BS/3, BS/6)
- );
+ dstream<<"WARNING: error_message = "
+ <<wide_to_narrow(error_message)<<std::endl;
+
+ GUIMessageMenu *menu2 =
+ new GUIMessageMenu(guienv, guiroot, -1,
+ &g_menumgr, error_message.c_str());
+ menu2->drop();
+ error_message = L"";
}
- if(distance < mindistance)
- {
- if(box.intersectsWithLine(shootline))
- {
- nodefound = true;
- nodepos = np;
- neighbourpos = np;
- mindistance = distance;
- nodehilightbox = box;
- }
- }
- }
- /*
- Regular blocks
- */
- else
- {
- for(u16 i=0; i<6; i++)
+ video::IVideoDriver* driver = device->getVideoDriver();
+
+ dstream<<"Created main menu"<<std::endl;
+
+ while(device->run() && kill == false)
{
- v3f dir_f = v3f(dirs[i].X,
- dirs[i].Y, dirs[i].Z);
- v3f centerpoint = npf + dir_f * BS/2;
- f32 distance =
- (centerpoint - camera_position).getLength();
-
- if(distance < mindistance)
- {
- core::CMatrix4<f32> m;
- m.buildRotateFromTo(v3f(0,0,1), dir_f);
-
- // This is the back face
- v3f corners[2] = {
- v3f(BS/2, BS/2, BS/2),
- v3f(-BS/2, -BS/2, BS/2+d)
- };
-
- for(u16 j=0; j<2; j++)
- {
- m.rotateVect(corners[j]);
- corners[j] += npf;
- }
-
- core::aabbox3d<f32> facebox(corners[0]);
- facebox.addInternalPoint(corners[1]);
-
- if(facebox.intersectsWithLine(shootline))
- {
- nodefound = true;
- nodepos = np;
- neighbourpos = np + dirs[i];
- mindistance = distance;
-
- //nodehilightbox = facebox;
-
- const float d = 0.502;
- core::aabbox3d<f32> nodebox
- (-BS*d, -BS*d, -BS*d, BS*d, BS*d, BS*d);
- v3f nodepos_f = intToFloat(nodepos, BS);
- nodebox.MinEdge += nodepos_f;
- nodebox.MaxEdge += nodepos_f;
- nodehilightbox = nodebox;
- }
- } // if distance < mindistance
- } // for dirs
- } // regular block
- } // for coords
-
- static float nodig_delay_counter = 0.0;
-
- if(nodefound)
- {
- static v3s16 nodepos_old(-32768,-32768,-32768);
+ if(menu->getStatus() == true)
+ break;
- static float dig_time = 0.0;
- static u16 dig_index = 0;
-
- // Visualize selection
+ //driver->beginScene(true, true, video::SColor(255,0,0,0));
+ driver->beginScene(true, true, video::SColor(255,128,128,128));
- hilightboxes.push_back(nodehilightbox);
+ drawMenuBackground(driver);
- // Handle digging
-
- if(g_input->getLeftReleased())
- {
- client.clearTempMod(nodepos);
- dig_time = 0.0;
- }
-
- if(nodig_delay_counter > 0.0)
- {
- nodig_delay_counter -= dtime;
- }
- else
- {
- if(nodepos != nodepos_old)
- {
- std::cout<<DTIME<<"Pointing at ("<<nodepos.X<<","
- <<nodepos.Y<<","<<nodepos.Z<<")"<<std::endl;
-
- if(nodepos_old != v3s16(-32768,-32768,-32768))
- {
- client.clearTempMod(nodepos_old);
- dig_time = 0.0;
- }
- }
-
- if(g_input->getLeftClicked() ||
- (g_input->getLeftState() && nodepos != nodepos_old))
- {
- dstream<<DTIME<<"Started digging"<<std::endl;
- client.groundAction(0, nodepos, neighbourpos, g_selected_item);
- }
- if(g_input->getLeftClicked())
- {
- client.setTempMod(nodepos, NodeMod(NODEMOD_CRACK, 0));
- }
- if(g_input->getLeftState())
- {
- MapNode n = client.getNode(nodepos);
-
- // Get tool name. Default is "" = bare hands
- std::string toolname = "";
- InventoryList *mlist = local_inventory.getList("main");
- if(mlist != NULL)
- {
- InventoryItem *item = mlist->getItem(g_selected_item);
- if(item && (std::string)item->getName() == "ToolItem")
- {
- ToolItem *titem = (ToolItem*)item;
- toolname = titem->getToolName();
- }
- }
-
- // Get digging properties for material and tool
- u8 material = n.d;
- DiggingProperties prop =
- getDiggingProperties(material, toolname);
+ guienv->drawAll();
- float dig_time_complete = 0.0;
-
- if(prop.diggable == false)
- {
- /*dstream<<"Material "<<(int)material
- <<" not diggable with \""
- <<toolname<<"\""<<std::endl;*/
- // I guess nobody will wait for this long
- dig_time_complete = 10000000.0;
- }
- else
- {
- dig_time_complete = prop.time;
- }
+ driver->endScene();
- if(dig_time_complete >= 0.001)
- {
- dig_index = (u16)((float)CRACK_ANIMATION_LENGTH
- * dig_time/dig_time_complete);
- }
- // This is for torches
- else
- {
- dig_index = CRACK_ANIMATION_LENGTH;
- }
-
- if(dig_index < CRACK_ANIMATION_LENGTH)
- {
- //TimeTaker timer("client.setTempMod");
- //dstream<<"dig_index="<<dig_index<<std::endl;
- client.setTempMod(nodepos, NodeMod(NODEMOD_CRACK, dig_index));
- }
- else
- {
- dstream<<DTIME<<"Digging completed"<<std::endl;
- client.groundAction(3, nodepos, neighbourpos, g_selected_item);
- client.clearTempMod(nodepos);
- client.removeNode(nodepos);
-
- dig_time = 0;
-
- nodig_delay_counter = dig_time_complete
- / (float)CRACK_ANIMATION_LENGTH;
-
- // We don't want a corresponding delay to
- // very time consuming nodes
- if(nodig_delay_counter > 0.5)
- {
- nodig_delay_counter = 0.5;
- }
- // We want a slight delay to very little
- // time consuming nodes
- float mindelay = 0.15;
- if(nodig_delay_counter < mindelay)
- {
- nodig_delay_counter = mindelay;
- }
- }
-
- dig_time += dtime;
+ // On some computers framerate doesn't seem to be
+ // automatically limited
+ sleep_ms(25);
}
- }
-
- if(g_input->getRightClicked())
- {
- std::cout<<DTIME<<"Ground right-clicked"<<std::endl;
- client.groundAction(1, nodepos, neighbourpos, g_selected_item);
- }
-
- nodepos_old = nodepos;
- }
- else{
- }
-
- } // selected_object == NULL
-
- g_input->resetLeftClicked();
- g_input->resetRightClicked();
-
- if(g_input->getLeftReleased())
- {
- std::cout<<DTIME<<"Left button released (stopped digging)"
- <<std::endl;
- client.groundAction(2, v3s16(0,0,0), v3s16(0,0,0), 0);
- }
- if(g_input->getRightReleased())
- {
- //std::cout<<DTIME<<"Right released"<<std::endl;
- // Nothing here
- }
-
- g_input->resetLeftReleased();
- g_input->resetRightReleased();
-
- /*
- Calculate stuff for drawing
- */
-
- camera->setAspectRatio((f32)screensize.X / (f32)screensize.Y);
-
- u32 daynight_ratio = client.getDayNightRatio();
- u8 l = decode_light((daynight_ratio * LIGHT_SUN) / 1000);
- video::SColor bgcolor = video::SColor(
- 255,
- skycolor.getRed() * l / 255,
- skycolor.getGreen() * l / 255,
- skycolor.getBlue() * l / 255);
-
- /*
- Fog
- */
-
- if(g_settings.getBool("enable_fog") == true)
- {
- //f32 range = draw_control.wanted_range * BS + MAP_BLOCKSIZE/2*BS;
- f32 range = draw_control.wanted_range * BS + 0.8*MAP_BLOCKSIZE*BS;
- //f32 range = draw_control.wanted_range * BS + 0.0*MAP_BLOCKSIZE*BS;
- if(draw_control.range_all)
- range = 100000*BS;
-
- driver->setFog(
- bgcolor,
- video::EFT_FOG_LINEAR,
- range*0.6,
- range*1.0,
- 0.01,
- false, // pixel fog
- false // range fog
- );
- }
- else
- {
- driver->setFog(
- bgcolor,
- video::EFT_FOG_LINEAR,
- 100000*BS,
- 110000*BS,
- 0.01,
- false, // pixel fog
- false // range fog
- );
- }
-
-
- /*
- Update gui stuff (0ms)
- */
+
+ // Break out of menu-game loop to shut down cleanly
+ if(device->run() == false || kill == true)
+ break;
+
+ dstream<<"Dropping main menu"<<std::endl;
- //TimeTaker guiupdatetimer("Gui updating");
-
- {
- static float drawtime_avg = 0;
- drawtime_avg = drawtime_avg * 0.95 + (float)drawtime*0.05;
- static float beginscenetime_avg = 0;
- beginscenetime_avg = beginscenetime_avg * 0.95 + (float)beginscenetime*0.05;
- static float scenetime_avg = 0;
- scenetime_avg = scenetime_avg * 0.95 + (float)scenetime*0.05;
- static float endscenetime_avg = 0;
- endscenetime_avg = endscenetime_avg * 0.95 + (float)endscenetime*0.05;
-
- char temptext[300];
- snprintf(temptext, 300, "Minetest-c55 ("
- "F: item=%i"
- ", R: range_all=%i"
- ")"
- " drawtime=%.0f, beginscenetime=%.0f"
- ", scenetime=%.0f, endscenetime=%.0f",
- g_selected_item,
- draw_control.range_all,
- drawtime_avg,
- beginscenetime_avg,
- scenetime_avg,
- endscenetime_avg
- );
-
- guitext->setText(narrow_to_wide(temptext).c_str());
- }
-
- {
- char temptext[300];
- snprintf(temptext, 300,
- "(% .1f, % .1f, % .1f)"
- " (% .3f < btime_jitter < % .3f"
- ", dtime_jitter = % .1f %%"
- ", v_range = %.1f)",
- player_position.X/BS,
- player_position.Y/BS,
- player_position.Z/BS,
- busytime_jitter1_min_sample,
- busytime_jitter1_max_sample,
- dtime_jitter1_max_fraction * 100.0,
- draw_control.wanted_range
- );
-
- guitext2->setText(narrow_to_wide(temptext).c_str());
- }
-
- {
- guitext_info->setText(infotext.c_str());
- }
-
- /*
- Get chat messages from client
- */
- {
- // Get new messages
- std::wstring message;
- while(client.getChatMessage(message))
- {
- chat_lines.push_back(ChatLine(message));
- /*if(chat_lines.size() > 6)
- {
- core::list<ChatLine>::Iterator
- i = chat_lines.begin();
- chat_lines.erase(i);
- }*/
- }
- // Append them to form the whole static text and throw
- // it to the gui element
- std::wstring whole;
- // This will correspond to the line number counted from
- // top to bottom, from size-1 to 0
- s16 line_number = chat_lines.size();
- // Count of messages to be removed from the top
- u16 to_be_removed_count = 0;
- for(core::list<ChatLine>::Iterator
- i = chat_lines.begin();
- i != chat_lines.end(); i++)
- {
- // After this, line number is valid for this loop
- line_number--;
- // Increment age
- (*i).age += dtime;
- /*
- This results in a maximum age of 60*6 to the
- lowermost line and a maximum of 6 lines
- */
- float allowed_age = (6-line_number) * 60.0;
-
- if((*i).age > allowed_age)
+ menu->drop();
+
+ // Delete map if requested
+ if(menudata.delete_map)
{
- to_be_removed_count++;
+ bool r = fs::RecursiveDeleteContent(map_dir);
+ if(r == false)
+ error_message = L"Delete failed";
continue;
}
- whole += (*i).text + L'\n';
- }
- for(u16 i=0; i<to_be_removed_count; i++)
- {
- core::list<ChatLine>::Iterator
- it = chat_lines.begin();
- chat_lines.erase(it);
- }
- guitext_chat->setText(whole.c_str());
-
- // Update gui element size and position
-
- /*core::rect<s32> rect(
- 10,
- screensize.Y - guitext_chat_pad_bottom
- - text_height*chat_lines.size(),
- screensize.X - 10,
- screensize.Y - guitext_chat_pad_bottom
- );*/
- core::rect<s32> rect(
- 10,
- 50,
- screensize.X - 10,
- 50 + text_height*chat_lines.size()
- );
-
- guitext_chat->setRelativePosition(rect);
-
- if(chat_lines.size() == 0)
- guitext_chat->setVisible(false);
- else
- guitext_chat->setVisible(true);
- }
-
- /*
- Inventory
- */
-
- static u16 old_selected_item = 65535;
- if(client.getLocalInventoryUpdated()
- || g_selected_item != old_selected_item)
- {
- old_selected_item = g_selected_item;
- //std::cout<<"Updating local inventory"<<std::endl;
- client.getLocalInventory(local_inventory);
- /*quick_inventory->setSelection(g_selected_item);
- quick_inventory->update();*/
- }
-
- /*
- Send actions returned by the inventory menu
- */
- while(inventory_action_queue.size() != 0)
- {
- InventoryAction *a = inventory_action_queue.pop_front();
-
- client.sendInventoryAction(a);
- // Eat it
- delete a;
- }
- /*
- Drawing begins
- */
+ playername = wide_to_narrow(menudata.name);
- TimeTaker drawtimer("Drawing");
+ password = translatePassword(playername, menudata.password);
-
- {
- TimeTaker timer("beginScene");
- driver->beginScene(true, true, bgcolor);
- //driver->beginScene(false, true, bgcolor);
- beginscenetime = timer.stop(true);
- }
-
- //timer3.stop();
-
- //std::cout<<DTIME<<"smgr->drawAll()"<<std::endl;
-
- {
- TimeTaker timer("smgr");
- smgr->drawAll();
- scenetime = timer.stop(true);
- }
-
- {
- //TimeTaker timer9("auxiliary drawings");
- // 0ms
-
- //timer9.stop();
- //TimeTaker //timer10("//timer10");
-
- video::SMaterial m;
- //m.Thickness = 10;
- m.Thickness = 3;
- m.Lighting = false;
- driver->setMaterial(m);
-
- driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
-
- for(core::list< core::aabbox3d<f32> >::Iterator i=hilightboxes.begin();
- i != hilightboxes.end(); i++)
- {
- /*std::cout<<"hilightbox min="
- <<"("<<i->MinEdge.X<<","<<i->MinEdge.Y<<","<<i->MinEdge.Z<<")"
- <<" max="
- <<"("<<i->MaxEdge.X<<","<<i->MaxEdge.Y<<","<<i->MaxEdge.Z<<")"
- <<std::endl;*/
- driver->draw3DBox(*i, video::SColor(255,0,0,0));
- }
-
- /*
- Draw crosshair
- */
- driver->draw2DLine(displaycenter - core::vector2d<s32>(10,0),
- displaycenter + core::vector2d<s32>(10,0),
- video::SColor(255,255,255,255));
- driver->draw2DLine(displaycenter - core::vector2d<s32>(0,10),
- displaycenter + core::vector2d<s32>(0,10),
- video::SColor(255,255,255,255));
+ address = wide_to_narrow(menudata.address);
+ int newport = stoi(wide_to_narrow(menudata.port));
+ if(newport != 0)
+ port = newport;
+ g_settings.set("new_style_leaves", itos(menudata.fancy_trees));
+ g_settings.set("smooth_lighting", itos(menudata.smooth_lighting));
+ g_settings.set("creative_mode", itos(menudata.creative_mode));
+ g_settings.set("enable_damage", itos(menudata.enable_damage));
+
+ // NOTE: These are now checked server side; no need to do it
+ // here, so let's not do it here.
+ /*// Check for valid parameters, restart menu if invalid.
+ if(playername == "")
+ {
+ error_message = L"Name required.";
+ continue;
+ }
+ // Check that name has only valid chars
+ if(string_allowed(playername, PLAYERNAME_ALLOWED_CHARS)==false)
+ {
+ error_message = L"Characters allowed: "
+ +narrow_to_wide(PLAYERNAME_ALLOWED_CHARS);
+ continue;
+ }*/
- /*
- Frametime log
- */
- if(g_settings.getBool("frametime_graph") == true)
- {
- s32 x = 10;
- for(core::list<float>::Iterator
- i = frametime_log.begin();
- i != frametime_log.end();
- i++)
- {
- driver->draw2DLine(v2s32(x,50),
- v2s32(x,50+(*i)*1000),
- video::SColor(255,255,255,255));
- x++;
+ // Save settings
+ g_settings.set("name", playername);
+ g_settings.set("address", address);
+ g_settings.set("port", itos(port));
+ // Update configuration file
+ if(configpath != "")
+ g_settings.updateConfigFile(configpath.c_str());
+
+ // Continue to game
+ break;
}
- }
-
- } // timer
-
- //timer10.stop();
- //TimeTaker //timer11("//timer11");
+
+ // Break out of menu-game loop to shut down cleanly
+ if(device->run() == false)
+ break;
+
+ // Initialize mapnode again to enable changed graphics settings
+ init_mapnode();
- /*
- Draw gui
- */
- // 0-1ms
- guienv->drawAll();
+ /*
+ Run game
+ */
+ the_game(
+ kill,
+ random_input,
+ input,
+ device,
+ font,
+ map_dir,
+ playername,
+ password,
+ address,
+ port,
+ error_message
+ );
- /*
- Draw hotbar
- */
+ } //try
+ catch(con::PeerNotFoundException &e)
{
- draw_hotbar(driver, font, v2s32(displaycenter.X, screensize.Y),
- hotbar_imagesize, hotbar_itemcount, &local_inventory);
+ dstream<<DTIME<<"Connection error (timed out?)"<<std::endl;
+ error_message = L"Connection error (timed out?)";
}
-
- // End drawing
+ catch(SocketException &e)
{
- TimeTaker timer("endScene");
- driver->endScene();
- endscenetime = timer.stop(true);
+ dstream<<DTIME<<"Socket error (port already in use?)"<<std::endl;
+ error_message = L"Socket error (port already in use?)";
}
-
- drawtime = drawtimer.stop(true);
-
- /*
- Drawing ends
- */
-
- static s16 lastFPS = 0;
- //u16 fps = driver->getFPS();
- u16 fps = (1.0/dtime_avg1);
-
- if (lastFPS != fps)
+#ifdef NDEBUG
+ catch(std::exception &e)
{
- core::stringw str = L"Minetest [";
- str += driver->getName();
- str += "] FPS:";
- str += fps;
-
- device->setWindowCaption(str.c_str());
- lastFPS = fps;
+ std::string narrow_message = "Some exception, what()=\"";
+ narrow_message += e.what();
+ narrow_message += "\"";
+ dstream<<DTIME<<narrow_message<<std::endl;
+ error_message = narrow_to_wide(narrow_message);
}
-
- /*}
- else
- device->yield();*/
- }
-
- //delete quick_inventory;
-
- /*
- Disable texture fetches and other stuff that is queued
- to be processed by the main loop.
-
- This has to be done before client goes out of scope.
- */
- g_irrlicht->Shutdown(true);
-
- } // client and server are deleted at this point
-
- } //try
- catch(con::PeerNotFoundException &e)
- {
- dstream<<DTIME<<"Connection timed out."<<std::endl;
- error_message = L"Connection timed out.";
- }
+#endif
} // Menu-game loop
- delete g_input;
+ delete input;
/*
In the end, delete the Irrlicht device.
*/
device->drop();
- /*
- Update configuration file
- */
- /*if(configpath != "")
- {
- g_settings.updateConfigFile(configpath.c_str());
- }*/
-
END_DEBUG_EXCEPTION_HANDLER
debugstreams_deinit();
diff --git a/src/main.h b/src/main.h
index b951ab5b0..450525c26 100644
--- a/src/main.h
+++ b/src/main.h
@@ -24,16 +24,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "utility.h"
extern Settings g_settings;
-// A thread safe wrapper to irrlicht
-// On a server build, this is always NULL.
-// TODO: Remove this
-#include "irrlichtwrapper.h"
-extern IrrlichtWrapper *g_irrlicht;
-
// This makes and maps textures
#include "tile.h"
extern ITextureSource *g_texturesource;
+// Global profiler
+#include "profiler.h"
+extern Profiler g_profiler;
+
// Debug streams
#include <fstream>
@@ -52,5 +50,105 @@ extern std::ostream *derr_server_ptr;
#define dout_server (*dout_server_ptr)
#define derr_server (*derr_server_ptr)
+/*
+ All kinds of stuff that needs to be exposed from main.cpp
+*/
+
+#include "modalMenu.h"
+#include "guiPauseMenu.h" //For IGameCallback
+
+extern gui::IGUIEnvironment* guienv;
+extern gui::IGUIStaticText *guiroot;
+
+// Handler for the modal menus
+
+class MainMenuManager : public IMenuManager
+{
+public:
+ virtual void createdMenu(GUIModalMenu *menu)
+ {
+ for(core::list<GUIModalMenu*>::Iterator
+ i = m_stack.begin();
+ i != m_stack.end(); i++)
+ {
+ assert(*i != menu);
+ }
+
+ if(m_stack.size() != 0)
+ (*m_stack.getLast())->setVisible(false);
+ m_stack.push_back(menu);
+ }
+
+ virtual void deletingMenu(GUIModalMenu *menu)
+ {
+ // Remove all entries if there are duplicates
+ bool removed_entry;
+ do{
+ removed_entry = false;
+ for(core::list<GUIModalMenu*>::Iterator
+ i = m_stack.begin();
+ i != m_stack.end(); i++)
+ {
+ if(*i == menu)
+ {
+ m_stack.erase(i);
+ removed_entry = true;
+ break;
+ }
+ }
+ }while(removed_entry);
+
+ /*core::list<GUIModalMenu*>::Iterator i = m_stack.getLast();
+ assert(*i == menu);
+ m_stack.erase(i);*/
+
+ if(m_stack.size() != 0)
+ (*m_stack.getLast())->setVisible(true);
+ }
+
+ u32 menuCount()
+ {
+ return m_stack.size();
+ }
+
+ core::list<GUIModalMenu*> m_stack;
+};
+
+extern MainMenuManager g_menumgr;
+
+extern bool noMenuActive();
+
+class MainGameCallback : public IGameCallback
+{
+public:
+ MainGameCallback(IrrlichtDevice *a_device):
+ disconnect_requested(false),
+ changepassword_requested(false),
+ device(a_device)
+ {
+ }
+
+ virtual void exitToOS()
+ {
+ device->closeDevice();
+ }
+
+ virtual void disconnect()
+ {
+ disconnect_requested = true;
+ }
+
+ virtual void changePassword()
+ {
+ changepassword_requested = true;
+ }
+
+ bool disconnect_requested;
+ bool changepassword_requested;
+ IrrlichtDevice *device;
+};
+
+extern MainGameCallback *g_gamecallback;
+
#endif
diff --git a/src/map.cpp b/src/map.cpp
index 8e71212b0..f9809e9fd 100644
--- a/src/map.cpp
+++ b/src/map.cpp
@@ -1,6 +1,6 @@
/*
Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "porting.h"
#include "mineral.h"
#include "noise.h"
+#include "serverobject.h"
/*
Map
@@ -36,8 +37,8 @@ Map::Map(std::ostream &dout):
m_dout(dout),
m_sector_cache(NULL)
{
- m_sector_mutex.Init();
- assert(m_sector_mutex.IsInitialized());
+ /*m_sector_mutex.Init();
+ assert(m_sector_mutex.IsInitialized());*/
}
Map::~Map()
@@ -103,7 +104,7 @@ MapSector * Map::getSectorNoGenerateNoExNoLock(v2s16 p)
MapSector * Map::getSectorNoGenerateNoEx(v2s16 p)
{
- JMutexAutoLock lock(m_sector_mutex);
+ //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
return getSectorNoGenerateNoExNoLock(p);
}
@@ -606,13 +607,13 @@ s16 Map::propagateSunlight(v3s16 start,
}
else
{
- // Turn mud into grass
+ /*// Turn mud into grass
if(n.d == CONTENT_MUD)
{
n.d = CONTENT_GRASS;
block->setNode(relpos, n);
modified_blocks.insert(blockpos, block);
- }
+ }*/
// Sunlight goes no further
break;
@@ -837,9 +838,6 @@ void Map::updateLighting(core::map<v3s16, MapBlock*> & a_blocks,
}
/*
- This is called after changing a node from transparent to opaque.
- The lighting value of the node should be left as-is after changing
- other values. This sets the lighting value to 0.
*/
void Map::addNodeAndUpdate(v3s16 p, MapNode n,
core::map<v3s16, MapBlock*> &modified_blocks)
@@ -876,12 +874,12 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
catch(InvalidPositionException &e)
{
}
-
+
+#if 1
/*
- If the new node doesn't propagate sunlight and there is
- grass below, change it to mud
+ If the new node is solid and there is grass below, change it to mud
*/
- if(content_features(n.d).sunlight_propagates == false)
+ if(content_features(n.d).walkable == true)
{
try{
MapNode bottomnode = getNode(bottompos);
@@ -897,7 +895,9 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
{
}
}
+#endif
+#if 0
/*
If the new node is mud and it is under sunlight, change it
to grass
@@ -906,6 +906,7 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
{
n.d = CONTENT_GRASS;
}
+#endif
/*
Remove all light that has come out of this node
@@ -941,19 +942,39 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
}
/*
+ If node lets sunlight through and is under sunlight, it has
+ sunlight too.
+ */
+ if(node_under_sunlight && content_features(n.d).sunlight_propagates)
+ {
+ n.setLight(LIGHTBANK_DAY, LIGHT_SUN);
+ }
+
+ /*
Set the node on the map
*/
setNode(p, n);
+
+ /*
+ Add intial metadata
+ */
+
+ NodeMetadata *meta_proto = content_features(n.d).initial_metadata;
+ if(meta_proto)
+ {
+ NodeMetadata *meta = meta_proto->clone();
+ setNodeMetadata(p, meta);
+ }
/*
- If node is under sunlight, take all sunlighted nodes under
- it and clear light from them and from where the light has
- been spread.
+ If node is under sunlight and doesn't let sunlight through,
+ take all sunlighted nodes under it and clear light from them
+ and from where the light has been spread.
TODO: This could be optimized by mass-unlighting instead
of looping
*/
- if(node_under_sunlight)
+ if(node_under_sunlight && !content_features(n.d).sunlight_propagates)
{
s16 y = p.Y - 1;
for(;; y--){
@@ -981,7 +1002,7 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
break;
}
}
-
+
for(s32 i=0; i<2; i++)
{
enum LightBank bank = banks[i];
@@ -1085,6 +1106,12 @@ void Map::removeNodeAndUpdate(v3s16 p,
}
/*
+ Remove node metadata
+ */
+
+ removeNodeMetadata(p);
+
+ /*
Remove the node.
This also clears the lighting.
*/
@@ -1320,7 +1347,7 @@ bool Map::dayNightDiffed(v3s16 blockpos)
*/
void Map::timerUpdate(float dtime)
{
- JMutexAutoLock lock(m_sector_mutex);
+ //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
core::map<v2s16, MapSector*>::Iterator si;
@@ -1370,7 +1397,7 @@ void Map::deleteSectors(core::list<v2s16> &list, bool only_blocks)
u32 Map::deleteUnusedSectors(float timeout, bool only_blocks,
core::list<v3s16> *deleted_blocks)
{
- JMutexAutoLock lock(m_sector_mutex);
+ //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
core::list<v2s16> sector_deletion_queue;
core::map<v2s16, MapSector*>::Iterator i = m_sectors.getIterator();
@@ -1687,14 +1714,88 @@ void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
//dstream<<"Map::transformLiquids(): loopcount="<<loopcount<<std::endl;
}
+NodeMetadata* Map::getNodeMetadata(v3s16 p)
+{
+ v3s16 blockpos = getNodeBlockPos(p);
+ v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
+ MapBlock *block = getBlockNoCreateNoEx(blockpos);
+ if(block == NULL)
+ {
+ dstream<<"WARNING: Map::setNodeMetadata(): Block not found"
+ <<std::endl;
+ return NULL;
+ }
+ NodeMetadata *meta = block->m_node_metadata.get(p_rel);
+ return meta;
+}
+
+void Map::setNodeMetadata(v3s16 p, NodeMetadata *meta)
+{
+ v3s16 blockpos = getNodeBlockPos(p);
+ v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
+ MapBlock *block = getBlockNoCreateNoEx(blockpos);
+ if(block == NULL)
+ {
+ dstream<<"WARNING: Map::setNodeMetadata(): Block not found"
+ <<std::endl;
+ return;
+ }
+ block->m_node_metadata.set(p_rel, meta);
+}
+
+void Map::removeNodeMetadata(v3s16 p)
+{
+ v3s16 blockpos = getNodeBlockPos(p);
+ v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
+ MapBlock *block = getBlockNoCreateNoEx(blockpos);
+ if(block == NULL)
+ {
+ dstream<<"WARNING: Map::removeNodeMetadata(): Block not found"
+ <<std::endl;
+ return;
+ }
+ block->m_node_metadata.remove(p_rel);
+}
+
+void Map::nodeMetadataStep(float dtime,
+ core::map<v3s16, MapBlock*> &changed_blocks)
+{
+ /*
+ NOTE:
+ Currently there is no way to ensure that all the necessary
+ blocks are loaded when this is run. (They might get unloaded)
+ NOTE: ^- Actually, that might not be so. In a quick test it
+ reloaded a block with a furnace when I walked back to it from
+ a distance.
+ */
+ core::map<v2s16, MapSector*>::Iterator si;
+ si = m_sectors.getIterator();
+ for(; si.atEnd() == false; si++)
+ {
+ MapSector *sector = si.getNode()->getValue();
+ core::list< MapBlock * > sectorblocks;
+ sector->getBlocks(sectorblocks);
+ core::list< MapBlock * >::Iterator i;
+ for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++)
+ {
+ MapBlock *block = *i;
+ bool changed = block->m_node_metadata.step(dtime);
+ if(changed)
+ changed_blocks[block->getPos()] = block;
+ }
+ }
+}
+
/*
ServerMap
*/
ServerMap::ServerMap(std::string savedir):
Map(dout_server),
- m_seed(0)
+ m_seed(0),
+ m_map_metadata_changed(true)
{
+ dstream<<__FUNCTION_NAME<<std::endl;
//m_chunksize = 64;
//m_chunksize = 16; // Too slow
@@ -1702,7 +1803,6 @@ ServerMap::ServerMap(std::string savedir):
//m_chunksize = 4;
//m_chunksize = 2;
- // TODO: Save to and load from a file
m_seed = (((u64)(myrand()%0xffff)<<0)
+ ((u64)(myrand()%0xffff)<<16)
+ ((u64)(myrand()%0xffff)<<32)
@@ -1736,11 +1836,19 @@ ServerMap::ServerMap(std::string savedir):
}
else
{
- // Load map metadata (seed, chunksize)
- loadMapMeta();
-
- // Load chunk metadata
- loadChunkMeta();
+ try{
+ // Load map metadata (seed, chunksize)
+ loadMapMeta();
+
+ // Load chunk metadata
+ loadChunkMeta();
+ }
+ catch(FileNotGoodException &e){
+ dstream<<DTIME<<"WARNING: Server: Could not load "
+ <<"metafile(s). Disabling chunk-based "
+ <<"generation."<<std::endl;
+ m_chunksize = 0;
+ }
/*// Load sector (0,0) and throw and exception on fail
if(loadSectorFull(v2s16(0,0)) == false)
@@ -1785,6 +1893,8 @@ ServerMap::ServerMap(std::string savedir):
ServerMap::~ServerMap()
{
+ dstream<<__FUNCTION_NAME<<std::endl;
+
try
{
if(m_map_saving_enabled)
@@ -1958,7 +2068,7 @@ double base_rock_level_2d(u64 seed, v2s16 p)
{
// The base ground level
double base = (double)WATER_LEVEL - (double)AVERAGE_MUD_AMOUNT
- + 25. * noise2d_perlin(
+ + 20. * noise2d_perlin(
0.5+(float)p.X/500., 0.5+(float)p.Y/500.,
(seed>>32)+654879876, 6, 0.6);
@@ -1970,7 +2080,7 @@ double base_rock_level_2d(u64 seed, v2s16 p)
base = base2;*/
#if 1
// Higher ground level
- double higher = (double)WATER_LEVEL + 25. + 45. * noise2d_perlin(
+ double higher = (double)WATER_LEVEL + 25. + 35. * noise2d_perlin(
0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
seed+85039, 5, 0.69);
//higher = 30; // For debugging
@@ -2011,184 +2121,240 @@ double base_rock_level_2d(u64 seed, v2s16 p)
return h;
}
-#define VMANIP_FLAG_DUNGEON VOXELFLAG_CHECKED1
+double get_mud_add_amount(u64 seed, v2s16 p)
+{
+ return ((float)AVERAGE_MUD_AMOUNT + 3.0 * noise2d_perlin(
+ 0.5+(float)p.X/200, 0.5+(float)p.Y/200,
+ seed+91013, 3, 0.55));
+}
/*
- This is the main map generation method
+ Adds random objects to block, depending on the content of the block
*/
-
-MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
- core::map<v3s16, MapBlock*> &changed_blocks,
- bool force)
+void addRandomObjects(MapBlock *block)
{
- DSTACK(__FUNCTION_NAME);
-
- /*
- Don't generate if already fully generated
- */
- if(force == false)
+ for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
+ for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
{
- MapChunk *chunk = getChunk(chunkpos);
- if(chunk != NULL && chunk->getGenLevel() == GENERATED_FULLY)
+ bool last_node_walkable = false;
+ for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
{
- dstream<<"generateChunkRaw(): Chunk "
- <<"("<<chunkpos.X<<","<<chunkpos.Y<<")"
- <<" already generated"<<std::endl;
- return chunk;
+ v3s16 p(x0,y0,z0);
+ MapNode n = block->getNodeNoEx(p);
+ if(n.d == CONTENT_IGNORE)
+ continue;
+ if(content_features(n.d).liquid_type != LIQUID_NONE)
+ continue;
+ if(content_features(n.d).walkable)
+ {
+ last_node_walkable = true;
+ continue;
+ }
+ if(last_node_walkable)
+ {
+ // If block contains light information
+ if(content_features(n.d).param_type == CPT_LIGHT)
+ {
+ if(n.getLight(LIGHTBANK_DAY) <= 3)
+ {
+ if(myrand() % 300 == 0)
+ {
+ v3f pos_f = intToFloat(p+block->getPosRelative(), BS);
+ pos_f.Y -= BS*0.4;
+ ServerActiveObject *obj = new RatSAO(NULL, 0, pos_f);
+ std::string data = obj->getStaticData();
+ StaticObject s_obj(obj->getType(),
+ obj->getBasePosition(), data);
+ // Add some
+ block->m_static_objects.insert(0, s_obj);
+ block->m_static_objects.insert(0, s_obj);
+ block->m_static_objects.insert(0, s_obj);
+ block->m_static_objects.insert(0, s_obj);
+ block->m_static_objects.insert(0, s_obj);
+ block->m_static_objects.insert(0, s_obj);
+ delete obj;
+ }
+ if(myrand() % 300 == 0)
+ {
+ v3f pos_f = intToFloat(p+block->getPosRelative(), BS);
+ pos_f.Y -= BS*0.4;
+ ServerActiveObject *obj = new Oerkki1SAO(NULL,0,pos_f);
+ std::string data = obj->getStaticData();
+ StaticObject s_obj(obj->getType(),
+ obj->getBasePosition(), data);
+ // Add one
+ block->m_static_objects.insert(0, s_obj);
+ delete obj;
+ }
+ }
+ }
+ }
+ last_node_walkable = false;
}
}
+ block->setChangedFlag();
+}
- dstream<<"generateChunkRaw(): Generating chunk "
- <<"("<<chunkpos.X<<","<<chunkpos.Y<<")"
- <<std::endl;
-
- TimeTaker timer("generateChunkRaw()");
-
- // The distance how far into the neighbors the generator is allowed to go.
- s16 max_spread_amount_sectors = 2;
- assert(max_spread_amount_sectors <= m_chunksize);
- s16 max_spread_amount = max_spread_amount_sectors * MAP_BLOCKSIZE;
-
- // Minimum amount of space left on sides for mud to fall in
- //s16 min_mud_fall_space = 2;
-
- // Maximum diameter of stone obstacles in X and Z
- /*s16 stone_obstacle_max_size = (max_spread_amount-min_mud_fall_space)*2;
- assert(stone_obstacle_max_size/2 <= max_spread_amount-min_mud_fall_space);*/
-
- s16 y_blocks_min = -4;
- s16 y_blocks_max = 3;
- s16 h_blocks = y_blocks_max - y_blocks_min + 1;
- s16 y_nodes_min = y_blocks_min * MAP_BLOCKSIZE;
- s16 y_nodes_max = y_blocks_max * MAP_BLOCKSIZE + MAP_BLOCKSIZE - 1;
-
- v2s16 sectorpos_base = chunk_to_sector(chunkpos);
- s16 sectorpos_base_size = m_chunksize;
+#define VMANIP_FLAG_DUNGEON VOXELFLAG_CHECKED1
- /*v2s16 sectorpos_bigbase = chunk_to_sector(chunkpos - v2s16(1,1));
- s16 sectorpos_bigbase_size = m_chunksize * 3;*/
- v2s16 sectorpos_bigbase =
- sectorpos_base - v2s16(1,1) * max_spread_amount_sectors;
- s16 sectorpos_bigbase_size =
- sectorpos_base_size + 2 * max_spread_amount_sectors;
+/*
+ This is the main map generation method
+*/
+void makeChunk(ChunkMakeData *data)
+{
+ if(data->no_op)
+ return;
+
+ s16 y_nodes_min = data->y_blocks_min * MAP_BLOCKSIZE;
+ s16 y_nodes_max = data->y_blocks_max * MAP_BLOCKSIZE + MAP_BLOCKSIZE - 1;
+ s16 h_blocks = data->y_blocks_max - data->y_blocks_min + 1;
+ u32 relative_volume = (u32)data->sectorpos_base_size*MAP_BLOCKSIZE
+ *(u32)data->sectorpos_base_size*MAP_BLOCKSIZE
+ *(u32)h_blocks*MAP_BLOCKSIZE;
v3s16 bigarea_blocks_min(
- sectorpos_bigbase.X,
- y_blocks_min,
- sectorpos_bigbase.Y
+ data->sectorpos_bigbase.X,
+ data->y_blocks_min,
+ data->sectorpos_bigbase.Y
);
-
v3s16 bigarea_blocks_max(
- sectorpos_bigbase.X + sectorpos_bigbase_size - 1,
- y_blocks_max,
- sectorpos_bigbase.Y + sectorpos_bigbase_size - 1
+ data->sectorpos_bigbase.X + data->sectorpos_bigbase_size - 1,
+ data->y_blocks_max,
+ data->sectorpos_bigbase.Y + data->sectorpos_bigbase_size - 1
);
-
- // Relative values to control amount of stuff in one chunk
- /*u32 relative_area = (u32)sectorpos_base_size*MAP_BLOCKSIZE
- *(u32)sectorpos_base_size*MAP_BLOCKSIZE;*/
- u32 relative_volume = (u32)sectorpos_base_size*MAP_BLOCKSIZE
- *(u32)sectorpos_base_size*MAP_BLOCKSIZE
- *(u32)h_blocks*MAP_BLOCKSIZE;
-
- /*
- The limiting edges of the lighting update, inclusive.
- */
- s16 lighting_min_d = 0-max_spread_amount;
- s16 lighting_max_d = sectorpos_base_size*MAP_BLOCKSIZE+max_spread_amount-1;
-
- /*
- Create the whole area of this and the neighboring chunks
- */
- {
- TimeTaker timer("generateChunkRaw() create area");
-
- for(s16 x=0; x<sectorpos_bigbase_size; x++)
- for(s16 z=0; z<sectorpos_bigbase_size; z++)
- {
- v2s16 sectorpos = sectorpos_bigbase + v2s16(x,z);
- ServerMapSector *sector = createSector(sectorpos);
- assert(sector);
-
- for(s16 y=y_blocks_min; y<=y_blocks_max; y++)
- {
- v3s16 blockpos(sectorpos.X, y, sectorpos.Y);
- MapBlock *block = createBlock(blockpos);
-
- // Lighting won't be calculated
- //block->setLightingExpired(true);
- // Lighting will be calculated
- block->setLightingExpired(false);
-
- /*
- Block gets sunlight if this is true.
-
- This should be set to true when the top side of a block
- is completely exposed to the sky.
-
- Actually this doesn't matter now because the
- initial lighting is done here.
- */
- block->setIsUnderground(y != y_blocks_max);
- }
- }
- }
-
- /*
- Now we have a big empty area.
-
- Make a ManualMapVoxelManipulator that contains this and the
- neighboring chunks
- */
-
- ManualMapVoxelManipulator vmanip(this);
- // Add the area we just generated
- {
- TimeTaker timer("generateChunkRaw() initialEmerge");
- vmanip.initialEmerge(bigarea_blocks_min, bigarea_blocks_max);
- }
+ s16 lighting_min_d = 0-data->max_spread_amount;
+ s16 lighting_max_d = data->sectorpos_base_size*MAP_BLOCKSIZE
+ + data->max_spread_amount-1;
// Clear all flags
- vmanip.clearFlag(0xff);
+ data->vmanip.clearFlag(0xff);
- TimeTaker timer_generate("generateChunkRaw() generate");
+ TimeTaker timer_generate("makeChunk() generate");
// Maximum height of the stone surface and obstacles.
- // This is used to disable dungeon generation from going too high.
+ // This is used to disable cave generation from going too high.
s16 stone_surface_max_y = 0;
/*
Generate general ground level to full area
*/
-
{
// 22ms @cs=8
- //TimeTaker timer1("ground level");
+ TimeTaker timer1("Generating ground level");
- for(s16 x=0; x<sectorpos_bigbase_size*MAP_BLOCKSIZE; x++)
- for(s16 z=0; z<sectorpos_bigbase_size*MAP_BLOCKSIZE; z++)
+#if 0
+ NoiseBuffer noisebuf1;
+ //NoiseBuffer noisebuf2;
+ {
+ v3f minpos_f(
+ data->sectorpos_bigbase.X*MAP_BLOCKSIZE,
+ y_nodes_min,
+ data->sectorpos_bigbase.Y*MAP_BLOCKSIZE
+ );
+ v3f maxpos_f = minpos_f + v3f(
+ data->sectorpos_bigbase_size*MAP_BLOCKSIZE,
+ y_nodes_max-y_nodes_min,
+ data->sectorpos_bigbase_size*MAP_BLOCKSIZE
+ );
+ v3f samplelength_f = v3f(4.0, 4.0, 4.0);
+
+ TimeTaker timer("noisebuf.create");
+
+ noisebuf1.create(data->seed+25104, 6, 0.60, 200.0,
+ minpos_f.X, minpos_f.Y, minpos_f.Z,
+ maxpos_f.X, maxpos_f.Y, maxpos_f.Z,
+ samplelength_f.X, samplelength_f.Y, samplelength_f.Z);
+ /*noisebuf1.create(data->seed+25104, 3, 0.60, 25.0,
+ minpos_f.X, minpos_f.Y, minpos_f.Z,
+ maxpos_f.X, maxpos_f.Y, maxpos_f.Z,
+ samplelength_f.X, samplelength_f.Y, samplelength_f.Z);
+ noisebuf2.create(data->seed+25105, 4, 0.50, 200.0,
+ minpos_f.X, minpos_f.Y, minpos_f.Z,
+ maxpos_f.X, maxpos_f.Y, maxpos_f.Z,
+ samplelength_f.X, samplelength_f.Y, samplelength_f.Z);*/
+ }
+
+ for(s16 x=0; x<data->sectorpos_bigbase_size*MAP_BLOCKSIZE; x++)
+ for(s16 z=0; z<data->sectorpos_bigbase_size*MAP_BLOCKSIZE; z++)
{
// Node position
- v2s16 p2d = sectorpos_bigbase*MAP_BLOCKSIZE + v2s16(x,z);
+ v2s16 p2d = data->sectorpos_bigbase*MAP_BLOCKSIZE + v2s16(x,z);
+
+ // Ground height at this point
+ float surface_y_f = 0.0;
+
+ // Use perlin noise for ground height
+ surface_y_f = base_rock_level_2d(data->seed, p2d);
+ //surface_y_f = base_rock_level_2d(data->seed, p2d);
+ // Convert to integer
+ s16 surface_y = (s16)surface_y_f;
+
+ // Log it
+ if(surface_y > stone_surface_max_y)
+ stone_surface_max_y = surface_y;
+
/*
- Skip of already generated
+ Fill ground with stone
*/
{
+ // Use fast index incrementing
+ v3s16 em = data->vmanip.m_area.getExtent();
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_nodes_min, p2d.Y));
+ for(s16 y=y_nodes_min; y<=y_nodes_max; y++)
+ {
+ // Skip if already generated.
+ // This is done here because there might be a cave at
+ // any point in ground, which could look like it
+ // wasn't generated.
+ if(data->vmanip.m_data[i].d != CONTENT_AIR)
+ break;
+
+ /*s16 noiseval = 50.0 * noise3d_perlin(
+ 0.5+(float)p2d.X/100.0,
+ 0.5+(float)y/100.0,
+ 0.5+(float)p2d.Y/100.0,
+ data->seed+123, 5, 0.5);*/
+ double noiseval = 64.0 * noisebuf1.get(p2d.X, y, p2d.Y);
+ /*double noiseval = 30.0 * noisebuf1.get(p2d.X, y, p2d.Y);
+ noiseval *= MYMAX(0, -0.2 + noisebuf2.get(p2d.X, y, p2d.Y));*/
+
+ //if(y < surface_y + noiseval)
+ if(noiseval > 0)
+ //if(noiseval > y)
+ data->vmanip.m_data[i].d = CONTENT_STONE;
+
+ data->vmanip.m_area.add_y(em, i, 1);
+ }
+ }
+ }
+#endif
+
+#if 1
+ for(s16 x=0; x<data->sectorpos_bigbase_size*MAP_BLOCKSIZE; x++)
+ for(s16 z=0; z<data->sectorpos_bigbase_size*MAP_BLOCKSIZE; z++)
+ {
+ // Node position
+ v2s16 p2d = data->sectorpos_bigbase*MAP_BLOCKSIZE + v2s16(x,z);
+
+ /*
+ Skip of already generated
+ */
+ /*{
v3s16 p(p2d.X, y_nodes_min, p2d.Y);
- if(vmanip.m_data[vmanip.m_area.index(p)].d != CONTENT_AIR)
+ if(data->vmanip.m_data[data->vmanip.m_area.index(p)].d != CONTENT_AIR)
continue;
- }
+ }*/
// Ground height at this point
float surface_y_f = 0.0;
// Use perlin noise for ground height
- surface_y_f = base_rock_level_2d(m_seed, p2d);
+ surface_y_f = base_rock_level_2d(data->seed, p2d);
/*// Experimental stuff
{
- float a = highlands_level_2d(m_seed, p2d);
+ float a = highlands_level_2d(data->seed, p2d);
if(a > surface_y_f)
surface_y_f = a;
}*/
@@ -2205,16 +2371,24 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
*/
{
// Use fast index incrementing
- v3s16 em = vmanip.m_area.getExtent();
- u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_min, p2d.Y));
+ v3s16 em = data->vmanip.m_area.getExtent();
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_nodes_min, p2d.Y));
for(s16 y=y_nodes_min; y<surface_y && y<=y_nodes_max; y++)
{
- vmanip.m_data[i].d = CONTENT_STONE;
+ // Skip if already generated.
+ // This is done here because there might be a cave at
+ // any point in ground, which could look like it
+ // wasn't generated.
+ if(data->vmanip.m_data[i].d != CONTENT_AIR)
+ break;
- vmanip.m_area.add_y(em, i, 1);
+ data->vmanip.m_data[i].d = CONTENT_STONE;
+
+ data->vmanip.m_area.add_y(em, i, 1);
}
}
}
+#endif
}//timer1
@@ -2222,167 +2396,49 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
Randomize some parameters
*/
- s32 stone_obstacle_count = 0;
+ //s32 stone_obstacle_count = 0;
/*s32 stone_obstacle_count =
- rangelim((1.0+noise2d(m_seed+897,
- sectorpos_base.X, sectorpos_base.Y))/2.0 * 30, 0, 100000);*/
+ rangelim((1.0+noise2d(data->seed+897,
+ data->sectorpos_base.X, data->sectorpos_base.Y))/2.0 * 30, 0, 100000);*/
- s16 stone_obstacle_max_height = 0;
+ //s16 stone_obstacle_max_height = 0;
/*s16 stone_obstacle_max_height =
- rangelim((1.0+noise2d(m_seed+5902,
- sectorpos_base.X, sectorpos_base.Y))/2.0 * 30, 0, 100000);*/
+ rangelim((1.0+noise2d(data->seed+5902,
+ data->sectorpos_base.X, data->sectorpos_base.Y))/2.0 * 30, 0, 100000);*/
/*
Loop this part, it will make stuff look older and newer nicely
*/
- //for(u32 i_age=0; i_age<1; i_age++)
- for(u32 i_age=0; i_age<2; i_age++)
+ const u32 age_loops = 2;
+ for(u32 i_age=0; i_age<age_loops; i_age++)
{ // Aging loop
+ /******************************
+ BEGINNING OF AGING LOOP
+ ******************************/
- {
- // 8ms @cs=8
- //TimeTaker timer1("stone obstacles");
-
- /*
- Add some random stone obstacles
- */
-
- for(s32 ri=0; ri<stone_obstacle_count; ri++)
- {
- // Randomize max height so usually stuff will be quite low
- s16 maxheight_randomized = myrand_range(0, stone_obstacle_max_height);
-
- //s16 stone_obstacle_max_size = sectorpos_base_size * MAP_BLOCKSIZE - 10;
- s16 stone_obstacle_max_size = MAP_BLOCKSIZE*4-4;
-
- v3s16 ob_size(
- myrand_range(5, stone_obstacle_max_size),
- myrand_range(0, maxheight_randomized),
- myrand_range(5, stone_obstacle_max_size)
- );
-
- // Don't make stupid small rectangle bumps
- if(ob_size.Y < 5)
- continue;
-
- v2s16 ob_place(
- myrand_range(1+ob_size.X/2+2,
- sectorpos_base_size*MAP_BLOCKSIZE-1-1-ob_size.X/2-2),
- myrand_range(1+ob_size.Z/2+2,
- sectorpos_base_size*MAP_BLOCKSIZE-1-1-ob_size.Z/2-2)
- );
-
- // Minimum space left on top of the obstacle
- s16 min_head_space = 12;
-
- for(s16 x=-ob_size.X/2; x<ob_size.X/2; x++)
- for(s16 z=-ob_size.Z/2; z<ob_size.Z/2; z++)
- {
- // Node position in 2d
- v2s16 p2d = sectorpos_base*MAP_BLOCKSIZE + ob_place + v2s16(x,z);
-
- // Find stone ground level
- // (ignore everything else than mud in already generated chunks)
- // and mud amount over the stone level
- s16 surface_y = 0;
- s16 mud_amount = 0;
- {
- v3s16 em = vmanip.m_area.getExtent();
- u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
- s16 y;
- // Go to ground level
- for(y=y_nodes_max; y>=y_nodes_min; y--)
- {
- MapNode *n = &vmanip.m_data[i];
- /*if(content_walkable(n.d)
- && n.d != CONTENT_MUD
- && n.d != CONTENT_GRASS)
- break;*/
- if(n->d == CONTENT_STONE)
- break;
-
- if(n->d == CONTENT_MUD || n->d == CONTENT_GRASS)
- {
- mud_amount++;
- /*
- Change to mud because otherwise we might
- be throwing mud on grass at the next
- step
- */
- n->d = CONTENT_MUD;
- }
-
- vmanip.m_area.add_y(em, i, -1);
- }
- if(y >= y_nodes_min)
- surface_y = y;
- else
- surface_y = y_nodes_min;
- }
-
-
- /*
- Add stone on ground
- */
- {
- v3s16 em = vmanip.m_area.getExtent();
- s16 y_start = surface_y+1;
- u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
- s16 y;
- // Add stone
- s16 count = 0;
- for(y=y_start; y<=y_nodes_max - min_head_space; y++)
- {
- MapNode &n = vmanip.m_data[i];
- n.d = CONTENT_STONE;
-
- if(y > stone_surface_max_y)
- stone_surface_max_y = y;
-
- count++;
- if(count >= ob_size.Y)
- break;
-
- vmanip.m_area.add_y(em, i, 1);
- }
- // Add mud
- count = 0;
- for(; y<=y_nodes_max - min_head_space; y++)
- {
- MapNode &n = vmanip.m_data[i];
- n.d = CONTENT_MUD;
- count++;
- if(count >= mud_amount)
- break;
-
- vmanip.m_area.add_y(em, i, 1);
- }
- }
-
- }
- }
-
- }//timer1
+#if 1
{
// 24ms @cs=8
- //TimeTaker timer1("dungeons");
+ //TimeTaker timer1("caves");
/*
- Make dungeons
+ Make caves
*/
- u32 dungeons_count = relative_volume / 600000;
+ u32 caves_count = relative_volume / 400000;
u32 bruises_count = relative_volume * stone_surface_max_y / 40000000;
if(stone_surface_max_y < WATER_LEVEL)
bruises_count = 0;
- /*u32 dungeons_count = 0;
+ /*u32 caves_count = 0;
u32 bruises_count = 0;*/
- for(u32 jj=0; jj<dungeons_count+bruises_count; jj++)
+ for(u32 jj=0; jj<caves_count+bruises_count; jj++)
{
- s16 min_tunnel_diameter = 2;
- s16 max_tunnel_diameter = 6;
- u16 tunnel_routepoints = 25;
+ s16 min_tunnel_diameter = 3;
+ s16 max_tunnel_diameter = 5;
+ u16 tunnel_routepoints = 20;
- bool bruise_surface = (jj < bruises_count);
+ v3f main_direction(0,0,0);
+
+ bool bruise_surface = (jj > caves_count);
if(bruise_surface)
{
@@ -2391,31 +2447,34 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
/*min_tunnel_diameter = MYMAX(0, stone_surface_max_y/6);
max_tunnel_diameter = myrand_range(MYMAX(0, stone_surface_max_y/6), MYMAX(0, stone_surface_max_y/2));*/
- /*s16 tunnel_rou = rangelim(25*(0.5+1.0*noise2d(m_seed+42,
- sectorpos_base.X, sectorpos_base.Y)), 0, 15);*/
+ /*s16 tunnel_rou = rangelim(25*(0.5+1.0*noise2d(data->seed+42,
+ data->sectorpos_base.X, data->sectorpos_base.Y)), 0, 15);*/
tunnel_routepoints = 5;
}
+ else
+ {
+ }
// Allowed route area size in nodes
v3s16 ar(
- sectorpos_base_size*MAP_BLOCKSIZE,
+ data->sectorpos_base_size*MAP_BLOCKSIZE,
h_blocks*MAP_BLOCKSIZE,
- sectorpos_base_size*MAP_BLOCKSIZE
+ data->sectorpos_base_size*MAP_BLOCKSIZE
);
// Area starting point in nodes
v3s16 of(
- sectorpos_base.X*MAP_BLOCKSIZE,
- y_blocks_min*MAP_BLOCKSIZE,
- sectorpos_base.Y*MAP_BLOCKSIZE
+ data->sectorpos_base.X*MAP_BLOCKSIZE,
+ data->y_blocks_min*MAP_BLOCKSIZE,
+ data->sectorpos_base.Y*MAP_BLOCKSIZE
);
// Allow a bit more
//(this should be more than the maximum radius of the tunnel)
//s16 insure = 5; // Didn't work with max_d = 20
s16 insure = 10;
- s16 more = max_spread_amount - max_tunnel_diameter/2 - insure;
+ s16 more = data->max_spread_amount - max_tunnel_diameter/2 - insure;
ar += v3s16(1,0,1) * more * 2;
of -= v3s16(1,0,1) * more;
@@ -2423,7 +2482,7 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
// Allow half a diameter + 7 over stone surface
s16 route_y_max = -of.Y + stone_surface_max_y + max_tunnel_diameter/2 + 7;
- /*// If dungeons, don't go through surface too often
+ /*// If caves, don't go through surface too often
if(bruise_surface == false)
route_y_max -= myrand_range(0, max_tunnel_diameter*2);*/
@@ -2448,16 +2507,16 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
s16 route_start_y_min = route_y_min;
s16 route_start_y_max = route_y_max;
- // Start every 2nd dungeon from surface
+ // Start every 2nd cave from surface
bool coming_from_surface = (jj % 2 == 0 && bruise_surface == false);
if(coming_from_surface)
{
- route_start_y_min = -of.Y + stone_surface_max_y + 5;
+ route_start_y_min = -of.Y + stone_surface_max_y + 10;
}
route_start_y_min = rangelim(route_start_y_min, 0, ar.Y-1);
- route_start_y_max = rangelim(route_start_y_max, 0, ar.Y-1);
+ route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y-1);
// Randomize starting position
v3f orp(
@@ -2474,6 +2533,16 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
for(u16 j=0; j<tunnel_routepoints; j++)
{
+ if(j%7==0 && bruise_surface == false)
+ {
+ main_direction = v3f(
+ ((float)(myrand()%20)-(float)10)/10,
+ ((float)(myrand()%20)-(float)10)/30,
+ ((float)(myrand()%20)-(float)10)/10
+ );
+ main_direction *= (float)myrand_range(1, 3);
+ }
+
// Randomize size
s16 min_d = min_tunnel_diameter;
s16 max_d = max_tunnel_diameter;
@@ -2486,7 +2555,7 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
}
else
{
- maxlen = v3s16(15, myrand_range(1, 20), 15);
+ maxlen = v3s16(rs*4, myrand_range(1, rs*3), rs*4);
}
v3f vec;
@@ -2507,6 +2576,8 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
(float)(myrand()%(maxlen.Z*2))-(float)maxlen.Z
);
}
+
+ vec += main_direction;
v3f rp = orp + vec;
if(rp.X < 0)
@@ -2553,8 +2624,8 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
continue;
p += of;
- //assert(vmanip.m_area.contains(p));
- if(vmanip.m_area.contains(p) == false)
+ //assert(data->vmanip.m_area.contains(p));
+ if(data->vmanip.m_area.contains(p) == false)
{
dstream<<"WARNING: "<<__FUNCTION_NAME
<<":"<<__LINE__<<": "
@@ -2565,13 +2636,13 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
// Just set it to air, it will be changed to
// water afterwards
- u32 i = vmanip.m_area.index(p);
- vmanip.m_data[i] = airnode;
+ u32 i = data->vmanip.m_area.index(p);
+ data->vmanip.m_data[i] = airnode;
if(bruise_surface == false)
{
// Set tunnel flag
- vmanip.m_flags[i] |= VMANIP_FLAG_DUNGEON;
+ data->vmanip.m_flags[i] |= VMANIP_FLAG_DUNGEON;
}
}
}
@@ -2584,6 +2655,9 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
}
}//timer1
+#endif
+
+#if 1
{
// 46ms @cs=8
//TimeTaker timer1("ore veins");
@@ -2597,22 +2671,22 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
// Allowed route area size in nodes
v3s16 ar(
- sectorpos_base_size*MAP_BLOCKSIZE,
+ data->sectorpos_base_size*MAP_BLOCKSIZE,
h_blocks*MAP_BLOCKSIZE,
- sectorpos_base_size*MAP_BLOCKSIZE
+ data->sectorpos_base_size*MAP_BLOCKSIZE
);
// Area starting point in nodes
v3s16 of(
- sectorpos_base.X*MAP_BLOCKSIZE,
- y_blocks_min*MAP_BLOCKSIZE,
- sectorpos_base.Y*MAP_BLOCKSIZE
+ data->sectorpos_base.X*MAP_BLOCKSIZE,
+ data->y_blocks_min*MAP_BLOCKSIZE,
+ data->sectorpos_base.Y*MAP_BLOCKSIZE
);
// Allow a bit more
//(this should be more than the maximum radius of the tunnel)
s16 insure = 3;
- s16 more = max_spread_amount - max_vein_diameter/2 - insure;
+ s16 more = data->max_spread_amount - max_vein_diameter/2 - insure;
ar += v3s16(1,0,1) * more * 2;
of -= v3s16(1,0,1) * more;
@@ -2698,12 +2772,12 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
continue;
p += of;
- assert(vmanip.m_area.contains(p));
+ assert(data->vmanip.m_area.contains(p));
// Just set it to air, it will be changed to
// water afterwards
- u32 i = vmanip.m_area.index(p);
- MapNode *n = &vmanip.m_data[i];
+ u32 i = data->vmanip.m_area.index(p);
+ MapNode *n = &data->vmanip.m_data[i];
if(n->d == CONTENT_STONE)
n->param = mineral;
}
@@ -2717,27 +2791,28 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
}
}//timer1
+#endif
+
+#if 1
{
// 15ms @cs=8
- //TimeTaker timer1("add mud");
+ TimeTaker timer1("add mud");
/*
Add mud to the central chunk
*/
- for(s16 x=0; x<sectorpos_base_size*MAP_BLOCKSIZE; x++)
- for(s16 z=0; z<sectorpos_base_size*MAP_BLOCKSIZE; z++)
+ for(s16 x=0; x<data->sectorpos_base_size*MAP_BLOCKSIZE; x++)
+ for(s16 z=0; z<data->sectorpos_base_size*MAP_BLOCKSIZE; z++)
{
// Node position in 2d
- v2s16 p2d = sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+ v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
// Randomize mud amount
- s16 mud_add_amount = (s16)(2.5 + 2.0 * noise2d_perlin(
- 0.5+(float)p2d.X/200, 0.5+(float)p2d.Y/200,
- m_seed+1, 3, 0.55));
+ s16 mud_add_amount = get_mud_add_amount(data->seed, p2d) / 2.0;
// Find ground level
- s16 surface_y = find_ground_level_clever(vmanip, p2d);
+ s16 surface_y = find_ground_level_clever(data->vmanip, p2d);
/*
If topmost node is grass, change it to mud.
@@ -2745,10 +2820,11 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
chunk and then converted.
*/
{
- u32 i = vmanip.m_area.index(v3s16(p2d.X, surface_y, p2d.Y));
- MapNode *n = &vmanip.m_data[i];
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, surface_y, p2d.Y));
+ MapNode *n = &data->vmanip.m_data[i];
if(n->d == CONTENT_GRASS)
- n->d = CONTENT_MUD;
+ *n = MapNode(CONTENT_MUD);
+ //n->d = CONTENT_MUD;
}
/*
@@ -2756,25 +2832,29 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
*/
{
s16 mudcount = 0;
- v3s16 em = vmanip.m_area.getExtent();
+ v3s16 em = data->vmanip.m_area.getExtent();
s16 y_start = surface_y+1;
- u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
for(s16 y=y_start; y<=y_nodes_max; y++)
{
if(mudcount >= mud_add_amount)
break;
- MapNode &n = vmanip.m_data[i];
- n.d = CONTENT_MUD;
+ MapNode &n = data->vmanip.m_data[i];
+ n = MapNode(CONTENT_MUD);
+ //n.d = CONTENT_MUD;
mudcount++;
- vmanip.m_area.add_y(em, i, 1);
+ data->vmanip.m_area.add_y(em, i, 1);
}
}
}
}//timer1
+#endif
+
+#if 1
{
// 340ms @cs=8
TimeTaker timer1("flow mud");
@@ -2784,8 +2864,8 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
*/
// Limit area by 1 because mud is flown into neighbors.
- s16 mudflow_minpos = 0-max_spread_amount+1;
- s16 mudflow_maxpos = sectorpos_base_size*MAP_BLOCKSIZE+max_spread_amount-2;
+ s16 mudflow_minpos = 0-data->max_spread_amount+1;
+ s16 mudflow_maxpos = data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount-2;
// Iterate a few times
for(s16 k=0; k<3; k++)
@@ -2806,10 +2886,10 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
}
// Node position in 2d
- v2s16 p2d = sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+ v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
- v3s16 em = vmanip.m_area.getExtent();
- u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
+ v3s16 em = data->vmanip.m_area.getExtent();
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
s16 y=y_nodes_max;
for(;; y--)
@@ -2818,22 +2898,22 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
// Find mud
for(; y>=y_nodes_min; y--)
{
- n = &vmanip.m_data[i];
+ n = &data->vmanip.m_data[i];
//if(content_walkable(n->d))
// break;
if(n->d == CONTENT_MUD || n->d == CONTENT_GRASS)
break;
- vmanip.m_area.add_y(em, i, -1);
+ data->vmanip.m_area.add_y(em, i, -1);
}
// Stop if out of area
- //if(vmanip.m_area.contains(i) == false)
+ //if(data->vmanip.m_area.contains(i) == false)
if(y < y_nodes_min)
break;
/*// If not mud, do nothing to it
- MapNode *n = &vmanip.m_data[i];
+ MapNode *n = &data->vmanip.m_data[i];
if(n->d != CONTENT_MUD && n->d != CONTENT_GRASS)
continue;*/
@@ -2842,11 +2922,11 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
*/
{
u32 i2 = i;
- vmanip.m_area.add_y(em, i2, -1);
+ data->vmanip.m_area.add_y(em, i2, -1);
// Cancel if out of area
- if(vmanip.m_area.contains(i2) == false)
+ if(data->vmanip.m_area.contains(i2) == false)
continue;
- MapNode *n2 = &vmanip.m_data[i2];
+ MapNode *n2 = &data->vmanip.m_data[i2];
if(n2->d != CONTENT_MUD && n2->d != CONTENT_GRASS)
continue;
}
@@ -2867,9 +2947,9 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
// Theck that upper is air or doesn't exist.
// Cancel dropping if upper keeps it in place
u32 i3 = i;
- vmanip.m_area.add_y(em, i3, 1);
- if(vmanip.m_area.contains(i3) == true
- && content_walkable(vmanip.m_data[i3].d) == true)
+ data->vmanip.m_area.add_y(em, i3, 1);
+ if(data->vmanip.m_area.contains(i3) == true
+ && content_walkable(data->vmanip.m_data[i3].d) == true)
{
continue;
}
@@ -2881,39 +2961,39 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
v3s16 dirp = dirs4[di];
u32 i2 = i;
// Move to side
- vmanip.m_area.add_p(em, i2, dirp);
+ data->vmanip.m_area.add_p(em, i2, dirp);
// Fail if out of area
- if(vmanip.m_area.contains(i2) == false)
+ if(data->vmanip.m_area.contains(i2) == false)
continue;
// Check that side is air
- MapNode *n2 = &vmanip.m_data[i2];
+ MapNode *n2 = &data->vmanip.m_data[i2];
if(content_walkable(n2->d))
continue;
// Check that under side is air
- vmanip.m_area.add_y(em, i2, -1);
- if(vmanip.m_area.contains(i2) == false)
+ data->vmanip.m_area.add_y(em, i2, -1);
+ if(data->vmanip.m_area.contains(i2) == false)
continue;
- n2 = &vmanip.m_data[i2];
+ n2 = &data->vmanip.m_data[i2];
if(content_walkable(n2->d))
continue;
/*// Check that under that is air (need a drop of 2)
- vmanip.m_area.add_y(em, i2, -1);
- if(vmanip.m_area.contains(i2) == false)
+ data->vmanip.m_area.add_y(em, i2, -1);
+ if(data->vmanip.m_area.contains(i2) == false)
continue;
- n2 = &vmanip.m_data[i2];
+ n2 = &data->vmanip.m_data[i2];
if(content_walkable(n2->d))
continue;*/
// Loop further down until not air
do{
- vmanip.m_area.add_y(em, i2, -1);
+ data->vmanip.m_area.add_y(em, i2, -1);
// Fail if out of area
- if(vmanip.m_area.contains(i2) == false)
+ if(data->vmanip.m_area.contains(i2) == false)
continue;
- n2 = &vmanip.m_data[i2];
+ n2 = &data->vmanip.m_data[i2];
}while(content_walkable(n2->d) == false);
// Loop one up so that we're in air
- vmanip.m_area.add_y(em, i2, 1);
- n2 = &vmanip.m_data[i2];
+ data->vmanip.m_area.add_y(em, i2, 1);
+ n2 = &data->vmanip.m_data[i2];
// Move mud to new place
*n2 = *n;
@@ -2929,26 +3009,29 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
}
}//timer1
+#endif
+
+#if 1
{
// 50ms @cs=8
- //TimeTaker timer1("add water");
+ TimeTaker timer1("add water");
/*
Add water to the central chunk (and a bit more)
*/
- for(s16 x=0-max_spread_amount;
- x<sectorpos_base_size*MAP_BLOCKSIZE+max_spread_amount;
+ for(s16 x=0-data->max_spread_amount;
+ x<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount;
x++)
- for(s16 z=0-max_spread_amount;
- z<sectorpos_base_size*MAP_BLOCKSIZE+max_spread_amount;
+ for(s16 z=0-data->max_spread_amount;
+ z<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount;
z++)
{
// Node position in 2d
- v2s16 p2d = sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+ v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
// Find ground level
- //s16 surface_y = find_ground_level(vmanip, p2d);
+ //s16 surface_y = find_ground_level(data->vmanip, p2d);
/*
If ground level is over water level, skip.
@@ -2963,42 +3046,27 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
Add water on ground
*/
{
- v3s16 em = vmanip.m_area.getExtent();
+ v3s16 em = data->vmanip.m_area.getExtent();
u8 light = LIGHT_MAX;
// Start at global water surface level
s16 y_start = WATER_LEVEL;
- u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
- MapNode *n = &vmanip.m_data[i];
-
- /*// Add first one to transforming liquid queue, if water
- if(n->d == CONTENT_WATER || n->d == CONTENT_WATERSOURCE)
- {
- v3s16 p = v3s16(p2d.X, y_start, p2d.Y);
- m_transforming_liquid.push_back(p);
- }*/
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
+ MapNode *n = &data->vmanip.m_data[i];
for(s16 y=y_start; y>=y_nodes_min; y--)
{
- n = &vmanip.m_data[i];
+ n = &data->vmanip.m_data[i];
// Stop when there is no water and no air
if(n->d != CONTENT_AIR && n->d != CONTENT_WATERSOURCE
&& n->d != CONTENT_WATER)
{
- /*// Add bottom one to transforming liquid queue
- vmanip.m_area.add_y(em, i, 1);
- n = &vmanip.m_data[i];
- if(n->d == CONTENT_WATER || n->d == CONTENT_WATERSOURCE)
- {
- v3s16 p = v3s16(p2d.X, y, p2d.Y);
- m_transforming_liquid.push_back(p);
- }*/
break;
}
- // Make water only not in dungeons
- if(!(vmanip.m_flags[i]&VMANIP_FLAG_DUNGEON))
+ // Make water only not in caves
+ if(!(data->vmanip.m_flags[i]&VMANIP_FLAG_DUNGEON))
{
n->d = CONTENT_WATERSOURCE;
//n->setLight(LIGHTBANK_DAY, light);
@@ -3006,11 +3074,11 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
// Add to transforming liquid queue (in case it'd
// start flowing)
v3s16 p = v3s16(p2d.X, y, p2d.Y);
- m_transforming_liquid.push_back(p);
+ data->transforming_liquid.push_back(p);
}
// Next one
- vmanip.m_area.add_y(em, i, -1);
+ data->vmanip.m_area.add_y(em, i, -1);
if(light > 0)
light--;
}
@@ -3019,9 +3087,14 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
}
}//timer1
+#endif
} // Aging loop
+ /***********************
+ END OF AGING LOOP
+ ************************/
+#if 1
{
//TimeTaker timer1("convert mud to sand");
@@ -3032,22 +3105,22 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
//s16 mud_add_amount = myrand_range(2, 4);
//s16 mud_add_amount = 0;
- /*for(s16 x=0; x<sectorpos_base_size*MAP_BLOCKSIZE; x++)
- for(s16 z=0; z<sectorpos_base_size*MAP_BLOCKSIZE; z++)*/
- for(s16 x=0-max_spread_amount+1;
- x<sectorpos_base_size*MAP_BLOCKSIZE+max_spread_amount-1;
+ /*for(s16 x=0; x<data->sectorpos_base_size*MAP_BLOCKSIZE; x++)
+ for(s16 z=0; z<data->sectorpos_base_size*MAP_BLOCKSIZE; z++)*/
+ for(s16 x=0-data->max_spread_amount+1;
+ x<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount-1;
x++)
- for(s16 z=0-max_spread_amount+1;
- z<sectorpos_base_size*MAP_BLOCKSIZE+max_spread_amount-1;
+ for(s16 z=0-data->max_spread_amount+1;
+ z<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount-1;
z++)
{
// Node position in 2d
- v2s16 p2d = sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+ v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
// Determine whether to have sand here
double sandnoise = noise2d_perlin(
0.5+(float)p2d.X/500, 0.5+(float)p2d.Y/500,
- m_seed+59420, 3, 0.50);
+ data->seed+59420, 3, 0.50);
bool have_sand = (sandnoise > -0.15);
@@ -3055,19 +3128,19 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
continue;
// Find ground level
- s16 surface_y = find_ground_level_clever(vmanip, p2d);
+ s16 surface_y = find_ground_level_clever(data->vmanip, p2d);
if(surface_y > WATER_LEVEL + 2)
continue;
{
- v3s16 em = vmanip.m_area.getExtent();
+ v3s16 em = data->vmanip.m_area.getExtent();
s16 y_start = surface_y;
- u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
u32 not_sand_counter = 0;
for(s16 y=y_start; y>=y_nodes_min; y--)
{
- MapNode *n = &vmanip.m_data[i];
+ MapNode *n = &data->vmanip.m_data[i];
if(n->d == CONTENT_MUD || n->d == CONTENT_GRASS)
{
n->d = CONTENT_SAND;
@@ -3079,13 +3152,16 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
break;
}
- vmanip.m_area.add_y(em, i, -1);
+ data->vmanip.m_area.add_y(em, i, -1);
}
}
}
}//timer1
+#endif
+
+#if 1
{
// 1ms @cs=8
//TimeTaker timer1("generate trees");
@@ -3096,72 +3172,77 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
{
// Divide area into parts
s16 div = 8;
- s16 sidelen = sectorpos_base_size*MAP_BLOCKSIZE / div;
+ s16 sidelen = data->sectorpos_base_size*MAP_BLOCKSIZE / div;
double area = sidelen * sidelen;
for(s16 x0=0; x0<div; x0++)
for(s16 z0=0; z0<div; z0++)
{
// Center position of part of division
v2s16 p2d_center(
- sectorpos_base.X*MAP_BLOCKSIZE + sidelen/2 + sidelen*x0,
- sectorpos_base.Y*MAP_BLOCKSIZE + sidelen/2 + sidelen*z0
+ data->sectorpos_base.X*MAP_BLOCKSIZE + sidelen/2 + sidelen*x0,
+ data->sectorpos_base.Y*MAP_BLOCKSIZE + sidelen/2 + sidelen*z0
);
// Minimum edge of part of division
v2s16 p2d_min(
- sectorpos_base.X*MAP_BLOCKSIZE + sidelen*x0,
- sectorpos_base.Y*MAP_BLOCKSIZE + sidelen*z0
+ data->sectorpos_base.X*MAP_BLOCKSIZE + sidelen*x0,
+ data->sectorpos_base.Y*MAP_BLOCKSIZE + sidelen*z0
);
// Maximum edge of part of division
v2s16 p2d_max(
- sectorpos_base.X*MAP_BLOCKSIZE + sidelen + sidelen*x0 - 1,
- sectorpos_base.Y*MAP_BLOCKSIZE + sidelen + sidelen*z0 - 1
+ data->sectorpos_base.X*MAP_BLOCKSIZE + sidelen + sidelen*x0 - 1,
+ data->sectorpos_base.Y*MAP_BLOCKSIZE + sidelen + sidelen*z0 - 1
);
// Amount of trees
- u32 tree_count = area * tree_amount_2d(m_seed, p2d_center);
+ u32 tree_count = area * tree_amount_2d(data->seed, p2d_center);
// Put trees in random places on part of division
for(u32 i=0; i<tree_count; i++)
{
s16 x = myrand_range(p2d_min.X, p2d_max.X);
s16 z = myrand_range(p2d_min.Y, p2d_max.Y);
- s16 y = find_ground_level(vmanip, v2s16(x,z));
+ s16 y = find_ground_level(data->vmanip, v2s16(x,z));
// Don't make a tree under water level
if(y < WATER_LEVEL)
continue;
+ // Don't make a tree so high that it doesn't fit
+ if(y > y_nodes_max - 6)
+ continue;
v3s16 p(x,y,z);
/*
Trees grow only on mud and grass
*/
{
- u32 i = vmanip.m_area.index(v3s16(p));
- MapNode *n = &vmanip.m_data[i];
+ u32 i = data->vmanip.m_area.index(v3s16(p));
+ MapNode *n = &data->vmanip.m_data[i];
if(n->d != CONTENT_MUD && n->d != CONTENT_GRASS)
continue;
}
p.Y++;
// Make a tree
- make_tree(vmanip, p);
+ make_tree(data->vmanip, p);
}
}
/*u32 tree_max = relative_area / 60;
//u32 count = myrand_range(0, tree_max);
for(u32 i=0; i<count; i++)
{
- s16 x = myrand_range(0, sectorpos_base_size*MAP_BLOCKSIZE-1);
- s16 z = myrand_range(0, sectorpos_base_size*MAP_BLOCKSIZE-1);
- x += sectorpos_base.X*MAP_BLOCKSIZE;
- z += sectorpos_base.Y*MAP_BLOCKSIZE;
- s16 y = find_ground_level(vmanip, v2s16(x,z));
+ s16 x = myrand_range(0, data->sectorpos_base_size*MAP_BLOCKSIZE-1);
+ s16 z = myrand_range(0, data->sectorpos_base_size*MAP_BLOCKSIZE-1);
+ x += data->sectorpos_base.X*MAP_BLOCKSIZE;
+ z += data->sectorpos_base.Y*MAP_BLOCKSIZE;
+ s16 y = find_ground_level(data->vmanip, v2s16(x,z));
// Don't make a tree under water level
if(y < WATER_LEVEL)
continue;
v3s16 p(x,y+1,z);
// Make a tree
- make_tree(vmanip, p);
+ make_tree(data->vmanip, p);
}*/
}
}//timer1
+#endif
+#if 1
{
// 19ms @cs=8
//TimeTaker timer1("grow grass");
@@ -3170,17 +3251,17 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
Grow grass
*/
- /*for(s16 x=0-4; x<sectorpos_base_size*MAP_BLOCKSIZE+4; x++)
- for(s16 z=0-4; z<sectorpos_base_size*MAP_BLOCKSIZE+4; z++)*/
- for(s16 x=0-max_spread_amount;
- x<sectorpos_base_size*MAP_BLOCKSIZE+max_spread_amount;
+ /*for(s16 x=0-4; x<data->sectorpos_base_size*MAP_BLOCKSIZE+4; x++)
+ for(s16 z=0-4; z<data->sectorpos_base_size*MAP_BLOCKSIZE+4; z++)*/
+ for(s16 x=0-data->max_spread_amount;
+ x<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount;
x++)
- for(s16 z=0-max_spread_amount;
- z<sectorpos_base_size*MAP_BLOCKSIZE+max_spread_amount;
+ for(s16 z=0-data->max_spread_amount;
+ z<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount;
z++)
{
// Node position in 2d
- v2s16 p2d = sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+ v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
/*
Find the lowest surface to which enough light ends up
@@ -3190,17 +3271,17 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
*/
s16 surface_y = 0;
{
- v3s16 em = vmanip.m_area.getExtent();
- u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
+ v3s16 em = data->vmanip.m_area.getExtent();
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
s16 y;
// Go to ground level
for(y=y_nodes_max; y>=y_nodes_min; y--)
{
- MapNode &n = vmanip.m_data[i];
+ MapNode &n = data->vmanip.m_data[i];
if(n.d != CONTENT_AIR
&& n.d != CONTENT_LEAVES)
break;
- vmanip.m_area.add_y(em, i, -1);
+ data->vmanip.m_area.add_y(em, i, -1);
}
if(y >= y_nodes_min)
surface_y = y;
@@ -3208,13 +3289,14 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
surface_y = y_nodes_min;
}
- u32 i = vmanip.m_area.index(p2d.X, surface_y, p2d.Y);
- MapNode *n = &vmanip.m_data[i];
+ u32 i = data->vmanip.m_area.index(p2d.X, surface_y, p2d.Y);
+ MapNode *n = &data->vmanip.m_data[i];
if(n->d == CONTENT_MUD)
n->d = CONTENT_GRASS;
}
}//timer1
+#endif
/*
Initial lighting (sunlight)
@@ -3226,6 +3308,7 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
// 750ms @cs=8, can't optimize more
TimeTaker timer1("initial lighting");
+ // NOTE: This is no used... umm... for some reason!
#if 0
/*
Go through the edges and add all nodes that have light to light_sources
@@ -3260,15 +3343,15 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
}
// Node position in 2d
- v2s16 p2d = sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+ v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
{
- v3s16 em = vmanip.m_area.getExtent();
+ v3s16 em = data->vmanip.m_area.getExtent();
s16 y_start = y_nodes_max;
- u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
for(s16 y=y_start; y>=y_nodes_min; y--)
{
- MapNode *n = &vmanip.m_data[i];
+ MapNode *n = &data->vmanip.m_data[i];
if(n->getLight(LIGHTBANK_DAY) != 0)
{
light_sources.insert(v3s16(p2d.X, y, p2d.Y), true);
@@ -3315,17 +3398,17 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
}
// Node position in 2d
- v2s16 p2d = sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+ v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
// Loop from top to down
{
u8 light = LIGHT_SUN;
- v3s16 em = vmanip.m_area.getExtent();
+ v3s16 em = data->vmanip.m_area.getExtent();
s16 y_start = y_nodes_max;
- u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
for(s16 y=y_start; y>=y_nodes_min; y--)
{
- MapNode *n = &vmanip.m_data[i];
+ MapNode *n = &data->vmanip.m_data[i];
if(light_propagates_content(n->d) == false)
{
light = 0;
@@ -3347,19 +3430,19 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
}
// Increment index by y
- vmanip.m_area.add_y(em, i, -1);
+ data->vmanip.m_area.add_y(em, i, -1);
}
}
}
#endif
- /*for(s16 x=0; x<sectorpos_base_size*MAP_BLOCKSIZE; x++)
- for(s16 z=0; z<sectorpos_base_size*MAP_BLOCKSIZE; z++)*/
- /*for(s16 x=0-max_spread_amount+1;
- x<sectorpos_base_size*MAP_BLOCKSIZE+max_spread_amount-1;
+ /*for(s16 x=0; x<data->sectorpos_base_size*MAP_BLOCKSIZE; x++)
+ for(s16 z=0; z<data->sectorpos_base_size*MAP_BLOCKSIZE; z++)*/
+ /*for(s16 x=0-data->max_spread_amount+1;
+ x<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount-1;
x++)
- for(s16 z=0-max_spread_amount+1;
- z<sectorpos_base_size*MAP_BLOCKSIZE+max_spread_amount-1;
+ for(s16 z=0-data->max_spread_amount+1;
+ z<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount-1;
z++)*/
#if 1
/*
@@ -3374,7 +3457,7 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
z++)
{
// Node position in 2d
- v2s16 p2d = sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+ v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
/*
Apply initial sunlight
@@ -3382,12 +3465,12 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
{
u8 light = LIGHT_SUN;
bool add_to_sources = false;
- v3s16 em = vmanip.m_area.getExtent();
+ v3s16 em = data->vmanip.m_area.getExtent();
s16 y_start = y_nodes_max;
- u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
+ u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
for(s16 y=y_start; y>=y_nodes_min; y--)
{
- MapNode *n = &vmanip.m_data[i];
+ MapNode *n = &data->vmanip.m_data[i];
if(light_propagates_content(n->d) == false)
{
@@ -3417,8 +3500,8 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
{
v3s16 dirp = dirs4[di];
u32 i2 = i;
- vmanip.m_area.add_p(em, i2, dirp);
- MapNode *n2 = &vmanip.m_data[i2];
+ data->vmanip.m_area.add_p(em, i2, dirp);
+ MapNode *n2 = &data->vmanip.m_data[i2];
if(
n2->d != CONTENT_AIR
&& n2->d != CONTENT_WATERSOURCE
@@ -3441,84 +3524,167 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
}
// Increment index by y
- vmanip.m_area.add_y(em, i, -1);
+ data->vmanip.m_area.add_y(em, i, -1);
}
}
}
#endif
-#if 0
- for(s16 x=lighting_min_d+1;
- x<=lighting_max_d-1;
- x++)
- for(s16 z=lighting_min_d+1;
- z<=lighting_max_d-1;
- z++)
+ }//timer1
+
+ // Spread light around
{
- // Node position in 2d
- v2s16 p2d = sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+ TimeTaker timer("makeChunk() spreadLight");
+ data->vmanip.spreadLight(LIGHTBANK_DAY, light_sources);
+ }
+
+ /*
+ Generation ended
+ */
+
+ timer_generate.stop();
+}
+
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+//###################################################################
+
+void ServerMap::initChunkMake(ChunkMakeData &data, v2s16 chunkpos)
+{
+ if(m_chunksize == 0)
+ {
+ data.no_op = true;
+ return;
+ }
+
+ data.no_op = false;
+
+ // The distance how far into the neighbors the generator is allowed to go.
+ s16 max_spread_amount_sectors = 2;
+ assert(max_spread_amount_sectors <= m_chunksize);
+ s16 max_spread_amount = max_spread_amount_sectors * MAP_BLOCKSIZE;
+
+ s16 y_blocks_min = -4;
+ s16 y_blocks_max = 3;
+
+ v2s16 sectorpos_base = chunk_to_sector(chunkpos);
+ s16 sectorpos_base_size = m_chunksize;
+
+ v2s16 sectorpos_bigbase =
+ sectorpos_base - v2s16(1,1) * max_spread_amount_sectors;
+ s16 sectorpos_bigbase_size =
+ sectorpos_base_size + 2 * max_spread_amount_sectors;
+
+ // Check limits
+ const s16 limit = MAP_GENERATION_LIMIT / MAP_BLOCKSIZE;
+ if(sectorpos_bigbase.X < -limit
+ || sectorpos_bigbase.X + sectorpos_bigbase_size >= limit
+ || sectorpos_bigbase.Y < -limit
+ || sectorpos_bigbase.Y + sectorpos_bigbase_size >= limit)
+ {
+ data.no_op = true;
+ return;
+ }
+
+ data.seed = m_seed;
+ data.chunkpos = chunkpos;
+ data.y_blocks_min = y_blocks_min;
+ data.y_blocks_max = y_blocks_max;
+ data.sectorpos_base = sectorpos_base;
+ data.sectorpos_base_size = sectorpos_base_size;
+ data.sectorpos_bigbase = sectorpos_bigbase;
+ data.sectorpos_bigbase_size = sectorpos_bigbase_size;
+ data.max_spread_amount = max_spread_amount;
+
+ /*
+ Create the whole area of this and the neighboring chunks
+ */
+ {
+ TimeTaker timer("initChunkMake() create area");
- /*
- Apply initial sunlight
- */
+ for(s16 x=0; x<sectorpos_bigbase_size; x++)
+ for(s16 z=0; z<sectorpos_bigbase_size; z++)
{
- u8 light = LIGHT_SUN;
- v3s16 em = vmanip.m_area.getExtent();
- s16 y_start = y_nodes_max;
- u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
- for(s16 y=y_start; y>=y_nodes_min; y--)
+ v2s16 sectorpos = sectorpos_bigbase + v2s16(x,z);
+ ServerMapSector *sector = createSector(sectorpos);
+ assert(sector);
+
+ for(s16 y=y_blocks_min; y<=y_blocks_max; y++)
{
- MapNode *n = &vmanip.m_data[i];
+ v3s16 blockpos(sectorpos.X, y, sectorpos.Y);
+ MapBlock *block = createBlock(blockpos);
- if(light_propagates_content(n->d) == false)
- {
- light = 0;
- }
- else if(light != LIGHT_SUN
- || sunlight_propagates_content(n->d) == false)
- {
- if(light > 0)
- light--;
- }
-
- n->setLight(LIGHTBANK_DAY, light);
- n->setLight(LIGHTBANK_NIGHT, 0);
-
- // This doesn't take much time
- if(light != 0)
- {
- // Insert light source
- light_sources.insert(v3s16(p2d.X, y, p2d.Y), true);
- }
-
- // Increment index by y
- vmanip.m_area.add_y(em, i, -1);
+ // Lighting won't be calculated
+ //block->setLightingExpired(true);
+ // Lighting will be calculated
+ block->setLightingExpired(false);
+
+ /*
+ Block gets sunlight if this is true.
+
+ This should be set to true when the top side of a block
+ is completely exposed to the sky.
+
+ Actually this doesn't matter now because the
+ initial lighting is done here.
+ */
+ block->setIsUnderground(y != y_blocks_max);
}
}
}
-#endif
-
- }//timer1
+
+ /*
+ Now we have a big empty area.
- // Spread light around
+ Make a ManualMapVoxelManipulator that contains this and the
+ neighboring chunks
+ */
+
+ v3s16 bigarea_blocks_min(
+ sectorpos_bigbase.X,
+ y_blocks_min,
+ sectorpos_bigbase.Y
+ );
+ v3s16 bigarea_blocks_max(
+ sectorpos_bigbase.X + sectorpos_bigbase_size - 1,
+ y_blocks_max,
+ sectorpos_bigbase.Y + sectorpos_bigbase_size - 1
+ );
+
+ data.vmanip.setMap(this);
+ // Add the area
{
- TimeTaker timer("generateChunkRaw() spreadLight");
- vmanip.spreadLight(LIGHTBANK_DAY, light_sources);
+ TimeTaker timer("initChunkMake() initialEmerge");
+ data.vmanip.initialEmerge(bigarea_blocks_min, bigarea_blocks_max);
}
- /*
- Generation ended
- */
-
- timer_generate.stop();
+}
+MapChunk* ServerMap::finishChunkMake(ChunkMakeData &data,
+ core::map<v3s16, MapBlock*> &changed_blocks)
+{
+ if(data.no_op)
+ return NULL;
+
/*
Blit generated stuff to map
*/
{
// 70ms @cs=8
//TimeTaker timer("generateChunkRaw() blitBackAll");
- vmanip.blitBackAll(&changed_blocks);
+ data.vmanip.blitBackAll(&changed_blocks);
}
/*
@@ -3533,7 +3699,35 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
}
}
-
+ /*
+ Copy transforming liquid information
+ */
+ while(data.transforming_liquid.size() > 0)
+ {
+ v3s16 p = data.transforming_liquid.pop_front();
+ m_transforming_liquid.push_back(p);
+ }
+
+ /*
+ Add random objects to blocks
+ */
+ {
+ for(s16 x=0; x<data.sectorpos_base_size; x++)
+ for(s16 z=0; z<data.sectorpos_base_size; z++)
+ {
+ v2s16 sectorpos = data.sectorpos_base + v2s16(x,z);
+ ServerMapSector *sector = createSector(sectorpos);
+ assert(sector);
+
+ for(s16 y=data.y_blocks_min; y<=data.y_blocks_max; y++)
+ {
+ v3s16 blockpos(sectorpos.X, y, sectorpos.Y);
+ MapBlock *block = createBlock(blockpos);
+ addRandomObjects(block);
+ }
+ }
+ }
+
/*
Create chunk metadata
*/
@@ -3541,7 +3735,7 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
for(s16 x=-1; x<=1; x++)
for(s16 y=-1; y<=1; y++)
{
- v2s16 chunkpos0 = chunkpos + v2s16(x,y);
+ v2s16 chunkpos0 = data.chunkpos + v2s16(x,y);
// Add chunk meta information
MapChunk *chunk = getChunk(chunkpos0);
if(chunk == NULL)
@@ -3557,7 +3751,7 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
/*
Set central chunk non-volatile
*/
- MapChunk *chunk = getChunk(chunkpos);
+ MapChunk *chunk = getChunk(data.chunkpos);
assert(chunk);
// Set non-volatile
//chunk->setIsVolatile(false);
@@ -3567,6 +3761,49 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
Save changed parts of map
*/
save(true);
+
+ return chunk;
+}
+
+#if 0
+// NOTE: Deprecated
+MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
+ core::map<v3s16, MapBlock*> &changed_blocks,
+ bool force)
+{
+ DSTACK(__FUNCTION_NAME);
+
+ /*
+ Don't generate if already fully generated
+ */
+ if(force == false)
+ {
+ MapChunk *chunk = getChunk(chunkpos);
+ if(chunk != NULL && chunk->getGenLevel() == GENERATED_FULLY)
+ {
+ dstream<<"generateChunkRaw(): Chunk "
+ <<"("<<chunkpos.X<<","<<chunkpos.Y<<")"
+ <<" already generated"<<std::endl;
+ return chunk;
+ }
+ }
+
+ dstream<<"generateChunkRaw(): Generating chunk "
+ <<"("<<chunkpos.X<<","<<chunkpos.Y<<")"
+ <<std::endl;
+
+ TimeTaker timer("generateChunkRaw()");
+
+ ChunkMakeData data;
+
+ // Initialize generation
+ initChunkMake(data, chunkpos);
+
+ // Generate stuff
+ makeChunk(&data);
+
+ // Finalize generation
+ MapChunk *chunk = finishChunkMake(data, changed_blocks);
/*
Return central chunk (which was requested)
@@ -3574,6 +3811,7 @@ MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
return chunk;
}
+// NOTE: Deprecated
MapChunk* ServerMap::generateChunk(v2s16 chunkpos1,
core::map<v3s16, MapBlock*> &changed_blocks)
{
@@ -3599,10 +3837,11 @@ MapChunk* ServerMap::generateChunk(v2s16 chunkpos1,
MapChunk *chunk = getChunk(chunkpos1);
return chunk;
}
+#endif
ServerMapSector * ServerMap::createSector(v2s16 p2d)
{
- DSTACK("%s: p2d=(%d,%d)",
+ DSTACKF("%s: p2d=(%d,%d)",
__FUNCTION_NAME,
p2d.X, p2d.Y);
@@ -3653,6 +3892,7 @@ ServerMapSector * ServerMap::createSector(v2s16 p2d)
return sector;
}
+#if 0
MapSector * ServerMap::emergeSector(v2s16 p2d,
core::map<v3s16, MapBlock*> &changed_blocks)
{
@@ -3739,6 +3979,7 @@ MapSector * ServerMap::emergeSector(v2s16 p2d,
*/
//return generateSector();
}
+#endif
/*
NOTE: This is not used for main map generation, only for blocks
@@ -3752,9 +3993,17 @@ MapBlock * ServerMap::generateBlock(
core::map<v3s16, MapBlock*> &lighting_invalidated_blocks
)
{
- DSTACK("%s: p=(%d,%d,%d)",
+ DSTACKF("%s: p=(%d,%d,%d)",
__FUNCTION_NAME,
p.X, p.Y, p.Z);
+
+ // If chunks are disabled
+ /*if(m_chunksize == 0)
+ {
+ dstream<<"ServerMap::generateBlock(): Chunks disabled -> "
+ <<"not generating."<<std::endl;
+ return NULL;
+ }*/
/*dstream<<"generateBlock(): "
<<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
@@ -3793,6 +4042,25 @@ MapBlock * ServerMap::generateBlock(
block->unDummify();
}
+#if 0
+ /*
+ Generate a completely empty block
+ */
+ for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
+ for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
+ {
+ for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
+ {
+ MapNode n;
+ n.d = CONTENT_AIR;
+ block->setNode(v3s16(x0,y0,z0), n);
+ }
+ }
+#else
+ /*
+ Generate a proper block
+ */
+
u8 water_material = CONTENT_WATERSOURCE;
s32 lowest_ground_y = 32767;
@@ -3805,8 +4073,13 @@ MapBlock * ServerMap::generateBlock(
//s16 surface_y = 0;
+ s16 mud_add_amount = get_mud_add_amount(m_seed, p2d_nodes+v2s16(x0,z0));
+
s16 surface_y = base_rock_level_2d(m_seed, p2d_nodes+v2s16(x0,z0))
- + AVERAGE_MUD_AMOUNT;
+ + mud_add_amount;
+ // If chunks are disabled
+ if(m_chunksize == 0)
+ surface_y = WATER_LEVEL + 1;
if(surface_y < lowest_ground_y)
lowest_ground_y = surface_y;
@@ -3852,7 +4125,7 @@ MapBlock * ServerMap::generateBlock(
else
n.d = CONTENT_AIR;
}
- // Else it's ground or dungeons (air)
+ // Else it's ground or caves (air)
else
{
// If it's surface_depth under ground, it's stone
@@ -3933,7 +4206,7 @@ MapBlock * ServerMap::generateBlock(
//dstream<<"generateBlock(): Done"<<std::endl;
/*
- Generate dungeons
+ Generate caves
*/
// Initialize temporary table
@@ -4071,36 +4344,36 @@ MapBlock * ServerMap::generateBlock(
continue_generating:
/*
- Choose whether to actually generate dungeon
+ Choose whether to actually generate cave
*/
- bool do_generate_dungeons = true;
+ bool do_generate_caves = true;
// Don't generate if no part is underground
if(!some_part_underground)
{
- do_generate_dungeons = false;
+ do_generate_caves = false;
}
// Don't generate if mostly underwater surface
/*else if(mostly_underwater_surface)
{
- do_generate_dungeons = false;
+ do_generate_caves = false;
}*/
// Partly underground = cave
else if(!completely_underground)
{
- do_generate_dungeons = (rand() % 100 <= (s32)(caves_amount*100));
+ do_generate_caves = (rand() % 100 <= (s32)(caves_amount*100));
}
- // Found existing dungeon underground
+ // Found existing cave underground
else if(found_existing && completely_underground)
{
- do_generate_dungeons = (rand() % 100 <= (s32)(caves_amount*100));
+ do_generate_caves = (rand() % 100 <= (s32)(caves_amount*100));
}
- // Underground and no dungeons found
+ // Underground and no caves found
else
{
- do_generate_dungeons = (rand() % 300 <= (s32)(caves_amount*100));
+ do_generate_caves = (rand() % 300 <= (s32)(caves_amount*100));
}
- if(do_generate_dungeons)
+ if(do_generate_caves)
{
/*
Generate some tunnel starting from orp and ors
@@ -4152,7 +4425,7 @@ continue_generating:
// Set to true if has caves.
// Set when some non-air is changed to air when making caves.
- bool has_dungeons = false;
+ bool has_caves = false;
/*
Apply temporary cave data to block
@@ -4165,7 +4438,7 @@ continue_generating:
{
MapNode n = block->getNode(v3s16(x0,y0,z0));
- // Create dungeons
+ // Create caves
if(underground_emptiness[
ued*ued*(z0*ued/MAP_BLOCKSIZE)
+ued*(y0*ued/MAP_BLOCKSIZE)
@@ -4174,7 +4447,7 @@ continue_generating:
if(content_features(n.d).walkable/*is_ground_content(n.d)*/)
{
// Has now caves
- has_dungeons = true;
+ has_caves = true;
// Set air to node
n.d = CONTENT_AIR;
}
@@ -4194,7 +4467,7 @@ continue_generating:
Force lighting update if some part of block is partly
underground and has caves.
*/
- /*if(some_part_underground && !completely_underground && has_dungeons)
+ /*if(some_part_underground && !completely_underground && has_caves)
{
//dstream<<"Half-ground caves"<<std::endl;
lighting_invalidated_blocks[block->getPos()] = block;
@@ -4324,6 +4597,8 @@ continue_generating:
}
}
}
+
+#endif // end of proper block generation
/*
Add block to sector.
@@ -4339,11 +4614,11 @@ continue_generating:
*/
dstream
<<"lighting_invalidated_blocks.size()"
- <<", has_dungeons"
+ <<", has_caves"
<<", completely_ug"
<<", some_part_ug"
<<" "<<lighting_invalidated_blocks.size()
- <<", "<<has_dungeons
+ <<", "<<has_caves
<<", "<<completely_underground
<<", "<<some_part_underground
<<std::endl;
@@ -4354,7 +4629,7 @@ continue_generating:
MapBlock * ServerMap::createBlock(v3s16 p)
{
- DSTACK("%s: p=(%d,%d,%d)",
+ DSTACKF("%s: p=(%d,%d,%d)",
__FUNCTION_NAME, p.X, p.Y, p.Z);
/*
@@ -4418,7 +4693,7 @@ MapBlock * ServerMap::emergeBlock(
core::map<v3s16, MapBlock*> &lighting_invalidated_blocks
)
{
- DSTACK("%s: p=(%d,%d,%d), only_from_disk=%d",
+ DSTACKF("%s: p=(%d,%d,%d), only_from_disk=%d",
__FUNCTION_NAME,
p.X, p.Y, p.Z, only_from_disk);
@@ -4604,13 +4879,16 @@ plan_b:
/*
Plan B: Get from map generator perlin noise function
*/
- double level = base_rock_level_2d(m_seed, p2d);
+ // This won't work if proper generation is disabled
+ if(m_chunksize == 0)
+ return WATER_LEVEL+2;
+ double level = base_rock_level_2d(m_seed, p2d) + AVERAGE_MUD_AMOUNT;
return (s16)level;
}
-void ServerMap::createDir(std::string path)
+void ServerMap::createDirs(std::string path)
{
- if(fs::CreateDir(path) == false)
+ if(fs::CreateAllDirs(path) == false)
{
m_dout<<DTIME<<"ServerMap: Failed to create directory "
<<"\""<<path<<"\""<<std::endl;
@@ -4618,29 +4896,52 @@ void ServerMap::createDir(std::string path)
}
}
-std::string ServerMap::getSectorSubDir(v2s16 pos)
+std::string ServerMap::getSectorDir(v2s16 pos, int layout)
{
char cc[9];
- snprintf(cc, 9, "%.4x%.4x",
- (unsigned int)pos.X&0xffff,
- (unsigned int)pos.Y&0xffff);
+ switch(layout)
+ {
+ case 1:
+ snprintf(cc, 9, "%.4x%.4x",
+ (unsigned int)pos.X&0xffff,
+ (unsigned int)pos.Y&0xffff);
- return std::string(cc);
-}
+ return m_savedir + "/sectors/" + cc;
+ case 2:
+ snprintf(cc, 9, "%.3x/%.3x",
+ (unsigned int)pos.X&0xfff,
+ (unsigned int)pos.Y&0xfff);
-std::string ServerMap::getSectorDir(v2s16 pos)
-{
- return m_savedir + "/sectors/" + getSectorSubDir(pos);
+ return m_savedir + "/sectors2/" + cc;
+ default:
+ assert(false);
+ }
}
v2s16 ServerMap::getSectorPos(std::string dirname)
{
- if(dirname.size() != 8)
- throw InvalidFilenameException("Invalid sector directory name");
unsigned int x, y;
- int r = sscanf(dirname.c_str(), "%4x%4x", &x, &y);
- if(r != 2)
- throw InvalidFilenameException("Invalid sector directory name");
+ int r;
+ size_t spos = dirname.rfind('/') + 1;
+ assert(spos != std::string::npos);
+ if(dirname.size() - spos == 8)
+ {
+ // Old layout
+ r = sscanf(dirname.substr(spos).c_str(), "%4x%4x", &x, &y);
+ }
+ else if(dirname.size() - spos == 3)
+ {
+ // New layout
+ r = sscanf(dirname.substr(spos-4).c_str(), "%3x/%3x", &x, &y);
+ // Sign-extend the 12 bit values up to 16 bits...
+ if(x&0x800) x|=0xF000;
+ if(y&0x800) y|=0xF000;
+ }
+ else
+ {
+ assert(false);
+ }
+ assert(r == 2);
v2s16 pos((s16)x, (s16)y);
return pos;
}
@@ -4672,14 +4973,23 @@ void ServerMap::save(bool only_changed)
dstream<<DTIME<<"ServerMap: Saving whole map, this can take time."
<<std::endl;
- saveMapMeta();
- saveChunkMeta();
+ if(only_changed == false || m_map_metadata_changed)
+ {
+ saveMapMeta();
+ }
+
+ // Disable saving chunk metadata if chunks are disabled
+ if(m_chunksize != 0)
+ {
+ if(only_changed == false || anyChunkModified())
+ saveChunkMeta();
+ }
u32 sector_meta_count = 0;
u32 block_count = 0;
{ //sectorlock
- JMutexAutoLock lock(m_sector_mutex);
+ //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
core::map<v2s16, MapSector*>::Iterator i = m_sectors.getIterator();
for(; i.atEnd() == false; i++)
@@ -4727,6 +5037,8 @@ void ServerMap::save(bool only_changed)
}
}
+#if 0
+// NOTE: Doing this is insane. Deprecated and probably broken.
void ServerMap::loadAll()
{
DSTACK(__FUNCTION_NAME);
@@ -4739,7 +5051,7 @@ void ServerMap::loadAll()
dstream<<DTIME<<"There are "<<list.size()<<" sectors."<<std::endl;
- JMutexAutoLock lock(m_sector_mutex);
+ //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
s32 counter = 0;
s32 printed_counter = -100000;
@@ -4787,6 +5099,7 @@ void ServerMap::loadAll()
}
dstream<<DTIME<<"ServerMap: Map loaded."<<std::endl;
}
+#endif
#if 0
void ServerMap::saveMasterHeightmap()
@@ -4827,7 +5140,7 @@ void ServerMap::saveMapMeta()
<<"seed="<<m_seed<<", chunksize="<<m_chunksize
<<std::endl;
- createDir(m_savedir);
+ createDirs(m_savedir);
std::string fullpath = m_savedir + "/map_meta.txt";
std::ofstream os(fullpath.c_str(), std::ios_base::binary);
@@ -4846,13 +5159,14 @@ void ServerMap::saveMapMeta()
os<<"[end_of_params]\n";
+ m_map_metadata_changed = false;
}
void ServerMap::loadMapMeta()
{
DSTACK(__FUNCTION_NAME);
- dstream<<"INFO: ServerMap::loadMapMeta(): Loading chunk metadata"
+ dstream<<"INFO: ServerMap::loadMapMeta(): Loading map metadata"
<<std::endl;
std::string fullpath = m_savedir + "/map_meta.txt";
@@ -4861,7 +5175,7 @@ void ServerMap::loadMapMeta()
{
dstream<<"ERROR: ServerMap::loadMapMeta(): "
<<"could not open"<<fullpath<<std::endl;
- throw FileNotGoodException("Cannot open chunk metadata");
+ throw FileNotGoodException("Cannot open map metadata");
}
Settings params;
@@ -4890,13 +5204,16 @@ void ServerMap::loadMapMeta()
void ServerMap::saveChunkMeta()
{
DSTACK(__FUNCTION_NAME);
+
+ // This should not be called if chunks are disabled.
+ assert(m_chunksize != 0);
u32 count = m_chunks.size();
dstream<<"INFO: ServerMap::saveChunkMeta(): Saving metadata of "
<<count<<" chunks"<<std::endl;
- createDir(m_savedir);
+ createDirs(m_savedir);
std::string fullpath = m_savedir + "/chunk_meta";
std::ofstream os(fullpath.c_str(), std::ios_base::binary);
@@ -4930,6 +5247,8 @@ void ServerMap::saveChunkMeta()
// Write chunk data
chunk->serialize(os, version);
}
+
+ setChunksNonModified();
}
void ServerMap::loadChunkMeta()
@@ -4982,10 +5301,8 @@ void ServerMap::saveSectorMeta(ServerMapSector *sector)
u8 version = SER_FMT_VER_HIGHEST;
// Get destination
v2s16 pos = sector->getPos();
- createDir(m_savedir);
- createDir(m_savedir+"/sectors");
std::string dir = getSectorDir(pos);
- createDir(dir);
+ createDirs(dir);
std::string fullpath = dir + "/meta";
std::ofstream o(fullpath.c_str(), std::ios_base::binary);
@@ -4997,20 +5314,41 @@ void ServerMap::saveSectorMeta(ServerMapSector *sector)
sector->differs_from_disk = false;
}
-MapSector* ServerMap::loadSectorMeta(std::string dirname)
+MapSector* ServerMap::loadSectorMeta(std::string sectordir, bool save_after_load)
{
DSTACK(__FUNCTION_NAME);
// Get destination
- v2s16 p2d = getSectorPos(dirname);
- std::string dir = m_savedir + "/sectors/" + dirname;
-
- std::string fullpath = dir + "/meta";
+ v2s16 p2d = getSectorPos(sectordir);
+
+ ServerMapSector *sector = NULL;
+
+ std::string fullpath = sectordir + "/meta";
std::ifstream is(fullpath.c_str(), std::ios_base::binary);
if(is.good() == false)
- throw FileNotGoodException("Cannot open sector metafile");
-
- ServerMapSector *sector = ServerMapSector::deSerialize
- (is, this, p2d, m_sectors);
+ {
+ // If the directory exists anyway, it probably is in some old
+ // format. Just go ahead and create the sector.
+ if(fs::PathExists(sectordir))
+ {
+ dstream<<"ServerMap::loadSectorMeta(): Sector metafile "
+ <<fullpath<<" doesn't exist but directory does."
+ <<" Continuing with a sector with no metadata."
+ <<std::endl;
+ sector = new ServerMapSector(this, p2d);
+ m_sectors.insert(p2d, sector);
+ }
+ else
+ {
+ throw FileNotGoodException("Cannot open sector metafile");
+ }
+ }
+ else
+ {
+ sector = ServerMapSector::deSerialize
+ (is, this, p2d, m_sectors);
+ if(save_after_load)
+ saveSectorMeta(sector);
+ }
sector->differs_from_disk = false;
@@ -5020,14 +5358,31 @@ MapSector* ServerMap::loadSectorMeta(std::string dirname)
bool ServerMap::loadSectorFull(v2s16 p2d)
{
DSTACK(__FUNCTION_NAME);
- std::string sectorsubdir = getSectorSubDir(p2d);
MapSector *sector = NULL;
- JMutexAutoLock lock(m_sector_mutex);
+ // The directory layout we're going to load from.
+ // 1 - original sectors/xxxxzzzz/
+ // 2 - new sectors2/xxx/zzz/
+ // If we load from anything but the latest structure, we will
+ // immediately save to the new one, and remove the old.
+ int loadlayout = 1;
+ std::string sectordir1 = getSectorDir(p2d, 1);
+ std::string sectordir;
+ if(fs::PathExists(sectordir1))
+ {
+ sectordir = sectordir1;
+ }
+ else
+ {
+ loadlayout = 2;
+ sectordir = getSectorDir(p2d, 2);
+ }
+
+ //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
try{
- sector = loadSectorMeta(sectorsubdir);
+ sector = loadSectorMeta(sectordir, loadlayout != 2);
}
catch(InvalidFilenameException &e)
{
@@ -5046,7 +5401,7 @@ bool ServerMap::loadSectorFull(v2s16 p2d)
Load blocks
*/
std::vector<fs::DirListNode> list2 = fs::GetDirListing
- (m_savedir+"/sectors/"+sectorsubdir);
+ (sectordir);
std::vector<fs::DirListNode>::iterator i2;
for(i2=list2.begin(); i2!=list2.end(); i2++)
{
@@ -5054,16 +5409,25 @@ bool ServerMap::loadSectorFull(v2s16 p2d)
if(i2->dir)
continue;
try{
- loadBlock(sectorsubdir, i2->name, sector);
+ loadBlock(sectordir, i2->name, sector, loadlayout != 2);
}
catch(InvalidFilenameException &e)
{
// This catches unknown crap in directory
}
}
+
+ if(loadlayout != 2)
+ {
+ dstream<<"Sector converted to new layout - deleting "<<
+ sectordir1<<std::endl;
+ fs::RecursiveDelete(sectordir1);
+ }
+
return true;
}
+
void ServerMap::saveBlock(MapBlock *block)
{
DSTACK(__FUNCTION_NAME);
@@ -5083,12 +5447,9 @@ void ServerMap::saveBlock(MapBlock *block)
// Get destination
v3s16 p3d = block->getPos();
v2s16 p2d(p3d.X, p3d.Z);
- createDir(m_savedir);
- createDir(m_savedir+"/sectors");
std::string dir = getSectorDir(p2d);
- createDir(dir);
+ createDirs(dir);
- // Block file is map/sectors/xxxxxxxx/xxxx
char cc[5];
snprintf(cc, 5, "%.4x", (unsigned int)p3d.Y&0xffff);
std::string fullpath = dir + "/" + cc;
@@ -5102,32 +5463,27 @@ void ServerMap::saveBlock(MapBlock *block)
*/
o.write((char*)&version, 1);
+ // Write basic data
block->serialize(o, version);
-
- /*
- Versions up from 9 have block objects.
- */
- if(version >= 9)
- {
- block->serializeObjects(o, version);
- }
- // We just wrote it to the disk
+ // Write extra data stored on disk
+ block->serializeDiskExtra(o, version);
+
+ // We just wrote it to the disk so clear modified flag
block->resetChangedFlag();
}
-void ServerMap::loadBlock(std::string sectordir, std::string blockfile, MapSector *sector)
+void ServerMap::loadBlock(std::string sectordir, std::string blockfile, MapSector *sector, bool save_after_load)
{
DSTACK(__FUNCTION_NAME);
+ std::string fullpath = sectordir+"/"+blockfile;
try{
- // Block file is map/sectors/xxxxxxxx/xxxx
- std::string fullpath = m_savedir+"/sectors/"+sectordir+"/"+blockfile;
std::ifstream is(fullpath.c_str(), std::ios_base::binary);
if(is.good() == false)
throw FileNotGoodException("Cannot open block file");
-
+
v3s16 p3d = getBlockPos(sectordir, blockfile);
v2s16 p2d(p3d.X, p3d.Z);
@@ -5158,26 +5514,21 @@ void ServerMap::loadBlock(std::string sectordir, std::string blockfile, MapSecto
created_new = true;
}
- // deserialize block data
+ // Read basic data
block->deSerialize(is, version);
-
- /*
- Versions up from 9 have block objects.
- */
- if(version >= 9)
- {
- block->updateObjects(is, version, NULL, 0);
- }
+ // Read extra data stored on disk
+ block->deSerializeDiskExtra(is, version);
+
+ // If it's a new block, insert it to the map
if(created_new)
sector->insertBlock(block);
/*
- Convert old formats to new and save
+ Save blocks loaded in old format in new format
*/
- // Save old format blocks in new format
- if(version < SER_FMT_VER_HIGHEST)
+ if(version < SER_FMT_VER_HIGHEST || save_after_load)
{
saveBlock(block);
}
@@ -5192,6 +5543,8 @@ void ServerMap::loadBlock(std::string sectordir, std::string blockfile, MapSecto
"(SerializationError). Ignoring. "
"A new one will be generated."
<<std::endl;
+
+ // TODO: Backup file; name is in fullpath.
}
}
@@ -5253,7 +5606,7 @@ MapSector * ClientMap::emergeSector(v2s16 p2d)
ClientMapSector *sector = new ClientMapSector(this, p2d);
{
- JMutexAutoLock lock(m_sector_mutex);
+ //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
m_sectors.insert(p2d, sector);
}
@@ -5265,7 +5618,7 @@ void ClientMap::deSerializeSector(v2s16 p2d, std::istream &is)
DSTACK(__FUNCTION_NAME);
ClientMapSector *sector = NULL;
- JMutexAutoLock lock(m_sector_mutex);
+ //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
core::map<v2s16, MapSector*>::Node *n = m_sectors.find(p2d);
@@ -5278,7 +5631,7 @@ void ClientMap::deSerializeSector(v2s16 p2d, std::istream &is)
{
sector = new ClientMapSector(this, p2d);
{
- JMutexAutoLock lock(m_sector_mutex);
+ //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
m_sectors.insert(p2d, sector);
}
}
@@ -5312,7 +5665,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
*/
int time1 = time(0);
- u32 daynight_ratio = m_client->getDayNightRatio();
+ //u32 daynight_ratio = m_client->getDayNightRatio();
m_camera_mutex.Lock();
v3f camera_position = m_camera_position;
@@ -5339,9 +5692,9 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
p_nodes_min.Y / MAP_BLOCKSIZE - 1,
p_nodes_min.Z / MAP_BLOCKSIZE - 1);
v3s16 p_blocks_max(
- p_nodes_max.X / MAP_BLOCKSIZE + 1,
- p_nodes_max.Y / MAP_BLOCKSIZE + 1,
- p_nodes_max.Z / MAP_BLOCKSIZE + 1);
+ p_nodes_max.X / MAP_BLOCKSIZE,
+ p_nodes_max.Y / MAP_BLOCKSIZE,
+ p_nodes_max.Z / MAP_BLOCKSIZE);
u32 vertex_count = 0;
@@ -5415,7 +5768,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
continue;
}
- // This is ugly
+ // This is ugly (spherical distance limit?)
/*if(m_control.range_all == false &&
d - 0.5*BS*MAP_BLOCKSIZE > range)
continue;*/
@@ -5423,6 +5776,9 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
#if 1
/*
Update expired mesh (used for day/night change)
+
+ It doesn't work exactly like it should now with the
+ tasked mesh update but whatever.
*/
bool mesh_expired = false;
@@ -5459,28 +5815,12 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
mesh_update_count++;
// Mesh has been expired: generate new mesh
- //block->updateMeshes(daynight_i);
- block->updateMesh(daynight_ratio);
+ //block->updateMesh(daynight_ratio);
+ m_client->addUpdateMeshTask(block->getPos());
mesh_expired = false;
}
- /*
- Don't draw an expired mesh that is far away
- */
- /*if(mesh_expired && d >= faraway)
- //if(mesh_expired)
- {
- // Instead, delete it
- JMutexAutoLock lock(block->mesh_mutex);
- if(block->mesh)
- {
- block->mesh->drop();
- block->mesh = NULL;
- }
- // And continue to next block
- continue;
- }*/
#endif
/*
Draw the faces of the block
@@ -5668,6 +6008,7 @@ void ClientMap::updateMeshes(v3s16 blockpos, u32 daynight_ratio)
v3s16 p = blockpos + v3s16(0,0,0);
MapBlock *b = getBlockNoCreate(p);
b->updateMesh(daynight_ratio);
+ //b->setMeshExpired(true);
}
catch(InvalidPositionException &e){}
// Leading edge
@@ -5675,40 +6016,61 @@ void ClientMap::updateMeshes(v3s16 blockpos, u32 daynight_ratio)
v3s16 p = blockpos + v3s16(-1,0,0);
MapBlock *b = getBlockNoCreate(p);
b->updateMesh(daynight_ratio);
+ //b->setMeshExpired(true);
}
catch(InvalidPositionException &e){}
try{
v3s16 p = blockpos + v3s16(0,-1,0);
MapBlock *b = getBlockNoCreate(p);
b->updateMesh(daynight_ratio);
+ //b->setMeshExpired(true);
}
catch(InvalidPositionException &e){}
try{
v3s16 p = blockpos + v3s16(0,0,-1);
MapBlock *b = getBlockNoCreate(p);
b->updateMesh(daynight_ratio);
+ //b->setMeshExpired(true);
}
catch(InvalidPositionException &e){}
- /*// Trailing edge
- try{
- v3s16 p = blockpos + v3s16(1,0,0);
- MapBlock *b = getBlockNoCreate(p);
- b->updateMesh(daynight_ratio);
- }
- catch(InvalidPositionException &e){}
- try{
- v3s16 p = blockpos + v3s16(0,1,0);
- MapBlock *b = getBlockNoCreate(p);
- b->updateMesh(daynight_ratio);
- }
- catch(InvalidPositionException &e){}
- try{
- v3s16 p = blockpos + v3s16(0,0,1);
- MapBlock *b = getBlockNoCreate(p);
+}
+
+#if 0
+/*
+ Update mesh of block in which the node is, and if the node is at the
+ leading edge, update the appropriate leading blocks too.
+*/
+void ClientMap::updateNodeMeshes(v3s16 nodepos, u32 daynight_ratio)
+{
+ v3s16 dirs[4] = {
+ v3s16(0,0,0),
+ v3s16(-1,0,0),
+ v3s16(0,-1,0),
+ v3s16(0,0,-1),
+ };
+ v3s16 blockposes[4];
+ for(u32 i=0; i<4; i++)
+ {
+ v3s16 np = nodepos + dirs[i];
+ blockposes[i] = getNodeBlockPos(np);
+ // Don't update mesh of block if it has been done already
+ bool already_updated = false;
+ for(u32 j=0; j<i; j++)
+ {
+ if(blockposes[j] == blockposes[i])
+ {
+ already_updated = true;
+ break;
+ }
+ }
+ if(already_updated)
+ continue;
+ // Update mesh
+ MapBlock *b = getBlockNoCreate(blockposes[i]);
b->updateMesh(daynight_ratio);
}
- catch(InvalidPositionException &e){}*/
}
+#endif
void ClientMap::PrintInfo(std::ostream &out)
{
@@ -5867,7 +6229,8 @@ void MapVoxelManipulator::blitBack
}
ManualMapVoxelManipulator::ManualMapVoxelManipulator(Map *map):
- MapVoxelManipulator(map)
+ MapVoxelManipulator(map),
+ m_create_area(false)
{
}
diff --git a/src/map.h b/src/map.h
index 1cebef634..6f7ac0d3b 100644
--- a/src/map.h
+++ b/src/map.h
@@ -1,6 +1,6 @@
/*
Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -39,14 +39,21 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "constants.h"
#include "voxel.h"
#include "mapchunk.h"
+#include "nodemetadata.h"
#define MAPTYPE_BASE 0
#define MAPTYPE_SERVER 1
#define MAPTYPE_CLIENT 2
enum MapEditEventType{
+ // Node added (changed from air or something else to something)
MEET_ADDNODE,
+ // Node removed (changed to air)
MEET_REMOVENODE,
+ // Node metadata of block changed (not knowing which node exactly)
+ // p stores block coordinate
+ MEET_BLOCK_NODE_METADATA_CHANGED,
+ // Anything else
MEET_OTHER
};
@@ -277,6 +284,22 @@ public:
void transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks);
/*
+ Node metadata
+ These are basically coordinate wrappers to MapBlock
+ */
+
+ NodeMetadata* getNodeMetadata(v3s16 p);
+ void setNodeMetadata(v3s16 p, NodeMetadata *meta);
+ void removeNodeMetadata(v3s16 p);
+ void nodeMetadataStep(float dtime,
+ core::map<v3s16, MapBlock*> &changed_blocks);
+
+ /*
+ Misc.
+ */
+ core::map<v2s16, MapSector*> *getSectorsPtr(){return &m_sectors;}
+
+ /*
Variables
*/
@@ -286,16 +309,13 @@ protected:
core::map<MapEventReceiver*, bool> m_event_receivers;
- // Mutex is important because on client map is accessed asynchronously
core::map<v2s16, MapSector*> m_sectors;
- JMutex m_sector_mutex;
+ //JMutex m_sector_mutex;
// Be sure to set this to NULL when the cached sector is deleted
MapSector *m_sector_cache;
v2s16 m_sector_cache_p;
- //WrapperHeightmap m_hwrapper;
-
// Queued transforming water nodes
UniqueQueue<v3s16> m_transforming_liquid;
};
@@ -306,6 +326,8 @@ protected:
This is the only map class that is able to generate map.
*/
+struct ChunkMakeData;
+
class ServerMap : public Map
{
public:
@@ -327,6 +349,8 @@ public:
// Returns the position of the chunk where the sector is in
v2s16 sector_to_chunk(v2s16 sectorpos)
{
+ if(m_chunksize == 0)
+ return v2s16(0,0);
sectorpos.X += m_chunksize / 2;
sectorpos.Y += m_chunksize / 2;
v2s16 chunkpos = getContainerPos(sectorpos, m_chunksize);
@@ -336,6 +360,8 @@ public:
// Returns the position of the (0,0) sector of the chunk
v2s16 chunk_to_sector(v2s16 chunkpos)
{
+ if(m_chunksize == 0)
+ return v2s16(0,0);
v2s16 sectorpos(
chunkpos.X * m_chunksize,
chunkpos.Y * m_chunksize
@@ -364,6 +390,9 @@ public:
*/
bool chunkNonVolatile(v2s16 chunkpos)
{
+ if(m_chunksize == 0)
+ return true;
+
/*for(s16 x=-1; x<=1; x++)
for(s16 y=-1; y<=1; y++)*/
s16 x=0;
@@ -378,22 +407,58 @@ public:
}
return true;
}
+
+ /*
+ Returns true if any chunk is marked as modified
+ */
+ bool anyChunkModified()
+ {
+ for(core::map<v2s16, MapChunk*>::Iterator
+ i = m_chunks.getIterator();
+ i.atEnd()==false; i++)
+ {
+ v2s16 p = i.getNode()->getKey();
+ MapChunk *chunk = i.getNode()->getValue();
+ if(chunk->isModified())
+ return true;
+ }
+ return false;
+ }
+
+ void setChunksNonModified()
+ {
+ for(core::map<v2s16, MapChunk*>::Iterator
+ i = m_chunks.getIterator();
+ i.atEnd()==false; i++)
+ {
+ v2s16 p = i.getNode()->getKey();
+ MapChunk *chunk = i.getNode()->getValue();
+ chunk->setModified(false);
+ }
+ }
+
+ /*
+ Chunks are generated by using these and makeChunk().
+ */
+ void initChunkMake(ChunkMakeData &data, v2s16 chunkpos);
+ MapChunk* finishChunkMake(ChunkMakeData &data,
+ core::map<v3s16, MapBlock*> &changed_blocks);
/*
Generate a chunk.
All chunks touching this one can be altered also.
*/
- MapChunk* generateChunkRaw(v2s16 chunkpos,
+ /*MapChunk* generateChunkRaw(v2s16 chunkpos,
core::map<v3s16, MapBlock*> &changed_blocks,
- bool force=false);
+ bool force=false);*/
/*
Generate a chunk and its neighbors so that it won't be touched
anymore.
*/
- MapChunk* generateChunk(v2s16 chunkpos,
- core::map<v3s16, MapBlock*> &changed_blocks);
+ /*MapChunk* generateChunk(v2s16 chunkpos,
+ core::map<v3s16, MapBlock*> &changed_blocks);*/
/*
Generate a sector.
@@ -416,14 +481,14 @@ public:
- Check disk (loads blocks also)
- Generate chunk
*/
- MapSector * emergeSector(v2s16 p,
- core::map<v3s16, MapBlock*> &changed_blocks);
+ /*MapSector * emergeSector(v2s16 p,
+ core::map<v3s16, MapBlock*> &changed_blocks);*/
- MapSector * emergeSector(v2s16 p)
+ /*MapSector * emergeSector(v2s16 p)
{
core::map<v3s16, MapBlock*> changed_blocks;
return emergeSector(p, changed_blocks);
- }
+ }*/
MapBlock * generateBlock(
v3s16 p,
@@ -486,19 +551,15 @@ public:
Misc. helper functions for fiddling with directory and file
names when saving
*/
- void createDir(std::string path);
- void createSaveDir();
- // returns something like "xxxxxxxx"
- std::string getSectorSubDir(v2s16 pos);
+ void createDirs(std::string path);
// returns something like "map/sectors/xxxxxxxx"
- std::string getSectorDir(v2s16 pos);
- std::string createSectorDir(v2s16 pos);
+ std::string getSectorDir(v2s16 pos, int layout = 2);
// dirname: final directory name
v2s16 getSectorPos(std::string dirname);
v3s16 getBlockPos(std::string sectordir, std::string blockfile);
void save(bool only_changed);
- void loadAll();
+ //void loadAll();
// Saves map seed and possibly other stuff
void saveMapMeta();
@@ -513,7 +574,7 @@ public:
// (no MapBlocks)
// DEPRECATED? Sectors have no metadata anymore.
void saveSectorMeta(ServerMapSector *sector);
- MapSector* loadSectorMeta(std::string dirname);
+ MapSector* loadSectorMeta(std::string dirname, bool save_after_load);
// Full load of a sector including all blocks.
// returns true on success, false on failure.
@@ -524,7 +585,7 @@ public:
void saveBlock(MapBlock *block);
// This will generate a sector with getSector if not found.
- void loadBlock(std::string sectordir, std::string blockfile, MapSector *sector);
+ void loadBlock(std::string sectordir, std::string blockfile, MapSector *sector, bool save_after_load=false);
// For debug printing
virtual void PrintInfo(std::ostream &out);
@@ -539,9 +600,16 @@ private:
bool m_map_saving_enabled;
// Chunk size in MapSectors
+ // If 0, chunks are disabled.
s16 m_chunksize;
// Chunks
core::map<v2s16, MapChunk*> m_chunks;
+
+ /*
+ Metadata is re-written on disk only if this is true.
+ This is reset to false when written on disk.
+ */
+ bool m_map_metadata_changed;
};
/*
@@ -719,6 +787,9 @@ class ManualMapVoxelManipulator : public MapVoxelManipulator
public:
ManualMapVoxelManipulator(Map *map);
virtual ~ManualMapVoxelManipulator();
+
+ void setMap(Map *map)
+ {m_map = map;}
virtual void emerge(VoxelArea a, s32 caller_id=-1);
@@ -731,5 +802,29 @@ protected:
bool m_create_area;
};
+struct ChunkMakeData
+{
+ bool no_op;
+ ManualMapVoxelManipulator vmanip;
+ u64 seed;
+ v2s16 chunkpos;
+ s16 y_blocks_min;
+ s16 y_blocks_max;
+ v2s16 sectorpos_base;
+ s16 sectorpos_base_size;
+ v2s16 sectorpos_bigbase;
+ s16 sectorpos_bigbase_size;
+ s16 max_spread_amount;
+ UniqueQueue<v3s16> transforming_liquid;
+
+ ChunkMakeData():
+ no_op(false),
+ vmanip(NULL),
+ seed(0)
+ {}
+};
+
+void makeChunk(ChunkMakeData *data);
+
#endif
diff --git a/src/mapblock.cpp b/src/mapblock.cpp
index d489ec8ac..e31596b9c 100644
--- a/src/mapblock.cpp
+++ b/src/mapblock.cpp
@@ -19,116 +19,69 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mapblock.h"
#include "map.h"
-// For g_settings and g_irrlicht
+// For g_settings
#include "main.h"
#include "light.h"
#include <sstream>
-
-/*
- MapBlock
-*/
-
-MapBlock::MapBlock(NodeContainer *parent, v3s16 pos, bool dummy):
- m_parent(parent),
- m_pos(pos),
- changed(true),
- is_underground(false),
- m_day_night_differs(false),
- m_objects(this)
-{
- data = NULL;
- if(dummy == false)
- reallocate();
-
- m_spawn_timer = -10000;
-
#ifndef SERVER
- m_mesh_expired = false;
- mesh_mutex.Init();
- mesh = NULL;
- m_temp_mods_mutex.Init();
-#endif
-}
-
-MapBlock::~MapBlock()
+void MeshMakeData::fill(u32 daynight_ratio, MapBlock *block)
{
-#ifndef SERVER
- {
- JMutexAutoLock lock(mesh_mutex);
-
- if(mesh)
- {
- mesh->drop();
- mesh = NULL;
- }
- }
-#endif
+ m_daynight_ratio = daynight_ratio;
+ m_blockpos = block->getPos();
- if(data)
- delete[] data;
-}
+ v3s16 blockpos_nodes = m_blockpos*MAP_BLOCKSIZE;
+
+ /*
+ There is no harm not copying the TempMods of the neighbors
+ because they are already copied to this block
+ */
+ m_temp_mods.clear();
+ block->copyTempMods(m_temp_mods);
+
+ /*
+ Copy data
+ */
-bool MapBlock::isValidPositionParent(v3s16 p)
-{
- if(isValidPosition(p))
- {
- return true;
- }
- else{
- return m_parent->isValidPosition(getPosRelative() + p);
- }
-}
+ // Allocate this block + neighbors
+ m_vmanip.clear();
+ m_vmanip.addArea(VoxelArea(blockpos_nodes-v3s16(1,1,1)*MAP_BLOCKSIZE,
+ blockpos_nodes+v3s16(1,1,1)*MAP_BLOCKSIZE*2-v3s16(1,1,1)));
-MapNode MapBlock::getNodeParent(v3s16 p)
-{
- if(isValidPosition(p) == false)
- {
- return m_parent->getNode(getPosRelative() + p);
- }
- else
{
- if(data == NULL)
- throw InvalidPositionException();
- return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X];
- }
-}
+ //TimeTaker timer("copy central block data");
+ // 0ms
-void MapBlock::setNodeParent(v3s16 p, MapNode & n)
-{
- if(isValidPosition(p) == false)
- {
- m_parent->setNode(getPosRelative() + p, n);
+ // Copy our data
+ block->copyTo(m_vmanip);
}
- else
{
- if(data == NULL)
- throw InvalidPositionException();
- data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X] = n;
- }
-}
+ //TimeTaker timer("copy neighbor block data");
+ // 0ms
-MapNode MapBlock::getNodeParentNoEx(v3s16 p)
-{
- if(isValidPosition(p) == false)
- {
- try{
- return m_parent->getNode(getPosRelative() + p);
- }
- catch(InvalidPositionException &e)
- {
- return MapNode(CONTENT_IGNORE);
- }
- }
- else
- {
- if(data == NULL)
+ /*
+ Copy neighbors. This is lightning fast.
+ Copying only the borders would be *very* slow.
+ */
+
+ // Get map
+ NodeContainer *parentcontainer = block->getParent();
+ // This will only work if the parent is the map
+ assert(parentcontainer->nodeContainerId() == NODECONTAINER_ID_MAP);
+ // OK, we have the map!
+ Map *map = (Map*)parentcontainer;
+
+ for(u16 i=0; i<6; i++)
{
- return MapNode(CONTENT_IGNORE);
+ const v3s16 &dir = g_6dirs[i];
+ v3s16 bp = m_blockpos + dir;
+ MapBlock *b = map->getBlockNoCreateNoEx(bp);
+ if(b)
+ b->copyTo(m_vmanip);
}
- return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X];
}
}
+#endif
/*
Parameters must consist of air and !air.
@@ -144,7 +97,7 @@ MapNode MapBlock::getNodeParentNoEx(v3s16 p)
returns encoded light value.
*/
-u8 MapBlock::getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
+u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
v3s16 face_dir)
{
try{
@@ -180,53 +133,94 @@ u8 MapBlock::getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
#ifndef SERVER
-void MapBlock::makeFastFace(TileSpec tile, u8 light, v3f p,
- v3s16 dir, v3f scale, v3f posRelative_f,
- core::array<FastFace> &dest)
+/*
+ vertex_dirs: v3s16[4]
+*/
+void getNodeVertexDirs(v3s16 dir, v3s16 *vertex_dirs)
{
- FastFace face;
-
- // Position is at the center of the cube.
- v3f pos = p * BS;
- posRelative_f *= BS;
-
- v3f vertex_pos[4];
- // If looking towards z+, this is the face that is behind
- // the center point, facing towards z+.
- vertex_pos[0] = v3f(-BS/2,-BS/2,BS/2);
- vertex_pos[1] = v3f( BS/2,-BS/2,BS/2);
- vertex_pos[2] = v3f( BS/2, BS/2,BS/2);
- vertex_pos[3] = v3f(-BS/2, BS/2,BS/2);
-
+ /*
+ If looked from outside the node towards the face, the corners are:
+ 0: bottom-right
+ 1: bottom-left
+ 2: top-left
+ 3: top-right
+ */
if(dir == v3s16(0,0,1))
{
- for(u16 i=0; i<4; i++)
- vertex_pos[i].rotateXZBy(0);
+ // If looking towards z+, this is the face that is behind
+ // the center point, facing towards z+.
+ vertex_dirs[0] = v3s16(-1,-1, 1);
+ vertex_dirs[1] = v3s16( 1,-1, 1);
+ vertex_dirs[2] = v3s16( 1, 1, 1);
+ vertex_dirs[3] = v3s16(-1, 1, 1);
}
else if(dir == v3s16(0,0,-1))
{
- for(u16 i=0; i<4; i++)
- vertex_pos[i].rotateXZBy(180);
+ // faces towards Z-
+ vertex_dirs[0] = v3s16( 1,-1,-1);
+ vertex_dirs[1] = v3s16(-1,-1,-1);
+ vertex_dirs[2] = v3s16(-1, 1,-1);
+ vertex_dirs[3] = v3s16( 1, 1,-1);
}
else if(dir == v3s16(1,0,0))
{
- for(u16 i=0; i<4; i++)
- vertex_pos[i].rotateXZBy(-90);
+ // faces towards X+
+ vertex_dirs[0] = v3s16( 1,-1, 1);
+ vertex_dirs[1] = v3s16( 1,-1,-1);
+ vertex_dirs[2] = v3s16( 1, 1,-1);
+ vertex_dirs[3] = v3s16( 1, 1, 1);
}
else if(dir == v3s16(-1,0,0))
{
- for(u16 i=0; i<4; i++)
- vertex_pos[i].rotateXZBy(90);
+ // faces towards X-
+ vertex_dirs[0] = v3s16(-1,-1,-1);
+ vertex_dirs[1] = v3s16(-1,-1, 1);
+ vertex_dirs[2] = v3s16(-1, 1, 1);
+ vertex_dirs[3] = v3s16(-1, 1,-1);
}
else if(dir == v3s16(0,1,0))
{
- for(u16 i=0; i<4; i++)
- vertex_pos[i].rotateYZBy(-90);
+ // faces towards Y+ (assume Z- as "down" in texture)
+ vertex_dirs[0] = v3s16( 1, 1,-1);
+ vertex_dirs[1] = v3s16(-1, 1,-1);
+ vertex_dirs[2] = v3s16(-1, 1, 1);
+ vertex_dirs[3] = v3s16( 1, 1, 1);
}
else if(dir == v3s16(0,-1,0))
{
- for(u16 i=0; i<4; i++)
- vertex_pos[i].rotateYZBy(90);
+ // faces towards Y- (assume Z+ as "down" in texture)
+ vertex_dirs[0] = v3s16( 1,-1, 1);
+ vertex_dirs[1] = v3s16(-1,-1, 1);
+ vertex_dirs[2] = v3s16(-1,-1,-1);
+ vertex_dirs[3] = v3s16( 1,-1,-1);
+ }
+}
+
+inline video::SColor lightColor(u8 alpha, u8 light)
+{
+ return video::SColor(alpha,light,light,light);
+}
+
+void makeFastFace(TileSpec tile, u8 li0, u8 li1, u8 li2, u8 li3, v3f p,
+ v3s16 dir, v3f scale, v3f posRelative_f,
+ core::array<FastFace> &dest)
+{
+ FastFace face;
+
+ // Position is at the center of the cube.
+ v3f pos = p * BS;
+ posRelative_f *= BS;
+
+ v3f vertex_pos[4];
+ v3s16 vertex_dirs[4];
+ getNodeVertexDirs(dir, vertex_dirs);
+ for(u16 i=0; i<4; i++)
+ {
+ vertex_pos[i] = v3f(
+ BS/2*vertex_dirs[i].X,
+ BS/2*vertex_dirs[i].Y,
+ BS/2*vertex_dirs[i].Z
+ );
}
for(u16 i=0; i<4; i++)
@@ -244,22 +238,18 @@ void MapBlock::makeFastFace(TileSpec tile, u8 light, v3f p,
v3f zerovector = v3f(0,0,0);
- //u8 li = decode_light(light);
- u8 li = light;
- //u8 li = 255; //DEBUG
-
u8 alpha = tile.alpha;
/*u8 alpha = 255;
if(tile.id == TILE_WATER)
alpha = WATER_ALPHA;*/
- video::SColor c = video::SColor(alpha,li,li,li);
-
float x0 = tile.texture.pos.X;
float y0 = tile.texture.pos.Y;
float w = tile.texture.size.X;
float h = tile.texture.size.Y;
+ /*video::SColor c = lightColor(alpha, li);
+
face.vertices[0] = video::S3DVertex(vertex_pos[0], v3f(0,1,0), c,
core::vector2d<f32>(x0+w*abs_scale, y0+h));
face.vertices[1] = video::S3DVertex(vertex_pos[1], v3f(0,1,0), c,
@@ -267,6 +257,19 @@ void MapBlock::makeFastFace(TileSpec tile, u8 light, v3f p,
face.vertices[2] = video::S3DVertex(vertex_pos[2], v3f(0,1,0), c,
core::vector2d<f32>(x0, y0));
face.vertices[3] = video::S3DVertex(vertex_pos[3], v3f(0,1,0), c,
+ core::vector2d<f32>(x0+w*abs_scale, y0));*/
+
+ face.vertices[0] = video::S3DVertex(vertex_pos[0], v3f(0,1,0),
+ lightColor(alpha, li0),
+ core::vector2d<f32>(x0+w*abs_scale, y0+h));
+ face.vertices[1] = video::S3DVertex(vertex_pos[1], v3f(0,1,0),
+ lightColor(alpha, li1),
+ core::vector2d<f32>(x0, y0+h));
+ face.vertices[2] = video::S3DVertex(vertex_pos[2], v3f(0,1,0),
+ lightColor(alpha, li2),
+ core::vector2d<f32>(x0, y0));
+ face.vertices[3] = video::S3DVertex(vertex_pos[3], v3f(0,1,0),
+ lightColor(alpha, li3),
core::vector2d<f32>(x0+w*abs_scale, y0));
face.tile = tile;
@@ -274,14 +277,13 @@ void MapBlock::makeFastFace(TileSpec tile, u8 light, v3f p,
//f->tile = TILE_STONE;
dest.push_back(face);
- //return f;
}
/*
Gets node tile from any place relative to block.
Returns TILE_NODE if doesn't exist or should not be drawn.
*/
-TileSpec MapBlock::getNodeTile(MapNode mn, v3s16 p, v3s16 face_dir,
+TileSpec getNodeTile(MapNode mn, v3s16 p, v3s16 face_dir,
NodeModMap &temp_mods)
{
TileSpec spec;
@@ -333,7 +335,7 @@ TileSpec MapBlock::getNodeTile(MapNode mn, v3s16 p, v3s16 face_dir,
return spec;
}
-u8 MapBlock::getNodeContent(v3s16 p, MapNode mn, NodeModMap &temp_mods)
+u8 getNodeContent(v3s16 p, MapNode mn, NodeModMap &temp_mods)
{
/*
Check temporary modifications on this node
@@ -371,12 +373,139 @@ u8 MapBlock::getNodeContent(v3s16 p, MapNode mn, NodeModMap &temp_mods)
return mn.d;
}
+v3s16 dirs8[8] = {
+ v3s16(0,0,0),
+ v3s16(0,0,1),
+ v3s16(0,1,0),
+ v3s16(0,1,1),
+ v3s16(1,0,0),
+ v3s16(1,1,0),
+ v3s16(1,0,1),
+ v3s16(1,1,1),
+};
+
+// Calculate lighting at the XYZ- corner of p
+u8 getSmoothLight(v3s16 p, VoxelManipulator &vmanip, u32 daynight_ratio)
+{
+ u16 ambient_occlusion = 0;
+ u16 light = 0;
+ u16 light_count = 0;
+ for(u32 i=0; i<8; i++)
+ {
+ MapNode n = vmanip.getNodeNoEx(p - dirs8[i]);
+ if(content_features(n.d).param_type == CPT_LIGHT)
+ {
+ light += decode_light(n.getLightBlend(daynight_ratio));
+ light_count++;
+ }
+ else
+ {
+ if(n.d != CONTENT_IGNORE)
+ ambient_occlusion++;
+ }
+ }
+
+ if(light_count == 0)
+ return 255;
+
+ light /= light_count;
+
+ if(ambient_occlusion > 4)
+ {
+ ambient_occlusion -= 4;
+ light = (float)light / ((float)ambient_occlusion * 0.5 + 1.0);
+ }
+
+ return light;
+}
+
+// Calculate lighting at the given corner of p
+u8 getSmoothLight(v3s16 p, v3s16 corner,
+ VoxelManipulator &vmanip, u32 daynight_ratio)
+{
+ if(corner.X == 1) p.X += 1;
+ else assert(corner.X == -1);
+ if(corner.Y == 1) p.Y += 1;
+ else assert(corner.Y == -1);
+ if(corner.Z == 1) p.Z += 1;
+ else assert(corner.Z == -1);
+
+ return getSmoothLight(p, vmanip, daynight_ratio);
+}
+
+void getTileInfo(
+ // Input:
+ v3s16 blockpos_nodes,
+ v3s16 p,
+ v3s16 face_dir,
+ u32 daynight_ratio,
+ VoxelManipulator &vmanip,
+ NodeModMap &temp_mods,
+ bool smooth_lighting,
+ // Output:
+ bool &makes_face,
+ v3s16 &p_corrected,
+ v3s16 &face_dir_corrected,
+ u8 *lights,
+ TileSpec &tile
+ )
+{
+ MapNode n0 = vmanip.getNodeNoEx(blockpos_nodes + p);
+ MapNode n1 = vmanip.getNodeNoEx(blockpos_nodes + p + face_dir);
+ TileSpec tile0 = getNodeTile(n0, p, face_dir, temp_mods);
+ TileSpec tile1 = getNodeTile(n1, p + face_dir, -face_dir, temp_mods);
+
+ // This is hackish
+ u8 content0 = getNodeContent(p, n0, temp_mods);
+ u8 content1 = getNodeContent(p + face_dir, n1, temp_mods);
+ u8 mf = face_contents(content0, content1);
+
+ if(mf == 0)
+ {
+ makes_face = false;
+ return;
+ }
+
+ makes_face = true;
+
+ if(mf == 1)
+ {
+ tile = tile0;
+ p_corrected = p;
+ face_dir_corrected = face_dir;
+ }
+ else
+ {
+ tile = tile1;
+ p_corrected = p + face_dir;
+ face_dir_corrected = -face_dir;
+ }
+
+ if(smooth_lighting == false)
+ {
+ lights[0] = lights[1] = lights[2] = lights[3] =
+ decode_light(getFaceLight(daynight_ratio, n0, n1, face_dir));
+ }
+ else
+ {
+ v3s16 vertex_dirs[4];
+ getNodeVertexDirs(face_dir_corrected, vertex_dirs);
+ for(u16 i=0; i<4; i++)
+ {
+ lights[i] = getSmoothLight(blockpos_nodes + p_corrected,
+ vertex_dirs[i], vmanip, daynight_ratio);
+ }
+ }
+
+ return;
+}
+
/*
startpos:
translate_dir: unit vector with only one of x, y or z
face_dir: unit vector with only one of x, y or z
*/
-void MapBlock::updateFastFaceRow(
+void updateFastFaceRow(
u32 daynight_ratio,
v3f posRelative_f,
v3s16 startpos,
@@ -386,45 +515,57 @@ void MapBlock::updateFastFaceRow(
v3s16 face_dir,
v3f face_dir_f,
core::array<FastFace> &dest,
- NodeModMap &temp_mods)
+ NodeModMap &temp_mods,
+ VoxelManipulator &vmanip,
+ v3s16 blockpos_nodes,
+ bool smooth_lighting)
{
v3s16 p = startpos;
u16 continuous_tiles_count = 0;
- MapNode n0 = getNodeParentNoEx(p);
- MapNode n1 = getNodeParentNoEx(p + face_dir);
-
- u8 light = getFaceLight(daynight_ratio, n0, n1, face_dir);
-
- TileSpec tile0 = getNodeTile(n0, p, face_dir, temp_mods);
- TileSpec tile1 = getNodeTile(n1, p + face_dir, -face_dir, temp_mods);
+ bool makes_face;
+ v3s16 p_corrected;
+ v3s16 face_dir_corrected;
+ u8 lights[4];
+ TileSpec tile;
+ getTileInfo(blockpos_nodes, p, face_dir, daynight_ratio,
+ vmanip, temp_mods, smooth_lighting,
+ makes_face, p_corrected, face_dir_corrected, lights, tile);
for(u16 j=0; j<length; j++)
{
+ // If tiling can be done, this is set to false in the next step
bool next_is_different = true;
v3s16 p_next;
- MapNode n0_next;
- MapNode n1_next;
- TileSpec tile0_next;
- TileSpec tile1_next;
- u8 light_next = 0;
+
+ bool next_makes_face = false;
+ v3s16 next_p_corrected;
+ v3s16 next_face_dir_corrected;
+ u8 next_lights[4] = {0,0,0,0};
+ TileSpec next_tile;
// If at last position, there is nothing to compare to and
// the face must be drawn anyway
if(j != length - 1)
{
p_next = p + translate_dir;
- n0_next = getNodeParentNoEx(p_next);
- n1_next = getNodeParentNoEx(p_next + face_dir);
- tile0_next = getNodeTile(n0_next, p_next, face_dir, temp_mods);
- tile1_next = getNodeTile(n1_next,p_next+face_dir,-face_dir, temp_mods);
- light_next = getFaceLight(daynight_ratio, n0_next, n1_next, face_dir);
-
- if(tile0_next == tile0
- && tile1_next == tile1
- && light_next == light)
+
+ getTileInfo(blockpos_nodes, p_next, face_dir, daynight_ratio,
+ vmanip, temp_mods, smooth_lighting,
+ next_makes_face, next_p_corrected,
+ next_face_dir_corrected, next_lights,
+ next_tile);
+
+ if(next_makes_face == makes_face
+ && next_p_corrected == p_corrected
+ && next_face_dir_corrected == face_dir_corrected
+ && next_lights[0] == lights[0]
+ && next_lights[1] == lights[1]
+ && next_lights[2] == lights[2]
+ && next_lights[3] == lights[3]
+ && next_tile == tile)
{
next_is_different = false;
}
@@ -438,22 +579,14 @@ void MapBlock::updateFastFaceRow(
If there is no texture, it can be tiled infinitely.
If tiled==0, it means the texture can be tiled infinitely.
Otherwise check tiled agains continuous_tiles_count.
-
- This check has to be made for both tiles, because this is
- a bit hackish and we know which one we're using only when
- the decision to make the faces is made.
*/
- if(tile0.texture.atlas != NULL && tile0.texture.tiled != 0)
+ if(tile.texture.atlas != NULL && tile.texture.tiled != 0)
{
- if(tile0.texture.tiled <= continuous_tiles_count)
- end_of_texture = true;
- }
- if(tile1.texture.atlas != NULL && tile1.texture.tiled != 0)
- {
- if(tile1.texture.tiled <= continuous_tiles_count)
+ if(tile.texture.tiled <= continuous_tiles_count)
end_of_texture = true;
}
+ // Do this to disable tiling textures
//end_of_texture = true; //DEBUG
if(next_is_different || end_of_texture)
@@ -461,54 +594,42 @@ void MapBlock::updateFastFaceRow(
/*
Create a face if there should be one
*/
- //u8 mf = face_contents(tile0, tile1);
- // This is hackish
- u8 content0 = getNodeContent(p, n0, temp_mods);
- u8 content1 = getNodeContent(p + face_dir, n1, temp_mods);
- u8 mf = face_contents(content0, content1);
-
- if(mf != 0)
+ if(makes_face)
{
// Floating point conversion of the position vector
- v3f pf(p.X, p.Y, p.Z);
+ v3f pf(p_corrected.X, p_corrected.Y, p_corrected.Z);
// Center point of face (kind of)
v3f sp = pf - ((f32)continuous_tiles_count / 2. - 0.5) * translate_dir_f;
v3f scale(1,1,1);
- if(translate_dir.X != 0){
+
+ if(translate_dir.X != 0)
+ {
scale.X = continuous_tiles_count;
}
- if(translate_dir.Y != 0){
+ if(translate_dir.Y != 0)
+ {
scale.Y = continuous_tiles_count;
}
- if(translate_dir.Z != 0){
+ if(translate_dir.Z != 0)
+ {
scale.Z = continuous_tiles_count;
}
- //FastFace *f;
-
- // If node at sp (tile0) is more solid
- if(mf == 1)
- {
- makeFastFace(tile0, decode_light(light),
- sp, face_dir, scale,
- posRelative_f, dest);
- }
- // If node at sp is less solid (mf == 2)
- else
- {
- makeFastFace(tile1, decode_light(light),
- sp+face_dir_f, -face_dir, scale,
- posRelative_f, dest);
- }
- //dest.push_back(f);
+ makeFastFace(tile, lights[0], lights[1], lights[2], lights[3],
+ sp, face_dir_corrected, scale,
+ posRelative_f, dest);
}
continuous_tiles_count = 0;
- n0 = n0_next;
- n1 = n1_next;
- tile0 = tile0_next;
- tile1 = tile1_next;
- light = light_next;
+
+ makes_face = next_makes_face;
+ p_corrected = next_p_corrected;
+ face_dir_corrected = next_face_dir_corrected;
+ lights[0] = next_lights[0];
+ lights[1] = next_lights[1];
+ lights[2] = next_lights[2];
+ lights[3] = next_lights[3];
+ tile = next_tile;
}
p = p_next;
@@ -608,42 +729,118 @@ private:
core::array<PreMeshBuffer> m_prebuffers;
};
-void MapBlock::updateMesh(u32 daynight_ratio)
+// Create a cuboid.
+// material - the material to use (for all 6 faces)
+// collector - the MeshCollector for the resulting polygons
+// pa - texture atlas pointer for the material
+// c - vertex colour - used for all
+// pos - the position of the centre of the cuboid
+// rz,ry,rz - the radius of the cuboid in each dimension
+// txc - texture coordinates - this is a list of texture coordinates
+// for the opposite corners of each face - therefore, there
+// should be (2+2)*6=24 values in the list. Alternatively, pass
+// NULL to use the entire texture for each face. The order of
+// the faces in the list is top-backi-right-front-left-bottom
+// If you specified 0,0,1,1 for each face, that would be the
+// same as passing NULL.
+void makeCuboid(video::SMaterial &material, MeshCollector *collector,
+ AtlasPointer* pa, video::SColor &c,
+ v3f &pos, f32 rx, f32 ry, f32 rz, f32* txc)
{
-#if 0
- /*
- DEBUG: If mesh has been generated, don't generate it again
- */
+ f32 tu0=pa->x0();
+ f32 tu1=pa->x1();
+ f32 tv0=pa->y0();
+ f32 tv1=pa->y1();
+ f32 txus=tu1-tu0;
+ f32 txvs=tv1-tv0;
+
+ video::S3DVertex v[4] =
{
- JMutexAutoLock meshlock(mesh_mutex);
- if(mesh != NULL)
- return;
+ video::S3DVertex(0,0,0, 0,0,0, c, tu0, tv1),
+ video::S3DVertex(0,0,0, 0,0,0, c, tu1, tv1),
+ video::S3DVertex(0,0,0, 0,0,0, c, tu1, tv0),
+ video::S3DVertex(0,0,0, 0,0,0, c, tu0, tv0)
+ };
+
+ for(int i=0;i<6;i++)
+ {
+ switch(i)
+ {
+ case 0: // top
+ v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
+ v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
+ v[2].Pos.X= rx; v[2].Pos.Y= ry; v[2].Pos.Z= rz;
+ v[3].Pos.X= rx; v[3].Pos.Y= ry, v[3].Pos.Z=-rz;
+ break;
+ case 1: // back
+ v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
+ v[1].Pos.X= rx; v[1].Pos.Y= ry; v[1].Pos.Z=-rz;
+ v[2].Pos.X= rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
+ v[3].Pos.X=-rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
+ break;
+ case 2: //right
+ v[0].Pos.X= rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
+ v[1].Pos.X= rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
+ v[2].Pos.X= rx; v[2].Pos.Y=-ry; v[2].Pos.Z= rz;
+ v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
+ break;
+ case 3: // front
+ v[0].Pos.X= rx; v[0].Pos.Y= ry; v[0].Pos.Z= rz;
+ v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
+ v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z= rz;
+ v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z= rz;
+ break;
+ case 4: // left
+ v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z= rz;
+ v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z=-rz;
+ v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
+ v[3].Pos.X=-rx; v[3].Pos.Y=-ry, v[3].Pos.Z= rz;
+ break;
+ case 5: // bottom
+ v[0].Pos.X= rx; v[0].Pos.Y=-ry; v[0].Pos.Z= rz;
+ v[1].Pos.X=-rx; v[1].Pos.Y=-ry; v[1].Pos.Z= rz;
+ v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
+ v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
+ break;
+ }
+
+ if(txc!=NULL)
+ {
+ v[0].TCoords.X=tu0+txus*txc[0]; v[0].TCoords.Y=tv0+txvs*txc[3];
+ v[1].TCoords.X=tu0+txus*txc[2]; v[1].TCoords.Y=tv0+txvs*txc[3];
+ v[2].TCoords.X=tu0+txus*txc[2]; v[2].TCoords.Y=tv0+txvs*txc[1];
+ v[3].TCoords.X=tu0+txus*txc[0]; v[3].TCoords.Y=tv0+txvs*txc[1];
+ txc+=4;
+ }
+
+ for(u16 i=0; i<4; i++)
+ v[i].Pos += pos;
+ u16 indices[] = {0,1,2,2,3,0};
+ collector->append(material, v, 4, indices, 6);
+
}
-#endif
-
+
+}
+
+scene::SMesh* makeMapBlockMesh(MeshMakeData *data)
+{
// 4-21ms for MAP_BLOCKSIZE=16
// 24-155ms for MAP_BLOCKSIZE=32
- //TimeTaker timer1("updateMesh()");
+ //TimeTaker timer1("makeMapBlockMesh()");
core::array<FastFace> fastfaces_new;
+
+ v3s16 blockpos_nodes = data->m_blockpos*MAP_BLOCKSIZE;
- v3f posRelative_f(getPosRelative().X, getPosRelative().Y,
- getPosRelative().Z); // floating point conversion
-
- /*
- Avoid interlocks by copying m_temp_mods
- */
- NodeModMap temp_mods;
- {
- JMutexAutoLock lock(m_temp_mods_mutex);
- m_temp_mods.copy(temp_mods);
- }
+ // floating point conversion
+ v3f posRelative_f(blockpos_nodes.X, blockpos_nodes.Y, blockpos_nodes.Z);
/*
Some settings
*/
bool new_style_water = g_settings.getBool("new_style_water");
bool new_style_leaves = g_settings.getBool("new_style_leaves");
+ bool smooth_lighting = g_settings.getBool("smooth_lighting");
float node_water_level = 1.0;
if(new_style_water)
@@ -662,48 +859,57 @@ void MapBlock::updateMesh(u32 daynight_ratio)
//TimeTaker timer2("updateMesh() collect");
/*
- Go through every y,z and get top faces in rows of x+
+ Go through every y,z and get top(y+) faces in rows of x+
*/
for(s16 y=0; y<MAP_BLOCKSIZE; y++){
for(s16 z=0; z<MAP_BLOCKSIZE; z++){
- updateFastFaceRow(daynight_ratio, posRelative_f,
+ updateFastFaceRow(data->m_daynight_ratio, posRelative_f,
v3s16(0,y,z), MAP_BLOCKSIZE,
v3s16(1,0,0), //dir
v3f (1,0,0),
v3s16(0,1,0), //face dir
v3f (0,1,0),
fastfaces_new,
- temp_mods);
+ data->m_temp_mods,
+ data->m_vmanip,
+ blockpos_nodes,
+ smooth_lighting);
}
}
/*
- Go through every x,y and get right faces in rows of z+
+ Go through every x,y and get right(x+) faces in rows of z+
*/
for(s16 x=0; x<MAP_BLOCKSIZE; x++){
for(s16 y=0; y<MAP_BLOCKSIZE; y++){
- updateFastFaceRow(daynight_ratio, posRelative_f,
+ updateFastFaceRow(data->m_daynight_ratio, posRelative_f,
v3s16(x,y,0), MAP_BLOCKSIZE,
v3s16(0,0,1),
v3f (0,0,1),
v3s16(1,0,0),
v3f (1,0,0),
fastfaces_new,
- temp_mods);
+ data->m_temp_mods,
+ data->m_vmanip,
+ blockpos_nodes,
+ smooth_lighting);
}
}
/*
- Go through every y,z and get back faces in rows of x+
+ Go through every y,z and get back(z+) faces in rows of x+
*/
for(s16 z=0; z<MAP_BLOCKSIZE; z++){
for(s16 y=0; y<MAP_BLOCKSIZE; y++){
- updateFastFaceRow(daynight_ratio, posRelative_f,
+ updateFastFaceRow(data->m_daynight_ratio, posRelative_f,
v3s16(0,y,z), MAP_BLOCKSIZE,
v3s16(1,0,0),
v3f (1,0,0),
v3s16(0,0,1),
v3f (0,0,1),
fastfaces_new,
- temp_mods);
+ data->m_temp_mods,
+ data->m_vmanip,
+ blockpos_nodes,
+ smooth_lighting);
}
}
}
@@ -733,8 +939,8 @@ void MapBlock::updateMesh(u32 daynight_ratio)
FastFace &f = fastfaces_new[i];
const u16 indices[] = {0,1,2,2,3,0};
+ const u16 indices_alternate[] = {0,1,3,2,3,1};
- //video::ITexture *texture = g_irrlicht->getTexture(f.tile.spec);
video::ITexture *texture = f.tile.texture.atlas;
if(texture == NULL)
continue;
@@ -742,8 +948,18 @@ void MapBlock::updateMesh(u32 daynight_ratio)
material.setTexture(0, texture);
f.tile.applyMaterialOptions(material);
+
+ const u16 *indices_p = indices;
- collector.append(material, f.vertices, 4, indices, 6);
+ /*
+ Revert triangles for nicer looking gradient if vertices
+ 1 and 3 have same color or 0 and 2 have different color.
+ */
+ if(f.vertices[0].Color != f.vertices[2].Color
+ || f.vertices[1].Color == f.vertices[3].Color)
+ indices_p = indices_alternate;
+
+ collector.append(material, f.vertices, 4, indices_p, 6);
}
}
@@ -759,12 +975,10 @@ void MapBlock::updateMesh(u32 daynight_ratio)
// Flowing water material
video::SMaterial material_water1;
material_water1.setFlag(video::EMF_LIGHTING, false);
- //material_water1.setFlag(video::EMF_BACK_FACE_CULLING, false);
+ material_water1.setFlag(video::EMF_BACK_FACE_CULLING, false);
material_water1.setFlag(video::EMF_BILINEAR_FILTER, false);
material_water1.setFlag(video::EMF_FOG_ENABLE, true);
material_water1.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
- //TODO
- //material_water1.setTexture(0, g_irrlicht->getTexture("water.png"));
AtlasPointer pa_water1 = g_texturesource->getTexture(
g_texturesource->getTextureId("water.png"));
material_water1.setTexture(0, pa_water1.atlas);
@@ -776,19 +990,37 @@ void MapBlock::updateMesh(u32 daynight_ratio)
material_leaves1.setFlag(video::EMF_BILINEAR_FILTER, false);
material_leaves1.setFlag(video::EMF_FOG_ENABLE, true);
material_leaves1.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
- //TODO
- //material_leaves1.setTexture(0, g_irrlicht->getTexture("leaves.png"));
AtlasPointer pa_leaves1 = g_texturesource->getTexture(
g_texturesource->getTextureId("leaves.png"));
material_leaves1.setTexture(0, pa_leaves1.atlas);
+ // Glass material
+ video::SMaterial material_glass;
+ material_glass.setFlag(video::EMF_LIGHTING, false);
+ material_glass.setFlag(video::EMF_BILINEAR_FILTER, false);
+ material_glass.setFlag(video::EMF_FOG_ENABLE, true);
+ material_glass.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
+ AtlasPointer pa_glass = g_texturesource->getTexture(
+ g_texturesource->getTextureId("glass.png"));
+ material_glass.setTexture(0, pa_glass.atlas);
+
+ // Wood material
+ video::SMaterial material_wood;
+ material_wood.setFlag(video::EMF_LIGHTING, false);
+ material_wood.setFlag(video::EMF_BILINEAR_FILTER, false);
+ material_wood.setFlag(video::EMF_FOG_ENABLE, true);
+ material_wood.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
+ AtlasPointer pa_wood = g_texturesource->getTexture(
+ g_texturesource->getTextureId("wood.png"));
+ material_wood.setTexture(0, pa_wood.atlas);
+
for(s16 z=0; z<MAP_BLOCKSIZE; z++)
for(s16 y=0; y<MAP_BLOCKSIZE; y++)
for(s16 x=0; x<MAP_BLOCKSIZE; x++)
{
v3s16 p(x,y,z);
- MapNode &n = getNodeRef(x,y,z);
+ MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes+p);
/*
Add torches to mesh
@@ -797,6 +1029,7 @@ void MapBlock::updateMesh(u32 daynight_ratio)
{
video::SColor c(255,255,255,255);
+ // Wall at X+ of node
video::S3DVertex vertices[4] =
{
video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c, 0,1),
@@ -822,7 +1055,7 @@ void MapBlock::updateMesh(u32 daynight_ratio)
if(dir == v3s16(0,1,0))
vertices[i].Pos.rotateXZBy(-45);
- vertices[i].Pos += intToFloat(p + getPosRelative(), BS);
+ vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
}
// Set material
@@ -853,18 +1086,77 @@ void MapBlock::updateMesh(u32 daynight_ratio)
collector.append(material, vertices, 4, indices, 6);
}
/*
+ Signs on walls
+ */
+ if(n.d == CONTENT_SIGN_WALL)
+ {
+ u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
+ video::SColor c(255,l,l,l);
+
+ float d = (float)BS/16;
+ // Wall at X+ of node
+ video::S3DVertex vertices[4] =
+ {
+ video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c, 0,1),
+ video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c, 1,1),
+ video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c, 1,0),
+ video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c, 0,0),
+ };
+
+ v3s16 dir = unpackDir(n.dir);
+
+ for(s32 i=0; i<4; i++)
+ {
+ if(dir == v3s16(1,0,0))
+ vertices[i].Pos.rotateXZBy(0);
+ if(dir == v3s16(-1,0,0))
+ vertices[i].Pos.rotateXZBy(180);
+ if(dir == v3s16(0,0,1))
+ vertices[i].Pos.rotateXZBy(90);
+ if(dir == v3s16(0,0,-1))
+ vertices[i].Pos.rotateXZBy(-90);
+ if(dir == v3s16(0,-1,0))
+ vertices[i].Pos.rotateXYBy(-90);
+ if(dir == v3s16(0,1,0))
+ vertices[i].Pos.rotateXYBy(90);
+
+ vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
+ }
+
+ // Set material
+ video::SMaterial material;
+ material.setFlag(video::EMF_LIGHTING, false);
+ material.setFlag(video::EMF_BACK_FACE_CULLING, false);
+ material.setFlag(video::EMF_BILINEAR_FILTER, false);
+ material.setFlag(video::EMF_FOG_ENABLE, true);
+ //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
+ material.MaterialType
+ = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
+
+ material.setTexture(0,
+ g_texturesource->getTextureRaw("sign_wall.png"));
+
+ u16 indices[] = {0,1,2,2,3,0};
+ // Add to mesh collector
+ collector.append(material, vertices, 4, indices, 6);
+ }
+ /*
Add flowing water to mesh
*/
else if(n.d == CONTENT_WATER)
{
bool top_is_water = false;
- try{
- MapNode n = getNodeParent(v3s16(x,y+1,z));
- if(n.d == CONTENT_WATER || n.d == CONTENT_WATERSOURCE)
- top_is_water = true;
- }catch(InvalidPositionException &e){}
+ MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
+ if(ntop.d == CONTENT_WATER || ntop.d == CONTENT_WATERSOURCE)
+ top_is_water = true;
- u8 l = decode_light(n.getLightBlend(daynight_ratio));
+ u8 l = 0;
+ // Use the light of the node on top if possible
+ if(content_features(ntop.d).param_type == CPT_LIGHT)
+ l = decode_light(ntop.getLightBlend(data->m_daynight_ratio));
+ // Otherwise use the light of this node (the water)
+ else
+ l = decode_light(n.getLightBlend(data->m_daynight_ratio));
video::SColor c(WATER_ALPHA,l,l,l);
// Neighbor water levels (key = relative position)
@@ -889,11 +1181,11 @@ void MapBlock::updateMesh(u32 daynight_ratio)
u8 content = CONTENT_AIR;
float level = -0.5 * BS;
u8 flags = 0;
- try{
- // Check neighbor
- v3s16 p2 = p + neighbor_dirs[i];
- MapNode n2 = getNodeParent(p2);
-
+ // Check neighbor
+ v3s16 p2 = p + neighbor_dirs[i];
+ MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
+ if(n2.d != CONTENT_IGNORE)
+ {
content = n2.d;
if(n2.d == CONTENT_WATERSOURCE)
@@ -906,11 +1198,10 @@ void MapBlock::updateMesh(u32 daynight_ratio)
// NOTE: This doesn't get executed if neighbor
// doesn't exist
p2.Y += 1;
- n2 = getNodeParent(p2);
+ n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
if(n2.d == CONTENT_WATERSOURCE || n2.d == CONTENT_WATER)
flags |= neighborflag_top_is_water;
}
- catch(InvalidPositionException &e){}
neighbor_levels.insert(neighbor_dirs[i], level);
neighbor_contents.insert(neighbor_dirs[i], content);
@@ -1066,7 +1357,7 @@ void MapBlock::updateMesh(u32 daynight_ratio)
if(dir == v3s16(1,0,-0))
vertices[j].Pos.rotateXZBy(-90);
- vertices[j].Pos += intToFloat(p + getPosRelative(), BS);
+ vertices[j].Pos += intToFloat(p + blockpos_nodes, BS);
}
u16 indices[] = {0,1,2,2,3,0};
@@ -1105,7 +1396,7 @@ void MapBlock::updateMesh(u32 daynight_ratio)
//vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
s32 j = corner_resolve[i];
vertices[i].Pos.Y += corner_levels[j];
- vertices[i].Pos += intToFloat(p + getPosRelative(), BS);
+ vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
}
u16 indices[] = {0,1,2,2,3,0};
@@ -1120,20 +1411,18 @@ void MapBlock::updateMesh(u32 daynight_ratio)
{
//bool top_is_water = false;
bool top_is_air = false;
- try{
- MapNode n = getNodeParent(v3s16(x,y+1,z));
- /*if(n.d == CONTENT_WATER || n.d == CONTENT_WATERSOURCE)
- top_is_water = true;*/
- if(n.d == CONTENT_AIR)
- top_is_air = true;
- }catch(InvalidPositionException &e){}
+ MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
+ /*if(n.d == CONTENT_WATER || n.d == CONTENT_WATERSOURCE)
+ top_is_water = true;*/
+ if(n.d == CONTENT_AIR)
+ top_is_air = true;
/*if(top_is_water == true)
continue;*/
if(top_is_air == false)
continue;
- u8 l = decode_light(n.getLightBlend(daynight_ratio));
+ u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
video::SColor c(WATER_ALPHA,l,l,l);
video::S3DVertex vertices[4] =
@@ -1155,7 +1444,7 @@ void MapBlock::updateMesh(u32 daynight_ratio)
for(s32 i=0; i<4; i++)
{
vertices[i].Pos.Y += (-0.5+node_water_level)*BS;
- vertices[i].Pos += intToFloat(p + getPosRelative(), BS);
+ vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
}
u16 indices[] = {0,1,2,2,3,0};
@@ -1167,8 +1456,8 @@ void MapBlock::updateMesh(u32 daynight_ratio)
*/
else if(n.d == CONTENT_LEAVES && new_style_leaves)
{
- /*u8 l = decode_light(n.getLightBlend(daynight_ratio));*/
- u8 l = decode_light(undiminish_light(n.getLightBlend(daynight_ratio)));
+ /*u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));*/
+ u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
video::SColor c(255,l,l,l);
for(u32 j=0; j<6; j++)
@@ -1222,7 +1511,7 @@ void MapBlock::updateMesh(u32 daynight_ratio)
for(u16 i=0; i<4; i++)
{
- vertices[i].Pos += intToFloat(p + getPosRelative(), BS);
+ vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
}
u16 indices[] = {0,1,2,2,3,0};
@@ -1230,6 +1519,150 @@ void MapBlock::updateMesh(u32 daynight_ratio)
collector.append(material_leaves1, vertices, 4, indices, 6);
}
}
+ /*
+ Add glass
+ */
+ else if(n.d == CONTENT_GLASS)
+ {
+ u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
+ video::SColor c(255,l,l,l);
+
+ for(u32 j=0; j<6; j++)
+ {
+ video::S3DVertex vertices[4] =
+ {
+ video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
+ pa_glass.x0(), pa_glass.y1()),
+ video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
+ pa_glass.x1(), pa_glass.y1()),
+ video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
+ pa_glass.x1(), pa_glass.y0()),
+ video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
+ pa_glass.x0(), pa_glass.y0()),
+ };
+
+ if(j == 0)
+ {
+ for(u16 i=0; i<4; i++)
+ vertices[i].Pos.rotateXZBy(0);
+ }
+ else if(j == 1)
+ {
+ for(u16 i=0; i<4; i++)
+ vertices[i].Pos.rotateXZBy(180);
+ }
+ else if(j == 2)
+ {
+ for(u16 i=0; i<4; i++)
+ vertices[i].Pos.rotateXZBy(-90);
+ }
+ else if(j == 3)
+ {
+ for(u16 i=0; i<4; i++)
+ vertices[i].Pos.rotateXZBy(90);
+ }
+ else if(j == 4)
+ {
+ for(u16 i=0; i<4; i++)
+ vertices[i].Pos.rotateYZBy(-90);
+ }
+ else if(j == 5)
+ {
+ for(u16 i=0; i<4; i++)
+ vertices[i].Pos.rotateYZBy(90);
+ }
+
+ for(u16 i=0; i<4; i++)
+ {
+ vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
+ }
+
+ u16 indices[] = {0,1,2,2,3,0};
+ // Add to mesh collector
+ collector.append(material_glass, vertices, 4, indices, 6);
+ }
+ }
+ /*
+ Add fence
+ */
+ else if(n.d == CONTENT_FENCE)
+ {
+ u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
+ video::SColor c(255,l,l,l);
+
+ const f32 post_rad=(f32)BS/10;
+ const f32 bar_rad=(f32)BS/20;
+ const f32 bar_len=(f32)(BS/2)-post_rad;
+
+ // The post - always present
+ v3f pos = intToFloat(p+blockpos_nodes, BS);
+ f32 postuv[24]={
+ 0.4,0.4,0.6,0.6,
+ 0.35,0,0.65,1,
+ 0.35,0,0.65,1,
+ 0.35,0,0.65,1,
+ 0.35,0,0.65,1,
+ 0.4,0.4,0.6,0.6};
+ makeCuboid(material_wood, &collector,
+ &pa_wood, c, pos,
+ post_rad,BS/2,post_rad, postuv);
+
+ // Now a section of fence, +X, if there's a post there
+ v3s16 p2 = p;
+ p2.X++;
+ MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
+ if(n2.d == CONTENT_FENCE)
+ {
+ pos = intToFloat(p+blockpos_nodes, BS);
+ pos.X += BS/2;
+ pos.Y += BS/4;
+ f32 xrailuv[24]={
+ 0,0.4,1,0.6,
+ 0,0.4,1,0.6,
+ 0,0.4,1,0.6,
+ 0,0.4,1,0.6,
+ 0,0.4,1,0.6,
+ 0,0.4,1,0.6};
+ makeCuboid(material_wood, &collector,
+ &pa_wood, c, pos,
+ bar_len,bar_rad,bar_rad, xrailuv);
+
+ pos.Y -= BS/2;
+ makeCuboid(material_wood, &collector,
+ &pa_wood, c, pos,
+ bar_len,bar_rad,bar_rad, xrailuv);
+ }
+
+ // Now a section of fence, +Z, if there's a post there
+ p2 = p;
+ p2.Z++;
+ n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
+ if(n2.d == CONTENT_FENCE)
+ {
+ pos = intToFloat(p+blockpos_nodes, BS);
+ pos.Z += BS/2;
+ pos.Y += BS/4;
+ f32 zrailuv[24]={
+ 0,0.4,1,0.6,
+ 0,0.4,1,0.6,
+ 0,0.4,1,0.6,
+ 0,0.4,1,0.6,
+ 0,0.4,1,0.6,
+ 0,0.4,1,0.6};
+ makeCuboid(material_wood, &collector,
+ &pa_wood, c, pos,
+ bar_rad,bar_rad,bar_len, zrailuv);
+ pos.Y -= BS/2;
+ makeCuboid(material_wood, &collector,
+ &pa_wood, c, pos,
+ bar_rad,bar_rad,bar_len, zrailuv);
+
+ }
+
+ }
+
+
+
}
/*
@@ -1276,11 +1709,154 @@ void MapBlock::updateMesh(u32 daynight_ratio)
the hardware buffer and then delete the mesh
*/
}
+
+ return mesh_new;
+
+ //std::cout<<"added "<<fastfaces.getSize()<<" faces."<<std::endl;
+}
+
+#endif // !SERVER
+
+/*
+ MapBlock
+*/
+
+MapBlock::MapBlock(NodeContainer *parent, v3s16 pos, bool dummy):
+ m_parent(parent),
+ m_pos(pos),
+ changed(true),
+ is_underground(false),
+ m_lighting_expired(true),
+ m_day_night_differs(false),
+ //m_not_fully_generated(false),
+ m_objects(this),
+ m_timestamp(BLOCK_TIMESTAMP_UNDEFINED)
+{
+ data = NULL;
+ if(dummy == false)
+ reallocate();
+
+ //m_spawn_timer = -10000;
+
+#ifndef SERVER
+ m_mesh_expired = false;
+ mesh_mutex.Init();
+ mesh = NULL;
+ m_temp_mods_mutex.Init();
+#endif
+}
+
+MapBlock::~MapBlock()
+{
+#ifndef SERVER
+ {
+ JMutexAutoLock lock(mesh_mutex);
+
+ if(mesh)
+ {
+ mesh->drop();
+ mesh = NULL;
+ }
+ }
+#endif
+
+ if(data)
+ delete[] data;
+}
+
+bool MapBlock::isValidPositionParent(v3s16 p)
+{
+ if(isValidPosition(p))
+ {
+ return true;
+ }
+ else{
+ return m_parent->isValidPosition(getPosRelative() + p);
+ }
+}
+
+MapNode MapBlock::getNodeParent(v3s16 p)
+{
+ if(isValidPosition(p) == false)
+ {
+ return m_parent->getNode(getPosRelative() + p);
+ }
+ else
+ {
+ if(data == NULL)
+ throw InvalidPositionException();
+ return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X];
+ }
+}
+
+void MapBlock::setNodeParent(v3s16 p, MapNode & n)
+{
+ if(isValidPosition(p) == false)
+ {
+ m_parent->setNode(getPosRelative() + p, n);
+ }
+ else
+ {
+ if(data == NULL)
+ throw InvalidPositionException();
+ data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X] = n;
+ }
+}
+
+MapNode MapBlock::getNodeParentNoEx(v3s16 p)
+{
+ if(isValidPosition(p) == false)
+ {
+ try{
+ return m_parent->getNode(getPosRelative() + p);
+ }
+ catch(InvalidPositionException &e)
+ {
+ return MapNode(CONTENT_IGNORE);
+ }
+ }
+ else
+ {
+ if(data == NULL)
+ {
+ return MapNode(CONTENT_IGNORE);
+ }
+ return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X];
+ }
+}
+
+#ifndef SERVER
+
+#if 1
+void MapBlock::updateMesh(u32 daynight_ratio)
+{
+#if 0
+ /*
+ DEBUG: If mesh has been generated, don't generate it again
+ */
+ {
+ JMutexAutoLock meshlock(mesh_mutex);
+ if(mesh != NULL)
+ return;
+ }
+#endif
+
+ MeshMakeData data;
+ data.fill(daynight_ratio, this);
+
+ scene::SMesh *mesh_new = makeMapBlockMesh(&data);
/*
Replace the mesh
*/
+ replaceMesh(mesh_new);
+
+}
+#endif
+
+void MapBlock::replaceMesh(scene::SMesh *mesh_new)
+{
mesh_mutex.Lock();
//scene::SMesh *mesh_old = mesh[daynight_i];
@@ -1317,39 +1893,25 @@ void MapBlock::updateMesh(u32 daynight_ratio)
}
mesh_mutex.Unlock();
-
- //std::cout<<"added "<<fastfaces.getSize()<<" faces."<<std::endl;
}
-
-/*void MapBlock::updateMeshes(s32 first_i)
-{
- assert(first_i >= 0 && first_i <= DAYNIGHT_CACHE_COUNT);
- updateMesh(first_i);
- for(s32 i=0; i<DAYNIGHT_CACHE_COUNT; i++)
- {
- if(i == first_i)
- continue;
- updateMesh(i);
- }
-}*/
-
+
#endif // !SERVER
/*
Propagates sunlight down through the block.
Doesn't modify nodes that are not affected by sunlight.
- Returns false if sunlight at bottom block is invalid
+ Returns false if sunlight at bottom block is invalid.
+ Returns true if sunlight at bottom block is valid.
Returns true if bottom block doesn't exist.
If there is a block above, continues from it.
If there is no block above, assumes there is sunlight, unless
is_underground is set or highest node is water.
- At the moment, all sunlighted nodes are added to light_sources.
- - SUGG: This could be optimized
+ All sunlighted nodes are added to light_sources.
- Turns sunglighted mud into grass.
+ If grow_grass==true, turns sunglighted mud into grass.
if remove_light==true, sets non-sunlighted nodes black.
@@ -1539,6 +2101,7 @@ bool MapBlock::propagateSunlight(core::map<v3s16, bool> & light_sources,
return block_below_is_valid;
}
+
void MapBlock::copyTo(VoxelManipulator &dst)
{
v3s16 data_size(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
@@ -1565,43 +2128,6 @@ void MapBlock::stepObjects(float dtime, bool server, u32 daynight_ratio)
Step objects
*/
m_objects.step(dtime, server, daynight_ratio);
-
- /*
- Spawn some objects at random.
-
- Use dayNightDiffed() to approximate being near ground level
- */
- if(m_spawn_timer < -999)
- {
- m_spawn_timer = 60;
- }
- if(dayNightDiffed() == true && getObjectCount() == 0)
- {
- m_spawn_timer -= dtime;
- if(m_spawn_timer <= 0.0)
- {
- m_spawn_timer += myrand() % 300;
-
- v2s16 p2d(
- (myrand()%(MAP_BLOCKSIZE-1))+0,
- (myrand()%(MAP_BLOCKSIZE-1))+0
- );
-
- s16 y = getGroundLevel(p2d);
-
- if(y >= 0)
- {
- v3s16 p(p2d.X, y+1, p2d.Y);
-
- if(getNode(p).d == CONTENT_AIR
- && getNode(p).getLightBlend(daynight_ratio) <= 11)
- {
- RatObject *obj = new RatObject(NULL, -1, intToFloat(p, BS));
- addObject(obj);
- }
- }
- }
- }
setChangedFlag();
}
@@ -1794,6 +2320,34 @@ void MapBlock::serialize(std::ostream &os, u8 version)
*/
compress(databuf, os, version);
+
+ /*
+ NodeMetadata
+ */
+ if(version >= 14)
+ {
+ if(version <= 15)
+ {
+ try{
+ std::ostringstream oss(std::ios_base::binary);
+ m_node_metadata.serialize(oss);
+ os<<serializeString(oss.str());
+ }
+ // This will happen if the string is longer than 65535
+ catch(SerializationError &e)
+ {
+ // Use an empty string
+ os<<serializeString("");
+ }
+ }
+ else
+ {
+ std::ostringstream oss(std::ios_base::binary);
+ m_node_metadata.serialize(oss);
+ compressZlib(oss.str(), os);
+ //os<<serializeLongString(oss.str());
+ }
+ }
}
}
@@ -1802,6 +2356,12 @@ void MapBlock::deSerialize(std::istream &is, u8 version)
if(!ser_ver_supported(version))
throw VersionMismatchException("ERROR: MapBlock format not supported");
+ // These have no lighting info
+ if(version <= 1)
+ {
+ setLightingExpired(true);
+ }
+
// These have no compression
if(version <= 3 || version == 5 || version == 6)
{
@@ -1907,11 +2467,42 @@ void MapBlock::deSerialize(std::istream &is, u8 version)
{
data[i].param2 = s[i+nodecount*2];
}
+
+ /*
+ NodeMetadata
+ */
+ if(version >= 14)
+ {
+ // Ignore errors
+ try{
+ if(version <= 15)
+ {
+ std::string data = deSerializeString(is);
+ std::istringstream iss(data, std::ios_base::binary);
+ m_node_metadata.deSerialize(iss);
+ }
+ else
+ {
+ //std::string data = deSerializeLongString(is);
+ std::ostringstream oss(std::ios_base::binary);
+ decompressZlib(is, oss);
+ std::istringstream iss(oss.str(), std::ios_base::binary);
+ m_node_metadata.deSerialize(iss);
+ }
+ }
+ catch(SerializationError &e)
+ {
+ dstream<<"WARNING: MapBlock::deSerialize(): Ignoring an error"
+ <<" while deserializing node metadata"<<std::endl;
+ }
+ }
}
/*
Translate nodes as specified in the translate_to fields of
node features
+
+ NOTE: This isn't really used. Should it be removed?
*/
for(u32 i=0; i<MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE; i++)
{
@@ -1927,5 +2518,55 @@ void MapBlock::deSerialize(std::istream &is, u8 version)
}
}
+void MapBlock::serializeDiskExtra(std::ostream &os, u8 version)
+{
+ // Versions up from 9 have block objects.
+ if(version >= 9)
+ {
+ serializeObjects(os, version);
+ }
+
+ // Versions up from 15 have static objects.
+ if(version >= 15)
+ {
+ m_static_objects.serialize(os);
+ }
+
+ // Timestamp
+ if(version >= 17)
+ {
+ writeU32(os, getTimestamp());
+ }
+}
+
+void MapBlock::deSerializeDiskExtra(std::istream &is, u8 version)
+{
+ /*
+ Versions up from 9 have block objects.
+ */
+ if(version >= 9)
+ {
+ updateObjects(is, version, NULL, 0);
+ }
+
+ /*
+ Versions up from 15 have static objects.
+ */
+ if(version >= 15)
+ {
+ m_static_objects.deSerialize(is);
+ }
+
+ // Timestamp
+ if(version >= 17)
+ {
+ setTimestamp(readU32(is));
+ }
+ else
+ {
+ setTimestamp(BLOCK_TIMESTAMP_UNDEFINED);
+ }
+}
+
//END
diff --git a/src/mapblock.h b/src/mapblock.h
index 02c072895..1eb97353c 100644
--- a/src/mapblock.h
+++ b/src/mapblock.h
@@ -31,6 +31,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "constants.h"
#include "mapblockobject.h"
#include "voxel.h"
+#include "nodemetadata.h"
+#include "staticobject.h"
+
+#define BLOCK_TIMESTAMP_UNDEFINED 0xffffffff
// Named by looking towards z+
enum{
@@ -154,8 +158,51 @@ public:
virtual MapNode getNode(v3s16 p) = 0;
virtual void setNode(v3s16 p, MapNode & n) = 0;
virtual u16 nodeContainerId() const = 0;
+
+ MapNode getNodeNoEx(v3s16 p)
+ {
+ try{
+ return getNode(p);
+ }
+ catch(InvalidPositionException &e){
+ return MapNode(CONTENT_IGNORE);
+ }
+ }
};
+/*
+ Mesh making stuff
+*/
+
+class MapBlock;
+
+#ifndef SERVER
+
+struct MeshMakeData
+{
+ u32 m_daynight_ratio;
+ NodeModMap m_temp_mods;
+ VoxelManipulator m_vmanip;
+ v3s16 m_blockpos;
+
+ /*
+ Copy central data directly from block, and other data from
+ parent of block.
+ */
+ void fill(u32 daynight_ratio, MapBlock *block);
+};
+
+scene::SMesh* makeMapBlockMesh(MeshMakeData *data);
+
+#endif
+
+u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
+ v3s16 face_dir);
+
+/*
+ MapBlock itself
+*/
+
class MapBlock : public NodeContainer
{
public:
@@ -166,7 +213,7 @@ public:
{
return NODECONTAINER_ID_MAPBLOCK;
}
-
+
NodeContainer * getParent()
{
return m_parent;
@@ -198,24 +245,28 @@ public:
reallocate();
}
- bool getChangedFlag()
+ /*
+ This is called internally or externally after the block is
+ modified, so that the block is saved and possibly not deleted from
+ memory.
+ */
+ void setChangedFlag()
{
- return changed;
+ changed = true;
}
void resetChangedFlag()
{
changed = false;
}
- void setChangedFlag()
+ bool getChangedFlag()
{
- changed = true;
+ return changed;
}
bool getIsUnderground()
{
return is_underground;
}
-
void setIsUnderground(bool a_is_underground)
{
is_underground = a_is_underground;
@@ -244,6 +295,16 @@ public:
return m_lighting_expired;
}
+ /*bool isFullyGenerated()
+ {
+ return !m_not_fully_generated;
+ }
+ void setFullyGenerated(bool b)
+ {
+ setChangedFlag();
+ m_not_fully_generated = !b;
+ }*/
+
bool isValid()
{
if(m_lighting_expired)
@@ -303,6 +364,15 @@ public:
return getNode(p.X, p.Y, p.Z);
}
+ MapNode getNodeNoEx(v3s16 p)
+ {
+ try{
+ return getNode(p.X, p.Y, p.Z);
+ }catch(InvalidPositionException &e){
+ return MapNode(CONTENT_IGNORE);
+ }
+ }
+
void setNode(s16 x, s16 y, s16 z, MapNode & n)
{
if(data == NULL)
@@ -369,55 +439,38 @@ public:
Graphics-related methods
*/
- // A quick version with nodes passed as parameters
+ /*// A quick version with nodes passed as parameters
u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
- v3s16 face_dir);
- // A more convenient version
+ v3s16 face_dir);*/
+ /*// A more convenient version
u8 getFaceLight(u32 daynight_ratio, v3s16 p, v3s16 face_dir)
{
return getFaceLight(daynight_ratio,
getNodeParentNoEx(p),
getNodeParentNoEx(p + face_dir),
face_dir);
+ }*/
+ u8 getFaceLight2(u32 daynight_ratio, v3s16 p, v3s16 face_dir)
+ {
+ return getFaceLight(daynight_ratio,
+ getNodeParentNoEx(p),
+ getNodeParentNoEx(p + face_dir),
+ face_dir);
}
-#ifndef SERVER
- // light = 0...255
- static void makeFastFace(TileSpec tile, u8 light, v3f p,
- v3s16 dir, v3f scale, v3f posRelative_f,
- core::array<FastFace> &dest);
-
- TileSpec getNodeTile(MapNode mn, v3s16 p, v3s16 face_dir,
- NodeModMap &temp_mods);
- u8 getNodeContent(v3s16 p, MapNode mn,
- NodeModMap &temp_mods);
-
- /*
- Generates the FastFaces of a node row. This has a
- ridiculous amount of parameters because that way they
- can be precalculated by the caller.
+#ifndef SERVER // Only on client
- translate_dir: unit vector with only one of x, y or z
- face_dir: unit vector with only one of x, y or z
- */
- void updateFastFaceRow(
- u32 daynight_ratio,
- v3f posRelative_f,
- v3s16 startpos,
- u16 length,
- v3s16 translate_dir,
- v3f translate_dir_f,
- v3s16 face_dir,
- v3f face_dir_f,
- core::array<FastFace> &dest,
- NodeModMap &temp_mods);
-
+#if 1
/*
Thread-safely updates the whole mesh of the mapblock.
+ NOTE: Prefer generating the mesh separately and then using
+ replaceMesh().
*/
void updateMesh(u32 daynight_ratio);
-
-#endif // !SERVER
+#endif
+ // Replace the mesh with a new one
+ void replaceMesh(scene::SMesh *mesh_new);
+#endif
// See comments in mapblock.cpp
bool propagateSunlight(core::map<v3s16, bool> & light_sources,
@@ -431,6 +484,7 @@ public:
/*
MapBlockObject stuff
+ DEPRECATED
*/
void serializeObjects(std::ostream &os, u8 version)
@@ -478,13 +532,6 @@ public:
*/
void stepObjects(float dtime, bool server, u32 daynight_ratio);
- /*void wrapObject(MapBlockObject *object)
- {
- m_objects.wrapObject(object);
-
- setChangedFlag();
- }*/
-
// origin is relative to block
void getObjects(v3f origin, f32 max_d,
core::array<DistanceSortedObject> &dest)
@@ -497,7 +544,7 @@ public:
return m_objects.getCount();
}
-#ifndef SERVER
+#ifndef SERVER // Only on client
/*
Methods for setting temporary modifications to nodes for
drawing
@@ -534,6 +581,11 @@ public:
return m_temp_mods.clear();
}
+ void copyTempMods(NodeModMap &dst)
+ {
+ JMutexAutoLock lock(m_temp_mods_mutex);
+ m_temp_mods.copy(dst);
+ }
#endif
/*
@@ -568,13 +620,29 @@ public:
s16 getGroundLevel(v2s16 p2d);
/*
+ Timestamp (see m_timestamp)
+ NOTE: BLOCK_TIMESTAMP_UNDEFINED=0xffffffff means there is no timestamp.
+ */
+ void setTimestamp(u32 time)
+ {
+ m_timestamp = time;
+ setChangedFlag();
+ }
+ u32 getTimestamp()
+ {
+ return m_timestamp;
+ }
+
+ /*
Serialization
*/
- // Doesn't write version by itself
+ // These don't write or read version by itself
void serialize(std::ostream &os, u8 version);
-
void deSerialize(std::istream &is, u8 version);
+ // Used after the basic ones when writing on disk (serverside)
+ void serializeDiskExtra(std::ostream &os, u8 version);
+ void deSerializeDiskExtra(std::istream &is, u8 version);
private:
/*
@@ -604,18 +672,20 @@ public:
Public member variables
*/
-#ifndef SERVER
+#ifndef SERVER // Only on client
scene::SMesh *mesh;
JMutex mesh_mutex;
#endif
+ NodeMetadataList m_node_metadata;
+ StaticObjectList m_static_objects;
+
private:
/*
Private member variables
*/
- // Parent container (practically the Map)
- // Not a MapSector, it is just a structural element.
+ // NOTE: Lots of things rely on this being the Map
NodeContainer *m_parent;
// Position in blocks on parent
v3s16 m_pos;
@@ -655,12 +725,10 @@ private:
// Whether day and night lighting differs
bool m_day_night_differs;
+ // DEPRECATED
MapBlockObjectList m_objects;
- // Object spawning stuff
- float m_spawn_timer;
-
-#ifndef SERVER
+#ifndef SERVER // Only on client
/*
Set to true if the mesh has been ordered to be updated
sometime in the background.
@@ -673,6 +741,12 @@ private:
NodeModMap m_temp_mods;
JMutex m_temp_mods_mutex;
#endif
+
+ /*
+ When block is removed from active blocks, this is set to gametime.
+ Value BLOCK_TIMESTAMP_UNDEFINED=0xffffffff means there is no timestamp.
+ */
+ u32 m_timestamp;
};
inline bool blockpos_over_limit(v3s16 p)
diff --git a/src/mapblockobject.cpp b/src/mapblockobject.cpp
index cd1618c3a..009163a18 100644
--- a/src/mapblockobject.cpp
+++ b/src/mapblockobject.cpp
@@ -17,12 +17,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+// This file contains the DEPRECATED MapBlockObject system
+
#include "mapblockobject.h"
#include "mapblock.h"
// For object wrapping
#include "map.h"
#include "inventory.h"
-#include "irrlichtwrapper.h"
#include "utility.h"
/*
@@ -62,7 +63,7 @@ v3f MovingObject::getAbsoluteShowPos()
void MovingObject::move(float dtime, v3f acceleration)
{
- DSTACK("%s: typeid=%i, pos=(%f,%f,%f), speed=(%f,%f,%f)"
+ DSTACKF("%s: typeid=%i, pos=(%f,%f,%f), speed=(%f,%f,%f)"
", dtime=%f, acc=(%f,%f,%f)",
__FUNCTION_NAME,
getTypeId(),
@@ -282,7 +283,7 @@ void RatObject::addToScene(scene::ISceneManager *smgr)
buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
buf->getMaterial().setTexture
- (0, driver->getTexture(porting::getDataPath("rat.png").c_str()));
+ (0, driver->getTexture(getTexturePath("rat.png").c_str()));
buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
@@ -359,8 +360,6 @@ video::ITexture * ItemObject::getItemImage()
InventoryItem *item = createInventoryItem();
if(item)
texture = item->getImage();
- /*else
- texture = g_irrlicht->getTexture(porting::getDataPath("cloud.png").c_str());*/
if(item)
delete item;
return texture;
@@ -414,7 +413,7 @@ void PlayerObject::addToScene(scene::ISceneManager *smgr)
// Set material
buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
//buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
- buf->getMaterial().setTexture(0, driver->getTexture(porting::getDataPath("player.png").c_str()));
+ buf->getMaterial().setTexture(0, driver->getTexture(getTexturePath("player.png").c_str()));
buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
//buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
@@ -438,7 +437,7 @@ void PlayerObject::addToScene(scene::ISceneManager *smgr)
// Set material
buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
//buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
- buf->getMaterial().setTexture(0, driver->getTexture(porting::getDataPath("player_back.png").c_str()));
+ buf->getMaterial().setTexture(0, driver->getTexture(getTexturePath("player_back.png").c_str()));
buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
@@ -756,7 +755,7 @@ void MapBlockObjectList::step(float dtime, bool server, u32 daynight_ratio)
core::map<s16, bool> ids_to_delete;
{
- DSTACK("%s: stepping objects", __FUNCTION_NAME);
+ DSTACKF("%s: stepping objects", __FUNCTION_NAME);
for(core::map<s16, MapBlockObject*>::Iterator
i = m_objects.getIterator();
@@ -764,7 +763,7 @@ void MapBlockObjectList::step(float dtime, bool server, u32 daynight_ratio)
{
MapBlockObject *obj = i.getNode()->getValue();
- DSTACK("%s: stepping object type %i", __FUNCTION_NAME,
+ DSTACKF("%s: stepping object type %i", __FUNCTION_NAME,
obj->getTypeId());
if(server)
@@ -792,7 +791,7 @@ void MapBlockObjectList::step(float dtime, bool server, u32 daynight_ratio)
}
{
- DSTACK("%s: deleting objects", __FUNCTION_NAME);
+ DSTACKF("%s: deleting objects", __FUNCTION_NAME);
// Delete objects in delete queue
for(core::map<s16, bool>::Iterator
@@ -816,7 +815,7 @@ void MapBlockObjectList::step(float dtime, bool server, u32 daynight_ratio)
return;
{
- DSTACK("%s: object wrap loop", __FUNCTION_NAME);
+ DSTACKF("%s: object wrap loop", __FUNCTION_NAME);
for(core::map<s16, MapBlockObject*>::Iterator
i = m_objects.getIterator();
diff --git a/src/mapblockobject.h b/src/mapblockobject.h
index d157162ec..804494715 100644
--- a/src/mapblockobject.h
+++ b/src/mapblockobject.h
@@ -17,6 +17,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+// This file contains the DEPRECATED MapBlockObject system
+
#ifndef MAPBLOCKOBJECT_HEADER
#define MAPBLOCKOBJECT_HEADER
@@ -430,7 +432,7 @@ public:
buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
//buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
buf->getMaterial().setTexture
- (0, driver->getTexture(porting::getDataPath("sign.png").c_str()));
+ (0, driver->getTexture(getTexturePath("sign.png").c_str()));
buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
@@ -454,7 +456,7 @@ public:
buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
//buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
buf->getMaterial().setTexture
- (0, driver->getTexture(porting::getDataPath("sign_back.png").c_str()));
+ (0, driver->getTexture(getTexturePath("sign_back.png").c_str()));
buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
@@ -561,7 +563,8 @@ public:
(-BS*0.3,-BS*.25,-BS*0.3, BS*0.3,BS*0.25,BS*0.3);
m_selection_box = new core::aabbox3d<f32>
(-BS*0.3,-BS*.25,-BS*0.3, BS*0.3,BS*0.25,BS*0.3);
-
+
+ m_yaw = 0;
m_counter1 = 0;
m_counter2 = 0;
m_age = 0;
@@ -904,7 +907,8 @@ class PlayerObject : public MovingObject
public:
PlayerObject(MapBlock *block, s16 id, v3f pos):
MovingObject(block, id, pos),
- m_node(NULL)
+ m_node(NULL),
+ m_yaw(0)
{
m_collision_box = new core::aabbox3d<f32>
(-BS*0.3,-BS*.25,-BS*0.3, BS*0.3,BS*0.25,BS*0.3);
diff --git a/src/mapchunk.h b/src/mapchunk.h
index 1819fa13e..9860abad0 100644
--- a/src/mapchunk.h
+++ b/src/mapchunk.h
@@ -36,7 +36,8 @@ class MapChunk
{
public:
MapChunk():
- m_generation_level(GENERATED_NOT)
+ m_generation_level(GENERATED_NOT),
+ m_modified(true)
{
}
@@ -58,8 +59,12 @@ public:
is.read((char*)&m_generation_level, 1);
}
+ bool isModified(){ return m_modified; }
+ void setModified(bool modified){ m_modified = modified; }
+
private:
u8 m_generation_level;
+ bool m_modified;
};
#endif
diff --git a/src/mapnode.cpp b/src/mapnode.cpp
index 2ca4ade7a..9a8a09a15 100644
--- a/src/mapnode.cpp
+++ b/src/mapnode.cpp
@@ -25,11 +25,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mineral.h"
// For g_settings
#include "main.h"
+#include "nodemetadata.h"
ContentFeatures::~ContentFeatures()
{
if(translate_to)
delete translate_to;
+ if(initial_metadata)
+ delete initial_metadata;
}
void ContentFeatures::setTexture(u16 i, std::string name, u8 alpha)
@@ -127,6 +130,9 @@ void init_mapnode()
for(u16 i=0; i<256; i++)
{
ContentFeatures *f = &g_content_features[i];
+ // Re-initialize
+ *f = ContentFeatures();
+
for(u16 j=0; j<6; j++)
f->tiles[j].material_type = initial_material_type;
}
@@ -140,7 +146,7 @@ void init_mapnode()
f->setInventoryTextureCube("stone.png", "stone.png", "stone.png");
f->param_type = CPT_MINERAL;
f->is_ground_content = true;
- f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
+ f->dug_item = std::string("MaterialItem ")+itos(CONTENT_COBBLE)+" 1";
i = CONTENT_GRASS;
f = &g_content_features[i];
@@ -200,7 +206,27 @@ void init_mapnode()
f->setAllTextures("[noalpha:leaves.png");
}
f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
-
+
+ i = CONTENT_GLASS;
+ f = &g_content_features[i];
+ f->light_propagates = true;
+ f->param_type = CPT_LIGHT;
+ f->is_ground_content = true;
+ f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
+ f->solidness = 0; // drawn separately, makes no faces
+ f->setInventoryTextureCube("glass.png", "glass.png", "glass.png");
+
+ i = CONTENT_FENCE;
+ f = &g_content_features[i];
+ f->light_propagates = true;
+ f->param_type = CPT_LIGHT;
+ f->is_ground_content = true;
+ f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
+ f->solidness = 0; // drawn separately, makes no faces
+ f->air_equivalent = true; // grass grows underneath
+ f->setInventoryTexture("item_fence.png");
+
+ // Deprecated
i = CONTENT_COALSTONE;
f = &g_content_features[i];
//f->translate_to = new MapNode(CONTENT_STONE, MINERAL_COAL);
@@ -235,6 +261,7 @@ void init_mapnode()
f->pointable = false;
f->diggable = false;
f->buildable_to = true;
+ f->air_equivalent = true;
i = CONTENT_WATER;
f = &g_content_features[i];
@@ -282,15 +309,99 @@ void init_mapnode()
f->setInventoryTexture("torch_on_floor.png");
f->param_type = CPT_LIGHT;
f->light_propagates = true;
+ f->sunlight_propagates = true;
+ f->solidness = 0; // drawn separately, makes no faces
+ f->walkable = false;
+ f->wall_mounted = true;
+ f->air_equivalent = true;
+ f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
+
+ i = CONTENT_SIGN_WALL;
+ f = &g_content_features[i];
+ f->setInventoryTexture("sign_wall.png");
+ f->param_type = CPT_LIGHT;
+ f->light_propagates = true;
+ f->sunlight_propagates = true;
f->solidness = 0; // drawn separately, makes no faces
f->walkable = false;
f->wall_mounted = true;
+ f->air_equivalent = true;
+ f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
+ if(f->initial_metadata == NULL)
+ f->initial_metadata = new SignNodeMetadata("Some sign");
+
+ i = CONTENT_CHEST;
+ f = &g_content_features[i];
+ f->param_type = CPT_FACEDIR_SIMPLE;
+ f->setAllTextures("chest_side.png");
+ f->setTexture(0, "chest_top.png");
+ f->setTexture(1, "chest_top.png");
+ f->setTexture(5, "chest_front.png"); // Z-
+ f->setInventoryTexture("chest_top.png");
+ //f->setInventoryTextureCube("chest_top.png", "chest_side.png", "chest_side.png");
+ f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
+ if(f->initial_metadata == NULL)
+ f->initial_metadata = new ChestNodeMetadata();
+
+ i = CONTENT_FURNACE;
+ f = &g_content_features[i];
+ f->param_type = CPT_FACEDIR_SIMPLE;
+ f->setAllTextures("furnace_side.png");
+ f->setTexture(5, "furnace_front.png"); // Z-
+ f->setInventoryTexture("furnace_front.png");
+ //f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
+ f->dug_item = std::string("MaterialItem ")+itos(CONTENT_COBBLE)+" 6";
+ if(f->initial_metadata == NULL)
+ f->initial_metadata = new FurnaceNodeMetadata();
+
+ i = CONTENT_COBBLE;
+ f = &g_content_features[i];
+ f->setAllTextures("cobble.png");
+ f->setInventoryTextureCube("cobble.png", "cobble.png", "cobble.png");
+ f->param_type = CPT_NONE;
+ f->is_ground_content = true;
+ f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
+
+ i = CONTENT_STEEL;
+ f = &g_content_features[i];
+ f->setAllTextures("steel_block.png");
+ f->setInventoryTextureCube("steel_block.png", "steel_block.png",
+ "steel_block.png");
+ f->param_type = CPT_NONE;
+ f->is_ground_content = true;
f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
+ // NOTE: Remember to add frequently used stuff to the texture atlas in tile.cpp
+}
+
+v3s16 facedir_rotate(u8 facedir, v3s16 dir)
+{
+ /*
+ Face 2 (normally Z-) direction:
+ facedir=0: Z-
+ facedir=1: X-
+ facedir=2: Z+
+ facedir=3: X+
+ */
+ v3s16 newdir;
+ if(facedir==0) // Same
+ newdir = v3s16(dir.X, dir.Y, dir.Z);
+ else if(facedir == 1) // Face is taken from rotXZccv(-90)
+ newdir = v3s16(-dir.Z, dir.Y, dir.X);
+ else if(facedir == 2) // Face is taken from rotXZccv(180)
+ newdir = v3s16(-dir.X, dir.Y, -dir.Z);
+ else if(facedir == 3) // Face is taken from rotXZccv(90)
+ newdir = v3s16(dir.Z, dir.Y, -dir.X);
+ else
+ newdir = dir;
+ return newdir;
}
TileSpec MapNode::getTile(v3s16 dir)
{
+ if(content_features(d).param_type == CPT_FACEDIR_SIMPLE)
+ dir = facedir_rotate(param1, dir);
+
TileSpec spec;
s32 dir_i = -1;
diff --git a/src/mapnode.h b/src/mapnode.h
index 03a294ad2..57382aa2b 100644
--- a/src/mapnode.h
+++ b/src/mapnode.h
@@ -27,7 +27,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "exceptions.h"
#include "serialization.h"
#include "tile.h"
-#include "iirrlichtwrapper.h"
/*
Initializes all kind of stuff in here.
@@ -94,12 +93,14 @@ void init_content_inventory_texture_paths();
#define CONTENT_COALSTONE 11
#define CONTENT_WOOD 12
#define CONTENT_SAND 13
-
-/*
- This is used by all kinds of things to allocate memory for all
- contents except CONTENT_AIR and CONTENT_IGNORE
-*/
-#define USEFUL_CONTENT_COUNT 14
+#define CONTENT_SIGN_WALL 14
+#define CONTENT_CHEST 15
+#define CONTENT_FURNACE 16
+//#define CONTENT_WORKBENCH 17
+#define CONTENT_COBBLE 18
+#define CONTENT_STEEL 19
+#define CONTENT_GLASS 20
+#define CONTENT_FENCE 21
/*
Content feature list
@@ -109,7 +110,9 @@ enum ContentParamType
{
CPT_NONE,
CPT_LIGHT,
- CPT_MINERAL
+ CPT_MINERAL,
+ // Direction for chests and furnaces and such
+ CPT_FACEDIR_SIMPLE
};
enum LiquidType
@@ -120,6 +123,7 @@ enum LiquidType
};
class MapNode;
+class NodeMetadata;
struct ContentFeatures
{
@@ -139,28 +143,36 @@ struct ContentFeatures
*/
TileSpec tiles[6];
- // TODO: Somehow specify inventory image
- //std::string inventory_image_path;
- //TextureSpec inventory_texture;
- //u32 inventory_texture_id;
video::ITexture *inventory_texture;
- bool is_ground_content; //TODO: Remove, use walkable instead
+ bool is_ground_content;
bool light_propagates;
bool sunlight_propagates;
u8 solidness; // Used when choosing which face is drawn
+ // This is used for collision detection.
+ // Also for general solidness queries.
bool walkable;
+ // Player can point to these
bool pointable;
+ // Player can dig these
bool diggable;
+ // Player can build on these
bool buildable_to;
+ // Whether the node has no liquid, source liquid or flowing liquid
enum LiquidType liquid_type;
- // If true, param2 is set to direction when placed
+ // If true, param2 is set to direction when placed. Used for torches.
// NOTE: the direction format is quite inefficient and should be changed
bool wall_mounted;
+ // If true, node is equivalent to air. Torches are, air is. Water is not.
+ // Is used for example to check whether a mud block can have grass on.
+ bool air_equivalent;
// Inventory item string as which the node appears in inventory when dug.
// Mineral overrides this.
std::string dug_item;
+
+ // Initial metadata is cloned from this
+ NodeMetadata *initial_metadata;
//TODO: Move more properties here
@@ -179,7 +191,9 @@ struct ContentFeatures
buildable_to = false;
liquid_type = LIQUID_NONE;
wall_mounted = false;
+ air_equivalent = false;
dug_item = "";
+ initial_metadata = NULL;
}
~ContentFeatures();
@@ -408,6 +422,13 @@ inline v3s16 unpackDir(u8 b)
return d;
}
+/*
+ facedir: CPT_FACEDIR_SIMPLE param1 value
+ dir: The face for which stuff is wanted
+ return value: The face from which the stuff is actually found
+*/
+v3s16 facedir_rotate(u8 facedir, v3s16 dir);
+
enum LightBank
{
LIGHTBANK_DAY,
@@ -430,14 +451,18 @@ struct MapNode
Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
- Contains 2 values, day- and night lighting. Each takes 4 bits.
*/
- s8 param;
+ union
+ {
+ s8 param;
+ u8 param1;
+ };
+ /*
+ The second parameter. Initialized to 0.
+ E.g. direction for torches and flowing water.
+ */
union
{
- /*
- The second parameter. Initialized to 0.
- Direction for torches and flowing water.
- */
u8 param2;
u8 dir;
};
@@ -500,7 +525,7 @@ struct MapNode
// Select the brightest of [light source, propagated light]
u8 lightday = 0;
u8 lightnight = 0;
- if(light_propagates())
+ if(content_features(d).param_type == CPT_LIGHT)
{
lightday = param & 0x0f;
lightnight = (param>>4)&0x0f;
@@ -521,7 +546,7 @@ struct MapNode
{
// Select the brightest of [light source, propagated light]
u8 light = 0;
- if(light_propagates())
+ if(content_features(d).param_type == CPT_LIGHT)
{
if(bank == LIGHTBANK_DAY)
light = param & 0x0f;
@@ -563,8 +588,8 @@ struct MapNode
void setLight(enum LightBank bank, u8 a_light)
{
- // If not transparent, can't set light
- if(light_propagates() == false)
+ // If node doesn't contain light data, ignore this
+ if(content_features(d).param_type != CPT_LIGHT)
return;
if(bank == LIGHTBANK_DAY)
{
diff --git a/src/materials.cpp b/src/materials.cpp
index f56b024b2..841f1d655 100644
--- a/src/materials.cpp
+++ b/src/materials.cpp
@@ -13,14 +13,42 @@ void setStoneLikeDiggingProperties(u8 material, float toughness)
DiggingProperties(true, 15.0*toughness, 0));
g_material_properties[material].setDiggingProperties("WPick",
- DiggingProperties(true, 1.5*toughness, 65535./30.*toughness));
+ DiggingProperties(true, 1.3*toughness, 65535./30.*toughness));
g_material_properties[material].setDiggingProperties("STPick",
- DiggingProperties(true, 0.7*toughness, 65535./100.*toughness));
+ DiggingProperties(true, 0.75*toughness, 65535./100.*toughness));
+ g_material_properties[material].setDiggingProperties("SteelPick",
+ DiggingProperties(true, 0.50*toughness, 65535./333.*toughness));
/*g_material_properties[material].setDiggingProperties("MesePick",
DiggingProperties(true, 0.0*toughness, 65535./20.*toughness));*/
}
+void setDirtLikeDiggingProperties(u8 material, float toughness)
+{
+ g_material_properties[material].setDiggingProperties("",
+ DiggingProperties(true, 0.75*toughness, 0));
+
+ g_material_properties[material].setDiggingProperties("WShovel",
+ DiggingProperties(true, 0.4*toughness, 65535./50.*toughness));
+ g_material_properties[material].setDiggingProperties("STShovel",
+ DiggingProperties(true, 0.2*toughness, 65535./150.*toughness));
+ g_material_properties[material].setDiggingProperties("SteelShovel",
+ DiggingProperties(true, 0.15*toughness, 65535./400.*toughness));
+}
+
+void setWoodLikeDiggingProperties(u8 material, float toughness)
+{
+ g_material_properties[material].setDiggingProperties("",
+ DiggingProperties(true, 3.0*toughness, 0));
+
+ g_material_properties[material].setDiggingProperties("WAxe",
+ DiggingProperties(true, 1.5*toughness, 65535./30.*toughness));
+ g_material_properties[material].setDiggingProperties("STAxe",
+ DiggingProperties(true, 0.75*toughness, 65535./100.*toughness));
+ g_material_properties[material].setDiggingProperties("SteelAxe",
+ DiggingProperties(true, 0.5*toughness, 65535./333.*toughness));
+}
+
void initializeMaterialProperties()
{
/*
@@ -29,36 +57,30 @@ void initializeMaterialProperties()
Add some digging properties to them.
*/
-
- setStoneLikeDiggingProperties(CONTENT_STONE, 1.0);
-
- g_material_properties[CONTENT_GRASS].setDiggingProperties("",
- DiggingProperties(true, 0.4, 0));
-
- g_material_properties[CONTENT_TORCH].setDiggingProperties("",
- DiggingProperties(true, 0.0, 0));
-
- g_material_properties[CONTENT_TREE].setDiggingProperties("",
- DiggingProperties(true, 1.5, 0));
-
- g_material_properties[CONTENT_LEAVES].setDiggingProperties("",
- DiggingProperties(true, 0.35, 0));
-
- g_material_properties[CONTENT_GRASS_FOOTSTEPS].setDiggingProperties("",
- DiggingProperties(true, 0.5, 0));
+ setStoneLikeDiggingProperties(CONTENT_STONE, 1.0);
setStoneLikeDiggingProperties(CONTENT_MESE, 0.5);
-
- g_material_properties[CONTENT_MUD].setDiggingProperties("",
- DiggingProperties(true, 0.4, 0));
-
setStoneLikeDiggingProperties(CONTENT_COALSTONE, 1.5);
-
- g_material_properties[CONTENT_WOOD].setDiggingProperties("",
- DiggingProperties(true, 1.0, 0));
+ setStoneLikeDiggingProperties(CONTENT_FURNACE, 3.0);
+ setStoneLikeDiggingProperties(CONTENT_COBBLE, 1.0);
+ setStoneLikeDiggingProperties(CONTENT_STEEL, 5.0);
+
+ setDirtLikeDiggingProperties(CONTENT_MUD, 1.0);
+ setDirtLikeDiggingProperties(CONTENT_GRASS, 1.0);
+ setDirtLikeDiggingProperties(CONTENT_GRASS_FOOTSTEPS, 1.0);
+ setDirtLikeDiggingProperties(CONTENT_SAND, 1.0);
- g_material_properties[CONTENT_SAND].setDiggingProperties("",
- DiggingProperties(true, 0.4, 0));
+ setWoodLikeDiggingProperties(CONTENT_TREE, 1.0);
+ setWoodLikeDiggingProperties(CONTENT_LEAVES, 0.15);
+ setWoodLikeDiggingProperties(CONTENT_GLASS, 0.15);
+ setWoodLikeDiggingProperties(CONTENT_FENCE, 0.75);
+ setWoodLikeDiggingProperties(CONTENT_WOOD, 0.75);
+ setWoodLikeDiggingProperties(CONTENT_CHEST, 1.0);
+
+ g_material_properties[CONTENT_SIGN_WALL].setDiggingProperties("",
+ DiggingProperties(true, 0.5, 0));
+ g_material_properties[CONTENT_TORCH].setDiggingProperties("",
+ DiggingProperties(true, 0.0, 0));
/*
Add MesePick to everything
diff --git a/src/mineral.cpp b/src/mineral.cpp
index e61c25c1e..038251fa3 100644
--- a/src/mineral.cpp
+++ b/src/mineral.cpp
@@ -27,7 +27,6 @@ const char *mineral_filenames[MINERAL_COUNT] =
"mineral_iron.png"
};
-//textureid_t mineral_textures[MINERAL_COUNT] = {0};
std::string mineral_textures[MINERAL_COUNT];
void init_mineral()
@@ -40,7 +39,6 @@ void init_mineral()
}
}
-//textureid_t mineral_block_texture(u8 mineral)
std::string mineral_block_texture(u8 mineral)
{
if(mineral >= MINERAL_COUNT)
diff --git a/src/mineral.h b/src/mineral.h
index fcc1bd123..970ff1f78 100644
--- a/src/mineral.h
+++ b/src/mineral.h
@@ -21,7 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define MINERAL_HEADER
#include "inventory.h"
-#include "texture.h"
+#include "tile.h"
/*
Minerals
@@ -39,7 +39,6 @@ void init_mineral();
#define MINERAL_COUNT 3
-//textureid_t mineral_block_texture(u8 mineral);
std::string mineral_block_texture(u8 mineral);
inline CraftItem * getDiggedMineralItem(u8 mineral)
diff --git a/src/modalMenu.h b/src/modalMenu.h
index ebbb06cfe..1f6d4d897 100644
--- a/src/modalMenu.h
+++ b/src/modalMenu.h
@@ -46,6 +46,8 @@ public:
IGUIElement(gui::EGUIET_ELEMENT, env, parent, id,
core::rect<s32>(0,0,100,100))
{
+ //m_force_regenerate_gui = false;
+
m_menumgr = menumgr;
m_allow_focus_removal = false;
m_screensize_old = v2u32(0,0);
@@ -76,10 +78,11 @@ public:
video::IVideoDriver* driver = Environment->getVideoDriver();
v2u32 screensize = driver->getScreenSize();
- if(screensize != m_screensize_old)
+ if(screensize != m_screensize_old /*|| m_force_regenerate_gui*/)
{
m_screensize_old = screensize;
regenerateGui(screensize);
+ //m_force_regenerate_gui = false;
}
drawMenu();
@@ -119,7 +122,9 @@ public:
virtual void regenerateGui(v2u32 screensize) = 0;
virtual void drawMenu() = 0;
virtual bool OnEvent(const SEvent& event) { return false; };
-
+
+protected:
+ //bool m_force_regenerate_gui;
private:
IMenuManager *m_menumgr;
// This might be necessary to expose to the implementation if it
diff --git a/src/nodemetadata.cpp b/src/nodemetadata.cpp
new file mode 100644
index 000000000..f9468e4fa
--- /dev/null
+++ b/src/nodemetadata.cpp
@@ -0,0 +1,512 @@
+/*
+Minetest-c55
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "nodemetadata.h"
+#include "utility.h"
+#include "mapnode.h"
+#include "exceptions.h"
+#include "inventory.h"
+#include <sstream>
+
+/*
+ NodeMetadata
+*/
+
+core::map<u16, NodeMetadata::Factory> NodeMetadata::m_types;
+
+NodeMetadata::NodeMetadata()
+{
+}
+
+NodeMetadata::~NodeMetadata()
+{
+}
+
+NodeMetadata* NodeMetadata::deSerialize(std::istream &is)
+{
+ // Read id
+ u8 buf[2];
+ is.read((char*)buf, 2);
+ s16 id = readS16(buf);
+
+ // Read data
+ std::string data = deSerializeString(is);
+
+ // Find factory function
+ core::map<u16, Factory>::Node *n;
+ n = m_types.find(id);
+ if(n == NULL)
+ {
+ // If factory is not found, just return.
+ dstream<<"WARNING: NodeMetadata: No factory for typeId="
+ <<id<<std::endl;
+ return NULL;
+ }
+
+ // Try to load the metadata. If it fails, just return.
+ try
+ {
+ std::istringstream iss(data, std::ios_base::binary);
+
+ Factory f = n->getValue();
+ NodeMetadata *meta = (*f)(iss);
+ return meta;
+ }
+ catch(SerializationError &e)
+ {
+ dstream<<"WARNING: NodeMetadata: ignoring SerializationError"<<std::endl;
+ return NULL;
+ }
+}
+
+void NodeMetadata::serialize(std::ostream &os)
+{
+ u8 buf[2];
+ writeU16(buf, typeId());
+ os.write((char*)buf, 2);
+
+ std::ostringstream oss(std::ios_base::binary);
+ serializeBody(oss);
+ os<<serializeString(oss.str());
+}
+
+void NodeMetadata::registerType(u16 id, Factory f)
+{
+ core::map<u16, Factory>::Node *n;
+ n = m_types.find(id);
+ if(n)
+ return;
+ m_types.insert(id, f);
+}
+
+/*
+ SignNodeMetadata
+*/
+
+// Prototype
+SignNodeMetadata proto_SignNodeMetadata("");
+
+SignNodeMetadata::SignNodeMetadata(std::string text):
+ m_text(text)
+{
+ NodeMetadata::registerType(typeId(), create);
+}
+u16 SignNodeMetadata::typeId() const
+{
+ return CONTENT_SIGN_WALL;
+}
+NodeMetadata* SignNodeMetadata::create(std::istream &is)
+{
+ std::string text = deSerializeString(is);
+ return new SignNodeMetadata(text);
+}
+NodeMetadata* SignNodeMetadata::clone()
+{
+ return new SignNodeMetadata(m_text);
+}
+void SignNodeMetadata::serializeBody(std::ostream &os)
+{
+ os<<serializeString(m_text);
+}
+std::string SignNodeMetadata::infoText()
+{
+ return std::string("\"")+m_text+"\"";
+}
+
+/*
+ ChestNodeMetadata
+*/
+
+// Prototype
+ChestNodeMetadata proto_ChestNodeMetadata;
+
+ChestNodeMetadata::ChestNodeMetadata()
+{
+ NodeMetadata::registerType(typeId(), create);
+
+ m_inventory = new Inventory();
+ m_inventory->addList("0", 8*4);
+}
+ChestNodeMetadata::~ChestNodeMetadata()
+{
+ delete m_inventory;
+}
+u16 ChestNodeMetadata::typeId() const
+{
+ return CONTENT_CHEST;
+}
+NodeMetadata* ChestNodeMetadata::create(std::istream &is)
+{
+ ChestNodeMetadata *d = new ChestNodeMetadata();
+ d->m_inventory->deSerialize(is);
+ return d;
+}
+NodeMetadata* ChestNodeMetadata::clone()
+{
+ ChestNodeMetadata *d = new ChestNodeMetadata();
+ *d->m_inventory = *m_inventory;
+ return d;
+}
+void ChestNodeMetadata::serializeBody(std::ostream &os)
+{
+ m_inventory->serialize(os);
+}
+std::string ChestNodeMetadata::infoText()
+{
+ return "Chest";
+}
+bool ChestNodeMetadata::nodeRemovalDisabled()
+{
+ /*
+ Disable removal if chest contains something
+ */
+ InventoryList *list = m_inventory->getList("0");
+ if(list == NULL)
+ return false;
+ if(list->getUsedSlots() == 0)
+ return false;
+ return true;
+}
+
+/*
+ FurnaceNodeMetadata
+*/
+
+// Prototype
+FurnaceNodeMetadata proto_FurnaceNodeMetadata;
+
+FurnaceNodeMetadata::FurnaceNodeMetadata()
+{
+ NodeMetadata::registerType(typeId(), create);
+
+ m_inventory = new Inventory();
+ m_inventory->addList("fuel", 1);
+ m_inventory->addList("src", 1);
+ m_inventory->addList("dst", 4);
+
+ m_step_accumulator = 0;
+ m_fuel_totaltime = 0;
+ m_fuel_time = 0;
+ m_src_totaltime = 0;
+ m_src_time = 0;
+}
+FurnaceNodeMetadata::~FurnaceNodeMetadata()
+{
+ delete m_inventory;
+}
+u16 FurnaceNodeMetadata::typeId() const
+{
+ return CONTENT_FURNACE;
+}
+NodeMetadata* FurnaceNodeMetadata::clone()
+{
+ FurnaceNodeMetadata *d = new FurnaceNodeMetadata();
+ *d->m_inventory = *m_inventory;
+ return d;
+}
+NodeMetadata* FurnaceNodeMetadata::create(std::istream &is)
+{
+ FurnaceNodeMetadata *d = new FurnaceNodeMetadata();
+
+ d->m_inventory->deSerialize(is);
+
+ int temp;
+ is>>temp;
+ d->m_fuel_totaltime = (float)temp/10;
+ is>>temp;
+ d->m_fuel_time = (float)temp/10;
+
+ return d;
+}
+void FurnaceNodeMetadata::serializeBody(std::ostream &os)
+{
+ m_inventory->serialize(os);
+ os<<itos(m_fuel_totaltime*10)<<" ";
+ os<<itos(m_fuel_time*10)<<" ";
+}
+std::string FurnaceNodeMetadata::infoText()
+{
+ //return "Furnace";
+ if(m_fuel_time >= m_fuel_totaltime)
+ {
+ InventoryList *src_list = m_inventory->getList("src");
+ assert(src_list);
+ InventoryItem *src_item = src_list->getItem(0);
+
+ if(src_item)
+ return "Furnace is out of fuel";
+ else
+ return "Furnace is inactive";
+ }
+ else
+ {
+ std::string s = "Furnace is active (";
+ s += itos(m_fuel_time/m_fuel_totaltime*100);
+ s += "%)";
+ return s;
+ }
+}
+void FurnaceNodeMetadata::inventoryModified()
+{
+ dstream<<"Furnace inventory modification callback"<<std::endl;
+}
+bool FurnaceNodeMetadata::step(float dtime)
+{
+ if(dtime > 60.0)
+ dstream<<"Furnace stepping a long time ("<<dtime<<")"<<std::endl;
+ // Update at a fixed frequency
+ const float interval = 2.0;
+ m_step_accumulator += dtime;
+ bool changed = false;
+ while(m_step_accumulator > interval)
+ {
+ m_step_accumulator -= interval;
+ dtime = interval;
+
+ //dstream<<"Furnace step dtime="<<dtime<<std::endl;
+
+ InventoryList *dst_list = m_inventory->getList("dst");
+ assert(dst_list);
+
+ InventoryList *src_list = m_inventory->getList("src");
+ assert(src_list);
+ InventoryItem *src_item = src_list->getItem(0);
+
+ // Start only if there are free slots in dst, so that it can
+ // accomodate any result item
+ if(dst_list->getFreeSlots() > 0 && src_item && src_item->isCookable())
+ {
+ m_src_totaltime = 3;
+ }
+ else
+ {
+ m_src_time = 0;
+ m_src_totaltime = 0;
+ }
+
+ if(m_fuel_time < m_fuel_totaltime)
+ {
+ //dstream<<"Furnace is active"<<std::endl;
+ m_fuel_time += dtime;
+ m_src_time += dtime;
+ if(m_src_time >= m_src_totaltime && m_src_totaltime > 0.001
+ && src_item)
+ {
+ InventoryItem *cookresult = src_item->createCookResult();
+ dst_list->addItem(cookresult);
+ src_list->decrementMaterials(1);
+ m_src_time = 0;
+ m_src_totaltime = 0;
+ }
+ changed = true;
+ continue;
+ }
+
+ if(src_item == NULL || m_src_totaltime < 0.001)
+ {
+ continue;
+ }
+
+ //dstream<<"Furnace is out of fuel"<<std::endl;
+
+ InventoryList *fuel_list = m_inventory->getList("fuel");
+ assert(fuel_list);
+ InventoryItem *fuel_item = fuel_list->getItem(0);
+
+ if(ItemSpec(ITEM_MATERIAL, CONTENT_TREE).checkItem(fuel_item))
+ {
+ m_fuel_totaltime = 30;
+ m_fuel_time = 0;
+ fuel_list->decrementMaterials(1);
+ changed = true;
+ }
+ else if(ItemSpec(ITEM_MATERIAL, CONTENT_WOOD).checkItem(fuel_item))
+ {
+ m_fuel_totaltime = 30/4;
+ m_fuel_time = 0;
+ fuel_list->decrementMaterials(1);
+ changed = true;
+ }
+ else if(ItemSpec(ITEM_CRAFT, "Stick").checkItem(fuel_item))
+ {
+ m_fuel_totaltime = 30/4/4;
+ m_fuel_time = 0;
+ fuel_list->decrementMaterials(1);
+ changed = true;
+ }
+ else if(ItemSpec(ITEM_CRAFT, "lump_of_coal").checkItem(fuel_item))
+ {
+ m_fuel_totaltime = 40;
+ m_fuel_time = 0;
+ fuel_list->decrementMaterials(1);
+ changed = true;
+ }
+ else
+ {
+ //dstream<<"No fuel found"<<std::endl;
+ }
+ }
+ return changed;
+}
+
+/*
+ NodeMetadatalist
+*/
+
+void NodeMetadataList::serialize(std::ostream &os)
+{
+ u8 buf[6];
+
+ u16 version = 1;
+ writeU16(buf, version);
+ os.write((char*)buf, 2);
+
+ u16 count = m_data.size();
+ writeU16(buf, count);
+ os.write((char*)buf, 2);
+
+ for(core::map<v3s16, NodeMetadata*>::Iterator
+ i = m_data.getIterator();
+ i.atEnd()==false; i++)
+ {
+ v3s16 p = i.getNode()->getKey();
+ NodeMetadata *data = i.getNode()->getValue();
+
+ u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
+ writeU16(buf, p16);
+ os.write((char*)buf, 2);
+
+ data->serialize(os);
+ }
+
+}
+void NodeMetadataList::deSerialize(std::istream &is)
+{
+ m_data.clear();
+
+ u8 buf[6];
+
+ is.read((char*)buf, 2);
+ u16 version = readU16(buf);
+
+ if(version > 1)
+ {
+ dstream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
+ <<std::endl;
+ throw SerializationError("NodeMetadataList::deSerialize");
+ }
+
+ is.read((char*)buf, 2);
+ u16 count = readU16(buf);
+
+ for(u16 i=0; i<count; i++)
+ {
+ is.read((char*)buf, 2);
+ u16 p16 = readU16(buf);
+
+ v3s16 p(0,0,0);
+ p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
+ p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
+ p.Y += p16 / MAP_BLOCKSIZE;
+ p16 -= p.Y * MAP_BLOCKSIZE;
+ p.X += p16;
+
+ NodeMetadata *data = NodeMetadata::deSerialize(is);
+
+ if(data == NULL)
+ continue;
+
+ if(m_data.find(p))
+ {
+ dstream<<"WARNING: NodeMetadataList::deSerialize(): "
+ <<"already set data at position"
+ <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
+ <<std::endl;
+ delete data;
+ continue;
+ }
+
+ m_data.insert(p, data);
+ }
+}
+
+NodeMetadataList::~NodeMetadataList()
+{
+ for(core::map<v3s16, NodeMetadata*>::Iterator
+ i = m_data.getIterator();
+ i.atEnd()==false; i++)
+ {
+ delete i.getNode()->getValue();
+ }
+}
+
+NodeMetadata* NodeMetadataList::get(v3s16 p)
+{
+ core::map<v3s16, NodeMetadata*>::Node *n;
+ n = m_data.find(p);
+ if(n == NULL)
+ return NULL;
+ return n->getValue();
+}
+
+void NodeMetadataList::remove(v3s16 p)
+{
+ NodeMetadata *olddata = get(p);
+ if(olddata)
+ {
+ delete olddata;
+ m_data.remove(p);
+ }
+}
+
+void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
+{
+ remove(p);
+ m_data.insert(p, d);
+}
+
+bool NodeMetadataList::step(float dtime)
+{
+ bool something_changed = false;
+ for(core::map<v3s16, NodeMetadata*>::Iterator
+ i = m_data.getIterator();
+ i.atEnd()==false; i++)
+ {
+ v3s16 p = i.getNode()->getKey();
+ NodeMetadata *meta = i.getNode()->getValue();
+ bool changed = meta->step(dtime);
+ if(changed)
+ something_changed = true;
+ /*if(res.inventory_changed)
+ {
+ std::string inv_id;
+ inv_id += "nodemeta:";
+ inv_id += itos(p.X);
+ inv_id += ",";
+ inv_id += itos(p.Y);
+ inv_id += ",";
+ inv_id += itos(p.Z);
+ InventoryContext c;
+ c.current_player = NULL;
+ inv_mgr->inventoryModified(&c, inv_id);
+ }*/
+ }
+ return something_changed;
+}
+
diff --git a/src/nodemetadata.h b/src/nodemetadata.h
new file mode 100644
index 000000000..ae02cfc3c
--- /dev/null
+++ b/src/nodemetadata.h
@@ -0,0 +1,163 @@
+/*
+Minetest-c55
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#ifndef NODEMETADATA_HEADER
+#define NODEMETADATA_HEADER
+
+#include "common_irrlicht.h"
+#include <string>
+#include <iostream>
+
+/*
+ Used for storing:
+
+ Oven:
+ - Item that is being burned
+ - Burning time
+ - Item stack that is being heated
+ - Result item stack
+
+ Sign:
+ - Text
+*/
+
+class Inventory;
+
+class NodeMetadata
+{
+public:
+ typedef NodeMetadata* (*Factory)(std::istream&);
+
+ NodeMetadata();
+ virtual ~NodeMetadata();
+
+ static NodeMetadata* deSerialize(std::istream &is);
+ void serialize(std::ostream &os);
+
+ // This usually is the CONTENT_ value
+ virtual u16 typeId() const = 0;
+ virtual NodeMetadata* clone() = 0;
+ virtual void serializeBody(std::ostream &os) = 0;
+ virtual std::string infoText() {return "";}
+ virtual Inventory* getInventory() {return NULL;}
+ // This is called always after the inventory is modified, before
+ // the changes are copied elsewhere
+ virtual void inventoryModified(){}
+ // A step in time. Returns true if metadata changed.
+ virtual bool step(float dtime) {return false;}
+ virtual bool nodeRemovalDisabled(){return false;}
+
+protected:
+ static void registerType(u16 id, Factory f);
+private:
+ static core::map<u16, Factory> m_types;
+};
+
+class SignNodeMetadata : public NodeMetadata
+{
+public:
+ SignNodeMetadata(std::string text);
+ //~SignNodeMetadata();
+
+ virtual u16 typeId() const;
+ static NodeMetadata* create(std::istream &is);
+ virtual NodeMetadata* clone();
+ virtual void serializeBody(std::ostream &os);
+ virtual std::string infoText();
+
+ std::string getText(){ return m_text; }
+ void setText(std::string t){ m_text = t; }
+
+private:
+ std::string m_text;
+};
+
+class ChestNodeMetadata : public NodeMetadata
+{
+public:
+ ChestNodeMetadata();
+ ~ChestNodeMetadata();
+
+ virtual u16 typeId() const;
+ static NodeMetadata* create(std::istream &is);
+ virtual NodeMetadata* clone();
+ virtual void serializeBody(std::ostream &os);
+ virtual std::string infoText();
+ virtual Inventory* getInventory() {return m_inventory;}
+
+ virtual bool nodeRemovalDisabled();
+
+private:
+ Inventory *m_inventory;
+};
+
+class FurnaceNodeMetadata : public NodeMetadata
+{
+public:
+ FurnaceNodeMetadata();
+ ~FurnaceNodeMetadata();
+
+ virtual u16 typeId() const;
+ virtual NodeMetadata* clone();
+ static NodeMetadata* create(std::istream &is);
+ virtual void serializeBody(std::ostream &os);
+ virtual std::string infoText();
+ virtual Inventory* getInventory() {return m_inventory;}
+ virtual void inventoryModified();
+ virtual bool step(float dtime);
+
+private:
+ Inventory *m_inventory;
+ float m_step_accumulator;
+ float m_fuel_totaltime;
+ float m_fuel_time;
+ float m_src_totaltime;
+ float m_src_time;
+};
+
+/*
+ List of metadata of all the nodes of a block
+*/
+
+class InventoryManager;
+
+class NodeMetadataList
+{
+public:
+ ~NodeMetadataList();
+
+ void serialize(std::ostream &os);
+ void deSerialize(std::istream &is);
+
+ // Get pointer to data
+ NodeMetadata* get(v3s16 p);
+ // Deletes data
+ void remove(v3s16 p);
+ // Deletes old data and sets a new one
+ void set(v3s16 p, NodeMetadata *d);
+
+ // A step in time. Returns true if something changed.
+ bool step(float dtime);
+
+private:
+ core::map<v3s16, NodeMetadata*> m_data;
+};
+
+#endif
+
diff --git a/src/noise.cpp b/src/noise.cpp
index 63682e1e4..6362f5b2c 100644
--- a/src/noise.cpp
+++ b/src/noise.cpp
@@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <math.h>
#include "noise.h"
#include <iostream>
+#include "debug.h"
#define NOISE_MAGIC_X 1619
#define NOISE_MAGIC_Y 31337
@@ -46,6 +47,8 @@ double linearInterpolation(double x0, double x1, double t){
double biLinearInterpolation(double x0y0, double x1y0, double x0y1, double x1y1, double x, double y){
double tx = easeCurve(x);
double ty = easeCurve(y);
+ /*double tx = x;
+ double ty = y;*/
double u = linearInterpolation(x0y0,x1y0,tx);
double v = linearInterpolation(x0y1,x1y1,tx);
return linearInterpolation(u,v,ty);
@@ -93,7 +96,6 @@ double noise3d(int x, int y, int z, int seed)
}
#if 0
-// This is too slow
double noise2d_gradient(double x, double y, int seed)
{
// Calculate the integer coordinates
@@ -175,6 +177,21 @@ double noise2d_perlin(double x, double y, int seed,
return a;
}
+double noise2d_perlin_abs(double x, double y, int seed,
+ int octaves, double persistence)
+{
+ double a = 0;
+ double f = 1.0;
+ double g = 1.0;
+ for(int i=0; i<octaves; i++)
+ {
+ a += g * fabs(noise2d_gradient(x*f, y*f, seed+i));
+ f *= 2.0;
+ g *= persistence;
+ }
+ return a;
+}
+
double noise3d_perlin(double x, double y, double z, int seed,
int octaves, double persistence)
{
@@ -190,3 +207,122 @@ double noise3d_perlin(double x, double y, double z, int seed,
return a;
}
+double noise3d_perlin_abs(double x, double y, double z, int seed,
+ int octaves, double persistence)
+{
+ double a = 0;
+ double f = 1.0;
+ double g = 1.0;
+ for(int i=0; i<octaves; i++)
+ {
+ a += g * fabs(noise3d_gradient(x*f, y*f, z*f, seed+i));
+ f *= 2.0;
+ g *= persistence;
+ }
+ return a;
+}
+
+/*
+ NoiseBuffer
+*/
+
+NoiseBuffer::NoiseBuffer():
+ m_data(NULL)
+{
+}
+
+NoiseBuffer::~NoiseBuffer()
+{
+ clear();
+}
+
+void NoiseBuffer::clear()
+{
+ if(m_data)
+ delete[] m_data;
+ m_data = NULL;
+ m_size_x = 0;
+ m_size_y = 0;
+ m_size_z = 0;
+}
+
+void NoiseBuffer::create(int seed, int octaves, double persistence,
+ double pos_scale,
+ double first_x, double first_y, double first_z,
+ double last_x, double last_y, double last_z,
+ double samplelength_x, double samplelength_y, double samplelength_z)
+{
+ clear();
+
+ m_start_x = first_x - samplelength_x;
+ m_start_y = first_y - samplelength_y;
+ m_start_z = first_z - samplelength_z;
+ m_samplelength_x = samplelength_x;
+ m_samplelength_y = samplelength_y;
+ m_samplelength_z = samplelength_z;
+
+ m_size_x = (last_x - m_start_x)/samplelength_x + 2;
+ m_size_y = (last_y - m_start_y)/samplelength_y + 2;
+ m_size_z = (last_z - m_start_z)/samplelength_z + 2;
+
+ /*dstream<<"m_size_x="<<m_size_x<<", m_size_y="<<m_size_y
+ <<", m_size_z="<<m_size_z<<std::endl;*/
+
+ m_data = new double[m_size_x*m_size_y*m_size_z];
+
+ for(int x=0; x<m_size_x; x++)
+ for(int y=0; y<m_size_y; y++)
+ for(int z=0; z<m_size_z; z++)
+ {
+ double xd = (m_start_x + (double)x*m_samplelength_x)/pos_scale;
+ double yd = (m_start_y + (double)y*m_samplelength_y)/pos_scale;
+ double zd = (m_start_z + (double)z*m_samplelength_z)/pos_scale;
+ intSet(x,y,z, noise3d_perlin(xd,yd,zd,seed,octaves,persistence));
+ }
+}
+
+void NoiseBuffer::intSet(int x, int y, int z, double d)
+{
+ int i = m_size_x*m_size_y*z + m_size_x*y + x;
+ assert(i >= 0);
+ assert(i < m_size_x*m_size_y*m_size_z);
+ m_data[i] = d;
+}
+
+double NoiseBuffer::intGet(int x, int y, int z)
+{
+ int i = m_size_x*m_size_y*z + m_size_x*y + x;
+ assert(i >= 0);
+ assert(i < m_size_x*m_size_y*m_size_z);
+ return m_data[i];
+}
+
+double NoiseBuffer::get(double x, double y, double z)
+{
+ x -= m_start_x;
+ y -= m_start_y;
+ z -= m_start_z;
+ x /= m_samplelength_x;
+ y /= m_samplelength_y;
+ z /= m_samplelength_z;
+ // Calculate the integer coordinates
+ int x0 = (x > 0.0 ? (int)x : (int)x - 1);
+ int y0 = (y > 0.0 ? (int)y : (int)y - 1);
+ int z0 = (z > 0.0 ? (int)z : (int)z - 1);
+ // Calculate the remaining part of the coordinates
+ double xl = x - (double)x0;
+ double yl = y - (double)y0;
+ double zl = z - (double)z0;
+ // Get values for corners of cube
+ double v000 = intGet(x0, y0, z0);
+ double v100 = intGet(x0+1, y0, z0);
+ double v010 = intGet(x0, y0+1, z0);
+ double v110 = intGet(x0+1, y0+1, z0);
+ double v001 = intGet(x0, y0, z0+1);
+ double v101 = intGet(x0+1, y0, z0+1);
+ double v011 = intGet(x0, y0+1, z0+1);
+ double v111 = intGet(x0+1, y0+1, z0+1);
+ // Interpolate
+ return triLinearInterpolation(v000,v100,v010,v110,v001,v101,v011,v111,xl,yl,zl);
+}
+
diff --git a/src/noise.h b/src/noise.h
index 63974e86a..ba26519f2 100644
--- a/src/noise.h
+++ b/src/noise.h
@@ -32,8 +32,38 @@ double noise3d_gradient(double x, double y, double z, int seed);
double noise2d_perlin(double x, double y, int seed,
int octaves, double persistence);
+double noise2d_perlin_abs(double x, double y, int seed,
+ int octaves, double persistence);
+
double noise3d_perlin(double x, double y, double z, int seed,
int octaves, double persistence);
+double noise3d_perlin_abs(double x, double y, double z, int seed,
+ int octaves, double persistence);
+
+class NoiseBuffer
+{
+public:
+ NoiseBuffer();
+ ~NoiseBuffer();
+
+ void clear();
+ void create(int seed, int octaves, double persistence,
+ double pos_scale,
+ double first_x, double first_y, double first_z,
+ double last_x, double last_y, double last_z,
+ double samplelength_x, double samplelength_y, double samplelength_z);
+
+ void intSet(int x, int y, int z, double d);
+ double intGet(int x, int y, int z);
+ double get(double x, double y, double z);
+
+private:
+ double *m_data;
+ double m_start_x, m_start_y, m_start_z;
+ double m_samplelength_x, m_samplelength_y, m_samplelength_z;
+ int m_size_x, m_size_y, m_size_z;
+};
+
#endif
diff --git a/src/player.cpp b/src/player.cpp
index 3bde8a563..198eca957 100644
--- a/src/player.cpp
+++ b/src/player.cpp
@@ -1,6 +1,6 @@
/*
Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -17,21 +17,20 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-/*
-(c) 2010 Perttu Ahola <celeron55@gmail.com>
-*/
-
#include "player.h"
#include "map.h"
#include "connection.h"
#include "constants.h"
#include "utility.h"
+
Player::Player():
touching_ground(false),
in_water(false),
in_water_stable(false),
swimming_up(false),
+ craftresult_is_preview(true),
+ hp(20),
peer_id(PEER_ID_INEXISTENT),
m_pitch(0),
m_yaw(0),
@@ -97,9 +96,12 @@ void Player::serialize(std::ostream &os)
Settings args;
args.setS32("version", 1);
args.set("name", m_name);
+ //args.set("password", m_password);
args.setFloat("pitch", m_pitch);
args.setFloat("yaw", m_yaw);
args.setV3F("position", m_position);
+ args.setBool("craftresult_is_preview", craftresult_is_preview);
+ args.setS32("hp", hp);
args.writeLines(os);
@@ -128,9 +130,37 @@ void Player::deSerialize(std::istream &is)
//args.getS32("version");
std::string name = args.get("name");
updateName(name.c_str());
+ /*std::string password = "";
+ if(args.exists("password"))
+ password = args.get("password");
+ updatePassword(password.c_str());*/
m_pitch = args.getFloat("pitch");
m_yaw = args.getFloat("yaw");
m_position = args.getV3F("position");
+ try{
+ craftresult_is_preview = args.getBool("craftresult_is_preview");
+ }catch(SettingNotFoundException &e){
+ craftresult_is_preview = true;
+ }
+ try{
+ hp = args.getS32("hp");
+ }catch(SettingNotFoundException &e){
+ hp = 20;
+ }
+ /*try{
+ std::string sprivs = args.get("privs");
+ if(sprivs == "all")
+ {
+ privs = PRIV_ALL;
+ }
+ else
+ {
+ std::istringstream ss(sprivs);
+ ss>>privs;
+ }
+ }catch(SettingNotFoundException &e){
+ privs = PRIV_DEFAULT;
+ }*/
inventory.deSerialize(is);
}
@@ -180,7 +210,7 @@ RemotePlayer::RemotePlayer(
// Set material
buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
//buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
- buf->getMaterial().setTexture(0, driver->getTexture(porting::getDataPath("player.png").c_str()));
+ buf->getMaterial().setTexture(0, driver->getTexture(getTexturePath("player.png").c_str()));
buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
//buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
@@ -204,7 +234,7 @@ RemotePlayer::RemotePlayer(
// Set material
buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
//buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
- buf->getMaterial().setTexture(0, driver->getTexture(porting::getDataPath("player_back.png").c_str()));
+ buf->getMaterial().setTexture(0, driver->getTexture(getTexturePath("player_back.png").c_str()));
buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
@@ -263,13 +293,17 @@ LocalPlayer::LocalPlayer():
m_sneak_node(32767,32767,32767),
m_sneak_node_exists(false)
{
+ // Initialize hp to 0, so that no hearts will be shown if server
+ // doesn't support health points
+ hp = 0;
}
LocalPlayer::~LocalPlayer()
{
}
-void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d)
+void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d,
+ core::list<CollisionInfo> *collision_info)
{
v3f position = getPosition();
v3f oldpos = position;
@@ -523,9 +557,23 @@ void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d)
*/
if(other_axes_overlap && main_axis_collides)
{
+ v3f old_speed = m_speed;
+
m_speed -= m_speed.dotProduct(dirs[i]) * dirs[i];
position -= position.dotProduct(dirs[i]) * dirs[i];
position += oldpos.dotProduct(dirs[i]) * dirs[i];
+
+ if(collision_info)
+ {
+ // Report fall collision
+ if(old_speed.Y < m_speed.Y - 0.1)
+ {
+ CollisionInfo info;
+ info.t = COLLISION_FALL;
+ info.speed = m_speed.Y - old_speed.Y;
+ collision_info->push_back(info);
+ }
+ }
}
}
@@ -610,6 +658,11 @@ void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d)
setPosition(position);
}
+void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d)
+{
+ move(dtime, map, pos_max_d, NULL);
+}
+
void LocalPlayer::applyControl(float dtime)
{
// Clear stuff
diff --git a/src/player.h b/src/player.h
index 710a9e0ed..a7a2433ce 100644
--- a/src/player.h
+++ b/src/player.h
@@ -1,6 +1,6 @@
/*
Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -17,25 +17,25 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-/*
-(c) 2010 Perttu Ahola <celeron55@gmail.com>
-*/
-
#ifndef PLAYER_HEADER
#define PLAYER_HEADER
#include "common_irrlicht.h"
#include "inventory.h"
+#include "collision.h"
#define PLAYERNAME_SIZE 20
-#define PLAYERNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.,"
+#define PLAYERNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
+
class Map;
class Player
{
public:
+
+
Player();
virtual ~Player();
@@ -122,6 +122,10 @@ public:
Inventory inventory;
+ bool craftresult_is_preview;
+
+ u16 hp;
+
u16 peer_id;
protected:
@@ -130,6 +134,9 @@ protected:
f32 m_yaw;
v3f m_speed;
v3f m_position;
+
+public:
+
};
/*
@@ -323,6 +330,8 @@ public:
return true;
}
+ void move(f32 dtime, Map &map, f32 pos_max_d,
+ core::list<CollisionInfo> *collision_info);
void move(f32 dtime, Map &map, f32 pos_max_d);
void applyControl(float dtime);
diff --git a/src/porting.cpp b/src/porting.cpp
index e8b135255..7c7ce48ea 100644
--- a/src/porting.cpp
+++ b/src/porting.cpp
@@ -136,7 +136,7 @@ void initializePaths()
char buf[BUFSIZ];
memset(buf, 0, BUFSIZ);
// Get path to executable
- readlink("/proc/self/exe", buf, BUFSIZ-1);
+ assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
pathRemoveFile(buf, '/');
@@ -200,7 +200,7 @@ void initializePaths()
char buf[BUFSIZ];
memset(buf, 0, BUFSIZ);
// Get path to executable
- readlink("/proc/self/exe", buf, BUFSIZ-1);
+ assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
pathRemoveFile(buf, '/');
diff --git a/src/profiler.h b/src/profiler.h
new file mode 100644
index 000000000..a30e34a7c
--- /dev/null
+++ b/src/profiler.h
@@ -0,0 +1,131 @@
+/*
+Minetest-c55
+Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#ifndef PROFILER_HEADER
+#define PROFILER_HEADER
+
+#include "common_irrlicht.h"
+#include <string>
+#include "utility.h"
+#include <jmutex.h>
+#include <jmutexautolock.h>
+
+/*
+ Time profiler
+*/
+
+class Profiler
+{
+public:
+ Profiler()
+ {
+ m_mutex.Init();
+ }
+
+ void add(const std::string &name, u32 duration)
+ {
+ JMutexAutoLock lock(m_mutex);
+ core::map<std::string, u32>::Node *n = m_data.find(name);
+ if(n == NULL)
+ {
+ m_data[name] = duration;
+ }
+ else
+ {
+ n->setValue(n->getValue()+duration);
+ }
+ }
+
+ void clear()
+ {
+ JMutexAutoLock lock(m_mutex);
+ for(core::map<std::string, u32>::Iterator
+ i = m_data.getIterator();
+ i.atEnd() == false; i++)
+ {
+ i.getNode()->setValue(0);
+ }
+ }
+
+ void print(std::ostream &o)
+ {
+ JMutexAutoLock lock(m_mutex);
+ for(core::map<std::string, u32>::Iterator
+ i = m_data.getIterator();
+ i.atEnd() == false; i++)
+ {
+ std::string name = i.getNode()->getKey();
+ o<<name<<": ";
+ s32 clampsize = 40;
+ s32 space = clampsize-name.size();
+ for(s32 j=0; j<space; j++)
+ {
+ if(j%2 == 0 && j < space - 1)
+ o<<"-";
+ else
+ o<<" ";
+ }
+ o<<i.getNode()->getValue();
+ o<<std::endl;
+ }
+ }
+
+private:
+ JMutex m_mutex;
+ core::map<std::string, u32> m_data;
+};
+
+class ScopeProfiler
+{
+public:
+ ScopeProfiler(Profiler *profiler, const std::string &name):
+ m_profiler(profiler),
+ m_name(name),
+ m_timer(NULL)
+ {
+ if(m_profiler)
+ m_timer = new TimeTaker(m_name.c_str());
+ }
+ // name is copied
+ ScopeProfiler(Profiler *profiler, const char *name):
+ m_profiler(profiler),
+ m_name(name),
+ m_timer(NULL)
+ {
+ if(m_profiler)
+ m_timer = new TimeTaker(m_name.c_str());
+ }
+ ~ScopeProfiler()
+ {
+ if(m_timer)
+ {
+ u32 duration = m_timer->stop(true);
+ if(m_profiler)
+ m_profiler->add(m_name, duration);
+ delete m_timer;
+ }
+ }
+private:
+ Profiler *m_profiler;
+ std::string m_name;
+ TimeTaker *m_timer;
+};
+
+#endif
+
diff --git a/src/serialization.cpp b/src/serialization.cpp
index c324ca0fd..6a43d9190 100644
--- a/src/serialization.cpp
+++ b/src/serialization.cpp
@@ -105,6 +105,12 @@ void compressZlib(SharedBuffer<u8> data, std::ostream &os)
}
+void compressZlib(const std::string &data, std::ostream &os)
+{
+ SharedBuffer<u8> databuf((u8*)data.c_str(), data.size());
+ compressZlib(databuf, os);
+}
+
void decompressZlib(std::istream &is, std::ostream &os)
{
z_stream z;
diff --git a/src/serialization.h b/src/serialization.h
index e84ceee3e..fad1388e0 100644
--- a/src/serialization.h
+++ b/src/serialization.h
@@ -26,45 +26,55 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "utility.h"
/*
+ Map format serialization version
+ --------------------------------
+
+ For map data (blocks, nodes, sectors).
+
NOTE: The goal is to increment this so that saved maps will be
loadable by any version. Other compatibility is not
maintained.
- Serialization format versions:
- == Unsupported ==
+
0: original networked test with 1-byte nodes
1: update with 2-byte nodes
- == Supported ==
2: lighting is transmitted in param
3: optional fetching of far blocks
4: block compression
5: sector objects NOTE: block compression was left accidentally out
6: failed attempt at switching block compression on again
7: block compression switched on again
- 8: (dev) server-initiated block transfers and all kinds of stuff
- 9: (dev) block objects
- 10: (dev) water pressure
- 11: (dev) zlib'd blocks, block flags
- 12: (dev) UnlimitedHeightmap now uses interpolated areas
- 13: (dev) Mapgen v2
+ 8: server-initiated block transfers and all kinds of stuff
+ 9: block objects
+ 10: water pressure
+ 11: zlib'd blocks, block flags
+ 12: UnlimitedHeightmap now uses interpolated areas
+ 13: Mapgen v2
+ 14: NodeMetadata
+ 15: StaticObjects
+ 16: larger maximum size of node metadata, and compression
+ 17: MapBlocks contain timestamp
*/
// This represents an uninitialized or invalid format
#define SER_FMT_VER_INVALID 255
// Highest supported serialization version
-#define SER_FMT_VER_HIGHEST 13
+#define SER_FMT_VER_HIGHEST 17
// Lowest supported serialization version
-#define SER_FMT_VER_LOWEST 2
+#define SER_FMT_VER_LOWEST 0
#define ser_ver_supported(v) (v >= SER_FMT_VER_LOWEST && v <= SER_FMT_VER_HIGHEST)
+/*
+ Misc. serialization functions
+*/
+
+void compressZlib(SharedBuffer<u8> data, std::ostream &os);
+void compressZlib(const std::string &data, std::ostream &os);
+void decompressZlib(std::istream &is, std::ostream &os);
+
+// These choose between zlib and a self-made one according to version
void compress(SharedBuffer<u8> data, std::ostream &os, u8 version);
+//void compress(const std::string &data, std::ostream &os, u8 version);
void decompress(std::istream &is, std::ostream &os, u8 version);
-/*class Serializable
-{
-public:
- void serialize(std::ostream &os, u8 version) = 0;
- void deSerialize(std::istream &istr);
-};*/
-
#endif
diff --git a/src/server.cpp b/src/server.cpp
index 24f22c6b3..96fcc0d07 100644
--- a/src/server.cpp
+++ b/src/server.cpp
@@ -1,6 +1,6 @@
/*
Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -28,6 +28,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "voxel.h"
#include "materials.h"
#include "mineral.h"
+#include "config.h"
+#include "servercommand.h"
+#include "filesys.h"
#define BLOCK_EMERGE_FLAG_FROMDISK (1<<0)
@@ -72,7 +75,7 @@ void * EmergeThread::Thread()
DSTACK(__FUNCTION_NAME);
- bool debug=false;
+ //bool debug=false;
BEGIN_DEBUG_EXCEPTION_HANDLER
@@ -91,7 +94,19 @@ void * EmergeThread::Thread()
SharedPtr<QueuedBlockEmerge> q(qptr);
v3s16 &p = q->pos;
-
+ v2s16 p2d(p.X,p.Z);
+
+ /*
+ Do not generate over-limit
+ */
+ if(p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE)
+ continue;
+
//derr_server<<"EmergeThread::Thread(): running"<<std::endl;
//TimeTaker timer("block emerge");
@@ -139,92 +154,107 @@ void * EmergeThread::Thread()
bool got_block = true;
core::map<v3s16, MapBlock*> modified_blocks;
- {//envlock
-
- //TimeTaker envlockwaittimer("block emerge envlock wait time");
+ bool only_from_disk = false;
- // 0-50ms
- JMutexAutoLock envlock(m_server->m_env_mutex);
+ if(optional)
+ only_from_disk = true;
- //envlockwaittimer.stop();
+ v2s16 chunkpos = map.sector_to_chunk(p2d);
- //TimeTaker timer("block emerge (while env locked)");
-
- try{
- bool only_from_disk = false;
-
- if(optional)
- only_from_disk = true;
+ bool generate_chunk = false;
+ if(only_from_disk == false)
+ {
+ JMutexAutoLock envlock(m_server->m_env_mutex);
+ if(map.chunkNonVolatile(chunkpos) == false)
+ generate_chunk = true;
+ }
+ if(generate_chunk)
+ {
+ ChunkMakeData data;
- // First check if the block already exists
- //block = map.getBlockNoCreate(p);
-
- if(block == NULL)
{
- //dstream<<"Calling emergeBlock"<<std::endl;
- block = map.emergeBlock(
- p,
- only_from_disk,
- changed_blocks,
- lighting_invalidated_blocks);
+ JMutexAutoLock envlock(m_server->m_env_mutex);
+ map.initChunkMake(data, chunkpos);
}
- // If it is a dummy, block was not found on disk
- if(block->isDummy())
- {
- //dstream<<"EmergeThread: Got a dummy block"<<std::endl;
- got_block = false;
+ makeChunk(&data);
- if(only_from_disk == false)
- {
- dstream<<"EmergeThread: wanted to generate a block but got a dummy"<<std::endl;
- assert(0);
- }
+ {
+ JMutexAutoLock envlock(m_server->m_env_mutex);
+ map.finishChunkMake(data, changed_blocks);
}
}
- catch(InvalidPositionException &e)
- {
- // Block not found.
- // This happens when position is over limit.
- got_block = false;
- }
-
- if(got_block)
+
+ /*
+ Fetch block from map or generate a single block
+ */
{
- if(debug && changed_blocks.size() > 0)
+ JMutexAutoLock envlock(m_server->m_env_mutex);
+
+ // Load sector if it isn't loaded
+ if(map.getSectorNoGenerateNoEx(p2d) == NULL)
+ map.loadSectorFull(p2d);
+
+ block = map.getBlockNoCreateNoEx(p);
+ if(!block || block->isDummy())
{
- dout_server<<DTIME<<"Got changed_blocks: ";
- for(core::map<v3s16, MapBlock*>::Iterator i = changed_blocks.getIterator();
- i.atEnd() == false; i++)
+ if(only_from_disk)
{
- MapBlock *block = i.getNode()->getValue();
- v3s16 p = block->getPos();
- dout_server<<"("<<p.X<<","<<p.Y<<","<<p.Z<<") ";
+ got_block = false;
+ }
+ else
+ {
+ // Get, load or create sector
+ ServerMapSector *sector =
+ (ServerMapSector*)map.createSector(p2d);
+ // Generate block
+ block = map.generateBlock(p, block, sector, changed_blocks,
+ lighting_invalidated_blocks);
+ if(block == NULL)
+ got_block = false;
}
- dout_server<<std::endl;
}
+ else
+ {
+ if(block->getLightingExpired()){
+ lighting_invalidated_blocks[block->getPos()] = block;
+ }
+ }
+
+ // TODO: Some additional checking and lighting updating,
+ // see emergeBlock
+ }
+ {//envlock
+ JMutexAutoLock envlock(m_server->m_env_mutex);
+
+ if(got_block)
+ {
/*
Collect a list of blocks that have been modified in
addition to the fetched one.
*/
-
- // Add all the "changed blocks" to modified_blocks
+
+ if(lighting_invalidated_blocks.size() > 0)
+ {
+ /*dstream<<"lighting "<<lighting_invalidated_blocks.size()
+ <<" blocks"<<std::endl;*/
+
+ // 50-100ms for single block generation
+ //TimeTaker timer("** EmergeThread updateLighting");
+
+ // Update lighting without locking the environment mutex,
+ // add modified blocks to changed blocks
+ map.updateLighting(lighting_invalidated_blocks, modified_blocks);
+ }
+
+ // Add all from changed_blocks to modified_blocks
for(core::map<v3s16, MapBlock*>::Iterator i = changed_blocks.getIterator();
i.atEnd() == false; i++)
{
MapBlock *block = i.getNode()->getValue();
modified_blocks.insert(block->getPos(), block);
}
-
- /*dstream<<"lighting "<<lighting_invalidated_blocks.size()
- <<" blocks"<<std::endl;*/
-
- //TimeTaker timer("** updateLighting");
-
- // Update lighting without locking the environment mutex,
- // add modified blocks to changed blocks
- map.updateLighting(lighting_invalidated_blocks, modified_blocks);
}
// If we got no block, there should be no invalidated blocks
else
@@ -278,8 +308,18 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
{
DSTACK(__FUNCTION_NAME);
+ /*u32 timer_result;
+ TimeTaker timer("RemoteClient::GetNextBlocks", &timer_result);*/
+
// Increment timers
- m_nearest_unsent_reset_timer += dtime;
+ m_nothing_to_send_pause_timer -= dtime;
+
+ if(m_nothing_to_send_pause_timer >= 0)
+ {
+ // Keep this reset
+ m_nearest_unsent_reset_timer = 0;
+ return;
+ }
// Won't send anything if already sending
if(m_blocks_sending.size() >= g_settings.getU16
@@ -289,14 +329,21 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
return;
}
+ //TimeTaker timer("RemoteClient::GetNextBlocks");
+
Player *player = server->m_env.getPlayer(peer_id);
assert(player != NULL);
v3f playerpos = player->getPosition();
v3f playerspeed = player->getSpeed();
+ v3f playerspeeddir(0,0,0);
+ if(playerspeed.getLength() > 1.0*BS)
+ playerspeeddir = playerspeed / playerspeed.getLength();
+ // Predict to next block
+ v3f playerpos_predicted = playerpos + playerspeeddir*MAP_BLOCKSIZE*BS;
- v3s16 center_nodepos = floatToInt(playerpos, BS);
+ v3s16 center_nodepos = floatToInt(playerpos_predicted, BS);
v3s16 center = getNodeBlockPos(center_nodepos);
@@ -307,11 +354,12 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
camera_dir.rotateYZBy(player->getPitch());
camera_dir.rotateXZBy(player->getYaw());
+ /*dstream<<"camera_dir=("<<camera_dir.X<<","<<camera_dir.Y<<","
+ <<camera_dir.Z<<")"<<std::endl;*/
+
/*
Get the starting value of the block finder radius.
*/
- s16 last_nearest_unsent_d;
- s16 d_start;
if(m_last_center != center)
{
@@ -321,21 +369,28 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
/*dstream<<"m_nearest_unsent_reset_timer="
<<m_nearest_unsent_reset_timer<<std::endl;*/
- if(m_nearest_unsent_reset_timer > 5.0)
+
+ // This has to be incremented only when the nothing to send pause
+ // is not active
+ m_nearest_unsent_reset_timer += dtime;
+
+ // Reset periodically to avoid possible bugs or other mishaps
+ if(m_nearest_unsent_reset_timer > 10.0)
{
m_nearest_unsent_reset_timer = 0;
m_nearest_unsent_d = 0;
- //dstream<<"Resetting m_nearest_unsent_d"<<std::endl;
+ /*dstream<<"Resetting m_nearest_unsent_d for "
+ <<server->getPlayerName(peer_id)<<std::endl;*/
}
- last_nearest_unsent_d = m_nearest_unsent_d;
-
- d_start = m_nearest_unsent_d;
+ //s16 last_nearest_unsent_d = m_nearest_unsent_d;
+ s16 d_start = m_nearest_unsent_d;
+
+ //dstream<<"d_start="<<d_start<<std::endl;
- u16 maximum_simultaneous_block_sends_setting = g_settings.getU16
+ u16 max_simul_sends_setting = g_settings.getU16
("max_simultaneous_block_sends_per_client");
- u16 maximum_simultaneous_block_sends =
- maximum_simultaneous_block_sends_setting;
+ u16 max_simul_sends_usually = max_simul_sends_setting;
/*
Check the time from last addNode/removeNode.
@@ -346,10 +401,13 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
if(m_time_from_building < g_settings.getFloat(
"full_block_send_enable_min_time_from_building"))
{
- maximum_simultaneous_block_sends
+ max_simul_sends_usually
= LIMITED_MAX_SIMULTANEOUS_BLOCK_SENDS;
}
+ /*
+ Number of blocks sending + number of blocks selected for sending
+ */
u32 num_blocks_selected = m_blocks_sending.size();
/*
@@ -364,9 +422,22 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
s16 d_max = g_settings.getS16("max_block_send_distance");
s16 d_max_gen = g_settings.getS16("max_block_generate_distance");
+ // Don't loop very much at a time
+ if(d_max > d_start+1)
+ d_max = d_start+1;
+ /*if(d_max_gen > d_start+2)
+ d_max_gen = d_start+2;*/
+
//dstream<<"Starting from "<<d_start<<std::endl;
- for(s16 d = d_start; d <= d_max; d++)
+ bool sending_something = false;
+
+ bool no_blocks_found_for_sending = true;
+
+ bool queue_is_full = false;
+
+ s16 d;
+ for(d = d_start; d <= d_max; d++)
{
//dstream<<"RemoteClient::SendBlocks(): d="<<d<<std::endl;
@@ -376,11 +447,11 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
update our d to it.
Else update m_nearest_unsent_d
*/
- if(m_nearest_unsent_d != last_nearest_unsent_d)
+ /*if(m_nearest_unsent_d != last_nearest_unsent_d)
{
d = m_nearest_unsent_d;
last_nearest_unsent_d = m_nearest_unsent_d;
- }
+ }*/
/*
Get the border/face dot coordinates of a "d-radiused"
@@ -402,25 +473,21 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
Also, don't send blocks that are already flying.
*/
- u16 maximum_simultaneous_block_sends_now =
- maximum_simultaneous_block_sends;
+ // Start with the usual maximum
+ u16 max_simul_dynamic = max_simul_sends_usually;
+ // If block is very close, allow full maximum
if(d <= BLOCK_SEND_DISABLE_LIMITS_MAX_D)
- {
- maximum_simultaneous_block_sends_now =
- maximum_simultaneous_block_sends_setting;
- }
+ max_simul_dynamic = max_simul_sends_setting;
- // Limit is dynamically lowered when building
- if(num_blocks_selected
- >= maximum_simultaneous_block_sends_now)
+ // Don't select too many blocks for sending
+ if(num_blocks_selected >= max_simul_dynamic)
{
- /*dstream<<"Not sending more blocks. Queue full. "
- <<m_blocks_sending.size()
- <<std::endl;*/
- goto queue_full;
+ queue_is_full = true;
+ goto queue_full_break;
}
-
+
+ // Don't send blocks that are currently being transferred
if(m_blocks_sending.find(p) != NULL)
continue;
@@ -448,40 +515,40 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
continue;
}
-#if 0
+#if 1
/*
If block is far away, don't generate it unless it is
- near ground level
-
- NOTE: We can't know the ground level this way with the
- new generator.
+ near ground level.
*/
- if(d > 4)
+ if(d >= 4)
{
- v2s16 p2d(p.X, p.Z);
- MapSector *sector = NULL;
- try
- {
- sector = server->m_env.getMap().getSectorNoGenerate(p2d);
- }
- catch(InvalidPositionException &e)
- {
- }
-
- if(sector != NULL)
- {
- // Get center ground height in nodes
- f32 gh = sector->getGroundHeight(
- v2s16(MAP_BLOCKSIZE/2, MAP_BLOCKSIZE/2));
- // Block center y in nodes
- f32 y = (f32)(p.Y * MAP_BLOCKSIZE + MAP_BLOCKSIZE/2);
- // If differs a lot, don't generate
- if(fabs(gh - y) > MAP_BLOCKSIZE*2)
- generate = false;
- }
+ #if 1
+ // Block center y in nodes
+ f32 y = (f32)(p.Y * MAP_BLOCKSIZE + MAP_BLOCKSIZE/2);
+ // Don't generate if it's very high or very low
+ if(y < -64 || y > 64)
+ generate = false;
+ #endif
+ #if 0
+ v2s16 p2d_nodes_center(
+ MAP_BLOCKSIZE*p.X,
+ MAP_BLOCKSIZE*p.Z);
+
+ // Get ground height in nodes
+ s16 gh = server->m_env.getServerMap().findGroundLevel(
+ p2d_nodes_center);
+
+ // If differs a lot, don't generate
+ if(fabs(gh - y) > MAP_BLOCKSIZE*2)
+ generate = false;
+ // Actually, don't even send it
+ //continue;
+ #endif
}
#endif
+ //dstream<<"d="<<d<<std::endl;
+
/*
Don't generate or send if not in sight
*/
@@ -496,7 +563,9 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
*/
{
if(m_blocks_sent.find(p) != NULL)
+ {
continue;
+ }
}
/*
@@ -508,21 +577,43 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
bool block_is_invalid = false;
if(block != NULL)
{
+ // Block is dummy if data doesn't exist.
+ // It means it has been not found from disk and not generated
if(block->isDummy())
{
surely_not_found_on_disk = true;
}
-
+
+ // Block is valid if lighting is up-to-date and data exists
if(block->isValid() == false)
{
block_is_invalid = true;
}
+ /*if(block->isFullyGenerated() == false)
+ {
+ block_is_invalid = true;
+ }*/
+
v2s16 p2d(p.X, p.Z);
ServerMap *map = (ServerMap*)(&server->m_env.getMap());
v2s16 chunkpos = map->sector_to_chunk(p2d);
if(map->chunkNonVolatile(chunkpos) == false)
block_is_invalid = true;
+#if 1
+ /*
+ If block is not close, don't send it unless it is near
+ ground level.
+
+ Block is near ground level if night-time mesh
+ differs from day-time mesh.
+ */
+ if(d > 3)
+ {
+ if(block->dayNightDiffed() == false)
+ continue;
+ }
+#endif
}
/*
@@ -536,13 +627,16 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
}
/*
- Record the lowest d from which a a block has been
+ Record the lowest d from which a block has been
found being not sent and possibly to exist
*/
- if(new_nearest_unsent_d == -1 || d < new_nearest_unsent_d)
+ if(no_blocks_found_for_sending)
{
- new_nearest_unsent_d = d;
+ if(generate == true)
+ new_nearest_unsent_d = d;
}
+
+ no_blocks_found_for_sending = false;
/*
Add inexistent block to emerge queue.
@@ -551,7 +645,8 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
{
//TODO: Get value from somewhere
// Allow only one block in emerge queue
- if(server->m_emerge_queue.peerItemCount(peer_id) < 1)
+ //if(server->m_emerge_queue.peerItemCount(peer_id) < 1)
+ if(server->m_emerge_queue.peerItemCount(peer_id) < 2)
{
//dstream<<"Adding block to emerge queue"<<std::endl;
@@ -578,14 +673,43 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
dest.push_back(q);
num_blocks_selected += 1;
+ sending_something = true;
}
}
-queue_full:
+queue_full_break:
- if(new_nearest_unsent_d != -1)
+ //dstream<<"Stopped at "<<d<<std::endl;
+
+ if(no_blocks_found_for_sending)
{
+ if(queue_is_full == false)
+ new_nearest_unsent_d = d;
+ }
+
+ if(new_nearest_unsent_d != -1)
m_nearest_unsent_d = new_nearest_unsent_d;
+
+ if(sending_something == false)
+ {
+ m_nothing_to_send_counter++;
+ if((s16)m_nothing_to_send_counter >=
+ g_settings.getS16("max_block_send_distance"))
+ {
+ // Pause time in seconds
+ m_nothing_to_send_pause_timer = 1.0;
+ /*dstream<<"nothing to send to "
+ <<server->getPlayerName(peer_id)
+ <<" (d="<<d<<")"<<std::endl;*/
+ }
+ }
+ else
+ {
+ m_nothing_to_send_counter = 0;
}
+
+ /*timer_result = timer.stop(true);
+ if(timer_result != 0)
+ dstream<<"GetNextBlocks duration: "<<timer_result<<" (!=0)"<<std::endl;*/
}
void RemoteClient::SendObjectData(
@@ -728,7 +852,7 @@ void RemoteClient::SendObjectData(
*/
if(stepped_blocks.find(p) == NULL)
{
- block->stepObjects(dtime, true, server->getDayNightRatio());
+ block->stepObjects(dtime, true, server->m_env.getDayNightRatio());
stepped_blocks.insert(p, true);
block->setChangedFlag();
}
@@ -859,6 +983,7 @@ void RemoteClient::SetBlocksNotSent(core::map<v3s16, MapBlock*> &blocks)
PlayerInfo::PlayerInfo()
{
name[0] = 0;
+ avg_rtt = 0;
}
void PlayerInfo::PrintLine(std::ostream *s)
@@ -895,9 +1020,9 @@ Server::Server(
):
m_env(new ServerMap(mapsavedir), this),
m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, this),
+ m_authmanager(mapsavedir+"/auth.txt"),
m_thread(this),
m_emergethread(this),
- m_time_of_day(9000),
m_time_counter(0),
m_time_of_day_send_timer(0),
m_uptime(0),
@@ -916,15 +1041,26 @@ Server::Server(
m_con_mutex.Init();
m_step_dtime_mutex.Init();
m_step_dtime = 0.0;
-
+
+ // Register us to receive map edit events
m_env.getMap().addEventReceiver(this);
+ // If file exists, load environment metadata
+ if(fs::PathExists(m_mapsavedir+"/env_meta.txt"))
+ {
+ dstream<<"Server: Loading environment metadata"<<std::endl;
+ m_env.loadMeta(m_mapsavedir);
+ }
+
// Load players
+ dstream<<"Server: Loading players"<<std::endl;
m_env.deSerializePlayers(m_mapsavedir);
}
Server::~Server()
{
+ dstream<<"Server::~Server()"<<std::endl;
+
/*
Send shutdown message
*/
@@ -946,14 +1082,25 @@ Server::~Server()
if(client->serialization_version == SER_FMT_VER_INVALID)
continue;
- SendChatMessage(client->peer_id, line);
+ try{
+ SendChatMessage(client->peer_id, line);
+ }
+ catch(con::PeerNotFoundException &e)
+ {}
}
}
/*
Save players
*/
+ dstream<<"Server: Saving players"<<std::endl;
m_env.serializePlayers(m_mapsavedir);
+
+ /*
+ Save environment metadata
+ */
+ dstream<<"Server: Saving environment metadata"<<std::endl;
+ m_env.saveMeta(m_mapsavedir);
/*
Stop threads
@@ -1012,11 +1159,6 @@ void Server::stop()
m_emergethread.stop();
dout_server<<"Server: Threads stopped"<<std::endl;
-
- dout_server<<"Server: Saving players"<<std::endl;
- // Save players
- // FIXME: Apparently this does not do anything here
- //m_env.serializePlayers(m_mapsavedir);
}
void Server::step(float dtime)
@@ -1041,12 +1183,16 @@ void Server::AsyncRunStep()
dtime = m_step_dtime;
}
- // Send blocks to clients
- SendBlocks(dtime);
+ {
+ ScopeProfiler sp(&g_profiler, "Server: selecting and sending "
+ "blocks to clients");
+ // Send blocks to clients
+ SendBlocks(dtime);
+ }
if(dtime < 0.001)
return;
-
+
//dstream<<"Server steps "<<dtime<<std::endl;
//dstream<<"Server::AsyncRunStep(): dtime="<<dtime<<std::endl;
@@ -1063,14 +1209,17 @@ void Server::AsyncRunStep()
}
/*
- Update m_time_of_day
+ Update m_time_of_day and overall game time
*/
{
+ JMutexAutoLock envlock(m_env_mutex);
+
m_time_counter += dtime;
f32 speed = g_settings.getFloat("time_speed") * 24000./(24.*3600);
u32 units = (u32)(m_time_counter*speed);
m_time_counter -= (f32)units / speed;
- m_time_of_day.set((m_time_of_day.get() + units) % 24000);
+
+ m_env.setTimeOfDay((m_env.getTimeOfDay() + units) % 24000);
//dstream<<"Server: m_time_of_day = "<<m_time_of_day.get()<<std::endl;
@@ -1094,7 +1243,7 @@ void Server::AsyncRunStep()
//Player *player = m_env.getPlayer(client->peer_id);
SharedBuffer<u8> data = makePacket_TOCLIENT_TIME_OF_DAY(
- m_time_of_day.get());
+ m_env.getTimeOfDay());
// Send as reliable
m_con.Send(client->peer_id, 0, data, true);
}
@@ -1104,12 +1253,14 @@ void Server::AsyncRunStep()
{
// Process connection's timeouts
JMutexAutoLock lock2(m_con_mutex);
+ ScopeProfiler sp(&g_profiler, "Server: connection timeout processing");
m_con.RunTimeouts(dtime);
}
{
// This has to be called so that the client list gets synced
// with the peer list of the connection
+ ScopeProfiler sp(&g_profiler, "Server: peer change handling");
handlePeerChanges();
}
@@ -1117,6 +1268,7 @@ void Server::AsyncRunStep()
// Step environment
// This also runs Map's timers
JMutexAutoLock lock(m_env_mutex);
+ ScopeProfiler sp(&g_profiler, "Server: environment step");
m_env.step(dtime);
}
@@ -1133,7 +1285,9 @@ void Server::AsyncRunStep()
m_liquid_transform_timer -= 1.00;
JMutexAutoLock lock(m_env_mutex);
-
+
+ ScopeProfiler sp(&g_profiler, "Server: liquid transform");
+
core::map<v3s16, MapBlock*> modified_blocks;
m_env.getMap().transformLiquids(modified_blocks);
#if 0
@@ -1189,21 +1343,28 @@ void Server::AsyncRunStep()
{
//u16 peer_id = i.getNode()->getKey();
RemoteClient *client = i.getNode()->getValue();
+ Player *player = m_env.getPlayer(client->peer_id);
+ if(player==NULL)
+ continue;
+ std::cout<<player->getName()<<"\t";
client->PrintInfo(std::cout);
}
}
}
- if(g_settings.getBool("enable_experimental"))
+ //if(g_settings.getBool("enable_experimental"))
{
/*
Check added and deleted active objects
*/
{
+ //dstream<<"Server: Checking added and deleted active objects"<<std::endl;
JMutexAutoLock envlock(m_env_mutex);
JMutexAutoLock conlock(m_con_mutex);
-
+
+ ScopeProfiler sp(&g_profiler, "Server: checking added and deleted objects");
+
// Radius inside which objects are active
s16 radius = 32;
@@ -1214,7 +1375,11 @@ void Server::AsyncRunStep()
RemoteClient *client = i.getNode()->getValue();
Player *player = m_env.getPlayer(client->peer_id);
if(player==NULL)
+ {
+ dstream<<"WARNING: "<<__FUNCTION_NAME<<": Client "<<client->peer_id
+ <<" has no associated player"<<std::endl;
continue;
+ }
v3s16 pos = floatToInt(player->getPosition(), BS);
core::map<u16, bool> removed_objects;
@@ -1226,7 +1391,10 @@ void Server::AsyncRunStep()
// Ignore if nothing happened
if(removed_objects.size() == 0 && added_objects.size() == 0)
+ {
+ //dstream<<"INFO: active objects: none changed"<<std::endl;
continue;
+ }
std::string data_buffer;
@@ -1278,9 +1446,12 @@ void Server::AsyncRunStep()
data_buffer.append(buf, 2);
writeU8((u8*)buf, type);
data_buffer.append(buf, 1);
-
- data_buffer.append(serializeLongString(
- obj->getClientInitializationData()));
+
+ if(obj)
+ data_buffer.append(serializeLongString(
+ obj->getClientInitializationData()));
+ else
+ data_buffer.append(serializeLongString(""));
// Add to known objects
client->m_known_objects.insert(i.getNode()->getKey(), false);
@@ -1302,6 +1473,33 @@ void Server::AsyncRunStep()
<<added_objects.size()<<" added, "
<<"packet size is "<<reply.getSize()<<std::endl;
}
+
+#if 0
+ /*
+ Collect a list of all the objects known by the clients
+ and report it back to the environment.
+ */
+
+ core::map<u16, bool> all_known_objects;
+
+ for(core::map<u16, RemoteClient*>::Iterator
+ i = m_clients.getIterator();
+ i.atEnd() == false; i++)
+ {
+ RemoteClient *client = i.getNode()->getValue();
+ // Go through all known objects of client
+ for(core::map<u16, bool>::Iterator
+ i = client->m_known_objects.getIterator();
+ i.atEnd()==false; i++)
+ {
+ u16 id = i.getNode()->getKey();
+ all_known_objects[id] = true;
+ }
+ }
+
+ m_env.setKnownActiveObjects(whatever);
+#endif
+
}
/*
@@ -1311,6 +1509,8 @@ void Server::AsyncRunStep()
JMutexAutoLock envlock(m_env_mutex);
JMutexAutoLock conlock(m_con_mutex);
+ ScopeProfiler sp(&g_profiler, "Server: sending object messages");
+
// Key = object id
// Value = data sent by object
core::map<u16, core::list<ActiveObjectMessage>* > buffered_messages;
@@ -1437,6 +1637,11 @@ void Server::AsyncRunStep()
dstream<<"Server: MEET_REMOVENODE"<<std::endl;
sendRemoveNode(event->p, event->already_known_by_peer);
}
+ else if(event->type == MEET_BLOCK_NODE_METADATA_CHANGED)
+ {
+ dstream<<"Server: MEET_BLOCK_NODE_METADATA_CHANGED"<<std::endl;
+ setBlockNotSent(event->p);
+ }
else if(event->type == MEET_OTHER)
{
dstream<<"WARNING: Server: MEET_OTHER not implemented"
@@ -1463,6 +1668,9 @@ void Server::AsyncRunStep()
{
JMutexAutoLock lock1(m_env_mutex);
JMutexAutoLock lock2(m_con_mutex);
+
+ ScopeProfiler sp(&g_profiler, "Server: sending mbo positions");
+
SendObjectData(counter);
counter = 0.0;
@@ -1470,6 +1678,39 @@ void Server::AsyncRunStep()
}
/*
+ Step node metadata
+ TODO: Move to ServerEnvironment and utilize active block stuff
+ */
+ /*{
+ //TimeTaker timer("Step node metadata");
+
+ JMutexAutoLock envlock(m_env_mutex);
+ JMutexAutoLock conlock(m_con_mutex);
+
+ ScopeProfiler sp(&g_profiler, "Server: stepping node metadata");
+
+ core::map<v3s16, MapBlock*> changed_blocks;
+ m_env.getMap().nodeMetadataStep(dtime, changed_blocks);
+
+ // Use setBlockNotSent
+
+ for(core::map<v3s16, MapBlock*>::Iterator
+ i = changed_blocks.getIterator();
+ i.atEnd() == false; i++)
+ {
+ MapBlock *block = i.getNode()->getValue();
+
+ for(core::map<u16, RemoteClient*>::Iterator
+ i = m_clients.getIterator();
+ i.atEnd()==false; i++)
+ {
+ RemoteClient *client = i.getNode()->getValue();
+ client->SetBlockNotSent(block->getPos());
+ }
+ }
+ }*/
+
+ /*
Trigger emergethread (it somehow gets to a non-triggered but
bysy state sometimes)
*/
@@ -1484,7 +1725,7 @@ void Server::AsyncRunStep()
}
}
- // Save map
+ // Save map, players and auth stuff
{
float &counter = m_savemap_timer;
counter += dtime;
@@ -1492,8 +1733,14 @@ void Server::AsyncRunStep()
{
counter = 0.0;
- JMutexAutoLock lock(m_env_mutex);
+ ScopeProfiler sp(&g_profiler, "Server: saving stuff");
+ // Auth stuff
+ if(m_authmanager.isModified())
+ m_authmanager.save();
+
+ // Map
+ JMutexAutoLock lock(m_env_mutex);
if(((ServerMap*)(&m_env.getMap()))->isSavingEnabled() == true)
{
// Save only changed parts
@@ -1510,6 +1757,9 @@ void Server::AsyncRunStep()
// Save players
m_env.serializePlayers(m_mapsavedir);
+
+ // Save environment metadata
+ m_env.saveMeta(m_mapsavedir);
}
}
}
@@ -1590,8 +1840,9 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
// [0] u16 TOSERVER_INIT
// [2] u8 SER_FMT_VER_HIGHEST
// [3] u8[20] player_name
+ // [23] u8[28] password <--- can be sent without this, from old versions
- if(datasize < 3)
+ if(datasize < 2+1+PLAYERNAME_SIZE)
return;
derr_server<<DTIME<<"Server: Got TOSERVER_INIT from "
@@ -1623,17 +1874,79 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
*/
// Get player name
- const u32 playername_size = 20;
- char playername[playername_size];
- for(u32 i=0; i<playername_size-1; i++)
+ char playername[PLAYERNAME_SIZE];
+ for(u32 i=0; i<PLAYERNAME_SIZE-1; i++)
{
playername[i] = data[3+i];
}
- playername[playername_size-1] = 0;
+ playername[PLAYERNAME_SIZE-1] = 0;
+
+ if(playername[0]=='\0')
+ {
+ derr_server<<DTIME<<"Server: Player has empty name"<<std::endl;
+ SendAccessDenied(m_con, peer_id,
+ L"Empty name");
+ return;
+ }
+
+ if(string_allowed(playername, PLAYERNAME_ALLOWED_CHARS)==false)
+ {
+ derr_server<<DTIME<<"Server: Player has invalid name"<<std::endl;
+ SendAccessDenied(m_con, peer_id,
+ L"Name contains unallowed characters");
+ return;
+ }
+
+ // Get password
+ char password[PASSWORD_SIZE];
+ if(datasize == 2+1+PLAYERNAME_SIZE)
+ {
+ // old version - assume blank password
+ password[0] = 0;
+ }
+ else
+ {
+ for(u32 i=0; i<PASSWORD_SIZE-1; i++)
+ {
+ password[i] = data[23+i];
+ }
+ password[PASSWORD_SIZE-1] = 0;
+ }
+
+ std::string checkpwd;
+ if(m_authmanager.exists(playername))
+ {
+ checkpwd = m_authmanager.getPassword(playername);
+ }
+ else
+ {
+ checkpwd = g_settings.get("default_password");
+ }
+ if(password != checkpwd && checkpwd != "")
+ {
+ derr_server<<DTIME<<"Server: peer_id="<<peer_id
+ <<": supplied invalid password for "
+ <<playername<<std::endl;
+ SendAccessDenied(m_con, peer_id, L"Invalid password");
+ return;
+ }
+
+ // Add player to auth manager
+ if(m_authmanager.exists(playername) == false)
+ {
+ derr_server<<DTIME<<"Server: adding player "<<playername
+ <<" to auth manager"<<std::endl;
+ m_authmanager.add(playername);
+ m_authmanager.setPassword(playername, checkpwd);
+ m_authmanager.setPrivs(playername,
+ stringToPrivs(g_settings.get("default_privs")));
+ m_authmanager.save();
+ }
+
// Get player
- Player *player = emergePlayer(playername, "", peer_id);
- //Player *player = m_env.getPlayer(peer_id);
+ Player *player = emergePlayer(playername, password, peer_id);
+
/*{
// DEBUG: Test serialization
@@ -1678,18 +1991,30 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
data[20+3-1] = 0;
player->updateName((const char*)&data[3]);
}*/
-
- // Now answer with a TOCLIENT_INIT
- SharedBuffer<u8> reply(2+1+6);
- writeU16(&reply[0], TOCLIENT_INIT);
- writeU8(&reply[2], deployed);
- writeV3S16(&reply[3], floatToInt(player->getPosition()+v3f(0,BS/2,0), BS));
- // Send as reliable
- m_con.Send(peer_id, 0, reply, true);
+ /*
+ Answer with a TOCLIENT_INIT
+ */
+ {
+ SharedBuffer<u8> reply(2+1+6+8);
+ writeU16(&reply[0], TOCLIENT_INIT);
+ writeU8(&reply[2], deployed);
+ writeV3S16(&reply[2+1], floatToInt(player->getPosition()+v3f(0,BS/2,0), BS));
+ //writeU64(&reply[2+1+6], m_env.getServerMap().getSeed());
+ writeU64(&reply[2+1+6], 0); // no seed
+
+ // Send as reliable
+ m_con.Send(peer_id, 0, reply, true);
+ }
+
+ /*
+ Send complete position information
+ */
+ SendMovePlayer(player);
return;
}
+
if(command == TOSERVER_INIT2)
{
derr_server<<DTIME<<"Server: Got TOSERVER_INIT2 from "
@@ -1707,12 +2032,19 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
SendPlayerInfos();
// Send inventory to player
+ UpdateCrafting(peer->id);
SendInventory(peer->id);
+
+ // Send HP
+ {
+ Player *player = m_env.getPlayer(peer_id);
+ SendPlayerHP(player);
+ }
// Send time of day
{
SharedBuffer<u8> data = makePacket_TOCLIENT_TIME_OF_DAY(
- m_time_of_day.get());
+ m_env.getTimeOfDay());
m_con.Send(peer->id, 0, data, true);
}
@@ -1832,6 +2164,9 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
if(datasize < 13)
return;
+ if((getPlayerPrivs(player) & PRIV_BUILD) == 0)
+ return;
+
/*
[0] u16 command
[2] u8 button (0=left, 1=right)
@@ -1900,6 +2235,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
// Add to inventory and send inventory
ilist->addItem(item);
+ UpdateCrafting(player->peer_id);
SendInventory(player->peer_id);
}
@@ -1907,6 +2243,102 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
block->removeObject(id);
}
}
+ else if(command == TOSERVER_CLICK_ACTIVEOBJECT)
+ {
+ if(datasize < 7)
+ return;
+
+ if((getPlayerPrivs(player) & PRIV_BUILD) == 0)
+ return;
+
+ /*
+ length: 7
+ [0] u16 command
+ [2] u8 button (0=left, 1=right)
+ [3] u16 id
+ [5] u16 item
+ */
+ u8 button = readU8(&data[2]);
+ u16 id = readS16(&data[3]);
+ u16 item_i = readU16(&data[11]);
+
+ ServerActiveObject *obj = m_env.getActiveObject(id);
+
+ if(obj == NULL)
+ {
+ derr_server<<"Server: CLICK_ACTIVEOBJECT: object not found"
+ <<std::endl;
+ return;
+ }
+
+ //TODO: Check that object is reasonably close
+
+ // Left click, pick object up (usually)
+ if(button == 0)
+ {
+ InventoryList *ilist = player->inventory.getList("main");
+ if(g_settings.getBool("creative_mode") == false && ilist != NULL)
+ {
+
+ // Skip if inventory has no free space
+ if(ilist->getUsedSlots() == ilist->getSize())
+ {
+ dout_server<<"Player inventory has no free space"<<std::endl;
+ return;
+ }
+
+ // Skip if object has been removed
+ if(obj->m_removed)
+ return;
+
+ /*
+ Create the inventory item
+ */
+ InventoryItem *item = obj->createPickedUpItem();
+
+ if(item)
+ {
+ // Add to inventory and send inventory
+ ilist->addItem(item);
+ UpdateCrafting(player->peer_id);
+ SendInventory(player->peer_id);
+
+ // Remove object from environment
+ obj->m_removed = true;
+ }
+ else
+ {
+ /*
+ Item cannot be picked up. Punch it instead.
+ */
+
+ ToolItem *titem = NULL;
+ std::string toolname = "";
+
+ InventoryList *mlist = player->inventory.getList("main");
+ if(mlist != NULL)
+ {
+ InventoryItem *item = mlist->getItem(item_i);
+ if(item && (std::string)item->getName() == "ToolItem")
+ {
+ titem = (ToolItem*)item;
+ toolname = titem->getToolName();
+ }
+ }
+
+ u16 wear = obj->punch(toolname);
+
+ if(titem)
+ {
+ bool weared_out = titem->addWear(wear);
+ if(weared_out)
+ mlist->deleteItem(item_i);
+ SendInventory(player->peer_id);
+ }
+ }
+ }
+ }
+ }
else if(command == TOSERVER_GROUND_ACTION)
{
if(datasize < 17)
@@ -1968,34 +2400,43 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
// Mandatory parameter; actually used for nothing
core::map<v3s16, MapBlock*> modified_blocks;
- u8 material;
+ u8 material = CONTENT_IGNORE;
u8 mineral = MINERAL_NONE;
+ bool cannot_remove_node = false;
+
try
{
MapNode n = m_env.getMap().getNode(p_under);
+ // Get mineral
+ mineral = n.getMineral();
// Get material at position
material = n.d;
- // If it's not diggable, do nothing
- if(content_diggable(material) == false)
+ // If not yet cancelled
+ if(cannot_remove_node == false)
{
- derr_server<<"Server: Not finishing digging: Node not diggable"
- <<std::endl;
-
- // Client probably has wrong data.
- // Set block not sent, so that client will get
- // a valid one.
- dstream<<"Client "<<peer_id<<" tried to dig "
- <<"node from invalid position; setting"
- <<" MapBlock not sent."<<std::endl;
- RemoteClient *client = getClient(peer_id);
- v3s16 blockpos = getNodeBlockPos(p_under);
- client->SetBlockNotSent(blockpos);
-
- return;
+ // If it's not diggable, do nothing
+ if(content_diggable(material) == false)
+ {
+ derr_server<<"Server: Not finishing digging: "
+ <<"Node not diggable"
+ <<std::endl;
+ cannot_remove_node = true;
+ }
+ }
+ // If not yet cancelled
+ if(cannot_remove_node == false)
+ {
+ // Get node metadata
+ NodeMetadata *meta = m_env.getMap().getNodeMetadata(p_under);
+ if(meta && meta->nodeRemovalDisabled() == true)
+ {
+ derr_server<<"Server: Not finishing digging: "
+ <<"Node metadata disables removal"
+ <<std::endl;
+ cannot_remove_node = true;
+ }
}
- // Get mineral
- mineral = n.getMineral();
}
catch(InvalidPositionException &e)
{
@@ -2004,13 +2445,46 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
<<std::endl;
m_emerge_queue.addBlock(peer_id,
getNodeBlockPos(p_over), BLOCK_EMERGE_FLAG_FROMDISK);
+ cannot_remove_node = true;
+ }
+
+ // Make sure the player is allowed to do it
+ if((getPlayerPrivs(player) & PRIV_BUILD) == 0)
+ {
+ dstream<<"Player "<<player->getName()<<" cannot remove node"
+ <<" because privileges are "<<getPlayerPrivs(player)
+ <<std::endl;
+ cannot_remove_node = true;
+ }
+
+ /*
+ If node can't be removed, set block to be re-sent to
+ client and quit.
+ */
+ if(cannot_remove_node)
+ {
+ derr_server<<"Server: Not finishing digging."<<std::endl;
+
+ // Client probably has wrong data.
+ // Set block not sent, so that client will get
+ // a valid one.
+ dstream<<"Client "<<peer_id<<" tried to dig "
+ <<"node; but node cannot be removed."
+ <<" setting MapBlock not sent."<<std::endl;
+ RemoteClient *client = getClient(peer_id);
+ v3s16 blockpos = getNodeBlockPos(p_under);
+ client->SetBlockNotSent(blockpos);
+
return;
}
/*
- Send the removal to all other clients
+ Send the removal to all other clients.
+ - If other player is close, send REMOVENODE
+ - Otherwise set blocks not sent
*/
- sendRemoveNode(p_over, peer_id);
+ core::list<u16> far_players;
+ sendRemoveNode(p_under, peer_id, &far_players, 100);
/*
Update and send inventory
@@ -2076,6 +2550,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
player->inventory.addItem("main", item);
// Send inventory
+ UpdateCrafting(player->peer_id);
SendInventory(player->peer_id);
}
}
@@ -2087,6 +2562,20 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
m_ignore_map_edit_events = true;
m_env.getMap().removeNodeAndUpdate(p_under, modified_blocks);
m_ignore_map_edit_events = false;
+
+ /*
+ Set blocks not sent to far players
+ */
+ for(core::list<u16>::Iterator
+ i = far_players.begin();
+ i != far_players.end(); i++)
+ {
+ u16 peer_id = *i;
+ RemoteClient *client = getClient(peer_id);
+ if(client==NULL)
+ continue;
+ client->SetBlocksNotSent(modified_blocks);
+ }
}
/*
@@ -2114,7 +2603,15 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
try{
// Don't add a node if this is not a free space
MapNode n2 = m_env.getMap().getNode(p_over);
- if(content_buildable_to(n2.d) == false)
+ bool no_enough_privs =
+ ((getPlayerPrivs(player) & PRIV_BUILD)==0);
+ if(no_enough_privs)
+ dstream<<"Player "<<player->getName()<<" cannot add node"
+ <<" because privileges are "<<getPlayerPrivs(player)
+ <<std::endl;
+
+ if(content_buildable_to(n2.d) == false
+ || no_enough_privs)
{
// Client probably has wrong data.
// Set block not sent, so that client will get
@@ -2151,7 +2648,8 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
/*
Send to all players
*/
- sendAddNode(p_over, n, 0);
+ core::list<u16> far_players;
+ sendAddNode(p_over, n, 0, &far_players, 100);
/*
Handle inventory
@@ -2165,6 +2663,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
else
mitem->remove(1);
// Send inventory
+ UpdateCrafting(peer_id);
SendInventory(peer_id);
}
@@ -2179,6 +2678,20 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
m_ignore_map_edit_events = false;
/*
+ Set blocks not sent to far players
+ */
+ for(core::list<u16>::Iterator
+ i = far_players.begin();
+ i != far_players.end(); i++)
+ {
+ u16 peer_id = *i;
+ RemoteClient *client = getClient(peer_id);
+ if(client==NULL)
+ continue;
+ client->SetBlocksNotSent(modified_blocks);
+ }
+
+ /*
Calculate special events
*/
@@ -2194,68 +2707,39 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
}*/
}
/*
- Handle other items
+ Place other item (not a block)
*/
else
{
v3s16 blockpos = getNodeBlockPos(p_over);
-
- MapBlock *block = NULL;
- try
- {
- block = m_env.getMap().getBlockNoCreate(blockpos);
- }
- catch(InvalidPositionException &e)
+
+ /*
+ Check that the block is loaded so that the item
+ can properly be added to the static list too
+ */
+ MapBlock *block = m_env.getMap().getBlockNoCreateNoEx(blockpos);
+ if(block==NULL)
{
derr_server<<"Error while placing object: "
"block not found"<<std::endl;
return;
}
- v3s16 block_pos_i_on_map = block->getPosRelative();
- v3f block_pos_f_on_map = intToFloat(block_pos_i_on_map, BS);
-
- v3f pos = intToFloat(p_over, BS);
- pos -= block_pos_f_on_map;
+ dout_server<<"Placing a miscellaneous item on map"
+ <<std::endl;
- /*dout_server<<"pos="
- <<"("<<pos.X<<","<<pos.Y<<","<<pos.Z<<")"
- <<std::endl;*/
-
- MapBlockObject *obj = NULL;
+ // Calculate a position for it
+ v3f pos = intToFloat(p_over, BS);
+ //pos.Y -= BS*0.45;
+ pos.Y -= BS*0.25; // let it drop a bit
+ // Randomize a bit
+ pos.X += BS*0.2*(float)myrand_range(-1000,1000)/1000.0;
+ pos.Z += BS*0.2*(float)myrand_range(-1000,1000)/1000.0;
/*
- Handle block object items
+ Create the object
*/
- if(std::string("MBOItem") == item->getName())
- {
- MapBlockObjectItem *oitem = (MapBlockObjectItem*)item;
-
- /*dout_server<<"Trying to place a MapBlockObjectItem: "
- "inventorystring=\""
- <<oitem->getInventoryString()
- <<"\""<<std::endl;*/
-
- obj = oitem->createObject
- (pos, player->getYaw(), player->getPitch());
- }
- /*
- Handle other items
- */
- else
- {
- dout_server<<"Placing a miscellaneous item on map"
- <<std::endl;
- /*
- Create an ItemObject that contains the item.
- */
- ItemObject *iobj = new ItemObject(NULL, -1, pos);
- std::ostringstream os(std::ios_base::binary);
- item->serialize(os);
- dout_server<<"Item string is \""<<os.str()<<"\""<<std::endl;
- iobj->setItemString(os.str());
- obj = iobj;
- }
+ ServerActiveObject *obj = item->createSAO(&m_env, 0, pos);
if(obj == NULL)
{
@@ -2265,16 +2749,34 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
}
else
{
- block->addObject(obj);
-
+ // Add the object to the environment
+ m_env.addActiveObject(obj);
+
dout_server<<"Placed object"<<std::endl;
- InventoryList *ilist = player->inventory.getList("main");
- if(g_settings.getBool("creative_mode") == false && ilist)
+ if(g_settings.getBool("creative_mode") == false)
{
- // Remove from inventory and send inventory
- ilist->deleteItem(item_i);
+ // Delete the right amount of items from the slot
+ u16 dropcount = item->getDropCount();
+
+ // Delete item if all gone
+ if(item->getCount() <= dropcount)
+ {
+ if(item->getCount() < dropcount)
+ dstream<<"WARNING: Server: dropped more items"
+ <<" than the slot contains"<<std::endl;
+
+ InventoryList *ilist = player->inventory.getList("main");
+ if(ilist)
+ // Remove from inventory and send inventory
+ ilist->deleteItem(item_i);
+ }
+ // Else decrement it
+ else
+ item->remove(dropcount);
+
// Send inventory
+ UpdateCrafting(peer_id);
SendInventory(peer_id);
}
}
@@ -2306,6 +2808,8 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
#endif
else if(command == TOSERVER_SIGNTEXT)
{
+ if((getPlayerPrivs(player) & PRIV_BUILD) == 0)
+ return;
/*
u16 command
v3s16 blockpos
@@ -2361,6 +2865,54 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
obj->getBlock()->setChangedFlag();
}
+ else if(command == TOSERVER_SIGNNODETEXT)
+ {
+ if((getPlayerPrivs(player) & PRIV_BUILD) == 0)
+ return;
+ /*
+ u16 command
+ v3s16 p
+ u16 textlen
+ textdata
+ */
+ std::string datastring((char*)&data[2], datasize-2);
+ std::istringstream is(datastring, std::ios_base::binary);
+ u8 buf[6];
+ // Read stuff
+ is.read((char*)buf, 6);
+ v3s16 p = readV3S16(buf);
+ is.read((char*)buf, 2);
+ u16 textlen = readU16(buf);
+ std::string text;
+ for(u16 i=0; i<textlen; i++)
+ {
+ is.read((char*)buf, 1);
+ text += (char)buf[0];
+ }
+
+ NodeMetadata *meta = m_env.getMap().getNodeMetadata(p);
+ if(!meta)
+ return;
+ if(meta->typeId() != CONTENT_SIGN_WALL)
+ return;
+ SignNodeMetadata *signmeta = (SignNodeMetadata*)meta;
+ signmeta->setText(text);
+
+ v3s16 blockpos = getNodeBlockPos(p);
+ MapBlock *block = m_env.getMap().getBlockNoCreateNoEx(blockpos);
+ if(block)
+ {
+ block->setChangedFlag();
+ }
+
+ for(core::map<u16, RemoteClient*>::Iterator
+ i = m_clients.getIterator();
+ i.atEnd()==false; i++)
+ {
+ RemoteClient *client = i.getNode()->getValue();
+ client->SetBlockNotSent(blockpos);
+ }
+ }
else if(command == TOSERVER_INVENTORY_ACTION)
{
/*// Ignore inventory changes if in creative mode
@@ -2378,6 +2930,10 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
InventoryAction *a = InventoryAction::deSerialize(is);
if(a != NULL)
{
+ // Create context
+ InventoryContext c;
+ c.current_player = player;
+
/*
Handle craftresult specially if not in creative mode
*/
@@ -2386,50 +2942,67 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
&& g_settings.getBool("creative_mode") == false)
{
IMoveAction *ma = (IMoveAction*)a;
- // Don't allow moving anything to craftresult
- if(ma->to_name == "craftresult")
- {
- // Do nothing
- disable_action = true;
- }
- // When something is removed from craftresult
- if(ma->from_name == "craftresult")
+ if(ma->to_inv == "current_player" &&
+ ma->from_inv == "current_player")
{
- disable_action = true;
- // Remove stuff from craft
+ InventoryList *rlist = player->inventory.getList("craftresult");
+ assert(rlist);
InventoryList *clist = player->inventory.getList("craft");
- if(clist)
+ assert(clist);
+ InventoryList *mlist = player->inventory.getList("main");
+ assert(mlist);
+ /*
+ Craftresult is no longer preview if something
+ is moved into it
+ */
+ if(ma->to_list == "craftresult"
+ && ma->from_list != "craftresult")
{
- u16 count = ma->count;
- if(count == 0)
- count = 1;
- clist->decrementMaterials(count);
+ // If it currently is a preview, remove
+ // its contents
+ if(player->craftresult_is_preview)
+ {
+ rlist->deleteItem(0);
+ }
+ player->craftresult_is_preview = false;
}
- // Do action
- // Feed action to player inventory
- a->apply(&player->inventory);
- // Eat it
- delete a;
- // If something appeared in craftresult, throw it
- // in the main list
- InventoryList *rlist = player->inventory.getList("craftresult");
- InventoryList *mlist = player->inventory.getList("main");
- if(rlist && mlist && rlist->getUsedSlots() == 1)
+ /*
+ Crafting takes place if this condition is true.
+ */
+ if(player->craftresult_is_preview &&
+ ma->from_list == "craftresult")
+ {
+ player->craftresult_is_preview = false;
+ clist->decrementMaterials(1);
+ }
+ /*
+ If the craftresult is placed on itself, move it to
+ main inventory instead of doing the action
+ */
+ if(ma->to_list == "craftresult"
+ && ma->from_list == "craftresult")
{
+ disable_action = true;
+
InventoryItem *item1 = rlist->changeItem(0, NULL);
mlist->addItem(item1);
}
}
}
+
if(disable_action == false)
{
// Feed action to player inventory
- a->apply(&player->inventory);
- // Eat it
+ a->apply(&c, this);
+ // Eat the action
delete a;
}
- // Send inventory
- SendInventory(player->peer_id);
+ else
+ {
+ // Send inventory
+ UpdateCrafting(player->peer_id);
+ SendInventory(player->peer_id);
+ }
}
else
{
@@ -2470,6 +3043,10 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
// Whether to send to other players
bool send_to_others = false;
+ // Local player gets all privileges regardless of
+ // what's set on their account.
+ u64 privs = getPlayerPrivs(player);
+
// Parse commands
std::wstring commandprefix = L"/#";
if(message.substr(0, commandprefix.size()) == commandprefix)
@@ -2477,73 +3054,36 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
line += L"Server: ";
message = message.substr(commandprefix.size());
- // Get player name as narrow string
- std::string name_s = player->getName();
- // Convert message to narrow string
- std::string message_s = wide_to_narrow(message);
- // Operator is the single name defined in config.
- std::string operator_name = g_settings.get("name");
- bool is_operator = (operator_name != "" &&
- wide_to_narrow(name) == operator_name);
- bool valid_command = false;
- if(message_s == "help")
- {
- line += L"-!- Available commands: ";
- line += L"status ";
- if(is_operator)
- {
- line += L"shutdown setting ";
- }
- else
- {
- }
- send_to_sender = true;
- valid_command = true;
- }
- else if(message_s == "status")
- {
- line = getStatusString();
- send_to_sender = true;
- valid_command = true;
- }
- else if(is_operator)
+
+ ServerCommandContext *ctx = new ServerCommandContext(
+ str_split(message, L' '),
+ this,
+ &m_env,
+ player,
+ privs);
+
+ line += processServerCommand(ctx);
+ send_to_sender = ctx->flags & 1;
+ send_to_others = ctx->flags & 2;
+ delete ctx;
+
+ }
+ else
+ {
+ if(privs & PRIV_SHOUT)
{
- if(message_s == "shutdown")
- {
- dstream<<DTIME<<" Server: Operator requested shutdown."
- <<std::endl;
- m_shutdown_requested.set(true);
-
- line += L"*** Server shutting down (operator request)";
- send_to_sender = true;
- valid_command = true;
- }
- else if(message_s.substr(0,8) == "setting ")
- {
- std::string confline = message_s.substr(8);
- g_settings.parseConfigLine(confline);
- line += L"-!- Setting changed.";
- send_to_sender = true;
- valid_command = true;
- }
+ line += L"<";
+ line += name;
+ line += L"> ";
+ line += message;
+ send_to_others = true;
}
-
- if(valid_command == false)
+ else
{
- line += L"-!- Invalid command: " + message;
+ line += L"Server: You are not allowed to shout";
send_to_sender = true;
}
}
- else
- {
- line += L"<";
- /*if(is_operator)
- line += L"@";*/
- line += name;
- line += L"> ";
- line += message;
- send_to_others = true;
- }
if(line != L"")
{
@@ -2573,6 +3113,93 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
}
}
}
+ else if(command == TOSERVER_DAMAGE)
+ {
+ if(g_settings.getBool("enable_damage"))
+ {
+ std::string datastring((char*)&data[2], datasize-2);
+ std::istringstream is(datastring, std::ios_base::binary);
+ u8 damage = readU8(is);
+ if(player->hp > damage)
+ {
+ player->hp -= damage;
+ }
+ else
+ {
+ player->hp = 0;
+
+ dstream<<"TODO: Server: TOSERVER_HP_DECREMENT: Player dies"
+ <<std::endl;
+
+ v3f pos = findSpawnPos(m_env.getServerMap());
+ player->setPosition(pos);
+ player->hp = 20;
+ SendMovePlayer(player);
+ SendPlayerHP(player);
+
+ //TODO: Throw items around
+ }
+ }
+
+ SendPlayerHP(player);
+ }
+ else if(command == TOSERVER_PASSWORD)
+ {
+ /*
+ [0] u16 TOSERVER_PASSWORD
+ [2] u8[28] old password
+ [30] u8[28] new password
+ */
+
+ if(datasize != 2+PASSWORD_SIZE*2)
+ return;
+ /*char password[PASSWORD_SIZE];
+ for(u32 i=0; i<PASSWORD_SIZE-1; i++)
+ password[i] = data[2+i];
+ password[PASSWORD_SIZE-1] = 0;*/
+ std::string oldpwd;
+ for(u32 i=0; i<PASSWORD_SIZE-1; i++)
+ {
+ char c = data[2+i];
+ if(c == 0)
+ break;
+ oldpwd += c;
+ }
+ std::string newpwd;
+ for(u32 i=0; i<PASSWORD_SIZE-1; i++)
+ {
+ char c = data[2+PASSWORD_SIZE+i];
+ if(c == 0)
+ break;
+ newpwd += c;
+ }
+
+ std::string playername = player->getName();
+
+ if(m_authmanager.exists(playername) == false)
+ {
+ dstream<<"Server: playername not found in authmanager"<<std::endl;
+ // Wrong old password supplied!!
+ SendChatMessage(peer_id, L"playername not found in authmanager");
+ return;
+ }
+
+ std::string checkpwd = m_authmanager.getPassword(playername);
+
+ if(oldpwd != checkpwd)
+ {
+ dstream<<"Server: invalid old password"<<std::endl;
+ // Wrong old password supplied!!
+ SendChatMessage(peer_id, L"Invalid old password supplied. Password NOT changed.");
+ return;
+ }
+
+ m_authmanager.setPassword(playername, newpwd);
+
+ dstream<<"Server: password change successful for "<<playername
+ <<std::endl;
+ SendChatMessage(peer_id, L"Password change successful");
+ }
else
{
derr_server<<"WARNING: Server::ProcessData(): Ignoring "
@@ -2597,6 +3224,74 @@ void Server::onMapEditEvent(MapEditEvent *event)
m_unsent_map_edit_queue.push_back(e);
}
+Inventory* Server::getInventory(InventoryContext *c, std::string id)
+{
+ if(id == "current_player")
+ {
+ assert(c->current_player);
+ return &(c->current_player->inventory);
+ }
+
+ Strfnd fn(id);
+ std::string id0 = fn.next(":");
+
+ if(id0 == "nodemeta")
+ {
+ v3s16 p;
+ p.X = stoi(fn.next(","));
+ p.Y = stoi(fn.next(","));
+ p.Z = stoi(fn.next(","));
+ NodeMetadata *meta = m_env.getMap().getNodeMetadata(p);
+ if(meta)
+ return meta->getInventory();
+ dstream<<"nodemeta at ("<<p.X<<","<<p.Y<<","<<p.Z<<"): "
+ <<"no metadata found"<<std::endl;
+ return NULL;
+ }
+
+ dstream<<__FUNCTION_NAME<<": unknown id "<<id<<std::endl;
+ return NULL;
+}
+void Server::inventoryModified(InventoryContext *c, std::string id)
+{
+ if(id == "current_player")
+ {
+ assert(c->current_player);
+ // Send inventory
+ UpdateCrafting(c->current_player->peer_id);
+ SendInventory(c->current_player->peer_id);
+ return;
+ }
+
+ Strfnd fn(id);
+ std::string id0 = fn.next(":");
+
+ if(id0 == "nodemeta")
+ {
+ v3s16 p;
+ p.X = stoi(fn.next(","));
+ p.Y = stoi(fn.next(","));
+ p.Z = stoi(fn.next(","));
+ v3s16 blockpos = getNodeBlockPos(p);
+
+ NodeMetadata *meta = m_env.getMap().getNodeMetadata(p);
+ if(meta)
+ meta->inventoryModified();
+
+ for(core::map<u16, RemoteClient*>::Iterator
+ i = m_clients.getIterator();
+ i.atEnd()==false; i++)
+ {
+ RemoteClient *client = i.getNode()->getValue();
+ client->SetBlockNotSent(blockpos);
+ }
+
+ return;
+ }
+
+ dstream<<__FUNCTION_NAME<<": unknown id "<<id<<std::endl;
+}
+
core::list<PlayerInfo> Server::getPlayerInfo()
{
DSTACK(__FUNCTION_NAME);
@@ -2666,6 +3361,45 @@ void Server::deletingPeer(con::Peer *peer, bool timeout)
m_peer_change_queue.push_back(c);
}
+/*
+ Static send methods
+*/
+
+void Server::SendHP(con::Connection &con, u16 peer_id, u8 hp)
+{
+ DSTACK(__FUNCTION_NAME);
+ std::ostringstream os(std::ios_base::binary);
+
+ writeU16(os, TOCLIENT_HP);
+ writeU8(os, hp);
+
+ // Make data buffer
+ std::string s = os.str();
+ SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+ // Send as reliable
+ con.Send(peer_id, 0, data, true);
+}
+
+void Server::SendAccessDenied(con::Connection &con, u16 peer_id,
+ const std::wstring &reason)
+{
+ DSTACK(__FUNCTION_NAME);
+ std::ostringstream os(std::ios_base::binary);
+
+ writeU16(os, TOCLIENT_ACCESS_DENIED);
+ os<<serializeWideString(reason);
+
+ // Make data buffer
+ std::string s = os.str();
+ SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+ // Send as reliable
+ con.Send(peer_id, 0, data, true);
+}
+
+/*
+ Non-static send methods
+*/
+
void Server::SendObjectData(float dtime)
{
DSTACK(__FUNCTION_NAME);
@@ -2724,294 +3458,12 @@ void Server::SendPlayerInfos()
m_con.SendToAll(0, data, true);
}
-/*
- Craft checking system
-*/
-
-enum ItemSpecType
-{
- ITEM_NONE,
- ITEM_MATERIAL,
- ITEM_CRAFT,
- ITEM_TOOL,
- ITEM_MBO
-};
-
-struct ItemSpec
-{
- ItemSpec():
- type(ITEM_NONE)
- {
- }
- ItemSpec(enum ItemSpecType a_type, std::string a_name):
- type(a_type),
- name(a_name),
- num(65535)
- {
- }
- ItemSpec(enum ItemSpecType a_type, u16 a_num):
- type(a_type),
- name(""),
- num(a_num)
- {
- }
- enum ItemSpecType type;
- // Only other one of these is used
- std::string name;
- u16 num;
-};
-
-/*
- items: a pointer to an array of 9 pointers to items
- specs: a pointer to an array of 9 ItemSpecs
-*/
-bool checkItemCombination(InventoryItem **items, ItemSpec *specs)
-{
- u16 items_min_x = 100;
- u16 items_max_x = 100;
- u16 items_min_y = 100;
- u16 items_max_y = 100;
- for(u16 y=0; y<3; y++)
- for(u16 x=0; x<3; x++)
- {
- if(items[y*3 + x] == NULL)
- continue;
- if(items_min_x == 100 || x < items_min_x)
- items_min_x = x;
- if(items_min_y == 100 || y < items_min_y)
- items_min_y = y;
- if(items_max_x == 100 || x > items_max_x)
- items_max_x = x;
- if(items_max_y == 100 || y > items_max_y)
- items_max_y = y;
- }
- // No items at all, just return false
- if(items_min_x == 100)
- return false;
-
- u16 items_w = items_max_x - items_min_x + 1;
- u16 items_h = items_max_y - items_min_y + 1;
-
- u16 specs_min_x = 100;
- u16 specs_max_x = 100;
- u16 specs_min_y = 100;
- u16 specs_max_y = 100;
- for(u16 y=0; y<3; y++)
- for(u16 x=0; x<3; x++)
- {
- if(specs[y*3 + x].type == ITEM_NONE)
- continue;
- if(specs_min_x == 100 || x < specs_min_x)
- specs_min_x = x;
- if(specs_min_y == 100 || y < specs_min_y)
- specs_min_y = y;
- if(specs_max_x == 100 || x > specs_max_x)
- specs_max_x = x;
- if(specs_max_y == 100 || y > specs_max_y)
- specs_max_y = y;
- }
- // No specs at all, just return false
- if(specs_min_x == 100)
- return false;
-
- u16 specs_w = specs_max_x - specs_min_x + 1;
- u16 specs_h = specs_max_y - specs_min_y + 1;
-
- // Different sizes
- if(items_w != specs_w || items_h != specs_h)
- return false;
-
- for(u16 y=0; y<specs_h; y++)
- for(u16 x=0; x<specs_w; x++)
- {
- u16 items_x = items_min_x + x;
- u16 items_y = items_min_y + y;
- u16 specs_x = specs_min_x + x;
- u16 specs_y = specs_min_y + y;
- InventoryItem *item = items[items_y * 3 + items_x];
- ItemSpec &spec = specs[specs_y * 3 + specs_x];
-
- if(spec.type == ITEM_NONE)
- {
- // Has to be no item
- if(item != NULL)
- return false;
- continue;
- }
-
- // There should be an item
- if(item == NULL)
- return false;
-
- std::string itemname = item->getName();
-
- if(spec.type == ITEM_MATERIAL)
- {
- if(itemname != "MaterialItem")
- return false;
- MaterialItem *mitem = (MaterialItem*)item;
- if(mitem->getMaterial() != spec.num)
- return false;
- }
- else if(spec.type == ITEM_CRAFT)
- {
- if(itemname != "CraftItem")
- return false;
- CraftItem *mitem = (CraftItem*)item;
- if(mitem->getSubName() != spec.name)
- return false;
- }
- else if(spec.type == ITEM_TOOL)
- {
- // Not supported yet
- assert(0);
- }
- else if(spec.type == ITEM_MBO)
- {
- // Not supported yet
- assert(0);
- }
- else
- {
- // Not supported yet
- assert(0);
- }
- }
-
- return true;
-}
-
void Server::SendInventory(u16 peer_id)
{
DSTACK(__FUNCTION_NAME);
Player* player = m_env.getPlayer(peer_id);
-
- /*
- Calculate crafting stuff
- */
- if(g_settings.getBool("creative_mode") == false)
- {
- InventoryList *clist = player->inventory.getList("craft");
- InventoryList *rlist = player->inventory.getList("craftresult");
- if(rlist)
- {
- rlist->clearItems();
- }
- if(clist && rlist)
- {
- InventoryItem *items[9];
- for(u16 i=0; i<9; i++)
- {
- items[i] = clist->getItem(i);
- }
-
- bool found = false;
-
- // Wood
- if(!found)
- {
- ItemSpec specs[9];
- specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_TREE);
- if(checkItemCombination(items, specs))
- {
- rlist->addItem(new MaterialItem(CONTENT_WOOD, 4));
- found = true;
- }
- }
-
- // Stick
- if(!found)
- {
- ItemSpec specs[9];
- specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
- if(checkItemCombination(items, specs))
- {
- rlist->addItem(new CraftItem("Stick", 4));
- found = true;
- }
- }
-
- // Sign
- if(!found)
- {
- ItemSpec specs[9];
- specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
- specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
- specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
- specs[3] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
- specs[4] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
- specs[5] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
- specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
- if(checkItemCombination(items, specs))
- {
- rlist->addItem(new MapBlockObjectItem("Sign"));
- found = true;
- }
- }
-
- // Torch
- if(!found)
- {
- ItemSpec specs[9];
- specs[0] = ItemSpec(ITEM_CRAFT, "lump_of_coal");
- specs[3] = ItemSpec(ITEM_CRAFT, "Stick");
- if(checkItemCombination(items, specs))
- {
- rlist->addItem(new MaterialItem(CONTENT_TORCH, 4));
- found = true;
- }
- }
-
- // Wooden pick
- if(!found)
- {
- ItemSpec specs[9];
- specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
- specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
- specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
- specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
- specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
- if(checkItemCombination(items, specs))
- {
- rlist->addItem(new ToolItem("WPick", 0));
- found = true;
- }
- }
-
- // Stone pick
- if(!found)
- {
- ItemSpec specs[9];
- specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_STONE);
- specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_STONE);
- specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_STONE);
- specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
- specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
- if(checkItemCombination(items, specs))
- {
- rlist->addItem(new ToolItem("STPick", 0));
- found = true;
- }
- }
-
- // Mese pick
- if(!found)
- {
- ItemSpec specs[9];
- specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_MESE);
- specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_MESE);
- specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_MESE);
- specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
- specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
- if(checkItemCombination(items, specs))
- {
- rlist->addItem(new ToolItem("MesePick", 0));
- found = true;
- }
- }
- }
- } // if creative_mode == false
+ assert(player);
/*
Serialize it
@@ -3078,8 +3530,45 @@ void Server::BroadcastChatMessage(const std::wstring &message)
}
}
-void Server::sendRemoveNode(v3s16 p, u16 ignore_id)
+void Server::SendPlayerHP(Player *player)
{
+ SendHP(m_con, player->peer_id, player->hp);
+}
+
+void Server::SendMovePlayer(Player *player)
+{
+ DSTACK(__FUNCTION_NAME);
+ std::ostringstream os(std::ios_base::binary);
+
+ writeU16(os, TOCLIENT_MOVE_PLAYER);
+ writeV3F1000(os, player->getPosition());
+ writeF1000(os, player->getPitch());
+ writeF1000(os, player->getYaw());
+
+ {
+ v3f pos = player->getPosition();
+ f32 pitch = player->getPitch();
+ f32 yaw = player->getYaw();
+ dstream<<"Server sending TOCLIENT_MOVE_PLAYER"
+ <<" pos=("<<pos.X<<","<<pos.Y<<","<<pos.Z<<")"
+ <<" pitch="<<pitch
+ <<" yaw="<<yaw
+ <<std::endl;
+ }
+
+ // Make data buffer
+ std::string s = os.str();
+ SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+ // Send as reliable
+ m_con.Send(player->peer_id, 0, data, true);
+}
+
+void Server::sendRemoveNode(v3s16 p, u16 ignore_id,
+ core::list<u16> *far_players, float far_d_nodes)
+{
+ float maxd = far_d_nodes*BS;
+ v3f p_f = intToFloat(p, BS);
+
// Create packet
u32 replysize = 8;
SharedBuffer<u8> reply(replysize);
@@ -3101,14 +3590,34 @@ void Server::sendRemoveNode(v3s16 p, u16 ignore_id)
// Don't send if it's the same one
if(client->peer_id == ignore_id)
continue;
+
+ if(far_players)
+ {
+ // Get player
+ Player *player = m_env.getPlayer(client->peer_id);
+ if(player)
+ {
+ // If player is far away, only set modified blocks not sent
+ v3f player_pos = player->getPosition();
+ if(player_pos.getDistanceFrom(p_f) > maxd)
+ {
+ far_players->push_back(client->peer_id);
+ continue;
+ }
+ }
+ }
// Send as reliable
m_con.Send(client->peer_id, 0, reply, true);
}
}
-void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id)
+void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id,
+ core::list<u16> *far_players, float far_d_nodes)
{
+ float maxd = far_d_nodes*BS;
+ v3f p_f = intToFloat(p, BS);
+
for(core::map<u16, RemoteClient*>::Iterator
i = m_clients.getIterator();
i.atEnd() == false; i++)
@@ -3123,6 +3632,22 @@ void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id)
if(client->peer_id == ignore_id)
continue;
+ if(far_players)
+ {
+ // Get player
+ Player *player = m_env.getPlayer(client->peer_id);
+ if(player)
+ {
+ // If player is far away, only set modified blocks not sent
+ v3f player_pos = player->getPosition();
+ if(player_pos.getDistanceFrom(p_f) > maxd)
+ {
+ far_players->push_back(client->peer_id);
+ continue;
+ }
+ }
+ }
+
// Create packet
u32 replysize = 8 + MapNode::serializedLength(client->serialization_version);
SharedBuffer<u8> reply(replysize);
@@ -3137,9 +3662,44 @@ void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id)
}
}
+void Server::setBlockNotSent(v3s16 p)
+{
+ for(core::map<u16, RemoteClient*>::Iterator
+ i = m_clients.getIterator();
+ i.atEnd()==false; i++)
+ {
+ RemoteClient *client = i.getNode()->getValue();
+ client->SetBlockNotSent(p);
+ }
+}
+
void Server::SendBlockNoLock(u16 peer_id, MapBlock *block, u8 ver)
{
DSTACK(__FUNCTION_NAME);
+
+ v3s16 p = block->getPos();
+
+#if 0
+ // Analyze it a bit
+ bool completely_air = true;
+ for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
+ for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
+ for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
+ {
+ if(block->getNodeNoEx(v3s16(x0,y0,z0)).d != CONTENT_AIR)
+ {
+ completely_air = false;
+ x0 = y0 = z0 = MAP_BLOCKSIZE; // Break out
+ }
+ }
+
+ // Print result
+ dstream<<"Server: Sending block ("<<p.X<<","<<p.Y<<","<<p.Z<<"): ";
+ if(completely_air)
+ dstream<<"[completely air] ";
+ dstream<<std::endl;
+#endif
+
/*
Create a packet with the block in the right format
*/
@@ -3151,14 +3711,13 @@ void Server::SendBlockNoLock(u16 peer_id, MapBlock *block, u8 ver)
u32 replysize = 8 + blockdata.getSize();
SharedBuffer<u8> reply(replysize);
- v3s16 p = block->getPos();
writeU16(&reply[0], TOCLIENT_BLOCKDATA);
writeS16(&reply[2], p.X);
writeS16(&reply[4], p.Y);
writeS16(&reply[6], p.Z);
memcpy(&reply[8], *blockdata, blockdata.getSize());
- /*dstream<<"Sending block ("<<p.X<<","<<p.Y<<","<<p.Z<<")"
+ /*dstream<<"Server: Sending block ("<<p.X<<","<<p.Y<<","<<p.Z<<")"
<<": \tpacket size: "<<replysize<<std::endl;*/
/*
@@ -3179,20 +3738,24 @@ void Server::SendBlocks(float dtime)
core::array<PrioritySortedBlockTransfer> queue;
s32 total_sending = 0;
-
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd() == false; i++)
+
{
- RemoteClient *client = i.getNode()->getValue();
- assert(client->peer_id == i.getNode()->getKey());
+ ScopeProfiler sp(&g_profiler, "Server: selecting blocks for sending");
- total_sending += client->SendingCount();
-
- if(client->serialization_version == SER_FMT_VER_INVALID)
- continue;
-
- client->GetNextBlocks(this, dtime, queue);
+ for(core::map<u16, RemoteClient*>::Iterator
+ i = m_clients.getIterator();
+ i.atEnd() == false; i++)
+ {
+ RemoteClient *client = i.getNode()->getValue();
+ assert(client->peer_id == i.getNode()->getKey());
+
+ total_sending += client->SendingCount();
+
+ if(client->serialization_version == SER_FMT_VER_INVALID)
+ continue;
+
+ client->GetNextBlocks(this, dtime, queue);
+ }
}
// Sort.
@@ -3229,6 +3792,372 @@ void Server::SendBlocks(float dtime)
}
}
+/*
+ Something random
+*/
+
+void Server::UpdateCrafting(u16 peer_id)
+{
+ DSTACK(__FUNCTION_NAME);
+
+ Player* player = m_env.getPlayer(peer_id);
+ assert(player);
+
+ /*
+ Calculate crafting stuff
+ */
+ if(g_settings.getBool("creative_mode") == false)
+ {
+ InventoryList *clist = player->inventory.getList("craft");
+ InventoryList *rlist = player->inventory.getList("craftresult");
+
+ if(rlist->getUsedSlots() == 0)
+ player->craftresult_is_preview = true;
+
+ if(rlist && player->craftresult_is_preview)
+ {
+ rlist->clearItems();
+ }
+ if(clist && rlist && player->craftresult_is_preview)
+ {
+ InventoryItem *items[9];
+ for(u16 i=0; i<9; i++)
+ {
+ items[i] = clist->getItem(i);
+ }
+
+ bool found = false;
+
+ // Wood
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_TREE);
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new MaterialItem(CONTENT_WOOD, 4));
+ found = true;
+ }
+ }
+
+ // Stick
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new CraftItem("Stick", 4));
+ found = true;
+ }
+ }
+
+ // Fence
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[3] = ItemSpec(ITEM_CRAFT, "Stick");
+ specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
+ specs[5] = ItemSpec(ITEM_CRAFT, "Stick");
+ specs[6] = ItemSpec(ITEM_CRAFT, "Stick");
+ specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
+ specs[8] = ItemSpec(ITEM_CRAFT, "Stick");
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new MaterialItem(CONTENT_FENCE, 2));
+ found = true;
+ }
+ }
+
+ // Sign
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[3] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[4] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[5] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
+ if(checkItemCombination(items, specs))
+ {
+ //rlist->addItem(new MapBlockObjectItem("Sign"));
+ rlist->addItem(new MaterialItem(CONTENT_SIGN_WALL, 1));
+ found = true;
+ }
+ }
+
+ // Torch
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[0] = ItemSpec(ITEM_CRAFT, "lump_of_coal");
+ specs[3] = ItemSpec(ITEM_CRAFT, "Stick");
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new MaterialItem(CONTENT_TORCH, 4));
+ found = true;
+ }
+ }
+
+ // Wooden pick
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
+ specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new ToolItem("WPick", 0));
+ found = true;
+ }
+ }
+
+ // Stone pick
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
+ specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
+ specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
+ specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
+ specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new ToolItem("STPick", 0));
+ found = true;
+ }
+ }
+
+ // Steel pick
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[0] = ItemSpec(ITEM_CRAFT, "steel_ingot");
+ specs[1] = ItemSpec(ITEM_CRAFT, "steel_ingot");
+ specs[2] = ItemSpec(ITEM_CRAFT, "steel_ingot");
+ specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
+ specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new ToolItem("SteelPick", 0));
+ found = true;
+ }
+ }
+
+ // Mese pick
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_MESE);
+ specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_MESE);
+ specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_MESE);
+ specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
+ specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new ToolItem("MesePick", 0));
+ found = true;
+ }
+ }
+
+ // Wooden shovel
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
+ specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new ToolItem("WShovel", 0));
+ found = true;
+ }
+ }
+
+ // Stone shovel
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
+ specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
+ specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new ToolItem("STShovel", 0));
+ found = true;
+ }
+ }
+
+ // Steel shovel
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[1] = ItemSpec(ITEM_CRAFT, "steel_ingot");
+ specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
+ specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new ToolItem("SteelShovel", 0));
+ found = true;
+ }
+ }
+
+ // Wooden axe
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[3] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
+ specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new ToolItem("WAxe", 0));
+ found = true;
+ }
+ }
+
+ // Stone axe
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
+ specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
+ specs[3] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
+ specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
+ specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new ToolItem("STAxe", 0));
+ found = true;
+ }
+ }
+
+ // Steel axe
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[0] = ItemSpec(ITEM_CRAFT, "steel_ingot");
+ specs[1] = ItemSpec(ITEM_CRAFT, "steel_ingot");
+ specs[3] = ItemSpec(ITEM_CRAFT, "steel_ingot");
+ specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
+ specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new ToolItem("SteelAxe", 0));
+ found = true;
+ }
+ }
+
+ // Wooden sword
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[4] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new ToolItem("WSword", 0));
+ found = true;
+ }
+ }
+
+ // Stone sword
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
+ specs[4] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
+ specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new ToolItem("STSword", 0));
+ found = true;
+ }
+ }
+
+ // Steel sword
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[1] = ItemSpec(ITEM_CRAFT, "steel_ingot");
+ specs[4] = ItemSpec(ITEM_CRAFT, "steel_ingot");
+ specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new ToolItem("SteelSword", 0));
+ found = true;
+ }
+ }
+
+ // Chest
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[3] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[5] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[6] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[7] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ specs[8] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new MaterialItem(CONTENT_CHEST, 1));
+ found = true;
+ }
+ }
+
+ // Furnace
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
+ specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
+ specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
+ specs[3] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
+ specs[5] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
+ specs[6] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
+ specs[7] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
+ specs[8] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new MaterialItem(CONTENT_FURNACE, 1));
+ found = true;
+ }
+ }
+
+ // Steel block
+ if(!found)
+ {
+ ItemSpec specs[9];
+ specs[0] = ItemSpec(ITEM_CRAFT, "steel_ingot");
+ specs[1] = ItemSpec(ITEM_CRAFT, "steel_ingot");
+ specs[2] = ItemSpec(ITEM_CRAFT, "steel_ingot");
+ specs[3] = ItemSpec(ITEM_CRAFT, "steel_ingot");
+ specs[4] = ItemSpec(ITEM_CRAFT, "steel_ingot");
+ specs[5] = ItemSpec(ITEM_CRAFT, "steel_ingot");
+ specs[6] = ItemSpec(ITEM_CRAFT, "steel_ingot");
+ specs[7] = ItemSpec(ITEM_CRAFT, "steel_ingot");
+ specs[8] = ItemSpec(ITEM_CRAFT, "steel_ingot");
+ if(checkItemCombination(items, specs))
+ {
+ rlist->addItem(new MaterialItem(CONTENT_STEEL, 1));
+ found = true;
+ }
+ }
+ }
+
+ } // if creative_mode == false
+}
RemoteClient* Server::getClient(u16 peer_id)
{
@@ -3245,8 +4174,10 @@ std::wstring Server::getStatusString()
{
std::wostringstream os(std::ios_base::binary);
os<<L"# Server: ";
+ // Version
+ os<<L"version="<<narrow_to_wide(VERSION_STRING);
// Uptime
- os<<L"uptime="<<m_uptime.get();
+ os<<L", uptime="<<m_uptime.get();
// Information about clients
os<<L", clients={";
for(core::map<u16, RemoteClient*>::Iterator
@@ -3278,14 +4209,24 @@ void setCreativeInventory(Player *player)
{
player->resetInventory();
- // Give some good picks
+ // Give some good tools
{
- InventoryItem *item = new ToolItem("STPick", 0);
+ InventoryItem *item = new ToolItem("MesePick", 0);
void* r = player->inventory.addItem("main", item);
assert(r == NULL);
}
{
- InventoryItem *item = new ToolItem("MesePick", 0);
+ InventoryItem *item = new ToolItem("SteelPick", 0);
+ void* r = player->inventory.addItem("main", item);
+ assert(r == NULL);
+ }
+ {
+ InventoryItem *item = new ToolItem("SteelAxe", 0);
+ void* r = player->inventory.addItem("main", item);
+ assert(r == NULL);
+ }
+ {
+ InventoryItem *item = new ToolItem("SteelShovel", 0);
void* r = player->inventory.addItem("main", item);
assert(r == NULL);
}
@@ -3293,6 +4234,40 @@ void setCreativeInventory(Player *player)
/*
Give materials
*/
+
+ // CONTENT_IGNORE-terminated list
+ u8 material_items[] = {
+ CONTENT_TORCH,
+ CONTENT_COBBLE,
+ CONTENT_MUD,
+ CONTENT_STONE,
+ CONTENT_SAND,
+ CONTENT_TREE,
+ CONTENT_LEAVES,
+ CONTENT_GLASS,
+ CONTENT_FENCE,
+ CONTENT_MESE,
+ CONTENT_WATERSOURCE,
+ CONTENT_CLOUD,
+ CONTENT_CHEST,
+ CONTENT_FURNACE,
+ CONTENT_SIGN_WALL,
+ CONTENT_IGNORE
+ };
+
+ u8 *mip = material_items;
+ for(u16 i=0; i<PLAYER_INVENTORY_SIZE; i++)
+ {
+ if(*mip == CONTENT_IGNORE)
+ break;
+
+ InventoryItem *item = new MaterialItem(*mip, 1);
+ player->inventory.addItem("main", item);
+
+ mip++;
+ }
+
+#if 0
assert(USEFUL_CONTENT_COUNT <= PLAYER_INVENTORY_SIZE);
// add torch first
@@ -3310,16 +4285,65 @@ void setCreativeInventory(Player *player)
InventoryItem *item = new MaterialItem(i, 1);
player->inventory.addItem("main", item);
}
- // Sign
+#endif
+
+ /*// Sign
{
InventoryItem *item = new MapBlockObjectItem("Sign Example text");
void* r = player->inventory.addItem("main", item);
assert(r == NULL);
+ }*/
+}
+
+v3f findSpawnPos(ServerMap &map)
+{
+ //return v3f(50,50,50)*BS;
+
+ v2s16 nodepos;
+ s16 groundheight = 0;
+
+ // Try to find a good place a few times
+ for(s32 i=0; i<1000; i++)
+ {
+ s32 range = 1 + i;
+ // We're going to try to throw the player to this position
+ nodepos = v2s16(-range + (myrand()%(range*2)),
+ -range + (myrand()%(range*2)));
+ v2s16 sectorpos = getNodeSectorPos(nodepos);
+ // Get sector (NOTE: Don't get because it's slow)
+ //m_env.getMap().emergeSector(sectorpos);
+ // Get ground height at point (fallbacks to heightmap function)
+ groundheight = map.findGroundLevel(nodepos);
+ // Don't go underwater
+ if(groundheight < WATER_LEVEL)
+ {
+ //dstream<<"-> Underwater"<<std::endl;
+ continue;
+ }
+ // Don't go to high places
+ if(groundheight > WATER_LEVEL + 4)
+ {
+ //dstream<<"-> Underwater"<<std::endl;
+ continue;
+ }
+
+ // Found a good place
+ //dstream<<"Searched through "<<i<<" places."<<std::endl;
+ break;
}
+
+ // If no suitable place was not found, go above water at least.
+ if(groundheight < WATER_LEVEL)
+ groundheight = WATER_LEVEL;
+
+ return intToFloat(v3s16(
+ nodepos.X,
+ groundheight + 2,
+ nodepos.Y
+ ), BS);
}
-Player *Server::emergePlayer(const char *name, const char *password,
- u16 peer_id)
+Player *Server::emergePlayer(const char *name, const char *password, u16 peer_id)
{
/*
Try to get an existing player
@@ -3365,6 +4389,10 @@ Player *Server::emergePlayer(const char *name, const char *password,
//player->peer_id = PEER_ID_INEXISTENT;
player->peer_id = peer_id;
player->updateName(name);
+ m_authmanager.add(name);
+ m_authmanager.setPassword(name, password);
+ m_authmanager.setPrivs(name,
+ stringToPrivs(g_settings.get("default_privs")));
/*
Set player position
@@ -3373,87 +4401,9 @@ Player *Server::emergePlayer(const char *name, const char *password,
dstream<<"Server: Finding spawn place for player \""
<<player->getName()<<"\""<<std::endl;
- v2s16 nodepos;
-#if 0
- player->setPosition(intToFloat(v3s16(
- 0,
- 45, //64,
- 0
- ), BS));
-#endif
-#if 1
- s16 groundheight = 0;
-#if 1
- // Try to find a good place a few times
- for(s32 i=0; i<500; i++)
- {
- s32 range = 1 + i;
- // We're going to try to throw the player to this position
- nodepos = v2s16(-range + (myrand()%(range*2)),
- -range + (myrand()%(range*2)));
- v2s16 sectorpos = getNodeSectorPos(nodepos);
- /*
- Ignore position if it is near a chunk edge.
- Otherwise it would cause excessive loading time at
- initial generation
- */
- {
- if(m_env.getServerMap().sector_to_chunk(sectorpos+v2s16(1,1))
- != m_env.getServerMap().sector_to_chunk(sectorpos+v2s16(-1,-1)))
- continue;
- }
- // Get sector
- m_env.getMap().emergeSector(sectorpos);
- // Get ground height at point
- groundheight = m_env.getServerMap().findGroundLevel(nodepos);
- // Don't go underwater
- if(groundheight < WATER_LEVEL)
- {
- //dstream<<"-> Underwater"<<std::endl;
- continue;
- }
-#if 0 // Doesn't work, generating blocks is a bit too complicated for doing here
- // Get block at point
- v3s16 nodepos3d;
- nodepos3d = v3s16(nodepos.X, groundheight+1, nodepos.Y);
- v3s16 blockpos = getNodeBlockPos(nodepos3d);
- ((ServerMap*)(&m_env.getMap()))->emergeBlock(blockpos);
- // Don't go inside ground
- try{
- /*v3s16 footpos(nodepos.X, groundheight+1, nodepos.Y);
- v3s16 headpos(nodepos.X, groundheight+2, nodepos.Y);*/
- v3s16 footpos = nodepos3d + v3s16(0,0,0);
- v3s16 headpos = nodepos3d + v3s16(0,1,0);
- if(m_env.getMap().getNode(footpos).d != CONTENT_AIR
- || m_env.getMap().getNode(headpos).d != CONTENT_AIR)
- {
- dstream<<"-> Inside ground"<<std::endl;
- // In ground
- continue;
- }
- }catch(InvalidPositionException &e)
- {
- dstream<<"-> Invalid position"<<std::endl;
- // Ignore invalid position
- continue;
- }
-#endif
- // Found a good place
- dstream<<"Searched through "<<i<<" places."<<std::endl;
- break;
- }
-#endif
-
- // If no suitable place was not found, go above water at least.
- if(groundheight < WATER_LEVEL)
- groundheight = WATER_LEVEL;
+ v3f pos = findSpawnPos(m_env.getServerMap());
- player->setPosition(intToFloat(v3s16(
- nodepos.X,
- groundheight + 1,
- nodepos.Y
- ), BS));
-#endif
+ player->setPosition(pos);
/*
Add player to environment
@@ -3469,13 +4419,33 @@ Player *Server::emergePlayer(const char *name, const char *password,
{
setCreativeInventory(player);
}
- else
+ else if(g_settings.getBool("give_initial_stuff"))
{
- /*{
- InventoryItem *item = new ToolItem("WPick", 32000);
+ {
+ InventoryItem *item = new ToolItem("SteelPick", 0);
void* r = player->inventory.addItem("main", item);
assert(r == NULL);
- }*/
+ }
+ {
+ InventoryItem *item = new MaterialItem(CONTENT_TORCH, 99);
+ void* r = player->inventory.addItem("main", item);
+ assert(r == NULL);
+ }
+ {
+ InventoryItem *item = new ToolItem("SteelAxe", 0);
+ void* r = player->inventory.addItem("main", item);
+ assert(r == NULL);
+ }
+ {
+ InventoryItem *item = new ToolItem("SteelShovel", 0);
+ void* r = player->inventory.addItem("main", item);
+ assert(r == NULL);
+ }
+ {
+ InventoryItem *item = new MaterialItem(CONTENT_COBBLE, 99);
+ void* r = player->inventory.addItem("main", item);
+ assert(r == NULL);
+ }
/*{
InventoryItem *item = new MaterialItem(CONTENT_MESE, 6);
void* r = player->inventory.addItem("main", item);
@@ -3506,13 +4476,7 @@ Player *Server::emergePlayer(const char *name, const char *password,
void* r = player->inventory.addItem("main", item);
assert(r == NULL);
}*/
- /*// Give some lights
- {
- InventoryItem *item = new MaterialItem(CONTENT_TORCH, 999);
- bool r = player->inventory.addItem("main", item);
- assert(r == true);
- }
- // and some signs
+ /*// and some signs
for(u16 i=0; i<4; i++)
{
InventoryItem *item = new MapBlockObjectItem("Sign Example text");
@@ -3532,30 +4496,6 @@ Player *Server::emergePlayer(const char *name, const char *password,
} // create new player
}
-#if 0
-void Server::UpdateBlockWaterPressure(MapBlock *block,
- core::map<v3s16, MapBlock*> &modified_blocks)
-{
- MapVoxelManipulator v(&m_env.getMap());
- v.m_disable_water_climb =
- g_settings.getBool("disable_water_climb");
-
- VoxelArea area(block->getPosRelative(),
- block->getPosRelative() + v3s16(1,1,1)*(MAP_BLOCKSIZE-1));
-
- try
- {
- v.updateAreaWaterPressure(area, m_flow_active_nodes);
- }
- catch(ProcessingLimitException &e)
- {
- dstream<<"Processing limit reached (1)"<<std::endl;
- }
-
- v.blitBack(modified_blocks);
-}
-#endif
-
void Server::handlePeerChange(PeerChange &c)
{
JMutexAutoLock envlock(m_env_mutex);
@@ -3591,6 +4531,23 @@ void Server::handlePeerChange(PeerChange &c)
// The client should exist
assert(n != NULL);
+ /*
+ Mark objects to be not known by the client
+ */
+ RemoteClient *client = n->getValue();
+ // Handle objects
+ for(core::map<u16, bool>::Iterator
+ i = client->m_known_objects.getIterator();
+ i.atEnd()==false; i++)
+ {
+ // Get object
+ u16 id = i.getNode()->getKey();
+ ServerActiveObject* obj = m_env.getActiveObject(id);
+
+ if(obj && obj->m_known_by_count > 0)
+ obj->m_known_by_count--;
+ }
+
// Collect information about leaving in chat
std::wstring message;
{
@@ -3649,29 +4606,69 @@ void Server::handlePeerChanges()
}
}
+u64 Server::getPlayerPrivs(Player *player)
+{
+ if(player==NULL)
+ return 0;
+ std::string playername = player->getName();
+ // Local player gets all privileges regardless of
+ // what's set on their account.
+ if(g_settings.get("name") == playername)
+ {
+ return PRIV_ALL;
+ }
+ else
+ {
+ return getPlayerAuthPrivs(playername);
+ }
+}
+
void dedicated_server_loop(Server &server, bool &kill)
{
DSTACK(__FUNCTION_NAME);
- std::cout<<DTIME<<std::endl;
- std::cout<<"========================"<<std::endl;
- std::cout<<"Running dedicated server"<<std::endl;
- std::cout<<"========================"<<std::endl;
- std::cout<<std::endl;
+ dstream<<DTIME<<std::endl;
+ dstream<<"========================"<<std::endl;
+ dstream<<"Running dedicated server"<<std::endl;
+ dstream<<"========================"<<std::endl;
+ dstream<<std::endl;
+
+ IntervalLimiter m_profiler_interval;
for(;;)
{
// This is kind of a hack but can be done like this
// because server.step() is very light
- sleep_ms(30);
+ {
+ ScopeProfiler sp(&g_profiler, "dedicated server sleep");
+ sleep_ms(30);
+ }
server.step(0.030);
if(server.getShutdownRequested() || kill)
{
- std::cout<<DTIME<<" dedicated_server_loop(): Quitting."<<std::endl;
+ dstream<<DTIME<<" dedicated_server_loop(): Quitting."<<std::endl;
break;
}
+ /*
+ Profiler
+ */
+ float profiler_print_interval =
+ g_settings.getFloat("profiler_print_interval");
+ if(profiler_print_interval != 0)
+ {
+ if(m_profiler_interval.step(0.030, profiler_print_interval))
+ {
+ dstream<<"Profiler:"<<std::endl;
+ g_profiler.print(dstream);
+ g_profiler.clear();
+ }
+ }
+
+ /*
+ Player info
+ */
static int counter = 0;
counter--;
if(counter <= 0)
@@ -3684,10 +4681,10 @@ void dedicated_server_loop(Server &server, bool &kill)
u32 sum = PIChecksum(list);
if(sum != sum_old)
{
- std::cout<<DTIME<<"Player info:"<<std::endl;
+ dstream<<DTIME<<"Player info:"<<std::endl;
for(i=list.begin(); i!=list.end(); i++)
{
- i->PrintLine(&std::cout);
+ i->PrintLine(&dstream);
}
}
sum_old = sum;
diff --git a/src/server.h b/src/server.h
index f997828b2..791ecdec7 100644
--- a/src/server.h
+++ b/src/server.h
@@ -1,6 +1,6 @@
/*
Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -31,7 +31,18 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "utility.h"
#include "porting.h"
#include "map.h"
+#include "inventory.h"
+#include "auth.h"
+/*
+ Some random functions
+*/
+v3f findSpawnPos(ServerMap &map);
+
+/*
+ A structure containing the data needed for queueing the fetching
+ of blocks.
+*/
struct QueuedBlockEmerge
{
v3s16 pos;
@@ -240,6 +251,8 @@ public:
pending_serialization_version = SER_FMT_VER_INVALID;
m_nearest_unsent_d = 0;
m_nearest_unsent_reset_timer = 0.0;
+ m_nothing_to_send_counter = 0;
+ m_nothing_to_send_pause_timer = 0;
}
~RemoteClient()
{
@@ -284,7 +297,7 @@ public:
void PrintInfo(std::ostream &o)
{
o<<"RemoteClient "<<peer_id<<": "
- <<", m_blocks_sent.size()="<<m_blocks_sent.size()
+ <<"m_blocks_sent.size()="<<m_blocks_sent.size()
<<", m_blocks_sending.size()="<<m_blocks_sending.size()
<<", m_nearest_unsent_d="<<m_nearest_unsent_d
<<", m_excess_gotblocks="<<m_excess_gotblocks
@@ -340,9 +353,14 @@ private:
This is resetted by PrintInfo()
*/
u32 m_excess_gotblocks;
+
+ // CPU usage optimization
+ u32 m_nothing_to_send_counter;
+ float m_nothing_to_send_pause_timer;
};
-class Server : public con::PeerHandler, public MapEventReceiver
+class Server : public con::PeerHandler, public MapEventReceiver,
+ public InventoryManager
{
public:
/*
@@ -365,14 +383,21 @@ public:
core::list<PlayerInfo> getPlayerInfo();
- u32 getDayNightRatio()
+ /*u32 getDayNightRatio()
{
return time_to_daynight_ratio(m_time_of_day.get());
+ }*/
+
+ // Environment must be locked when called
+ void setTimeOfDay(u32 time)
+ {
+ m_env.setTimeOfDay(time);
+ m_time_of_day_send_timer = 0;
}
bool getShutdownRequested()
{
- return m_shutdown_requested.get();
+ return m_shutdown_requested;
}
/*
@@ -382,58 +407,132 @@ public:
*/
void onMapEditEvent(MapEditEvent *event);
+ /*
+ Shall be called with the environment and the connection locked.
+ */
+ Inventory* getInventory(InventoryContext *c, std::string id);
+ void inventoryModified(InventoryContext *c, std::string id);
+
+ // Connection must be locked when called
+ std::wstring getStatusString();
+
+ void requestShutdown(void)
+ {
+ m_shutdown_requested = true;
+ }
+
+
+ // Envlock and conlock should be locked when calling this
+ void SendMovePlayer(Player *player);
+
+ u64 getPlayerAuthPrivs(const std::string &name)
+ {
+ try{
+ return m_authmanager.getPrivs(name);
+ }
+ catch(AuthNotFoundException &e)
+ {
+ dstream<<"WARNING: Auth not found for "<<name<<std::endl;
+ return 0;
+ }
+ }
+
+ void setPlayerAuthPrivs(const std::string &name, u64 privs)
+ {
+ try{
+ return m_authmanager.setPrivs(name, privs);
+ }
+ catch(AuthNotFoundException &e)
+ {
+ dstream<<"WARNING: Auth not found for "<<name<<std::endl;
+ }
+ }
+
private:
- // Virtual methods from con::PeerHandler.
+ // con::PeerHandler implementation.
+ // These queue stuff to be processed by handlePeerChanges().
// As of now, these create and remove clients and players.
void peerAdded(con::Peer *peer);
void deletingPeer(con::Peer *peer, bool timeout);
+ /*
+ Static send methods
+ */
+
+ static void SendHP(con::Connection &con, u16 peer_id, u8 hp);
+ static void SendAccessDenied(con::Connection &con, u16 peer_id,
+ const std::wstring &reason);
+
+ /*
+ Non-static send methods
+ */
+
// Envlock and conlock should be locked when calling these
void SendObjectData(float dtime);
void SendPlayerInfos();
void SendInventory(u16 peer_id);
void SendChatMessage(u16 peer_id, const std::wstring &message);
void BroadcastChatMessage(const std::wstring &message);
- void sendRemoveNode(v3s16 p, u16 ignore_id=0);
- void sendAddNode(v3s16 p, MapNode n, u16 ignore_id=0);
+ void SendPlayerHP(Player *player);
+ /*
+ Send a node removal/addition event to all clients except ignore_id.
+ Additionally, if far_players!=NULL, players further away than
+ far_d_nodes are ignored and their peer_ids are added to far_players
+ */
+ // Envlock and conlock should be locked when calling these
+ void sendRemoveNode(v3s16 p, u16 ignore_id=0,
+ core::list<u16> *far_players=NULL, float far_d_nodes=100);
+ void sendAddNode(v3s16 p, MapNode n, u16 ignore_id=0,
+ core::list<u16> *far_players=NULL, float far_d_nodes=100);
+ void setBlockNotSent(v3s16 p);
- // Environment and Connection must be locked when called
+ // Environment and Connection must be locked when called
void SendBlockNoLock(u16 peer_id, MapBlock *block, u8 ver);
- // Sends blocks to clients
+ // Sends blocks to clients (locks env and con on its own)
void SendBlocks(float dtime);
+
+ /*
+ Something random
+ */
+
+ void UpdateCrafting(u16 peer_id);
// When called, connection mutex should be locked
RemoteClient* getClient(u16 peer_id);
- // Connection must be locked when called
- std::wstring getStatusString();
-
+ // When called, environment mutex should be locked
+ std::string getPlayerName(u16 peer_id)
+ {
+ Player *player = m_env.getPlayer(peer_id);
+ if(player == NULL)
+ return "[id="+itos(peer_id);
+ return player->getName();
+ }
+
/*
Get a player from memory or creates one.
If player is already connected, return NULL
+ The password is not checked here - it is only used to
+ set the password if a new player is created.
Call with env and con locked.
*/
- Player *emergePlayer(const char *name, const char *password,
- u16 peer_id);
-
- /*
- Update water pressure.
- This also adds suitable nodes to active_nodes.
-
- environment has to be locked when calling.
- */
- /*void UpdateBlockWaterPressure(MapBlock *block,
- core::map<v3s16, MapBlock*> &modified_blocks);*/
+ Player *emergePlayer(const char *name, const char *password, u16 peer_id);
// Locks environment and connection by its own
struct PeerChange;
void handlePeerChange(PeerChange &c);
void handlePeerChanges();
+
+ u64 getPlayerPrivs(Player *player);
+
+ /*
+ Variables
+ */
- //float m_flowwater_timer;
+ // Some timers
float m_liquid_transform_timer;
float m_print_info_timer;
float m_objectdata_timer;
@@ -442,52 +541,86 @@ private:
// NOTE: If connection and environment are both to be locked,
// environment shall be locked first.
- JMutex m_env_mutex;
- ServerEnvironment m_env;
- JMutex m_con_mutex;
+ // Environment
+ ServerEnvironment m_env;
+ JMutex m_env_mutex;
+
+ // Connection
con::Connection m_con;
- core::map<u16, RemoteClient*> m_clients; // Behind the con mutex
+ JMutex m_con_mutex;
+ // Connected clients (behind the con mutex)
+ core::map<u16, RemoteClient*> m_clients;
+ // User authentication
+ AuthManager m_authmanager;
+
+ /*
+ Threads
+ */
+
+ // A buffer for time steps
+ // step() increments and AsyncRunStep() run by m_thread reads it.
float m_step_dtime;
JMutex m_step_dtime_mutex;
+ // The server mainly operates in this thread
ServerThread m_thread;
+ // This thread fetches and generates map
EmergeThread m_emergethread;
-
+ // Queue of block coordinates to be processed by the emerge thread
BlockEmergeQueue m_emerge_queue;
- // Nodes that are destinations of flowing liquid at the moment
- //core::map<v3s16, u8> m_flow_active_nodes;
+ /*
+ Time related stuff
+ */
// 0-23999
- MutexedVariable<u32> m_time_of_day;
+ //MutexedVariable<u32> m_time_of_day;
// Used to buffer dtime for adding to m_time_of_day
float m_time_counter;
+ // Timer for sending time of day over network
float m_time_of_day_send_timer;
-
+ // Uptime of server in seconds
MutexedVariable<double> m_uptime;
+ /*
+ Peer change queue.
+ Queues stuff from peerAdded() and deletingPeer() to
+ handlePeerChanges()
+ */
enum PeerChangeType
{
PEER_ADDED,
PEER_REMOVED
};
-
struct PeerChange
{
PeerChangeType type;
u16 peer_id;
bool timeout;
};
-
Queue<PeerChange> m_peer_change_queue;
+ /*
+ Random stuff
+ */
+
+ // Map directory
std::string m_mapsavedir;
- MutexedVariable<bool> m_shutdown_requested;
+ bool m_shutdown_requested;
/*
+ Map edit event queue. Automatically receives all map edits.
+ The constructor of this class registers us to receive them through
+ onMapEditEvent
+
+ NOTE: Should these be moved to actually be members of
+ ServerEnvironment?
+ */
+
+ /*
Queue of map edits from the environment for sending to the clients
This is behind m_env_mutex
*/
@@ -505,6 +638,8 @@ private:
*/
u16 m_ignore_map_edit_events_peer_id;
+ Profiler *m_profiler;
+
friend class EmergeThread;
friend class RemoteClient;
};
diff --git a/src/servercommand.cpp b/src/servercommand.cpp
new file mode 100644
index 000000000..333e29084
--- /dev/null
+++ b/src/servercommand.cpp
@@ -0,0 +1,236 @@
+/*
+Part of Minetest-c55
+Copyright (C) 2010-11 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2011 Ciaran Gultnieks <ciaran@ciarang.com>
+
+Permission to use, copy, modify, and distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#include "servercommand.h"
+#include "utility.h"
+
+void cmd_status(std::wostringstream &os,
+ ServerCommandContext *ctx)
+{
+ os<<ctx->server->getStatusString();
+}
+
+void cmd_privs(std::wostringstream &os,
+ ServerCommandContext *ctx)
+{
+ if(ctx->parms.size() == 1)
+ {
+ // Show our own real privs, without any adjustments
+ // made for admin status
+ os<<L"-!- " + narrow_to_wide(privsToString(
+ ctx->server->getPlayerAuthPrivs(ctx->player->getName())));
+ return;
+ }
+
+ if((ctx->privs & PRIV_PRIVS) == 0)
+ {
+ os<<L"-!- You don't have permission to do that";
+ return;
+ }
+
+ Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
+ if(tp == NULL)
+ {
+ os<<L"-!- No such player";
+ return;
+ }
+
+ os<<L"-!- " + narrow_to_wide(privsToString(ctx->server->getPlayerAuthPrivs(tp->getName())));
+}
+
+void cmd_grantrevoke(std::wostringstream &os,
+ ServerCommandContext *ctx)
+{
+ if(ctx->parms.size() != 3)
+ {
+ os<<L"-!- Missing parameter";
+ return;
+ }
+
+ if((ctx->privs & PRIV_PRIVS) == 0)
+ {
+ os<<L"-!- You don't have permission to do that";
+ return;
+ }
+
+ u64 newprivs = stringToPrivs(wide_to_narrow(ctx->parms[2]));
+ if(newprivs == PRIV_INVALID)
+ {
+ os<<L"-!- Invalid privileges specified";
+ return;
+ }
+
+ Player *tp = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
+ if(tp == NULL)
+ {
+ os<<L"-!- No such player";
+ return;
+ }
+
+ std::string playername = wide_to_narrow(ctx->parms[1]);
+ u64 privs = ctx->server->getPlayerAuthPrivs(playername);
+
+ if(ctx->parms[0] == L"grant")
+ privs |= newprivs;
+ else
+ privs &= ~newprivs;
+
+ ctx->server->setPlayerAuthPrivs(playername, privs);
+
+ os<<L"-!- Privileges change to ";
+ os<<narrow_to_wide(privsToString(privs));
+}
+
+void cmd_time(std::wostringstream &os,
+ ServerCommandContext *ctx)
+{
+ if(ctx->parms.size() != 2)
+ {
+ os<<L"-!- Missing parameter";
+ return;
+ }
+
+ if((ctx->privs & PRIV_SETTIME) ==0)
+ {
+ os<<L"-!- You don't have permission to do that";
+ return;
+ }
+
+ u32 time = stoi(wide_to_narrow(ctx->parms[1]));
+ ctx->server->setTimeOfDay(time);
+ os<<L"-!- time_of_day changed.";
+}
+
+void cmd_shutdown(std::wostringstream &os,
+ ServerCommandContext *ctx)
+{
+ if((ctx->privs & PRIV_SERVER) ==0)
+ {
+ os<<L"-!- You don't have permission to do that";
+ return;
+ }
+
+ dstream<<DTIME<<" Server: Operator requested shutdown."
+ <<std::endl;
+ ctx->server->requestShutdown();
+
+ os<<L"*** Server shutting down (operator request)";
+ ctx->flags |= 2;
+}
+
+void cmd_setting(std::wostringstream &os,
+ ServerCommandContext *ctx)
+{
+ if((ctx->privs & PRIV_SERVER) ==0)
+ {
+ os<<L"-!- You don't have permission to do that";
+ return;
+ }
+
+ std::string confline = wide_to_narrow(ctx->parms[1] + L" = " + ctx->parms[2]);
+ g_settings.parseConfigLine(confline);
+ os<< L"-!- Setting changed.";
+}
+
+void cmd_teleport(std::wostringstream &os,
+ ServerCommandContext *ctx)
+{
+ if((ctx->privs & PRIV_TELEPORT) ==0)
+ {
+ os<<L"-!- You don't have permission to do that";
+ return;
+ }
+
+ if(ctx->parms.size() != 2)
+ {
+ os<<L"-!- Missing parameter";
+ return;
+ }
+
+ std::vector<std::wstring> coords = str_split(ctx->parms[1], L',');
+ if(coords.size() != 3)
+ {
+ os<<L"-!- You can only specify coordinates currently";
+ return;
+ }
+
+ v3f dest(stoi(coords[0])*10, stoi(coords[1])*10, stoi(coords[2])*10);
+ ctx->player->setPosition(dest);
+ ctx->server->SendMovePlayer(ctx->player);
+
+ os<< L"-!- Teleported.";
+}
+
+
+std::wstring processServerCommand(ServerCommandContext *ctx)
+{
+
+ std::wostringstream os(std::ios_base::binary);
+ ctx->flags = 1; // Default, unless we change it.
+
+ u64 privs = ctx->privs;
+
+ if(ctx->parms.size() == 0 || ctx->parms[0] == L"help")
+ {
+ os<<L"-!- Available commands: ";
+ os<<L"status privs ";
+ if(privs & PRIV_SERVER)
+ os<<L"shutdown setting ";
+ if(privs & PRIV_SETTIME)
+ os<<L" time";
+ if(privs & PRIV_TELEPORT)
+ os<<L" teleport";
+ if(privs & PRIV_PRIVS)
+ os<<L" grant revoke";
+ }
+ else if(ctx->parms[0] == L"status")
+ {
+ cmd_status(os, ctx);
+ }
+ else if(ctx->parms[0] == L"privs")
+ {
+ cmd_privs(os, ctx);
+ }
+ else if(ctx->parms[0] == L"grant" || ctx->parms[0] == L"revoke")
+ {
+ cmd_grantrevoke(os, ctx);
+ }
+ else if(ctx->parms[0] == L"time")
+ {
+ cmd_time(os, ctx);
+ }
+ else if(ctx->parms[0] == L"shutdown")
+ {
+ cmd_shutdown(os, ctx);
+ }
+ else if(ctx->parms[0] == L"setting")
+ {
+ cmd_setting(os, ctx);
+ }
+ else if(ctx->parms[0] == L"teleport")
+ {
+ cmd_teleport(os, ctx);
+ }
+ else
+ {
+ os<<L"-!- Invalid command: " + ctx->parms[0];
+ }
+ return os.str();
+}
+
+
diff --git a/src/servercommand.h b/src/servercommand.h
new file mode 100644
index 000000000..9013bc2a6
--- /dev/null
+++ b/src/servercommand.h
@@ -0,0 +1,60 @@
+/*
+Part of Minetest-c55
+Copyright (C) 2010-11 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2011 Ciaran Gultnieks <ciaran@ciarang.com>
+
+Permission to use, copy, modify, and distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#ifndef SERVERCOMMAND_HEADER
+#define SERVERCOMMAND_HEADER
+
+#include <vector>
+#include <sstream>
+#include "common_irrlicht.h"
+#include "player.h"
+#include "server.h"
+
+struct ServerCommandContext
+{
+
+ std::vector<std::wstring> parms;
+ Server* server;
+ ServerEnvironment *env;
+ Player* player;
+ // Effective privs for the player, which may be different to their
+ // stored ones - e.g. if they are named in the config as an admin.
+ u64 privs;
+ u32 flags;
+
+ ServerCommandContext(
+ std::vector<std::wstring> parms,
+ Server* server,
+ ServerEnvironment *env,
+ Player* player,
+ u64 privs)
+ : parms(parms), server(server), env(env), player(player), privs(privs)
+ {
+ }
+
+};
+
+// Process a command sent from a client. The environment and connection
+// should be locked when this is called.
+// Returns a response message, to be dealt with according to the flags set
+// in the context.
+std::wstring processServerCommand(ServerCommandContext *ctx);
+
+#endif
+
+
diff --git a/src/servermain.cpp b/src/servermain.cpp
index 254b1f28a..f83e2ae76 100644
--- a/src/servermain.cpp
+++ b/src/servermain.cpp
@@ -79,6 +79,9 @@ Settings g_settings;
extern void set_default_settings();
+// Global profiler
+Profiler g_profiler;
+
// A dummy thing
ITextureSource *g_texturesource = NULL;
@@ -301,7 +304,7 @@ int main(int argc, char *argv[])
}
// Figure out path to map
- std::string map_dir = porting::path_userdata+"/map";
+ std::string map_dir = porting::path_userdata+"/world";
if(cmd_args.exists("map-dir"))
map_dir = cmd_args.get("map-dir");
else if(g_settings.exists("map-dir"))
diff --git a/src/serverobject.cpp b/src/serverobject.cpp
index e62f1efd0..d31e9a31c 100644
--- a/src/serverobject.cpp
+++ b/src/serverobject.cpp
@@ -20,11 +20,18 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "serverobject.h"
#include <fstream>
#include "environment.h"
+#include "inventory.h"
+#include "collision.h"
+
+core::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
ServerActiveObject::ServerActiveObject(ServerEnvironment *env, u16 id, v3f pos):
ActiveObject(id),
m_known_by_count(0),
m_removed(false),
+ m_pending_deactivation(false),
+ m_static_exists(false),
+ m_static_block(1337,1337,1337),
m_env(env),
m_base_position(pos)
{
@@ -34,18 +41,59 @@ ServerActiveObject::~ServerActiveObject()
{
}
+ServerActiveObject* ServerActiveObject::create(u8 type,
+ ServerEnvironment *env, u16 id, v3f pos,
+ const std::string &data)
+{
+ // Find factory function
+ core::map<u16, Factory>::Node *n;
+ n = m_types.find(type);
+ if(n == NULL)
+ {
+ // If factory is not found, just return.
+ dstream<<"WARNING: ServerActiveObject: No factory for type="
+ <<type<<std::endl;
+ return NULL;
+ }
+
+ Factory f = n->getValue();
+ ServerActiveObject *object = (*f)(env, id, pos, data);
+ return object;
+}
+
+void ServerActiveObject::registerType(u16 type, Factory f)
+{
+ core::map<u16, Factory>::Node *n;
+ n = m_types.find(type);
+ if(n)
+ return;
+ m_types.insert(type, f);
+}
+
+
/*
TestSAO
*/
+// Prototype
+TestSAO proto_TestSAO(NULL, 0, v3f(0,0,0));
+
TestSAO::TestSAO(ServerEnvironment *env, u16 id, v3f pos):
ServerActiveObject(env, id, pos),
m_timer1(0),
m_age(0)
{
+ ServerActiveObject::registerType(getType(), create);
}
-void TestSAO::step(float dtime, Queue<ActiveObjectMessage> &messages)
+ServerActiveObject* TestSAO::create(ServerEnvironment *env, u16 id, v3f pos,
+ const std::string &data)
+{
+ return new TestSAO(env, id, pos);
+}
+
+void TestSAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
+ bool send_recommended)
{
m_age += dtime;
if(m_age > 10)
@@ -58,6 +106,9 @@ void TestSAO::step(float dtime, Queue<ActiveObjectMessage> &messages)
if(m_base_position.Y > 8*BS)
m_base_position.Y = 2*BS;
+ if(send_recommended == false)
+ return;
+
m_timer1 -= dtime;
if(m_timer1 < 0.0)
{
@@ -79,727 +130,541 @@ void TestSAO::step(float dtime, Queue<ActiveObjectMessage> &messages)
}
}
-/*
- LuaSAO
-*/
-
-extern "C"{
-#include "lstring.h"
-}
-
-/*
- Callbacks in script:
-
- on_step(self, dtime)
- on_get_client_init_data(self)
- on_get_server_init_data(self)
- on_initialize(self, data)
-*/
-
-/*
- object_remove(x,y,z)
-*/
-static int lf_object_remove(lua_State *L)
-{
- // 1: self
- LuaSAO *self = (LuaSAO*)lua_touserdata(L, -1);
- lua_pop(L, 1);
-
- assert(self);
-
- self->m_removed = true;
-
- return 0;
-}
/*
- ServerEnvironment object_get_environment(self)
+ ItemSAO
*/
-static int lf_object_get_environment(lua_State *L)
-{
- // 1: self
- LuaSAO *self = (LuaSAO*)lua_touserdata(L, -1);
- lua_pop(L, 1);
-
- assert(self);
-
- lua_pushlightuserdata(L, self->getEnv());
- return 1;
-}
-/*
- object_set_base_position(self, {X=,Y=,Z=})
-*/
-static int lf_object_set_base_position(lua_State *L)
-{
- // 2: position
- assert(lua_istable(L, -1));
- lua_pushstring(L, "X");
- lua_gettable(L, -2);
- lua_Number x = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pushstring(L, "Y");
- lua_gettable(L, -2);
- lua_Number y = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pushstring(L, "Z");
- lua_gettable(L, -2);
- lua_Number z = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pop(L, 1);
- // 1: self
- LuaSAO *self = (LuaSAO*)lua_touserdata(L, -1);
- lua_pop(L, 1);
-
- assert(self);
-
- self->setBasePosition(v3f(x*BS,y*BS,z*BS));
-
- return 0; // Number of return values
-}
+// Prototype
+ItemSAO proto_ItemSAO(NULL, 0, v3f(0,0,0), "");
-/*
- {X=,Y=,Z=} object_get_base_position(self)
-*/
-static int lf_object_get_base_position(lua_State *L)
+ItemSAO::ItemSAO(ServerEnvironment *env, u16 id, v3f pos,
+ const std::string inventorystring):
+ ServerActiveObject(env, id, pos),
+ m_inventorystring(inventorystring),
+ m_speed_f(0,0,0),
+ m_last_sent_position(0,0,0)
{
- // 1: self
- LuaSAO *self = (LuaSAO*)lua_touserdata(L, -1);
- lua_pop(L, 1);
-
- assert(self);
-
- v3f pos = self->getBasePosition();
-
- lua_newtable(L);
-
- lua_pushstring(L, "X");
- lua_pushnumber(L, pos.X/BS);
- lua_settable(L, -3);
-
- lua_pushstring(L, "Y");
- lua_pushnumber(L, pos.Y/BS);
- lua_settable(L, -3);
-
- lua_pushstring(L, "Z");
- lua_pushnumber(L, pos.Z/BS);
- lua_settable(L, -3);
-
- return 1; // Number of return values
+ ServerActiveObject::registerType(getType(), create);
}
-/*
- object_add_message(self, string data)
- lf = luafunc
-*/
-static int lf_object_add_message(lua_State *L)
+ServerActiveObject* ItemSAO::create(ServerEnvironment *env, u16 id, v3f pos,
+ const std::string &data)
{
- // 2: data
- size_t datalen = 0;
- const char *data_c = lua_tolstring(L, -1, &datalen);
- lua_pop(L, 1);
- // 1: self
- LuaSAO *self = (LuaSAO*)lua_touserdata(L, -1);
- lua_pop(L, 1);
-
- assert(self);
- assert(data_c);
-
- std::string data(data_c, datalen);
- //dstream<<"object_add_message: data="<<data<<std::endl;
-
- // Create message and add to queue
- ActiveObjectMessage aom(self->getId());
- aom.reliable = true;
- aom.datastring = data;
- self->m_message_queue.push_back(aom);
-
- return 0; // Number of return values
+ std::istringstream is(data, std::ios::binary);
+ char buf[1];
+ // read version
+ is.read(buf, 1);
+ u8 version = buf[0];
+ // check if version is supported
+ if(version != 0)
+ return NULL;
+ std::string inventorystring = deSerializeString(is);
+ dstream<<"ItemSAO::create(): Creating item \""
+ <<inventorystring<<"\""<<std::endl;
+ return new ItemSAO(env, id, pos, inventorystring);
}
-/*
- env_get_node(env, {X=,Y=,Z=})
-*/
-static int lf_env_get_node(lua_State *L)
+void ItemSAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
+ bool send_recommended)
{
- // 2: position
- assert(lua_istable(L, -1));
- lua_pushstring(L, "X");
- lua_gettable(L, -2);
- lua_Number x = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pushstring(L, "Y");
- lua_gettable(L, -2);
- lua_Number y = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pushstring(L, "Z");
- lua_gettable(L, -2);
- lua_Number z = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pop(L, 1);
- // 1: env
- ServerEnvironment *env = (ServerEnvironment*)lua_touserdata(L, -1);
- lua_pop(L, 1);
-
- assert(env);
+ assert(m_env);
- v3s16 pos = floatToInt(v3f(x,y,z), 1.0);
+ const float interval = 0.2;
+ if(m_move_interval.step(dtime, interval)==false)
+ return;
+ dtime = interval;
+
+ core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
+ collisionMoveResult moveresult;
+ // Apply gravity
+ m_speed_f += v3f(0, -dtime*9.81*BS, 0);
+ // Maximum movement without glitches
+ f32 pos_max_d = BS*0.25;
+ // Limit speed
+ if(m_speed_f.getLength()*dtime > pos_max_d)
+ m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
+ v3f pos_f = getBasePosition();
+ v3f pos_f_old = pos_f;
+ moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
+ box, dtime, pos_f, m_speed_f);
+
+ if(send_recommended == false)
+ return;
- /*dstream<<"Checking node from pos=("<<pos.X<<","<<pos.Y<<","<<pos.Z
- <<")"<<std::endl;*/
-
- // Get the node
- MapNode n(CONTENT_IGNORE);
- n = env->getMap().getNodeNoEx(pos);
-
- // Create a table with some data about the node
- lua_newtable(L);
- lua_pushstring(L, "content");
- lua_pushinteger(L, n.d);
- lua_settable(L, -3);
- lua_pushstring(L, "param1");
- lua_pushinteger(L, n.param);
- lua_settable(L, -3);
- lua_pushstring(L, "param2");
- lua_pushinteger(L, n.param2);
- lua_settable(L, -3);
-
- // Return the table
- return 1;
+ if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
+ {
+ setBasePosition(pos_f);
+ m_last_sent_position = pos_f;
+
+ std::ostringstream os(std::ios::binary);
+ char buf[6];
+ // command (0 = update position)
+ buf[0] = 0;
+ os.write(buf, 1);
+ // pos
+ writeS32((u8*)buf, m_base_position.X*1000);
+ os.write(buf, 4);
+ writeS32((u8*)buf, m_base_position.Y*1000);
+ os.write(buf, 4);
+ writeS32((u8*)buf, m_base_position.Z*1000);
+ os.write(buf, 4);
+ // create message and add to list
+ ActiveObjectMessage aom(getId(), false, os.str());
+ messages.push_back(aom);
+ }
}
-/*
- get_content_features(content)
-*/
-static int lf_get_content_features(lua_State *L)
+std::string ItemSAO::getClientInitializationData()
{
- MapNode n;
-
- // 1: content
- n.d = lua_tointeger(L, -1);
- lua_pop(L, 1);
-
- // Get and return information
- ContentFeatures &f = content_features(n.d);
-
- lua_newtable(L);
- lua_pushstring(L, "walkable");
- lua_pushboolean(L, f.walkable);
- lua_settable(L, -3);
- lua_pushstring(L, "diggable");
- lua_pushboolean(L, f.diggable);
- lua_settable(L, -3);
- lua_pushstring(L, "buildable_to");
- lua_pushboolean(L, f.buildable_to);
- lua_settable(L, -3);
-
- return 1;
+ std::ostringstream os(std::ios::binary);
+ char buf[6];
+ // version
+ buf[0] = 0;
+ os.write(buf, 1);
+ // pos
+ writeS32((u8*)buf, m_base_position.X*1000);
+ os.write(buf, 4);
+ writeS32((u8*)buf, m_base_position.Y*1000);
+ os.write(buf, 4);
+ writeS32((u8*)buf, m_base_position.Z*1000);
+ os.write(buf, 4);
+ // inventorystring
+ os<<serializeString(m_inventorystring);
+ return os.str();
}
-/*
- bool env_dig_node(env, {X=,Y=,Z=})
- Return true on success
-*/
-static int lf_env_dig_node(lua_State *L)
+std::string ItemSAO::getStaticData()
{
- // 2: position
- assert(lua_istable(L, -1));
- lua_pushstring(L, "X");
- lua_gettable(L, -2);
- lua_Number x = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pushstring(L, "Y");
- lua_gettable(L, -2);
- lua_Number y = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pushstring(L, "Z");
- lua_gettable(L, -2);
- lua_Number z = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pop(L, 1);
- // 1: env
- ServerEnvironment *env = (ServerEnvironment*)lua_touserdata(L, -1);
- lua_pop(L, 1);
- assert(env);
-
- v3s16 pos = floatToInt(v3f(x,y,z), 1.0);
-
- /*
- Do stuff.
- This gets sent to the server by the map through the edit
- event system.
- */
- bool succeeded = env->getMap().removeNodeWithEvent(pos);
-
- lua_pushboolean(L, succeeded);
- return 1;
+ dstream<<__FUNCTION_NAME<<std::endl;
+ std::ostringstream os(std::ios::binary);
+ char buf[1];
+ // version
+ buf[0] = 0;
+ os.write(buf, 1);
+ // inventorystring
+ os<<serializeString(m_inventorystring);
+ return os.str();
}
-/*
- bool env_place_node(env, {X=,Y=,Z=}, node)
- node={content=,param1=,param2=}
- param1 and param2 are optional
- Return true on success
-*/
-static int lf_env_place_node(lua_State *L)
+InventoryItem * ItemSAO::createInventoryItem()
{
- // 3: node
- MapNode n(CONTENT_STONE);
- assert(lua_istable(L, -1));
+ try{
+ std::istringstream is(m_inventorystring, std::ios_base::binary);
+ InventoryItem *item = InventoryItem::deSerialize(is);
+ dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
+ <<m_inventorystring<<"\" -> item="<<item
+ <<std::endl;
+ return item;
+ }
+ catch(SerializationError &e)
{
- lua_pushstring(L, "content");
- lua_gettable(L, -2);
- if(lua_isnumber(L, -1))
- n.d = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pushstring(L, "param1");
- lua_gettable(L, -2);
- if(lua_isnumber(L, -1))
- n.param = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pushstring(L, "param2");
- lua_gettable(L, -2);
- if(lua_isnumber(L, -1))
- n.param2 = lua_tonumber(L, -1);
- lua_pop(L, 1);
+ dstream<<__FUNCTION_NAME<<": serialization error: "
+ <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;
+ return NULL;
}
- lua_pop(L, 1);
- // 2: position
- assert(lua_istable(L, -1));
- lua_pushstring(L, "X");
- lua_gettable(L, -2);
- lua_Number x = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pushstring(L, "Y");
- lua_gettable(L, -2);
- lua_Number y = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pushstring(L, "Z");
- lua_gettable(L, -2);
- lua_Number z = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pop(L, 1);
- // 1: env
- ServerEnvironment *env = (ServerEnvironment*)lua_touserdata(L, -1);
- lua_pop(L, 1);
- assert(env);
-
- v3s16 pos = floatToInt(v3f(x,y,z), 1.0);
-
- /*
- Do stuff.
- This gets sent to the server by the map through the edit
- event system.
- */
- bool succeeded = env->getMap().addNodeWithEvent(pos, n);
-
- lua_pushboolean(L, succeeded);
- return 1;
}
-/*
- string env_get_nearest_player_name(env, {X=,Y=,Z=})
-*/
-static int lf_env_get_nearest_player_name(lua_State *L)
-{
- // 2: position
- assert(lua_istable(L, -1));
- lua_pushstring(L, "X");
- lua_gettable(L, -2);
- lua_Number x = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pushstring(L, "Y");
- lua_gettable(L, -2);
- lua_Number y = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pushstring(L, "Z");
- lua_gettable(L, -2);
- lua_Number z = lua_tonumber(L, -1);
- lua_pop(L, 1);
- lua_pop(L, 1);
- // 1: env
- ServerEnvironment *env = (ServerEnvironment*)lua_touserdata(L, -1);
- lua_pop(L, 1);
- assert(env);
-
- v3f pos_f = v3f(x,y,z)*BS;
-
- Player *player = env->getNearestConnectedPlayer(pos_f);
-
- if(player)
- lua_pushstring(L, player->getName());
- else
- lua_pushstring(L, "");
-
- return 1; // Number of return values
-}
/*
- {exists=, pos={X=,Y=,Z=}, connected=} env_get_player_info(env, name)
+ RatSAO
*/
-static int lf_env_get_player_info(lua_State *L)
-{
- // 2: name
- const char *name = lua_tostring(L, -1);
- lua_pop(L, 1);
- // 1: env
- ServerEnvironment *env = (ServerEnvironment*)lua_touserdata(L, -1);
- lua_pop(L, 1);
- assert(env);
-
- Player *player = env->getPlayer(name);
- v3f pos(0,0,0);
- if(player)
- pos = player->getPosition();
- lua_newtable(L);
+// Prototype
+RatSAO proto_RatSAO(NULL, 0, v3f(0,0,0));
- lua_pushstring(L, "exists");
- lua_pushboolean(L, (player != NULL));
- lua_settable(L, -3);
-
- if(player != NULL)
- {
- lua_pushstring(L, "pos");
- {
- lua_newtable(L);
-
- lua_pushstring(L, "X");
- lua_pushnumber(L, pos.X/BS);
- lua_settable(L, -3);
-
- lua_pushstring(L, "Y");
- lua_pushnumber(L, pos.Y/BS);
- lua_settable(L, -3);
-
- lua_pushstring(L, "Z");
- lua_pushnumber(L, pos.Z/BS);
- lua_settable(L, -3);
- }
- lua_settable(L, -3);
-
- lua_pushstring(L, "connected");
- lua_pushboolean(L, (player->peer_id != 0));
- lua_settable(L, -3);
- }
-
- return 1; // Number of return values
-}
-
-LuaSAO::LuaSAO(ServerEnvironment *env, u16 id, v3f pos):
+RatSAO::RatSAO(ServerEnvironment *env, u16 id, v3f pos):
ServerActiveObject(env, id, pos),
- L(NULL)
+ m_is_active(false),
+ m_speed_f(0,0,0)
{
- dstream<<"LuaSAO::LuaSAO(): id="<<id<<std::endl;
- L = lua_open();
- assert(L);
-
- // Load libraries
- luaopen_base(L);
- luaopen_table(L);
- luaopen_string(L);
- luaopen_math(L);
-
- // Add globals
- //lua_pushlightuserdata(L, this);
- //lua_setglobal(L, "self");
-
- // Register functions
-#define LUA_REGISTER_FUNC(L, x) lua_register(L, #x, lf_ ## x)
- LUA_REGISTER_FUNC(L, object_remove);
- LUA_REGISTER_FUNC(L, object_get_environment);
- LUA_REGISTER_FUNC(L, object_set_base_position);
- LUA_REGISTER_FUNC(L, object_get_base_position);
- LUA_REGISTER_FUNC(L, object_add_message);
- LUA_REGISTER_FUNC(L, env_get_node);
- LUA_REGISTER_FUNC(L, get_content_features);
- LUA_REGISTER_FUNC(L, env_dig_node);
- LUA_REGISTER_FUNC(L, env_place_node);
- LUA_REGISTER_FUNC(L, env_get_nearest_player_name);
- LUA_REGISTER_FUNC(L, env_get_player_info);
+ ServerActiveObject::registerType(getType(), create);
+
+ m_oldpos = v3f(0,0,0);
+ m_last_sent_position = v3f(0,0,0);
+ m_yaw = 0;
+ m_counter1 = 0;
+ m_counter2 = 0;
+ m_age = 0;
+ m_touching_ground = false;
}
-LuaSAO::~LuaSAO()
+ServerActiveObject* RatSAO::create(ServerEnvironment *env, u16 id, v3f pos,
+ const std::string &data)
{
- lua_close(L);
+ std::istringstream is(data, std::ios::binary);
+ char buf[1];
+ // read version
+ is.read(buf, 1);
+ u8 version = buf[0];
+ // check if version is supported
+ if(version != 0)
+ return NULL;
+ return new RatSAO(env, id, pos);
}
-std::string LuaSAO::getClientInitializationData()
+void RatSAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
+ bool send_recommended)
{
+ assert(m_env);
+
+ if(m_is_active == false)
+ {
+ if(m_inactive_interval.step(dtime, 0.5)==false)
+ return;
+ }
+
/*
- Read client-side script from file
- */
-
- std::string relative_path;
- relative_path += "scripts/objects/";
- relative_path += m_script_name;
- relative_path += "/client.lua";
- std::string full_path = porting::getDataPath(relative_path.c_str());
- std::string script;
- std::ifstream file(full_path.c_str(), std::ios::binary | std::ios::ate);
- int size = file.tellg();
- SharedBuffer<char> buf(size);
- file.seekg(0, std::ios::beg);
- file.read(&buf[0], size);
- file.close();
-
- /*
- Create data string
+ The AI
*/
- std::string data;
- // Append script
- std::string script_string(&buf[0], buf.getSize());
- data += serializeLongString(script_string);
+ /*m_age += dtime;
+ if(m_age > 60)
+ {
+ // Die
+ m_removed = true;
+ return;
+ }*/
+
+ // Apply gravity
+ m_speed_f.Y -= dtime*9.81*BS;
/*
- Get data from server-side script for inclusion
+ Move around if some player is close
*/
- std::string other_data;
-
- do{
-
- const char *funcname = "on_get_client_init_data";
- lua_getglobal(L, funcname);
- if(!lua_isfunction(L,-1))
+ bool player_is_close = false;
+ // Check connected players
+ core::list<Player*> players = m_env->getPlayers(true);
+ core::list<Player*>::Iterator i;
+ for(i = players.begin();
+ i != players.end(); i++)
+ {
+ Player *player = *i;
+ v3f playerpos = player->getPosition();
+ if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
{
- lua_pop(L,1);
- dstream<<"WARNING: LuaSAO: Function not found: "
- <<funcname<<std::endl;
+ player_is_close = true;
break;
}
-
- // Parameters:
- // 1: self
- lua_pushlightuserdata(L, this);
-
- // Call (1 parameters, 1 result)
- if(lua_pcall(L, 1, 1, 0))
+ }
+
+ m_is_active = player_is_close;
+
+ if(player_is_close == false)
+ {
+ m_speed_f.X = 0;
+ m_speed_f.Z = 0;
+ }
+ else
+ {
+ // Move around
+ v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
+ f32 speed = 2*BS;
+ m_speed_f.X = speed * dir.X;
+ m_speed_f.Z = speed * dir.Z;
+
+ if(m_touching_ground && (m_oldpos - m_base_position).getLength()
+ < dtime*speed/2)
{
- dstream<<"WARNING: LuaSAO: Error running function "
- <<funcname<<": "
- <<lua_tostring(L,-1)<<std::endl;
- break;
+ m_counter1 -= dtime;
+ if(m_counter1 < 0.0)
+ {
+ m_counter1 += 1.0;
+ m_speed_f.Y = 5.0*BS;
+ }
}
- // Retrieve result
- if(!lua_isstring(L,-1))
{
- dstream<<"WARNING: LuaSAO: Function "<<funcname
- <<" didn't return a string"<<std::endl;
- break;
+ m_counter2 -= dtime;
+ if(m_counter2 < 0.0)
+ {
+ m_counter2 += (float)(myrand()%100)/100*3.0;
+ m_yaw += ((float)(myrand()%200)-100)/100*180;
+ m_yaw = wrapDegrees(m_yaw);
+ }
}
-
- size_t cs_len = 0;
- const char *cs = lua_tolstring(L, -1, &cs_len);
- lua_pop(L,1);
+ }
+
+ m_oldpos = m_base_position;
- other_data = std::string(cs, cs_len);
+ /*
+ Move it, with collision detection
+ */
- }while(0);
-
- data += serializeLongString(other_data);
+ core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
+ collisionMoveResult moveresult;
+ // Maximum movement without glitches
+ f32 pos_max_d = BS*0.25;
+ // Limit speed
+ if(m_speed_f.getLength()*dtime > pos_max_d)
+ m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
+ v3f pos_f = getBasePosition();
+ v3f pos_f_old = pos_f;
+ moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
+ box, dtime, pos_f, m_speed_f);
+ m_touching_ground = moveresult.touching_ground;
+
+ setBasePosition(pos_f);
+
+ if(send_recommended == false)
+ return;
- return data;
+ if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
+ {
+ m_last_sent_position = pos_f;
+
+ std::ostringstream os(std::ios::binary);
+ // command (0 = update position)
+ writeU8(os, 0);
+ // pos
+ writeV3F1000(os, m_base_position);
+ // yaw
+ writeF1000(os, m_yaw);
+ // create message and add to list
+ ActiveObjectMessage aom(getId(), false, os.str());
+ messages.push_back(aom);
+ }
}
-std::string LuaSAO::getServerInitializationData()
+std::string RatSAO::getClientInitializationData()
{
- std::string data;
-
- // Script name
- data.append(serializeString(m_script_name));
+ std::ostringstream os(std::ios::binary);
+ // version
+ writeU8(os, 0);
+ // pos
+ writeV3F1000(os, m_base_position);
+ return os.str();
+}
- /*
- Get data from server-side script for inclusion
- */
- std::string other_data;
-
- do{
+std::string RatSAO::getStaticData()
+{
+ //dstream<<__FUNCTION_NAME<<std::endl;
+ std::ostringstream os(std::ios::binary);
+ // version
+ writeU8(os, 0);
+ return os.str();
+}
- const char *funcname = "on_get_server_init_data";
- lua_getglobal(L, funcname);
- if(!lua_isfunction(L,-1))
- {
- lua_pop(L,1);
- dstream<<"WARNING: LuaSAO: Function not found: "
- <<funcname<<std::endl;
- break;
- }
-
- // Parameters:
- // 1: self
- lua_pushlightuserdata(L, this);
-
- // Call (1 parameters, 1 result)
- if(lua_pcall(L, 1, 1, 0))
- {
- dstream<<"WARNING: LuaSAO: Error running function "
- <<funcname<<": "
- <<lua_tostring(L,-1)<<std::endl;
- break;
- }
+InventoryItem* RatSAO::createPickedUpItem()
+{
+ std::istringstream is("CraftItem rat 1", std::ios_base::binary);
+ InventoryItem *item = InventoryItem::deSerialize(is);
+ return item;
+}
- // Retrieve result
- if(!lua_isstring(L,-1))
- {
- dstream<<"WARNING: LuaSAO: Function "<<funcname
- <<" didn't return a string"<<std::endl;
- break;
- }
-
- size_t cs_len = 0;
- const char *cs = lua_tolstring(L, -1, &cs_len);
- lua_pop(L,1);
+/*
+ Oerkki1SAO
+*/
- other_data = std::string(cs, cs_len);
+// Prototype
+Oerkki1SAO proto_Oerkki1SAO(NULL, 0, v3f(0,0,0));
- }while(0);
-
- data += serializeLongString(other_data);
+Oerkki1SAO::Oerkki1SAO(ServerEnvironment *env, u16 id, v3f pos):
+ ServerActiveObject(env, id, pos),
+ m_is_active(false),
+ m_speed_f(0,0,0)
+{
+ ServerActiveObject::registerType(getType(), create);
+
+ m_oldpos = v3f(0,0,0);
+ m_last_sent_position = v3f(0,0,0);
+ m_yaw = 0;
+ m_counter1 = 0;
+ m_counter2 = 0;
+ m_age = 0;
+ m_touching_ground = false;
+ m_hp = 20;
+}
- return data;
+ServerActiveObject* Oerkki1SAO::create(ServerEnvironment *env, u16 id, v3f pos,
+ const std::string &data)
+{
+ std::istringstream is(data, std::ios::binary);
+ // read version
+ u8 version = readU8(is);
+ // read hp
+ u8 hp = readU8(is);
+ // check if version is supported
+ if(version != 0)
+ return NULL;
+ Oerkki1SAO *o = new Oerkki1SAO(env, id, pos);
+ o->m_hp = hp;
+ return o;
}
-void LuaSAO::initializeFromNothing(const std::string &script_name)
+void Oerkki1SAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
+ bool send_recommended)
{
- loadScripts(script_name);
+ assert(m_env);
- /*
- Call on_initialize(self, data) in the script
- */
-
- const char *funcname = "on_initialize";
- lua_getglobal(L, funcname);
- if(!lua_isfunction(L,-1))
+ if(m_is_active == false)
{
- lua_pop(L,1);
- dstream<<"WARNING: LuaSAO: Function not found: "
- <<funcname<<std::endl;
- return;
+ if(m_inactive_interval.step(dtime, 0.5)==false)
+ return;
}
-
- // Parameters:
- // 1: self
- lua_pushlightuserdata(L, this);
- // 2: data (other)
- lua_pushstring(L, "");
-
- // Call (2 parameters, 0 result)
- if(lua_pcall(L, 2, 0, 0))
+
+ /*
+ The AI
+ */
+
+ m_age += dtime;
+ if(m_age > 120)
{
- dstream<<"WARNING: LuaSAO: Error running function "
- <<funcname<<": "
- <<lua_tostring(L,-1)<<std::endl;
+ // Die
+ m_removed = true;
return;
}
-}
-void LuaSAO::initializeFromSave(const std::string &data)
-{
- std::istringstream is(data, std::ios::binary);
- std::string script_name = deSerializeString(is);
- std::string other = deSerializeLongString(is);
-
- loadScripts(script_name);
+ // Apply gravity
+ m_speed_f.Y -= dtime*9.81*BS;
/*
- Call on_initialize(self, data) in the script
+ Move around if some player is close
*/
-
- const char *funcname = "on_initialize";
- lua_getglobal(L, funcname);
- if(!lua_isfunction(L,-1))
+ bool player_is_close = false;
+ v3f near_player_pos;
+ // Check connected players
+ core::list<Player*> players = m_env->getPlayers(true);
+ core::list<Player*>::Iterator i;
+ for(i = players.begin();
+ i != players.end(); i++)
{
- lua_pop(L,1);
- dstream<<"WARNING: LuaSAO: Function not found: "
- <<funcname<<std::endl;
- return;
+ Player *player = *i;
+ v3f playerpos = player->getPosition();
+ if(m_base_position.getDistanceFrom(playerpos) < BS*15.0)
+ {
+ player_is_close = true;
+ near_player_pos = playerpos;
+ break;
+ }
}
+
+ m_is_active = player_is_close;
- // Parameters:
- // 1: self
- lua_pushlightuserdata(L, this);
- // 2: data (other)
- lua_pushlstring(L, other.c_str(), other.size());
-
- // Call (2 parameters, 0 result)
- if(lua_pcall(L, 2, 0, 0))
+ if(player_is_close == false)
{
- dstream<<"WARNING: LuaSAO: Error running function "
- <<funcname<<": "
- <<lua_tostring(L,-1)<<std::endl;
- return;
+ m_speed_f.X = 0;
+ m_speed_f.Z = 0;
}
-}
+ else
+ {
+ // Move around
+
+ v3f ndir = near_player_pos - m_base_position;
+ ndir.Y = 0;
+ ndir /= ndir.getLength();
+ f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
+ if(nyaw < m_yaw - 180)
+ nyaw += 360;
+ else if(nyaw > m_yaw + 180)
+ nyaw -= 360;
+ m_yaw = 0.95*m_yaw + 0.05*nyaw;
+ m_yaw = wrapDegrees(m_yaw);
+
+ v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
+ f32 speed = 2*BS;
+ m_speed_f.X = speed * dir.X;
+ m_speed_f.Z = speed * dir.Z;
+
+ if(m_touching_ground && (m_oldpos - m_base_position).getLength()
+ < dtime*speed/2)
+ {
+ m_counter1 -= dtime;
+ if(m_counter1 < 0.0)
+ {
+ m_counter1 += 1.0;
+ // Jump
+ m_speed_f.Y = 5.0*BS;
+ }
+ }
-void LuaSAO::loadScripts(const std::string &script_name)
-{
- m_script_name = script_name;
+ {
+ m_counter2 -= dtime;
+ if(m_counter2 < 0.0)
+ {
+ m_counter2 += (float)(myrand()%100)/100*3.0;
+ //m_yaw += ((float)(myrand()%200)-100)/100*180;
+ m_yaw += ((float)(myrand()%200)-100)/100*90;
+ m_yaw = wrapDegrees(m_yaw);
+ }
+ }
+ }
- std::string relative_path;
- relative_path += "scripts/objects/";
- relative_path += script_name;
- std::string server_file = relative_path + "/server.lua";
- std::string server_path = porting::getDataPath(server_file.c_str());
-
- // Load the file
- int ret;
- ret = luaL_loadfile(L, server_path.c_str());
- if(ret)
- {
- const char *message = lua_tostring(L, -1);
- lua_pop(L, 1);
- dstream<<"LuaSAO::loadScript(): lua_loadfile failed: "
- <<message<<std::endl;
- assert(0);
+ m_oldpos = m_base_position;
+
+ /*
+ Move it, with collision detection
+ */
+
+ core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*5./3.,BS/3.);
+ collisionMoveResult moveresult;
+ // Maximum movement without glitches
+ f32 pos_max_d = BS*0.25;
+ // Limit speed
+ if(m_speed_f.getLength()*dtime > pos_max_d)
+ m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
+ v3f pos_f = getBasePosition();
+ v3f pos_f_old = pos_f;
+ moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
+ box, dtime, pos_f, m_speed_f);
+ m_touching_ground = moveresult.touching_ground;
+
+ setBasePosition(pos_f);
+
+ if(send_recommended == false)
return;
- }
- ret = lua_pcall(L, 0, 0, 0);
- if(ret)
+
+ if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
{
- const char *message = lua_tostring(L, -1);
- lua_pop(L, 1);
- dstream<<"LuaSAO::loadScript(): lua_pcall failed: "
- <<message<<std::endl;
- assert(0);
- return;
+ m_last_sent_position = pos_f;
+
+ std::ostringstream os(std::ios::binary);
+ // command (0 = update position)
+ writeU8(os, 0);
+ // pos
+ writeV3F1000(os, m_base_position);
+ // yaw
+ writeF1000(os, m_yaw);
+ // create message and add to list
+ ActiveObjectMessage aom(getId(), false, os.str());
+ messages.push_back(aom);
}
}
-void LuaSAO::step(float dtime, Queue<ActiveObjectMessage> &messages)
+std::string Oerkki1SAO::getClientInitializationData()
{
- const char *funcname = "on_step";
- lua_getglobal(L, funcname);
- if(!lua_isfunction(L,-1))
- {
- lua_pop(L,1);
- dstream<<"WARNING: LuaSAO::step(): Function not found: "
- <<funcname<<std::endl;
- return;
- }
-
- // Parameters:
- // 1: self
- lua_pushlightuserdata(L, this);
- // 2: dtime
- lua_pushnumber(L, dtime);
-
- // Call (2 parameters, 0 result)
- if(lua_pcall(L, 2, 0, 0))
+ std::ostringstream os(std::ios::binary);
+ // version
+ writeU8(os, 0);
+ // pos
+ writeV3F1000(os, m_base_position);
+ return os.str();
+}
+
+std::string Oerkki1SAO::getStaticData()
+{
+ //dstream<<__FUNCTION_NAME<<std::endl;
+ std::ostringstream os(std::ios::binary);
+ // version
+ writeU8(os, 0);
+ // hp
+ writeU8(os, m_hp);
+ return os.str();
+}
+
+u16 Oerkki1SAO::punch(const std::string &toolname)
+{
+ u16 amount = 5;
+ if(amount < m_hp)
{
- dstream<<"WARNING: LuaSAO::step(): Error running function "
- <<funcname<<": "
- <<lua_tostring(L,-1)<<std::endl;
- return;
+ m_hp -= amount;
}
-
- // Move messages
- while(m_message_queue.size() != 0)
+ else
{
- messages.push_back(m_message_queue.pop_front());
+ // Die
+ m_removed = true;
}
+ return 65536/100;
}
-
diff --git a/src/serverobject.h b/src/serverobject.h
index 557d9b618..955969819 100644
--- a/src/serverobject.h
+++ b/src/serverobject.h
@@ -32,40 +32,54 @@ Some planning
* Server environment adds an active object, which gets the id 1
* The active object list is scanned for each client once in a while,
and it finds out what objects have been added that are not known
- by the client yet. This scan is initiated by the server and the
- result ends up directly to the server.
+ by the client yet. This scan is initiated by the Server class and
+ the result ends up directly to the server.
* A network packet is created with the info and sent to the client.
+* Environment converts objects to static data and static data to
+ objects, based on how close players are to them.
*/
class ServerEnvironment;
+class InventoryItem;
class ServerActiveObject : public ActiveObject
{
public:
- ServerActiveObject(ServerEnvironment *env, u16 id, v3f pos=v3f(0,0,0));
+ /*
+ NOTE: m_env can be NULL, but step() isn't called if it is.
+ Prototypes are used that way.
+ */
+ ServerActiveObject(ServerEnvironment *env, u16 id, v3f pos);
virtual ~ServerActiveObject();
- v3f getBasePosition()
- {
- return m_base_position;
- }
+ // Create a certain type of ServerActiveObject
+ static ServerActiveObject* create(u8 type,
+ ServerEnvironment *env, u16 id, v3f pos,
+ const std::string &data);
+ /*
+ Some simple getters/setters
+ */
+ v3f getBasePosition()
+ {return m_base_position;}
void setBasePosition(v3f pos)
- {
- m_base_position = pos;
- }
-
+ {m_base_position = pos;}
ServerEnvironment* getEnv()
- {
- return m_env;
- }
+ {return m_env;}
/*
Step object in time.
Messages added to messages are sent to client over network.
+
+ send_recommended:
+ True at around 5-10 times a second, same for all objects.
+ This is used to let objects send most of the data at the
+ same time so that the data can be combined in a single
+ packet.
*/
- virtual void step(float dtime, Queue<ActiveObjectMessage> &messages){}
+ virtual void step(float dtime, Queue<ActiveObjectMessage> &messages,
+ bool send_recommended){}
/*
The return value of this is passed to the client-side object
@@ -75,28 +89,74 @@ public:
/*
The return value of this is passed to the server-side object
- when it is loaded from disk or from a static object
+ when it is created (converted from static to active - actually
+ the data is the static form)
+ */
+ virtual std::string getStaticData(){return "";}
+
+ /*
+ Item that the player gets when this object is picked up.
+ If NULL, object cannot be picked up.
*/
- virtual std::string getServerInitializationData(){return "";}
+ virtual InventoryItem* createPickedUpItem(){return NULL;}
/*
- This takes the return value of getServerInitializationData
+ If the object doesn't return an item, this will be called.
+ Return value is tool wear.
*/
- virtual void initialize(const std::string &data){}
+ virtual u16 punch(const std::string &toolname){return 0;}
- // Number of players which know about this object
+ /*
+ Number of players which know about this object. Object won't be
+ deleted until this is 0 to keep the id preserved for the right
+ object.
+ */
u16 m_known_by_count;
+
/*
- Whether this object is to be removed when nobody knows about
- it anymore.
- Removal is delayed to preserve the id for the time during which
- it could be confused to some other object by some client.
+ - Whether this object is to be removed when nobody knows about
+ it anymore.
+ - Removal is delayed to preserve the id for the time during which
+ it could be confused to some other object by some client.
+ - This is set to true by the step() method when the object wants
+ to be deleted.
+ - This can be set to true by anything else too.
*/
bool m_removed;
+ /*
+ This is set to true when a block should be removed from the active
+ object list but couldn't be removed because the id has to be
+ reserved for some client.
+
+ The environment checks this periodically. If this is true and also
+ m_known_by_count is true,
+ */
+ bool m_pending_deactivation;
+
+ /*
+ Whether the object's static data has been stored to a block
+ */
+ bool m_static_exists;
+ /*
+ The block from which the object was loaded from, and in which
+ a copy of the static data resides.
+ */
+ v3s16 m_static_block;
+
protected:
+ // Used for creating objects based on type
+ typedef ServerActiveObject* (*Factory)
+ (ServerEnvironment *env, u16 id, v3f pos,
+ const std::string &data);
+ static void registerType(u16 type, Factory f);
+
ServerEnvironment *m_env;
v3f m_base_position;
+
+private:
+ // Used for creating objects based on type
+ static core::map<u16, Factory> m_types;
};
class TestSAO : public ServerActiveObject
@@ -104,51 +164,90 @@ class TestSAO : public ServerActiveObject
public:
TestSAO(ServerEnvironment *env, u16 id, v3f pos);
u8 getType() const
- {
- return ACTIVEOBJECT_TYPE_TEST;
- }
- void step(float dtime, Queue<ActiveObjectMessage> &messages);
+ {return ACTIVEOBJECT_TYPE_TEST;}
+ static ServerActiveObject* create(ServerEnvironment *env, u16 id, v3f pos,
+ const std::string &data);
+ void step(float dtime, Queue<ActiveObjectMessage> &messages,
+ bool send_recommended);
private:
float m_timer1;
float m_age;
};
-extern "C"{
-#include "lua.h"
-#include "lualib.h"
-#include "lauxlib.h"
-}
-
-class LuaSAO : public ServerActiveObject
+class ItemSAO : public ServerActiveObject
{
public:
- LuaSAO(ServerEnvironment *env, u16 id, v3f pos);
- virtual ~LuaSAO();
-
+ ItemSAO(ServerEnvironment *env, u16 id, v3f pos,
+ const std::string inventorystring);
u8 getType() const
- {
- return ACTIVEOBJECT_TYPE_LUA;
- }
-
- virtual std::string getClientInitializationData();
- virtual std::string getServerInitializationData();
-
- void initializeFromNothing(const std::string &script_name);
- void initializeFromSave(const std::string &data);
-
- void loadScripts(const std::string &script_name);
-
- void step(float dtime, Queue<ActiveObjectMessage> &messages);
+ {return ACTIVEOBJECT_TYPE_ITEM;}
+ static ServerActiveObject* create(ServerEnvironment *env, u16 id, v3f pos,
+ const std::string &data);
+ void step(float dtime, Queue<ActiveObjectMessage> &messages,
+ bool send_recommended);
+ std::string getClientInitializationData();
+ std::string getStaticData();
+ InventoryItem* createInventoryItem();
+ InventoryItem* createPickedUpItem(){return createInventoryItem();}
+private:
+ std::string m_inventorystring;
+ v3f m_speed_f;
+ v3f m_last_sent_position;
+ IntervalLimiter m_move_interval;
+};
- /*
- Stuff available for usage for the lua callbacks
- */
- // This is moved onwards at the end of step()
- Queue<ActiveObjectMessage> m_message_queue;
+class RatSAO : public ServerActiveObject
+{
+public:
+ RatSAO(ServerEnvironment *env, u16 id, v3f pos);
+ u8 getType() const
+ {return ACTIVEOBJECT_TYPE_RAT;}
+ static ServerActiveObject* create(ServerEnvironment *env, u16 id, v3f pos,
+ const std::string &data);
+ void step(float dtime, Queue<ActiveObjectMessage> &messages,
+ bool send_recommended);
+ std::string getClientInitializationData();
+ std::string getStaticData();
+ InventoryItem* createPickedUpItem();
+private:
+ bool m_is_active;
+ IntervalLimiter m_inactive_interval;
+ v3f m_speed_f;
+ v3f m_oldpos;
+ v3f m_last_sent_position;
+ float m_yaw;
+ float m_counter1;
+ float m_counter2;
+ float m_age;
+ bool m_touching_ground;
+};
+class Oerkki1SAO : public ServerActiveObject
+{
+public:
+ Oerkki1SAO(ServerEnvironment *env, u16 id, v3f pos);
+ u8 getType() const
+ {return ACTIVEOBJECT_TYPE_OERKKI1;}
+ static ServerActiveObject* create(ServerEnvironment *env, u16 id, v3f pos,
+ const std::string &data);
+ void step(float dtime, Queue<ActiveObjectMessage> &messages,
+ bool send_recommended);
+ std::string getClientInitializationData();
+ std::string getStaticData();
+ InventoryItem* createPickedUpItem(){return NULL;}
+ u16 punch(const std::string &toolname);
private:
- lua_State* L;
- std::string m_script_name;
+ bool m_is_active;
+ IntervalLimiter m_inactive_interval;
+ v3f m_speed_f;
+ v3f m_oldpos;
+ v3f m_last_sent_position;
+ float m_yaw;
+ float m_counter1;
+ float m_counter2;
+ float m_age;
+ bool m_touching_ground;
+ u8 m_hp;
};
#endif
diff --git a/src/sha1.cpp b/src/sha1.cpp
new file mode 100644
index 000000000..98180adc7
--- /dev/null
+++ b/src/sha1.cpp
@@ -0,0 +1,207 @@
+/* sha1.cpp
+
+Copyright (c) 2005 Michael D. Leonhard
+
+http://tamale.net/
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#include "sha1.h"
+
+// print out memory in hexadecimal
+void SHA1::hexPrinter( unsigned char* c, int l )
+{
+ assert( c );
+ assert( l > 0 );
+ while( l > 0 )
+ {
+ printf( " %02x", *c );
+ l--;
+ c++;
+ }
+}
+
+// circular left bit rotation. MSB wraps around to LSB
+Uint32 SHA1::lrot( Uint32 x, int bits )
+{
+ return (x<<bits) | (x>>(32 - bits));
+};
+
+// Save a 32-bit unsigned integer to memory, in big-endian order
+void SHA1::storeBigEndianUint32( unsigned char* byte, Uint32 num )
+{
+ assert( byte );
+ byte[0] = (unsigned char)(num>>24);
+ byte[1] = (unsigned char)(num>>16);
+ byte[2] = (unsigned char)(num>>8);
+ byte[3] = (unsigned char)num;
+}
+
+
+// Constructor *******************************************************
+SHA1::SHA1()
+{
+ // make sure that the data type is the right size
+ assert( sizeof( Uint32 ) * 5 == 20 );
+
+ // initialize
+ H0 = 0x67452301;
+ H1 = 0xefcdab89;
+ H2 = 0x98badcfe;
+ H3 = 0x10325476;
+ H4 = 0xc3d2e1f0;
+ unprocessedBytes = 0;
+ size = 0;
+}
+
+// Destructor ********************************************************
+SHA1::~SHA1()
+{
+ // erase data
+ H0 = H1 = H2 = H3 = H4 = 0;
+ for( int c = 0; c < 64; c++ ) bytes[c] = 0;
+ unprocessedBytes = size = 0;
+}
+
+// process ***********************************************************
+void SHA1::process()
+{
+ assert( unprocessedBytes == 64 );
+ //printf( "process: " ); hexPrinter( bytes, 64 ); printf( "\n" );
+ int t;
+ Uint32 a, b, c, d, e, K, f, W[80];
+ // starting values
+ a = H0;
+ b = H1;
+ c = H2;
+ d = H3;
+ e = H4;
+ // copy and expand the message block
+ for( t = 0; t < 16; t++ ) W[t] = (bytes[t*4] << 24)
+ +(bytes[t*4 + 1] << 16)
+ +(bytes[t*4 + 2] << 8)
+ + bytes[t*4 + 3];
+ for(; t< 80; t++ ) W[t] = lrot( W[t-3]^W[t-8]^W[t-14]^W[t-16], 1 );
+
+ /* main loop */
+ Uint32 temp;
+ for( t = 0; t < 80; t++ )
+ {
+ if( t < 20 ) {
+ K = 0x5a827999;
+ f = (b & c) | ((b ^ 0xFFFFFFFF) & d);//TODO: try using ~
+ } else if( t < 40 ) {
+ K = 0x6ed9eba1;
+ f = b ^ c ^ d;
+ } else if( t < 60 ) {
+ K = 0x8f1bbcdc;
+ f = (b & c) | (b & d) | (c & d);
+ } else {
+ K = 0xca62c1d6;
+ f = b ^ c ^ d;
+ }
+ temp = lrot(a,5) + f + e + W[t] + K;
+ e = d;
+ d = c;
+ c = lrot(b,30);
+ b = a;
+ a = temp;
+ //printf( "t=%d %08x %08x %08x %08x %08x\n",t,a,b,c,d,e );
+ }
+ /* add variables */
+ H0 += a;
+ H1 += b;
+ H2 += c;
+ H3 += d;
+ H4 += e;
+ //printf( "Current: %08x %08x %08x %08x %08x\n",H0,H1,H2,H3,H4 );
+ /* all bytes have been processed */
+ unprocessedBytes = 0;
+}
+
+// addBytes **********************************************************
+void SHA1::addBytes( const char* data, int num )
+{
+ assert( data );
+ assert( num > 0 );
+ // add these bytes to the running total
+ size += num;
+ // repeat until all data is processed
+ while( num > 0 )
+ {
+ // number of bytes required to complete block
+ int needed = 64 - unprocessedBytes;
+ assert( needed > 0 );
+ // number of bytes to copy (use smaller of two)
+ int toCopy = (num < needed) ? num : needed;
+ // Copy the bytes
+ memcpy( bytes + unprocessedBytes, data, toCopy );
+ // Bytes have been copied
+ num -= toCopy;
+ data += toCopy;
+ unprocessedBytes += toCopy;
+
+ // there is a full block
+ if( unprocessedBytes == 64 ) process();
+ }
+}
+
+// digest ************************************************************
+unsigned char* SHA1::getDigest()
+{
+ // save the message size
+ Uint32 totalBitsL = size << 3;
+ Uint32 totalBitsH = size >> 29;
+ // add 0x80 to the message
+ addBytes( "\x80", 1 );
+
+ unsigned char footer[64] = {
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+ // block has no room for 8-byte filesize, so finish it
+ if( unprocessedBytes > 56 )
+ addBytes( (char*)footer, 64 - unprocessedBytes);
+ assert( unprocessedBytes <= 56 );
+ // how many zeros do we need
+ int neededZeros = 56 - unprocessedBytes;
+ // store file size (in bits) in big-endian format
+ storeBigEndianUint32( footer + neededZeros , totalBitsH );
+ storeBigEndianUint32( footer + neededZeros + 4, totalBitsL );
+ // finish the final block
+ addBytes( (char*)footer, neededZeros + 8 );
+ // allocate memory for the digest bytes
+ unsigned char* digest = (unsigned char*)malloc( 20 );
+ // copy the digest bytes
+ storeBigEndianUint32( digest, H0 );
+ storeBigEndianUint32( digest + 4, H1 );
+ storeBigEndianUint32( digest + 8, H2 );
+ storeBigEndianUint32( digest + 12, H3 );
+ storeBigEndianUint32( digest + 16, H4 );
+ // return the digest
+ return digest;
+}
diff --git a/src/sha1.h b/src/sha1.h
new file mode 100644
index 000000000..c04947373
--- /dev/null
+++ b/src/sha1.h
@@ -0,0 +1,51 @@
+/* sha1.h
+
+Copyright (c) 2005 Michael D. Leonhard
+
+http://tamale.net/
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+*/
+
+#ifndef SHA1_HEADER
+typedef unsigned int Uint32;
+
+class SHA1
+{
+ private:
+ // fields
+ Uint32 H0, H1, H2, H3, H4;
+ unsigned char bytes[64];
+ int unprocessedBytes;
+ Uint32 size;
+ void process();
+ public:
+ SHA1();
+ ~SHA1();
+ void addBytes( const char* data, int num );
+ unsigned char* getDigest();
+ // utility methods
+ static Uint32 lrot( Uint32 x, int bits );
+ static void storeBigEndianUint32( unsigned char* byte, Uint32 num );
+ static void hexPrinter( unsigned char* c, int l );
+};
+
+#define SHA1_HEADER
+#endif
diff --git a/src/socket.cpp b/src/socket.cpp
index b159fa14c..ab3ca62c5 100644
--- a/src/socket.cpp
+++ b/src/socket.cpp
@@ -53,6 +53,8 @@ void sockets_cleanup()
Address::Address()
{
+ m_address = 0;
+ m_port = 0;
}
Address::Address(unsigned int address, unsigned short port)
diff --git a/src/staticobject.h b/src/staticobject.h
new file mode 100644
index 000000000..c3369af3c
--- /dev/null
+++ b/src/staticobject.h
@@ -0,0 +1,167 @@
+/*
+Minetest-c55
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#ifndef STATICOBJECT_HEADER
+#define STATICOBJECT_HEADER
+
+#include "common_irrlicht.h"
+#include <string>
+#include <sstream>
+#include "utility.h"
+
+struct StaticObject
+{
+ u8 type;
+ v3f pos;
+ std::string data;
+
+ StaticObject():
+ type(0),
+ pos(0,0,0)
+ {
+ }
+ StaticObject(u8 type_, v3f pos_, const std::string &data_):
+ type(type_),
+ pos(pos_),
+ data(data_)
+ {
+ }
+
+ void serialize(std::ostream &os)
+ {
+ char buf[12];
+ // type
+ buf[0] = type;
+ os.write(buf, 1);
+ // pos
+ writeV3S32((u8*)buf, v3s32(pos.X*1000,pos.Y*1000,pos.Z*1000));
+ os.write(buf, 12);
+ // data
+ os<<serializeString(data);
+ }
+ void deSerialize(std::istream &is, u8 version)
+ {
+ char buf[12];
+ // type
+ is.read(buf, 1);
+ type = buf[0];
+ // pos
+ is.read(buf, 12);
+ v3s32 intp = readV3S32((u8*)buf);
+ pos.X = (f32)intp.X/1000;
+ pos.Y = (f32)intp.Y/1000;
+ pos.Z = (f32)intp.Z/1000;
+ // data
+ data = deSerializeString(is);
+ }
+};
+
+class StaticObjectList
+{
+public:
+ /*
+ Inserts an object to the container.
+ Id must be unique or 0.
+ */
+ void insert(u16 id, StaticObject obj)
+ {
+ if(id == 0)
+ {
+ m_stored.push_back(obj);
+ }
+ else
+ {
+ if(m_active.find(id) != NULL)
+ {
+ dstream<<"ERROR: StaticObjectList::insert(): "
+ <<"id already exists"<<std::endl;
+ assert(0);
+ return;
+ }
+ m_active.insert(id, obj);
+ }
+ }
+
+ void remove(u16 id)
+ {
+ assert(id != 0);
+ if(m_active.find(id) == NULL)
+ {
+ dstream<<"WARNING: StaticObjectList::remove(): id="<<id
+ <<" not found"<<std::endl;
+ return;
+ }
+ m_active.remove(id);
+ }
+
+ void serialize(std::ostream &os)
+ {
+ char buf[12];
+ // version
+ buf[0] = 0;
+ os.write(buf, 1);
+ // count
+ u16 count = m_stored.size() + m_active.size();
+ writeU16((u8*)buf, count);
+ os.write(buf, 2);
+ for(core::list<StaticObject>::Iterator
+ i = m_stored.begin();
+ i != m_stored.end(); i++)
+ {
+ StaticObject &s_obj = *i;
+ s_obj.serialize(os);
+ }
+ for(core::map<u16, StaticObject>::Iterator
+ i = m_active.getIterator();
+ i.atEnd()==false; i++)
+ {
+ StaticObject s_obj = i.getNode()->getValue();
+ s_obj.serialize(os);
+ }
+ }
+ void deSerialize(std::istream &is)
+ {
+ char buf[12];
+ // version
+ is.read(buf, 1);
+ u8 version = buf[0];
+ // count
+ is.read(buf, 2);
+ u16 count = readU16((u8*)buf);
+ for(u16 i=0; i<count; i++)
+ {
+ StaticObject s_obj;
+ s_obj.deSerialize(is, version);
+ m_stored.push_back(s_obj);
+ }
+ }
+
+ /*
+ NOTE: When an object is transformed to active, it is removed
+ from m_stored and inserted to m_active.
+ The caller directly manipulates these containers.
+ */
+ core::list<StaticObject> m_stored;
+ core::map<u16, StaticObject> m_active;
+
+private:
+};
+
+#endif
+
diff --git a/src/test.cpp b/src/test.cpp
index 1de902787..07ef772ef 100644
--- a/src/test.cpp
+++ b/src/test.cpp
@@ -517,14 +517,14 @@ struct TestMapBlock
assert(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
assert(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
- assert(b.getFaceLight(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
- assert(b.getFaceLight(1000, p, v3s16(0,-1,0)) == 0);
- assert(b.getFaceLight(0, p, v3s16(0,-1,0)) == 0);
+ assert(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
+ assert(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
+ assert(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
// According to MapBlock::getFaceLight,
// The face on the z+ side should have double-diminished light
//assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
// The face on the z+ side should have diminished light
- assert(b.getFaceLight(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
+ assert(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
}
/*
Check how the block handles being in between blocks with some non-sunlight
@@ -951,18 +951,18 @@ struct TestConnection
assert(got_exception);
}
{
- //u8 data1[1100];
- SharedBuffer<u8> data1(1100);
- for(u16 i=0; i<1100; i++){
+ const int datasize = 30000;
+ SharedBuffer<u8> data1(datasize);
+ for(u16 i=0; i<datasize; i++){
data1[i] = i/4;
}
- dstream<<"Sending data (size="<<1100<<"):";
- for(int i=0; i<1100 && i<20; i++){
+ dstream<<"Sending data (size="<<datasize<<"):";
+ for(int i=0; i<datasize && i<20; i++){
if(i%2==0) DEBUGPRINT(" ");
DEBUGPRINT("%.2X", ((int)((const char*)*data1)[i])&0xff);
}
- if(1100>20)
+ if(datasize>20)
dstream<<"...";
dstream<<std::endl;
@@ -970,10 +970,10 @@ struct TestConnection
sleep_ms(50);
- u8 recvdata[2000];
+ u8 recvdata[datasize + 1000];
dstream<<"** running client.Receive()"<<std::endl;
u16 peer_id = 132;
- u16 size = client.Receive(peer_id, recvdata, 2000);
+ u16 size = client.Receive(peer_id, recvdata, datasize + 1000);
dstream<<"** Client received: peer_id="<<peer_id
<<", size="<<size
<<std::endl;
diff --git a/src/texture.h b/src/texture.h
deleted file mode 100644
index d8310789d..000000000
--- a/src/texture.h
+++ /dev/null
@@ -1,134 +0,0 @@
-/*
-Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
-
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License along
-with this program; if not, write to the Free Software Foundation, Inc.,
-51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-*/
-
-#ifndef TEXTURE_HEADER
-#define TEXTURE_HEADER
-
-// This file now contains all that was here
-#include "tile.h"
-
-// TODO: Remove this
-typedef u16 textureid_t;
-
-#if 0
-
-#include "common_irrlicht.h"
-//#include "utility.h"
-#include "debug.h"
-
-/*
- All textures are given a "texture id".
- 0 = nothing (a NULL pointer texture)
-*/
-typedef u16 textureid_t;
-
-/*
- Every texture in the game can be specified by this.
-
- It exists instead of specification strings because arbitary
- texture combinations for map nodes are handled using this,
- and strings are too slow for that purpose.
-
- Plain texture pointers are not used because they don't contain
- content information by themselves. A texture can be completely
- reconstructed by just looking at this, while this also is a
- fast unique key to containers.
-*/
-
-#define TEXTURE_SPEC_TEXTURE_COUNT 4
-
-struct TextureSpec
-{
- TextureSpec()
- {
- clear();
- }
-
- TextureSpec(textureid_t id0)
- {
- clear();
- tids[0] = id0;
- }
-
- TextureSpec(textureid_t id0, textureid_t id1)
- {
- clear();
- tids[0] = id0;
- tids[1] = id1;
- }
-
- void clear()
- {
- for(u32 i=0; i<TEXTURE_SPEC_TEXTURE_COUNT; i++)
- {
- tids[i] = 0;
- }
- }
-
- bool empty() const
- {
- for(u32 i=0; i<TEXTURE_SPEC_TEXTURE_COUNT; i++)
- {
- if(tids[i] != 0)
- return false;
- }
- return true;
- }
-
- void addTid(textureid_t tid)
- {
- for(u32 i=0; i<TEXTURE_SPEC_TEXTURE_COUNT; i++)
- {
- if(tids[i] == 0)
- {
- tids[i] = tid;
- return;
- }
- }
- // Too many textures
- assert(0);
- }
-
- bool operator==(const TextureSpec &other) const
- {
- for(u32 i=0; i<TEXTURE_SPEC_TEXTURE_COUNT; i++)
- {
- if(tids[i] != other.tids[i])
- return false;
- }
- return true;
- }
-
- bool operator<(const TextureSpec &other) const
- {
- for(u32 i=0; i<TEXTURE_SPEC_TEXTURE_COUNT; i++)
- {
- if(tids[i] >= other.tids[i])
- return false;
- }
- return true;
- }
-
- // Ids of textures. They are blit on each other.
- textureid_t tids[TEXTURE_SPEC_TEXTURE_COUNT];
-};
-
-#endif
-
-#endif
diff --git a/src/tile.cpp b/src/tile.cpp
index cfbb68249..dabc1dcf3 100644
--- a/src/tile.cpp
+++ b/src/tile.cpp
@@ -1,6 +1,6 @@
/*
Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -21,6 +21,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "debug.h"
#include "main.h" // for g_settings
#include "filesys.h"
+#include "utility.h"
+
+/*
+ A cache from texture name to texture path
+*/
+MutexedMap<std::string, std::string> g_texturename_to_path_cache;
/*
Replaces the filename extension.
@@ -30,7 +36,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
-> image = "a/image.jpg"
Returns true on success.
*/
-inline bool replace_ext(std::string &path, const char *ext)
+static bool replace_ext(std::string &path, const char *ext)
{
if(ext == NULL)
return false;
@@ -61,7 +67,7 @@ inline bool replace_ext(std::string &path, const char *ext)
If failed, return "".
*/
-inline std::string getImagePath(std::string path)
+static std::string getImagePath(std::string path)
{
// A NULL-ended list of possible image extensions
const char *extensions[] = {
@@ -86,25 +92,55 @@ inline std::string getImagePath(std::string path)
/*
Gets the path to a texture by first checking if the texture exists
in texture_path and if not, using the data path.
+
+ Checks all supported extensions by replacing the original extension.
+
+ If not found, returns "".
+
+ Utilizes a thread-safe cache.
*/
-inline std::string getTexturePath(std::string filename)
+std::string getTexturePath(const std::string &filename)
{
+ std::string fullpath = "";
+ /*
+ Check from cache
+ */
+ bool incache = g_texturename_to_path_cache.get(filename, &fullpath);
+ if(incache)
+ return fullpath;
+
+ /*
+ Check from texture_path
+ */
std::string texture_path = g_settings.get("texture_path");
if(texture_path != "")
{
- std::string fullpath = texture_path + '/' + filename;
- // Check all filename extensions
- fullpath = getImagePath(fullpath);
- // If found, return it
- if(fullpath != "")
- return fullpath;
+ std::string testpath = texture_path + '/' + filename;
+ // Check all filename extensions. Returns "" if not found.
+ fullpath = getImagePath(testpath);
}
- std::string fullpath = porting::getDataPath(filename.c_str());
- // Check all filename extensions
- fullpath = getImagePath(fullpath);
+
+ /*
+ Check from default data directory
+ */
+ if(fullpath == "")
+ {
+ std::string testpath = porting::getDataPath(filename.c_str());
+ // Check all filename extensions. Returns "" if not found.
+ fullpath = getImagePath(testpath);
+ }
+
+ // Add to cache (also an empty result is cached)
+ g_texturename_to_path_cache.set(filename, fullpath);
+
+ // Finally return it
return fullpath;
}
+/*
+ TextureSource
+*/
+
TextureSource::TextureSource(IrrlichtDevice *device):
m_device(device),
m_main_atlas_image(NULL),
@@ -476,7 +512,9 @@ void TextureSource::buildMainAtlas()
sourcelist.push_back("tree_top.png");
sourcelist.push_back("water.png");
sourcelist.push_back("leaves.png");
+ sourcelist.push_back("glass.png");
sourcelist.push_back("mud.png^grass_side.png");
+ sourcelist.push_back("cobble.png");
sourcelist.push_back("stone.png^mineral_coal.png");
sourcelist.push_back("stone.png^mineral_iron.png");
@@ -486,7 +524,7 @@ void TextureSource::buildMainAtlas()
sourcelist.push_back("sand.png^mineral_iron.png");
// Padding to disallow texture bleeding
- s32 padding = 8;
+ s32 padding = 16;
/*
First pass: generate almost everything
diff --git a/src/tile.h b/src/tile.h
index d11899438..216d76508 100644
--- a/src/tile.h
+++ b/src/tile.h
@@ -1,6 +1,6 @@
/*
Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -26,6 +26,22 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <string>
/*
+ tile.{h,cpp}: Texture handling stuff.
+*/
+
+/*
+ Gets the path to a texture by first checking if the texture exists
+ in texture_path and if not, using the data path.
+
+ Checks all supported extensions by replacing the original extension.
+
+ If not found, returns "".
+
+ Utilizes a thread-safe cache.
+*/
+std::string getTexturePath(const std::string &filename);
+
+/*
Specifies a texture in an atlas.
This is used to specify single textures also.
@@ -271,6 +287,7 @@ struct TileSpec
// Use this so that leaves don't need a separate material
//material_type(MATERIAL_ALPHA_SIMPLE),
material_flags(
+ //0 // <- DEBUG, Use the one below
MATERIAL_FLAG_BACKFACE_CULLING
)
{
diff --git a/src/utility.cpp b/src/utility.cpp
index ed0c179e8..0721100cb 100644
--- a/src/utility.cpp
+++ b/src/utility.cpp
@@ -22,8 +22,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/
#include "utility.h"
-#include "irrlichtwrapper.h"
#include "gettime.h"
+#include "sha1.h"
+#include "base64.h"
TimeTaker::TimeTaker(const char *name, u32 *result)
{
@@ -61,6 +62,17 @@ u32 TimeTaker::getTime()
return dtime;
}
+const v3s16 g_6dirs[6] =
+{
+ // +right, +top, +back
+ v3s16( 0, 0, 1), // back
+ v3s16( 0, 1, 0), // top
+ v3s16( 1, 0, 0), // right
+ v3s16( 0, 0,-1), // front
+ v3s16( 0,-1, 0), // bottom
+ v3s16(-1, 0, 0) // left
+};
+
const v3s16 g_26dirs[26] =
{
// +right, +top, +back
@@ -174,6 +186,10 @@ bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir, f32 range,
if(distance_ptr)
*distance_ptr = d;
+ // If block is very close, it is always in sight
+ if(d < 1.44*1.44*MAP_BLOCKSIZE*BS/2)
+ return true;
+
// If block is far away, it's not in sight
if(d > range * BS)
return false;
@@ -203,3 +219,24 @@ bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir, f32 range,
return true;
}
+// Get an sha-1 hash of the player's name combined with
+// the password entered. That's what the server uses as
+// their password. (Exception : if the password field is
+// blank, we send a blank password - this is for backwards
+// compatibility with password-less players).
+std::string translatePassword(std::string playername, std::wstring password)
+{
+ if(password.length() == 0)
+ return "";
+
+ std::string slt = playername + wide_to_narrow(password);
+ SHA1 sha1;
+ sha1.addBytes(slt.c_str(), slt.length());
+ unsigned char *digest = sha1.getDigest();
+ std::string pwd = base64_encode(digest, 20);
+ free(digest);
+ return pwd;
+}
+
+
+
diff --git a/src/utility.h b/src/utility.h
index 2b878b82b..534aea483 100644
--- a/src/utility.h
+++ b/src/utility.h
@@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <fstream>
#include <string>
#include <sstream>
+#include <vector>
#include <jthread.h>
#include <jmutex.h>
#include <jmutexautolock.h>
@@ -34,11 +35,25 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "exceptions.h"
#include "porting.h"
+extern const v3s16 g_6dirs[6];
+
extern const v3s16 g_26dirs[26];
// 26th is (0,0,0)
extern const v3s16 g_27dirs[27];
+inline void writeU64(u8 *data, u64 i)
+{
+ data[0] = ((i>>56)&0xff);
+ data[1] = ((i>>48)&0xff);
+ data[2] = ((i>>40)&0xff);
+ data[3] = ((i>>32)&0xff);
+ data[4] = ((i>>24)&0xff);
+ data[5] = ((i>>16)&0xff);
+ data[6] = ((i>> 8)&0xff);
+ data[7] = ((i>> 0)&0xff);
+}
+
inline void writeU32(u8 *data, u32 i)
{
data[0] = ((i>>24)&0xff);
@@ -58,6 +73,14 @@ inline void writeU8(u8 *data, u8 i)
data[0] = ((i>> 0)&0xff);
}
+inline u64 readU64(u8 *data)
+{
+ return ((u64)data[0]<<56) | ((u64)data[1]<<48)
+ | ((u64)data[2]<<40) | ((u64)data[3]<<32)
+ | ((u64)data[4]<<24) | ((u64)data[5]<<16)
+ | ((u64)data[6]<<8) | ((u64)data[7]<<0);
+}
+
inline u32 readU32(u8 *data)
{
return (data[0]<<24) | (data[1]<<16) | (data[2]<<8) | (data[3]<<0);
@@ -73,8 +96,6 @@ inline u8 readU8(u8 *data)
return (data[0]<<0);
}
-// Signed variants of the above
-
inline void writeS32(u8 *data, s32 i){
writeU32(data, (u32)i);
}
@@ -82,6 +103,13 @@ inline s32 readS32(u8 *data){
return (s32)readU32(data);
}
+inline void writeF1000(u8 *data, f32 i){
+ writeS32(data, i*1000);
+}
+inline f32 readF1000(u8 *data){
+ return (f32)readS32(data)/1000.;
+}
+
inline void writeS16(u8 *data, s16 i){
writeU16(data, (u16)i);
}
@@ -95,7 +123,6 @@ inline void writeV3S32(u8 *data, v3s32 p)
writeS32(&data[4], p.Y);
writeS32(&data[8], p.Z);
}
-
inline v3s32 readV3S32(u8 *data)
{
v3s32 p;
@@ -105,6 +132,21 @@ inline v3s32 readV3S32(u8 *data)
return p;
}
+inline void writeV3F1000(u8 *data, v3f p)
+{
+ writeF1000(&data[0], p.X);
+ writeF1000(&data[4], p.Y);
+ writeF1000(&data[8], p.Z);
+}
+inline v3f readV3F1000(u8 *data)
+{
+ v3f p;
+ p.X = (float)readF1000(&data[0]);
+ p.Y = (float)readF1000(&data[4]);
+ p.Z = (float)readF1000(&data[8]);
+ return p;
+}
+
inline void writeV2S16(u8 *data, v2s16 p)
{
writeS16(&data[0], p.X);
@@ -150,6 +192,78 @@ inline v3s16 readV3S16(u8 *data)
}
/*
+ The above stuff directly interfaced to iostream
+*/
+
+inline void writeU8(std::ostream &os, u8 p)
+{
+ char buf[1];
+ writeU8((u8*)buf, p);
+ os.write(buf, 1);
+}
+inline u8 readU8(std::istream &is)
+{
+ char buf[1];
+ is.read(buf, 1);
+ return readU8((u8*)buf);
+}
+
+inline void writeU16(std::ostream &os, u16 p)
+{
+ char buf[2];
+ writeU16((u8*)buf, p);
+ os.write(buf, 2);
+}
+inline u16 readU16(std::istream &is)
+{
+ char buf[2];
+ is.read(buf, 2);
+ return readU16((u8*)buf);
+}
+
+inline void writeU32(std::ostream &os, u16 p)
+{
+ char buf[4];
+ writeU16((u8*)buf, p);
+ os.write(buf, 4);
+}
+inline u16 readU32(std::istream &is)
+{
+ char buf[4];
+ is.read(buf, 4);
+ return readU32((u8*)buf);
+}
+
+inline void writeF1000(std::ostream &os, f32 p)
+{
+ char buf[2];
+ writeF1000((u8*)buf, p);
+ os.write(buf, 2);
+}
+inline f32 readF1000(std::istream &is)
+{
+ char buf[2];
+ is.read(buf, 2);
+ // TODO: verify if this gets rid of the valgrind warning
+ //if(is.gcount() != 2)
+ // return 0;
+ return readF1000((u8*)buf);
+}
+
+inline void writeV3F1000(std::ostream &os, v3f p)
+{
+ char buf[12];
+ writeV3F1000((u8*)buf, p);
+ os.write(buf, 12);
+}
+inline v3f readV3F1000(std::istream &is)
+{
+ char buf[12];
+ is.read(buf, 12);
+ return readV3F1000((u8*)buf);
+}
+
+/*
None of these are used at the moment
*/
@@ -273,10 +387,20 @@ template <typename T>
class SharedBuffer
{
public:
+ SharedBuffer()
+ {
+ m_size = 0;
+ data = NULL;
+ refcount = new unsigned int;
+ (*refcount) = 1;
+ }
SharedBuffer(unsigned int size)
{
m_size = size;
- data = new T[size];
+ if(m_size != 0)
+ data = new T[m_size];
+ else
+ data = NULL;
refcount = new unsigned int;
(*refcount) = 1;
}
@@ -306,8 +430,13 @@ public:
SharedBuffer(T *t, unsigned int size)
{
m_size = size;
- data = new T[size];
- memcpy(data, t, size);
+ if(m_size != 0)
+ {
+ data = new T[m_size];
+ memcpy(data, t, m_size);
+ }
+ else
+ data = NULL;
refcount = new unsigned int;
(*refcount) = 1;
}
@@ -316,9 +445,14 @@ public:
*/
SharedBuffer(const Buffer<T> &buffer)
{
- m_size = buffer.m_size;
- data = new T[buffer.getSize()];
- memcpy(data, *buffer, buffer.getSize());
+ m_size = buffer.getSize();
+ if(m_size != 0)
+ {
+ data = new T[m_size];
+ memcpy(data, *buffer, buffer.getSize());
+ }
+ else
+ data = NULL;
refcount = new unsigned int;
(*refcount) = 1;
}
@@ -328,6 +462,7 @@ public:
}
T & operator[](unsigned int i) const
{
+ //assert(i < m_size)
return data[i];
}
T * operator*() const
@@ -345,7 +480,8 @@ private:
(*refcount)--;
if(*refcount == 0)
{
- delete[] data;
+ if(data)
+ delete[] data;
delete refcount;
}
}
@@ -399,8 +535,6 @@ private:
TimeTaker
*/
-class IrrlichtWrapper;
-
class TimeTaker
{
public:
@@ -636,6 +770,19 @@ inline std::string wide_to_narrow(const std::wstring& wcs)
return *mbs;
}
+// Split a string using the given delimiter. Returns a vector containing
+// the component parts.
+inline std::vector<std::wstring> str_split(const std::wstring &str, wchar_t delimiter)
+{
+ std::vector<std::wstring> parts;
+ std::wstringstream sstr(str);
+ std::wstring part;
+ while(std::getline(sstr, part, delimiter))
+ parts.push_back(part);
+ return parts;
+}
+
+
/*
See test.cpp for example cases.
wraps degrees to the range of -360...360
@@ -691,11 +838,20 @@ inline s32 stoi(const std::string &s, s32 min, s32 max)
return i;
}
+
+// MSVC2010 includes it's own versions of these
+#if !defined(_MSC_VER) || _MSC_VER < 1600
+
inline s32 stoi(std::string s)
{
return atoi(s.c_str());
}
+inline s32 stoi(std::wstring s)
+{
+ return atoi(wide_to_narrow(s).c_str());
+}
+
inline float stof(std::string s)
{
float f;
@@ -704,6 +860,8 @@ inline float stof(std::string s)
return f;
}
+#endif
+
inline std::string itos(s32 i)
{
std::ostringstream o;
@@ -1234,6 +1392,14 @@ public:
return value;
}
+ void setBool(std::string name, bool value)
+ {
+ if(value)
+ set(name, "true");
+ else
+ set(name, "false");
+ }
+
void setS32(std::string name, s32 value)
{
set(name, itos(value));
@@ -1369,6 +1535,7 @@ public:
}
u32 size()
{
+ JMutexAutoLock lock(m_mutex);
return m_list.size();
}
void push_back(T t)
@@ -1444,6 +1611,10 @@ protected:
core::list<T> m_list;
};
+/*
+ A single worker thread - multiple client threads queue framework.
+*/
+
template<typename Caller, typename Data>
class CallerInfo
{
@@ -1493,11 +1664,6 @@ public:
core::list<CallerInfo<Caller, CallerData> > callers;
};
-/*
- Quickhands for typical request-result queues.
- Used for distributing work between threads.
-*/
-
template<typename Key, typename T, typename Caller, typename CallerData>
class RequestQueue
{
@@ -1638,12 +1804,12 @@ private:
core::list<Value> m_list;
};
-#if 0
+#if 1
template<typename Key, typename Value>
-class MutexedCache
+class MutexedMap
{
public:
- MutexedCache()
+ MutexedMap()
{
m_mutex.Init();
assert(m_mutex.IsInitialized());
@@ -1665,8 +1831,10 @@ public:
if(n == NULL)
return false;
-
- *result = n->getValue();
+
+ if(result != NULL)
+ *result = n->getValue();
+
return true;
}
@@ -1806,9 +1974,11 @@ inline v3f intToFloat(v3s16 p, f32 d)
*/
// Creates a string with the length as the first two bytes
-inline std::string serializeString(const std::string plain)
+inline std::string serializeString(const std::string &plain)
{
- assert(plain.size() <= 65535);
+ //assert(plain.size() <= 65535);
+ if(plain.size() > 65535)
+ throw SerializationError("String too long for serializeString");
char buf[2];
writeU16((u8*)&buf[0], plain.size());
std::string s;
@@ -1817,13 +1987,21 @@ inline std::string serializeString(const std::string plain)
return s;
}
-// Reads a string with the length as the first two bytes
-inline std::string deSerializeString(const std::string encoded)
+// Creates a string with the length as the first two bytes from wide string
+inline std::string serializeWideString(const std::wstring &plain)
{
- u16 s_size = readU16((u8*)&encoded.c_str()[0]);
+ //assert(plain.size() <= 65535);
+ if(plain.size() > 65535)
+ throw SerializationError("String too long for serializeString");
+ char buf[2];
+ writeU16((u8*)buf, plain.size());
std::string s;
- s.reserve(s_size);
- s.append(&encoded.c_str()[2], s_size);
+ s.append(buf, 2);
+ for(u32 i=0; i<plain.size(); i++)
+ {
+ writeU16((u8*)buf, plain[i]);
+ s.append(buf, 2);
+ }
return s;
}
@@ -1845,8 +2023,29 @@ inline std::string deSerializeString(std::istream &is)
return s;
}
+// Reads a wide string with the length as the first two bytes
+inline std::wstring deSerializeWideString(std::istream &is)
+{
+ char buf[2];
+ is.read(buf, 2);
+ if(is.gcount() != 2)
+ throw SerializationError("deSerializeString: size not read");
+ u16 s_size = readU16((u8*)buf);
+ if(s_size == 0)
+ return L"";
+ std::wstring s;
+ s.reserve(s_size);
+ for(u32 i=0; i<s_size; i++)
+ {
+ is.read(&buf[0], 2);
+ wchar_t c16 = readU16((u8*)buf);
+ s.append(&c16, 1);
+ }
+ return s;
+}
+
// Creates a string with the length as the first four bytes
-inline std::string serializeLongString(const std::string plain)
+inline std::string serializeLongString(const std::string &plain)
{
char buf[4];
writeU32((u8*)&buf[0], plain.size());
@@ -1857,16 +2056,6 @@ inline std::string serializeLongString(const std::string plain)
}
// Reads a string with the length as the first four bytes
-inline std::string deSerializeLongString(const std::string encoded)
-{
- u32 s_size = readU32((u8*)&encoded.c_str()[0]);
- std::string s;
- s.reserve(s_size);
- s.append(&encoded.c_str()[2], s_size);
- return s;
-}
-
-// Reads a string with the length as the first four bytes
inline std::string deSerializeLongString(std::istream &is)
{
char buf[4];
@@ -1894,7 +2083,8 @@ inline u32 time_to_daynight_ratio(u32 time_of_day)
s32 d = daylength;
s32 t = (((time_of_day)%24000)/(24000/d));
if(t < nightlength/2 || t >= d - nightlength/2)
- return 300;
+ //return 300;
+ return 350;
else if(t >= d/2 - daytimelength/2 && t < d/2 + daytimelength/2)
return 1000;
else
@@ -1914,6 +2104,33 @@ inline core::aabbox3d<f32> getNodeBox(v3s16 p, float d)
);
}
+class IntervalLimiter
+{
+public:
+ IntervalLimiter():
+ m_accumulator(0)
+ {
+ }
+ /*
+ dtime: time from last call to this method
+ wanted_interval: interval wanted
+ return value:
+ true: action should be skipped
+ false: action should be done
+ */
+ bool step(float dtime, float wanted_interval)
+ {
+ m_accumulator += dtime;
+ if(m_accumulator < wanted_interval)
+ return false;
+ m_accumulator -= wanted_interval;
+ return true;
+ }
+protected:
+ float m_accumulator;
+};
+
+std::string translatePassword(std::string playername, std::wstring password);
#endif
diff --git a/src/voxel.h b/src/voxel.h
index eced43ed5..2e8015eb1 100644
--- a/src/voxel.h
+++ b/src/voxel.h
@@ -373,6 +373,34 @@ public:
return m_data[m_area.index(p)];
}
+ MapNode getNodeNoEx(v3s16 p)
+ {
+ emerge(p);
+
+ if(m_flags[m_area.index(p)] & VOXELFLAG_INEXISTENT)
+ {
+ return MapNode(CONTENT_IGNORE);
+ }
+
+ return m_data[m_area.index(p)];
+ }
+ MapNode & getNodeRef(v3s16 p)
+ {
+ emerge(p);
+
+ if(m_flags[m_area.index(p)] & VOXELFLAG_INEXISTENT)
+ {
+ dstream<<"EXCEPT: VoxelManipulator::getNode(): "
+ <<"p=("<<p.X<<","<<p.Y<<","<<p.Z<<")"
+ <<", index="<<m_area.index(p)
+ <<", flags="<<(int)m_flags[m_area.index(p)]
+ <<" is inexistent"<<std::endl;
+ throw InvalidPositionException
+ ("VoxelManipulator: getNode: inexistent");
+ }
+
+ return m_data[m_area.index(p)];
+ }
void setNode(v3s16 p, MapNode &n)
{
emerge(p);
@@ -525,7 +553,7 @@ public:
/*
Some settings
*/
- bool m_disable_water_climb;
+ //bool m_disable_water_climb;
private:
};