From 0aae46ecc38005236210f7e243f02cac39ab1dc3 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Sun, 27 Jan 2019 10:32:09 -0500 Subject: Inital commit --- client/data/commands.lua | 426 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 426 insertions(+) create mode 100644 client/data/commands.lua (limited to 'client/data/commands.lua') diff --git a/client/data/commands.lua b/client/data/commands.lua new file mode 100644 index 0000000..e9df205 --- /dev/null +++ b/client/data/commands.lua @@ -0,0 +1,426 @@ +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.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.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.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 + for k,v in pairs(lp.location.last_houses) do + places[#places + 1] = v + 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.settle = function(...) + local oldruncommand = run_command + 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 + 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 +cmds.test = function(...) + lp:goto_house("City/one") +end +cmds.test_hint = function(...) + +end +local valid_directions = { + north = true, south = true, east = true, west = true, up = true, down = true +} +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[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") + 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) + end + + --cur_house:create_room(cur_house,direction,name) +end +cmds.create_room_hint = function(...) + return {"create_room "} +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 "} +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,".* $") then + local root = string.match(text,"^([^%s]+)") + print("root is:") + if cmds[root] then + local hints = cmds[root .. "_hint"]() + assert(type(hints) == "table","Tried to set hints not a table!") + for k,v in pairs(hints) do + assertf(type(v) == "string","Hints table has something other than a string: %s",type(v)) + end + set_hints(unpack(hints)) + end + elseif 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) + +return cmds -- cgit v1.2.3-70-g09d2