init.lua 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. survivalist = survivalist or {}
  2. survivalist.modpath = minetest.get_modpath("survivalist")
  3. survivalist.players = survivalist.players or {}
  4. survivalist.groups = survivalist.groups or {}
  5. -- Positions of surface & nether cities.
  6. local surfacecitypos = {x=0, y=-8, z=0}
  7. local nethercitypos = {x=0, y=-30793, z=0}
  8. -- Valid gamemodes are 'surface', 'cave', & 'nether'.
  9. -- The game rules description. Shown in formspec.
  10. survivalist.gamerules =
  11. "===| Survival Challenge Description |===\n\n" ..
  12. "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" ..
  13. "The Survival Challenge focuses on survival in the wild far from developed regions. You can play it solo or with other players.\n\n" ..
  14. "You will need to scroll this text in order to read all of it.\n\n" ..
  15. "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" ..
  16. "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" ..
  17. "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" ..
  18. "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" ..
  19. "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" ..
  20. "(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" ..
  21. "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" ..
  22. "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" ..
  23. "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" ..
  24. "The following is a basic description of each challenge.\n\n" ..
  25. "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" ..
  26. "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" ..
  27. "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" ..
  28. "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" ..
  29. "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" ..
  30. "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" ..
  31. "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" ..
  32. "===| Challenge Description End |==="
  33. -- Add player to a list of players contained in the database.
  34. -- This is because we can't iterate over the database entries directly,
  35. -- so we need a way to find all players who have ranking entries.
  36. function survivalist.add_player_to_rankings(pname)
  37. -- Obtain the database in string form.
  38. local data = survivalist.modstorage:get_string(":ranked_players:")
  39. -- If the string is empty, create a dummy table for deserialization.
  40. if data == "" then
  41. data = "return {}"
  42. end
  43. -- Deserialize the rankings database into a table.
  44. local tb = minetest.deserialize(data)
  45. -- Add player to table.
  46. tb[pname] = true
  47. -- Reserialize the database table.
  48. local serialized = minetest.serialize(tb)
  49. -- Don't overwrite the old database unless the table serialized successfully.
  50. if type(serialized) == "string" then
  51. survivalist.modstorage:set_string(":ranked_players:", serialized)
  52. end
  53. end
  54. function survivalist.get_ranking_entries()
  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. -- Return the table.
  64. return tb or {}
  65. end
  66. function survivalist.inventory_empty(inv, name)
  67. local count = 0
  68. for i = 1, inv:get_size(name) do
  69. local stack = inv:get_stack(name, i)
  70. if not passport.is_passport(stack:get_name()) then
  71. if stack:get_count() > 0 then
  72. count = count + stack:get_count()
  73. end
  74. end
  75. end
  76. return (count == 0)
  77. end
  78. function survivalist.check_inventories_empty(pname)
  79. local player = minetest.get_player_by_name(pname)
  80. if not player then
  81. return
  82. end
  83. local inv = player:get_inventory()
  84. if survivalist.inventory_empty(inv, "main") and
  85. survivalist.inventory_empty(inv, "craft") and
  86. survivalist.inventory_empty(inv, "armor") and
  87. survivalist.inventory_empty(inv, "voidchest:voidchest") and
  88. survivalist.inventory_empty(inv, "craftresult") and
  89. survivalist.inventory_empty(inv, "craftpreview") and
  90. survivalist.inventory_empty(inv, "bag1contents") and
  91. survivalist.inventory_empty(inv, "bag2contents") and
  92. survivalist.inventory_empty(inv, "bag3contents") and
  93. survivalist.inventory_empty(inv, "bag4contents") and
  94. survivalist.inventory_empty(inv, "xchest") and
  95. survivalist.inventory_empty(inv, "bag1") and
  96. survivalist.inventory_empty(inv, "bag2") and
  97. survivalist.inventory_empty(inv, "bag3") and
  98. survivalist.inventory_empty(inv, "bag4") then
  99. return true
  100. end
  101. end
  102. -- Loot parameters & stuff.
  103. dofile(survivalist.modpath .. "/loot.lua")
  104. -- Actually teleport the player to the start location and announce in chat.
  105. -- Also, record that the game has begun.
  106. function survivalist.teleport_and_announce(pname, pos, gamemode)
  107. local player = minetest.get_player_by_name(pname)
  108. if not player then
  109. return
  110. end
  111. -- Player's inventories must be empty.
  112. if not survivalist.check_inventories_empty(pname) then
  113. 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.")
  114. easyvend.sound_error(pname)
  115. return
  116. end
  117. -- Abort if player is trying to cheat by sitting in a cart. >:)
  118. if default.player_attached[pname] then
  119. minetest.chat_send_player(pname, "# Server: Transport error. Player attached!")
  120. return
  121. end
  122. -- Record home position.
  123. local homepos = vector.round(player:get_pos())
  124. -- Teleport player.
  125. wield3d.on_teleport()
  126. player:set_pos(vector.add(pos, {x=math.random(-3, 3), y=0.5, z=math.random(-3, 3)}))
  127. -- Make sure player is healthy.
  128. heal.heal_health_and_hunger(pname)
  129. -- Remove posibility of cheating via netherportals.
  130. flameportal.clear_return_location(pname)
  131. -- Clear bed respawn position. Player must make new bed to survive.
  132. beds.clear_player_spawn(pname)
  133. -- Give the game name some interesting names.
  134. local gamestring = "Void"
  135. if gamemode == "surface" then
  136. gamestring = "Iceworld"
  137. elseif gamemode == "cave" then
  138. gamestring = "Underearth"
  139. elseif gamemode == "nether" then
  140. gamestring = "Netherealm"
  141. end
  142. -- Inform player the game has begun.
  143. if not gdac.player_is_admin(pname) then
  144. local dname = rename.gpn(pname)
  145. minetest.chat_send_all("# Server: Player <" .. dname .. "> has begun a test of skill in the " .. gamestring .. " at " .. rc.pos_to_namestr(vector.round(pos)) .. "!")
  146. else
  147. minetest.chat_send_player(pname, "# Server: You have begun a test of skill in the " .. gamestring .. " at " .. rc.pos_to_namestr(vector.round(pos)) .. "!")
  148. end
  149. survivalist.shout_player_stats(pname)
  150. 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.")
  151. -- Give player the starting items.
  152. give_initial_stuff.give(player)
  153. -- Record the player's new gamemode.
  154. survivalist.modstorage:set_string(pname .. ":mode", gamemode)
  155. -- Record the player's starting position.
  156. survivalist.modstorage:set_string(pname .. ":pos", minetest.pos_to_string(vector.round(pos)))
  157. -- Record the player's home position. Used when canceling a Challenge.
  158. survivalist.modstorage:set_string(pname .. ":home", minetest.pos_to_string(homepos))
  159. -- Record that this player is accepting groups.
  160. survivalist.groups[pname] = survivalist.groups[pname] or {count=0}
  161. survivalist.groups[pname].count = survivalist.groups[pname].count + 1
  162. minetest.after(60*5, function()
  163. if survivalist.groups[pname] then
  164. survivalist.groups[pname].count = survivalist.groups[pname].count - 1
  165. if survivalist.groups[pname].count <= 0 then
  166. survivalist.groups[pname] = nil
  167. end
  168. end
  169. end)
  170. end
  171. -- This function must find a location for the player and teleport them there.
  172. -- Also create a starting dungeon (must not destroy protected stuff) and a chest with extra resources.
  173. function survivalist.prepare_dungeon(pname, pos, gamemode)
  174. -- Positions to load. Need a larger area in order to make sure any protections are discovered.
  175. local minp = vector.add(pos, vector.new(-64, -64, -64))
  176. local maxp = vector.add(pos, vector.new(64, 64, 64))
  177. -- Dungeon coordinates.
  178. local dminp = vector.add(pos, vector.new(-4, 0, -4))
  179. local dmaxp = vector.add(pos, vector.new(4, 4, 4))
  180. -- Copy the position table so it doesn't get modified.
  181. local pos2 = table.copy(pos)
  182. -- Build callback function. When the map is loaded, we need to check protections and create the dungeon.
  183. local tbparam = {}
  184. local cb = function(blockpos, action, calls_remaining, param)
  185. if action == core.EMERGE_CANCELLED or action == core.EMERGE_ERRORED then
  186. minetest.chat_send_player(pname, "# Server: Internal error, try again or report.")
  187. easyvend.sound_error(pname)
  188. return
  189. end
  190. -- We don't do anything until the last callback.
  191. if calls_remaining ~= 0 then
  192. return
  193. end
  194. -- Check for protections, and if there are none, create a dungeon.
  195. for x = dminp.x, dmaxp.x, 1 do
  196. for y = dminp.y, dmaxp.y, 1 do
  197. for z = dminp.z, dmaxp.z, 1 do
  198. if minetest.test_protection({x=x, y=y, z=z}, "") then
  199. -- Return failure if target is protected. This shouldn't happen often.
  200. minetest.chat_send_player(pname, "# Server: Error: did not succeed in finding a suitable start location! If this happens, just try again.")
  201. easyvend.sound_error(pname)
  202. return
  203. end
  204. end
  205. end
  206. end
  207. -- Check if spawning in air.
  208. if minetest.get_node(pos2).name == "air" then
  209. minetest.chat_send_player(pname, "# Server: Error: did not succeed in finding a suitable start location! If this happens, just try again.")
  210. easyvend.sound_error(pname)
  211. return
  212. end
  213. -- No protections? Create the dungeon.
  214. -- Generate dungeon for the player to spawn in.
  215. local path = survivalist.modpath .. "/schematics/survivalist_" .. gamemode .. "_dungeon.mts"
  216. minetest.place_schematic(vector.add(pos2, {x=-4, y=0, z=-4}), path, "random", nil, true)
  217. -- Choose a location for the chest.
  218. local chestpos = vector.add(pos2, vector.new(math.random(-3, 3), 1, math.random(-3, 3)))
  219. chestpos = vector.round(chestpos)
  220. -- Create chest with stuff.
  221. minetest.set_node(chestpos, {
  222. name = "morechests:goldchest_public_closed",
  223. param2 = math.random(0, 3),
  224. })
  225. local meta = minetest.get_meta(chestpos)
  226. local inv = meta:get_inventory()
  227. if inv then
  228. survivalist.fill_loot_chest(inv, gamemode)
  229. end
  230. -- Teleport player and announce.
  231. survivalist.teleport_and_announce(pname, pos2, gamemode)
  232. end
  233. -- Emerge the target area. Once emergence is complete the dungeon will spawn.
  234. minetest.chat_send_player(pname, "# Server: Checking reliability of target location ... please stand by, this can take several seconds.")
  235. minetest.emerge_area(minp, maxp, cb, tbparam)
  236. end
  237. function survivalist.shout_player_stats(pname)
  238. local ms = survivalist.modstorage
  239. local wins_total = pname .. ":wins_total"
  240. local wins_streak = pname .. ":wins_streak"
  241. local wins_surface = pname .. ":wins_surface"
  242. local wins_cave = pname .. ":wins_cave"
  243. local wins_nether = pname .. ":wins_nether"
  244. local wins_fail = pname .. ":wins_fail"
  245. local wins_bstreak = pname .. ":wins_bstreak"
  246. local wins_tokens = pname .. ":wins_tokens"
  247. if not gdac.player_is_admin(pname) then
  248. local dname = rename.gpn(pname)
  249. minetest.chat_send_all("# Server: Survivalist stats for <" ..
  250. dname .. ">: Victories: " .. ms:get_int(wins_total) ..
  251. ". Deaths: " .. ms:get_int(wins_fail) ..
  252. ". Iceworld: " .. ms:get_int(wins_surface) ..
  253. ". Underearth: " .. ms:get_int(wins_cave) ..
  254. ". Netherealm: " .. ms:get_int(wins_nether) ..
  255. ". C-Streak: " .. ms:get_int(wins_streak) ..
  256. ". B-Streak: " .. ms:get_int(wins_bstreak) ..
  257. ". Marks: " .. ms:get_int(wins_tokens) ..
  258. ".")
  259. end
  260. end
  261. function survivalist.game_in_progress(pname)
  262. local cg = survivalist.modstorage:get_string(pname .. ":mode")
  263. if cg == "surface" or cg == "cave" or cg == "nether" then
  264. return true
  265. end
  266. end
  267. -- This function is called when the player wants to begin the game.
  268. -- It must handle all game validation.
  269. function survivalist.start_game(pname, gamemode)
  270. -- Get player and make sure he exists.
  271. local player = minetest.get_player_by_name(pname)
  272. if not player then
  273. return
  274. end
  275. -- Is a game already running?
  276. local currentgame = survivalist.modstorage:get_string(pname .. ":mode")
  277. if currentgame == "surface" or currentgame == "cave" or currentgame == "nether" then
  278. minetest.chat_send_player(pname, "# Server: You are already engaged in a Survivalist Challenge, you cannot start a concurrent game.")
  279. easyvend.sound_error(pname)
  280. return
  281. end
  282. local pp = player:get_pos()
  283. if pp.y < -100 or pp.y > 1000 then
  284. minetest.chat_send_player(pname, "# Server: You need to be on the surface of the Overworld to start a challenge.")
  285. easyvend.sound_error(pname)
  286. return
  287. end
  288. -- Validate the gamemode.
  289. if type(gamemode) ~= "string" then
  290. minetest.chat_send_player(pname, "# Server: No starting option selected!")
  291. easyvend.sound_error(pname)
  292. return
  293. end
  294. if gamemode ~= "surface" and gamemode ~= "cave" and gamemode ~= "nether" then
  295. minetest.chat_send_player(pname, "# Server: You must choose a valid starting option!")
  296. easyvend.sound_error(pname)
  297. return
  298. end
  299. -- Player's inventories must be empty.
  300. if not survivalist.check_inventories_empty(pname) then
  301. 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.")
  302. easyvend.sound_error(pname)
  303. return
  304. end
  305. local pos = {x=0, y=0, z=0}
  306. local group = false
  307. -- Group survival: check if the player should be grouped with someone else.
  308. local players = minetest.get_connected_players()
  309. for k, v in ipairs(players) do
  310. local oname = v:get_player_name()
  311. local omode = survivalist.modstorage:get_string(oname .. ":mode")
  312. -- Gamemodes must match.
  313. if omode == gamemode then
  314. local opos = minetest.string_to_pos(survivalist.modstorage:get_string(oname .. ":home"))
  315. -- Home position must have been recorded.
  316. if opos then
  317. -- We must be close enough to the other player's home pos, in order to be grouped with them.
  318. if vector.distance(opos, player:get_pos()) <= 5 then
  319. -- The other player must currently be accepting groups.
  320. -- Groups are not saved across restarts; this means that a player cannot
  321. -- team with another after the server restarts, even if less time than
  322. -- the time limit has gone by.
  323. if survivalist.groups[oname] then
  324. -- Get location of the other player's dungeon.
  325. local p2 = minetest.string_to_pos(survivalist.modstorage:get_string(oname .. ":pos"))
  326. if p2 then
  327. pos = p2
  328. group = true
  329. break
  330. end
  331. end
  332. end
  333. end
  334. end
  335. end
  336. if not group then
  337. -- Find a random position on the X,Z plane.
  338. while vector.distance(pos, surfacecitypos) < 10000 or vector.distance(pos, nethercitypos) < 10000 do
  339. for j, k in ipairs({"x", "z"}) do
  340. pos[k] = math.random(-30000, 30000)
  341. end
  342. -- Gamemode determines depth.
  343. if gamemode == "surface" then
  344. pos.y = -10
  345. elseif gamemode == "cave" then
  346. pos.y = math.random(-24000, -3000)
  347. elseif gamemode == "nether" then
  348. pos.y = math.random(-30860, -30810)
  349. end
  350. end
  351. end
  352. -- Prepare target location. The remaining logic is executed from a callback.
  353. if group then
  354. -- The dungeon has already been generated.
  355. survivalist.teleport_and_announce(pname, pos, gamemode)
  356. else
  357. -- Need to generate a dungeon, then teleport the player.
  358. survivalist.prepare_dungeon(pname, pos, gamemode)
  359. end
  360. end
  361. -- Checks if a player is in range to claim victory.
  362. -- Performs no other validation; this is a simple distance check.
  363. function survivalist.player_in_victory_range(pname)
  364. local player = minetest.get_player_by_name(pname)
  365. if not player then
  366. return
  367. end
  368. local pos = player:get_pos()
  369. if vector.distance(pos, surfacecitypos) <= 20 then
  370. return true
  371. elseif vector.distance(pos, nethercitypos) <= 20 then
  372. return true
  373. end
  374. end
  375. -- Called when the player wants to claim victory. Must validate.
  376. function survivalist.attempt_claim(pname)
  377. local player = minetest.get_player_by_name(pname)
  378. if not player then
  379. return
  380. end
  381. -- Make sure a game is actually running.
  382. local currentgame = survivalist.modstorage:get_string(pname .. ":mode")
  383. if currentgame ~= "surface" and currentgame ~= "cave" and currentgame ~= "nether" then
  384. minetest.chat_send_player(pname, "# Server: You are not currently engaged in a Survivalist Challenge.")
  385. easyvend.sound_error(pname)
  386. return
  387. end
  388. -- Check if the player is in the city.
  389. local pos = player:get_pos()
  390. local cityname = ""
  391. -- The position and name of the city the player claims victory in.
  392. local finalcitypos
  393. if vector.distance(pos, surfacecitypos) <= 20 then
  394. finalcitypos = table.copy(surfacecitypos)
  395. cityname = "Surface Colony"
  396. elseif vector.distance(pos, nethercitypos) <= 20 then
  397. finalcitypos = table.copy(nethercitypos)
  398. cityname = "Nether City"
  399. end
  400. if not finalcitypos then
  401. minetest.chat_send_player(pname, "# Server: You must be within 20 meters of the main square of the Surface Colony or the Nether City in order to claim victory!")
  402. easyvend.sound_error(pname)
  403. return
  404. end
  405. -- What rank (copper, silver, gold) has the player earned?
  406. local ranks = {
  407. ["surface"] = {rank="copper", upper="Copper"},
  408. ["cave"] = {rank="silver", upper="Silver"},
  409. ["nether"] = {rank="gold", upper="Gold"},
  410. }
  411. local rank = ranks[currentgame].rank
  412. local upperank = ranks[currentgame].upper
  413. -- Reward the player.
  414. local tokencount = 1
  415. local startpos = minetest.string_to_pos(survivalist.modstorage:get_string(pname .. ":pos"))
  416. -- If the starting position couldn't be parsed we'll just give the player 1 token.
  417. if startpos then
  418. local dist = vector.distance(finalcitypos, startpos)
  419. -- Discount the minimum distance.
  420. dist = dist - 10000
  421. -- Get distance in kilometers.
  422. -- One skill mark per extra kilometer over 10k.
  423. dist = math.floor(dist / 1000)
  424. -- Clamp, just in case.
  425. if dist < 1 then
  426. dist = 1
  427. end
  428. tokencount = dist
  429. end
  430. local dname = rename.gpn(pname)
  431. if not gdac.player_is_admin(pname) then
  432. minetest.chat_send_all("# Server: Player <" .. dname .. "> has claimed victory in the " .. cityname .. "!")
  433. minetest.chat_send_all("# Server: Player <" .. dname .. ">'s skill has been tested in a Survival Challenge and proved worthy.")
  434. minetest.chat_send_all("# Server: Player <" .. dname .. "> has earned " .. tokencount .. " " .. upperank .. " Skill Mark(s).")
  435. else
  436. minetest.chat_send_player(pname, "# Server: You have won the Survival Challenge!")
  437. end
  438. local inv = player:get_inventory()
  439. local leftover = inv:add_item("main", ItemStack("survivalist:" .. rank .. "_skill_token " .. tokencount))
  440. -- No room in inventory? Drop 'em.
  441. if leftover:get_count() > 0 then
  442. minetest.item_drop(leftover, player, pos)
  443. if not gdac.player_is_admin(pname) then
  444. minetest.chat_send_all("# Server: Player <" .. dname .. ">'s Skill Mark was dropped on the ground!")
  445. end
  446. end
  447. 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!")
  448. -- Record that the challenge is over.
  449. survivalist.modstorage:set_string(pname .. ":mode", nil)
  450. survivalist.modstorage:set_string(pname .. ":pos", nil)
  451. survivalist.modstorage:set_string(pname .. ":home", nil)
  452. survivalist.players[pname].choice = nil
  453. -- Record total wins, win streaks, and win types.
  454. local ms = survivalist.modstorage
  455. local wins_total = pname .. ":wins_total"
  456. local wins_streak = pname .. ":wins_streak"
  457. local wins_bstreak = pname .. ":wins_bstreak"
  458. local wins_type = pname .. ":wins_" .. currentgame
  459. local wins_tokens = pname .. ":wins_tokens"
  460. ms:set_int(wins_total, ms:get_int(wins_total) + 1)
  461. ms:set_int(wins_streak, ms:get_int(wins_streak) + 1)
  462. ms:set_int(wins_type, ms:get_int(wins_type) + 1)
  463. ms:set_int(wins_tokens, ms:get_int(wins_tokens) + tokencount)
  464. -- If current streak is better than best streak, update best streak.
  465. -- Best streak is never erased.
  466. if ms:get_int(wins_streak) > ms:get_int(wins_bstreak) then
  467. ms:set_int(wins_bstreak, ms:get_int(wins_streak))
  468. end
  469. -- Grant player the big_hotbar priv.
  470. -- Rewarded by the 'surface' gamemode only.
  471. if currentgame == "surface" then
  472. if not minetest.check_player_privs(player, {big_hotbar=true}) then
  473. local privs = minetest.get_player_privs(pname)
  474. privs.big_hotbar = true
  475. minetest.set_player_privs(pname, privs)
  476. minetest.notify_authentication_modified(pname)
  477. player:hud_set_hotbar_image("gui_hotbar2.png")
  478. player:hud_set_hotbar_itemcount(16)
  479. end
  480. end
  481. -- Let everyone know the player's win stats.
  482. survivalist.shout_player_stats(pname)
  483. -- Add player's name to the rankings.
  484. survivalist.add_player_to_rankings(pname)
  485. end
  486. function survivalist.player_beat_cave_challenge(pname)
  487. local ms = survivalist.modstorage
  488. local va = ms:get_int(pname .. ":wins_cave") or 0
  489. if va > 0 then
  490. return true
  491. end
  492. return false
  493. end
  494. function survivalist.player_beat_nether_challenge(pname)
  495. local ms = survivalist.modstorage
  496. local va = ms:get_int(pname .. ":wins_nether") or 0
  497. if va > 0 then
  498. return true
  499. end
  500. return false
  501. end
  502. -- If a player joins the server and a Survivalist Challenge is running, inform them.
  503. function survivalist.on_joinplayer(player)
  504. local pname = player:get_player_name()
  505. survivalist.players[pname] = {}
  506. minetest.after(0.5, function()
  507. local player = minetest.get_player_by_name(pname)
  508. if not player then
  509. return
  510. end
  511. local gamemode = survivalist.modstorage:get_string(pname .. ":mode")
  512. if gamemode == "surface" or gamemode == "cave" or gamemode == "nether" then
  513. local dname = rename.gpn(pname)
  514. if not gdac.player_is_admin(pname) then
  515. minetest.chat_send_all("# Server: Player <" .. dname .. "> is engaged in a Survival Challenge.")
  516. end
  517. minetest.chat_send_player(pname, "# Server: You are in a Survival Challenge. Avoid death by sleeping!")
  518. 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.")
  519. end
  520. end)
  521. end
  522. -- Dying cancels the challenge.
  523. function survivalist.on_dieplayer(player)
  524. local pname = player:get_player_name()
  525. -- If player has a bed respawn set, then they don't fail the challenge.
  526. if beds.has_respawn_bed(pname) then
  527. if survivalist.game_in_progress(pname) then
  528. minetest.after(1, function()
  529. minetest.chat_send_player(pname, "# Server: You should respawn in your bed. Warning: if you die without a bed, you will fail the Challenge.")
  530. end)
  531. end
  532. return
  533. end
  534. local gamemode = survivalist.modstorage:get_string(pname .. ":mode")
  535. if gamemode == "surface" or gamemode == "cave" or gamemode == "nether" then
  536. -- Delay the chat messages slightly.
  537. minetest.after(1, function()
  538. if not gdac.player_is_admin(pname) then
  539. local dname = rename.gpn(pname)
  540. minetest.chat_send_all("# Server: Player <" .. dname .. ">'s survival skill was tested and found wanting!")
  541. end
  542. survivalist.shout_player_stats(pname)
  543. minetest.chat_send_player(pname, "# Server: You failed the Survivalist Challenge. No goodies for you!")
  544. end)
  545. -- No game in progress. Win streak is reset to 0.
  546. survivalist.modstorage:set_string(pname .. ":mode", nil)
  547. survivalist.modstorage:set_string(pname .. ":pos", nil)
  548. survivalist.modstorage:set_string(pname .. ":home", nil)
  549. survivalist.modstorage:set_int(pname .. ":wins_streak", 0)
  550. -- Count loss.
  551. local ms = survivalist.modstorage
  552. local ff = pname .. ":wins_fail"
  553. ms:set_int(ff, ms:get_int(ff) + 1)
  554. -- Add player's name to the rankings (deaths count too).
  555. survivalist.add_player_to_rankings(pname)
  556. end
  557. end
  558. -- Abort a running game without loss to player score.
  559. function survivalist.abort_game(pname)
  560. local gamemode = survivalist.modstorage:get_string(pname .. ":mode")
  561. if gamemode == "surface" or gamemode == "cave" or gamemode == "nether" then
  562. if not survivalist.check_inventories_empty(pname) then
  563. 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).")
  564. easyvend.sound_error(pname)
  565. return
  566. end
  567. minetest.chat_send_player(pname, "# Server: Challenge termination confirmed.")
  568. local target = rc.static_spawn("abyss")
  569. target = minetest.string_to_pos(survivalist.modstorage:get_string(pname .. ":home")) or target
  570. -- Pre-teleport callback function. We may need to abort to prevent cheating.
  571. local pcb = function()
  572. if not survivalist.check_inventories_empty(pname) then
  573. 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).")
  574. easyvend.sound_error(pname)
  575. -- Returning success means to abort the teleport.
  576. -- Only applicable to pre-teleport callbacks.
  577. return true
  578. end
  579. -- Abort if player is trying to cheat by sitting in a cart. >:)
  580. if default.player_attached[pname] then
  581. minetest.chat_send_player(pname, "# Server: Transport error. Player attached!")
  582. return true
  583. end
  584. end
  585. -- Don't clear the challenge gamestate unless teleport successful.
  586. local bcb = function()
  587. -- No game in progress.
  588. -- Do not touch scores.
  589. survivalist.modstorage:set_string(pname .. ":mode", nil)
  590. survivalist.modstorage:set_string(pname .. ":pos", nil)
  591. survivalist.modstorage:set_string(pname .. ":home", nil)
  592. flameportal.clear_return_location(pname)
  593. beds.clear_player_spawn(pname)
  594. if not gdac.player_is_admin(pname) then
  595. local dname = rename.gpn(pname)
  596. minetest.chat_send_all("# Server: Player <" .. dname .. "> canceled a Survival Challenge and went home.")
  597. else
  598. minetest.chat_send_player(pname, "# Server: You canceled a Survival Challenge and went home.")
  599. end
  600. end
  601. -- Teleport is forced.
  602. preload_tp.preload_and_teleport(pname, target, 32, pcb, bcb, nil, true)
  603. else
  604. minetest.chat_send_player(pname, "# Server: You are not in a Survival Challenge; cannot abort.")
  605. easyvend.sound_error(pname)
  606. end
  607. end
  608. -- Compost the GUI formspec.
  609. function survivalist.compose_formspec(pname)
  610. local modestring = ""
  611. local inchallenge = false
  612. local gamemode = survivalist.modstorage:get_string(pname .. ":mode")
  613. if gamemode == "surface" then
  614. modestring = " (You are currently in an Iceworld Challenge)"
  615. inchallenge = true
  616. elseif gamemode == "cave" then
  617. modestring = " (You are currently in an Underearth Challenge)"
  618. inchallenge = true
  619. elseif gamemode == "nether" then
  620. modestring = " (You are currently in a Netherealm Challenge)"
  621. inchallenge = true
  622. end
  623. local type_surface = "false"
  624. local type_cave = "false"
  625. local type_nether = "false"
  626. -- Choose which checkbox will be shown as selected.
  627. if survivalist.players[pname] and survivalist.players[pname].choice then
  628. local choice = survivalist.players[pname].choice
  629. if choice == "surface" then
  630. type_surface = "true"
  631. elseif choice == "cave" then
  632. type_cave = "true"
  633. elseif choice == "nether" then
  634. type_nether = "true"
  635. end
  636. end
  637. local formspec = ""
  638. formspec = formspec .. "size[8,7]" ..
  639. default.gui_bg ..
  640. default.gui_bg_img ..
  641. default.gui_slots ..
  642. "label[0,0;Survivalist Challenge" .. modestring .. "]" ..
  643. "textarea[0.3,0.5;8,4;rules;;" .. minetest.formspec_escape(survivalist.gamerules) .. "]"
  644. -- Show challenge choices only if no challenge is in progress.
  645. if not inchallenge then
  646. formspec = formspec .. "label[0,4;Choose Your Challenge!]" ..
  647. "checkbox[0,4.3;type_surface;Surface Survival (Copper Mark);" .. type_surface .. "]" ..
  648. "checkbox[0,4.8;type_cave;Cave Survival (Silver Mark);" .. type_cave .. "]" ..
  649. "checkbox[0,5.3;type_nether;Nether Survival (Gold Mark);" .. type_nether .. "]"
  650. end
  651. -- Choose between start/abort buttons.
  652. if inchallenge then
  653. formspec = formspec .. "button[0,6.2;2,1;abort;Go Home]"
  654. else
  655. formspec = formspec .. "button[0,6.2;3,1;start;Begin Challenge]"
  656. end
  657. -- Show `Claim Victory` only if a challenge is in progress.
  658. if inchallenge then
  659. formspec = formspec .. "button[2,6.2;2,1;victory;Claim Victory]"
  660. end
  661. formspec = formspec .. "button[4,6.2;2,1;show_rankings;Rankings]" ..
  662. "button[6,6.2;2,1;close;Close]"
  663. return formspec
  664. end
  665. function survivalist.compose_rankings_formspec(pname)
  666. local players = survivalist.get_ranking_entries()
  667. local data = {}
  668. local ms = survivalist.modstorage
  669. for k, v in pairs(players) do
  670. data[#data+1] = {
  671. name = k,
  672. wins = ms:get_int(k .. ":wins_total"),
  673. deaths = ms:get_int(k .. ":wins_fail"),
  674. streak = ms:get_int(k .. ":wins_streak"),
  675. bstreak = ms:get_int(k .. ":wins_bstreak"),
  676. surface = ms:get_int(k .. ":wins_surface"),
  677. cave = ms:get_int(k .. ":wins_cave"),
  678. nether = ms:get_int(k .. ":wins_nether"),
  679. tokens = ms:get_int(k .. ":wins_tokens"),
  680. }
  681. end
  682. local form = ""
  683. form = form .. "size[13,7]" ..
  684. default.gui_bg ..
  685. default.gui_bg_img ..
  686. default.gui_slots ..
  687. "label[0,0;Survival Challenge Rankings]" ..
  688. "button[11,6.2;2,1;close_rankings;Close]"
  689. form = form .. "tablecolumns[color;text;text;text;text;text;text;text;text;text;text;text;text;text;text;text;text;text]"
  690. form = form .. "table[0,0.5;12.8,5;rankings_table;"
  691. form = form .. "#00ffff,Username,|,Victories,|,Deaths,|,C-Streak,|,B-Streak,|,Iceworld,|,Underearth,|,Netherealm,|,Marks,"
  692. for k, v in ipairs(data) do
  693. form = form .. ",<" .. rename.gpn(v.name) .. ">,|," .. v.wins .. ",|," .. v.deaths .. ",|," ..
  694. v.streak .. ",|," .. v.bstreak .. ",|," .. v.surface .. ",|," .. v.cave ..
  695. ",|," .. v.nether .. ",|," .. v.tokens .. ","
  696. end
  697. form = string.gsub(form, ",$", "")
  698. form = form .. ";1]"
  699. return form
  700. end
  701. function survivalist.show_rankings_formspec(pname)
  702. local formspec = survivalist.compose_rankings_formspec(pname)
  703. minetest.show_formspec(pname, "survivalist:rankings", formspec)
  704. end
  705. -- API function (called from passport mod, for instance).
  706. function survivalist.show_formspec(pname)
  707. local formspec = survivalist.compose_formspec(pname)
  708. minetest.show_formspec(pname, "survivalist:survivalist", formspec)
  709. end
  710. -- GUI form input handler.
  711. function survivalist.on_receive_fields(player, formname, fields)
  712. local pname = player:get_player_name()
  713. if formname ~= "survivalist:survivalist" and formname ~= "survivalist:rankings" then
  714. return
  715. end
  716. if fields.quit then
  717. return true
  718. end
  719. if fields.show_rankings then
  720. survivalist.show_rankings_formspec(pname)
  721. return true
  722. end
  723. if fields.rankings_table then
  724. survivalist.show_rankings_formspec(pname)
  725. return true
  726. end
  727. if fields.close_rankings or formname == "survivalist:rankings" then
  728. survivalist.show_formspec(pname)
  729. return true
  730. end
  731. -- Start game.
  732. if fields.start then
  733. minetest.close_formspec(pname, "survivalist:survivalist")
  734. survivalist.start_game(pname, survivalist.players[pname].choice)
  735. return true
  736. end
  737. -- Claim victory.
  738. if fields.victory then
  739. minetest.close_formspec(pname, "survivalist:survivalist")
  740. survivalist.attempt_claim(pname)
  741. return true
  742. end
  743. -- Abort challenge.
  744. if fields.abort then
  745. minetest.close_formspec(pname, "survivalist:survivalist")
  746. survivalist.abort_game(pname)
  747. return true
  748. end
  749. -- Handle gamemode checkboxes.
  750. for j, k in ipairs({"surface", "cave", "nether"}) do
  751. if fields["type_" .. k] then
  752. if fields["type_" .. k] == "true" then
  753. survivalist.players[pname].choice = k
  754. else
  755. survivalist.players[pname].choice = nil
  756. end
  757. survivalist.show_formspec(pname)
  758. return true
  759. end
  760. end
  761. if fields.close then
  762. --minetest.close_formspec(pname, "survivalist:survivalist")
  763. passport.show_formspec(pname)
  764. return true
  765. end
  766. survivalist.show_formspec(pname)
  767. return true
  768. end
  769. -- Here belongs code which must run only once.
  770. if not survivalist.run_once then
  771. -- Obtain modstorage.
  772. survivalist.modstorage = minetest.get_mod_storage()
  773. minetest.register_privilege("big_hotbar", {
  774. description = "Player has double-wide hotbar for item wielding.",
  775. give_to_singleplayer = false,
  776. })
  777. -- Define the victory tokens.
  778. local nodebox = {
  779. type = "fixed",
  780. fixed = {
  781. {-0.5, -0.5, -0.5, 0.5, -15/32, 0.5},
  782. },
  783. }
  784. for j, k in ipairs({
  785. {name="copper", upper="Copper"},
  786. {name="silver", upper="Silver"},
  787. {name="gold", upper="Gold"},
  788. }) do
  789. minetest.register_node("survivalist:" .. k.name .. "_skill_token", {
  790. description = k.upper .. " Skill Mark",
  791. tiles = {"survivalist_" .. k.name .. "_token.png"},
  792. inventory_image = "survivalist_" .. k.name .. "_token.png",
  793. wield_image = "survivalist_" .. k.name .. "_token.png",
  794. -- If stack_max is clamped, then players can lose marks they win due to
  795. -- stack clamping, when marks are placed in inventory or dropped on the ground.
  796. --stack_max = 1,
  797. paramtype = "light",
  798. paramtype2 = "facedir",
  799. sunlight_propagates = true,
  800. walkable = false,
  801. groups = utility.dig_groups("item"),
  802. sounds = default.node_sound_metal_defaults(),
  803. drawtype = "nodebox",
  804. node_box = nodebox,
  805. selection_box = nodebox,
  806. on_place = minetest.rotate_node,
  807. })
  808. end
  809. -- Compatibility alias.
  810. minetest.register_alias("survivalist:skill_token", "survivalist:silver_skill_token")
  811. -- GUI input handler.
  812. minetest.register_on_player_receive_fields(function(...)
  813. return survivalist.on_receive_fields(...)
  814. end)
  815. -- Update state for players that join.
  816. minetest.register_on_joinplayer(function(...)
  817. return survivalist.on_joinplayer(...)
  818. end)
  819. -- Dead players can't win anything.
  820. minetest.register_on_dieplayer(function(...)
  821. return survivalist.on_dieplayer(...)
  822. end)
  823. -- File is reloadable.
  824. local c = "survivalist:core"
  825. local f = survivalist.modpath .. "/init.lua"
  826. reload.register_file(c, f, false)
  827. -- Done.
  828. survivalist.run_once = true
  829. end