· 5 years ago · Apr 11, 2020, 09:04 PM
1// Initialize global variables.
2num_players <- 0;
3list_players <- [];
4_equip <- null; // game_player_equip entity we will use to spawn weapons.
5most_recent_pistol <- "";
6initial_zombies_spawned <- false;
7
8// Make weapon tables.
9STARTING_PISTOL_TABLE <- {}; // This will be a weighted table for startiing pistols.
10TIER2_PISTOL_TABLE <- {}; // This will be a weighted table for Tier 2 pistols.
11
12// Populate Starting Pistol table. (Higher number means more likely to be picked.)
13STARTING_PISTOL_TABLE["glock"] <- 25;
14STARTING_PISTOL_TABLE["usp_silencer"] <- 25;
15STARTING_PISTOL_TABLE["hkp2000"] <- 25;
16STARTING_PISTOL_TABLE["p250"] <- 25;
17STARTING_PISTOL_TABLE["deagle"] <- 3;
18STARTING_PISTOL_TABLE["revolver"] <- 3;
19STARTING_PISTOL_TABLE["elite"] <- 3;
20STARTING_PISTOL_TABLE["fiveseven"] <- 3;
21STARTING_PISTOL_TABLE["tec9"] <- 3;
22STARTING_PISTOL_TABLE["cz75a"] <- 3;
23
24// Populate Tier 2 Pistol table.
25TIER2_PISTOL_TABLE["cz75a"] <- 3;
26TIER2_PISTOL_TABLE["tec9"] <- 3;
27TIER2_PISTOL_TABLE["fiveseven"] <- 3;
28TIER2_PISTOL_TABLE["deagle"] <- 2;
29TIER2_PISTOL_TABLE["revolver"] <- 2;
30TIER2_PISTOL_TABLE["elite"] <- 2;
31
32
33function startRound() {
34 // On the start of the round, get the initial player count.
35 updatePlayerCount();
36 printl(format("New round has started, %d players total detected.", num_players));
37 initial_zombies_spawned = false;
38
39 // local random_tier2_pistol = pickRandom(TIER2_PISTOL_TABLE);
40 // local ent = null;
41 // ent = Entities.FindByName(ent, "template_deagle")
42 // EntFireByHandle(ent, "ForceSpawnAtEntityOrigin", "tier2_pistol", 0, null, null);
43
44 return;
45}
46
47function giveStartingPistol() {
48 // Give the activator of this function a random starting pistol.
49 local pistol_name = pickRandom(STARTING_PISTOL_TABLE);
50
51 // If the pistol that was chosen is the same as the previous, pick a new one.
52 while(pistol_name == most_recent_pistol)
53 pistol_name = pickRandom(STARTING_PISTOL_TABLE);
54
55 // Update most recent pistol and then equip it.
56 most_recent_pistol = pistol_name;
57 equipWeapon(pistol_name);
58 return;
59}
60
61function spawnZombieWave() {
62 // Spawn a wave of zombie bots, scaled to the number of players.
63 local num_zombies = 1; // Default is 1 zombie per wave.
64
65 // Make sure the player count we are basing this wave off of is accurate.
66 updatePlayerCount();
67
68 // If there are 3 or more players, spawn 2 zombies per wave.
69 if(num_players > 2)
70 num_zombies = 2;
71
72 // If there are 5 or more players, spawn 3 zombies per wave.
73 if(num_players > 5)
74 num_zombies = 3;
75
76 // Log how many zombies we are spawning in the console.
77 local log_msg = "Spawning a zombie wave! %d players detected, spawning %d zombies.";
78 printl(format(log_msg, num_players, num_zombies));
79
80 // Spawn the appropriate number of zombies.
81 for(local i = 0; i < num_zombies; i++) {
82 // Since there are limited "Expert" bot profiles, use "hard" for 2nd zombie.
83 if(i == 1)
84 spawnZombie("hard");
85 // Otherwise use "Expert" difficulty most of the time.
86 else
87 spawnZombie("expert");
88 }
89
90 return;
91}
92
93function spawnInitialZombies() {
94 // Spawn the initial number of zombies on the map.
95 if(initial_zombies_spawned){
96 printl("Initial zombies have already spawned this round.")
97 return;
98 }
99
100 updatePlayerCount();
101
102 local base_zombies = 1; // Automatically start off with this many zombies.
103 local num_intial_zombies = base_zombies + (num_players * 2);
104
105 printl(format("Spawning %d initial zombies.", num_intial_zombies));
106
107 for(local i = 0; i < num_intial_zombies; i++)
108 spawnZombie("expert");
109
110 initial_zombies_spawned = true;
111 return;
112}
113
114function updatePlayerCount() {
115 num_players = 0;
116 list_players = [];
117 local player = null;
118
119 while (Entities.FindByClassname(player, "player") != null) {
120 player = Entities.FindByClassname(player, "player");
121
122 if (player.GetTeam() == 3) {
123 list_players.append(player);
124 num_players = num_players + 1;
125 }
126 }
127}
128
129function spawnZombie(difficulty = "hard") {
130 // Spawn a single zombie with the given difficulty.
131 SendToConsole("bot_add t " + difficulty);
132 printl(format("Executing 'bot_add t %s' on server.", difficulty));
133
134 return;
135}
136
137function pickRandom(weighted_table) {
138 // Pick a random key from a given weighted table.
139 if(weighted_table.rawin("weighted_list") == false)
140 // Initialize table if it doesn't have the weighted list generated.
141 initWeightedTable(weighted_table);
142
143 local weighted_list = weighted_table["weighted_list"];
144
145 local random_index = RandomInt(0, weighted_list.len());
146
147 return weighted_list[random_index];
148}
149
150function initWeightedTable(weighted_table) {
151 // Initialize a weighted table for being used.
152 local weighted_list = []; // List of indexes for each weight.
153
154 foreach(key, value in weighted_table) {
155 for(local i = 0; i < value; i++){
156 weighted_list.append(key);
157 }
158 }
159 weighted_list.sort();
160 weighted_table["weighted_list"] <- weighted_list;
161 return;
162}
163
164function equipWeapon(weapon_name) {
165 // Equip the activating player with a specified weapon.
166 printl("Giving player a " + weapon_name);
167
168 //Check whether our game_player_equip exists and is configured, if not create one
169 if(_equip == null)
170 {
171 _equip <- Entities.CreateByClassname("game_player_equip");
172 _equip.__KeyValueFromInt("spawnflags", 5); //Strip same weapon type, use only flags
173 }
174
175 // Trigger the game_player_equip to give the activating player the weapon.
176 EntFireByHandle(_equip, "TriggerForActivatedPlayer", "weapon_" + weapon_name,
177 0, activator, activator);
178 return;
179}
180
181function printList(given_list) {
182 // Print contents of given list/array.e
183 printl("List contents:")
184 foreach(i in given_list){
185 printl(i.tostring())
186 }
187}