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
CARS = {}
MetaCars = {}
MetaCars.__index = MetaCars
math.pi = 3.14--15926535
car_rotatespeed = 0.03 --you can tweak this value to change the angle of the green line
function MetaCars.new(cx,cy,crot)
	local newcar = {
		x = cx,
		y = cy,
		rot = standardangle(crot),
		img = image("gfx/cars/car"..math.random(1,23)..".bmp",0,0,1),
		dpl = 0,
		is = true
	}
	if crot < 0 then crot = crot +180 else crot = crot -180 end
	imagepos(newcar.img,cx,cy,crot)
	return setmetatable(newcar,MetaCars)
end
function MetaCars:reset(cx,cy,crot)
	self.x = cx
	self.y = cy
	self.rot = standardangle(crot)
	freeimage(self.img)
	self.img = image("gfx/cars/car"..math.random(1,23)..".bmp",0,0,1)
	self.dpl = 0
	self.is = true
	if crot < 0 then crot = crot +180 else crot = crot -180 end
	imagepos(self.img,cx,cy,crot)
end
function MetaCars:rotate(pl_rot,rot_spd)
	local difference = pl_rot - self.rot
	
	if rot_spd >= math.abs(difference) then
		self.rot = pl_rot --Prevent car from "shaking"
		return 0
	end
	
	local pi = math.pi
	if difference < 0 then
		if difference <= -pi then
			self.rot = self.rot + rot_spd
		else
			self.rot = self.rot - rot_spd
		end
	else
		if difference > pi then
			self.rot = self.rot - rot_spd
		else
			self.rot = self.rot + rot_spd
		end
	end
	
	if self.rot <= -pi then
		self.rot = -self.rot
	elseif self.rot > pi then
		self.rot = -pi*2+self.rot
	end
end
function MetaCars:move(speed)
	local ceil = math.ceil
	local floor = math.floor
	local x = self.x + math.cos(self.rot)*speed
	local y = self.y + math.sin(self.rot)*speed
	--Map bounds checking
	if x >= 0 and y >= 0 then
		if x <= map("xsize")*32 and y <= map("ysize")*32 then
			--Wall collision checking
			if tile(floor(x/32-1), floor(y/32-1), "walkable") then
				if tile(ceil(x/32), ceil(y/32), "walkable") then
			
					parse("setpos "..self.dpl.." "..x.." "..y)
					self.x, self.y = x, y
					
				end
			end
			
		end
	end
	imagepos(self.img, self.x, self.y, cs2dAngle(self.rot))
end
function standardangle(angle) --CS2D system
	if angle <= 180 and angle >= -90 then
		angle = angle - 90
	else
		angle = angle + 270
	end
	return math.rad(angle)
end
function cs2dAngle(angle)
	local pi = math.pi
	if angle <= pi and angle >= -pi*0.5 then
		angle = angle - 0.5*pi
	else
		angle = angle + 1.5*pi
	end
	return math.deg(angle)
end
addhook("startround","cars_startround")
function cars_startround()
	local c_cars = 1
	for y=1,map("ysize") do
		for x=1,map("xsize") do
			if entity(x,y,"exists") then
				if entity(x,y,"name") == "car" then
					if not CARS[c_cars] then
						table.insert(CARS,MetaCars.new( x*32, y*32, entity(x,y,"int4") ))
					else
						CARS[c_cars]:reset( x*32, y*32, entity(x,y,"int4") )
					end
					c_cars = c_cars + 1
				end
			end
		end
	end
end
addhook("always","cars_frame")
function cars_frame()
	if #CARS>0 then
		local car_move = MetaCars.move
		local car_rotate = MetaCars.rotate
		for i,CAR_TBL in ipairs(CARS) do
			if CAR_TBL.is then
				if CAR_TBL.dpl > 0 then
					local p = CAR_TBL.dpl
					if player(p,"exists") and player(p,"health") > 0 then
						car_rotate(CAR_TBL,standardangle( player(p,"rot") ),car_rotatespeed)
						car_move(CAR_TBL,4)
					else
						CARS[i].dpl = 0
					end
				end
			end
			CARS[i] = CAR_TBL
		end
	end
end
addhook("use","entercar")
function entercar(id,event)
	if event == 0 then
		local x = player(id,"x")
		local y = player(id,"y")
		for i=1,#CARS do
			if CARS[i].dpl == id then
				CARS[i].dpl = 0
				parse("speedmod "..id.." 0")
				return 0
			end
		end
		for i=1,#CARS do
			if math.abs(CARS[i].x - x+16) <= 24 then
				if math.abs(CARS[i].y - y) <= 24 then
					if CARS[i].dpl == 0 then
						CARS[i].dpl = id
						parse("setpos "..id.." "..CARS[i].x.." "..CARS[i].y)
						parse("speedmod "..id.." -100")
						break
					end							
				end
			end
		end
	end
end
addhook("die","leave_car")
function leave_car(v,k)
	for i=1,#CARS do
		if CARS[i].dpl == v then
			CARS[i].dpl = 0
			break
		end
	end
end