summaryrefslogtreecommitdiffstats
path: root/example/scripts/monster/basic_ai.lua
blob: 9c35f42c2aca2bf764ab9c5cbe1c174052ea2280 (plain)
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
--[[

 Basic stroll ai

--]]

local STROLL_TIMEOUT = 20
local STROLL_TIMEOUT_RANDOMNESS = 10

-- Wrapping the monster update callback in order to do the stroll ai here
local old_on_update = MonsterClass.on_update
local update_functions = {}
function MonsterClass:on_update(callback)
    update_functions[self] = callback
end

local stroll_timer = {}
local angerlist = {}

local mob_config = require "scripts/monster/settings"

local function find_target(mob, config)
    local target
    local target_priority
    local attack_x, attack_y

    for _, being in ipairs(get_beings_in_circle(mob, config.trackrange)) do
        if being:type() == OBJECT_CHARACTER
           and being:action() ~= ACTION_DEAD
        then
            local anger = angerlist[being] or 0
            if anger == 0 and config.aggressive then
                anger = 1
            end

            local possible_attack_positions = {
                {
                    x = being:x() - config.attack_distance or TILESIZE,
                    y = being:y()
                },
                {

                    x = being:x()
                    y = being:y() - config.attack_distance or TILESIZE,
                },
                {

                    x = being:x() + config.attack_distance or TILESIZE,
                    y = being:y(),
                },
                {

                    x = being:x()
                    y = being:y() + config.attack_distance or TILESIZE,
                },
            }
            for _, point in ipairs(possible_attack_positions) do
                local priority = calculate_position_priority(mob:position(), point.x, point.y)
            end
            
           

        end
    end
end

local function stroll_update(mob, tick)
    local stroll_tick = stroll_timer[mob]
    local mobconfig = mob_config[mob:name()]


    local trackrange = mobconfig and mobconfig.trackrange or nil


    local strollrange = mobconfig and mobconfig.strollrange or nil
    if (not stroll_tick or stroll_tick <= tick) and strollrange then
        local x, y = mob:position()
        local destination_x = math.random(x - strollrange, x + strollrange)
        local destination_y = math.random(y - strollrange, y + strollrange)
        if is_walkable(destination_x, destination_y) then
            mob:walk(destination_x, destination_y)
        end
        stroll_timer[mob] = tick + STROLL_TIMEOUT
                            + math.random(STROLL_TIMEOUT_RANDOMNESS)
    end

    local monsterclass = get_monster_class(mob:monster_id())
    local update_function = update_functions[monsterclass]
    if update_function then
        return update_function(mob, tick)
    end
end

-- Register all update functions for strolling
for _, monsterclass in ipairs(get_monster_classes()) do
    old_on_update(monsterclass, stroll_update)
end