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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
local localplayer = {}
local world = require("world")
local net = require("net")
local save = require("save")
print("Doing local player")
local directions = {
"north","south","east","west","up","down"
}
local directions_index = {}
for k,v in pairs(directions) do
directions_index[v] = k
end
--print("In localplayer, world.locations are:",world.locations)
--for k,v in pairs(world.locations) do
--print(k,":",v)
--end
local lp_base = {
location = world.locations["City"],
has_settled = false,
go = function(self,dir)
dir = string.lower(dir)
if self.location.is_location then
local to
for k,v in pairs(self.location.go) do
if v[1] == dir then
to = v[2]()
break
end
end
self.location = to
cls()
out(self.location.goto_txt)
else --We're in a house/room
local toroom = self.location.go[dir]
--print("got to room:",toroom)
if toroom == "exit" then
print("special exit room")
self.location = self.in_house["in"]
self.in_house = nil
self.owns = false
cls()
out(self.location.goto_txt)
return
end
self.location = toroom
cls()
out(toroom:get_room_desc())
end
end,
goto_house = function(self,location,house)
print("going to ",name)
print("Got location:",location,"Got name:",house)
local house = net.get_house(location,house)
self.in_house = house
local init_room_name = "1x1x1"
local init_room = house.rooms[init_room_name]
local init_room_meta = getmetatable(init_room).__index
print("init_room_meta",init_room_meta)
local owner = net.do_i_own(location,house.name,self.id)
print("Looked at do_i_own:",owner)
if owner then
self.owns = true
else
self.owns = false
end
self.location = init_room
cls()
out(init_room:get_room_desc())
print("I'm in the inital room!",init_room)
end,
goto_location = function(self,loc)
self.in_house = nil
print("Setting location to",loc)
self.location = loc
cls()
out(self.location.goto_txt)
end,
--goto_location = function(self,name)
--assert(name ~= nil,"tried to go to a nil location")
--print("Self is",self,"name is",name)
--print("valid locations are:")
--for k,v in pairs(world.locations) do
--print(k,":",v)
--end
--assertf(world.locations[name],"The location %q does not exist.",name)
--cls()
--out(world.locations[name].goto_txt)
--localplayer.location = name
----out(world.locations[name].desc)
--end,
get_loc = function(self)
--Check if the player is in one of the locations
print("Trying to get location:",self.location)
print("World locations:",#world.locations)
for k,v in pairs(world.locations) do
print(k,":",v)
end
local loc
if world.locations[self.location] then
loc = world.locations[self.location]
end
print("Got location:",loc)
return loc
end,
}
local lp_m = {__index = lp_base}
local function load_save()
return require("savedata")
end
local success , data = pcall(load_save)
if success then
print("setting local player to saved data")
localplayer = data
setmetatable(localplayer,lp_m)
out(localplayer.location.desc)
if not localplayer.has_settled then
world.basic_commands[#world.basic_commands + 1] = "settle"
end
else
print("Creating new player")
out([[
Welcome to HTA, the Home Text Adventure. The text here describes where you
are or what your are doing. Use the text box at the bottom to enter commands.
You can submit commands by pressing the button to the right of the textbox,
or by pressing the Enter key.
Hints for what kinds of things you can type in are displayed right above the
text box. Try typing "look" in the command box.
You wake up in the City of Beginnings
]])
setmetatable(localplayer,lp_m)
localplayer.id = math.random()
localplayer.owns = false
localplayer.in_house = nil
world.basic_commands[#world.basic_commands + 1] = "settle"
set_hints(unpack(world.basic_commands))
local f = io.open("../data/savedata.lua","w")
f:write(save.table_to_string(localplayer))
f:close()
end
return localplayer
|