init.lua 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. survivalist = survivalist or {}
  2. survivalist.modpath = minetest.get_modpath("survivalist")
  3. survivalist.players = survivalist.players or {}
  4. survivalist.groups = survivalist.groups or {}
  5. -- XP requirements. Amounts are somewhat arbitrary.
  6. survivalist.xp_minimum = 700
  7. survivalist.xp_surface = 700
  8. survivalist.xp_cave = 1500
  9. survivalist.xp_nether = 2400
  10. -- XP completion requirements. Amounts are somewhat arbitrary.
  11. survivalist.completion_xp_surface = 1100
  12. survivalist.completion_xp_cave = 2700
  13. survivalist.completion_xp_nether = 4500
  14. function survivalist.have_xp(pname, ch)
  15. local amount = (survivalist["xp_" .. ch]) or 0
  16. return (xp.get_xp(pname, "digxp") >= amount)
  17. end
  18. -- Localize for performance.
  19. local vector_distance = vector.distance
  20. local vector_round = vector.round
  21. local math_floor = math.floor
  22. local math_random = math.random
  23. -- Positions of surface & nether cities.
  24. local surfacecitypos = {x=0, y=-8, z=0}
  25. local nethercitypos = {x=0, y=-30793, z=0}
  26. -- Valid gamemodes are 'surface', 'cave', & 'nether'.
  27. -- The game rules description. Shown in formspec.
  28. survivalist.gamerules =
  29. "===| Survival Challenge Description |===\n\n" ..
  30. "This page contains the rules for this mini-game, with options to begin a Challenge or claim victory on a Challenge currently in-progress.\n\n" ..
  31. "The Survival Challenge focuses on survival in the wild far from developed regions. You can play it solo or with other players.\n\n" ..
  32. "You will need to scroll this text in order to read all of it.\n\n" ..
  33. "When beginning a Challenge it is possible to end up in a starting situation where survival is impossible. You always have the option to cancel without affecting your score.\n\n" ..
  34. "On accepting a challenge you are transported into a small dungeon, which is at a distant location in the world very far from the Surface Colony and the Nether City. The minimum distance you’ll be teleported is 10,000 meters; the server will not choose a dungeon closer to either city than this. You’ll have the basic starting items that every player starts with when they first join the server, and the dungeon has a chest containing additional items to make getting a head-start a little easier, since otherwise some scenarios would be impossible to beat. The chest contents differ between challenge types and are somewhat random.\n\n" ..
  35. "Before you can begin a challenge all your inventories must be empty: your main inventory, your craft grid, the craft-preview and craft-output inventory slots (yes, those too, cheaters!), your Star-Fissure Box, your armor inventory, your bags, and the wielditem inventory (if you don’t know what this is, then don’t worry about it). Proof of Citizenship tokens are exempt.\n\n" ..
  36. "You can play this mini-game cooperatively. To do so one player should to start a Survival Challenge just as if they were going to play solo. Whenever a player starts a Challenge there is a 5 minute window in which other players can join the first player by being transported to the same dungeon. To join another player in their dungeon, you must stand within 5 meters of the place where they stood at the time they began their Challenge. You must also choose the same gamemode (Iceworld, Underearth, or Netherealm) as the first player. If you are too far away, or you choose a wrong gamemode, or the 5 minute window has passed, you won’t be transported to the same dungeon. If you are are unsuccessful and are not teleported to the same dungeon, you can simply cancel and try again if you wish. There is no limit on the number of players who may play cooperatively. Every player who joins extends the 5-minute window a bit. Do note that supplies are not increased with each player beyond the first, so you’ll need to share. Players win or lose the Challenge separately, so if one player has a fatal accident, the remaining players can keep playing.\n\n" ..
  37. "Finding a suitable location to build the starting dungeon can take the server some time, so you have a few seconds to interact with the world after accepting a challenge. Don’t rely on this, though.\n\n" ..
  38. "(Note to builders in distant lands: the server will not overwrite any of your builds when creating a player’s starting dungeon, as long as your builds are protected. Although the world is vast and the chance of randomly selecting a location inside someone else’s home is small, the server nonetheless performs checks to avoid accidents.)\n\n" ..
  39. "In order to win after starting a challenge, you must find your way back to the Surface Colony OR the Nether City and claim victory (press the ‘Claim Victory’ button below these rules) in the Central Square of either location. Finding civilization to claim victory is your goal; avoiding a fatal death during your travels in the wild is your challenge. Rules for death are below the Challenge descriptions.\n\n" ..
  40. "Note that you may to use your Proof of Citizenship to teleport you the rest of the way to the Surface Colony or Nether City once you are in range of the teleport beacons. Use of diamond teleporters is also allowed, as are flameportals which go to and from the Netherealm, or any other form of transportation. However, when starting a challenge your flameportal’s previous return position (if you had one set) is erased to prevent using a return portal as a ‘cheat’ to obtain an easy win. The same is true for beds.\n\n" ..
  41. "There are three challenge types: the Iceworld Challenge, the Underearth Challenge, and the Netherealm Challenge, with difficulty being in that order. Each challenge awards Skill Marks when victory is successfully claimed. The Skill Marks are Copper, Silver, and Gold. The amount of marks you’ll receive on successful completion of a challenge depends on the distance you travel to either the Surface Colony or the Nether City, starting from your initial dungeon. You receive 1 mark for every 1K meters of distance (rounded down), minus the first 10 kilometers. You always receive at least 1 Skill Mark.\n\n" ..
  42. "The following is a basic description of each challenge.\n\n" ..
  43. "In the Iceworld Challenge you start in an ice-and-brick dungeon at what is normally sea-level (if this server were to have a liquid sea). Chances are, you’ll be under a mountain or below a raised plain, so your first goal will be to dig up and find the surface. If you are very lucky you will be on an ice-lake instead. Avoid the icemen and don’t go out at night! This is the easiest challenge. The first time you complete it successfully, the skill learned causes your wield-hotbar (the 8 itemslots below your health and hunger stats) to expand to 16 slots for easier building. (Note: if you prefer the regular 8 slots anyway and don’t want the bigger version, you can let the server administrator know, to get it reset back to 8 slots!)\n\n" ..
  44. "In the Underearth Challenge you start in a cobble dungeon deep under the earth, between -24K and -3K. Since you are underground, your first priority is using the materials in the starting chest to build a small farm. Note that the server never locates a dungeon to be floating in the middle of a caverealm void, but it is remotely possible that you could have lava outside the dungeon walls. Once you have a food source, the main difficulty is digging your way to the surface or to the nether (if you choose to claim victory in the Nether City). Beware of mining instabilities! Note that this Challenge typically takes the longest to complete. Once you’ve established a base with a bed, consider using Obsidian Gateways to try and bring yourself closer to the surface. The first time you complete this Challenge, you unlock a hidden feature of the Key of Citizenship which allows you to jaunt (teleport) to other players who also possess a Key—provided their Key’s beacon signal can be successfully triangulated ….\n\n" ..
  45. "The Netherealm Challenge is similar to the Underearth Challenge, except that your starting dungeon is located in the second-worst place in the world, some 60 meters under the brimstone ocean. (The worst place in the world is the islands directly above all that lava.) Your starting dungeon is made of black rackstone, but don’t imagine you’ll be able to use it for anything except stone picks. You need to be very, very careful in this challenge, because lava is everywhere, and netherack is nasty to dig. There are very few usable resources in the nether, so finding the few resources you can is very important. The first thing you need to do is dig up to the islands without letting the brimstone ocean flow into your dungeon. Once there you can explore the brimstone cavern for a few meager resources. In this Challenge, you are recommended to claim victory in the Nether City rather than attempt to dig to the surface. However, those who dig to the surface will receive more reward. If you choose to dig up out of the nether into normal rock you’ll be able to find a few more resources, and the caverealms of the Underearth Challenge will seem like a garden. :-) The first time you beat this Challenge, you unlock a hidden feature of the Key of Citizenship which allows you to access its little-understood cloaking capabilities, which can render you invisible in all but a few circumstances ….\n\n" ..
  46. "After starting a Challenge you may cancel it without affecting your death/success score. This option is available as long as a Challenge is running. The conditions for canceling a challenge are the same as the conditions for starting one: all your inventories must be empty except for passport tokens. (You cannot bring anything with you when you cancel.) You may choose this option if, for instance, you do not like your starting situation. When you cancel a Challenge you are returned to the same spot you were when you began the Challenge.\n\n" ..
  47. "Rules for death: you lose the Challenge if you die and have not slept in a bed. Lacking a bed is your initial state whenever you begin a Challenge, and you avoid losing the Challenge by crafting a bed and sleeping in it. Afterwards if you die you will respawn in your bed and the Challenge will continue as before. Note that a bed is only good for a limited number of respawns, so you should remember to sleep often to refresh it.\n\n" ..
  48. "Warning: since your bed is lost if you are killed by another player (but not by a mob or the environment), the effect of being murdered is that you will fail the Challenge. Your murderer does not get any special benefits other than hearing your screams of fury.\n\n" ..
  49. "Each player’s total number of victories, fatal deaths, and victory streaks are recorded permanently, as well as the number of victories in the three individual challenges, and the total number of marks earned. This information is never reset.\n\n" ..
  50. "===| Challenge Description End |==="
  51. -- Add player to a list of players contained in the database.
  52. -- This is because we can't iterate over the database entries directly,
  53. -- so we need a way to find all players who have ranking entries.
  54. function survivalist.add_player_to_rankings(pname)
  55. -- Obtain the database in string form.
  56. local data = survivalist.modstorage:get_string(":ranked_players:")
  57. -- If the string is empty, create a dummy table for deserialization.
  58. if data == "" then
  59. data = "return {}"
  60. end
  61. -- Deserialize the rankings database into a table.
  62. local tb = minetest.deserialize(data)
  63. -- Add player to table.
  64. tb[pname] = true
  65. -- Reserialize the database table.
  66. local serialized = minetest.serialize(tb)
  67. -- Don't overwrite the old database unless the table serialized successfully.
  68. if type(serialized) == "string" then
  69. survivalist.modstorage:set_string(":ranked_players:", serialized)
  70. end
  71. end
  72. function survivalist.get_ranking_entries()
  73. -- Obtain the database in string form.
  74. local data = survivalist.modstorage:get_string(":ranked_players:")
  75. -- If the string is empty, create a dummy table for deserialization.
  76. if data == "" then
  77. data = "return {}"
  78. end
  79. -- Deserialize the rankings database into a table.
  80. local tb = minetest.deserialize(data)
  81. -- Return the table.
  82. return tb or {}
  83. end
  84. function survivalist.inventory_empty(inv, name)
  85. local count = 0
  86. for i = 1, inv:get_size(name) do
  87. local stack = inv:get_stack(name, i)
  88. if not passport.is_passport(stack:get_name()) then
  89. if stack:get_count() > 0 then
  90. count = count + stack:get_count()
  91. end
  92. end
  93. end
  94. return (count == 0)
  95. end
  96. function survivalist.check_inventories_empty(pname)
  97. local player = minetest.get_player_by_name(pname)
  98. if not player then
  99. return
  100. end
  101. local inv = player:get_inventory()
  102. if survivalist.inventory_empty(inv, "main") and
  103. survivalist.inventory_empty(inv, "craft") and
  104. survivalist.inventory_empty(inv, "armor") and
  105. survivalist.inventory_empty(inv, "voidchest:voidchest") and
  106. survivalist.inventory_empty(inv, "craftresult") and
  107. survivalist.inventory_empty(inv, "craftpreview") and
  108. survivalist.inventory_empty(inv, "bag1contents") and
  109. survivalist.inventory_empty(inv, "bag2contents") and
  110. survivalist.inventory_empty(inv, "bag3contents") and
  111. survivalist.inventory_empty(inv, "bag4contents") and
  112. survivalist.inventory_empty(inv, "xchest") and
  113. survivalist.inventory_empty(inv, "bag1") and
  114. survivalist.inventory_empty(inv, "bag2") and
  115. survivalist.inventory_empty(inv, "bag3") and
  116. survivalist.inventory_empty(inv, "bag4") then
  117. return true
  118. end
  119. end
  120. -- Loot parameters & stuff.
  121. dofile(survivalist.modpath .. "/loot.lua")
  122. -- Actually teleport the player to the start location and announce in chat.
  123. -- Also, record that the game has begun.
  124. function survivalist.teleport_and_announce(pname, pos, gamemode)
  125. local player = minetest.get_player_by_name(pname)
  126. if not player then
  127. return
  128. end
  129. -- Player's inventories must be empty.
  130. if not survivalist.check_inventories_empty(pname) then
  131. minetest.chat_send_player(pname, "# Server: All your inventories (including the Starfissure Box and armor) must be empty before you can begin a challenge (the Proof of Citizenship does not count). You will receive starting items when you begin.")
  132. easyvend.sound_error(pname)
  133. return
  134. end
  135. -- Abort if player is trying to cheat by sitting in a cart. >:)
  136. if default.player_attached[pname] then
  137. minetest.chat_send_player(pname, "# Server: Transport error. Player attached!")
  138. return
  139. end
  140. -- Record home position.
  141. local homepos = vector_round(player:get_pos())
  142. -- Teleport player.
  143. wield3d.on_teleport()
  144. player:set_pos(vector.add(pos, {x=math_random(-3, 3), y=0.5, z=math_random(-3, 3)}))
  145. -- Make sure player is healthy.
  146. heal.heal_health_and_hunger(pname)
  147. -- Remove posibility of cheating via netherportals.
  148. flameportal.clear_return_location(pname)
  149. -- Clear bed respawn position. Player must make new bed to survive.
  150. beds.clear_player_spawn(pname)
  151. -- Give the game name some interesting names.
  152. local gamestring = "Void"
  153. if gamemode == "surface" then
  154. gamestring = "Iceworld"
  155. elseif gamemode == "cave" then
  156. gamestring = "Underearth"
  157. elseif gamemode == "nether" then
  158. gamestring = "Netherealm"
  159. end
  160. -- Inform player the game has begun.
  161. if not gdac.player_is_admin(pname) then
  162. local dname = rename.gpn(pname)
  163. minetest.chat_send_all("# Server: Player <" .. dname .. "> has begun a test of skill in the " .. gamestring .. " at " .. rc.pos_to_namestr(vector_round(pos)) .. "!")
  164. else
  165. minetest.chat_send_player(pname, "# Server: You have begun a test of skill in the " .. gamestring .. " at " .. rc.pos_to_namestr(vector_round(pos)) .. "!")
  166. end
  167. survivalist.shout_player_stats(pname)
  168. minetest.chat_send_player(pname, "# Server: To win, you must find the city and claim victory in the Main Square. If you die without sleeping, you will fail the Challenge.")
  169. -- Give player the starting items.
  170. give_initial_stuff.give(player)
  171. -- Record the player's new gamemode.
  172. survivalist.modstorage:set_string(pname .. ":mode", gamemode)
  173. -- Record the player's starting position.
  174. survivalist.modstorage:set_string(pname .. ":pos", minetest.pos_to_string(vector_round(pos)))
  175. -- Record the player's home position. Used when canceling a Challenge.
  176. survivalist.modstorage:set_string(pname .. ":home", minetest.pos_to_string(homepos))
  177. -- Record the amount of XP player has at the start of the Challenge.
  178. survivalist.modstorage:set_string(pname .. ":xp", tostring(xp.get_xp(pname, "digxp")))
  179. -- Record that this player is accepting groups.
  180. survivalist.groups[pname] = survivalist.groups[pname] or {count=0}
  181. survivalist.groups[pname].count = survivalist.groups[pname].count + 1
  182. minetest.after(60*5, function()
  183. if survivalist.groups[pname] then
  184. survivalist.groups[pname].count = survivalist.groups[pname].count - 1
  185. if survivalist.groups[pname].count <= 0 then
  186. survivalist.groups[pname] = nil
  187. end
  188. end
  189. end)
  190. end
  191. -- This function must find a location for the player and teleport them there.
  192. -- Also create a starting dungeon (must not destroy protected stuff) and a chest with extra resources.
  193. function survivalist.prepare_dungeon(pname, pos, gamemode)
  194. -- Positions to load. Need a larger area in order to make sure any protections are discovered.
  195. local minp = vector.add(pos, vector.new(-64, -64, -64))
  196. local maxp = vector.add(pos, vector.new(64, 64, 64))
  197. -- Dungeon coordinates.
  198. local dminp = vector.add(pos, vector.new(-4, 0, -4))
  199. local dmaxp = vector.add(pos, vector.new(4, 4, 4))
  200. -- Copy the position table so it doesn't get modified.
  201. local pos2 = table.copy(pos)
  202. -- Build callback function. When the map is loaded, we need to check protections and create the dungeon.
  203. local tbparam = {}
  204. local cb = function(blockpos, action, calls_remaining, param)
  205. if action == core.EMERGE_CANCELLED or action == core.EMERGE_ERRORED then
  206. minetest.chat_send_player(pname, "# Server: Internal error, try again or report.")
  207. easyvend.sound_error(pname)
  208. return
  209. end
  210. -- We don't do anything until the last callback.
  211. if calls_remaining ~= 0 then
  212. return
  213. end
  214. -- Check for the presence of a city-block.
  215. if city_block:in_city_suburbs(pos2) then
  216. minetest.chat_send_player(pname, "# Server: Error: did not succeed in finding a suitable start location! If this happens, just try again.")
  217. easyvend.sound_error(pname)
  218. return
  219. end
  220. -- Check for protections, and if there are none, create a dungeon.
  221. for x = dminp.x, dmaxp.x, 1 do
  222. for y = dminp.y, dmaxp.y, 1 do
  223. for z = dminp.z, dmaxp.z, 1 do
  224. if minetest.test_protection({x=x, y=y, z=z}, "") then
  225. -- Return failure if target is protected. This shouldn't happen often.
  226. minetest.chat_send_player(pname, "# Server: Error: did not succeed in finding a suitable start location! If this happens, just try again.")
  227. easyvend.sound_error(pname)
  228. return
  229. end
  230. end
  231. end
  232. end
  233. -- Check if spawning in air (e.g., due to large cavern).
  234. if minetest.get_node(pos2).name == "air" then
  235. minetest.chat_send_player(pname, "# Server: Error: did not succeed in finding a suitable start location! If this happens, just try again.")
  236. easyvend.sound_error(pname)
  237. return
  238. end
  239. -- No protections? Create the dungeon.
  240. -- Generate dungeon for the player to spawn in.
  241. local path = survivalist.modpath .. "/schematics/survivalist_" .. gamemode .. "_dungeon.mts"
  242. minetest.place_schematic(vector.add(pos2, {x=-4, y=0, z=-4}), path, "random", nil, true)
  243. -- Choose a location for the chest.
  244. local chestpos = vector.add(pos2, vector.new(math_random(-3, 3), 1, math_random(-3, 3)))
  245. chestpos = vector_round(chestpos)
  246. -- Create chest with stuff.
  247. minetest.set_node(chestpos, {
  248. name = "morechests:goldchest_public_closed",
  249. param2 = math_random(0, 3),
  250. })
  251. local meta = minetest.get_meta(chestpos)
  252. local inv = meta:get_inventory()
  253. if inv then
  254. survivalist.fill_loot_chest(inv, gamemode)
  255. end
  256. -- Teleport player and announce.
  257. survivalist.teleport_and_announce(pname, pos2, gamemode)
  258. end
  259. -- Emerge the target area. Once emergence is complete the dungeon will spawn.
  260. minetest.chat_send_player(pname, "# Server: Checking reliability of target location ... please stand by, this can take several seconds.")
  261. minetest.emerge_area(minp, maxp, cb, tbparam)
  262. end
  263. function survivalist.shout_player_stats(pname)
  264. local ms = survivalist.modstorage
  265. local wins_total = pname .. ":wins_total"
  266. local wins_streak = pname .. ":wins_streak"
  267. local wins_surface = pname .. ":wins_surface"
  268. local wins_cave = pname .. ":wins_cave"
  269. local wins_nether = pname .. ":wins_nether"
  270. local wins_fail = pname .. ":wins_fail"
  271. local wins_bstreak = pname .. ":wins_bstreak"
  272. local wins_tokens = pname .. ":wins_tokens"
  273. if not gdac.player_is_admin(pname) then
  274. local dname = rename.gpn(pname)
  275. minetest.chat_send_all("# Server: Survivalist stats for <" ..
  276. dname .. ">: Victories: " .. ms:get_int(wins_total) ..
  277. ". Deaths: " .. ms:get_int(wins_fail) ..
  278. ". Iceworld: " .. ms:get_int(wins_surface) ..
  279. ". Underearth: " .. ms:get_int(wins_cave) ..
  280. ". Netherealm: " .. ms:get_int(wins_nether) ..
  281. ". C-Streak: " .. ms:get_int(wins_streak) ..
  282. ". B-Streak: " .. ms:get_int(wins_bstreak) ..
  283. ". Marks: " .. ms:get_int(wins_tokens) ..
  284. ".")
  285. end
  286. end
  287. function survivalist.game_in_progress(pname)
  288. local cg = survivalist.modstorage:get_string(pname .. ":mode")
  289. if cg == "surface" or cg == "cave" or cg == "nether" then
  290. return true
  291. end
  292. end
  293. -- This function is called when the player wants to begin the game.
  294. -- It must handle all game validation.
  295. function survivalist.start_game(pname, gamemode)
  296. -- Get player and make sure he exists.
  297. local player = minetest.get_player_by_name(pname)
  298. if not player then
  299. return
  300. end
  301. -- Is a game already running?
  302. local currentgame = survivalist.modstorage:get_string(pname .. ":mode")
  303. if currentgame == "surface" or currentgame == "cave" or currentgame == "nether" then
  304. minetest.chat_send_player(pname, "# Server: You are already engaged in a Survivalist Challenge, you cannot start a concurrent game.")
  305. easyvend.sound_error(pname)
  306. return
  307. end
  308. local pp = player:get_pos()
  309. if pp.y < -100 or pp.y > 1000 then
  310. minetest.chat_send_player(pname, "# Server: You need to be on the surface of the Overworld to start a challenge.")
  311. easyvend.sound_error(pname)
  312. return
  313. end
  314. -- Validate the gamemode.
  315. if type(gamemode) ~= "string" then
  316. minetest.chat_send_player(pname, "# Server: No starting option selected!")
  317. easyvend.sound_error(pname)
  318. return
  319. end
  320. if gamemode ~= "surface" and gamemode ~= "cave" and gamemode ~= "nether" then
  321. minetest.chat_send_player(pname, "# Server: You must choose a valid starting option!")
  322. easyvend.sound_error(pname)
  323. return
  324. end
  325. -- Player must have minimum XP for this kind of Challenge.
  326. if not survivalist.have_xp(pname, gamemode) then
  327. minetest.chat_send_player(pname, "# Server: Insufficient XP to start this Challenge.")
  328. easyvend.sound_error(pname)
  329. return
  330. end
  331. -- Player's inventories must be empty.
  332. if not survivalist.check_inventories_empty(pname) then
  333. minetest.chat_send_player(pname, "# Server: All your inventories (including the Starfissure Box and armor) must be empty before you can begin a challenge (the Proof of Citizenship does not count). You will receive starting items when you begin.")
  334. easyvend.sound_error(pname)
  335. return
  336. end
  337. local pos = {x=0, y=0, z=0}
  338. local group = false
  339. -- Group survival: check if the player should be grouped with someone else.
  340. local players = minetest.get_connected_players()
  341. for k, v in ipairs(players) do
  342. local oname = v:get_player_name()
  343. local omode = survivalist.modstorage:get_string(oname .. ":mode")
  344. -- Gamemodes must match.
  345. if omode == gamemode then
  346. local opos = minetest.string_to_pos(survivalist.modstorage:get_string(oname .. ":home"))
  347. -- Home position must have been recorded.
  348. if opos then
  349. -- We must be close enough to the other player's home pos, in order to be grouped with them.
  350. if vector_distance(opos, player:get_pos()) <= 5 then
  351. -- The other player must currently be accepting groups.
  352. -- Groups are not saved across restarts; this means that a player cannot
  353. -- team with another after the server restarts, even if less time than
  354. -- the time limit has gone by.
  355. if survivalist.groups[oname] then
  356. -- Get location of the other player's dungeon.
  357. local p2 = minetest.string_to_pos(survivalist.modstorage:get_string(oname .. ":pos"))
  358. if p2 then
  359. pos = p2
  360. group = true
  361. break
  362. end
  363. end
  364. end
  365. end
  366. end
  367. end
  368. if not group then
  369. -- Find a random position on the X,Z plane.
  370. while vector_distance(pos, surfacecitypos) < 10000 or vector_distance(pos, nethercitypos) < 10000 do
  371. for j, k in ipairs({"x", "z"}) do
  372. pos[k] = math_random(-30000, 30000)
  373. end
  374. -- Gamemode determines depth.
  375. if gamemode == "surface" then
  376. pos.y = -10
  377. elseif gamemode == "cave" then
  378. pos.y = math_random(-24000, -3000)
  379. elseif gamemode == "nether" then
  380. pos.y = math_random(-30860, -30810)
  381. end
  382. end
  383. end
  384. -- Prepare target location. The remaining logic is executed from a callback.
  385. if group then
  386. -- The dungeon has already been generated.
  387. survivalist.teleport_and_announce(pname, pos, gamemode)
  388. else
  389. -- Need to generate a dungeon, then teleport the player.
  390. survivalist.prepare_dungeon(pname, pos, gamemode)
  391. end
  392. end
  393. -- Checks if a player is in range to claim victory.
  394. -- Performs no other validation; this is a simple distance check.
  395. function survivalist.player_in_victory_range(pname)
  396. local player = minetest.get_player_by_name(pname)
  397. if not player then
  398. return
  399. end
  400. local pos = player:get_pos()
  401. if vector_distance(pos, surfacecitypos) <= 20 then
  402. return true
  403. elseif vector_distance(pos, nethercitypos) <= 20 then
  404. return true
  405. end
  406. end
  407. -- Called when the player wants to claim victory. Must validate.
  408. function survivalist.attempt_claim(pname)
  409. local player = minetest.get_player_by_name(pname)
  410. if not player then
  411. return
  412. end
  413. -- Make sure a game is actually running.
  414. local currentgame = survivalist.modstorage:get_string(pname .. ":mode")
  415. if currentgame ~= "surface" and currentgame ~= "cave" and currentgame ~= "nether" then
  416. minetest.chat_send_player(pname, "# Server: You are not currently engaged in a Survivalist Challenge.")
  417. easyvend.sound_error(pname)
  418. return
  419. end
  420. -- Check if player has earned the needed XP over the course of the Challenge.
  421. -- This ensures player does not complete the Challenge too fast, and needs to actually
  422. -- take some time, make a base/home, do some farming, etc!
  423. do
  424. local sxp = survivalist.modstorage:get_string(pname .. ":xp")
  425. local oxp = math.floor(tonumber(sxp) or 0)
  426. local cxp = math.floor(xp.get_xp(pname, "digxp"))
  427. local wxp = (survivalist["completion_xp_" .. currentgame]) or 0
  428. local dxp = (cxp - oxp)
  429. if dxp < wxp then
  430. local nxp = math.floor(oxp + wxp)
  431. minetest.chat_send_player(pname, "# Server: You need to obtain at least " .. wxp .. " XP over the course of the Challenge, first.")
  432. minetest.chat_send_player(pname, "# Server: You started with " .. oxp .. " XP. You must reach " .. nxp .. " XP!")
  433. easyvend.sound_error(pname)
  434. return
  435. end
  436. end
  437. -- Check if the player is in the city.
  438. local pos = player:get_pos()
  439. local cityname = ""
  440. local home_pos = minetest.string_to_pos(survivalist.modstorage:get_string(pname .. ":home"))
  441. -- The position and name of the city the player claims victory in.
  442. local finalcitypos
  443. if vector_distance(pos, surfacecitypos) <= 20 then
  444. finalcitypos = table.copy(surfacecitypos)
  445. cityname = "Surface Colony"
  446. elseif vector_distance(pos, nethercitypos) <= 20 then
  447. finalcitypos = table.copy(nethercitypos)
  448. cityname = "Nether City"
  449. elseif vector_distance(pos, home_pos) <= 20 then
  450. finalcitypos = table.copy(home_pos)
  451. cityname = "place they started"
  452. end
  453. if not finalcitypos then
  454. minetest.chat_send_player(pname, "# Server: You must be within 20 meters of the main square of the Surface Colony or the Nether City or the place where you started the challenge in order to claim victory!")
  455. easyvend.sound_error(pname)
  456. return
  457. end
  458. -- What rank (copper, silver, gold) has the player earned?
  459. local ranks = {
  460. ["surface"] = {rank="copper", upper="Copper"},
  461. ["cave"] = {rank="silver", upper="Silver"},
  462. ["nether"] = {rank="gold", upper="Gold"},
  463. }
  464. local rank = ranks[currentgame].rank
  465. local upperank = ranks[currentgame].upper
  466. -- Reward the player.
  467. local tokencount = 1
  468. local startpos = minetest.string_to_pos(survivalist.modstorage:get_string(pname .. ":pos"))
  469. -- If the starting position couldn't be parsed we'll just give the player 1 token.
  470. if startpos then
  471. local dist = vector_distance(finalcitypos, startpos)
  472. -- Discount the minimum distance.
  473. dist = dist - 10000
  474. -- Get distance in kilometers.
  475. -- One skill mark per extra kilometer over 10k.
  476. dist = math_floor(dist / 1000)
  477. -- Clamp, just in case.
  478. if dist < 1 then
  479. dist = 1
  480. end
  481. tokencount = dist
  482. end
  483. local dname = rename.gpn(pname)
  484. if not gdac.player_is_admin(pname) then
  485. minetest.chat_send_all("# Server: Player <" .. dname .. "> has claimed victory in the " .. cityname .. "!")
  486. minetest.chat_send_all("# Server: Player <" .. dname .. ">'s skill has been tested in a Survival Challenge and proved worthy.")
  487. minetest.chat_send_all("# Server: Player <" .. dname .. "> has earned " .. tokencount .. " " .. upperank .. " Skill Mark(s).")
  488. else
  489. minetest.chat_send_player(pname, "# Server: You have won the Survival Challenge!")
  490. end
  491. local inv = player:get_inventory()
  492. local leftover = inv:add_item("main", ItemStack("survivalist:" .. rank .. "_skill_token " .. tokencount))
  493. -- No room in inventory? Drop 'em.
  494. if leftover:get_count() > 0 then
  495. minetest.item_drop(leftover, player, pos)
  496. if not gdac.player_is_admin(pname) then
  497. minetest.chat_send_all("# Server: Player <" .. dname .. ">'s Skill Mark was dropped on the ground!")
  498. end
  499. end
  500. minetest.chat_send_player(pname, "# Server: You should have received a skill mark in your inventory. If your inventory was full, check near your feet!")
  501. -- Record that the challenge is over.
  502. survivalist.modstorage:set_string(pname .. ":mode", nil)
  503. survivalist.modstorage:set_string(pname .. ":pos", nil)
  504. survivalist.modstorage:set_string(pname .. ":home", nil)
  505. survivalist.modstorage:set_string(pname .. ":xp", nil)
  506. survivalist.players[pname].choice = nil
  507. -- Record total wins, win streaks, and win types.
  508. local ms = survivalist.modstorage
  509. local wins_total = pname .. ":wins_total"
  510. local wins_streak = pname .. ":wins_streak"
  511. local wins_bstreak = pname .. ":wins_bstreak"
  512. local wins_type = pname .. ":wins_" .. currentgame
  513. local wins_tokens = pname .. ":wins_tokens"
  514. ms:set_int(wins_total, ms:get_int(wins_total) + 1)
  515. ms:set_int(wins_streak, ms:get_int(wins_streak) + 1)
  516. ms:set_int(wins_type, ms:get_int(wins_type) + 1)
  517. ms:set_int(wins_tokens, ms:get_int(wins_tokens) + tokencount)
  518. -- If current streak is better than best streak, update best streak.
  519. -- Best streak is never erased.
  520. if ms:get_int(wins_streak) > ms:get_int(wins_bstreak) then
  521. ms:set_int(wins_bstreak, ms:get_int(wins_streak))
  522. end
  523. -- Grant player the big_hotbar priv.
  524. -- Rewarded by the 'surface' gamemode only.
  525. if currentgame == "surface" then
  526. if not minetest.check_player_privs(player, {big_hotbar=true}) then
  527. local privs = minetest.get_player_privs(pname)
  528. privs.big_hotbar = true
  529. minetest.set_player_privs(pname, privs)
  530. minetest.notify_authentication_modified(pname)
  531. player:hud_set_hotbar_image("gui_hotbar2.png")
  532. player:hud_set_hotbar_itemcount(16)
  533. end
  534. end
  535. -- Let everyone know the player's win stats.
  536. survivalist.shout_player_stats(pname)
  537. -- Add player's name to the rankings.
  538. survivalist.add_player_to_rankings(pname)
  539. end
  540. function survivalist.player_beat_cave_challenge(pname)
  541. local ms = survivalist.modstorage
  542. local va = ms:get_int(pname .. ":wins_cave") or 0
  543. if va > 0 then
  544. return true
  545. end
  546. return false
  547. end
  548. function survivalist.player_beat_nether_challenge(pname)
  549. local ms = survivalist.modstorage
  550. local va = ms:get_int(pname .. ":wins_nether") or 0
  551. if va > 0 then
  552. return true
  553. end
  554. return false
  555. end
  556. -- If a player joins the server and a Survivalist Challenge is running, inform them.
  557. function survivalist.on_joinplayer(player)
  558. local pname = player:get_player_name()
  559. survivalist.players[pname] = {}
  560. minetest.after(0.5, function()
  561. local player = minetest.get_player_by_name(pname)
  562. if not player then
  563. return
  564. end
  565. local gamemode = survivalist.modstorage:get_string(pname .. ":mode")
  566. if gamemode == "surface" or gamemode == "cave" or gamemode == "nether" then
  567. local dname = rename.gpn(pname)
  568. if not gdac.player_is_admin(pname) then
  569. minetest.chat_send_all("# Server: Player <" .. dname .. "> is engaged in a Survival Challenge.")
  570. end
  571. minetest.chat_send_player(pname, "# Server: You are in a Survival Challenge. Avoid death by sleeping!")
  572. minetest.chat_send_player(pname, "# Server: Find the Surface Colony or the Nether City and claim victory in the Central Square to beat the Challenge.")
  573. end
  574. end)
  575. end
  576. -- Dying cancels the challenge.
  577. function survivalist.on_dieplayer(player)
  578. local pname = player:get_player_name()
  579. -- If player has a bed respawn set, then they don't fail the challenge.
  580. if beds.has_respawn_bed(pname) then
  581. if survivalist.game_in_progress(pname) then
  582. minetest.after(1, function()
  583. minetest.chat_send_player(pname, "# Server: You should respawn in your bed. Warning: if you die without a bed, you will fail the Challenge.")
  584. end)
  585. end
  586. return
  587. end
  588. local gamemode = survivalist.modstorage:get_string(pname .. ":mode")
  589. if gamemode == "surface" or gamemode == "cave" or gamemode == "nether" then
  590. -- Delay the chat messages slightly.
  591. minetest.after(1, function()
  592. if not gdac.player_is_admin(pname) then
  593. local dname = rename.gpn(pname)
  594. minetest.chat_send_all("# Server: Player <" .. dname .. ">'s survival skill was tested and found wanting!")
  595. end
  596. survivalist.shout_player_stats(pname)
  597. minetest.chat_send_player(pname, "# Server: You failed the Survivalist Challenge. No goodies for you!")
  598. end)
  599. -- No game in progress. Win streak is reset to 0.
  600. survivalist.modstorage:set_string(pname .. ":mode", nil)
  601. survivalist.modstorage:set_string(pname .. ":pos", nil)
  602. survivalist.modstorage:set_string(pname .. ":home", nil)
  603. survivalist.modstorage:set_string(pname .. ":xp", nil)
  604. survivalist.modstorage:set_int(pname .. ":wins_streak", 0)
  605. -- Count loss.
  606. local ms = survivalist.modstorage
  607. local ff = pname .. ":wins_fail"
  608. ms:set_int(ff, ms:get_int(ff) + 1)
  609. -- Add player's name to the rankings (deaths count too).
  610. survivalist.add_player_to_rankings(pname)
  611. end
  612. end
  613. -- Abort a running game without loss to player score.
  614. function survivalist.abort_game(pname)
  615. local gamemode = survivalist.modstorage:get_string(pname .. ":mode")
  616. if gamemode == "surface" or gamemode == "cave" or gamemode == "nether" then
  617. if not survivalist.check_inventories_empty(pname) then
  618. minetest.chat_send_player(pname, "# Server: All your inventories (including the Starfissure Box and armor) must be empty before you may cancel a challenge (the Proof of Citizenship does not count).")
  619. easyvend.sound_error(pname)
  620. return
  621. end
  622. minetest.chat_send_player(pname, "# Server: Challenge termination confirmed.")
  623. local target = rc.static_spawn("abyss")
  624. target = minetest.string_to_pos(survivalist.modstorage:get_string(pname .. ":home")) or target
  625. -- Pre-teleport callback function. We may need to abort to prevent cheating.
  626. local pcb = function()
  627. if not survivalist.check_inventories_empty(pname) then
  628. minetest.chat_send_player(pname, "# Server: All your inventories (including the Starfissure Box and armor) must be empty before you may cancel a challenge (the Proof of Citizenship does not count).")
  629. easyvend.sound_error(pname)
  630. -- Returning success means to abort the teleport.
  631. -- Only applicable to pre-teleport callbacks.
  632. return true
  633. end
  634. -- Abort if player is trying to cheat by sitting in a cart. >:)
  635. if default.player_attached[pname] then
  636. minetest.chat_send_player(pname, "# Server: Transport error. Player attached!")
  637. return true
  638. end
  639. end
  640. -- Don't clear the challenge gamestate unless teleport successful.
  641. local bcb = function()
  642. -- No game in progress.
  643. -- Do not touch scores.
  644. survivalist.modstorage:set_string(pname .. ":mode", nil)
  645. survivalist.modstorage:set_string(pname .. ":pos", nil)
  646. survivalist.modstorage:set_string(pname .. ":home", nil)
  647. survivalist.modstorage:set_string(pname .. ":xp", nil)
  648. flameportal.clear_return_location(pname)
  649. beds.clear_player_spawn(pname)
  650. if not gdac.player_is_admin(pname) then
  651. local dname = rename.gpn(pname)
  652. minetest.chat_send_all("# Server: Player <" .. dname .. "> canceled a Survival Challenge and went home.")
  653. else
  654. minetest.chat_send_player(pname, "# Server: You canceled a Survival Challenge and went home.")
  655. end
  656. end
  657. -- Teleport is forced.
  658. preload_tp.execute({
  659. player_name = pname,
  660. target_position = target,
  661. emerge_radius = 32,
  662. pre_teleport_callback = pcb,
  663. post_teleport_callback = bcb,
  664. force_teleport = true,
  665. send_blocks = true,
  666. particle_effects = true,
  667. })
  668. else
  669. minetest.chat_send_player(pname, "# Server: You are not in a Survival Challenge; cannot abort.")
  670. easyvend.sound_error(pname)
  671. end
  672. end
  673. -- Compost the GUI formspec.
  674. function survivalist.compose_formspec(pname)
  675. local modestring = ""
  676. local inchallenge = false
  677. local gamemode = survivalist.modstorage:get_string(pname .. ":mode")
  678. if gamemode == "surface" then
  679. modestring = " (You are currently in an Iceworld Challenge)"
  680. inchallenge = true
  681. elseif gamemode == "cave" then
  682. modestring = " (You are currently in an Underearth Challenge)"
  683. inchallenge = true
  684. elseif gamemode == "nether" then
  685. modestring = " (You are currently in a Netherealm Challenge)"
  686. inchallenge = true
  687. end
  688. local type_surface = "false"
  689. local type_cave = "false"
  690. local type_nether = "false"
  691. -- Choose which checkbox will be shown as selected.
  692. if survivalist.players[pname] and survivalist.players[pname].choice then
  693. local choice = survivalist.players[pname].choice
  694. if choice == "surface" then
  695. type_surface = "true"
  696. elseif choice == "cave" then
  697. type_cave = "true"
  698. elseif choice == "nether" then
  699. type_nether = "true"
  700. end
  701. end
  702. local formspec = ""
  703. formspec = formspec .. "size[8,7]" ..
  704. default.gui_bg ..
  705. default.gui_bg_img ..
  706. default.gui_slots ..
  707. "label[0,0;Survivalist Challenge" .. modestring .. "]" ..
  708. "textarea[0.3,0.5;8,4;rules;;" .. minetest.formspec_escape(survivalist.gamerules) .. "]"
  709. -- Show challenge choices only if no challenge is in progress.
  710. if not inchallenge and survivalist.have_xp(pname, "minimum") then
  711. formspec = formspec .. "label[0,4;Choose Your Challenge!]"
  712. if survivalist.have_xp(pname, "surface") then
  713. formspec = formspec .. "checkbox[0,4.3;type_surface;Surface Survival (Copper Mark);" .. type_surface .. "]"
  714. end
  715. if survivalist.have_xp(pname, "cave") then
  716. formspec = formspec .. "checkbox[0,4.8;type_cave;Cave Survival (Silver Mark);" .. type_cave .. "]"
  717. end
  718. if survivalist.have_xp(pname, "nether") then
  719. formspec = formspec .. "checkbox[0,5.3;type_nether;Nether Survival (Gold Mark);" .. type_nether .. "]"
  720. end
  721. end
  722. -- Choose between start/abort buttons.
  723. if inchallenge then
  724. formspec = formspec .. "button[0,6.2;2,1;abort;Go Home]"
  725. else
  726. if survivalist.have_xp(pname, "minimum") then
  727. formspec = formspec .. "button[0,6.2;3,1;start;Begin Challenge]"
  728. end
  729. end
  730. -- Show `Claim Victory` only if a challenge is in progress.
  731. if inchallenge then
  732. formspec = formspec .. "button[2,6.2;2,1;victory;Claim Victory]"
  733. end
  734. formspec = formspec .. "button[4,6.2;2,1;show_rankings;Rankings]" ..
  735. "button[6,6.2;2,1;close;Close]"
  736. return formspec
  737. end
  738. function survivalist.compose_rankings_formspec(pname)
  739. local players = survivalist.get_ranking_entries()
  740. local data = {}
  741. local ms = survivalist.modstorage
  742. for k, v in pairs(players) do
  743. data[#data+1] = {
  744. name = k,
  745. wins = ms:get_int(k .. ":wins_total"),
  746. deaths = ms:get_int(k .. ":wins_fail"),
  747. streak = ms:get_int(k .. ":wins_streak"),
  748. bstreak = ms:get_int(k .. ":wins_bstreak"),
  749. surface = ms:get_int(k .. ":wins_surface"),
  750. cave = ms:get_int(k .. ":wins_cave"),
  751. nether = ms:get_int(k .. ":wins_nether"),
  752. tokens = ms:get_int(k .. ":wins_tokens"),
  753. }
  754. end
  755. local form = ""
  756. form = form .. "size[13,7]" ..
  757. default.gui_bg ..
  758. default.gui_bg_img ..
  759. default.gui_slots ..
  760. "label[0,0;Survival Challenge Rankings]" ..
  761. "button[11,6.2;2,1;close_rankings;Close]"
  762. form = form .. "tablecolumns[color;text;text;text;text;text;text;text;text;text;text;text;text;text;text;text;text;text]"
  763. form = form .. "table[0,0.5;12.8,5;rankings_table;"
  764. form = form .. "#00ffff,Username,|,Victories,|,Deaths,|,C-Streak,|,B-Streak,|,Iceworld,|,Underearth,|,Netherealm,|,Marks,"
  765. for k, v in ipairs(data) do
  766. form = form .. ",<" .. rename.gpn(v.name) .. ">,|," .. v.wins .. ",|," .. v.deaths .. ",|," ..
  767. v.streak .. ",|," .. v.bstreak .. ",|," .. v.surface .. ",|," .. v.cave ..
  768. ",|," .. v.nether .. ",|," .. v.tokens .. ","
  769. end
  770. form = string.gsub(form, ",$", "")
  771. form = form .. ";1]"
  772. return form
  773. end
  774. function survivalist.show_rankings_formspec(pname)
  775. local formspec = survivalist.compose_rankings_formspec(pname)
  776. minetest.show_formspec(pname, "survivalist:rankings", formspec)
  777. end
  778. -- API function (called from passport mod, for instance).
  779. function survivalist.show_formspec(pname)
  780. local formspec = survivalist.compose_formspec(pname)
  781. minetest.show_formspec(pname, "survivalist:survivalist", formspec)
  782. end
  783. -- GUI form input handler.
  784. function survivalist.on_receive_fields(player, formname, fields)
  785. local pname = player:get_player_name()
  786. if formname ~= "survivalist:survivalist" and formname ~= "survivalist:rankings" then
  787. return
  788. end
  789. if fields.quit then
  790. return true
  791. end
  792. if fields.show_rankings then
  793. survivalist.show_rankings_formspec(pname)
  794. return true
  795. end
  796. if fields.rankings_table then
  797. survivalist.show_rankings_formspec(pname)
  798. return true
  799. end
  800. if fields.close_rankings or formname == "survivalist:rankings" then
  801. survivalist.show_formspec(pname)
  802. return true
  803. end
  804. -- Start game.
  805. if fields.start and survivalist.have_xp(pname, "minimum") then
  806. minetest.close_formspec(pname, "survivalist:survivalist")
  807. survivalist.start_game(pname, survivalist.players[pname].choice)
  808. return true
  809. end
  810. -- Claim victory.
  811. if fields.victory then
  812. minetest.close_formspec(pname, "survivalist:survivalist")
  813. survivalist.attempt_claim(pname)
  814. return true
  815. end
  816. -- Abort challenge.
  817. if fields.abort then
  818. minetest.close_formspec(pname, "survivalist:survivalist")
  819. survivalist.abort_game(pname)
  820. return true
  821. end
  822. -- Handle gamemode checkboxes.
  823. for j, k in ipairs({"surface", "cave", "nether"}) do
  824. if fields["type_" .. k] then
  825. if fields["type_" .. k] == "true" then
  826. survivalist.players[pname].choice = k
  827. else
  828. survivalist.players[pname].choice = nil
  829. end
  830. survivalist.show_formspec(pname)
  831. return true
  832. end
  833. end
  834. if fields.close then
  835. --minetest.close_formspec(pname, "survivalist:survivalist")
  836. passport.show_formspec(pname)
  837. return true
  838. end
  839. survivalist.show_formspec(pname)
  840. return true
  841. end
  842. -- Here belongs code which must run only once.
  843. if not survivalist.run_once then
  844. -- Obtain modstorage.
  845. survivalist.modstorage = minetest.get_mod_storage()
  846. minetest.register_privilege("big_hotbar", {
  847. description = "Player has double-wide hotbar for item wielding.",
  848. give_to_singleplayer = false,
  849. })
  850. -- Define the victory tokens.
  851. local nodebox = {
  852. type = "fixed",
  853. fixed = {
  854. {-0.5, -0.5, -0.5, 0.5, -15/32, 0.5},
  855. },
  856. }
  857. for j, k in ipairs({
  858. {name="copper", upper="Copper"},
  859. {name="silver", upper="Silver"},
  860. {name="gold", upper="Gold"},
  861. }) do
  862. minetest.register_node("survivalist:" .. k.name .. "_skill_token", {
  863. description = k.upper .. " Skill Mark",
  864. tiles = {"survivalist_" .. k.name .. "_token.png"},
  865. inventory_image = "survivalist_" .. k.name .. "_token.png",
  866. wield_image = "survivalist_" .. k.name .. "_token.png",
  867. -- If stack_max is clamped, then players can lose marks they win due to
  868. -- stack clamping, when marks are placed in inventory or dropped on the ground.
  869. --stack_max = 1,
  870. paramtype = "light",
  871. paramtype2 = "facedir",
  872. sunlight_propagates = true,
  873. walkable = false,
  874. groups = utility.dig_groups("item"),
  875. sounds = default.node_sound_metal_defaults(),
  876. drawtype = "nodebox",
  877. node_box = nodebox,
  878. selection_box = nodebox,
  879. on_place = minetest.rotate_node,
  880. })
  881. end
  882. -- Compatibility alias.
  883. minetest.register_alias("survivalist:skill_token", "survivalist:silver_skill_token")
  884. -- GUI input handler.
  885. minetest.register_on_player_receive_fields(function(...)
  886. return survivalist.on_receive_fields(...)
  887. end)
  888. -- Update state for players that join.
  889. minetest.register_on_joinplayer(function(...)
  890. return survivalist.on_joinplayer(...)
  891. end)
  892. -- Dead players can't win anything.
  893. minetest.register_on_dieplayer(function(...)
  894. return survivalist.on_dieplayer(...)
  895. end)
  896. -- File is reloadable.
  897. local c = "survivalist:core"
  898. local f = survivalist.modpath .. "/init.lua"
  899. reload.register_file(c, f, false)
  900. -- Done.
  901. survivalist.run_once = true
  902. end