local room = require("room") local world = require("world") local house = {} local direction_map = { north = {0,1,0}, south = {0,-1,0}, east = {1,0,0}, west = {-1,0,0}, up = {0,0,1}, down = {0,0,-1}, } local opposite = { north = "south", south = "north", east = "west", west = "east", up = "down", down = "up" } local house_base = { create_room = function(self,room_from,direction,roomname) print("create_room called with roomname",roomname) local from_loc = room_from.location local xs,ys,zs = string.match(from_loc,"(%d+)x(%d+)x(%d+)") local x,y,z = tonumber(xs),tonumber(ys),tonumber(zs) local vec = {x,y,z} local sdir = string.lower(direction) assertf(direction_map[sdir],"Could not find direction %s",sdir) local to = direction_map[sdir] for i = 1,3 do vec[i] = vec[i] + to[i] end local newroom_loc = table.concat(vec,"x") if newroom_loc == "1x1x1" then fail_hint("You cannot overwrite the origin room") return end print("Creating a room with name", roomname) local newroom = room.create_new(roomname,newroom_loc) print("Settings new room's location to", newroom.location,"newroom is",newroom.name) room_from.go[sdir] = newroom newroom.go[opposite[sdir]] = room_from self.rooms[newroom_loc] = newroom print("After creating room, house's rooms are ") for k,v in pairs(self.rooms) do if v ~= "exit" then print(k,v.name) print("can go") for i,j in pairs(v.go) do print("\t",i,j.name) end end end end, destroy_room = function(self,room_from,direction) local from_loc = room_from.location local xs,ys,zs = string.match(from_loc,"(%d+)x(%d+)x(%d+)") local x,y,z = tonumber(xs),tonumber(ys),tonumber(zs) local vec = {x,y,z} local sdir = string.lower(direction) assertf(direction_map[sdir],"Could not find direction %s",sdir) local to = direction_map[sdir] for i = 1,3 do vec[i] = vec[i] + to[i] end local roomloc = table.concat(vec,"x") if roomloc == "1x1x1" or roomloc == "1x0x1" then --Don't let them destory the origin room fail_hint("You cannot destroy the origin or exit rooms") return end for dirname,dirvec in pairs(direction_map) do local newvec = {vec[1],vec[2],vec[3]} for i = 1,3 do newvec[i] = newvec[i] + dirvec[i] end local dc_loc = table.concat(newvec,"x") if self.rooms[dc_loc] and self.rooms[dc_loc].go[opposite[dirname]] then self.rooms[dc_loc].go[opposite[dirname]] = nil end end self.rooms[roomloc] = nil end, create_doorway = function(self,room_from, direction) local from_loc = room_from.location local xs,ys,zs = string.match(from_loc,"(%d+)x(%d+)x(%d+)") local x,y,z = tonumber(xs),tonumber(ys),tonumber(zs) local vec = {x,y,z} local sdir = string.lower(direction) for i = 1,3 do vec[i] = vec[i] + direction_map[direction][i] end local otherroom = self.rooms[table.concat(vec,"x")] room_from.go[direction] = otherroom otherroom.go[opposite[direction]] = room_from end, has_room_in_direction = function(self,room_from,direction) direction = string.lower(direction) assertf(direction_map[direction],"Did not understand direction: %q",direction) local from_loc = room_from.location print("From location is:",from_loc) local xs,ys,zs = string.match(from_loc,"(%d+)x(%d+)x(%d+)") local x,y,z = tonumber(xs),tonumber(ys),tonumber(zs) local vec = {x,y,z} for i = 1,3 do vec[i] = vec[i] + direction_map[direction][i] end local sdir = string.lower(direction) local other_loc = table.concat(vec,"x") print("Other loc:",other_loc) return self.rooms[outher_loc] ~= nil end, } local house_m = {__index=house_base} function house.create_new(housename,from) local ret = {name = housename, rooms={}, ownerid = playerid, ["in"]=from} ret.rooms["1x1x1"] = room.create_new("Front yard","1x1x1") ret.rooms["1x1x1"].go["south"] = "exit" ret.rooms["1x0x1"] = "exit" setmetatable(house,house_m) return ret end function house.create_from_data(data) local ret = assert(loadstring(data,"loaded_house")) setmetatable(ret,house_m) return ret end house.meta = house_m return house