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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
vl={}
function initArray2(f,v)
	local cmd={}
	for c=1,f do
		cmd[c]=v
	end
	return cmd
end
function doublearray(m,n,k)
local a, x, y = {}
for x = 0, m do a[x] = {}
for y = 0, n do
a[x][y] = k or 0
end
end
return a
end
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
vl.buttonstyles={
	[1] = {
		name="Default",
		image="gfx/button.png",
		hover="gfx/buttonhover.png",
		click="gfx/buttonclick.png",
		sizex=112,
		sizey=34,
		txtr=0,
		txtg=0,
		txtb=0,
		ox=-56,
		oy=-9,
	},
	[2] = {
		name="CS2D Close X",
		image="gfx/ui/cs2d_close.png",
		hover="gfx/ui/cs2d_close_hover.png",
		click="gfx/ui/cs2d_close_hover.png",
		sizex=14,
		sizey=14,
	},
	[3] = {
		name="CS2D Regular Button",
		image="gfx/ui/button_cs2d.png",
		hover="gfx/ui/button_cs2d_hover.png",
		click="gfx/ui/button_cs2d_hover.png",
		sizex=240,
		sizey=25,
	},
}
vl.windowstyles={
	[1] = {
		name="Default",
		image="gfx/sprites/block.bmp",
		sizex=32,
		sizey=32,
		bgr=255,
		bgg=255,
		bgb=255,
		brr=210,
		brg=216,
		brb=221,
		txtr=0,
		txtg=0,
		txtb=0,
		hlr=0,
		hlg=0,
		hlb=174,
	},
	[2] = {
		name="CS2D Window",
		image="gfx/ui/cs2d_window.png",
		sizex=617,
		sizey=455,
		bgr=255,
		bgg=255,
		bgb=255,
		brr=210,
		brg=216,
		brb=221,
		txtr=119,
		txtg=119,
		txtb=119,
		hlr=160,
		hlg=160,
		hlb=160,
	},
}
vl.currentTextID=1
vl.textpos=doublearray(1024,2,0)
vl.texttext=initArray2(1024,"")
vl.textcolor=initArray2(1024,"")
vl.plyInRange=initArray2(32,0)
vl.buttonimg=initArray2(1024,0)
vl.buttonpos=doublearray(1024,2,0)
vl.buttonsize=doublearray(1024,2,0)
vl.buttonply=initArray2(1024,0)
vl.buttonstyle=initArray2(1024,0)
vl.buttonfunc=initArray2(1024,nil)
vl.buttonhoveredupon=initArray2(1024,false)
vl.buttoncaption=initArray2(1024,0)
vl.buttonclicker=initArray2(1024,0)
vl.windowimg=initArray2(1024,0)
vl.windowbr=initArray2(1024,0)
vl.windowbr2=initArray2(1024,0)
vl.windowpos=doublearray(1024,2,0)
vl.windowsize=doublearray(1024,2,0)
vl.windowply=initArray2(1024,0)
vl.windowhl=initArray2(1024,0)
function vl.createWindow(id,ply,x,y,scalex,scaley,style,border,headline)
	if border==nil then border=0 end
	if ply==0 then ply=nil end
	if (border) then
		vl.windowbr[id]=image(vl.windowstyles
[style].image,x,y,2,ply)
		vl.windowbr2[id]=image(vl.windowstyles
[style].image,x,y,2,ply)
	end
	vl.windowimg[id]=image(vl.windowstyles[style].image,x,y,2,ply)
	imagescale(vl.windowimg[id],scalex,scaley)
	imagecolor(vl.windowimg[id],vl.windowstyles
[style].bgr,vl.windowstyles[style].bgg,vl.windowstyles[style].bgb)
	vl.windowply[id]=ply
	vl.windowpos[id][1]=x
	vl.windowpos[id][2]=y
	vl.windowsize[id][1]=vl.windowstyles[style].sizex*scalex
	vl.windowsize[id][2]=vl.windowstyles[style].sizey*scaley
	if (border) then
		local offsety=(vl.windowstyles[style].sizey*(scaley
+((scaley/60)*2)))-(vl.windowstyles[style].sizey*scaley)
		imagescale(vl.windowbr[id],scalex+(scalex/120),(scaley
+(scaley/60))+((scaley/60)*3.5))
		imagepos(vl.windowbr[id],x,y-offsety,0)
		imagecolor(vl.windowbr[id],vl.windowstyles
[style].brr,vl.windowstyles[style].brg,vl.windowstyles[style].brb)
		
		imagescale(vl.windowbr2[id],scalex+(scalex/120),scaley
+(scaley/120))
		imagepos(vl.windowbr2[id],x,y,0)
		imagecolor(vl.windowbr2[id],0,0,0)
		
		if (headline~=nil) then
			if (scaley>=6) then
				vl.windowhl[id]=vl.createText
(ply,colorcode(vl.windowstyles[style].hlr,vl.windowstyles
[style].hlg,vl.windowstyles[style].hlb),headline,x-(vl.windowsize[id]
[1]/2)+24,y-(vl.windowsize[id][2]/2)-(offsety*2.1))
			else
				print("©255000000VisualLua
exception - headline for window #"..id.." could not be created (window size too
small)")
			end
		end
	end
	if (not border and headline) then
		print("©255000000VisualLua exception - headline for
window #"..id.." could not be created (no border)")
	end
end
-- Function: closeWindow
-- Arguments: id - window ID
-- Possible argument values: id - 1-1024
function vl.closeWindow(id)
	freeimage(vl.windowimg[id])
	vl.windowimg[id]=0
	freeimage(vl.windowbr[id])
	vl.windowbr[id]=0
	freeimage(vl.windowbr2[id])
	vl.windowbr2[id]=0
	vl.removeText(vl.windowhl[id])
	vl.windowhl[id]=0
	vl.windowply[id]=0
	vl.windowpos[id][1]=0
	vl.windowpos[id][2]=0
	vl.windowsize[id][1]=0	
	vl.windowsize[id][2]=0
end
-- Function: createText
-- Arguments: ply - player to be shown the text, color - the text color, text - the text
itself, x - pixel X coordinate on the screen, y - pixel Y coordinate on the screen
-- Possible argument values: ply - 1-32, color - "000000000"-"255255255" (any
combination), text - any string, x - 0-640, y - 0-480
function vl.createText(ply,color,text,x,y)
	local formattext=("©"..color.." "..text)
	local tid=vl.currentTextID
	if ply == nil then ply = 1 end
	parse('hudtxt2 '..ply..' '..vl.currentTextID..' '..formattext..' '..x..' '..y..'')
	vl.textpos[vl.currentTextID][1]=x
	vl.textpos[vl.currentTextID][2]=y
	vl.texttext[vl.currentTextID]=text
	vl.textcolor[vl.currentTextID]=color
	vl.currentTextID=vl.currentTextID+1
	return tid
end
-- Function: removeText
-- Arguments: id - text ID
-- Possible argument values: id - 1-1024
function vl.removeText(id)
	parse('hudtxt2 0 '..id..' "" 0 0')
	vl.textpos[id][1]=0
	vl.textpos[id][2]=0
	vl.texttext[id]=0
	vl.textcolor[id]=0
end
-- Function: createButton
-- Arguments: id - button ID, ply - player to be shown the button, x - button's X
position on player's screen, y - button's Y position on player's screen, style -
button's style
-- Possible argument values: id - 1-1024, ply - 0-32 (0 for all players), x - 0-640, y -
0-480, style - nil-any button style ID
function vl.createButton(id,ply,x,y,style,caption,func)
	if style==nil then style=1 end
	vl.buttonimg[id]=image(vl.buttonstyles[style].image,x,y,2,ply)
	vl.buttonply[id]=ply
	vl.buttonpos[id][1]=x
	vl.buttonpos[id][2]=y
	vl.buttonsize[id][1]=vl.buttonstyles[style].sizex
	vl.buttonsize[id][2]=vl.buttonstyles[style].sizey
	vl.buttonstyle[id]=style
	vl.buttonfunc[id]=func
	if (caption~=nil) then
		vl.buttoncaption[id]=vl.createText(ply,colorcode
(vl.buttonstyles[style].txtr,vl.buttonstyles[style].txtg,vl.buttonstyles
[style].txtb),caption,x+vl.buttonstyles[style].ox,y+vl.buttonstyles[style].oy)
	end
end
function colorcode(r,g,b)
	if (tostring(r):len()==1) then
		r="00"..r
	elseif (tostring(r):len()==2) then
		r="0"..r
	end
	if (tostring(g):len()==1) then
		g="00"..g
	elseif (tostring(g):len()==2) then
		g="0"..g
	end
	if (tostring(b):len()==1) then
		b="00"..b
	elseif (tostring(b):len()==2) then
		b="0"..b
	end
	return r..""..g..""..b
end
-- Function: hoverButton
-- Arguments: id - button ID, ply - player hovering over the button
-- Possible argument values: id - 1-1024, ply - 1-32
function vl.hoverButton(id,ply)
	msg(vl.buttonply[id])
	msg(ply)
	if (vl.buttonply[id]==ply) then
		freeimage(vl.buttonimg[id])
		vl.buttonimg[id]=image(vl.buttonstyles[vl.buttonstyle
[id]].hover,vl.buttonpos[id][1],vl.buttonpos[id][2],2,ply)
		vl.buttonhoveredupon[id]=true
		vl.plyInRange[ply]=id
	end
end
-- Function: dehoverButton
-- Arguments: id - button ID, ply - player hovering over the button
-- Possible argument values: id - 1-1024, ply - 1-32
function vl.dehoverButton(id,ply)
	if (vl.buttonhoveredupon[id] and vl.buttonply[id]==ply) then
		freeimage(vl.buttonimg[id])
		vl.buttonimg[id]=image(vl.buttonstyles[vl.buttonstyle
[id]].image,vl.buttonpos[id][1],vl.buttonpos[id][2],2,ply)
		vl.buttonhoveredupon[id]=false
		vl.plyInRange[ply]=0
	end
end
-- Function: buttonCLick
-- Arguments: id - button ID, ply - player clicking the button
-- Possible argument values: id - 1-1024, ply - 1-32
function vl.buttonClick(id,ply)
	freeimage(vl.buttonimg[id])
	vl.buttonimg[id]=image(vl.buttonstyles[vl.buttonstyle
[id]].click,vl.buttonpos[id][1],vl.buttonpos[id][2],2,ply)
	vl.buttonclicker[id]=ply
	timer(100,"parse","lua vl.buttonUnclick("..id..","..ply..")")
	msg("©255000000"..vl.buttonply[id])
	msg(ply)
end
-- Function: buttonUncLick
-- Arguments: id - button ID, ply - player clicking the button
-- Possible argument values: id - 1-1024, ply - 1-32
function vl.buttonUnclick(id,ply)
	if (vl.buttonimg[id]~=0 and vl.buttonply[id]==ply) then
		freeimage(vl.buttonimg[id])
		vl.buttonimg[id]=image(vl.buttonstyles[vl.buttonstyle
[id]].image,vl.buttonpos[id][1],vl.buttonpos[id][2],2,ply)
	end
	if (vl.buttonfunc[id]~=nil and vl.buttonply[id]==ply) then
		vl.buttonfunc[id]()
	end
	vl.buttonclicker[id]=0
end
-- Function: removeButton
-- Arguments: id - button ID
-- Possible argument values: id - 1-1024
function vl.removeButton(id)
	freeimage(vl.buttonimg[id])
	vl.buttonimg[id]=0
	vl.buttonply[id]=0
	vl.buttonpos[id][1]=0
	vl.buttonpos[id][2]=0
	vl.buttonsize[id][1]=0
	vl.buttonsize[id][2]=0
	vl.buttonstyle[id]=0
	vl.buttonfunc[id]=nil
	vl.buttonhoveredupon[id]=false
	vl.removeText(vl.buttoncaption[id])
	vl.buttoncaption[id]=0
	vl.buttonclicker[id]=0
end
	
addhook("clientdata","vl.read")
function vl.read(ply,type,a1,a2)
	if type==0 then
		for buttons=1,1024 do
			if (not player(ply,"bot")) then
				if (a1>=vl.buttonpos[buttons][1]-
vl.buttonsize[buttons][1]/2 and a1<=vl.buttonpos[buttons][1]+vl.buttonsize
[buttons][1]/2 and a2>=vl.buttonpos[buttons][2]-vl.buttonsize[buttons][2]/2
and a2<=vl.buttonpos[buttons][2]+vl.buttonsize[buttons][2]/2) then
					--if (vl.buttonply
[buttons]==ply) then
						
vl.hoverButton(buttons,ply)
					--end
				else
					--if (vl.buttonply
[buttons]==ply) then
						
vl.dehoverButton(buttons,ply)
					--end
				end
			end
		end
	end
end
addhook("ms100","vl.send")
function vl.send()
	for ply=1,32 do
		if (player(ply,"exists")) then
			reqcld(ply,0)
		end
	end
end
addhook("attack","vl.click")
function vl.click(ply)
	if (vl.plyInRange[ply]~=0) then
		vl.buttonClick(vl.plyInRange[ply],ply)
	end
end
addhook("serveraction","otak")
function otak(id,a)
if a==1 then
vl.createWindow( ID ,1,320,240,9,7,true,1,"Welcome screen")
local txt1=vl.createText(0,"000000000","Hello! This server uses the VisualLua
mod by",180,160)
local txt2=vl.createText(0,"000000000","EngiN33R. Have fun!",180,175)
vl.createButton(1,1,384,310,2,"",function() vl.closeWindow(1) vl.removeButton(1)
vl.removeText(txt1) vl.removeText(txt2) end)
end
end