Forum

> > CS2D > General > Rewriting the equip function - AutoStrip weapons
Forums overviewCS2D overviewGeneral overviewLog in to reply

English Rewriting the equip function - AutoStrip weapons

2 replies
To the start Previous 1 Next To the start

old closed Rewriting the equip function - AutoStrip weapons

Lee
Moderator Off Offline

Quote
This is assuming that you're using wrapper.lua, if not, simply include it in the main execution via dofile("sys/lua/wrapper.lua")

Also, this will use Kiffer-Opa's Weapons.lua file (Thanks a lot for it btw) and a tabled version of the weapons chart.

A syntax highlighted version
http://amx2d.co.cc/viewtopic.php?f=12&t=39

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
-------------------------------------------------------------------
-- Utility: equip() Rewrite. Includes auto stripping. --
-- 31.03.2009 - http://amx2d.co.cc/ - AMX2D Scripting --
-- Author - Lee Gao
-- http://amx2d.co.cc/viewtopic.php?f=12&t=39
-------------------------------------------------------------------

--[[--
**INFO:**
This rewrite of the equip() function automatically
strips all of the conflicting weapons so the weapons
do not overlap. IE: No more of having an AK47 and a M4A1
at the same time.

**USAGE:**
Copy equip.lua (This file) into the sys/lua/ folder
In server.lua, add: dofile("sys/lua/equip.lua")

Equiping: equip(Player, Weapon(Name or ID), NoConflicts)

*Example:*
Player 1 has AK47, Deagle, HE, and Knife.

Do
	equip(1, "M4A1", true) leads to
Result: M4A1, Deagle, HE, and Knife

Do
	equip(1, "Laser", false) or equip(1, "Laser") leads to
Result: Laser, M4A1, Deagle, HE, and Knife.
--]]--

--###################################--
--############ Conditions ###########--
--###################################--

if not amx2d then
	print("Please read the comment on AMX2D exclusive features.")
end
if not equip then dofile("sys/lua/wrapper.lua") end
if not Knife then
	print("Please include Kiffer-Opa's Weapon list.")
	-- Author: Kiffer-Opa
	-- Melee Weapons
	Knife =50;
	Machete =69;
	Wrench =74;
	Claw =78;
	Chainsaw =85;
end
if not trim then
	--[[--
	If trim has not already been declared then just return the
	string and warn developers that they must not add extra spaces
	when calling equip - AMX2D.
	--]]--
	function trim(t) return t end
end

--###################################--
--############# equip() #############--
--###################################--

local _equip = equip -- Preparing to overload function equip.

function equip(p, w, noConflict)
	--If used as equip(player, wpn), then return the normal version.
	if not noConflict then return _equip(p, w) end

	--[[--
	Checks if w is a string, if it is, converts it to the
	wpntype equivalent. Then it checks if the wpntype ID exists.

	**Note:**
	Must have AMX2D loaded to have this feature on, else you
	must use WeaponType instead of a string.
	--]]--

	if amx2d then
		if type(w) == "string" then
			if wpn.id[trim(w)] then
				w = wpn.id[trim(w)]
			else return
			end
		end
	end
	if not wpn[tonumber(w)] then return end

	--[[--
	This creates a list of all of the current weapons and
	strips them from the player, then gives him back the
	Knife.
	--]]--

	local _pwpn = {slot={}, oldslot = {}}

	local slot = function(_wpn)
		if (_wpn<=40 and _wpn>=10) or (_wpn<=49 and _wpn>=45) then
			--Primary Weapon - Slot 1
			return 1
		elseif (_wpn < 10) then
			--Secondary Weapon - Slot 2
			return 2
		elseif (_wpn == Knife or _wpn == Machete or _wpn == Wrench or _wpn == Claw or _wpn == Chainsaw) then
			--Melee - Slot 3
			return 3
		elseif (_wpn < 55 and _wpn > 50) then
			--Grenades - Slot 4
			return 4
		else
			--Slotless
			return 0
		end
	end

	while not (player(p, "weapontype") == 0) do
		local _wpn = player(p, "weapontype")
		_pwpn.oldslot[slot(_wpn)] = _wpn
		_pwpn.slot[slot(_wpn)] = _wpn
		_pwpn[_wpn] = _wpn -- In case we need to call _pwpn[wpntyp] to see if it exists.
		strip(p, _wpn)
	end

	equip(p, Knife)

	--Slot 1 or 2 should strip these weapons.
	if (slot(w) == 1) or (slot(w)==2) then
		if _pwpn.slot[slot(w)] then
			strip(p, _pwpn.slot[slot(w)])
			_pwpn.slot[slot(w)] = w
		end
	end

	--[[--
	Iterates through all of the slots of the new weapon table and
	equips them using equip (which we do not pass in the last boolean
	and thus will not do a full recusion)
	--]]--

	for k, v in pairs (_pwpn.slot) do
		--Will Re-equip everything, including Knife again.
		equip(p, v)
	end

	return _pwpn -- Returns the old and current slot listing.
end
edited 1×, last 01.04.09 04:42:52 am
To the start Previous 1 Next To the start
Log in to replyGeneral overviewCS2D overviewForums overview