init.lua 43 KB

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