game.lua 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. core.log("info", "Initializing asynchronous environment (game)")
  2. local function pack2(...)
  3. return {n=select('#', ...), ...}
  4. end
  5. -- Entrypoint to run async jobs, called by C++
  6. function core.job_processor(func, params)
  7. local retval = pack2(func(unpack(params, 1, params.n)))
  8. return retval
  9. end
  10. -- Import a bunch of individual files from builtin/game/
  11. local gamepath = core.get_builtin_path() .. "game" .. DIR_DELIM
  12. local commonpath = core.get_builtin_path() .. "common" .. DIR_DELIM
  13. local builtin_shared = {}
  14. dofile(gamepath .. "constants.lua")
  15. assert(loadfile(commonpath .. "item_s.lua"))(builtin_shared)
  16. dofile(gamepath .. "misc_s.lua")
  17. dofile(gamepath .. "features.lua")
  18. dofile(gamepath .. "voxelarea.lua")
  19. -- Transfer of globals
  20. do
  21. local all = assert(core.transferred_globals)
  22. core.transferred_globals = nil
  23. all.registered_nodes = {}
  24. all.registered_craftitems = {}
  25. all.registered_tools = {}
  26. for k, v in pairs(all.registered_items) do
  27. -- Disable further modification
  28. setmetatable(v, {__newindex = {}})
  29. -- Reassemble the other tables
  30. if v.type == "node" then
  31. getmetatable(v).__index = all.nodedef_default
  32. all.registered_nodes[k] = v
  33. elseif v.type == "craft" then
  34. getmetatable(v).__index = all.craftitemdef_default
  35. all.registered_craftitems[k] = v
  36. elseif v.type == "tool" then
  37. getmetatable(v).__index = all.tooldef_default
  38. all.registered_tools[k] = v
  39. else
  40. getmetatable(v).__index = all.noneitemdef_default
  41. end
  42. end
  43. for k, v in pairs(all) do
  44. core[k] = v
  45. end
  46. end
  47. -- For tables that are indexed by item name:
  48. -- If table[X] does not exist, default to table[core.registered_aliases[X]]
  49. local alias_metatable = {
  50. __index = function(t, name)
  51. return rawget(t, core.registered_aliases[name])
  52. end
  53. }
  54. setmetatable(core.registered_items, alias_metatable)
  55. setmetatable(core.registered_nodes, alias_metatable)
  56. setmetatable(core.registered_craftitems, alias_metatable)
  57. setmetatable(core.registered_tools, alias_metatable)
  58. builtin_shared.cache_content_ids()