aboutsummaryrefslogtreecommitdiff
path: root/gamemode/npcsystem/aidirector.lua
blob: fb85b2d8136d79948201f6a331228eae392c9198 (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
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
--Lol i dunno, spawn some npc's or something

concommand.Add("ws_spawnzomb",function(ply, cmd, args)
    SpawnNpcByName("Zombie",ply:GetPos())
end)

concommand.Add("ws_spawnbird",function(ply,cmd,args)
    SpawnNpcByName("Bird",ply:GetPos())
end)

concommand.Add("ws_spawnnpc",function(ply,cmd,args)
    if(!args[1]) then print("You must enter the name of an npc")
    return end

    local npc = GetNpcByName(args[1])
    if(npc == nil) then print("Not a valid name!")
    return end
    print("attempting to spawn npc...")
    SpawnNpcByName(args[1],ply:GetPos())
    print("NPC spawned!")
end, function(cmd,stringargs)
    print(cmd, stringargs)
    local tbl = {}
    stringargs = string.sub(stringargs,2)
    if(stringargs == " ") then
        for k,v in pairs(GAMEMODE.Npcs) do
            table.insert(tbl,cmd .. " \"" .. v.Name .. "\"")
        end
        return tbl
    end
    for k,v in pairs(GAMEMODE.Npcs) do
        if(string.find(string.lower(v.Name),stringargs)) then
            table.insert(tbl,cmd .. " \"" .. v.Name .. "\"")
        else
            print("Could not find " .. stringargs .. " in " .. string.lower(v.Name))
        end
    end
    return tbl
end, "Spawns an NPC from winter survival, useage: ws_spawnnpc <name>")

function SpawnNpcByName(name, position)
    if(CLIENT) then return end
    entdata = GetNpcByName(name)
    if not entdata then
        print("Could not find npc data for name " .. name)
        return
    end
    if not position then
        print("Invalid position:")
        print(position)
        return
    end
    ent = ents.Create("ws_npc_ambient")
    if(not ent) then
        print("Could not spawn npc ws_npc_ambient")
        return
    end
    ent:SetPos(position)
    print("From server!")
    if(entdata.Name) then
        ent.Name = entdata.Name
    end
    if(entdata.Model) then
        ent.Model = entdata.Model
    end
    if(entdata.Drops) then
        ent.Drops = entdata.Drops
    end
    if(entdata.OnDammage) then
        ent.OnDammage = entdata.OnDammage
    end
    if(entdata.Behave) then
        ent.Behave = entdata.Behave
    end
    if(entdata.Act) then
        ent.Act = entdata.Act
    end
    if(entdata.Stats) then
        ent.Stats = entdata.Stats
    end
    if(entdata.IdleSequences) then
        ent.IdleSequences = entdata.IdleSequences
    end
    if(entdata.Attacks) then
        ent.Attacks = entdata.Attacks
    end
    if(entdata.AttackPriority) then
        ent.AttackPriority = entdata.AttackPriority
    end
    if(entdata.AwareEnemies) then
        ent.AwareEnemies = entdata.AwareEnemies
    end
    if(entdata.OnSpawn) then
        ent.OnSpawn = entdata.OnSpawn
    end
    print("NPC created")
    ent:Spawn()
    print("NPC spawned")
end

local random = math.random
local traceline = util.TraceLine
local contents  = util.PointContents
local Up 		= Vector(0,0,1)

--Randomly spawn npc's around?
local ambientnpcs = {
    [0] = "Bird",
    [1] = "Zombie",
    [2] = "Antlion Scout",
    [3] = "Antlion Pouncer"
}
local Tick = CurTime()
hook.Add("Tick","SpawnAmbient",function()
    if(CLIENT) then return end
    if (Tick < CurTime()) then
        --Assume most players are about ground level
        --Spawn 1 bird for each player on the server
        local areas = {}
        for k,v in pairs(player.GetAllHumans()) do

            for i,area in pairs(ents.FindByClass("info_target")) do
                if (area:GetName() == "survival_spawn") then
                    local parent = area:GetParent()
                    if (IsValid(parent)) then
                        areas[area] = parent
                    end
                end
            end
            --SpawnNpcByName("Bird",v:GetPos() + (v:GetUp()*500))
        end

        for pAe,pBe in pairs(areas) do
            local pA,pB = pAe:GetPos(),pBe:GetPos()
            local Dis = pA:Distance(pB)

            local V = Vector(random(pB.x,pA.x),random(pB.y,pA.y),random(pB.z,pA.z))
            local Tr = traceline({start=V,endpos=V-Up*40000})
            local Pos = Tr.HitPos+Up*20
            local C  = contents(Pos)

            if (C != CONTENTS_WATER and C != CONTENTS_WATER+CONTENTS_TRANSLUCENT) then
                --print("Appropriate place found, spawning bird)")
                local randnpcnum = math.Round(math.Rand(0, #ambientnpcs))
                local npc = GetNpcByName(ambientnpcs[randnpcnum])
                if(npc:SpawnLocations(Pos)) then
                    SpawnNpcByName(ambientnpcs[randnpcnum],Pos)
                end
                break
            end
        end

        Tick = CurTime()+30
    end
end)