-- player_use_smart_object - Allows players to route the approriate triggers to smart objects via commands. -- Created by: Thor Alexander/Sean Riley -- Created on: 4/3/2008 -- 08/28/08 - Added Just In Time Capability - J.McNab -- Attach The Following Scripts on select event -- object_radial_menu -- object_scaleable -- object_removeable -- configurable -- 10/22/08 - Connection to Save to Template in OX -- CONSTANTS -- [J.McNab] Added for Save to Template ERROR_DISPLAY_TIME = 2000 INVALID_SCRIPT_LIST = {'12071:3', '12071:19', '12071:26', '12071:39'} TEMPLATE_PROPERTIES = {'tickInterval', 'physical', 'collisionShape', 'collisionScaleX', 'collisionScaleY', 'collisionScaleZ', 'lockFacing'} OBJECT_PROPERTIES = {'blocking'} SPRITE_PROPERTIES = {{'spriteScaleX', 'scaleX'}, {'spriteScaleY', 'scaleY'}, {'spriteOrigin', 'originMode'}, {'maskRed', 'red'}, {'maskGreen', 'green'}, {'maskBlue', 'blue'}, {'frames', 'frames'}, {'cols', 'cols'}, {'rows', 'rows'}, {'interval', 'interval'}} -- [J.McNab] -- Scripts to attached to an object on the fly. NOTE: Order matters for radial menu values BASE_SCRIPTS = {'12071:19', '12071:39', '12071:26', '12071:3', '19378:56'} Define Properties() -- Update 07/03/08 - J.McNab smart_object_window = 0 smart_object_window_coords = {x = -1, y = -1} radial_menu_tooltips = {} selected_object_id = 0 end Define Commands() MakeInput('use','mouse-object', 'click', 'none', 'use') MakeInput('configure','mouse-object','click', 'shift', 'configure') MakeCommand("use", "use a smart object", "target:object") MakeCommand("configure", "configure a smart object", "target:object") MakeCommand("event", "send an event to a smart object", "event_name:string", "target:object") MakeCommand("set_multiple_params", "set multiple parameters in OX", "object_id:int", "user_id:int", "script_id:string", "command:string", "parameter_table:lua_table") -- [J.McNab] Added for Save to Template functionality MakeCommand('template_from_object', 'Create a Template from an Object', 'obj:int', 'name:string') -- [J.McNab] -- end Command use(object, ...) Debug("%s used %s", self.name, object.type) if(object.use_range ~= nil) then Debug("range = %s and use_range = %s", Distance(self, object),object.use_range) end if (object.use_range == nil or object.use_range == 0 or Distance(self, object) <= object.use_range) then UseObject(self, object, ...) else SendTo(self, 'not_in_use_range', 0, object) end end Command configure(object, ...) Debug("%s configured %s", self.name, object.type) if IsSuperuser(self) == 1 then if(self.selected_object_id ~= 0) then local last_object = GetObjectById(self.selected_object_id) if(last_object ~= nil) then detach_base_scripts(last_object) end self.selected_object_id = 0 end jit_script_attach(object, '12071:3') self.selected_object_id = object.id ConfigureObject(self, object, ...) end end Command event(event_name, object, ...) if(event_name == 'select') then if(self.selected_object_id ~= 0) then local last_object = GetObjectById(self.selected_object_id) if(last_object ~= nil) then detach_base_scripts(last_object) end self.selected_object_id = 0 end attach_base_scripts(object) self.selected_object_id = object.id end if(event_name == 'deselect') then local last_object = GetObjectById(self.selected_object_id) if(last_object ~= nil) then detach_base_scripts(last_object) end self.selected_object_id = 0 end SendTo(object, event_name, 0, self, ...) end Command set_multiple_params(object_id, user_id, script_id, command, parameter_table) local command_args = string.split(command, ' ') local command = command_args[1] table.remove(command_args, 1) if(type(parameter_table) == 'table') then local object = GetObjectById(object_id) for param, value in pairs(parameter_table) do SendTo(object, 'set_script_param', 0, object, user_id, script_id, param, value) end end DoCommand(self, command, unpack(command_args)) end -- [J.McNab] Added for Save to Template functionality Command template_from_object(obj, name) -- Only super users may perform if(IsSuperuser(self) == 0) then return end local target_object = GetObjectById(obj) if(target_object == nil) then SendTo(self, 'error_alert', 0, self, 'Cannot find that object') return end -- Test name value is valid if(name == nil or type(name) ~= 'string' or name == '') then SendTo(self, 'error_alert', 0, self, 'Please enter a template name') return end -- Verify that name is available for _, template_id in pairs(stylesheet.templates._all_) do local template = stylesheet.templates[template_id] if(name == template.name) then SendTo(self, 'error_alert', 0, self, 'Template with name ' .. name .. ' already exists.') return end end local object_template = '_object_' -- Find the template of the object for certain aspects for _, template_id in pairs(stylesheet.templates._all_) do local template = stylesheet.templates[template_id] if(template.name == target_object.type) then object_template = template break end end -- Get the scripts attached to object local attached_scripts = {} for _, script_id in pairs(stylesheet.scripts._all_) do local script = stylesheet.scripts[script_id] if(HasScriptById(target_object, script_id) == 1) then -- Make sure script is valid local valid = 1 for _, invalid_script_id in pairs(INVALID_SCRIPT_LIST) do if(script_id == invalid_script_id) then valid = 0 end end if(valid == 1) then table.insert(attached_scripts, script_id) end end end -- Create the template DoCommand(self, '/create_template_base', name, 1) local new_template_id = '' -- Find the new template for _, template_id in pairs(stylesheet.templates._all_) do local template = stylesheet.templates[template_id] if(template.name == name) then new_template_id = template_id break end end -- Debug('Starting template properties') -- Set template properties for _, template_prop in pairs(TEMPLATE_PROPERTIES) do local value = stylesheet.templates[object_template.baseId][template_prop] -- Debug('Setting template property %s to %s', template_prop, value) if(value ~= nil) then DoCommand(self, '/set_template', new_template_id, template_prop, object_template[template_prop]) end end -- Debug('Finish template properties') -- Debug('Create a new sprite') local current_sprite_id = target_object.spriteId local current_sprite = stylesheet.sprites[current_sprite_id] DoCommand(self, '/create_sprite', current_sprite.url, name .. '_sprite', current_sprite.frames, current_sprite.cols, current_sprite.rows, current_sprite.interval, '0', ',') -- Locate the new sprite's id local new_sprite_id = '' for _, sprite_id in pairs(stylesheet.sprites._all_) do local sprite = stylesheet.sprites[sprite_id] if(sprite.name == name .. '_sprite') then new_sprite_id = sprite_id break end end DoCommand(self, '/set_template', new_template_id, 'spriteId', new_sprite_id) for _, sprite_prop in pairs(SPRITE_PROPERTIES) do local value = target_object[sprite_prop[1]] if(value == nil) then value = stylesheet.sprites[target_object.spriteId][sprite_prop[1]] end DoCommand(self, '/set_sprite', new_sprite_id, sprite_prop[2], value) end -- Debug('Starting object properties') -- Object level properties for _, object_prop in pairs(OBJECT_PROPERTIES) do local value = target_object[object_prop] if(value ~= nil) then -- Debug('Setting object property %s to %s', object_prop, value) DoCommand(self, '/set_template', new_template_id, object_prop, value) end end -- Debug('Finish object properties') -- Debug('Starting script properties') -- Attach scripts for _, script_id in pairs(attached_scripts) do stylesheet.AttachTemplateScript(new_template_id, script_id) for _, param in pairs(stylesheet.scripts[script_id].params._all_) do local value = target_object[param] if(type(value) ~= 'table') then -- Debug('Setting script property %s in %s to %s', param, script_id, value) DoCommand(self, '/set_template_param', new_template_id, script_id, param, 'value', value) else -- local format_table = table.tostring(param) -- Debug(format_table) end end end -- Debug('Finish script properties') end -- [J.McNab] -- Trigger deselect(object) if(self.smart_object_window ~= 0) then UiDelete(self.smart_object_window) self.smart_object_window =0 end end Trigger not_in_use_range(object) Debug("INFO: %s is to far away to use the %s.", self.name, object.type) end Trigger ui_library_window_closed(window_id) if(window_id == self.smart_object_window) then self.smart_object_window = 0 local last_object = GetObjectById(self.selected_object_id) if(last_object ~= nil) then jit_script_detach(last_object, '12071:3') end end end Trigger ui_move(windowId, destinationX, destinationY) if(windowId == self.smart_object_window) then self.smart_object_window_coords.x = destinationX self.smart_object_window_coords.y = destinationY end end function jit_script_attach(object, script_id) if(HasScriptById(object, script_id) == 0) then AttachScriptById(object, script_id) end end function jit_script_detach(object, script_id) if(HasScriptById(object, script_id) == 1) then DetachScriptById(object, script_id) end end function attach_base_scripts(object) for index, script_id in pairs(BASE_SCRIPTS) do if(script_id ~= '12071:26') then jit_script_attach(object, script_id) else if(object.type ~= 'player') then jit_script_attach(object, script_id) end end end if(HasScriptById(object, '12071:8') == 1) then AttachScriptById(object, '12071:8') end -- SendTo(object, 'attach', 0) end function detach_base_scripts(object) for index, script_id in pairs(BASE_SCRIPTS) do jit_script_detach(object, script_id) end -- SendTo(object, 'detach', 0) end