Last active
January 11, 2018 20:47
-
-
Save mkarneim/25d7f1b552e7eb1571e11b27cb8bdf88 to your computer and use it in GitHub Desktop.
Wizards of Lua - Zones
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "mickkay.wol.Vec3" | |
| local pkg = {} | |
| local diamond = { | |
| "diamond_sword", "diamond_axe", "diamond_pickaxe", "diamond_shovel" | |
| } | |
| local iron = { | |
| "iron_sword", "iron_axe", "iron_pickaxe", "iron_shovel" | |
| } | |
| local stone = { | |
| "stone_sword", "stone_axe", "stone_pickaxe", "stone_shovel" | |
| } | |
| local wood = { | |
| "wooden_sword", "wooden_axe", "wooden_pickaxe", "wooden_shovel" | |
| } | |
| local zones = { | |
| diamond, | |
| iron, | |
| stone, | |
| wood, | |
| } | |
| local width = 2000 | |
| local center = Vec3(-149, 113, 476) | |
| local function log(message) | |
| spell:execute("say "..message) | |
| end | |
| local function strStarts( str, start) | |
| local len = string.len(start) | |
| local sub = string.sub(str,1,len) | |
| return sub == start | |
| end | |
| local function clearInventory( p, items) | |
| for _,item in pairs(items) do | |
| local cmd = string.format("clear %s %s", p.name, item) | |
| spell:execute(cmd) | |
| sleep(1) | |
| end | |
| end | |
| local function randomZonePos() | |
| local r = width * (#zones+1) | |
| local a = math.random() * 2 * math.pi | |
| local z = math.sin(a) * r | |
| local x = math.cos(a) * r | |
| return Vec3(center.x + x + 0.5, 0, center.z + z + 0.5) | |
| end | |
| local function handlePlayer( p) | |
| local offset = center - p.pos | |
| local dist = offset:magnitude() | |
| local zoneIndex = math.min(#zones, math.floor(dist/width)) | |
| for i=1,zoneIndex do | |
| --sleep(1) | |
| clearInventory(p, zones[i]) | |
| end | |
| end | |
| function pkg.zones() | |
| spell:singleton("mickkay.zones") | |
| while true do | |
| local players = Entities.find("@a") | |
| --local players = Entities.find("@a[name=mickkay]") | |
| for _,p in pairs(players) do | |
| --if p.nbt.gameTypePlayer == 0 or p.nbt.gameTypePlayer == 2 then | |
| local start = Time.luatime; | |
| handlePlayer(p) | |
| --print("handlePlayer.duration", Time.luatime-start) | |
| sleep(1) | |
| --end | |
| end | |
| end | |
| end | |
| function pkg.zoneGiveStartItem() | |
| local zonePos = randomZonePos():floor() | |
| local swingArmAction = string.format("/lua include('mickkay'); mickkay.zoneStart(spell.owner, Vec3(%s,%s,%s))", zonePos.x, zonePos.y, zonePos.z) | |
| local replaceItemCmd = string.format("/replaceitem entity @p slot.hotbar.8 minecraft:compass 8 0 {OnSwingArmEvent:\"%s\"}", swingArmAction) | |
| spell:execute(replaceItemCmd) | |
| end | |
| local function updateHighscore( player, duration, distance) | |
| local seconds = duration / 1000 | |
| local avg = distance / seconds | |
| local message = string.format("%s's Score: %.2f m in %.2f seconds -> %.2f m/s", player.name, distance, seconds, avg) | |
| log(message) | |
| end | |
| local function deleteTags( player, key) | |
| local tags = player.tags | |
| for _,tag in pairs(tags) do | |
| if strStarts(tag,key) then | |
| player:removeTag(tag) | |
| end | |
| end | |
| end | |
| local function startStats( player) | |
| deleteTags(player, "zone:started") | |
| deleteTags(player, "zone:dist") | |
| local startTime = Time.realtime | |
| local tagTime = "zone:started="..startTime | |
| player:addTag(tagTime) | |
| local dist = (center - player.pos):magnitude() | |
| local tagDist = "zone:dist="..dist | |
| player:addTag(tagDist) | |
| end | |
| local function getTagValue( player, key) | |
| for _,tag in pairs(player.tags) do | |
| if strStarts(tag,key) then | |
| local idx = string.find( tag, "=") | |
| local result = string.sub(tag, idx+1) | |
| return result | |
| end | |
| end | |
| return nil | |
| end | |
| local function finishStats( player) | |
| local endTime = Time.realtime | |
| local tagTime = getTagValue( player, "zone:started") | |
| local startTime = tonumber(tagTime) | |
| local duration = endTime - startTime | |
| local tagDist = getTagValue( player, "zone:dist") | |
| local dist = tonumber(tagDist) | |
| updateHighscore( player, duration, dist) | |
| end | |
| function pkg.zoneStart( player, targetPos) | |
| spell:execute('playsound minecraft:item.firecharge.use master @p[r==10] ~ ~ ~ 1') | |
| spell:execute('clear %s',player.name) | |
| for y=255,0,-1 do | |
| spell.pos = targetPos+Vec3(0,y,0) | |
| if spell.block.material.solid or spell.block.material.liquid then | |
| break | |
| end | |
| end | |
| player.pos = spell.pos + Vec3(0,2,0) | |
| player:addTag("zone-player") | |
| startStats( player) | |
| sleep(20) | |
| spell:execute('give %s clock', player.name) | |
| spell:execute('give %s compass', player.name) | |
| spell:execute('spawnpoint %s %s %s %s', player.name, center.x, center.y, center.z) | |
| end | |
| local function isZonePlayer(player) | |
| local tags = player.tags | |
| for _,tag in pairs(tags) do | |
| if tag == "zone-player" then | |
| return true | |
| end | |
| end | |
| return false | |
| end | |
| local function enterLobby( player) | |
| player.pos = center | |
| end | |
| local function finish( player) | |
| spell:execute("/msg %s Congratulations!", player.name) | |
| player:giveHead() | |
| player:removeTag("zone-player") | |
| finishStats( player) | |
| end | |
| function pkg.zoneEntrance( player) | |
| if isZonePlayer(player) then | |
| finish( player) | |
| else | |
| enterLobby( player) | |
| end | |
| end | |
| return pkg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment