summaryrefslogtreecommitdiff
path: root/client/data/commands.lua
blob: d1328dac5e6b57d271f0bc5adc615ab8599e19f1 (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
local cmds = {}
local lp = require("localplayer")
local world = require("world")
local net = require("net")
local save = require("save")
local house = require("house")
function cmds.hints() end --A method to detour for palces to give hints

local dont_hint = false
cmds.can_look = function(...)
	return true --We can always look
end
cmds.look_hint = function(...)
	return {""} --We can always look
end
cmds.look = function(...)
	cls()
	if lp.location.is_location then
		out(lp.location:get_desc())
	else
		out(lp.location:get_room_desc())
	end
end

cmds.can_go = function(...)
	return true --We can always go
end
cmds.go = function(direction)
	local input = string.lower(direction)
	print("got input to go",input)
	set_hints(unpack(world.basic_commands))
	lp:go(direction)
	--local direction = 
end
cmds.go_hint = function(...)
	local loc = lp.location
	local directions = {}
	if loc.go then
		for k,v in pairs(loc.go) do
			directions[#directions + 1] = v[1]
		end
	end
	return directions
end

cmds.can_goto = function(...)
	return lp.in_house == nil
end
cmds.goto_hint = function(tbl)
	--print("Getting goto hint")
	--print("Got loc:", loc)
	local places = {}
	if lp.location.is_location then
		for k,v in pairs(lp.location.go) do
			places[#places + 1] = v[2]().name
		end
		if lp.location.last_houses then
			for k,v in pairs(lp.location.last_houses) do
				places[#places + 1] = v
			end
		end
	end
	return places
end
cmds.goto = function(...)
	local place_name = table.concat({...}," ")
	local pns = string.lower(place_name)
	print("Place name:",place_name)
	local loc = lp.location
	local found_place
	for k,v in pairs(lp.location.go) do
		if string.lower(v[2]().name) == pns then
			found_place = v[2]()
			print("Found place location!",found_place.name)
			break
		end
	end
	if found_place == nil then --We haven't found what they're looking for yet
		print("Did not find place name, looking further")
		if net.house_exists(loc.name,place_name) then
			print("Found place!",loc.name,place_name)
			local place = net.get_house(loc.name,place_name)
			print("Place was:",place)
			found_place = place
		end
	end
	if found_place == nil then
		print("Places:")
		for k,v in pairs(loc.go) do
			print(k,":",v)
		end
		fail_hint("Could not find a place:", place_name)
		return
	end
	print("found_place is location:",found_place.is_location)
	if found_place.is_location then
		print("Using lp.goto_location",found_place.name)
		lp:goto_location(found_place)
	elseif found_place ~= nil then
		print("Using lp:goto_house")
		lp:goto_house(loc.name,place_name)
	end
	print("Found place was",found_place)
	--lp:goto_location(found_place)
end

cmds.can_settle = function(...)
	return not lp.has_settled
end
cmds.settle = function(...)
	cls()
	out("Enter a name for your house:")
	set_hints("")
	dont_hint = true
	command_over = function(text)
		print("Got text:",text)
		if net.is_name_available(text, lp.location.name) then
			local nhouse = house.create_new(text)
			local house_data = save.table_to_string(nhouse)
			print("Made house:",house_data)
			net.claim_house(lp.location.name,text,lp.id)
			net.write_house(lp.id,house_data)
			command_over = nil
			clear_cmdbox()
			cls()
			out(lp.location.settle_txt)
			dont_hint=false
			lp:goto_house(text,false)
		else
			fail_hint("That name is already taken! Try another!")
		end
	end
	world.basic_commands[#world.basic_commands] = nil
end
cmds.settle_hint = function(...)
	if lp.has_settled then
		return {"You have already setteled!"}
	else
		return {"Once you settle an area, you cannot settle again!"}
	end
end

local valid_directions = {"north","south","east","west","up","down"}
local valid_directions_index = {}
for k,v in pairs(valid_directions) do valid_directions_index[v] = true end

cmds.create_room = function(direction, ...)
	local name = table.concat({...}," ")
	print("create_room called with", direction,name)
	if not direction and name then
		fail_hint("To create a room, give it a direction (north, east, up, ect.) and a name")
	end
	direction = string.lower(direction)
	if not lp.in_house then
		fail_hint("You must be in your house to create a room!")
		return
	end
	if not valid_directions_index[direction] then
		fail_hint("You can't make a room \"" .. direction .. "\". Use north, east, up, ect.")
		return
	end
	local cur_house = lp.in_house
	local cur_room = lp.location
	--print("cur_house is", cur_house)
	if cur_house:has_room_in_direction(cur_room,direction) then
		cls()
		out([[
There is already a room in this direction, would you like make a doorway to
this room instead? (yes/no)
]])
		dont_hint = true
		command_over = function(text)
			clear_cmdbox()
			if text == "yes" then
				cur_house:create_doorway(cur_room,direction)
				cls()
				out("You created a doorway")
				lp:go(direction)
			else
				cls()
				out("You don't do anything")
			end
			command_over = nil
			dont_hint = false

		end
	else
		cur_house:create_room(cur_room,direction,name)
		cls()
		out("You create a new room " .. direction)
		lp:go(direction)
	end

	--cur_house:create_room(cur_house,direction,name)
end
cmds.create_room_hint = function(...)
	return {"create_room <direction> <room_name>"}
end

cmds.edit_room = function(field)
	if field == "description" then
		enable_multibox(true)
		multibox:settext(lp.location.desc)
		--set_editbox_multi(true)
		multibox_donebut.onClick = function(self)
			print("over multibut click")
			local toset = multibox:gettext()
			print("Setting text to ", toset)
			lp.location.desc = toset
			cls()
			out(lp.location:get_room_desc())
			enable_multibox(false)
			--set_editbox_multi(false)
		end
	elseif field == "name" then
		cls()
		out("Enter the new room name")
		dont_hint = true
		command_over = function(text)
			print("Overrideing command")
			lp.location.name = text
			clear_cmdbox()
			cls()
			out("You set the room's name to " .. text)
			command_over = nil
			dont_hint = false
		end
	end

end
cmds.edit_room_hint = function(...)
	return {"edit_room (name|description)"}
end

cmds.create_item = function(...)
	local itemname = table.concat({...}," ")
	dont_hint = true
	cls()
	out("Describe the location of the item in the room")
	command_over = function(text)
		cls()
		out("Enter a description for this item")
		local item_loc = text
		enable_multibox(true)
		multibox_donebut.onClick = function(self)
			local description = multibox:gettext()
			print("Creating an item in", lp.location.name)
			lp.location:add_item(itemname,description,item_loc)
			cls()
			out(lp.location:get_room_desc())
			enable_multibox(false)
		end
		clear_cmdbox()
		command_over = nil
		dont_hint = false
	end
end

cmds.create_item_hint = function(...)
	return {"create_item <item_name>"}
end

cmds.edit_item = function(field,...)
	local itemname = table.concat({...}," ")
	local this_item
	if lp.location.items == nil then
		fail_hint("You can only edit items in a house")
		return
	end
	for itemnum,item in pairs(lp.location.items) do
		if item.name == itemname then
			this_item = item
			break
		end
	end
	if this_item == nil then
		fail_hint("Could not find an item named " .. itemname)
		return
	end
	if field == "name" then
		cls()
		out("Enter a new name")
		dont_hint = true
		command_over = function(text)
			this_item.name = text
			command_over = nil
			clear_cmdbox()
			out(lp.location:get_room_desc())
			dont_hint = false
		end
	elseif field == "description" then
		cls()
		enable_multibox(true)
		multibox:settext(this_item.desc)
		multibox_donebut.onClick = function(self)
			local newdesc = multibox:gettext()
			this_item.desc = newdesc
			cls()
			out(lp.location:get_room_desc())
			enable_multibox(false)
		end
	elseif field == "location" then
		cls()
		out("Enter a new location")
		dont_hint = true
		command_over = function(text)
			this_item.location = text
			command_over = nil
			clear_cmdbox()
			out(lp.location:get_room_desc())
			dont_hint = false
		end
		
	else
		fail_hint("Could not edit an item's \"" .. field .. "\".")
	end
end

cmds.edit_item_hint = function(...)
	return {"edit_item (name|description|location) item_name"}
end

cmds.examine = function(...)
	local item_name = table.concat({...}," ")
	if lp.location.items then
		local item_found = false
		cls()
		for k,item in pairs(lp.location.items) do
			if item.name == item_name then
				out(item.desc)
				item_found = true
			end
		end
		if not item_found then
			fail_hint("You couldn't find a " .. item_name .. " to examine")
		end
	else
		fail_hint("You couldn't find anything to examine")
	end
end

cmds.examine_hint = function(...)
	local itemlist = {}
	for k,item in pairs(lp.location.items) do
		itemlist[#itemlist + 1] = item.name
	end
	return itemlist
end

cmds.save = function(...)
	print("in_house:",lp.in_house)
	print("owns:",lp.owns)
	if (not lp.in_house) or (not lp.owns) then
		fail_hint("You can only save when you're in your house.")
		return
	end
	--lp.in_house["in"] = nil
	local housedata = save.table_to_string(lp.in_house)
	net.write_house(lp.id,housedata)
	cls()
	out("House saved!")
	print("House data is", housedata)
end
cmds.save_hint = function(...)
	return {"Save your house!"}
end

cmds.destroy_room = function(direction)
	direction = string.lower(direction)
	if not lp.in_house or not lp.owns then
		fail_hint("You can only destory rooms in your own house.")
		return
	end
	lp.in_house:destroy_room(lp.location,direction)
	cls()
	out(lp.location:get_room_desc())
	print("Room destroyed!")
end
cmds.destroy_room_hint = function(...)
	return {"destroy_room <direction>"}
end
add_cmdbox_hook(function(cmdbox)
	local text = cmdbox:gettext()
	if text == "go south" and (not lp.location.is_location) and (lp.location.south == "exit") and (lp.owns) then
		set_hints("Don't forget to save before exiting your house!")
	end
end)


add_cmdbox_hook(function(cmdbox)
	if dont_hint then return end
	local text = cmdbox:gettext()
	if string.find(text,"^[^%s]*$") then --Does not have a root command yet
		local cmd_set = {}
		for k,v in pairs(world.basic_commands) do
			cmd_set[k] = v
		end
		if lp.owns and lp.in_house then
			print("We own this place, adding extra commands")
			cmd_set[#cmd_set + 1] = "create_room"
			cmd_set[#cmd_set + 1] = "edit_room"
			cmd_set[#cmd_set + 1] = "destroy_room"

			cmd_set[#cmd_set + 1] = "create_item"
			cmd_set[#cmd_set + 1] = "edit_item"
			cmd_set[#cmd_set + 1] = "destroy_item"

			cmd_set[#cmd_set + 1] = "save"
		end
		if (not lp.location.is_location) and (lp.location.items) then
			cmd_set[#cmd_set + 1] = "examine"
		else
			cmd_set[#cmd_set + 1] = "goto"
		end
		set_hints(unpack(cmd_set))
	else
		local root = string.match(text,"^([^%s]+)%s.+")
		print("root is:")
		if cmds[root] then
			local hints = cmds[root .. "_hint"]()
			assert(type(hints) == "table","Tried to set hints not a table!")
			set_hints(unpack(hints))
		end
		
	end
end)

--Hint if we've typed the name of a command
add_cmdbox_hook(function(cmdbox)
	local text = cmdbox:gettext()
	if cmds[text] and cmds[text .. "_hint"] and cmds["can_" .. text] then
		print("Hint command")
		local hints = cmds[text .. "_hint"]()
		set_hints(unpack(hints))
	end
end)

return cmds