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
--[[Constants]]--
rpiconst = 180 / math.pi
imagepath = "sys/lua/rocketmod/Rocket.png" -- path to image
speed = 15 --speed of rocket
dmg = 100 --damage the rocket does
turnrate = 20 --@depricated turnrate of the rocket
tileconst =32 --width/height of tile
collisionradius = 200 -- the radius in which the rocket will do damage
money = 1000
pl={}
pl.x={}
pl.y={}
--[[ Table setups! ]]--
ROCKETS = {} -- table to store all rockets
rocket = {}
rocket_mt = { __index = rocket}
rocketmod = {}
--[[ Hooks! ]]--
addhook("attack","boom")
function boom(id)
	for _k,_v in ipairs(ROCKETS) do
		if(id == _v.pid) then
			_v:explode()
			break
		end
	end
end
addhook("serveraction","rocketmod.serveraction")
function rocketmod.serveraction(id,action)
	if action == 1 then
		if(buyItem(id,money)) then
			local fb = rocket:new(player(id,"x"),player(id,"y"),toRad(player(id,"rot")),player(id,"rot"),id)
			table.insert(ROCKETS,fb)
			fb.id = #ROCKETS
			fb:draw() -- draw once to get an imageid
		end
	end
	if action == 2 then
		msg2(id,"attack to explode!")
	end
end
addhook("ms100","my_ms100")
function my_ms100()
	for i,v in ipairs(ROCKETS) do
		reqcld(1,2)--change to "reqcld(0,2)" when playing with other real players (with bots leave it as it is)
		v:update()
	end
end
--[[
	Below here is the rocket class!
	]]--
function rocket:new(x,y,dir,rot,playerid)
	return setmetatable({x=x,y=y,dir=dir,rot=rot,id = nil,imageid = nil,pid = playerid},rocket_mt)
end
function rocket:draw()
	self.imageid = image(imagepath,self.x,self.y,1)
	imagepos(self.imageid,self.x,self.y,self.rot)
	imagescale(self.imageid,0.6,0.6)
end
function rocket:explode()
	parse(string.format("%s %i %i %i %i %i","explosion",self.x,self.y,collisionradius,dmg,self.pid))
	self:destroy()
end
addhook("clientdata","getCursorPos")
function getCursorPos(id,mode,d1,d2)
	pl.x[id]=d1
	pl.y[id]=d2
end
function rocket:update()
	local dis = math.sqrt((self.x-pl.x[self.pid])*(self.x-pl.x[self.pid])+(self.y-pl.y[self.pid])*(self.y-pl.y[self.pid]))
	self.y = self.y - ((self.y-pl.y[self.pid])/dis)*speed
	self.x = self.x - ((self.x-pl.x[self.pid])/dis)*speed
	tween_rotate(self.imageid,1,math.deg(math.atan((self.y-pl.y[self.pid])/(self.x-pl.x[self.pid])))-90) --some problem here :)
	--Map boundaries collision
	if(self.x > (map("xsize")*tileconst) or self.x < 0 or self.y > (map("ysize")*tileconst) or self.y < 0) then
		self:destroy()
	else
		imagepos(self.imageid,self.x,self.y,self.rot)
	end
end
function rocket:destroy()
	freeimage(self.imageid)
	table.remove(ROCKETS,self.id)
	for i,m in ipairs(ROCKETS) do
		m.id = i
	end
end
function rocket:collision(playerid)
	if((self.x > player(playerid,"x") - collisionradius) and (self.x < player(playerid,"x") + collisionradius)) then
		if((self.y > player(playerid,"y") - collisionradius) and (self.y < player(playerid,"y") + collisionradius)) then
			return true
		end
	end
	return false
end
function rocket:turn(rate)
	self.dir = self.dir + toRad(rate)
	self.rot = self.rot + rate
end
--[[ Additional functions ]]--
function buyItem(id,price)
	cm = player(id,"money")
	if(cm>=price) then
		parse("setmoney "..id.." "..cm-price)
		return true
	else
		msg2(id,"©255000000 You have insufficient funds!")
		return false
	end
end
function toRad(deg)
	return ((deg-90)/ rpiconst)
end