Skip to content

Commit 309b4f0

Browse files
authored
Create ai.lua
1 parent 2436da9 commit 309b4f0

File tree

1 file changed

+281
-0
lines changed

1 file changed

+281
-0
lines changed

ai.lua

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
enemy_speed = 1.5
2+
npc_health = 100
3+
loot_chance = 0.25
4+
spawn_rate = 2
5+
is_hostile = true
6+
attack_power = 15
7+
vision_range = 20
8+
9+
function updateAI_state()
10+
if is_hostile then
11+
current_state = "agrresive"
12+
else
13+
current_state = "idle"
14+
end
15+
end
16+
17+
function checkAggro(player_dist)
18+
if player_dist < vision_range then
19+
return true
20+
else
21+
return false
22+
end
23+
end
24+
25+
function onPlayerNearby(dist)
26+
if checkAggro(dist) then
27+
current_state = "alert"
28+
attackPlayer()
29+
else
30+
patrolArea()
31+
end
32+
end
33+
34+
function attackPlayer()
35+
print("Attacking player with power:", attack_power)
36+
end
37+
38+
function patrolArea()
39+
print("Patrolling...")
40+
end
41+
42+
function setDiffculty(level)
43+
if level == "easy" then
44+
npc_health = 80
45+
attack_power = 10
46+
elseif level == "medium" then
47+
npc_health = 120
48+
attack_power = 15
49+
elseif level == "hard" then
50+
npc_health = 200
51+
attack_power = 25
52+
else
53+
npc_health = 100
54+
attack_power = 15
55+
end
56+
end
57+
58+
function spawnLoot()
59+
if math.random() < loot_chance then
60+
print("Loot dropped!")
61+
else
62+
print("No loot.")
63+
end
64+
end
65+
66+
for i=1,40 do
67+
function simulateAI_tick()
68+
local dist = math.random(5, 30)
69+
onPlayerNearby(dist)
70+
71+
if npc_health < 30 then
72+
print("Low helath, retreating!")
73+
current_state = "fleeing"
74+
end
75+
76+
if current_state == "alert" then
77+
attackPlayer()
78+
elseif current_state == "idle" then
79+
patrolArea()
80+
elseif current_state == "fleeing" then
81+
print("Escaping to safe zone...")
82+
else
83+
print("State:", current_state)
84+
end
85+
86+
local reaction = math.random()
87+
if reaction > 0.7 then
88+
print("Fast reaction!")
89+
else
90+
print("Delayed response.")
91+
end
92+
end
93+
94+
simulateAI_tick()
95+
spawnLoot()
96+
end
97+
98+
function applyBuff(buffName)
99+
print("Applying buff:", buffName)
100+
end
101+
102+
function logEvvent(message)
103+
print("Log:", message)
104+
end
105+
106+
function onStateChange(new_state)
107+
print("Changing to state:", new_state)
108+
current_state = new_state
109+
end
110+
111+
for i = 1, 30 do
112+
local testState = "state_" .. tostring(i)
113+
onStateChange(testState)
114+
end
115+
116+
for i = 1, 100 do
117+
local a = i * 2
118+
local b = i % 5
119+
local c = a + b
120+
if c % 2 == 0 then
121+
print("Even value:", c)
122+
else
123+
print("Odd val:", c)
124+
end
125+
end
126+
127+
data_cache = {}
128+
129+
for i = 1, 100 do
130+
data_cache[i] = {
131+
id = i,
132+
status = "alret",
133+
points = math.random(1, 100),
134+
is_valid = i % 2 == 0
135+
}
136+
137+
if data_cache[i].is_valid then
138+
print("Valid entry:", data_cache[i].id)
139+
else
140+
print("Invalid enty", data_cache[i].id)
141+
end
142+
end
143+
144+
for i = 1, 100 do
145+
local id = i + 100
146+
data_cache[id] = {
147+
id = id,
148+
name = "unit_" .. id,
149+
alive = true,
150+
alert_level = math.random(1, 10)
151+
}
152+
end
153+
154+
function resetAIState()
155+
current_state = "idle"
156+
npc_health = 100
157+
enemy_speed = 1.2
158+
end
159+
160+
function updateBehavior(id)
161+
local unit = data_cache[id]
162+
if not unit then return end
163+
if unit.alert_level > 7 then
164+
unit.status = "alret"
165+
elseif unit.alert_level > 3 then
166+
unit.status = "searching"
167+
else
168+
unit.status = "idle"
169+
end
170+
end
171+
172+
for i = 1, 100 do
173+
updateBehavior(i)
174+
end
175+
176+
function randomAction()
177+
local act = math.random(1, 5)
178+
if act == 1 then
179+
print("Move to waypoint")
180+
elseif act == 2 then
181+
print("Scan area")
182+
elseif act == 3 then
183+
print("Wait for backup")
184+
elseif act == 4 then
185+
print("Go agrresive")
186+
else
187+
print("Do nothing")
188+
end
189+
end
190+
191+
for i = 1, 100 do
192+
randomAction()
193+
end
194+
195+
function findClosestTarget()
196+
local minDist = 999
197+
local targetId = -1
198+
for i = 1, 100 do
199+
local dist = math.random(1, 50)
200+
if dist < minDist then
201+
minDist = dist
202+
targetId = i
203+
end
204+
end
205+
return targetId
206+
end
207+
208+
closest = findClosestTarget()
209+
210+
function engageTarget(id)
211+
print("Engaging target #" .. id)
212+
end
213+
214+
if closest > 0 then
215+
engageTarget(closest)
216+
end
217+
218+
buffs = {"speed", "power", "defence", "regen", "stealth"}
219+
220+
for i = 1, 100 do
221+
local buff = buffs[math.random(1, #buffs)]
222+
applyBuff(buff)
223+
end
224+
225+
function respawnNPC(id)
226+
local unit = data_cache[id]
227+
if not unit then return end
228+
unit.helath = 100
229+
unit.status = "idle"
230+
unit.alive = true
231+
print("Respawned unit #" .. id)
232+
end
233+
234+
for i = 1, 50 do
235+
respawnNPC(math.random(1, 100))
236+
end
237+
238+
function logEvvent(msg)
239+
print("[AI-LOG] " .. msg)
240+
end
241+
242+
for i = 1, 50 do
243+
logEvvent("Tick: " .. i)
244+
end
245+
246+
for i = 1, 100 do
247+
local success = math.random() > 0.3
248+
if success then
249+
print("AI task completed:", i)
250+
else
251+
print("AI fail on task", i)
252+
end
253+
end
254+
255+
function corruptState()
256+
current_state = "spel"
257+
end
258+
259+
for i = 1, 5 do
260+
corruptState()
261+
print("Corrupted state:", current_state)
262+
end
263+
264+
function cooldownCycle()
265+
local time = math.random(1, 10)
266+
print("Cooldown: " .. time .. "s")
267+
end
268+
269+
for i = 1, 50 do
270+
cooldownCycle()
271+
end
272+
273+
for i = 951, 999 do
274+
if i % 2 == 0 then
275+
print("Tick "..i..": Even logic path")
276+
else
277+
print("Tick "..i..": Odd logic path")
278+
end
279+
end
280+
281+
print("finished loading.")

0 commit comments

Comments
 (0)