1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
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
|