• Reference
  • LUA API
  • Example
  • Equipment Limitation

Equipment limitation

This is an example of a piece of equipment that we often see in FPS games where the player picks up one primary weapon and then automatically discards the other primary weapon, i.e. only one of the same type of weapon can be carried at a time, this example is a simple version of that functionality.

Brief description

  1. the camera follows the unit as it moves
  2. picking up a particular weapon type
  3. if a weapon type already exists, the newly picked up weapon of the same type is automatically discarded
  4. each type of weapon has its own special effects
  5. a weapon can be discarded manually

Code

Methods

do
	local unitId = 9 -- unit number
	local playerId = 1 -- player number
 
	-- Game initialisation
	function gameInit()
		local player = cli.player(playerId)._base
		local unit = cli.actor_unit(unitId)._base
		player:camera_focus(unit)
	end
 
	-- Acquired items
	function getItem(tri, ...)
		local args = {...}
		local item = args[2]
		local unit = args[1]
		local itemType = cli.get_item_kv(item:get_id(), "type")
		itemType = type(itemType) == "string" and itemType or tostring(itemType)
		if itemType then
			local unitItemType = unit[itemType] -- get unit owner item
			local player = cli.player(playerId)
			-- judge is owner equipmenbt
			if unitItemType then
				player:msg("The character already has an equipment type" .. itemType)
				-- exist equipment, discard and pickup
				cli.create_item(unit:get_point(), item:get_id(), player)
				item.udefProSystemDiscard = true -- flag as system discard
				item:remove()
			else
				player:msg(itemType .. " type equipped with new items")
				-- writ in new equipment
				unit[itemType] = item -- set unit owner item
				generateEffectProc(unit, item)
			end
		end
	end
 
	-- Lost items
	function discardItem(tri, ...)
		local args = {...}
		local item = args[2]
		local unit = args[1]
		local itemType = cli.get_item_kv(item:get_id(), "type")
		itemType = type(itemType) == "string" and itemType or tostring(itemType)
		local player = cli.player(playerId)
		if itemType then
			-- judge is system discard
			if item.udefProSystemDiscard == true then
				item.udefProSystemDiscard = nil
				player:msg("Discarded items are discarded by the system")
			else
				unit[itemType] = nil
				player:msg("Delete the corresponding type of possession")
			end
		else
			player:msg("Discarded items have no type")
		end
	end
 
	-- Generate effects processor
	function generateEffectProc(unit, item)
		local itemType = item:get_id()
		local waeponType_1 = 134238342
		local waeponType_2 = 134225980
		local armour_1 = 134275390
		local armour_2 = 134250198
		-- waepon
		if itemType == waeponType_1 or itemType == waeponType_2 then
			local projectType = cli.get_item_kv(item:get_id(), "projectType")
			generateWeaponEffect(unit, item, projectType)
		end
		-- armour_1
		if itemType == armour_1 then
			generateArmourEffect_1(unit, item)
		end
		-- armour_2
		if itemType == armour_2 then
			generateArmourEffect_2(unit, item)
		end
	end
	
	-- Generating weapon effects
	function generateWeaponEffect(unit, item, projectType)
		local modifierBase = unit._base.api_add_modifier(projectType)
		local itemType = cli.get_item_kv(item:get_id(), "type")
		cli.loop(1, function(timer)
			-- Determine whether to switch equipment
			if unit[itemType] ~= item then
				modifierBase.api_remove()
				timer:remove()
			end
		end)
	end
	
	-- Generate armour 1 effect
	function generateArmourEffect_1(unit, item)
		local itemType = cli.get_item_kv(item:get_id(), "type")
		local cnt = 0
		cli.loop(1, function(timer)
			if unit[itemType] == item then
				cnt = cnt + 1
				if cnt == 3 then
					cnt = 0
					for _, v in pairs({134275138, 134262478}) do
						cli.effect({
							id = v,
							point = unit:get_point(),
							angle = 0,
							owner = cli.player(32), -- Neutral and friendly
							skill = nil,
							life = 3,
							is_life = true,
							height = 0,
							visible_type = 1
						})
					end
				else
					for _, v in pairs({134266480, 134248884}) do
						local ef = cli.effect({
							id = v,
							point = unit:get_point(),
							angle = 0,
							owner = cli.player(32), -- Neutral and friendly
							skill = nil,
							life = 2,
							is_life = true,
							height = 0,
							visible_type = 1
						})
					end
				end
			else
				timer:remove()
			end
		end)
	end
	
	-- Generate armour 2 effects
	function generateArmourEffect_2(unit, item)
		local itemType = cli.get_item_kv(item:get_id(), "type")
		local cnt = 0
		local angle_1 = 90
		local angle_2 = 270
		local projects = {}
		for k, v in pairs({134235651, 134229874}) do
			projects[k] = cli.effect({
				id = v,
				point = unit:get_point(),
				angle = 0,
				owner = cli.player(32), -- Neutral and friendly
				skill = nil,
				life = nil,
				height = 0,
				visible_type = 1
			})
		end
		cli.loop(0.03, function(timer)
			if unit[itemType] == item then
				-- data deal
				angle_1 = angle_1 - 2
				angle_2 = angle_2 + 6
				local x, y = unit:get_point():offset(angle_1, 100 * 100):get() -- ! offset() will make "1" -> "100"
				local point_1 = cli.point(x / 100, y / 100, 0)
				local xx, yy = point_1:offset(angle_2, 75 * 100):get()
				local point_2 = cli.point(xx / 100, yy / 100, 0)
				projects[1]:set_point(point_1)
				projects[2]:set_point(point_2)
				-- logic deal
				cnt = cnt + 1
				if cnt == 1 then
					projects[3] = cli.effect({
						id = 134240748,
						point = unit:get_point(),
						angle = 0,
						owner = cli.player(32), -- Neutral and friendly
						skill = nil,
						life = 3,
						height = 0,
						visible_type = 1
					})
				end
				if cnt >= 2 and cnt <= 13 then
					projects[3]:set_point(point_2)
				end
				if cnt >= 60 then
					cnt = 0
				end
			else
				for i = 1, 3, 1 do
					projects[i]:remove()
				end
				timer:remove()
			end
		end)
	end

Events

	-- Game initialisation events
	cli.game:event("Game-Init", gameInit)
 
	-- Acquired items event
	cli.game:event("Unit-GetItem", getItem)
 
	-- Lost item events
	cli.game:event("Unit-LoseItem", discardItem)
end

Download

link unvailable for now.