Lua Scripts/Questions/Help
6,770 replies01.04.09 11:40:04 am
Please use this thread to post your CS2D Lua Scripts or to to ask questions about Lua!
Moreover you can upload finished and good scripts there:
Lua Scripts file section
I recommend to use the [ code ] your script [ /code ] tags (without the spaces) to post your scripts.
NEW ALTERNATIVE: CS2D.org
Lee established a page for your Lua questions! Visit www.CS2D.org
EDIT:
CLOSED
we now have SUBFORUMS and there is an extra forum for all Lua questions! feel free to create new threads there:
http://www.unrealsoftware.de/forum_threads.php?forum=105⊂=2
Moreover you can upload finished and good scripts there:

I recommend to use the [ code ] your script [ /code ] tags (without the spaces) to post your scripts.

Lee established a page for your Lua questions! Visit www.CS2D.org
EDIT:
CLOSED
we now have SUBFORUMS and there is an extra forum for all Lua questions! feel free to create new threads there:
http://www.unrealsoftware.de/forum_threads.php?forum=105⊂=2
edited 7×, last 28.12.10 05:46:24 pm
Simple function to swap teams.
The basic concept is to create a table with the index of 1 (traditionally the teamid for Terrorists) to have a value of "ct" and 2 to have a value of "t"
Remember the swap commands are parse("maket ID") or parse("makect ID") So you can just pass in the team ID of the player as the index.
Complete script at
http://amx2d.co.cc/viewtopic.php?f=11&t=43
Also, can you make an exception on double posting in this thread since stringing all of the code together at the same time can create pretty long posts.
The basic concept is to create a table with the index of 1 (traditionally the teamid for Terrorists) to have a value of "ct" and 2 to have a value of "t"
Remember the swap commands are parse("maket ID") or parse("makect ID") So you can just pass in the team ID of the player as the index.
Complete script at
http://amx2d.co.cc/viewtopic.php?f=11&t=43
Code:
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
_rteam = {[1] = "ct", [2] = "t", [0] = "spec"}
function swapsides()
for p = 1, 32, 1 do
if player(p, "exists") then
msg("Server Teams have being swapped")
parse(string.format("make%s %s", _rteam[player(p, "team")], p))
end
end
end
function swapsides()
for p = 1, 32, 1 do
if player(p, "exists") then
msg("Server Teams have being swapped")
parse(string.format("make%s %s", _rteam[player(p, "team")], p))
end
end
end
Also, can you make an exception on double posting in this thread since stringing all of the code together at the same time can create pretty long posts.
This small script gives you 16000$ every time you spawn:
It's my first lua script
EDIT: I've fixed the code, so it can work with other scripts that use the startround hook
Code:
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
if flacko==nil then flacko={} end
flacko.cash={}
addhook("startround","flacko.cash.spawncash")
function flacko.cash.spawncash(p)
for i=1, 32 do
if(player(i,"exists")) then
parse("setmoney "..i.." 16000")
end
end
end
flacko.cash={}
addhook("startround","flacko.cash.spawncash")
function flacko.cash.spawncash(p)
for i=1, 32 do
if(player(i,"exists")) then
parse("setmoney "..i.." 16000")
end
end
end
It's my first lua script

EDIT: I've fixed the code, so it can work with other scripts that use the startround hook
edited 1×, last 24.04.09 09:18:23 pm
rather pointless because there are settings for that but.. yah.. nice 
btw: you shouldn't use the samples table! it's for my samples
I recommend to use a table with your name or something like that as "mastertable" for all your script.
like that (just replaced "sample" with "flacko"):

btw: you shouldn't use the samples table! it's for my samples

I recommend to use a table with your name or something like that as "mastertable" for all your script.
like that (just replaced "sample" with "flacko"):
Code:
1
2
3
4
5
6
7
2
3
4
5
6
7
if flacko==nil then flacko={} end
flacko.cash={}
addhook("spawn","flacko.cash.spawn")
function flacko.cash.spawn(p)
parse("setmoney "..p.." 16000")
end
flacko.cash={}
addhook("spawn","flacko.cash.spawn")
function flacko.cash.spawn(p)
parse("setmoney "..p.." 16000")
end
edited 1×, last 02.04.09 02:11:57 am
Sorry for profanating your samples table DC
BTW: The script was meant for "Standard" game mode, so, no more things like restarts and reconnecting

BTW: The script was meant for "Standard" game mode, so, no more things like restarts and reconnecting

03.04.09 09:01:41 am
It might be a good idea for other people to post here periodically or else we may get pretty long posts.
The following scripts are for developers ease, they're utility functions and should be used as such.
Scroll down for the Grenade-Mod code. (No-AMX2D and less features)
Select the main weapon
Usage: selectMain(p)
Grenade Mod - No AMX2D
Just include the file in server.lua
Color Function
Usage: Color(R, G, B)
Example: print(Color(255, 40, 40).."Output") will print out "©255040040Output" and display the word "Output" in a reddish-brown color.
Note: If you go above 255 on the RGB value or below 0, the function will automatically return either 255 or 0 to make sure the color works.
msg() and msg2() rewrites
This will allow you to use msg(t) normally, or use it as such
msg(Color, Text, Centered)
same with msg2.
Example:
msg(Color(255, 0, 0), "Some red text that's centered", true)
msg2(1, Color(0, 255, 0), "A msg2 that's blue and not centered (Check the console as well)")
sayto(2, Color(0, 0, 255), "sayto "..player(2, "name"), false)
Revision to the enhanced equip function, I wrote the original from the top of my head so I had to make a few assumptions, turns out a few of those were incorrect including the one about weapontypes going down to 0, this then means that the original would give you a never ending while loop. Here's a tested and revised version.
EDITED: Also, equiping the special primary weapons (RPGLauncher, GrenadeLauncher, RocketLauncher, etc) will allow you to have multiple primary weapons. Basically these weapons are back ups for the primary.
The following scripts are for developers ease, they're utility functions and should be used as such.



Usage: selectMain(p)
Code:
1
2
3
4
2
3
4
function selectMain(p)
strip(p, 50)
equip(p, 50)
end
strip(p, 50)
equip(p, 50)
end

Just include the file in server.lua
Code:
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
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
wpns = {}
for i = 1, 32, 1 do
wpns[i] = {nades = 30, gun = 0, credits = 10}
end
nadetext = 'hudtxt2 %s %s "%s>>You have %s grenades<<" 160 100 1'
credittext = 'hudtxt2 %s %s "%s>>You have %s credits<<" 160 120 1'
guntext = 'hudtxt2 %s %s "%s>>%s<<" 160 140 1'
parse('hudtxt 4 "©255100000Type !nade to buy 2 nades for 2 Credits" 160 170 1')
function nade(p)
local nades = wpns[p].nades
if wpns[p].nades <= 0 then nades = "no" end
local gun = "Your Gun is a "..wpns[p].gun
if wpns[p].gun == 0 then gun = "You do not have a gun" end
parse(string.format(nadetext, p, 1, "©255000000", nades))
parse(string.format(credittext, p, 2, "©255000000", wpns[p].credits))
parse(string.format(guntext, p, 3, "©255000000", gun))
end
addhook("startround", "init")
function init()
for i = 1, 32, 1 do
if player(i, "exists") then nade(i) end
end
end
addhook("die", "die")
function die(p, w)
if wpns[p].nades < 5 then
wpns[p].nades = 5
end
wpns[p].credits = wpns[p].credits + 5
nade(p)
end
addhook("spawn", "spawn")
function spawn(p, w)
nade(p)
parse("strip "..p.." 0")
parse("equip "..p.." 51")
if not wpns[p].gun == 0 then
parse("equip "..p.." "..wpns[p].gun)
end
parse("strip "..p.." 50")
parse("equip "..p.." 50")
end
addhook("collect", "collect")
function collect(p)
return 1
end
addhook("buy", "buy")
function buy(p)
return 1
end
addhook("projectile", "projectile")
function projectile(p, w)
wpns[p].nades = wpns[p].nades - 1
nade(p)
if wpns[p].nades > 0 then
parse("equip "..p.." 51")
end
end
addhook("kill", "kill")
function kill(p, w)
df = 0
if w == 50 then
df = 5
end
wpns[p].nades = wpns[p].nades + 2 + df
wpns[p].credits = wpns[p].credits + 3 + df
nades(p)
parse("equip "..p.." 51")
end
addhook("leave", "leave")
function leave(p)
wpns[p] = {nades = 5, gun = 0, credits = 10}
end
addhook("say", "say")
function say(p, t)
if t == "!nade" then
if wpns[p].credits >= 2 then
wpns[p].credits = wpns[p].credits - 2
wpns[p].nades = wpns[p].nades + 2
nade(p)
parse("equip "..p.." 51")
msg2(p, "©155200000You have bought 2 Nades for 2 Credits.")
else
msg2(p, "©255100000You do not have enough credits")
end
return 1
end
end
for i = 1, 32, 1 do
wpns[i] = {nades = 30, gun = 0, credits = 10}
end
nadetext = 'hudtxt2 %s %s "%s>>You have %s grenades<<" 160 100 1'
credittext = 'hudtxt2 %s %s "%s>>You have %s credits<<" 160 120 1'
guntext = 'hudtxt2 %s %s "%s>>%s<<" 160 140 1'
parse('hudtxt 4 "©255100000Type !nade to buy 2 nades for 2 Credits" 160 170 1')
function nade(p)
local nades = wpns[p].nades
if wpns[p].nades <= 0 then nades = "no" end
local gun = "Your Gun is a "..wpns[p].gun
if wpns[p].gun == 0 then gun = "You do not have a gun" end
parse(string.format(nadetext, p, 1, "©255000000", nades))
parse(string.format(credittext, p, 2, "©255000000", wpns[p].credits))
parse(string.format(guntext, p, 3, "©255000000", gun))
end
addhook("startround", "init")
function init()
for i = 1, 32, 1 do
if player(i, "exists") then nade(i) end
end
end
addhook("die", "die")
function die(p, w)
if wpns[p].nades < 5 then
wpns[p].nades = 5
end
wpns[p].credits = wpns[p].credits + 5
nade(p)
end
addhook("spawn", "spawn")
function spawn(p, w)
nade(p)
parse("strip "..p.." 0")
parse("equip "..p.." 51")
if not wpns[p].gun == 0 then
parse("equip "..p.." "..wpns[p].gun)
end
parse("strip "..p.." 50")
parse("equip "..p.." 50")
end
addhook("collect", "collect")
function collect(p)
return 1
end
addhook("buy", "buy")
function buy(p)
return 1
end
addhook("projectile", "projectile")
function projectile(p, w)
wpns[p].nades = wpns[p].nades - 1
nade(p)
if wpns[p].nades > 0 then
parse("equip "..p.." 51")
end
end
addhook("kill", "kill")
function kill(p, w)
df = 0
if w == 50 then
df = 5
end
wpns[p].nades = wpns[p].nades + 2 + df
wpns[p].credits = wpns[p].credits + 3 + df
nades(p)
parse("equip "..p.." 51")
end
addhook("leave", "leave")
function leave(p)
wpns[p] = {nades = 5, gun = 0, credits = 10}
end
addhook("say", "say")
function say(p, t)
if t == "!nade" then
if wpns[p].credits >= 2 then
wpns[p].credits = wpns[p].credits - 2
wpns[p].nades = wpns[p].nades + 2
nade(p)
parse("equip "..p.." 51")
msg2(p, "©155200000You have bought 2 Nades for 2 Credits.")
else
msg2(p, "©255100000You do not have enough credits")
end
return 1
end
end

Usage: Color(R, G, B)
Example: print(Color(255, 40, 40).."Output") will print out "©255040040Output" and display the word "Output" in a reddish-brown color.
Note: If you go above 255 on the RGB value or below 0, the function will automatically return either 255 or 0 to make sure the color works.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function digits(n, p, m)
if not m then m = 10^p - 1 end
if n == 0 then return "000" end
if n <= m then
local _p = 0
while (n < 10^(p-1)) do
p = p-1
_p = _p + 1
end
local str = n
for i = 1, _p, 1 do
str = "0"..str
end
return str
else
return m
end
end
function Color(r, b, g)
return string.format("©%s%s%s", digits(r, 3, 255), digits(b, 3, 255),digits(g, 3, 255))
end
if not m then m = 10^p - 1 end
if n == 0 then return "000" end
if n <= m then
local _p = 0
while (n < 10^(p-1)) do
p = p-1
_p = _p + 1
end
local str = n
for i = 1, _p, 1 do
str = "0"..str
end
return str
else
return m
end
end
function Color(r, b, g)
return string.format("©%s%s%s", digits(r, 3, 255), digits(b, 3, 255),digits(g, 3, 255))
end

This will allow you to use msg(t) normally, or use it as such
msg(Color, Text, Centered)
same with msg2.
Example:
msg(Color(255, 0, 0), "Some red text that's centered", true)
msg2(1, Color(0, 255, 0), "A msg2 that's blue and not centered (Check the console as well)")
sayto(2, Color(0, 0, 255), "sayto "..player(2, "name"), false)
Code:
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
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
_msg = msg
function msg(t1, t2, b)
local centered = ""
local color = ""
local text = ""
if t2 then
color = t1
text = t2
else
color = ""
text = t1
end
if b then
centered = "@C"
end
return _msg(color..text..centered)
end
say = msg
_msg2 = msg2
function msg2(p, t1, t2, b)
local centered = ""
local color = ""
local text = ""
if t2 then
color = t1
text = t2
else
color = ""
text = t1
end
if b then
centered = "@C"
end
print(string.format("%sSayTo(%s:ID#\'%s\'): %s", color, player(p,"name"), p, text))
return _msg2(p, color..text..centered)
end
sayto = msg2
function msg(t1, t2, b)
local centered = ""
local color = ""
local text = ""
if t2 then
color = t1
text = t2
else
color = ""
text = t1
end
if b then
centered = "@C"
end
return _msg(color..text..centered)
end
say = msg
_msg2 = msg2
function msg2(p, t1, t2, b)
local centered = ""
local color = ""
local text = ""
if t2 then
color = t1
text = t2
else
color = ""
text = t1
end
if b then
centered = "@C"
end
print(string.format("%sSayTo(%s:ID#\'%s\'): %s", color, player(p,"name"), p, text))
return _msg2(p, color..text..centered)
end
sayto = msg2

EDITED: Also, equiping the special primary weapons (RPGLauncher, GrenadeLauncher, RocketLauncher, etc) will allow you to have multiple primary weapons. Basically these weapons are back ups for the primary.
Code:
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
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
-------------------------------------------------------------------
-- 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
function selectMain(p)
strip(p, Knife)
equip(p, Knife)
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.name[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
selectMain(p)
while not (player(p, "weapontype") == 50 or player(p, "weapontype") == 0) do
local _wpn = player(p, "weapontype")
msg2(p, Color(50, 50, 50), _wpn)
_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
--Slot 1 or 2 should strip these weapons.
if (slot(w) == 1) or (slot(w)==2) then
msg2(p, Color(50, 50, 50), tostring((slot(w) == 1)))
_pwpn.slot[slot(w)] = w
else
equip(p, w)
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
selectMain(p)
return _pwpn -- Returns the old and current slot listing.
end
-- 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
function selectMain(p)
strip(p, Knife)
equip(p, Knife)
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.name[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
selectMain(p)
while not (player(p, "weapontype") == 50 or player(p, "weapontype") == 0) do
local _wpn = player(p, "weapontype")
msg2(p, Color(50, 50, 50), _wpn)
_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
--Slot 1 or 2 should strip these weapons.
if (slot(w) == 1) or (slot(w)==2) then
msg2(p, Color(50, 50, 50), tostring((slot(w) == 1)))
_pwpn.slot[slot(w)] = w
else
equip(p, w)
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
selectMain(p)
return _pwpn -- Returns the old and current slot listing.
end
edited 7×, last 30.04.09 11:11:40 pm
06.04.09 11:26:11 pm
I finally finished "Kiffer-Opa's GunGame".
If you want, you can modify it for you own needs without asking me. All I want from you that you don't claim it is all work, you have to make clear, it was also my work.
Download here! (current version: 1.1)
That's what this game is about:
kill a certain amount of players with a certain weapon
you get a new weapon when you killed enough
be the first to pass the last weapon level to win (so only one player can win)
Features
Each level can have an invidual level up requirement (eg. you need 3 kills for AK-47 and 2 kills for M4A1 to level up)
get grenades back after they exploded (the player still has to switch manually to it, though)
your current level, kills needed to level up and name of current weapon is always visible
Optional: Steal a level from other player when you kill him with Knife without being on Knife level
server decides what happens after a round ended: change map or keep map
Server can set the time to wait before a new round starts
last level warning sound and message when someone reached the last level
lose 1 kill if you selfkill with any weapon
Server functions
Type these in the console to use them as a server:
true: allow knife in all levels and activate level stealing by knife kill
false: disallow knife in almost all levels and deactivate level stealing
resets all levels and kills of all players to 0, but doesn't restart round
This controls the behaviour of the game after a round ended:
true: change map according to the mapcycle
false: just restart round and keep map
set default game mode for this GunGame (important if you want to override the set game mode in mapcycle.cfg.
<game mode> is either Standard, Deathmatch, TeamDeathmatch (without space), Construction or Zombies (without exclamation mark) (recommended are Deathmatch or TeamDeathmatch)
<filename> is the complete name of the gunlevels file. It must be in CS2D/sys/lua/KOGG/gunlevels/
preinstalled gunlevels files:
cslike.lua: all guns from Counter-Strike, HE and Knife, perfectly for games with a medium amout of players
allweapons.lua: a long GunGame almost all weapons from CS2D, perfectly for games with many players
sniper.lua: 10 kills with Scout, 10 kills with AWP (good for AWP maps)
nades.lua: 10 kills with Molotov Cocktail, 10 kills with HE (good for HE maps)
melee.lua: 10 kills with Chainsaw, Machete, Claw and Knife
Installation Instructions
Extract the zip file into the main CS2D directory.
Then add this line into the file "server.lua" in the sub directory "sys/lua/" of CS2D:
The Game will now start everytime you start the server. If you don't want this, you also can start the game manually by typing this into console:
btw: Thanks to leegao and TMK for these small but useful scripts: string function, color function, msg rewrite
Download here! (current version: 1.1)
If you want, you can modify it for you own needs without asking me. All I want from you that you don't claim it is all work, you have to make clear, it was also my work.

That's what this game is about:



Features








Server functions
Type these in the console to use them as a server:
Code:
1
lua kogg_levelstealing=[i]true/false[/i]
true: allow knife in all levels and activate level stealing by knife kill
false: disallow knife in almost all levels and deactivate level stealing
Code:
1
lua kogg_reset()
resets all levels and kills of all players to 0, but doesn't restart round
Code:
1
lua kogg_mapchange=[i]true/false[/i]
This controls the behaviour of the game after a round ended:
true: change map according to the mapcycle
false: just restart round and keep map
Code:
1
lua kogg_defaultmode=[i]<game mode>[/i]
set default game mode for this GunGame (important if you want to override the set game mode in mapcycle.cfg.
<game mode> is either Standard, Deathmatch, TeamDeathmatch (without space), Construction or Zombies (without exclamation mark) (recommended are Deathmatch or TeamDeathmatch)
Code:
1
lua kogg_gunlevelsfile="[i]<filename>[/i]"
<filename> is the complete name of the gunlevels file. It must be in CS2D/sys/lua/KOGG/gunlevels/
preinstalled gunlevels files:
cslike.lua: all guns from Counter-Strike, HE and Knife, perfectly for games with a medium amout of players
allweapons.lua: a long GunGame almost all weapons from CS2D, perfectly for games with many players
sniper.lua: 10 kills with Scout, 10 kills with AWP (good for AWP maps)
nades.lua: 10 kills with Molotov Cocktail, 10 kills with HE (good for HE maps)
melee.lua: 10 kills with Chainsaw, Machete, Claw and Knife
Installation Instructions
Extract the zip file into the main CS2D directory.
Then add this line into the file "server.lua" in the sub directory "sys/lua/" of CS2D:
Code:
1
dofile("sys/lua/KOGG/KOGG.lua")
The Game will now start everytime you start the server. If you don't want this, you also can start the game manually by typing this into console:
Code:
1
lua dofile("sys/lua/KOGG/KOGG.lua")
btw: Thanks to leegao and TMK for these small but useful scripts: string function, color function, msg rewrite

edited 2×, last 09.04.09 06:11:10 pm
XMPP: Wuzzy2@jabber.ccc.de
@Kiffer-Opa
My PC become laggy when on your mod.. whats going on? other mod like normal only.. no lag.
My PC become laggy when on your mod.. whats going on? other mod like normal only.. no lag.
My curse will sail against the wind and enrage the earth.
go to line 215 where it says
This should decrease the lag for you, otherwise it's fine
Also, @Kiffer-Opa, try using a different technique for the parsing. Using string.sub alone may sometimes take too many conditions to fulfill and in the end just give you a huge unmanageable mess. An alternative way of doing this is as follows.
here for example, if we have been given a command such as:
"guns AK47" - we would do
as opposed to using substrings to find the different commands
ALSO, be very cautious here too, if you are taking in a number for an argument to parse, be sure to convert it using tonumber().
Code:
and change it to1
addhook("always","kogg_hud")
Code:
1
addhook("second","kogg_hud")
This should decrease the lag for you, otherwise it's fine
Also, @Kiffer-Opa, try using a different technique for the parsing. Using string.sub alone may sometimes take too many conditions to fulfill and in the end just give you a huge unmanageable mess. An alternative way of doing this is as follows.
Code:
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
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
function split(t, b)
return toTable(t, b)
end
function toTable(t, b)
local cmd = {}
local match = "[^%s]+"
if b then
match = "%w+"
end
if type(b) == "string" then match = "[^"..b.."]+" end
if not t then return cmd end
for word in string.gmatch(t, match) do
table.insert(cmd, word)
end
return cmd
end
function trim(t)
t = split(t)
s = ""
if not t then return end
for i, v in ipairs(t) do
s = s.." "..v
end
s = string.sub(s, 2)
return s
end
return toTable(t, b)
end
function toTable(t, b)
local cmd = {}
local match = "[^%s]+"
if b then
match = "%w+"
end
if type(b) == "string" then match = "[^"..b.."]+" end
if not t then return cmd end
for word in string.gmatch(t, match) do
table.insert(cmd, word)
end
return cmd
end
function trim(t)
t = split(t)
s = ""
if not t then return end
for i, v in ipairs(t) do
s = s.." "..v
end
s = string.sub(s, 2)
return s
end
here for example, if we have been given a command such as:
"guns AK47" - we would do
Code:
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
function example_say(p, t)
local _t = split(t)
local cmd = _t[1]
if not cmd then return nil end
if cmd == "guns"
local gun = _t[2] -- The default split automatically trims
if not gun then return nil end
if not type(_G[gun]) == "number" then return nil end
equip(p, _G[gun])
return 2
end
end
local _t = split(t)
local cmd = _t[1]
if not cmd then return nil end
if cmd == "guns"
local gun = _t[2] -- The default split automatically trims
if not gun then return nil end
if not type(_G[gun]) == "number" then return nil end
equip(p, _G[gun])
return 2
end
end
as opposed to using substrings to find the different commands
ALSO, be very cautious here too, if you are taking in a number for an argument to parse, be sure to convert it using tonumber().
edited 2×, last 07.04.09 05:53:30 am
09.04.09 06:08:56 pm
Here is my new version:
Download Kiffer-Opa's GunGame v1.1
It was a kinda dumb idea to refresh the HUD of 32 players every frame. That is probably the cause of slowing down CS2D. I re-programmed the HUD, now the speed should be OK.
If you download the new version you don't need the leegao's trick anymore.
New features: You lose 1 kill if you teamkill (only if the game mode is not Deathmatch)
And you also can use the GunGame in game mode "Standard".
And to the parse stuff: Maybe I add that later. In the meanwhile server still can use the "lua" commands I mentioned in my last post. But thanks for your suggestion, leegao.

It was a kinda dumb idea to refresh the HUD of 32 players every frame. That is probably the cause of slowing down CS2D. I re-programmed the HUD, now the speed should be OK.
If you download the new version you don't need the leegao's trick anymore.
New features: You lose 1 kill if you teamkill (only if the game mode is not Deathmatch)
And you also can use the GunGame in game mode "Standard".
And to the parse stuff: Maybe I add that later. In the meanwhile server still can use the "lua" commands I mentioned in my last post. But thanks for your suggestion, leegao.
edited 2×, last 09.04.09 06:22:01 pm
XMPP: Wuzzy2@jabber.ccc.de
10.04.09 01:10:10 am
This is mainly to help you work with text manipulation, especially when trying to get commands to work
For example, traditionally, you would be forced to use lots and lots of string.sub() to cut the text into its different components, giving you a function with dozens of if statements. What if you can do something like this:
Well, now you can. Forget about all of the string.subs and the hours wasted just trying to get the commands to be cutted out right, you will now be able to use string.split(text, delimiter) and string.qsplit(text) to do that for you. But there's more
FEATURES of string.lua
Revised:
tostring(object) - Returns String
EX:
Added:
string.join(text, delimiter) - Returns String (Same as tostring)
EX:
string.trim(text) - Returns String
EX:
string.split(text, delimiter) - Returns Table
EX:
string.capitalize(text) - Returns Text
EX:
string.letters(text) - Returns Table
EX:
string.word(text, sub) - Returns table
EX:
string.index(text, sub) - Returns table
EX:
string.translate(text, {subs}) - Returns string
EX:
string.qsplit(text) - Returns table
EX:
For example, traditionally, you would be forced to use lots and lots of string.sub() to cut the text into its different components, giving you a function with dozens of if statements. What if you can do something like this:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
addhook("say", "sayto")
function sayto(p, text)
--text = ' !say "Hello AMX2D" 5 '
text = string.trim(text)
--text = '!say "Hello AMX2D" 5'
text = string.qsplit(text)
--text = {"!say", "Hello AMX2D", "5"}
local cmd = text[1]
--cmd = "!say"
if cmd == "!say" then
local t = text[2]
--t = "Hello AMX2D"
local p = tonumber(text[3])
--p = 5
msg2(p, t)
--msg2(5, "Hello AMX2D")
end
end
function sayto(p, text)
--text = ' !say "Hello AMX2D" 5 '
text = string.trim(text)
--text = '!say "Hello AMX2D" 5'
text = string.qsplit(text)
--text = {"!say", "Hello AMX2D", "5"}
local cmd = text[1]
--cmd = "!say"
if cmd == "!say" then
local t = text[2]
--t = "Hello AMX2D"
local p = tonumber(text[3])
--p = 5
msg2(p, t)
--msg2(5, "Hello AMX2D")
end
end
Well, now you can. Forget about all of the string.subs and the hours wasted just trying to get the commands to be cutted out right, you will now be able to use string.split(text, delimiter) and string.qsplit(text) to do that for you. But there's more
Code:
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
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
function string.join(t, d)
if not (type(t) == "table") then return end
return tostring(t, d)
end
_tostring = tostring
function tostring(t, d)
if type(t) == "table" then
if not d then d = "," end
t = tableToString(t, 1, d)
t = string.sub(t, 1, #t-#d)
return t
end
if type(_tostring(t)) == "string" then return _tostring(t) end
end
function tableToString(t, n, d)
local s = ""
if (n == nil) then n = 1 end
if not d then d = " " end
while (n <= #t) do
s = s .. t[n] .. "".. d
n = n + 1
end
return s
end
function string.trim(t)
t = string.split(t)
s = ""
if not t then return end
for i, v in ipairs(t) do
s = s.." "..v
end
s = string.sub(s, 2)
return s
end
function string.split(t, b)
local cmd = {}
local match = "[^%s]+"
if b then
match = "%w+"
end
if type(b) == "string" then match = "[^"..b.."]+" end
if not t then return invalid() end
for word in string.gmatch(t, match) do
table.insert(cmd, word)
end
return cmd
end
function string.capitalize(t)
local str = string.split(t)
for i, word in ipairs(str) do
local eword = string.sub(word, 2)
local bword = string.upper(string.sub(word, 1, 1))
str[i] = bword..eword
end
return tostring(str, " ")
end
function string.letters(t)
local letters = {}
for word in string.gmatch(t, ".") do
table.insert(letters, word)
end
return letters
end
function string.word(t, sub)
local str = string.split(t)
sub = string.trim(sub)
local tab = {}
for i, word in ipairs(str) do
if word == sub then
table.insert(tab, i)
end
end
return tab
end
function string.index(t, sub)
local str = string.letters(t)
local n = #sub
local tab = {}
for i, letter in ipairs(str) do
if letter == string.sub(sub, 1, 1) then
local _let = letter
for _i = 1, n-1, 1 do
if not str[_i+i] == string.sub(sub, _i, _i) then break end
_let = _let .. str[_i+i]
end
if _let == sub then
table.insert(tab, i)
end
end
end
return tab
end
function string.translate(t, d)
if not (type(d) == "table") then d = string.split(d) end
for i, v in ipairs(d) do
d[tostring(v)] = true
end
local str = string.letters(t)
local _t = ""
for i, letter in ipairs(str) do
if not d[letter] then
_t = _t..letter
end
end
return _t
end
function string.qsplit(t)
local tab = string.split(t, '"')
if #tab == 1 then
return string.split(tab[1])
end
local ret = {}
for i, v in ipairs(tab) do
if i%2 == 0 then
table.insert(ret, v)
else
v = string.split(v)
for _i, _v in ipairs(v) do
table.insert(ret, _v)
end
end
end
return ret
end
--[-- Examples - Uncomment and Run
print(tostring({"1", "2", "3"}, " + "))
print(string.join({"1", "2", "3"}, " + "))
print(string.trim(" asdf sdf s dsf "))
print(string.split("say I love AMX2D"))
print(string.capitalize("testing something"))
print(string.letters("omfg oO"))
print(string.word("say I love AMX and AMX2D and AMX", "AMX"))
print(string.index("say I love AMX and AMX2D and AMX", "AMX"))
print(string.translate("say I love AMX and AMX2D and AMX", {"a"}))
print(string.qsplit("!say \"Hello AMX2D\" 5"))
--]]--
if not (type(t) == "table") then return end
return tostring(t, d)
end
_tostring = tostring
function tostring(t, d)
if type(t) == "table" then
if not d then d = "," end
t = tableToString(t, 1, d)
t = string.sub(t, 1, #t-#d)
return t
end
if type(_tostring(t)) == "string" then return _tostring(t) end
end
function tableToString(t, n, d)
local s = ""
if (n == nil) then n = 1 end
if not d then d = " " end
while (n <= #t) do
s = s .. t[n] .. "".. d
n = n + 1
end
return s
end
function string.trim(t)
t = string.split(t)
s = ""
if not t then return end
for i, v in ipairs(t) do
s = s.." "..v
end
s = string.sub(s, 2)
return s
end
function string.split(t, b)
local cmd = {}
local match = "[^%s]+"
if b then
match = "%w+"
end
if type(b) == "string" then match = "[^"..b.."]+" end
if not t then return invalid() end
for word in string.gmatch(t, match) do
table.insert(cmd, word)
end
return cmd
end
function string.capitalize(t)
local str = string.split(t)
for i, word in ipairs(str) do
local eword = string.sub(word, 2)
local bword = string.upper(string.sub(word, 1, 1))
str[i] = bword..eword
end
return tostring(str, " ")
end
function string.letters(t)
local letters = {}
for word in string.gmatch(t, ".") do
table.insert(letters, word)
end
return letters
end
function string.word(t, sub)
local str = string.split(t)
sub = string.trim(sub)
local tab = {}
for i, word in ipairs(str) do
if word == sub then
table.insert(tab, i)
end
end
return tab
end
function string.index(t, sub)
local str = string.letters(t)
local n = #sub
local tab = {}
for i, letter in ipairs(str) do
if letter == string.sub(sub, 1, 1) then
local _let = letter
for _i = 1, n-1, 1 do
if not str[_i+i] == string.sub(sub, _i, _i) then break end
_let = _let .. str[_i+i]
end
if _let == sub then
table.insert(tab, i)
end
end
end
return tab
end
function string.translate(t, d)
if not (type(d) == "table") then d = string.split(d) end
for i, v in ipairs(d) do
d[tostring(v)] = true
end
local str = string.letters(t)
local _t = ""
for i, letter in ipairs(str) do
if not d[letter] then
_t = _t..letter
end
end
return _t
end
function string.qsplit(t)
local tab = string.split(t, '"')
if #tab == 1 then
return string.split(tab[1])
end
local ret = {}
for i, v in ipairs(tab) do
if i%2 == 0 then
table.insert(ret, v)
else
v = string.split(v)
for _i, _v in ipairs(v) do
table.insert(ret, _v)
end
end
end
return ret
end
--[-- Examples - Uncomment and Run
print(tostring({"1", "2", "3"}, " + "))
print(string.join({"1", "2", "3"}, " + "))
print(string.trim(" asdf sdf s dsf "))
print(string.split("say I love AMX2D"))
print(string.capitalize("testing something"))
print(string.letters("omfg oO"))
print(string.word("say I love AMX and AMX2D and AMX", "AMX"))
print(string.index("say I love AMX and AMX2D and AMX", "AMX"))
print(string.translate("say I love AMX and AMX2D and AMX", {"a"}))
print(string.qsplit("!say \"Hello AMX2D\" 5"))
--]]--


Revised:
tostring(object) - Returns String
EX:
Code:
1
print(tostring({"1", "2", "3"}, " + "))
Code:
1
1 + 2 + 3
Added:
string.join(text, delimiter) - Returns String (Same as tostring)
EX:
Code:
1
string.join({"1", "2", "3"}, " + ")
Code:
1
1 + 2 + 3
string.trim(text) - Returns String
EX:
Code:
1
string.trim(" asdf sdf s dsf ")
Code:
1
asdf sdf s dsf
string.split(text, delimiter) - Returns Table
EX:
Code:
1
string.split("say I love AMX2D")
Code:
1
{"say", "I", "love", "AMX2D"}
string.capitalize(text) - Returns Text
EX:
Code:
1
string.capitalize("testing something")
Code:
1
[b]T[/b]esting [b]S[/b]omething
string.letters(text) - Returns Table
EX:
Code:
1
string.letters("omfg oO")
Code:
1
{"o", "m", "f", "g", " ", "o", "O"}
string.word(text, sub) - Returns table
EX:
Code:
1
string.word("say I love AMX and AMX2D and AMX", "AMX")
Code:
1
{4, 7}
string.index(text, sub) - Returns table
EX:
Code:
1
string.index("say I love AMX and AMX2D and AMX", "AMX")
Code:
1
{12, 20, 30}
string.translate(text, {subs}) - Returns string
EX:
Code:
1
string.translate("say I love AMX2D", {"a"})
Code:
1
"sy I love AMX2D"
string.qsplit(text) - Returns table
EX:
Code:
1
string.qsplit('guns "Player 1" AK47')
Code:
1
{"guns", "Player 1", "AK47"}
edited 1×, last 10.04.09 01:41:43 am
lol, thanks for replying, I needed someone to say something so I can post again
Struct.lua
This was a really difficult library for me to write in part because lua offers no structural packing and unpacking functions for lowlevel datatypes. Besides the four basic datatypes, there's not much else you can do.
Now, take something like sys/stats/userstats.dat, these are all written using 8-bit, 16-bit, and 32-bit characters, making it extremely difficult to manipulate these sorts of files in game.
Now back in the days of CS2D 0.1.0.4, 3rr0r was able to emulate the Blitz WriteByte, WriteInt, WriteLine, WriteShort, and etc + its read equivalent, as well as the stream-like behaviors, in Python in his udp_bb.py . This will do the exact same but in Lua.
struct.lua
Packs and Unpacks lua data into it's C-variant data structure. To facilitate building CS2D compatible data files, the data is treated as streams.
Samples usage:
1. Unpacking:
loadfile("struct.lua")()
2. Packing:
output:
Usage API:
1. Load the file using loadfile("struct.lua")()
2. Read Functions:
ReadByte - Unpacks the data and returns the byte at the pointer
ReadShort - Unpacks the first 2 bytes into C-Type short at the pointer
ReadInt - Unpacks the first 4 bytes into C-Type long at the pointer
ReadString - Unpacks the first byte at the pointer and then reads the number of characters in the stream given by that first byte
ReadLine - Read until it encounters the first "\n" (ASCII:10) character and returns the trimmed version of the string.
3. Write Functions
WriteByte - packs the Lua int into a byte and returns the char as a string
WriteShort - packs the Lua int into a Short and returns the 2 chars as a string
WriteInt - packs the Lua int into 4 bytes and returns the 4 chars as a string
WriteString - Packs the length of the string into a byte and appends the string
WriteLine - Appends a "\r" (ASCII:13) char followed by a "\n" (ASCII:10) char

Struct.lua
This was a really difficult library for me to write in part because lua offers no structural packing and unpacking functions for lowlevel datatypes. Besides the four basic datatypes, there's not much else you can do.
Now, take something like sys/stats/userstats.dat, these are all written using 8-bit, 16-bit, and 32-bit characters, making it extremely difficult to manipulate these sorts of files in game.
Now back in the days of CS2D 0.1.0.4, 3rr0r was able to emulate the Blitz WriteByte, WriteInt, WriteLine, WriteShort, and etc + its read equivalent, as well as the stream-like behaviors, in Python in his udp_bb.py . This will do the exact same but in Lua.
struct.lua
Packs and Unpacks lua data into it's C-variant data structure. To facilitate building CS2D compatible data files, the data is treated as streams.
Samples usage:
1. Unpacking:
loadfile("struct.lua")()
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
stream = "afadsf3"
print(ReadByte(stream)) -- Returns ASCII of 'a'
print(ReadByte(stream)) -- Returns ASCII of 'f'
print(ReadLine(stream)) -- Returns the stream from position 3
[code]
output:
[code]
97
103
adsf3
print(ReadByte(stream)) -- Returns ASCII of 'a'
print(ReadByte(stream)) -- Returns ASCII of 'f'
print(ReadLine(stream)) -- Returns the stream from position 3
[code]
output:
[code]
97
103
adsf3
2. Packing:
Code:
1
2
3
2
3
loadfile("struct.lua")()
print(WriteByte(97)..WriteByte(103)..WriteLine("testing"))
print(WriteByte(97)..WriteByte(103)..WriteLine("testing"))
output:
Code:
1
astest
Usage API:
1. Load the file using loadfile("struct.lua")()
2. Read Functions:
ReadByte - Unpacks the data and returns the byte at the pointer
ReadShort - Unpacks the first 2 bytes into C-Type short at the pointer
ReadInt - Unpacks the first 4 bytes into C-Type long at the pointer
ReadString - Unpacks the first byte at the pointer and then reads the number of characters in the stream given by that first byte
ReadLine - Read until it encounters the first "\n" (ASCII:10) character and returns the trimmed version of the string.
3. Write Functions
WriteByte - packs the Lua int into a byte and returns the char as a string
WriteShort - packs the Lua int into a Short and returns the 2 chars as a string
WriteInt - packs the Lua int into 4 bytes and returns the 4 chars as a string
WriteString - Packs the length of the string into a byte and appends the string
WriteLine - Appends a "\r" (ASCII:13) char followed by a "\n" (ASCII:10) char
Code:
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
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
--**********************--
--**********************--
--****-=Struct.lua=-****--
--**********************--
--**********************--
--*****************************************--
--This is a port of 3rr0r's bb_udp.py******--
--Note, lua does not have native support***--
--for C styled Data Structure packing******--
--so the packing logic have to be rewritten--
--*****************************************--
--[[
**Author: Lee Gao
**Website: http://tgvserver.info
**Title: Struct.lua
**Description: This is a tool to treat data as
a stream and uses pointers to traverse the "stream"
This is a port of the standard library developed by
3rr0r which was used in the development of his dedicated
servers.
--]]
--Initialization--
readcount = {}
--Utility Functions
function isIn(t, a)
for i,v in ipairs(a) do
if v == t then return true end
end
end
function s_delStream(t)
if readcount[t] then
readcount[t] = nil
end
end
function s_get(t)
if readcount[t] then
return string.sub(t,readcount[t])
else
readcount[t] = 1
return t
end
end
function s_update(t, ln)
readcount[t] = readcount[t] + ln
if readcount[t] > #t then
readcount[t] = 1
end
end
function getByte(t)
local t_ = {}
for i = 1, #t, 1 do
table.insert(t_, string.byte(t,i))
end
return t_
end
function isByte(n)
for i, v in ipairs(n) do
if v > 255 then return false end
end
return true
end
--Write Functions--
function WriteByte(p)
if p > 255 then p = 255 elseif p < 0 then p = 0 end
return(string.char(b1))
end
function WriteShort(p)
local b1 = p%256
local b2 = (p-b1)/256
--[[
Concept: A short is 2 bytes, the first byte denotes the lower
255 bytes and the 2nd denotes the upper 255^2 bytes.
]]
if not isByte({b1, b2}) then
return string.char(255)..string.char(255)
else
return(string.char(b1)..string.char(b2))
end
end
function WriteInt(p)
local b1 = p%256
local p2 = (p-b1)/256
local b2 = p2%256
local p3 = (p2-b2)/256
local b3 = p3%256
local b4 = (p3-b3)/256
--[[
Concept: Similar to short, but instead of 2 bytes,
there are 4 bytes. The 1st byte denotes the lower
255 bytes and the 2nd denotes the 255^2 bytes, the
3rd denotes the 255^3 bytes and the 4th denotes the
255^4
]]
if not isByte({b1, b2, b3, b4}) then
return string.char(255)..string.char(255)..string.char(255)..string.char(255)
end
return(string.char(b1)..string.char(b2)..string.char(b3)..string.char(b4))
end
function WriteLine(t)
return(t..string.char(13)..string.char(10))
end
function WriteString(t)
return(WriteByte(#t)..t)
end
--Read Functions--
function ReadByte(t)
local stream = s_get(t)
local byte = getByte(stream)[1]
s_update(t, 1)
return byte
end
function ReadShort(t)
local stream = s_get(t)
local b1 = getByte(stream)[1]
local b2 = getByte(stream)[2]
s_update(t, 2)
return b2*256+b1
end
function ReadInt(t)
local stream = s_get(t)
local b1 = getByte(stream)[1]
local b2 = getByte(stream)[2]
local b3 = getByte(stream)[3]
local b4 = getByte(stream)[4]
s_update(t, 4)
return b2*256+b1+b3*256*256+b4*256*256*256
end
function ReadLine(t)
s = s_get(t)
if string.find(s, "\n") then
i = string.find(s,"\n")
x = string.find(s, string.char(13))
if x then
s = string.sub(s, 1, i-2)
else
s = string.sub(s, 1, i-1)
end
s_update(t, i)
return s
else
s_update(t, #s)
return s
end
end
function ReadString(t,n)
if not n then
n = ReadByte(t)
end
s = s_get(t)
s = string.sub(s,1,n)
s_update(t, n)
return s
end
--**********************--
--****-=Struct.lua=-****--
--**********************--
--**********************--
--*****************************************--
--This is a port of 3rr0r's bb_udp.py******--
--Note, lua does not have native support***--
--for C styled Data Structure packing******--
--so the packing logic have to be rewritten--
--*****************************************--
--[[
**Author: Lee Gao
**Website: http://tgvserver.info
**Title: Struct.lua
**Description: This is a tool to treat data as
a stream and uses pointers to traverse the "stream"
This is a port of the standard library developed by
3rr0r which was used in the development of his dedicated
servers.
--]]
--Initialization--
readcount = {}
--Utility Functions
function isIn(t, a)
for i,v in ipairs(a) do
if v == t then return true end
end
end
function s_delStream(t)
if readcount[t] then
readcount[t] = nil
end
end
function s_get(t)
if readcount[t] then
return string.sub(t,readcount[t])
else
readcount[t] = 1
return t
end
end
function s_update(t, ln)
readcount[t] = readcount[t] + ln
if readcount[t] > #t then
readcount[t] = 1
end
end
function getByte(t)
local t_ = {}
for i = 1, #t, 1 do
table.insert(t_, string.byte(t,i))
end
return t_
end
function isByte(n)
for i, v in ipairs(n) do
if v > 255 then return false end
end
return true
end
--Write Functions--
function WriteByte(p)
if p > 255 then p = 255 elseif p < 0 then p = 0 end
return(string.char(b1))
end
function WriteShort(p)
local b1 = p%256
local b2 = (p-b1)/256
--[[
Concept: A short is 2 bytes, the first byte denotes the lower
255 bytes and the 2nd denotes the upper 255^2 bytes.
]]
if not isByte({b1, b2}) then
return string.char(255)..string.char(255)
else
return(string.char(b1)..string.char(b2))
end
end
function WriteInt(p)
local b1 = p%256
local p2 = (p-b1)/256
local b2 = p2%256
local p3 = (p2-b2)/256
local b3 = p3%256
local b4 = (p3-b3)/256
--[[
Concept: Similar to short, but instead of 2 bytes,
there are 4 bytes. The 1st byte denotes the lower
255 bytes and the 2nd denotes the 255^2 bytes, the
3rd denotes the 255^3 bytes and the 4th denotes the
255^4
]]
if not isByte({b1, b2, b3, b4}) then
return string.char(255)..string.char(255)..string.char(255)..string.char(255)
end
return(string.char(b1)..string.char(b2)..string.char(b3)..string.char(b4))
end
function WriteLine(t)
return(t..string.char(13)..string.char(10))
end
function WriteString(t)
return(WriteByte(#t)..t)
end
--Read Functions--
function ReadByte(t)
local stream = s_get(t)
local byte = getByte(stream)[1]
s_update(t, 1)
return byte
end
function ReadShort(t)
local stream = s_get(t)
local b1 = getByte(stream)[1]
local b2 = getByte(stream)[2]
s_update(t, 2)
return b2*256+b1
end
function ReadInt(t)
local stream = s_get(t)
local b1 = getByte(stream)[1]
local b2 = getByte(stream)[2]
local b3 = getByte(stream)[3]
local b4 = getByte(stream)[4]
s_update(t, 4)
return b2*256+b1+b3*256*256+b4*256*256*256
end
function ReadLine(t)
s = s_get(t)
if string.find(s, "\n") then
i = string.find(s,"\n")
x = string.find(s, string.char(13))
if x then
s = string.sub(s, 1, i-2)
else
s = string.sub(s, 1, i-1)
end
s_update(t, i)
return s
else
s_update(t, #s)
return s
end
end
function ReadString(t,n)
if not n then
n = ReadByte(t)
end
s = s_get(t)
s = string.sub(s,1,n)
s_update(t, n)
return s
end
11.04.09 09:45:45 pm
New features of Kiffer-Opa's GunGame v1.2:
U.S.G.N. users will keep their level when they re-join in the same round
fixed: you didn't receive the new weapon when you stole a level with the knife
http://www.unrealsoftware.de/files_show.php?file=1067



XMPP: Wuzzy2@jabber.ccc.de
better tables
http://amx2d.pbwiki.com/tables
Try using these
print("!Initial Values of test1d")
test1d = {a = 1, b=2, c=3, d = 3, 3, 2, 4, 2}
table.dump(test1d)
print("!Initial Values of sorted, which is test1d sorted via indices. Note the string keys are unsorted.")
sorted = table.ssort(test1d)
table.dump(sorted)
print("!Sorted using ssort to remove none-indices")
sorted = table.sort(test1d)
table.dump(sorted)
print("!The DIFF between test1d and sorted")
table.dump(table.diff(test1d, table.sort(test1d)))
print("!Inverted values of test1d")
test1d = table.invert(test1d)
table.dump(test1d)
print("!Inverted values of inverted values of test1d")
table.dump(table.invert(test1d, true))
print("!The DIFF between doubleinverted test1d and sorted")
table.dump(table.diff(table.invert(test1d, true), sorted))
print("!Inverted values of inverted values of test1d with smart keys")
test1d = table.invert(test1d)
table.dump(test1d)
print("!Initial Values of _2d matrix")
_2d = {
lee = {score = 1, rank = 3},
joe = {score = 2, rank = 2},
bob = {score = 5, rank = 1}
}
table.dump(_2d)
print("!_2d with its keys stripped as 'name'")
_2d = table.stripkeys(_2d, "name")
table.dump(_2d)
print("!_2d with its 'score' column set as the keys")
_2d = table.setkey(_2d, "score", true)
table.dump(_2d)
print("!Returns a table of the values of column 'score'")
table.dump(table.get(_2d, "score"))
print("!Returns a table of the values of column 'rank'")
table.dump(table.get(_2d, "rank"))
Actual Code
http://amx2d.pbwiki.com/tables
Try using these
print("!Initial Values of test1d")
test1d = {a = 1, b=2, c=3, d = 3, 3, 2, 4, 2}
table.dump(test1d)
print("!Initial Values of sorted, which is test1d sorted via indices. Note the string keys are unsorted.")
sorted = table.ssort(test1d)
table.dump(sorted)
print("!Sorted using ssort to remove none-indices")
sorted = table.sort(test1d)
table.dump(sorted)
print("!The DIFF between test1d and sorted")
table.dump(table.diff(test1d, table.sort(test1d)))
print("!Inverted values of test1d")
test1d = table.invert(test1d)
table.dump(test1d)
print("!Inverted values of inverted values of test1d")
table.dump(table.invert(test1d, true))
print("!The DIFF between doubleinverted test1d and sorted")
table.dump(table.diff(table.invert(test1d, true), sorted))
print("!Inverted values of inverted values of test1d with smart keys")
test1d = table.invert(test1d)
table.dump(test1d)
print("!Initial Values of _2d matrix")
_2d = {
lee = {score = 1, rank = 3},
joe = {score = 2, rank = 2},
bob = {score = 5, rank = 1}
}
table.dump(_2d)
print("!_2d with its keys stripped as 'name'")
_2d = table.stripkeys(_2d, "name")
table.dump(_2d)
print("!_2d with its 'score' column set as the keys")
_2d = table.setkey(_2d, "score", true)
table.dump(_2d)
print("!Returns a table of the values of column 'score'")
table.dump(table.get(_2d, "score"))
print("!Returns a table of the values of column 'rank'")
table.dump(table.get(_2d, "rank"))
Output has written:
!Initial Values of test1d
[Table:test1d]
1 = 3
2 = 2
3 = 4
4 = 2
a = 1
d = 3
c = 3
b = 2
!Initial Values of sorted, which is test1d sorted via indices. Note the string keys are unsorted.
[Table:sorted]
1 = 2
2 = 2
3 = 3
4 = 4
a = 1
d = 3
c = 3
b = 2
!Sorted using ssort to remove none-indices
[Table:sorted]
1 = 2
2 = 2
3 = 3
4 = 4
!The DIFF between test1d and sorted
[Table:(Anonymous)]
removed = {a = 1, d = 3, c = 3, b = 2}
same = {2 = 2}
Tables = {t2 = '', t1 = ''}
changed = {1 = [T], 3 = [T], 4 = [T], a = [T], d = [T], c = [T], b = [T]}
1 = {new = 2, old = 3}
3 = {new = 3, old = 4}
4 = {new = 4, old = 2}
a = {old = 1}
d = {old = 3}
c = {old = 3}
b = {old = 2}
new = {}
!Inverted values of test1d
[Table:test1d]
1 = a
2 = {1 = 2, 2 = 4, 3 = 'b'}
3 = {1 = 1, 2 = 'd', 3 = 'c'}
4 = 3
!Inverted values of inverted values of test1d
[Table:(Anonymous)]
a = 1
{1 = 2, 2 = 4, 3 = 'b'} = 2
3 = 4
{1 = 1, 2 = 'd', 3 = 'c'} = 3
!The DIFF between doubleinverted test1d and sorted
[Table:(Anonymous)]
removed = {a = 1, table: 002C5960 = 2, table: 002C59B0 = 3}
same = {}
Tables = {t2 = 'sorted', t1 = 'Anon:T1'}
changed = {a = [T], table: 002C5960 = [T], table: 002C59B0 = [T], 3 = [T]}
a = {old = 1}
{1 = 2, 2 = 4, 3 = 'b'} = {old = 2}
{1 = 1, 2 = 'd', 3 = 'c'} = {old = 3}
3 = {new = 3, old = 4}
new = {1 = 2, 2 = 2, 4 = 4}
!Inverted values of inverted values of test1d with smart keys
[Table:test1d]
1 = 3
2 = 2
3 = 4
4 = 2
a = 1
d = 3
c = 3
b = 2
!Initial Values of _2d matrix
[Table:_2d]
lee = {score = 1, rank = 3}
joe = {score = 2, rank = 2}
bob = {score = 5, rank = 1}
!_2d with its keys stripped as 'name'
[Table:_2d]
1 = {name = 'lee', score = 1, rank = 3}
2 = {name = 'joe', score = 2, rank = 2}
3 = {name = 'bob', score = 5, rank = 1}
!_2d with its 'score' column set as the keys
[Table:_2d]
1 = {name = 'lee', score = 1, rank = 3}
2 = {name = 'joe', score = 2, rank = 2}
5 = {name = 'bob', score = 5, rank = 1}
!Returns a table of the values of column 'score'
[Table:(Anonymous)]
1 = {key = 1, value = 1}
2 = {key = 2, value = 2}
3 = {key = 5, value = 5}
!Returns a table of the values of column 'rank'
[Table:(Anonymous)]
1 = {key = 1, value = 3}
2 = {key = 2, value = 2}
3 = {key = 5, value = 1}
[Table:test1d]
1 = 3
2 = 2
3 = 4
4 = 2
a = 1
d = 3
c = 3
b = 2
!Initial Values of sorted, which is test1d sorted via indices. Note the string keys are unsorted.
[Table:sorted]
1 = 2
2 = 2
3 = 3
4 = 4
a = 1
d = 3
c = 3
b = 2
!Sorted using ssort to remove none-indices
[Table:sorted]
1 = 2
2 = 2
3 = 3
4 = 4
!The DIFF between test1d and sorted
[Table:(Anonymous)]
removed = {a = 1, d = 3, c = 3, b = 2}
same = {2 = 2}
Tables = {t2 = '', t1 = ''}
changed = {1 = [T], 3 = [T], 4 = [T], a = [T], d = [T], c = [T], b = [T]}
1 = {new = 2, old = 3}
3 = {new = 3, old = 4}
4 = {new = 4, old = 2}
a = {old = 1}
d = {old = 3}
c = {old = 3}
b = {old = 2}
new = {}
!Inverted values of test1d
[Table:test1d]
1 = a
2 = {1 = 2, 2 = 4, 3 = 'b'}
3 = {1 = 1, 2 = 'd', 3 = 'c'}
4 = 3
!Inverted values of inverted values of test1d
[Table:(Anonymous)]
a = 1
{1 = 2, 2 = 4, 3 = 'b'} = 2
3 = 4
{1 = 1, 2 = 'd', 3 = 'c'} = 3
!The DIFF between doubleinverted test1d and sorted
[Table:(Anonymous)]
removed = {a = 1, table: 002C5960 = 2, table: 002C59B0 = 3}
same = {}
Tables = {t2 = 'sorted', t1 = 'Anon:T1'}
changed = {a = [T], table: 002C5960 = [T], table: 002C59B0 = [T], 3 = [T]}
a = {old = 1}
{1 = 2, 2 = 4, 3 = 'b'} = {old = 2}
{1 = 1, 2 = 'd', 3 = 'c'} = {old = 3}
3 = {new = 3, old = 4}
new = {1 = 2, 2 = 2, 4 = 4}
!Inverted values of inverted values of test1d with smart keys
[Table:test1d]
1 = 3
2 = 2
3 = 4
4 = 2
a = 1
d = 3
c = 3
b = 2
!Initial Values of _2d matrix
[Table:_2d]
lee = {score = 1, rank = 3}
joe = {score = 2, rank = 2}
bob = {score = 5, rank = 1}
!_2d with its keys stripped as 'name'
[Table:_2d]
1 = {name = 'lee', score = 1, rank = 3}
2 = {name = 'joe', score = 2, rank = 2}
3 = {name = 'bob', score = 5, rank = 1}
!_2d with its 'score' column set as the keys
[Table:_2d]
1 = {name = 'lee', score = 1, rank = 3}
2 = {name = 'joe', score = 2, rank = 2}
5 = {name = 'bob', score = 5, rank = 1}
!Returns a table of the values of column 'score'
[Table:(Anonymous)]
1 = {key = 1, value = 1}
2 = {key = 2, value = 2}
3 = {key = 5, value = 5}
!Returns a table of the values of column 'rank'
[Table:(Anonymous)]
1 = {key = 1, value = 3}
2 = {key = 2, value = 2}
3 = {key = 5, value = 1}
Actual Code
Code:
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
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
if not _tostring then _tostring = tostring end
local _table = {}
for k, v in pairs(table) do _table[k] = v end
function table.find(t, o)
for k, v in pairs(t) do
if v == o then
return k
end
end
end
function table.seperate(t)
local itab = {}
local tab = {}
for k, v in pairs(t) do
if type(k) == "number" then
itab[k] = v
else
tab[k] = v
end
end
return itab, tab
end
function table.invert(t, _i)
local tab = {}
for k, v in pairs(t) do
if type(v) == "table" and not _i then
for _k, _v in pairs(v) do
tab[_v] = k
end
else
if not tab[v] then
tab[v] = k
else
if not (type(tab[v]) == "table") then
tab[v] = {tab[v], k}
else
table.insert(tab[v], k)
end
end
end
end
return tab
end
function table.length(t)
local n = 0
for k, v in pairs(t) do
n = n+1
end
return n
end
function table.ascend(x, y)
return x<y
end
function table.descend(x, y)
return x>y
end
function table.ssort(t,f)
if not f then f = table.ascend end
local i=1
local x, _x = table.seperate(t)
local n = table.length(x)
while i<=n do
local m,j=i,i+1
while j<=n do
if f(x[j],x[m]) then m=j end
j=j+1
end
x[i],x[m]=x[m],x[i] -- swap x[i] and x[m]
i=i+1
end
return table.join(x, _x, false)
end
function table.sort(t, f)
return table.seperate(table.ssort(t, f))
end
function table.first(t)
for k, v in pairs(t) do
return k, v
end
end
function table.sfirst(t)
for k, v in pairs(t) do
if type(k) == "string" then
return k, v
end
end
end
function table.ifirst(t)
for k, v in pairs(t) do
if type(k) == "number" then
return k, v
end
end
end
function table.firstVal(t)
for k, v in pairs(t) do
return v
end
end
function table.sfirstVal(t)
for k, v in pairs(t) do
if type(k) == "string" then
return v
end
end
end
function table.ifirstVal(t)
for k, v in pairs(t) do
if type(k) == "number" then
return v
end
end
end
function table.join(t1, t2, o)
local tab = t1
for k, v in pairs(t2) do
if (t1[k] and o) or not (t1[k]) then
tab[k] = v
end
end
return tab
end
local seen = {}
local _s_ = ""
function table.dump(t,i, b)
seen[t]=true
if not i then i = "" end
for k,v in pairs(t) do
local _v, _b = table.tostring(v)
_s_ = _s_..i.._tostring(table.tostring(k)).." = ".._tostring(_v).." \n"
if _b and not seen[v] then
table.dump(v, i.."\t", true)
end
end
if not b then
local name = table.find(_G, t)
if not name then name = "(Anonymous)" end
_s_ = "[Table:"..name.. "]\n".._s_
print (_s_)
local r = _s_
_s_ = ""
seen = {}
return r
end
end
function table.tostring(t)
if not (type(t) == "table") then if type(t) == "function" then return "fn()" end return _tostring(t) end
local s = "{"
local b = false
for k, v in pairs(t) do
if type(v) == "string" then v = "'"..v.."'" end
if type(v) == "function" then v= "fn()" end
if type(v) == "table" then
v = "[T]"
b = true
end
s = s .. _tostring(k) .. " = " .. _tostring(v) .. ", "
end
s = string.sub(s, 1, #s - 2).."}"
return s, b
end
function table.stripkeys(t, n)
local tab = {}
if not n then n = "key" end
for k, v in pairs(t) do
if isTable(v) then
v[n] = k
table.insert(tab,v)
end
end
return tab
end
function table.getkeys(t)
local tab = {}
for k, v in pairs(t) do
tab[k] = true
end
return tab
end
function table.ismatrix(t)
local _k, _v = table.first(t)
if not (type(_v) == "table") then return false end
local keys = table.getkeys(_v)
for k, v in pairs(t) do
if not (type(v) == "table") then return false end
if not table.havekeys(table.getkeys(v), keys) then return false end
end
return true
end
function table.equal(t1, t2)
for k, v in pairs(t1) do
if t2[k] ~= v then return false end
end
return true
end
function table.havekeys(t1, t2)
for k, v in pairs(t1) do
if not t2[k] then return false end
end
return true
end
function table.haskey(t, k)
if not t[k] then return false end
return true
end
function table.setkey(t, k, p)
if not table.ismatrix(t) then return t end
if not table.haskey(table.firstVal(t), k) then return t end
local tab = {}
for i, v in pairs(t) do
tab[v[k]] = v
if not p then
tab[v[k]][k] = nil
end
end
return tab
end
function table.get(t, key)
if not table.ismatrix(t) then return t end
if not table.haskey(table.firstVal(t), key) then return t end
local tab = {}
for k, v in pairs(t) do
table.insert(tab, {key = k, value = v[key]})
end
return tab
end
function table.diff(t1, t2, b)
local tab1, tab2 = "", ""
if not b then
tab1 = table.find(_G, t1)
tab2 = table.find(_G, t2)
end
if not tab1 then tab1 = "Anon:T1" end
if not tab2 then tab2 = "Anon:T2" end
local changed = {}
local removed = {}
local new = {}
local same = {}
for k, v in pairs(t1) do
if not t2[k] then
removed[k] = v
end
if not (t2[k] == v) then
changed[k] = {old = v, new = t2[k]}
end
if t2[k] == t1[k] then
same[k] = v
end
end
for k, v in pairs(t2) do
if not (changed[k] or removed[k] or same[k]) then
new[k] = v
end
end
return {changed = changed, removed = removed, new = new, same = same, Tables = {t1 = tab1, t2 = tab2}}
end
function isTable(t)
if type(t) == "table" then return true end
end
--[--
print("!Initial Values of test1d")
test1d = {a = 1, b=2, c=3, d = 3, 3, 2, 4, 2}
table.dump(test1d)
print("!Initial Values of sorted, which is test1d sorted via indices. Note the string keys are unsorted.")
sorted = table.ssort(test1d)
table.dump(sorted)
print("!Sorted using ssort to remove none-indices")
sorted = table.sort(test1d)
table.dump(sorted)
print("!The DIFF between test1d and sorted")
table.dump(table.diff(test1d, table.sort(test1d)))
print("!Inverted values of test1d")
test1d = table.invert(test1d)
table.dump(test1d)
print("!Inverted values of inverted values of test1d")
table.dump(table.invert(test1d, true))
print("!The DIFF between doubleinverted test1d and sorted")
table.dump(table.diff(table.invert(test1d, true), sorted))
print("!Inverted values of inverted values of test1d with smart keys")
test1d = table.invert(test1d)
table.dump(test1d)
print("!Initial Values of _2d matrix")
_2d = {
lee = {score = 1, rank = 3},
joe = {score = 2, rank = 2},
bob = {score = 5, rank = 1}
}
table.dump(_2d)
print("!_2d with its keys stripped as 'name'")
_2d = table.stripkeys(_2d, "name")
table.dump(_2d)
print("!_2d with its 'score' column set as the keys")
_2d = table.setkey(_2d, "score", true)
table.dump(_2d)
print("!Returns a table of the values of column 'score'")
table.dump(table.get(_2d, "score"))
print("!Returns a table of the values of column 'rank'")
table.dump(table.get(_2d, "rank"))
--]]--
local _table = {}
for k, v in pairs(table) do _table[k] = v end
function table.find(t, o)
for k, v in pairs(t) do
if v == o then
return k
end
end
end
function table.seperate(t)
local itab = {}
local tab = {}
for k, v in pairs(t) do
if type(k) == "number" then
itab[k] = v
else
tab[k] = v
end
end
return itab, tab
end
function table.invert(t, _i)
local tab = {}
for k, v in pairs(t) do
if type(v) == "table" and not _i then
for _k, _v in pairs(v) do
tab[_v] = k
end
else
if not tab[v] then
tab[v] = k
else
if not (type(tab[v]) == "table") then
tab[v] = {tab[v], k}
else
table.insert(tab[v], k)
end
end
end
end
return tab
end
function table.length(t)
local n = 0
for k, v in pairs(t) do
n = n+1
end
return n
end
function table.ascend(x, y)
return x<y
end
function table.descend(x, y)
return x>y
end
function table.ssort(t,f)
if not f then f = table.ascend end
local i=1
local x, _x = table.seperate(t)
local n = table.length(x)
while i<=n do
local m,j=i,i+1
while j<=n do
if f(x[j],x[m]) then m=j end
j=j+1
end
x[i],x[m]=x[m],x[i] -- swap x[i] and x[m]
i=i+1
end
return table.join(x, _x, false)
end
function table.sort(t, f)
return table.seperate(table.ssort(t, f))
end
function table.first(t)
for k, v in pairs(t) do
return k, v
end
end
function table.sfirst(t)
for k, v in pairs(t) do
if type(k) == "string" then
return k, v
end
end
end
function table.ifirst(t)
for k, v in pairs(t) do
if type(k) == "number" then
return k, v
end
end
end
function table.firstVal(t)
for k, v in pairs(t) do
return v
end
end
function table.sfirstVal(t)
for k, v in pairs(t) do
if type(k) == "string" then
return v
end
end
end
function table.ifirstVal(t)
for k, v in pairs(t) do
if type(k) == "number" then
return v
end
end
end
function table.join(t1, t2, o)
local tab = t1
for k, v in pairs(t2) do
if (t1[k] and o) or not (t1[k]) then
tab[k] = v
end
end
return tab
end
local seen = {}
local _s_ = ""
function table.dump(t,i, b)
seen[t]=true
if not i then i = "" end
for k,v in pairs(t) do
local _v, _b = table.tostring(v)
_s_ = _s_..i.._tostring(table.tostring(k)).." = ".._tostring(_v).." \n"
if _b and not seen[v] then
table.dump(v, i.."\t", true)
end
end
if not b then
local name = table.find(_G, t)
if not name then name = "(Anonymous)" end
_s_ = "[Table:"..name.. "]\n".._s_
print (_s_)
local r = _s_
_s_ = ""
seen = {}
return r
end
end
function table.tostring(t)
if not (type(t) == "table") then if type(t) == "function" then return "fn()" end return _tostring(t) end
local s = "{"
local b = false
for k, v in pairs(t) do
if type(v) == "string" then v = "'"..v.."'" end
if type(v) == "function" then v= "fn()" end
if type(v) == "table" then
v = "[T]"
b = true
end
s = s .. _tostring(k) .. " = " .. _tostring(v) .. ", "
end
s = string.sub(s, 1, #s - 2).."}"
return s, b
end
function table.stripkeys(t, n)
local tab = {}
if not n then n = "key" end
for k, v in pairs(t) do
if isTable(v) then
v[n] = k
table.insert(tab,v)
end
end
return tab
end
function table.getkeys(t)
local tab = {}
for k, v in pairs(t) do
tab[k] = true
end
return tab
end
function table.ismatrix(t)
local _k, _v = table.first(t)
if not (type(_v) == "table") then return false end
local keys = table.getkeys(_v)
for k, v in pairs(t) do
if not (type(v) == "table") then return false end
if not table.havekeys(table.getkeys(v), keys) then return false end
end
return true
end
function table.equal(t1, t2)
for k, v in pairs(t1) do
if t2[k] ~= v then return false end
end
return true
end
function table.havekeys(t1, t2)
for k, v in pairs(t1) do
if not t2[k] then return false end
end
return true
end
function table.haskey(t, k)
if not t[k] then return false end
return true
end
function table.setkey(t, k, p)
if not table.ismatrix(t) then return t end
if not table.haskey(table.firstVal(t), k) then return t end
local tab = {}
for i, v in pairs(t) do
tab[v[k]] = v
if not p then
tab[v[k]][k] = nil
end
end
return tab
end
function table.get(t, key)
if not table.ismatrix(t) then return t end
if not table.haskey(table.firstVal(t), key) then return t end
local tab = {}
for k, v in pairs(t) do
table.insert(tab, {key = k, value = v[key]})
end
return tab
end
function table.diff(t1, t2, b)
local tab1, tab2 = "", ""
if not b then
tab1 = table.find(_G, t1)
tab2 = table.find(_G, t2)
end
if not tab1 then tab1 = "Anon:T1" end
if not tab2 then tab2 = "Anon:T2" end
local changed = {}
local removed = {}
local new = {}
local same = {}
for k, v in pairs(t1) do
if not t2[k] then
removed[k] = v
end
if not (t2[k] == v) then
changed[k] = {old = v, new = t2[k]}
end
if t2[k] == t1[k] then
same[k] = v
end
end
for k, v in pairs(t2) do
if not (changed[k] or removed[k] or same[k]) then
new[k] = v
end
end
return {changed = changed, removed = removed, new = new, same = same, Tables = {t1 = tab1, t2 = tab2}}
end
function isTable(t)
if type(t) == "table" then return true end
end
--[--
print("!Initial Values of test1d")
test1d = {a = 1, b=2, c=3, d = 3, 3, 2, 4, 2}
table.dump(test1d)
print("!Initial Values of sorted, which is test1d sorted via indices. Note the string keys are unsorted.")
sorted = table.ssort(test1d)
table.dump(sorted)
print("!Sorted using ssort to remove none-indices")
sorted = table.sort(test1d)
table.dump(sorted)
print("!The DIFF between test1d and sorted")
table.dump(table.diff(test1d, table.sort(test1d)))
print("!Inverted values of test1d")
test1d = table.invert(test1d)
table.dump(test1d)
print("!Inverted values of inverted values of test1d")
table.dump(table.invert(test1d, true))
print("!The DIFF between doubleinverted test1d and sorted")
table.dump(table.diff(table.invert(test1d, true), sorted))
print("!Inverted values of inverted values of test1d with smart keys")
test1d = table.invert(test1d)
table.dump(test1d)
print("!Initial Values of _2d matrix")
_2d = {
lee = {score = 1, rank = 3},
joe = {score = 2, rank = 2},
bob = {score = 5, rank = 1}
}
table.dump(_2d)
print("!_2d with its keys stripped as 'name'")
_2d = table.stripkeys(_2d, "name")
table.dump(_2d)
print("!_2d with its 'score' column set as the keys")
_2d = table.setkey(_2d, "score", true)
table.dump(_2d)
print("!Returns a table of the values of column 'score'")
table.dump(table.get(_2d, "score"))
print("!Returns a table of the values of column 'rank'")
table.dump(table.get(_2d, "rank"))
--]]--
i can't get this one to work. Kinda C&P
Why its not showing spaces?
Code:
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
addhook("say","player_say")
function player_say(id,txt)
if(txt=="!usp") then
parse("equip "..id.." 1")
if(txt=="!glock") then
parse("equip "..id.." 2")
if(txt=="!deagle") then
parse("equip "..id.." 3")
end
end
function player_say(id,txt)
if(txt=="!usp") then
parse("equip "..id.." 1")
if(txt=="!glock") then
parse("equip "..id.." 2")
if(txt=="!deagle") then
parse("equip "..id.." 3")
end
end
Why its not showing spaces?
edited 1×, last 12.04.09 01:43:59 pm
aww! please ALWAYS use tab to format your scripts. otherwise they are pretty unreadable!
I did it for you:
and now you should notice that there are some "end"s missing. it's totally obvious now. each if needs an end (or else or elseif).
everything else seems to be correct.
I did it for you:
Code:
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
addhook("say","player_say")
function player_say(id,txt)
if(txt=="!usp") then
parse("equip "..id.." 1")
if(txt=="!glock") then
parse("equip "..id.." 2")
if(txt=="!deagle") then
parse("equip "..id.." 3")
end
end
function player_say(id,txt)
if(txt=="!usp") then
parse("equip "..id.." 1")
if(txt=="!glock") then
parse("equip "..id.." 2")
if(txt=="!deagle") then
parse("equip "..id.." 3")
end
end
and now you should notice that there are some "end"s missing. it's totally obvious now. each if needs an end (or else or elseif).
everything else seems to be correct.
Thanks DC, but its still not working. I don't know where to put that ends.
I tried like this:
I tried like this:
Code:
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
addhook("say","player_say")
function player_say(id,txt)
if(txt=="!usp") then
parse("equip "..id.." 1")end
if(txt=="!glock") then
parse("equip "..id.." 2")end
if(txt=="!deagle") then
parse("equip "..id.." 3")end
end
end
function player_say(id,txt)
if(txt=="!usp") then
parse("equip "..id.." 1")end
if(txt=="!glock") then
parse("equip "..id.." 2")end
if(txt=="!deagle") then
parse("equip "..id.." 3")end
end
end