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
mob_list = {}
mob = {}
Class(mob, function(mob_id, tilex, tiley, respawn, respawn_delay)
	local obj = mobs[mob_id]
	
	self.mob_id = mob_id
	self.tilex = tilex
	self.tiley = tiley
	self.respawn = respawn or false
	self.respawn_delay = (respawn_delay) and respawn_delay * 1000 or 10 * 1000
	self.x = misc.tile_to_pixel(tilex)
	self.y = misc.tile_to_pixel(tiley)
	self.name = obj.name
	self.level = obj.level
	self.exp = obj.exp
	self.hit_sound = obj.hit_sound
	self.die_sound = obj.die_sound
	self.attack_sounds = obj.attack_sounds
	self.wiggle = obj.wiggle
	self.width = obj.size[1]
	self.height = obj.size[2]
	self.health = obj.health
	self.max_health = obj.health
	self.dmg = obj.dmg
	self.defense = obj.def
	self.speed = obj.speed
	self.view_dis = obj.view_dis
	self.attack_range = obj.attack_range
	self.attack_speed = obj.attack_speed * 10
	self.attack_cooldown = 0
	self.spawn_distance = obj.spawn_distance
	self.spawn_points = {tilex, tiley}
	self.target = false
	self.target_cooldown = 0
	self.max_target_cooldown = 5 * 10
	self.back = false
	self.free_move = obj.free_move
	self.max_free_move_cooldown = 5 * 10
	self.free_move_cooldown = self.max_free_move_cooldown
	self.free_move_x = false
	self.free_move_y = false
	
	self.shadow = image(DIR_GFX .. "shadow.bmp<a>", self.x, self.y, 0)
	imagescale(self.shadow, 1.5, 1.5)
	imagecolor(self.shadow, 0, 0, 0)
	imagealpha(self.shadow, 0.7)
	
	self.mob = image(obj.path, self.x, self.y, 0)
	imagehitzone(self.mob, obj.blood_type + 100, -self.width / 2, -self.height / 2, self.width, self.height)
	imagescale(self.mob, 1, 1)
	
	-- SOFT SPAWN
	imagealpha(self.shadow, 0)
	imagealpha(self.mob, 0)
	tween_alpha(self.shadow, 1000, 0.7)
	tween_alpha(self.mob, 1000, 1)
	
	table.insert(mob_list, self)
end)
function mob:delete()
	freeimage(self.shadow)
	freeimage(self.mob)
	
	table.removeValue(mob_list, self)
	self = nil
end
function mob:die(user, no_effect)
	if self.die_sound then
		sv_soundpos(self.die_sound, self.x, self.y, 0)
	end
	local respawn_func = function()
		mob.new(self.mob_id, self.tilex, self.tiley, true, self.respawn_delay / 1000)
	end
	if self.respawn then timerEx(self.respawn_delay, respawn_func, 1) end
	if user then
		user.stats:giveExp(self.exp)
		if not no_effect then
			for i = 0, 3 do
				spawnprojectile(user.id, 86, self.x, self.y, 32, math.random(0, 359))
			end
		end
		quest.killMob(user, self.mob_id)
	end
	self:delete()
end
function mob:update()
	self.x = object(self.mob, "x")
	self.y = object(self.mob, "y")
	self.rot = object(self.mob, "rot"); self.rot = (self.rot < -90) and self.rot + 360 or self.rot
	
	if self.back then
		local spawn_tilex = self.spawn_points[1]
		local spawn_tiley = self.spawn_points[2]
		local spawn_x = misc.tile_to_pixel(spawn_tilex)
		local spawn_y = misc.tile_to_pixel(spawn_tiley)
		
		self:go(spawn_x, spawn_y)
		
		if spawn_tilex == misc.pixel_to_tile(self.x) and spawn_tiley == misc.pixel_to_tile(self.y) then
			self.back = false
			self.free_move_cooldown = self.max_free_move_cooldown
			self.free_move_x, self.free_move_y = false, false
		end
		
		return
	end
	
	if self.target_cooldown == 0 then
		self.target = self:closestPlayer()
		
		if self.target then
			self.target_cooldown = self.max_target_cooldown
		end
	end
	
	if self.target then
		local x, y = player(self.target, "x"), player(self.target, "y")
		local dis = misc.point_distance(self.x, self.y, x, y)
		local rot = misc.point_direction(x, y, self.x, self.y)
		
		if self.attack_range < dis then
			x, y = misc.pos_trigger(x, y, rot, self.attack_range - 1)
			self:go(x, y, rot)
		else
			self:attack(self.target, rot)
		end
	elseif self.free_move then
		if self.free_move_cooldown == 0 then
			local spawn_x = misc.tile_to_pixel(self.spawn_points[1])
			local spawn_y = misc.tile_to_pixel(self.spawn_points[2])
			local dis = misc.point_distance(self.x, self.y, spawn_x, spawn_y)
			
			if dis > self.spawn_distance then
				self.back = true
				return
			end
			
			if not self.free_move_x and not self.free_move_y then
				local rot = math.random(0, 359)
				
				self.free_move_x, self.free_move_y = misc.pos_trigger(self.x, self.y, rot, math.random(32, 96))
			end
			
			self:go(self.free_move_x, self.free_move_y)
			
			if misc.pixel_to_tile(self.free_move_x) == misc.pixel_to_tile(self.x) and misc.pixel_to_tile(self.free_move_y) == misc.pixel_to_tile(self.y) then
				self.free_move_cooldown = self.max_free_move_cooldown
				self.free_move_x, self.free_move_y = false, false
			end
		end
	end
end
function mob:addHP(hp, user)
	return self:setHP(self.health + hp, user)
end
function mob:setHP(hp, user)
	self.health = (hp == "max") and self.max_health or hp
	self.health = (self.health > self.max_health) and self.max_health or self.health
	if self.health <= 0 then
		self:die(user)
	end
	
	return self.health
end
function mob:hit(user, dmg)
	dmg = dmg or -item.calcDmg(user.stats.attack, self.defense)
	
	if not user.horse:canAttack() then return end
	
	if self.hit_sound then
		sv_soundpos(self.hit_sound[math.random(1, #self.hit_sound)], self.x, self.y, 0)
	end
	self.target = user.id
	self.target_cooldown = self.max_target_cooldown
	self.back = false
	if self:addHP(dmg, user) > 0 then
		user.huds:showMobHP(self)
	else
		user.huds:hideMobHP(self)
	end
end
function mob:attack(target, rot)
	local user = users[target]
	local x, y = player(self.target, "x"), player(self.target, "y")
	local rot = rot or misc.point_direction(x, y, self.x, self.y)
	
	if player(target, "health") <= 0 then
		self.target = false
		self.back = true
		
		return
	end
	
	if self.attack_cooldown == 0 then
		self.attack_cooldown = self.attack_speed
		
		if self.wiggle then
			tween_rotate(self.mob, 100, rot - 40)
		end
		
		if #self.attack_sounds > 0 then
			sv_soundpos(self.attack_sounds[math.random(1, #self.attack_sounds)], self.x, self.y, 0)
		end
		
		user:hit("mob", self, self.dmg)
		self.target_cooldown = self.max_target_cooldown
	else
		tween_rotate(self.mob, 100, rot)
	end
end
function mob:go(x, y, rot)
	local rot = rot or misc.point_direction(x, y, self.x, self.y)
	
	x = self.x - misc.lengthdir_x(rot, self.speed + 20)
	y = self.y - misc.lengthdir_y(rot, self.speed + 20)
	
	tween_move(self.shadow, 100, x, y, 0)
	tween_move(self.mob, 100, x, y, rot)
end
function mob:closestPlayer()
	return misc.closestPlayer(self.x, self.y, self.view_dis)
end
function mob.closestMob(sx, sy, max_dist)
local closest_id = nil
local closest_dist = max_dist
local pl = player(0,"tableliving")
local i = 0
while (i < #pl) do
i = i + 1
local temp_id = pl[i]
local dist = misc.point_distance(sx,sy,player(temp_id,"x"),player(temp_id,"y"))
if (dist <= closest_dist) then
closest_id = temp_id
closest_dist = dist
end
end
return closest_id
end
function mob.closestMob(x, y, max_dist)
	local closest_mob = nil
	local closest_dist = max_dist
	for _, i in pairs(mob_list) do
		local dist = misc.point_distance(x, y, i.x, i.y)
		if dist <= closest_dist then
			closest_mob = i
			closest_dist = dist
		end
	end
	return closest_mob
end
function mob.draw()
	for _, i in pairs(mob_list) do
		if misc.closestPlayer(i.x, i.y, 400, player(0, "table")) then
			i:update()
		end
		
		if i.attack_cooldown > 0 then
			i.attack_cooldown = i.attack_cooldown - 1
		end
		
		if i.target_cooldown > 0 then
			i.target_cooldown = i.target_cooldown - 1
			
			if i.target_cooldown == 0 then
				i.target = false
				i.back = true
			end
		end
		
		if i.free_move_cooldown > 0 then
			i.free_move_cooldown = i.free_move_cooldown - 1
		end
	end
end
addAction("ms100", mob.draw)