Forum
CS2D Scripts Laser ModLaser Mod
10 replies 1
| Wall
/, \ Laser
_ Empty
L = laser shot from
______
|_/\__|
|/_|\_|
|\_|_\
|_\|__\
|_/|___\
|/_|____\
|\_|_____\
|_L|______\
edited 1×, last 07.08.11 03:39:00 pm
@Factis699 not really, lua isn't the difficultly however you do need a basic understanding of cos/sin/tan
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
--[[initialization, arrays, reseting]]-------------------------- math.randomseed(os.time()) math.randomseed(os.time() * math.sin(os.time() * math.random())) images = 0 max_images = 12 --lower to decrease lag IOR = {1, 1.5, 0} --index of refraction of ground, obstacle, wall. < 1 means mirror. power_threshold = 0.01 --raise to decrease lag gamma = 1 / 2.2 --how bright faint lines are fade_time = 1000 --how long lines last ping = {} --array of filtered pings of living players mode = {{}, {}, {}} --glock burst, famas burst, zoom buffer = {{}, {}} --buffer of current and past positions in x and y coordinates enable = {} --whether lag comp is enabled for one player disabled = {} --weapons whose bullets are not compensated for i, v in ipairs({0, 47, 51, 72, 73, 75, 76, 77, 86, 87, 253, 254, 255}) do 	disabled[v] = true end armor = {} --special armor protection values for i, v in ipairs({25, 50, 75, 50, 95}) do 	armor[200 + i] = 1 - (v / 100) end function reset(id) 	mode[1][id] = 0 	mode[2][id] = 0 	mode[3][id] = 0 	buffer[1][id] = {} 	buffer[2][id] = {} 	ping[id] = nil end function clear(id) 	reset(id) 	enable[id] = 1 end for i = 1, 32 do --initial 	clear(i) end addhook("leave", "clear") addhook("die", "reset") function updateping(id) 	local actualping = player(id, "ping") 	local lastping = ping[id] 	if not lastping then lastping = 0 end 	if actualping then 		if actualping - lastping <= 30 or lastping == 0 then --regular 			ping[id] = actualping 		else --spike is "damped" 			ping[id] = 0.7 * lastping + 0.3 * actualping 		end 	end end addhook("spawn", "updateping") ---------------------------------------------------------------- --[[periodic functions]]---------------------------------------- frame = 1 BUFFER_SIZE = 25 function updatebuffer() 	frame = frame + 1 	for i in pairs(ping) do 		buffer[1][i][frame], buffer[2][i][frame] = player(i, "x"), player(i, "y") 		buffer[1][i][frame - BUFFER_SIZE], buffer[2][i][frame - BUFFER_SIZE] = nil, nil 	end 	images = 0 end addhook("always", "updatebuffer") function onsecond() 	for i in pairs(ping) do 		updateping(i) 	end end addhook("second", "onsecond") ---------------------------------------------------------------- --[[new hit system]]-------------------------------------------- addhook("hit", "onhit") function onhit(v, id, wpn) 	if disabled[wpn] or id == 0 or enable[id] ~= 1 then --preserve cs2d's internal hit system in these cases 		return 0 	end 	return 1 end addhook("attack", "onattack") function onattack(id) 	local wpn = player(id, "weapon") 	if disabled[wpn] or enable[id] ~= 1 then --preserve cs2d's internal hit system in these cases 		return 	end 	local dmg = itemtype(wpn, "dmg") * game("mp_damagefactor") 	if (wpn == 2 and mode[1][id] == 1) or (wpn == 39 and mode[2][id] == 1) then --burst weapons 		dmg = math.floor(dmg * 0.64 + 0.5) 		local rot1 = player(id, "rot") - 6 + 1 * math.random() 		local rot2 = player(id, "rot") + 6 + 1 * math.random() 		local rot3 = player(id, "rot") - 6 - 1 * math.random() 		simulate_attack(id, wpn, dmg, rot1) 		simulate_attack(id, wpn, dmg, rot2) 		simulate_attack(id, wpn, dmg, rot3) 		return 	elseif wpn == 10 or wpn == 11 then 		for i=1,5 do 			simulate_attack(id, wpn, dmg, player(id, "rot") - 20 + 40 * math.random(), 180) 		end 		return 	end 	if mode[3][id] == 1 then --scoped weapons 		dmg = itemtype(wpn, "dmg_z1") * game("mp_damagefactor") 	elseif mode[3][id] == 2 then 		dmg = itemtype(wpn, "dmg_z2") * game("mp_damagefactor") 	end 	local rot = player(id, "rot") + itemtype(wpn, "dispersion") * (2 * math.random() - 1) 	simulate_attack(id, wpn, dmg, rot) end addhook("attack2", "onattack2") function onattack2(id, m) 	local wpn = player(id, "weapon") 	if wpn == 50 or wpn == 69 then 		if enable[id] == 1 then 			simulate_attack(id, wpn, itemtype(wpn, "dmg_z1") * game ("mp_damagefactor")) 		end ---------------------------------------------------------------- --[[syncs burst/zoom for each player to actual cs2d burst/zoom]] 	elseif wpn == 2 then 		mode[1][id] = m 	elseif wpn == 39 then 		mode[2][id] = m 	elseif wpn ~= 32 and wpn >= 31 and wpn <= 37 then 		mode[3][id] = m 	end	 end addhook("reload", "unzoom") addhook("select", "unzoom") function unzoom(id) 	mode[3][id] = 0 end addhook("drop", "ondrop") function ondrop(id,iid,wpn,ain,a,m) 	mode[3][id] = 0 	if wpn == 2 then 		mode[1][id] = 0 	elseif wpn == 39 then 		mode[2][id] = 0 	end end addhook("collect", "oncollect") function oncollect(id,iid,wpn,ain,a,m) 	if wpn == 2 then 		mode[1][id] = m 	elseif wpn == 39 then 		mode[2][id] = m 	end end ---------------------------------------------------------------- --[[bullet simulation]]----------------------------------------- --[[simulates the shooting of a bullet of damage (dmg) from (wpn) by (id) with angle (rot) and range (range). it has two parts. part 1 finds bullet's path before hitting a wall; part 2 calculates hits on other players (with lag compensation).]] function simulate_attack(id, wpn, dmg, rot, range) 	if not wpn then wpn = player(id, "weapon") end 	if not dmg then dmg = itemtype(wpn, "dmg") * game("mp_damagefactor") end 	if not rot then rot = player(id, "rot") end 	if not range then range = itemtype(wpn, "range") end 	local start_x = player(id, "x") 	local start_y = player(id, "y") 	--part 1 - split up beams 	local beams = flattenbeam(get_rays(start_x, start_y, rot, 3 * range, 1)) 	--part 2 - detect hits 	local frames = math.floor(ping[id] / 20) 	if frames > (BUFFER_SIZE - 1) then 		frames = (BUFFER_SIZE - 1) 	end 	local victims = {} 	if game("sv_friendlyfire") == "0" and game("sv_gamemode") ~= "1" then 		for i in pairs(ping) do 			if player(i, "team") ~= player(id, "team") then 				victims[i] = true 			end 		end 	else 		for i in pairs(ping) do 			victims[i] = true 		end 		victims[id] = nil 	end 	local olddmg = dmg 	for _, beam in ipairs(beams) do 		dmg = olddmg * (beam[5] or 1) 		for i in pairs(victims) do 			if intersect(beam[1], beam[2], beam[3], beam[4], buffer[1][i][frame - frames], buffer[2][i][frame - frames], 1) then 				parse("sv_sound2 "..id.." player/hit"..math.ceil(3 * math.random())..".wav") 				parse("sv_sound2 "..i.." player/hit"..math.ceil(3 * math.random())..".wav") 				local newhealth 				local newarmor = player(i, "armor") 				if newarmor <= 200 then 					newarmor = newarmor - dmg 					if newarmor < 0 then 						newarmor = 0 					end 					newhealth = player(i, "health") - (dmg - math.floor(game("mp_kevlar") * (player(i, "armor") - newarmor))) 					parse("setarmor "..i.." "..newarmor) 				else --special armor values 					newhealth = player(i, "health") - math.floor((dmg * (armor[newarmor] or 1))) 				end 				if newhealth > 0 then 					parse("sethealth "..i.." "..newhealth) 				else 					parse("customkill "..id.." "..itemtype(wpn, "name").." "..i) 				end 			end 		end 	end end --the following functions are used in simulate_attack. function get_rays(start_x, start_y, rot, range, power) 	if range <= 0 then return end 	if power <= power_threshold then return end 	if not rot then return end 	local end_x = start_x + (range) * math.sin(math.rad(rot)) 	local end_y = start_y - (range) * math.cos(math.rad(rot)) 	local inc_x, inc_y 	if rot < 0 then 		inc_x = -1 	elseif rot > 0 and rot ~= 180 then 		inc_x = 1 	end 	if math.abs(rot) > 90 then 		inc_y = 1 	elseif math.abs(rot) < 90 then 		inc_y = -1 	end 	local tile_x = math.floor(start_x / 32) 	local tile_y = math.floor(start_y / 32) 	if (inc_x or 0) == -1 and start_x % 32 == 0 then tile_x = tile_x - 1 end 	if (inc_y or 0) == -1 and start_y % 32 == 0 then tile_y = tile_y - 1 end 	local beams = {} 	local n2 = tile_IOR(tile_x, tile_y) 	while true do 		n1 = n2 		n2 = tile_IOR(tile_x, tile_y) 		if n2 < 1 then --mirror, 100% reflection. 			local i 			end_x, end_y, i = intersect(start_x, start_y, end_x, end_y, topixel(tile_x), topixel(tile_y), 16) 			if i == 1 then --vertical reflection 				rot = -rot 			elseif i == 0 then --horizontal reflection 				rot = ((1 + (inc_x or 1)) * 180 - rot) % 360 - 180 			else 				--msg("wtf") 				break 			end 			range = range - math.sqrt((end_x - start_x) ^ 2 + (end_y - start_y) ^ 2) 			beams[#beams + 1] = {start_x, start_y, end_x, end_y, power} -- incident 			beams[#beams + 1] = get_rays(end_x, end_y, rot, range, power) -- reflected 			break 		elseif n1 ~= n2 then --interface. fresnel + snell. 			local i 			end_x, end_y, i = intersect(start_x, start_y, end_x, end_y, topixel(tile_x), topixel(tile_y), 16) 			local rot_r, rot_t --reflection and transmission angles 			local R --fraction of power reflected. T = 1 - R by conservation of energy. 			local a, b --helpers 			if i == 1 then 				rot_r = -rot 				local something = n1 / n2 * math.sin(math.rad(rot - (inc_x or 1) * 90)) 				if math.abs(something) <=1 then 					rot_t = (inc_x or 1) * 90 + math.deg(math.asin (something)) --snell's law 				end 				a = rot_t and n1 * math.cos(math.rad(rot - (inc_x or 1) * 90)) or 1 				b = rot_t and n2 * math.cos(math.rad(rot_t - (inc_x or 1) * 90)) or 0 			elseif i == 0 then 				rot_r = (inc_x or 1) * 180 - rot 				local something = n1 / n2 * math.sin(math.rad(90 * (1 + (inc_y or 1)) - rot)) 				if math.abs(something) <= 1 then 					rot_t = (90 * (3 + (inc_y or 1)) - math.deg (math.asin(something))) % 360 - 180 				end 				a = rot_t and n1 * math.cos(math.rad(90 * (1 + (inc_y or 1)) - rot)) or 1 				b = rot_t and n2 * math.cos(math.rad(90 * (1 + (inc_y or 1)) - rot_t)) or 0 			else 				--msg("debug: wtf") 				break 			end 			R = ((a - b) / (a + b)) ^ 2 -- fresnel's equation 			range = range - math.sqrt((end_x - start_x) ^ 2 + (end_y - start_y) ^ 2) 			beams[#beams + 1] = {start_x, start_y, end_x, end_y, power} -- incident 			if R < 0.5 then --this makes sure beam is sorted by power 				rot_r, rot_t = rot_t, rot_r 				R = 1 - R 			end 			beams[#beams + 1] = get_rays(end_x, end_y, rot_r, range, power * R) 			beams[#beams + 1] = get_rays(end_x, end_y, rot_t, range, power * (1 - R)) 			break 		end 		local temp_x, temp_y = tile_x, tile_y 		if inc_x and intersect(start_x, start_y, end_x, end_y, topixel(temp_x + inc_x), topixel(temp_y), 16) then 			tile_x = temp_x + inc_x 		end 		if inc_y and intersect(start_x, start_y, end_x, end_y, topixel(temp_x), topixel(temp_y + inc_y), 16) then 			tile_y = temp_y + inc_y 		end 		if tile_x == temp_x and tile_y == temp_y then 			--msg("range") 			beams[#beams + 1] = {start_x, start_y, end_x, end_y, power} 			break 		end 	end 	return beams end function tile_IOR(tile_x, tile_y) 	if tile(tile_x, tile_y, "wall") then 		return IOR[3] 	elseif tile(tile_x, tile_y, "obstacle") then 		return IOR[2] 	else 		return IOR[1] 	end end function isbeam(tab) 	if type(tab) ~= "table" or #tab ~= 5 then return false end 	for i, v in ipairs(tab) do 		if type(v) == "table" then return false end 	end 	return true end function flattenbeam(tab) --also draws them :D 	local flat = {} 	for i, v in ipairs(tab) do 		if isbeam(v) then 			drawbeam(v) 			flat[#flat + 1] = {} 			for a, b in ipairs(v) do 				flat[#flat][a] = b 			end 		else 			for c, d in ipairs(flattenbeam(v)) do 				flat[#flat + 1] = {} 				for e, f in ipairs(d) do 					flat[#flat][e] = f 				end 			end 		end 	end 	return flat end function drawbeam(beam) 	if images <= max_images then 		images = images + 1 		local x, y = (beam[1] + beam[3]) / 2, (beam[2] + beam[4]) / 2 		local id = image("gfx/sprites/laserbeam1.bmp", x, y, 1) 		imagealpha(id, beam[5] ^ gamma) --crude srgb gamma correction 		imagecolor(id, 255, 0, 0) 		imagescale(id, 0.25, math.sqrt((beam[4] - beam[2]) ^ 2 + (beam[3] - beam [1]) ^ 2) / 32) 		imagepos(id, x, y, 90 + math.deg(math.atan2((beam[4] - beam[2]) , (beam[3] - beam[1])))) 		tween_alpha(id, fade_time, 0) 		timer(fade_time, "freeimage", id) 	end end --converts a tile number to the tile's center pixel. function topixel(tile) 	return (tile * 32) + 16 end --quick test to see if i is in between s and e. used in intersect(). function isinorder(s, i, e) 	return (e >= i and i >= s) or (e <= i and i <= s) end --[[returns the first point of intersection between a box centered at (bx, by) with half side length (bl) and a line segment starting from (sx, sy) and ending at (ex, ey). if the line segment is enclosed by the box, (ex, ey) is returned.]] function intersect(sx, sy, ex, ey, bx, by, bl) 	if not (bx and by) then return end --fixes rare lua error 	if math.abs(sx - bx) <= bl and math.abs(sy - by) <= bl then 		if math.abs(ex - bx) <= bl and math.abs(ey - by) <= bl then 			if math.abs(ey - by) == bl then 				return ex, ey, 0 			end 			return ex, ey, 1 		else 			sx, sy, ex, ey = ex, ey, sx, sy 		end 	end 	local i_x, i_y 	if ey > sy then 		i_y = by - bl 	elseif ey < sy then 		i_y = by + bl 	end 	if i_y and isinorder(sy, i_y, ey) then 		i_x = ((ex - sx) * i_y + (sx * ey - sy * ex)) / (ey - sy) 		if math.abs(i_x - bx) <= bl and isinorder(sx, i_x, ex) then 			return i_x, i_y, 0 -- collision with horizontal edge 		end 	end 	if ex > sx then 		i_x = bx - bl 	elseif ex < sx then 		i_x = bx + bl 	end 	if i_x and isinorder(sx, i_x, ex) then 		i_y = ((ey - sy) * i_x + (sy * ex - sx * ey)) / (ex - sx) 		if math.abs(i_y - by) <= bl and isinorder(sy, i_y, ey) then 			return i_x, i_y, 1 -- collision with vertical edge 		end 	end end ----------------------------------------------------------------
I want one again reflected how?
______
|__/\_|
|_/_|\|
|/__|_|
|L__|_|
Apache uwu has written
@Factis699 not really, lua isn't the difficultly however you do need a basic understanding of cos/sin/tan
and some facts about optics
it's much more complicated than it looks honestly
1