-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai.lua
More file actions
492 lines (442 loc) · 13.5 KB
/
Copy pathai.lua
File metadata and controls
492 lines (442 loc) · 13.5 KB
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
--
--Copyright (C) 2022 Matthias Gatto
--
--This program is free software: you can redistribute it and/or modify
--it under the terms of the GNU Lesser General Public License as published by
--the Free Software Foundation, either version 3 of the License, or
--(at your option) any later version.
--
--This program is distributed in the hope that it will be useful,
--but WITHOUT ANY WARRANTY; without even the implied warranty of
--MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
--GNU General Public License for more details.
--
--You should have received a copy of the GNU Lesser General Public License
--along with this program. If not, see <http://www.gnu.org/licenses/>.
--
PIX_PER_FRAME = 6
--local ACTION_MOVE = 1
ACTION_NPC = 0
local ACTION_MV_TBL = 1
local ACTION_MV_TBL_IDX = 2
local ENEMY_IDLE = 0
local ENEMY_ATTACKING = 1
local phq = Entity.wrapp(ygGet("phq"))
local function npc_check_col(canvas, col_r, pos_add)
local ret = false
local cols = ywCanvasProjectedArColisionArray(canvas, col_r, pos_add)
local i = 0
while i < yeLen(cols) do
local col = yeGet(cols, i)
if yIsNil(yeGet(col, "is_npc")) and yeGetIntAt(col, "Collision") ~= 0 then
ret = true
break
end
i = i + 1
end
yeDestroy(cols)
return ret
end
function randomMovements(_osef, action)
action = Entity.wrapp(action)
directions = {LPCS_LEFT, LPCS_DOWN,
LPCS_RIGHT, LPCS_UP}
local h = action.hdl
local pix_mv = ywidGetTurnTimer() / 10000
if (yIsNil(action.timer_add)) or action.timer_add > 2000 then
if yIsNil(action.timer_add) then
action.hdl = main_widget.npcs[action[0]]
h = action.hdl
pix_mv = 1
end
action.timer_add = 0
action.timer_left = 0
local tmp = Entity.new_array()
local dir = rand_array_elem(directions)
saveNpcCanvasMatadata(tmp, h.canvas)
yGenericSetDir(h, dir)
action._d = dir
yGenericHandlerRefresh(h)
restoreNpcCanvasMatadata(h.canvas, tmp)
end
action.timer_add = action.timer_add + ywidGetTurnTimer() / 100
add_x, add_y = lpcsDirToXY(action._d);
-- do correct movement coputing here
--local PIX_MV_PER_MS = 4
--add_x = add_x * (PIX_MV_PER_MS / TURN_LENGTH)
--add_y = add_y * (PIX_MV_PER_MS / TURN_LENGTH)
local sav = pix_mv - math.floor(pix_mv)
pix_mv = math.floor(pix_mv)
action.timer_left = action.timer_left + sav
if (action.timer_left > 1) then
pix_mv = pix_mv + 1
action.timer_left = action.timer_left - 1
end
add_x = add_x * pix_mv
add_y = add_y * pix_mv
add_pos = Pos.new(add_x, add_y)
if npc_check_col(main_widget_screen,
Rect.new_ps(yGenericHandlerPos(h),
yGenericHandlerSize(h)).ent,
add_pos.ent) == false then
yGenericHandlerMoveXY(h, add_x, add_y)
end
--print("random movements !!!", action, ywidGetTurnTimer() / 100)
end
function npcAdvenceTime()
main_widget.npc_act = {}
phq.env.ai_point = {}
if yIsNNil(phq.env.const_ai_p) then
local caip = phq.env.const_ai_p
for i = 0, yeLen(caip) - 1 do
local info = caip[i]
local who = yeGetKeyAt(caip, i)
push_to_ai_point(info[0], info[1], who)
end
end
local npcs_l = yeLen(npcs)
local threshold = yuiRand()
for i0 = 0, npcs_l - 1 do
local i = (i0 + threshold) % npcs_l
local n = npcs[i]
if yIsNil(n) then
goto skip
end
local ai = n.ai
if ai then
Entity.new_func(yeGetString(ai))(main_widget, n, yeGetKeyAt(npcs, i))
end
:: skip ::
end
end
local function searching(wid, enemy)
local pos = ylpcsHandePos(enemy)
local pj_pos = ylpcsHandePos(wid.pj)
local pj_dist = ywPosDistance(pos, pj_pos)
local up_down = 0
local left_right = 0
local is_attacking = false
local colRect = Rect.new(ywPosX(pos) + 10, ywPosY(pos) + 30, 20, 20).ent
if pj_dist > 300 then
return nil
end
if yeGetInt(enemy.ai_state) == ENEMY_ATTACKING then
is_attacking = true
end
if ywPosX(pj_pos) > ywPosX(pos) then
if (is_attacking == false and enemy.y:to_int() == LPCS_LEFT) then
return nil
end
left_right = 1
elseif ywPosX(pj_pos) < ywPosX(pos) then
if (is_attacking == false and enemy.y:to_int() == LPCS_RIGHT) then
return nil
end
left_right = -1
end
if ywPosY(pj_pos) > ywPosY(pos) then
if (is_attacking == false and enemy.y:to_int() == LPCS_UP) then
return nil
end
up_down = 1
elseif ywPosY(pj_pos) < ywPosY(pos) then
if (is_attacking == false and enemy.y:to_int() == LPCS_DOWN) then
return nil
end
up_down = -1
end
local y_dist = math.abs(ywPosY(pos) - ywPosY(pj_pos))
local x_dist = math.abs(ywPosX(pos) - ywPosX(pj_pos))
local mvx = pix_mv
local mvy = pix_mv
if x_dist < mvx then
mvx = x_dist
end
if y_dist < mvy then
mvy = y_dist
end
local ret = Pos.new(mvx * left_right, mvy * up_down).ent
if npc_check_col(main_widget_screen, colRect, ret) then
if x_dist > y_dist then
ret = Pos.new(mvx * left_right, 0).ent
if npc_check_col(main_widget_screen, colRect, ret) == false then
return ret
end
ret = Pos.new(mvx * left_right, -mvy * up_down).ent
if npc_check_col(main_widget_screen, colRect, ret) == false then
return ret
end
ret = Pos.new(0, mvy * up_down).ent
if npc_check_col(main_widget_screen, colRect, ret) == false then
return ret
end
else
ret = Pos.new(0, mvy * up_down).ent
if npc_check_col(main_widget_screen, colRect, ret) == false then
return ret
end
ret = Pos.new(-mvx * left_right, mvy * up_down).ent
if npc_check_col(main_widget_screen, colRect, ret) == false then
return ret
end
ret = Pos.new(mvx * left_right, 0).ent
if npc_check_col(main_widget_screen, colRect, ret) == false then
return ret
end
end
return nil
end
return ret
end
function saveNpcCanvasMatadata(dst, npc_canvas)
dst = Entity.wrapp(dst)
npc_canvas = Entity.wrapp(npc_canvas)
dst.Collision = npc_canvas.Collision
dst.is_npc = npc_canvas.is_npc
dst.dialogue = yeGetString(npc_canvas.dialogue)
dst.current = npc_canvas.current
dst.agresive = npc_canvas.agresive
dst.st = npc_canvas.small_talk
dst.dc = npc_canvas.dialogue_condition
end
function restoreNpcCanvasMatadata(npc_canvas, src)
src = Entity.wrapp(src)
npc_canvas = Entity.wrapp(npc_canvas)
npc_canvas.Collision = src.Collision
npc_canvas.is_npc = src.is_npc
npc_canvas.dialogue = src.dialogue
npc_canvas.current = src.current
npc_canvas.agresive = src.agresive
npc_canvas.small_talk = src.st
npc_canvas.dialogue_condition = src.dc
end
function NpcTurn(wid)
local i = 0
local npc_act = wid.npc_act
local nb_na = yeLen(npc_act)
while i < nb_na do
if npc_act[i] then
wid.npc_act[i].controller(wid, npc_act[i])
end
i = i + 1
end
i = 0
while i < yeLen(wid.enemies) do
local enemy = wid.enemies[i]
local mv_pos
if yIsNil(enemy) then
goto loop_next;
end
mv_pos = searching(wid, enemy)
if (mv_pos and yuiRand() % 2) then
local ec = enemy.canvas
local tmp = Entity.new_array()
if yeGetIntAt(enemy, "ai_state") == ENEMY_IDLE then
enemy.mv_pix = 0
end
saveNpcCanvasMatadata(tmp, ec)
enemy.ai_state = ENEMY_ATTACKING
if math.abs(ywPosX(mv_pos)) > math.abs(ywPosY(mv_pos)) and
(enemy.y:to_int() == LPCS_UP or enemy.y:to_int() == LPCS_DOWN) then
if ywPosX(mv_pos) > 0 then
lpcs.handlerSetOrigXY(enemy, 0, LPCS_RIGHT)
else
lpcs.handlerSetOrigXY(enemy, 0, LPCS_LEFT)
end
elseif math.abs(ywPosX(mv_pos)) < math.abs(ywPosY(mv_pos)) and
(enemy.y:to_int() == LPCS_LEFT or enemy.y:to_int() == LPCS_RIGHT) then
if ywPosY(mv_pos) > 0 then
lpcs.handlerSetOrigXY(enemy, 0, LPCS_DOWN)
else
lpcs.handlerSetOrigXY(enemy, 0, LPCS_UP)
end
else
if (enemy.mv_pix > 20) then
enemy.mv_pix = 0
ylpcsHandlerNextStep(enemy)
else
enemy.mv_pix = enemy.mv_pix + ywSizeDistance(mv_pos)
end
end
ylpcsHandlerRefresh(enemy)
ylpcsHandlerMove(enemy, mv_pos)
ec = enemy.canvas
restoreNpcCanvasMatadata(ec, tmp)
end
:: loop_next ::
i = i + 1
end
end
local PjLeaveController_t = 0
function PjLeaveController(wid, action)
PjLeaveController_t = PjLeaveController_t + ywidGetTurnTimer()
if PjLeaveController_t > 20000 then
PjLeaveController_t = 0
else
return
end
wid = Entity.wrapp(wid)
action = Entity.wrapp(action)
local mv_tbl_idx = action[ACTION_MV_TBL_IDX]
local mvPos = action[ACTION_MV_TBL][mv_tbl_idx:to_int()]
local npc = action[ACTION_NPC]
if yIsNil(npc) or npc.char == nil then
return
end
local curPos = yGenericHandlerPos(npc)
local dif_x = ywPosX(mvPos) - ywPosX(curPos)
local dif_y = ywPosY(mvPos) - ywPosY(curPos)
action[ACTION_MV_TBL_IDX] = action[ACTION_MV_TBL_IDX] + 1
if mvPos == nil then
local end_f = action.end_f
if (end_f) then
end_f(wid, npc)
else
ywCanvasRemoveObj(npc.wid, npc.canvas)
yeRemoveChild(main_widget.npcs, npc)
end
yeRemoveChild(wid.npc_act, action)
return
end
-- if checkcolision still todo:
npc.move.left_right = 1
if math.abs(dif_y) == 0 then
if dif_x > 0 then
npc.y = LPCS_RIGHT
else
npc.y = LPCS_LEFT
end
end
if math.abs(dif_x) == 0 then
if dif_y < 0 then
npc.y = LPCS_UP
else
npc.y = LPCS_DOWN
end
end
local tmp = Entity.new_array()
saveNpcCanvasMatadata(tmp, npc.canvas)
if yeGetString(npc.char.type) == "sprite" then
yGenericSetDir(npc, npc.y)
sprite_man.handlerSetPos(npc, mvPos)
sprite_man.handlerRefresh(npc)
else
walkDoStep(wid, npc)
ylpcsHandlerSetPos(npc, mvPos)
end
restoreNpcCanvasMatadata(npc.canvas, tmp)
end
function NpcGoToTbl(npc, tbl, end_f)
local main = main_widget
local action = Entity.new_array()
npc = Entity.wrapp(npc)
if speed == nil then
speed = 1
end
npc.move = {}
action[ACTION_NPC] = npc
action[ACTION_MV_TBL] = tbl
action[ACTION_MV_TBL_IDX] = 0
if end_f then
action.end_f = end_f
end
action.controller = Entity.new_func(PjLeaveController)
if main.npc_act == nil then
main.npc_act = {}
end
yePush(main.npc_act, action)
end
function NpcGoTo(npc, dest_pos, speed, end_f)
local tbl = Entity.new_array()
if speed == nil then
speed = 1
end
ywCanvasDoPathfinding(main_widget_screen, ywCanvasObjPos(npc.canvas),
dest_pos,
Pos.new(PIX_PER_FRAME * speed, PIX_PER_FRAME * speed).ent,
tbl)
NpcGoToTbl(npc, tbl, end_f)
end
function pushPjLeave(npc, entryPoint)
local main = main_widget
if main.npc_act == nil then
main.npc_act = {}
end
local action = Entity.new_array(main.npc_act)
local exit = main.exits[yeGetInt(entryPoint)]
npc.move = {}
action[ACTION_NPC] = npc
action[ACTION_MV_TBL] = {}
action[ACTION_MV_TBL_IDX] = 0
ywCanvasDoPathfinding(main_widget_screen, ywCanvasObjPos(npc.canvas),
exit.rect,
Pos.new(PIX_PER_FRAME, PIX_PER_FRAME).ent,
action[ACTION_MV_TBL])
action.controller = Entity.new_func(PjLeaveController)
end
function PjLeave(owid, eve, entryPoint)
wid = Entity.wrapp(yDialogueGetMain(owid))
local main = Entity.wrapp(ywCntWidgetFather(wid))
local npc = main.npcs[wid.npc_nb:to_int()]
if main.npc_act == nil then
main.npc_act = {}
end
local action = Entity.new_array(main.npc_act)
local exit = main.exits[yeGetInt(entryPoint)]
npc.move = {}
action[ACTION_NPC] = npc
action[ACTION_MV_TBL] = {}
action[ACTION_MV_TBL_IDX] = 0
ywCanvasDoPathfinding(main_widget_screen, ywCanvasObjPos(npc.canvas),
exit.rect,
Pos.new(PIX_PER_FRAME, PIX_PER_FRAME).ent,
action[ACTION_MV_TBL])
action.controller = Entity.new_func(PjLeaveController)
backToGame(owid, eve, arg)
end
function calsses_event_dialog_gen(_wid, cur_dialogue)
local pj = phq.pj
cur_dialogue = Entity.wrapp(cur_dialogue)
cur_dialogue["answer"] = "(ok, nice...)"
if yuiRand() % 5 == 0 then
cur_dialogue["text"] = "when going to class, someone try steal you"
cur_dialogue["answer"] = Entity.new_array()
cur_dialogue["answer"]["text"] = "(fight)"
cur_dialogue["answer"]["action"] = {"phq.StartFight", "Thrug"}
return
end
if phq_pc.trait.lazy > 3 and yuiRand() % 3 == 0 then
cur_dialogue["text"] = "You slack off durring class"
increaseStat(nil, phq.pj.reputation, "glandeur", 1)
return
end
local whos = {}
local whos_names = {}
local whos_nb = 1
--local ph_year = yeGetIntAt(pj, "student_year")
for i = 0, yeLen(npcs) - 1 do
local npc = yeGet(npcs, i)
local npc_names = yeGetKeyAt(npcs, i)
if yeGetIntAt(npc, "student_year") == yeGetIntAt(pj, "student_year") and
yeGetIntAt(npc, "class") == yeGetIntAt(pj, "class") then
whos[whos_nb] = Entity.wrapp(npc)
whos_names[whos_nb] = npc_names
whos_nb = whos_nb + 1
end
end
local target = nil
local target_i = 0
repeat
target_i = yuiRand() % whos_nb + 1
target = whos[target_i]
until yIsNNil(target)
if yIsNil(target.relation) then
target.relation = {}
end
if yIsNil(target.relation.affection) then
target.relation.affection = 0
end
target.relation.affection = target.relation.affection + 1
cur_dialogue["text"] = "you chat with " .. whos_names[target_i] ..
"\n(current affection: " .. yeGetInt(target.relation.affection) .. ")";
end