diff --git a/bam.lua b/bam.lua index 11ac7b9..36d9a47 100644 --- a/bam.lua +++ b/bam.lua @@ -1,5 +1,3 @@ -CheckVersion("0.4") - Import("configure.lua") Import("other/sdl/sdl.lua") Import("other/freetype/freetype.lua") @@ -20,7 +18,7 @@ function Script(name) if family == "windows" then return str_replace(name, "/", "\\") end - return "python " .. name + return "python3 " .. name end function CHash(output, ...) diff --git a/configure.lua b/configure.lua index 2db70cc..31444ee 100644 --- a/configure.lua +++ b/configure.lua @@ -1,502 +1,565 @@ +function loadfile_(filename, env) + local file + if _VERSION == "Lua 5.1" then + file = loadfile(filename) + setfenv(file, env) + else + file = loadfile(filename, nil, env) + end + return file +end ---[[@GROUP Configuration@END]]-- +--[[@GROUP Configuration@END]] -- --[[@FUNCTION TODO -@END]]-- +@END]] -- function NewConfig(on_configured_callback) - local config = {} - - config.OnConfigured = function(self) - return true - end - - if on_configured_callback then config.OnConfigured = on_configured_callback end - - config.options = {} - config.settings = NewSettings() - - config.NewSettings = function(self) - local s = NewSettings() - for _,v in pairs(self.options) do - v:Apply(s) - end - return s - end - - config.Add = function(self, o) - table.insert(self.options, o) - self[o.name] = o - end - - config.Print = function(self) - for k,v in pairs(self.options) do - print(v:FormatDisplay()) - end - end - - config.Save = function(self, filename) - print("saved configuration to '"..filename.."'") - local file = io.open(filename, "w") - - -- Define a little helper function to save options - local saver = {} - saver.file = file - - saver.line = function(self, str) - self.file:write(str .. "\n") - end - - saver.option = function(self, option, name) - local valuestr = "no" - if type(option[name]) == type(0) then - valuestr = option[name] - elseif type(option[name]) == type(true) then - valuestr = "false" - if option[name] then - valuestr = "true" - end - elseif type(option[name]) == type("") then - valuestr = "'"..option[name].."'" - else - error("option "..name.." have a value of type ".. type(option[name]).." that can't be saved") - end - self.file:write(option.name.."."..name.." = ".. valuestr.."\n") - end - - -- Save all the options - for k,v in pairs(self.options) do - v:Save(saver) - end - file:close() - end - - config.Load = function(self, filename) - local options_func = loadfile(filename) - local options_table = {} - - if not options_func then - print("auto configuration") - self:Config(filename) - options_func = loadfile(filename) - end - - if options_func then - -- Setup the options tables - for k,v in pairs(self.options) do - options_table[v.name] = {} - end - setfenv(options_func, options_table) - - -- this is to make sure that we get nice error messages when - -- someone sets an option that isn't valid. - local mt = {} - mt.__index = function(t, key) - local v = rawget(t, key) - if v ~= nil then return v end - error("there is no configuration option named '" .. key .. "'") - end - - setmetatable(options_table, mt) - - -- Process the options - options_func() - - -- Copy the options - for k,v in pairs(self.options) do - if options_table[v.name] then - for k2,v2 in pairs(options_table[v.name]) do - v[k2] = v2 - end - v.auto_detected = false - end - end - else - print("error: no '"..filename.."' found") - print("") - print("run 'bam config' to generate") - print("run 'bam config help' for configuration options") - print("") - os.exit(1) - end - end - - config.Config = function(self, filename) - print("") - print("configuration:") - if _bam_targets[1] == "print" then - self:Load(filename) - self:Print() - print("") - print("notes:") - self:OnConfigured() - print("") - else - self:Autodetect() - print("") - print("notes:") - if self:OnConfigured() then - self:Save(filename) - end - print("") - end - - end - - config.Autodetect = function(self) - for k,v in pairs(self.options) do - v:Check(self.settings) - print(v:FormatDisplay()) - self[v.name] = v - end - end - - config.PrintHelp = function(self) - print("options:") - for k,v in pairs(self.options) do - if v.PrintHelp then - v:PrintHelp() - end - end - end - - config.Finalize = function(self, filename) - if _bam_targets[0] == "config" then - if _bam_targets[1] == "help" then - self:PrintHelp() - os.exit(0) - end - - self:Config(filename) - - os.exit(0) - end - - self:Load(filename) - bam_update_globalstamp(filename) - end - - return config + local config = {} + + config.OnConfigured = function(self) + return true + end + + if on_configured_callback then + config.OnConfigured = on_configured_callback + end + + config.options = {} + config.settings = NewSettings() + + config.NewSettings = function(self) + local s = NewSettings() + for _, v in pairs(self.options) do + v:Apply(s) + end + return s + end + + config.Add = function(self, o) + table.insert(self.options, o) + self[o.name] = o + end + + config.Print = function(self) + for k, v in pairs(self.options) do + print(v:FormatDisplay()) + end + end + + config.Save = function(self, filename) + print("saved configuration to '" .. filename .. "'") + local file = io.open(filename, "w") + + -- Define a little helper function to save options + local saver = {} + saver.file = file + + saver.line = function(self, str) + self.file:write(str .. "\n") + end + + saver.option = function(self, option, name) + local valuestr = "no" + if type(option[name]) == type(0) then + valuestr = option[name] + elseif type(option[name]) == type(true) then + valuestr = "false" + if option[name] then + valuestr = "true" + end + elseif type(option[name]) == type("") then + valuestr = "'" .. option[name] .. "'" + else + error("option " .. name .. " have a value of type " .. type(option[name]) .. " that can't be saved") + end + self.file:write(option.name .. "." .. name .. " = " .. valuestr .. "\n") + end + + -- Save all the options + for k, v in pairs(self.options) do + v:Save(saver) + end + file:close() + end + + config.Load = function(self, filename) + local options_table = {} + local options_func = loadfile_(filename, options_table) + + if not options_func then + print("auto configuration") + self:Config(filename) + options_func = loadfile_(filename, options_table) + end + + if options_func then + -- Setup the options tables + for k, v in pairs(self.options) do + options_table[v.name] = {} + end + + -- this is to make sure that we get nice error messages when + -- someone sets an option that isn't valid. + local mt = {} + mt.__index = function(t, key) + local v = rawget(t, key) + if v ~= nil then + return v + end + error("there is no configuration option named '" .. key .. "'") + end + + setmetatable(options_table, mt) + + -- Process the options + options_func() + + -- Copy the options + for k, v in pairs(self.options) do + if options_table[v.name] then + for k2, v2 in pairs(options_table[v.name]) do + v[k2] = v2 + end + v.auto_detected = false + end + end + else + print("error: no '" .. filename .. "' found") + print("") + print("run 'bam config' to generate") + print("run 'bam config help' for configuration options") + print("") + os.exit(1) + end + end + + config.Config = function(self, filename) + print("") + print("configuration:") + if _bam_targets[1] == "print" then + self:Load(filename) + self:Print() + print("") + print("notes:") + self:OnConfigured() + print("") + else + self:Autodetect() + print("") + print("notes:") + if self:OnConfigured() then + self:Save(filename) + end + print("") + end + + end + + config.Autodetect = function(self) + for k, v in pairs(self.options) do + v:Check(self.settings) + print(v:FormatDisplay()) + self[v.name] = v + end + end + + config.PrintHelp = function(self) + print("options:") + for k, v in pairs(self.options) do + if v.PrintHelp then + v:PrintHelp() + end + end + end + + config.Finalize = function(self, filename) + if _bam_targets[0] == "config" then + if _bam_targets[1] == "help" then + self:PrintHelp() + os.exit(0) + end + + self:Config(filename) + + os.exit(0) + end + + self:Load(filename) + bam_update_globalstamp(filename) + end + + return config end - -- Helper functions -------------------------------------- function DefaultOptionDisplay(option) - if not option.value then return "no" end - if option.value == 1 or option.value == true then return "yes" end - return option.value + if not option.value then + return "no" + end + if option.value == 1 or option.value == true then + return "yes" + end + return option.value end function IsNegativeTerm(s) - if s == "no" then return true end - if s == "false" then return true end - if s == "off" then return true end - if s == "disable" then return true end - if s == "0" then return true end - return false + if s == "no" then + return true + end + if s == "false" then + return true + end + if s == "off" then + return true + end + if s == "disable" then + return true + end + if s == "0" then + return true + end + return false end function IsPositiveTerm(s) - if s == "yes" then return true end - if s == "true" then return true end - if s == "on" then return true end - if s == "enable" then return true end - if s == "1" then return true end - return false + if s == "yes" then + return true + end + if s == "true" then + return true + end + if s == "on" then + return true + end + if s == "enable" then + return true + end + if s == "1" then + return true + end + return false end function MakeOption(name, value, check, save, display, printhelp) - local o = {} - o.name = name - o.value = value - o.Check = check - o.Save = save - o.auto_detected = true - o.FormatDisplay = function(self) - local a = "SET" - if self.auto_detected then a = "AUTO" end - return string.format("%-5s %-20s %s", a, self.name, self:Display()) - end - - o.Display = display - o.PrintHelp = printhelp - if o.Display == nil then o.Display = DefaultOptionDisplay end - return o + local o = {} + o.name = name + o.value = value + o.Check = check + o.Save = save + o.auto_detected = true + o.FormatDisplay = function(self) + local a = "SET" + if self.auto_detected then + a = "AUTO" + end + return string.format("%-5s %-20s %s", a, self.name, self:Display()) + end + + o.Display = display + o.PrintHelp = printhelp + if o.Display == nil then + o.Display = DefaultOptionDisplay + end + return o end - -- Test Compile C -------------------------------------- function OptTestCompileC(name, source, compileoptions, desc) - local check = function(option, settings) - option.value = false - if ScriptArgs[option.name] then - if IsNegativeTerm(ScriptArgs[option.name]) then - option.value = false - elseif IsPositiveTerm(ScriptArgs[option.name]) then - option.value = true - else - error(ScriptArgs[option.name].." is not a valid value for option "..option.name) - end - option.auto_detected = false - else - if CTestCompile(settings, option.source, option.compileoptions) then - option.value = true - end - end - end - - local save = function(option, output) - output:option(option, "value") - end - - local printhelp = function(option) - print("\t"..option.name.."=on|off") - if option.desc then print("\t\t"..option.desc) end - end - - local o = MakeOption(name, false, check, save, nil, printhelp) - o.desc = desc - o.source = source - o.compileoptions = compileoptions - return o + local check = function(option, settings) + option.value = false + if ScriptArgs[option.name] then + if IsNegativeTerm(ScriptArgs[option.name]) then + option.value = false + elseif IsPositiveTerm(ScriptArgs[option.name]) then + option.value = true + else + error(ScriptArgs[option.name] .. " is not a valid value for option " .. option.name) + end + option.auto_detected = false + else + if CTestCompile(settings, option.source, option.compileoptions) then + option.value = true + end + end + end + + local save = function(option, output) + output:option(option, "value") + end + + local printhelp = function(option) + print("\t" .. option.name .. "=on|off") + if option.desc then + print("\t\t" .. option.desc) + end + end + + local o = MakeOption(name, false, check, save, nil, printhelp) + o.desc = desc + o.source = source + o.compileoptions = compileoptions + return o end - -- OptToggle -------------------------------------- function OptToggle(name, default_value, desc) - local check = function(option, settings) - if ScriptArgs[option.name] then - if IsNegativeTerm(ScriptArgs[option.name]) then - option.value = false - elseif IsPositiveTerm(ScriptArgs[option.name]) then - option.value = true - else - error(ScriptArgs[option.name].." is not a valid value for option "..option.name) - end - end - end - - local save = function(option, output) - output:option(option, "value") - end - - local printhelp = function(option) - print("\t"..option.name.."=on|off") - if option.desc then print("\t\t"..option.desc) end - end - - local o = MakeOption(name, default_value, check, save, nil, printhelp) - o.desc = desc - return o + local check = function(option, settings) + if ScriptArgs[option.name] then + if IsNegativeTerm(ScriptArgs[option.name]) then + option.value = false + elseif IsPositiveTerm(ScriptArgs[option.name]) then + option.value = true + else + error(ScriptArgs[option.name] .. " is not a valid value for option " .. option.name) + end + end + end + + local save = function(option, output) + output:option(option, "value") + end + + local printhelp = function(option) + print("\t" .. option.name .. "=on|off") + if option.desc then + print("\t\t" .. option.desc) + end + end + + local o = MakeOption(name, default_value, check, save, nil, printhelp) + o.desc = desc + return o end -- OptInteger -------------------------------------- function OptInteger(name, default_value, desc) - local check = function(option, settings) - if ScriptArgs[option.name] then - option.value = tonumber(ScriptArgs[option.name]) - end - end - - local save = function(option, output) - output:option(option, "value") - end - - local printhelp = function(option) - print("\t"..option.name.."=N") - if option.desc then print("\t\t"..option.desc) end - end - - local o = MakeOption(name, default_value, check, save, nil, printhelp) - o.desc = desc - return o + local check = function(option, settings) + if ScriptArgs[option.name] then + option.value = tonumber(ScriptArgs[option.name]) + end + end + + local save = function(option, output) + output:option(option, "value") + end + + local printhelp = function(option) + print("\t" .. option.name .. "=N") + if option.desc then + print("\t\t" .. option.desc) + end + end + + local o = MakeOption(name, default_value, check, save, nil, printhelp) + o.desc = desc + return o end - -- OptString -------------------------------------- function OptString(name, default_value, desc) - local check = function(option, settings) - if ScriptArgs[option.name] then - option.value = ScriptArgs[option.name] - end - end - - local save = function(option, output) - output:option(option, "value") - end - - local printhelp = function(option) - print("\t"..option.name.."=STRING") - if option.desc then print("\t\t"..option.desc) end - end - - local o = MakeOption(name, default_value, check, save, nil, printhelp) - o.desc = desc - return o + local check = function(option, settings) + if ScriptArgs[option.name] then + option.value = ScriptArgs[option.name] + end + end + + local save = function(option, output) + output:option(option, "value") + end + + local printhelp = function(option) + print("\t" .. option.name .. "=STRING") + if option.desc then + print("\t\t" .. option.desc) + end + end + + local o = MakeOption(name, default_value, check, save, nil, printhelp) + o.desc = desc + return o end -- Find Compiler -------------------------------------- --[[@FUNCTION TODO -@END]]-- +@END]] -- function OptCCompiler(name, default_driver, default_c, default_cxx, desc) - local check = function(option, settings) - if ScriptArgs[option.name] then - -- set compile driver - option.driver = ScriptArgs[option.name] - - -- set c compiler - if ScriptArgs[option.name..".c"] then - option.c_compiler = ScriptArgs[option.name..".c"] - end - - -- set c+= compiler - if ScriptArgs[option.name..".cxx"] then - option.cxx_compiler = ScriptArgs[option.name..".cxx"] - end - - option.auto_detected = false - elseif option.driver then - -- no need todo anything if we have a driver - -- TODO: test if we can find the compiler - else - if ExecuteSilent("cl") == 0 then - option.driver = "cl" - elseif ExecuteSilent("g++ -v") == 0 then - option.driver = "gcc" - else - error("no c/c++ compiler found") - end - end - --setup_compiler(option.value) - end - - local apply = function(option, settings) - if option.driver == "cl" then - SetDriversCL(settings) - elseif option.driver == "gcc" then - SetDriversGCC(settings) - elseif option.driver == "clang" then - SetDriversClang(settings) - else - error(option.driver.." is not a known c/c++ compile driver") - end - - if option.c_compiler then settings.cc.c_compiler = option.c_compiler end - if option.cxx_compiler then settings.cc.cxx_compiler = option.cxx_compiler end - end - - local save = function(option, output) - output:option(option, "driver") - output:option(option, "c_compiler") - output:option(option, "cxx_compiler") - end - - local printhelp = function(option) - local a = "" - if option.desc then a = "for "..option.desc end - print("\t"..option.name.."=gcc|cl|clang") - print("\t\twhat c/c++ compile driver to use"..a) - print("\t"..option.name..".c=FILENAME") - print("\t\twhat c compiler executable to use"..a) - print("\t"..option.name..".cxx=FILENAME") - print("\t\twhat c++ compiler executable to use"..a) - end - - local display = function(option) - local s = option.driver - if option.c_compiler then s = s .. " c="..option.c_compiler end - if option.cxx_compiler then s = s .. " cxx="..option.cxx_compiler end - return s - end - - local o = MakeOption(name, nil, check, save, display, printhelp) - o.desc = desc - o.driver = false - o.c_compiler = false - o.cxx_compiler = false - - if default_driver then o.driver = default_driver end - if default_c then o.c_compiler = default_c end - if default_cxx then o.cxx_compiler = default_cxx end - - o.Apply = apply - return o + local check = function(option, settings) + if ScriptArgs[option.name] then + -- set compile driver + option.driver = ScriptArgs[option.name] + + -- set c compiler + if ScriptArgs[option.name .. ".c"] then + option.c_compiler = ScriptArgs[option.name .. ".c"] + end + + -- set c+= compiler + if ScriptArgs[option.name .. ".cxx"] then + option.cxx_compiler = ScriptArgs[option.name .. ".cxx"] + end + + option.auto_detected = false + elseif option.driver then + -- no need todo anything if we have a driver + -- TODO: test if we can find the compiler + else + if ExecuteSilent("cl") == 0 then + option.driver = "cl" + elseif ExecuteSilent("g++ -v") == 0 then + option.driver = "gcc" + else + error("no c/c++ compiler found") + end + end + -- setup_compiler(option.value) + end + + local apply = function(option, settings) + if option.driver == "cl" then + SetDriversCL(settings) + elseif option.driver == "gcc" then + SetDriversGCC(settings) + elseif option.driver == "clang" then + SetDriversClang(settings) + else + error(option.driver .. " is not a known c/c++ compile driver") + end + + if option.c_compiler then + settings.cc.c_compiler = option.c_compiler + end + if option.cxx_compiler then + settings.cc.cxx_compiler = option.cxx_compiler + end + end + + local save = function(option, output) + output:option(option, "driver") + output:option(option, "c_compiler") + output:option(option, "cxx_compiler") + end + + local printhelp = function(option) + local a = "" + if option.desc then + a = "for " .. option.desc + end + print("\t" .. option.name .. "=gcc|cl|clang") + print("\t\twhat c/c++ compile driver to use" .. a) + print("\t" .. option.name .. ".c=FILENAME") + print("\t\twhat c compiler executable to use" .. a) + print("\t" .. option.name .. ".cxx=FILENAME") + print("\t\twhat c++ compiler executable to use" .. a) + end + + local display = function(option) + local s = option.driver + if option.c_compiler then + s = s .. " c=" .. option.c_compiler + end + if option.cxx_compiler then + s = s .. " cxx=" .. option.cxx_compiler + end + return s + end + + local o = MakeOption(name, nil, check, save, display, printhelp) + o.desc = desc + o.driver = false + o.c_compiler = false + o.cxx_compiler = false + + if default_driver then + o.driver = default_driver + end + if default_c then + o.c_compiler = default_c + end + if default_cxx then + o.cxx_compiler = default_cxx + end + + o.Apply = apply + return o end -- Option Library -------------------------------------- --[[@FUNCTION TODO -@END]]-- +@END]] -- function OptLibrary(name, header, desc) - local check = function(option, settings) - option.value = false - option.include_path = false - - local function check_compile_include(filename, paths) - if CTestCompile(settings, "#include <" .. filename .. ">\nint main(){return 0;}", "") then - return "" - end - - for k,v in pairs(paths) do - if CTestCompile(settings, "#include <" .. filename .. ">\nint main(){return 0;}", "-I"..v) then - return v - end - end - - return false - end - - if ScriptArgs[option.name] then - if IsNegativeTerm(ScriptArgs[option.name]) then - option.value = false - elseif ScriptArgs[option.name] == "system" then - option.value = true - else - option.value = true - option.include_path = ScriptArgs[option.name] - end - option.auto_detected = false - else - option.include_path = check_compile_include(option.header, {}) - if option.include_path == false then - if option.required then - print(name.." library not found and is required") - error("required library not found") - end - else - option.value = true - option.include_path = false - end - end - end - - local save = function(option, output) - output:option(option, "value") - output:option(option, "include_path") - end - - local display = function(option) - if option.value then - if option.include_path then - return option.include_path - else - return "(in system path)" - end - else - return "not found" - end - end - - local printhelp = function(option) - print("\t"..option.name.."=disable|system|PATH") - if option.desc then print("\t\t"..option.desc) end - end - - local o = MakeOption(name, false, check, save, display, printhelp) - o.include_path = false - o.header = header - o.desc = desc - return o + local check = function(option, settings) + option.value = false + option.include_path = false + + local function check_compile_include(filename, paths) + if CTestCompile(settings, "#include <" .. filename .. ">\nint main(){return 0;}", "") then + return "" + end + + for k, v in pairs(paths) do + if CTestCompile(settings, "#include <" .. filename .. ">\nint main(){return 0;}", "-I" .. v) then + return v + end + end + + return false + end + + if ScriptArgs[option.name] then + if IsNegativeTerm(ScriptArgs[option.name]) then + option.value = false + elseif ScriptArgs[option.name] == "system" then + option.value = true + else + option.value = true + option.include_path = ScriptArgs[option.name] + end + option.auto_detected = false + else + option.include_path = check_compile_include(option.header, {}) + if option.include_path == false then + if option.required then + print(name .. " library not found and is required") + error("required library not found") + end + else + option.value = true + option.include_path = false + end + end + end + + local save = function(option, output) + output:option(option, "value") + output:option(option, "include_path") + end + + local display = function(option) + if option.value then + if option.include_path then + return option.include_path + else + return "(in system path)" + end + else + return "not found" + end + end + + local printhelp = function(option) + print("\t" .. option.name .. "=disable|system|PATH") + if option.desc then + print("\t\t" .. option.desc) + end + end + + local o = MakeOption(name, false, check, save, display, printhelp) + o.include_path = false + o.header = header + o.desc = desc + return o end diff --git a/other/freetype/freetype.lua b/other/freetype/freetype.lua index aa5af22..5cb4306 100644 --- a/other/freetype/freetype.lua +++ b/other/freetype/freetype.lua @@ -1,71 +1,88 @@ FreeType = { - basepath = PathDir(ModuleFilename()), - - OptFind = function (name, required) - local check = function(option, settings) - option.value = false - option.use_ftconfig = false - option.use_winlib = 0 - option.lib_path = nil - - if ExecuteSilent("freetype-config") > 0 and ExecuteSilent("freetype-config --cflags") == 0 then - option.value = true - option.use_ftconfig = true - end - - if platform == "win32" then - option.value = true - option.use_winlib = 32 - elseif platform == "win64" then - option.value = true - option.use_winlib = 64 - end - end - - local apply = function(option, settings) - -- include path - settings.cc.includes:Add(FreeType.basepath .. "/include") - - if option.use_ftconfig == true then - settings.cc.flags:Add("`freetype-config --cflags`") - settings.link.flags:Add("`freetype-config --libs`") - - elseif option.use_winlib > 0 then - if option.use_winlib == 32 then - settings.link.libpath:Add(FreeType.basepath .. "/lib32") - else - settings.link.libpath:Add(FreeType.basepath .. "/lib64") - end - settings.link.libs:Add("freetype") - end - end - - local save = function(option, output) - output:option(option, "value") - output:option(option, "use_ftconfig") - output:option(option, "use_winlib") - end - - local display = function(option) - if option.value == true then - if option.use_ftconfig == true then return "using freetype-config" end - if option.use_winlib == 32 then return "using supplied win32 libraries" end - if option.use_winlib == 64 then return "using supplied win64 libraries" end - return "using unknown method" - else - if option.required then - return "not found (required)" - else - return "not found (optional)" - end - end - end - - local o = MakeOption(name, 0, check, save, display) - o.Apply = apply - o.include_path = nil - o.lib_path = nil - o.required = required - return o - end + basepath = PathDir(ModuleFilename()), + + OptFind = function(name, required) + local check = function(option, settings) + option.value = false + option.use_ftconfig = false + option.use_pkgconfig = false + option.use_winlib = 0 + option.lib_path = nil + + if ExecuteSilent("freetype-config") > 0 and ExecuteSilent("freetype-config --cflags") == 0 then + option.value = true + option.use_ftconfig = true + elseif ExecuteSilent("pkg-config") > 0 and ExecuteSilent("pkg-config freetype2") == 0 then + option.value = true + option.use_pkgconfig = true + end + + if platform == "win32" then + option.value = true + option.use_winlib = 32 + elseif platform == "win64" then + option.value = true + option.use_winlib = 64 + end + end + + local apply = function(option, settings) + -- include path + settings.cc.includes:Add(FreeType.basepath .. "/include") + + if option.use_ftconfig == true then + settings.cc.flags:Add("`freetype-config --cflags`") + settings.link.flags:Add("`freetype-config --libs`") + elseif option.use_pkgconfig == true then + settings.cc.flags:Add("`pkg-config freetype2 --cflags`") + settings.link.flags:Add("`pkg-config freetype2 --libs`") + + elseif option.use_winlib > 0 then + if option.use_winlib == 32 then + settings.link.libpath:Add(FreeType.basepath .. "/windows/lib32") + else + settings.link.libpath:Add(FreeType.basepath .. "/windows/lib64") + end + settings.link.libs:Add("freetype") + end + end + + local save = function(option, output) + output:option(option, "value") + output:option(option, "use_ftconfig") + output:option(option, "use_pkgconfig") + output:option(option, "use_winlib") + end + + local display = function(option) + if option.value == true then + if option.use_ftconfig == true then + return "using freetype-config" + end + if option.use_pkgconfig == true then + return "using pkg-config" + end + if option.use_winlib == 32 then + return "using supplied win32 libraries" + end + if option.use_winlib == 64 then + return "using supplied win64 libraries" + end + return "using unknown method" + else + if option.required then + return "not found (required)" + else + return "not found (optional)" + end + end + end + + local o = MakeOption(name, 0, check, save, display) + o.Apply = apply + o.include_path = nil + o.lib_path = nil + o.required = required + return o + end } diff --git a/src/base/math.h b/src/base/math.h index d58dbf1..07b0639 100644 --- a/src/base/math.h +++ b/src/base/math.h @@ -20,7 +20,7 @@ inline float sign(float f) return f<0.0f?-1.0f:1.0f; } -inline int round(float f) +inline int round_to_int(float f) { if(f > 0) return (int)(f+0.5f); diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 3300d6c..51487b7 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -935,7 +935,7 @@ void CClient::ProcessConnlessPacket(CNetChunk *pPacket) { NETADDR Addr; - static char IPV4Mapping[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF }; + static char IPV4Mapping[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (char)0xFF, (char)0xFF}; // copy address if(!mem_comp(IPV4Mapping, pAddrs[i].m_aIp, sizeof(IPV4Mapping))) diff --git a/src/engine/client/text.cpp b/src/engine/client/text.cpp index d838ef2..6e7c11e 100644 --- a/src/engine/client/text.cpp +++ b/src/engine/client/text.cpp @@ -326,14 +326,14 @@ class CTextRender : public IEngineTextRender if(pBitmap->pixel_mode == FT_PIXEL_MODE_GRAY) // ignore_convention { - for(py = 0; py < pBitmap->rows; py++) // ignore_convention - for(px = 0; px < pBitmap->width; px++) // ignore_convention + for (py = 0; (int)py < (int)pBitmap->rows; py++) // ignore_convention + for (px = 0; (int)px < (int)pBitmap->width; px++) // ignore_convention ms_aGlyphData[(py+y)*SlotW+px+x] = pBitmap->buffer[py*pBitmap->pitch+px]; // ignore_convention } else if(pBitmap->pixel_mode == FT_PIXEL_MODE_MONO) // ignore_convention { - for(py = 0; py < pBitmap->rows; py++) // ignore_convention - for(px = 0; px < pBitmap->width; px++) // ignore_convention + for (py = 0; (int)py < (int)pBitmap->rows; py++) // ignore_convention + for (px = 0; (int)px < (int)pBitmap->width; px++) // ignore_convention { if(pBitmap->buffer[py*pBitmap->pitch+px/8]&(1<<(7-(px%8)))) // ignore_convention ms_aGlyphData[(py+y)*SlotW+px+x] = 255; diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index cc1f26d..fc0c9b3 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -29,19 +29,18 @@ #include - #include "register.h" #include "server.h" #if defined(CONF_FAMILY_WINDOWS) - #define _WIN32_WINNT 0x0501 - #define WIN32_LEAN_AND_MEAN - #include +#define _WIN32_WINNT 0x0501 +#define WIN32_LEAN_AND_MEAN +#include #endif static const char *StrLtrim(const char *pStr) { - while(*pStr && *pStr >= 0 && *pStr <= 32) + while (*pStr && *pStr >= 0 && *pStr <= 32) pStr++; return pStr; } @@ -49,16 +48,15 @@ static const char *StrLtrim(const char *pStr) static void StrRtrim(char *pStr) { int i = str_length(pStr); - while(i >= 0) + while (i >= 0) { - if(pStr[i] < 0 || pStr[i] > 32) + if (pStr[i] < 0 || pStr[i] > 32) break; pStr[i] = 0; i--; } } - CSnapIDPool::CSnapIDPool() { Reset(); @@ -66,13 +64,13 @@ CSnapIDPool::CSnapIDPool() void CSnapIDPool::Reset() { - for(int i = 0; i < MAX_IDS; i++) + for (int i = 0; i < MAX_IDS; i++) { - m_aIDs[i].m_Next = i+1; + m_aIDs[i].m_Next = i + 1; m_aIDs[i].m_State = 0; } - m_aIDs[MAX_IDS-1].m_Next = -1; + m_aIDs[MAX_IDS - 1].m_Next = -1; m_FirstFree = 0; m_FirstTimed = -1; m_LastTimed = -1; @@ -80,7 +78,6 @@ void CSnapIDPool::Reset() m_InUsage = 0; } - void CSnapIDPool::RemoveFirstTimeout() { int NextTimed = m_aIDs[m_FirstTimed].m_Next; @@ -92,7 +89,7 @@ void CSnapIDPool::RemoveFirstTimeout() // remove it from the timed list m_FirstTimed = NextTimed; - if(m_FirstTimed == -1) + if (m_FirstTimed == -1) m_LastTimed = -1; m_Usage--; @@ -103,12 +100,12 @@ int CSnapIDPool::NewID() int64 Now = time_get(); // process timed ids - while(m_FirstTimed != -1 && m_aIDs[m_FirstTimed].m_Timeout < Now) + while (m_FirstTimed != -1 && m_aIDs[m_FirstTimed].m_Timeout < Now) RemoveFirstTimeout(); int ID = m_FirstFree; dbg_assert(ID != -1, "id error"); - if(ID == -1) + if (ID == -1) return ID; m_FirstFree = m_aIDs[m_FirstFree].m_Next; m_aIDs[ID].m_State = 1; @@ -120,22 +117,22 @@ int CSnapIDPool::NewID() void CSnapIDPool::TimeoutIDs() { // process timed ids - while(m_FirstTimed != -1) + while (m_FirstTimed != -1) RemoveFirstTimeout(); } void CSnapIDPool::FreeID(int ID) { - if(ID < 0) + if (ID < 0) return; dbg_assert(m_aIDs[ID].m_State == 1, "id is not alloced"); m_InUsage--; m_aIDs[ID].m_State = 2; - m_aIDs[ID].m_Timeout = time_get()+time_freq()*5; + m_aIDs[ID].m_Timeout = time_get() + time_freq() * 5; m_aIDs[ID].m_Next = -1; - if(m_LastTimed != -1) + if (m_LastTimed != -1) { m_aIDs[m_LastTimed].m_Next = ID; m_LastTimed = ID; @@ -147,50 +144,49 @@ void CSnapIDPool::FreeID(int ID) } } - -void CServerBan::InitServerBan(IConsole *pConsole, IStorage *pStorage, CServer* pServer) +void CServerBan::InitServerBan(IConsole *pConsole, IStorage *pStorage, CServer *pServer) { CNetBan::Init(pConsole, pStorage); m_pServer = pServer; // overwrites base command, todo: improve this - Console()->Register("ban", "s?ir", CFGFLAG_SERVER|CFGFLAG_STORE, ConBanExt, this, "Ban player with ip/client id for x minutes for any reason"); + Console()->Register("ban", "s?ir", CFGFLAG_SERVER | CFGFLAG_STORE, ConBanExt, this, "Ban player with ip/client id for x minutes for any reason"); } -template +template int CServerBan::BanExt(T *pBanPool, const typename T::CDataType *pData, int Seconds, const char *pReason) { // validate address - if(Server()->m_RconClientID >= 0 && Server()->m_RconClientID < MAX_CLIENTS && + if (Server()->m_RconClientID >= 0 && Server()->m_RconClientID < MAX_CLIENTS && Server()->m_aClients[Server()->m_RconClientID].m_State != CServer::CClient::STATE_EMPTY) { - if(NetMatch(pData, Server()->m_NetServer.ClientAddr(Server()->m_RconClientID))) + if (NetMatch(pData, Server()->m_NetServer.ClientAddr(Server()->m_RconClientID))) { Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "net_ban", "ban error (you can't ban yourself)"); return -1; } - for(int i = 0; i < MAX_CLIENTS; ++i) + for (int i = 0; i < MAX_CLIENTS; ++i) { - if(i == Server()->m_RconClientID || Server()->m_aClients[i].m_State == CServer::CClient::STATE_EMPTY) + if (i == Server()->m_RconClientID || Server()->m_aClients[i].m_State == CServer::CClient::STATE_EMPTY) continue; - if(Server()->m_aClients[i].m_Authed >= Server()->m_RconAuthLevel && NetMatch(pData, Server()->m_NetServer.ClientAddr(i))) + if (Server()->m_aClients[i].m_Authed >= Server()->m_RconAuthLevel && NetMatch(pData, Server()->m_NetServer.ClientAddr(i))) { Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "net_ban", "ban error (command denied)"); return -1; } } } - else if(Server()->m_RconClientID == IServer::RCON_CID_VOTE) + else if (Server()->m_RconClientID == IServer::RCON_CID_VOTE) { - for(int i = 0; i < MAX_CLIENTS; ++i) + for (int i = 0; i < MAX_CLIENTS; ++i) { - if(Server()->m_aClients[i].m_State == CServer::CClient::STATE_EMPTY) + if (Server()->m_aClients[i].m_State == CServer::CClient::STATE_EMPTY) continue; - if(Server()->m_aClients[i].m_Authed != CServer::AUTHED_NO && NetMatch(pData, Server()->m_NetServer.ClientAddr(i))) + if (Server()->m_aClients[i].m_Authed != CServer::AUTHED_NO && NetMatch(pData, Server()->m_NetServer.ClientAddr(i))) { Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "net_ban", "ban error (command denied)"); return -1; @@ -199,17 +195,17 @@ int CServerBan::BanExt(T *pBanPool, const typename T::CDataType *pData, int Seco } int Result = Ban(pBanPool, pData, Seconds, pReason); - if(Result != 0) + if (Result != 0) return Result; // drop banned clients typename T::CDataType Data = *pData; - for(int i = 0; i < MAX_CLIENTS; ++i) + for (int i = 0; i < MAX_CLIENTS; ++i) { - if(Server()->m_aClients[i].m_State == CServer::CClient::STATE_EMPTY) + if (Server()->m_aClients[i].m_State == CServer::CClient::STATE_EMPTY) continue; - if(NetMatch(&Data, Server()->m_NetServer.ClientAddr(i))) + if (NetMatch(&Data, Server()->m_NetServer.ClientAddr(i))) { CNetHash NetHash(&Data); char aBuf[256]; @@ -228,7 +224,7 @@ int CServerBan::BanAddr(const NETADDR *pAddr, int Seconds, const char *pReason) int CServerBan::BanRange(const CNetRange *pRange, int Seconds, const char *pReason) { - if(pRange->IsValid()) + if (pRange->IsValid()) return BanExt(&m_BanRangePool, pRange, Seconds, pReason); Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "net_ban", "ban failed (invalid range)"); @@ -240,26 +236,25 @@ void CServerBan::ConBanExt(IConsole::IResult *pResult, void *pUser) CServerBan *pThis = static_cast(pUser); const char *pStr = pResult->GetString(0); - int Minutes = pResult->NumArguments()>1 ? clamp(pResult->GetInteger(1), 0, 44640) : 30; - const char *pReason = pResult->NumArguments()>2 ? pResult->GetString(2) : "No reason given"; + int Minutes = pResult->NumArguments() > 1 ? clamp(pResult->GetInteger(1), 0, 44640) : 30; + const char *pReason = pResult->NumArguments() > 2 ? pResult->GetString(2) : "No reason given"; - if(StrAllnum(pStr)) + if (StrAllnum(pStr)) { int ClientID = str_toint(pStr); - if(ClientID < 0 || ClientID >= MAX_CLIENTS || pThis->Server()->m_aClients[ClientID].m_State == CServer::CClient::STATE_EMPTY) + if (ClientID < 0 || ClientID >= MAX_CLIENTS || pThis->Server()->m_aClients[ClientID].m_State == CServer::CClient::STATE_EMPTY) pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "net_ban", "ban error (invalid client id)"); else - pThis->BanAddr(pThis->Server()->m_NetServer.ClientAddr(ClientID), Minutes*60, pReason); + pThis->BanAddr(pThis->Server()->m_NetServer.ClientAddr(ClientID), Minutes * 60, pReason); } else ConBan(pResult, pUser); } - void CServer::CClient::Reset() { // reset input - for(int i = 0; i < 200; i++) + for (int i = 0; i < 200; i++) m_aInputs[i].m_GameTick = -1; m_CurrentInput = 0; mem_zero(&m_LatestInput, sizeof(m_LatestInput)); @@ -268,7 +263,7 @@ void CServer::CClient::Reset() m_LastAckedSnapshot = -1; m_LastInputTick = -1; m_SnapRate = CClient::SNAPRATE_INIT; - //m_Score = 0; + // m_Score = 0; } CServer::CServer() : m_DemoRecorder(&m_SnapshotDelta) @@ -291,7 +286,6 @@ CServer::CServer() : m_DemoRecorder(&m_SnapshotDelta) Init(); } - int CServer::TrySetClientName(int ClientID, const char *pName) { char aTrimmedName[64]; @@ -301,11 +295,11 @@ int CServer::TrySetClientName(int ClientID, const char *pName) StrRtrim(aTrimmedName); // check for empty names - if(!aTrimmedName[0]) + if (!aTrimmedName[0]) return -1; // check if new and old name are the same - if(m_aClients[ClientID].m_aName[0] && str_comp(m_aClients[ClientID].m_aName, aTrimmedName) == 0) + if (m_aClients[ClientID].m_aName[0] && str_comp(m_aClients[ClientID].m_aName, aTrimmedName) == 0) return 0; char aBuf[256]; @@ -314,10 +308,10 @@ int CServer::TrySetClientName(int ClientID, const char *pName) pName = aTrimmedName; // make sure that two clients doesn't have the same name - for(int i = 0; i < MAX_CLIENTS; i++) - if(i != ClientID && m_aClients[i].m_State >= CClient::STATE_READY) + for (int i = 0; i < MAX_CLIENTS; i++) + if (i != ClientID && m_aClients[i].m_State >= CClient::STATE_READY) { - if(str_comp(pName, m_aClients[i].m_aName) == 0) + if (str_comp(pName, m_aClients[i].m_aName) == 0) return -1; } @@ -326,34 +320,25 @@ int CServer::TrySetClientName(int ClientID, const char *pName) return 0; } - - void CServer::SetClientName(int ClientID, const char *pName) { - if(ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State < CClient::STATE_READY) + if (ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State < CClient::STATE_READY) return; - if(!pName) + if (!pName) return; char aCleanName[MAX_NAME_LENGTH]; str_copy(aCleanName, pName, sizeof(aCleanName)); - // clear name - for(char *p = aCleanName; *p; ++p) - { - if(*p < 32) - *p = ' '; - } - - if(TrySetClientName(ClientID, aCleanName)) + if (TrySetClientName(ClientID, aCleanName)) { // auto rename - for(int i = 1;; i++) + for (int i = 1;; i++) { char aNameTry[MAX_NAME_LENGTH]; str_format(aNameTry, sizeof(aCleanName), "(%d)%s", i, aCleanName); - if(TrySetClientName(ClientID, aNameTry) == 0) + if (TrySetClientName(ClientID, aNameTry) == 0) break; } } @@ -361,7 +346,7 @@ void CServer::SetClientName(int ClientID, const char *pName) void CServer::SetClientClan(int ClientID, const char *pClan) { - if(ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State < CClient::STATE_READY || !pClan) + if (ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State < CClient::STATE_READY || !pClan) return; str_copy(m_aClients[ClientID].m_aClan, pClan, MAX_CLAN_LENGTH); @@ -369,7 +354,7 @@ void CServer::SetClientClan(int ClientID, const char *pClan) void CServer::SetClientCountry(int ClientID, int Country) { - if(ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State < CClient::STATE_READY) + if (ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State < CClient::STATE_READY) return; m_aClients[ClientID].m_Country = Country; @@ -377,7 +362,7 @@ void CServer::SetClientCountry(int ClientID, int Country) void CServer::SetClientScore(int ClientID, int Score) { - if(ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State < CClient::STATE_READY) + if (ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State < CClient::STATE_READY) return; m_aClients[ClientID].m_Score = Score; } @@ -386,27 +371,26 @@ void CServer::Kick(int ClientID, const char *pReason) { m_NetServer.m_SlotTakenByBot[ClientID] = false; m_aClients[ClientID].m_Bot = false; - - if(ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State == CClient::STATE_EMPTY) + + if (ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State == CClient::STATE_EMPTY) { Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", "invalid client id to kick"); return; } - else if(m_RconClientID == ClientID) + else if (m_RconClientID == ClientID) { Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", "you can't kick yourself"); - return; + return; } - else if(m_aClients[ClientID].m_Authed > m_RconAuthLevel) + else if (m_aClients[ClientID].m_Authed > m_RconAuthLevel) { Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", "kick command denied"); - return; + return; } m_NetServer.Drop(ClientID, pReason); } - void CServer::KickBots() { for (int i = 0; i < MAX_CLIENTS; i++) @@ -419,7 +403,6 @@ void CServer::KickBots() } } - /*int CServer::Tick() { return m_CurrentGameTick; @@ -427,7 +410,7 @@ void CServer::KickBots() int64 CServer::TickStartTime(int Tick) { - return m_GameStartTime + (time_freq()*Tick)/SERVER_TICK_SPEED; + return m_GameStartTime + (time_freq() * Tick) / SERVER_TICK_SPEED; } /*int CServer::TickSpeed() @@ -437,7 +420,7 @@ int64 CServer::TickStartTime(int Tick) int CServer::Init() { - for(int i = 0; i < MAX_CLIENTS; i++) + for (int i = 0; i < MAX_CLIENTS; i++) { m_aClients[i].m_State = CClient::STATE_EMPTY; m_aClients[i].m_aName[0] = 0; @@ -466,7 +449,7 @@ int CServer::GetClientInfo(int ClientID, CClientInfo *pInfo) dbg_assert(ClientID >= 0 && ClientID < MAX_CLIENTS, "client_id is not valid"); dbg_assert(pInfo != 0, "info can not be null"); - if(m_aClients[ClientID].m_State == CClient::STATE_INGAME) + if (m_aClients[ClientID].m_State == CClient::STATE_INGAME) { pInfo->m_pName = m_aClients[ClientID].m_aName; pInfo->m_Latency = m_aClients[ClientID].m_Latency; @@ -477,27 +460,25 @@ int CServer::GetClientInfo(int ClientID, CClientInfo *pInfo) void CServer::GetClientAddr(int ClientID, char *pAddrStr, int Size) { - if(ClientID >= 0 && ClientID < MAX_CLIENTS && m_aClients[ClientID].m_State == CClient::STATE_INGAME) + if (ClientID >= 0 && ClientID < MAX_CLIENTS && m_aClients[ClientID].m_State == CClient::STATE_INGAME) net_addr_str(m_NetServer.ClientAddr(ClientID), pAddrStr, Size, false); } - const char *CServer::ClientName(int ClientID) { - if(ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State == CServer::CClient::STATE_EMPTY) + if (ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State == CServer::CClient::STATE_EMPTY) return "(invalid)"; - if(m_aClients[ClientID].m_State == CServer::CClient::STATE_INGAME) + if (m_aClients[ClientID].m_State == CServer::CClient::STATE_INGAME) return m_aClients[ClientID].m_aName; else return "(connecting)"; - } const char *CServer::ClientClan(int ClientID) { - if(ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State == CServer::CClient::STATE_EMPTY) + if (ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State == CServer::CClient::STATE_EMPTY) return ""; - if(m_aClients[ClientID].m_State == CServer::CClient::STATE_INGAME) + if (m_aClients[ClientID].m_State == CServer::CClient::STATE_INGAME) return m_aClients[ClientID].m_aClan; else return ""; @@ -505,9 +486,9 @@ const char *CServer::ClientClan(int ClientID) int CServer::ClientCountry(int ClientID) { - if(ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State == CServer::CClient::STATE_EMPTY) + if (ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State == CServer::CClient::STATE_EMPTY) return -1; - if(m_aClients[ClientID].m_State == CServer::CClient::STATE_INGAME) + if (m_aClients[ClientID].m_State == CServer::CClient::STATE_INGAME) return m_aClients[ClientID].m_Country; else return -1; @@ -531,7 +512,7 @@ int CServer::SendMsg(CMsgPacker *pMsg, int Flags, int ClientID) int CServer::SendMsgEx(CMsgPacker *pMsg, int Flags, int ClientID, bool System) { CNetChunk Packet; - if(!pMsg) + if (!pMsg) return -1; mem_zero(&Packet, sizeof(CNetChunk)); @@ -541,27 +522,27 @@ int CServer::SendMsgEx(CMsgPacker *pMsg, int Flags, int ClientID, bool System) Packet.m_DataSize = pMsg->Size(); // HACK: modify the message id in the packet and store the system flag - *((unsigned char*)Packet.m_pData) <<= 1; - if(System) - *((unsigned char*)Packet.m_pData) |= 1; + *((unsigned char *)Packet.m_pData) <<= 1; + if (System) + *((unsigned char *)Packet.m_pData) |= 1; - if(Flags&MSGFLAG_VITAL) + if (Flags & MSGFLAG_VITAL) Packet.m_Flags |= NETSENDFLAG_VITAL; - if(Flags&MSGFLAG_FLUSH) + if (Flags & MSGFLAG_FLUSH) Packet.m_Flags |= NETSENDFLAG_FLUSH; // write message to demo recorder - if(!(Flags&MSGFLAG_NORECORD)) + if (!(Flags & MSGFLAG_NORECORD)) m_DemoRecorder.RecordMessage(pMsg->Data(), pMsg->Size()); - if(!(Flags&MSGFLAG_NOSEND)) + if (!(Flags & MSGFLAG_NOSEND)) { - if(ClientID == -1) + if (ClientID == -1) { // broadcast int i; - for(i = 0; i < MAX_CLIENTS; i++) - if(m_aClients[i].m_State == CClient::STATE_INGAME && !m_aClients[i].m_Bot) + for (i = 0; i < MAX_CLIENTS; i++) + if (m_aClients[i].m_State == CClient::STATE_INGAME && !m_aClients[i].m_Bot) { Packet.m_ClientID = i; m_NetServer.Send(&Packet); @@ -582,7 +563,7 @@ void CServer::DoSnapshot() GameServer()->OnPreSnap(); // create snapshot for demo recording - if(m_DemoRecorder.IsRecording()) + if (m_DemoRecorder.IsRecording()) { char aData[CSnapshot::MAX_SIZE]; int SnapshotSize; @@ -597,23 +578,23 @@ void CServer::DoSnapshot() } // create snapshots for all clients - for(int i = 0; i < MAX_CLIENTS; i++) + for (int i = 0; i < MAX_CLIENTS; i++) { // client must be ingame to recive snapshots - if(m_aClients[i].m_State != CClient::STATE_INGAME) + if (m_aClients[i].m_State != CClient::STATE_INGAME) continue; // this client is trying to recover, don't spam snapshots - if(m_aClients[i].m_SnapRate == CClient::SNAPRATE_RECOVER && (Tick()%50) != 0) + if (m_aClients[i].m_SnapRate == CClient::SNAPRATE_RECOVER && (Tick() % 50) != 0) continue; // this client is trying to recover, don't spam snapshots - if(m_aClients[i].m_SnapRate == CClient::SNAPRATE_INIT && (Tick()%10) != 0) + if (m_aClients[i].m_SnapRate == CClient::SNAPRATE_INIT && (Tick() % 10) != 0) continue; { char aData[CSnapshot::MAX_SIZE]; - CSnapshot *pData = (CSnapshot*)aData; // Fix compiler warning for strict-aliasing + CSnapshot *pData = (CSnapshot *)aData; // Fix compiler warning for strict-aliasing char aDeltaData[CSnapshot::MAX_SIZE]; char aCompData[CSnapshot::MAX_SIZE]; int SnapshotSize; @@ -634,7 +615,7 @@ void CServer::DoSnapshot() // remove old snapshos // keep 3 seconds worth of snapshots - m_aClients[i].m_Snapshots.PurgeUntil(m_CurrentGameTick-SERVER_TICK_SPEED*3); + m_aClients[i].m_Snapshots.PurgeUntil(m_CurrentGameTick - SERVER_TICK_SPEED * 3); // save it the snapshot m_aClients[i].m_Snapshots.Add(m_CurrentGameTick, time_get(), SnapshotSize, pData, 0); @@ -644,12 +625,12 @@ void CServer::DoSnapshot() { DeltashotSize = m_aClients[i].m_Snapshots.Get(m_aClients[i].m_LastAckedSnapshot, 0, &pDeltashot, 0); - if(DeltashotSize >= 0) + if (DeltashotSize >= 0) DeltaTick = m_aClients[i].m_LastAckedSnapshot; else { // no acked package found, force client to recover rate - if(m_aClients[i].m_SnapRate == CClient::SNAPRATE_FULL) + if (m_aClients[i].m_SnapRate == CClient::SNAPRATE_FULL) m_aClients[i].m_SnapRate = CClient::SNAPRATE_RECOVER; } } @@ -657,7 +638,7 @@ void CServer::DoSnapshot() // create delta DeltaSize = m_SnapshotDelta.CreateDelta(pDeltashot, pData, aDeltaData); - if(DeltaSize) + if (DeltaSize) { // compress it int SnapshotSize; @@ -665,33 +646,33 @@ void CServer::DoSnapshot() int NumPackets; SnapshotSize = CVariableInt::Compress(aDeltaData, DeltaSize, aCompData); - NumPackets = (SnapshotSize+MaxSize-1)/MaxSize; + NumPackets = (SnapshotSize + MaxSize - 1) / MaxSize; - for(int n = 0, Left = SnapshotSize; Left; n++) + for (int n = 0, Left = SnapshotSize; Left; n++) { int Chunk = Left < MaxSize ? Left : MaxSize; Left -= Chunk; - if(NumPackets == 1) + if (NumPackets == 1) { CMsgPacker Msg(NETMSG_SNAPSINGLE); Msg.AddInt(m_CurrentGameTick); - Msg.AddInt(m_CurrentGameTick-DeltaTick); + Msg.AddInt(m_CurrentGameTick - DeltaTick); Msg.AddInt(Crc); Msg.AddInt(Chunk); - Msg.AddRaw(&aCompData[n*MaxSize], Chunk); + Msg.AddRaw(&aCompData[n * MaxSize], Chunk); SendMsgEx(&Msg, MSGFLAG_FLUSH, i, true); } else { CMsgPacker Msg(NETMSG_SNAP); Msg.AddInt(m_CurrentGameTick); - Msg.AddInt(m_CurrentGameTick-DeltaTick); + Msg.AddInt(m_CurrentGameTick - DeltaTick); Msg.AddInt(NumPackets); Msg.AddInt(n); Msg.AddInt(Crc); Msg.AddInt(Chunk); - Msg.AddRaw(&aCompData[n*MaxSize], Chunk); + Msg.AddRaw(&aCompData[n * MaxSize], Chunk); SendMsgEx(&Msg, MSGFLAG_FLUSH, i, true); } } @@ -700,7 +681,7 @@ void CServer::DoSnapshot() { CMsgPacker Msg(NETMSG_SNAPEMPTY); Msg.AddInt(m_CurrentGameTick); - Msg.AddInt(m_CurrentGameTick-DeltaTick); + Msg.AddInt(m_CurrentGameTick - DeltaTick); SendMsgEx(&Msg, MSGFLAG_FLUSH, i, true); } } @@ -709,7 +690,6 @@ void CServer::DoSnapshot() GameServer()->OnPostSnap(); } - int CServer::NewClientCallback(int ClientID, void *pUser) { CServer *pThis = (CServer *)pUser; @@ -731,11 +711,11 @@ int CServer::DelClientCallback(int ClientID, const char *pReason, void *pUser) char aAddrStr[NETADDR_MAXSTRSIZE]; net_addr_str(pThis->m_NetServer.ClientAddr(ClientID), aAddrStr, sizeof(aAddrStr), true); char aBuf[256]; - str_format(aBuf, sizeof(aBuf), "client dropped. cid=%d addr=%s reason='%s'", ClientID, aAddrStr, pReason); + str_format(aBuf, sizeof(aBuf), "client dropped. cid=%d addr=%s reason='%s'", ClientID, aAddrStr, pReason); pThis->Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "server", aBuf); // notify the mod about the drop - if(pThis->m_aClients[ClientID].m_State >= CClient::STATE_READY) + if (pThis->m_aClients[ClientID].m_State >= CClient::STATE_READY) pThis->GameServer()->OnClientDrop(ClientID, pReason); pThis->m_aClients[ClientID].m_State = CClient::STATE_EMPTY; @@ -755,13 +735,13 @@ void CServer::SendMap(int ClientID) Msg.AddString(GetMapName(), 0); Msg.AddInt(m_CurrentMapCrc); Msg.AddInt(m_CurrentMapSize); - SendMsgEx(&Msg, MSGFLAG_VITAL|MSGFLAG_FLUSH, ClientID, true); + SendMsgEx(&Msg, MSGFLAG_VITAL | MSGFLAG_FLUSH, ClientID, true); } void CServer::SendConnectionReady(int ClientID) { CMsgPacker Msg(NETMSG_CON_READY); - SendMsgEx(&Msg, MSGFLAG_VITAL|MSGFLAG_FLUSH, ClientID, true); + SendMsgEx(&Msg, MSGFLAG_VITAL | MSGFLAG_FLUSH, ClientID, true); } void CServer::SendRconLine(int ClientID, const char *pLine) @@ -777,12 +757,13 @@ void CServer::SendRconLineAuthed(const char *pLine, void *pUser) static volatile int ReentryGuard = 0; int i; - if(ReentryGuard) return; + if (ReentryGuard) + return; ReentryGuard++; - for(i = 0; i < MAX_CLIENTS; i++) + for (i = 0; i < MAX_CLIENTS; i++) { - if(pThis->m_aClients[i].m_State != CClient::STATE_EMPTY && pThis->m_aClients[i].m_Authed >= pThis->m_RconAuthLevel) + if (pThis->m_aClients[i].m_State != CClient::STATE_EMPTY && pThis->m_aClients[i].m_Authed >= pThis->m_RconAuthLevel) pThis->SendRconLine(i, pLine); } @@ -809,10 +790,10 @@ void CServer::UpdateClientRconCommands() { int ClientID = Tick() % MAX_CLIENTS; - if(m_aClients[ClientID].m_State != CClient::STATE_EMPTY && m_aClients[ClientID].m_Authed) + if (m_aClients[ClientID].m_State != CClient::STATE_EMPTY && m_aClients[ClientID].m_Authed) { int ConsoleAccessLevel = m_aClients[ClientID].m_Authed == AUTHED_ADMIN ? IConsole::ACCESS_LEVEL_ADMIN : IConsole::ACCESS_LEVEL_MOD; - for(int i = 0; i < MAX_RCONCMD_SEND && m_aClients[ClientID].m_pRconCmdToSend; ++i) + for (int i = 0; i < MAX_RCONCMD_SEND && m_aClients[ClientID].m_pRconCmdToSend; ++i) { SendRconCmdAdd(m_aClients[ClientID].m_pRconCmdToSend, ClientID); m_aClients[ClientID].m_pRconCmdToSend = m_aClients[ClientID].m_pRconCmdToSend->NextCommandInfo(ConsoleAccessLevel, CFGFLAG_SERVER); @@ -828,21 +809,21 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket) // unpack msgid and system flag int Msg = Unpacker.GetInt(); - int Sys = Msg&1; + int Sys = Msg & 1; Msg >>= 1; - if(Unpacker.Error()) + if (Unpacker.Error()) return; - if(Sys) + if (Sys) { // system message - if(Msg == NETMSG_INFO) + if (Msg == NETMSG_INFO) { - if(m_aClients[ClientID].m_State == CClient::STATE_AUTH) + if (m_aClients[ClientID].m_State == CClient::STATE_AUTH) { const char *pVersion = Unpacker.GetString(CUnpacker::SANITIZE_CC); - if(str_comp(pVersion, GameServer()->NetVersion()) != 0) + if (str_comp(pVersion, GameServer()->NetVersion()) != 0) { // wrong version char aReason[256]; @@ -852,7 +833,7 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket) } const char *pPassword = Unpacker.GetString(CUnpacker::SANITIZE_CC); - if(g_Config.m_Password[0] != 0 && str_comp(g_Config.m_Password, pPassword) != 0) + if (g_Config.m_Password[0] != 0 && str_comp(g_Config.m_Password, pPassword) != 0) { // wrong password m_NetServer.Drop(ClientID, "Wrong password"); @@ -860,29 +841,29 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket) } m_aClients[ClientID].m_State = CClient::STATE_CONNECTING; - //m_aClients[ClientID].m_Bot = false; - + // m_aClients[ClientID].m_Bot = false; + SendMap(ClientID); } } - else if(Msg == NETMSG_REQUEST_MAP_DATA) + else if (Msg == NETMSG_REQUEST_MAP_DATA) { - if(m_aClients[ClientID].m_State < CClient::STATE_CONNECTING) + if (m_aClients[ClientID].m_State < CClient::STATE_CONNECTING) return; int Chunk = Unpacker.GetInt(); - unsigned int ChunkSize = 1024-128; + unsigned int ChunkSize = 1024 - 128; unsigned int Offset = Chunk * ChunkSize; int Last = 0; // drop faulty map data requests - if(Chunk < 0 || Offset > m_CurrentMapSize) + if (Chunk < 0 || (int)Offset > m_CurrentMapSize) return; - if(Offset+ChunkSize >= m_CurrentMapSize) + if ((int)(Offset + ChunkSize) >= m_CurrentMapSize) { - ChunkSize = m_CurrentMapSize-Offset; - if(ChunkSize < 0) + ChunkSize = m_CurrentMapSize - Offset; + if (ChunkSize < 0) ChunkSize = 0; Last = 1; } @@ -893,18 +874,18 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket) Msg.AddInt(Chunk); Msg.AddInt(ChunkSize); Msg.AddRaw(&m_pCurrentMapData[Offset], ChunkSize); - SendMsgEx(&Msg, MSGFLAG_VITAL|MSGFLAG_FLUSH, ClientID, true); + SendMsgEx(&Msg, MSGFLAG_VITAL | MSGFLAG_FLUSH, ClientID, true); - if(g_Config.m_Debug) + if (g_Config.m_Debug) { char aBuf[256]; str_format(aBuf, sizeof(aBuf), "sending chunk %d with size %d", Chunk, ChunkSize); Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "server", aBuf); } } - else if(Msg == NETMSG_READY) + else if (Msg == NETMSG_READY) { - if(m_aClients[ClientID].m_State == CClient::STATE_CONNECTING) + if (m_aClients[ClientID].m_State == CClient::STATE_CONNECTING) { char aAddrStr[NETADDR_MAXSTRSIZE]; net_addr_str(m_NetServer.ClientAddr(ClientID), aAddrStr, sizeof(aAddrStr), true); @@ -917,9 +898,9 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket) SendConnectionReady(ClientID); } } - else if(Msg == NETMSG_ENTERGAME) + else if (Msg == NETMSG_ENTERGAME) { - if(m_aClients[ClientID].m_State == CClient::STATE_READY && GameServer()->IsClientReady(ClientID)) + if (m_aClients[ClientID].m_State == CClient::STATE_READY && GameServer()->IsClientReady(ClientID)) { char aAddrStr[NETADDR_MAXSTRSIZE]; net_addr_str(m_NetServer.ClientAddr(ClientID), aAddrStr, sizeof(aAddrStr), true); @@ -931,7 +912,7 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket) GameServer()->OnClientEnter(ClientID); } } - else if(Msg == NETMSG_INPUT) + else if (Msg == NETMSG_INPUT) { CClient::CInput *pInput; int64 TagTime; @@ -941,20 +922,20 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket) int Size = Unpacker.GetInt(); // check for errors - if(Unpacker.Error() || Size/4 > MAX_INPUT_SIZE) + if (Unpacker.Error() || Size / 4 > MAX_INPUT_SIZE) return; - if(m_aClients[ClientID].m_LastAckedSnapshot > 0) + if (m_aClients[ClientID].m_LastAckedSnapshot > 0) m_aClients[ClientID].m_SnapRate = CClient::SNAPRATE_FULL; - if(m_aClients[ClientID].m_Snapshots.Get(m_aClients[ClientID].m_LastAckedSnapshot, &TagTime, 0, 0) >= 0) - m_aClients[ClientID].m_Latency = (int)(((time_get()-TagTime)*1000)/time_freq()); + if (m_aClients[ClientID].m_Snapshots.Get(m_aClients[ClientID].m_LastAckedSnapshot, &TagTime, 0, 0) >= 0) + m_aClients[ClientID].m_Latency = (int)(((time_get() - TagTime) * 1000) / time_freq()); // add message to report the input timing // skip packets that are old - if(IntendedTick > m_aClients[ClientID].m_LastInputTick) + if (IntendedTick > m_aClients[ClientID].m_LastInputTick) { - int TimeLeft = ((TickStartTime(IntendedTick)-time_get())*1000) / time_freq(); + int TimeLeft = ((TickStartTime(IntendedTick) - time_get()) * 1000) / time_freq(); CMsgPacker Msg(NETMSG_INPUTTIMING); Msg.AddInt(IntendedTick); @@ -966,28 +947,28 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket) pInput = &m_aClients[ClientID].m_aInputs[m_aClients[ClientID].m_CurrentInput]; - if(IntendedTick <= Tick()) - IntendedTick = Tick()+1; + if (IntendedTick <= Tick()) + IntendedTick = Tick() + 1; pInput->m_GameTick = IntendedTick; - for(int i = 0; i < Size/4; i++) + for (int i = 0; i < Size / 4; i++) pInput->m_aData[i] = Unpacker.GetInt(); - mem_copy(m_aClients[ClientID].m_LatestInput.m_aData, pInput->m_aData, MAX_INPUT_SIZE*sizeof(int)); + mem_copy(m_aClients[ClientID].m_LatestInput.m_aData, pInput->m_aData, MAX_INPUT_SIZE * sizeof(int)); m_aClients[ClientID].m_CurrentInput++; m_aClients[ClientID].m_CurrentInput %= 200; // call the mod with the fresh input data - if(m_aClients[ClientID].m_State == CClient::STATE_INGAME) + if (m_aClients[ClientID].m_State == CClient::STATE_INGAME) GameServer()->OnClientDirectInput(ClientID, m_aClients[ClientID].m_LatestInput.m_aData); } - else if(Msg == NETMSG_RCON_CMD) + else if (Msg == NETMSG_RCON_CMD) { const char *pCmd = Unpacker.GetString(); - if(Unpacker.Error() == 0 && m_aClients[ClientID].m_Authed) + if (Unpacker.Error() == 0 && m_aClients[ClientID].m_Authed) { char aBuf[256]; str_format(aBuf, sizeof(aBuf), "ClientID=%d rcon='%s'", ClientID, pCmd); @@ -1001,62 +982,62 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket) m_RconAuthLevel = AUTHED_ADMIN; } } - else if(Msg == NETMSG_RCON_AUTH) + else if (Msg == NETMSG_RCON_AUTH) { const char *pPw; Unpacker.GetString(); // login name, not used pPw = Unpacker.GetString(CUnpacker::SANITIZE_CC); - if(Unpacker.Error() == 0) + if (Unpacker.Error() == 0) { - if(g_Config.m_SvRconPassword[0] == 0 && g_Config.m_SvRconModPassword[0] == 0) + if (g_Config.m_SvRconPassword[0] == 0 && g_Config.m_SvRconModPassword[0] == 0) { SendRconLine(ClientID, "No rcon password set on server. Set sv_rcon_password and/or sv_rcon_mod_password to enable the remote console."); } - else if(g_Config.m_SvRconPassword[0] && str_comp(pPw, g_Config.m_SvRconPassword) == 0) + else if (g_Config.m_SvRconPassword[0] && str_comp(pPw, g_Config.m_SvRconPassword) == 0) { CMsgPacker Msg(NETMSG_RCON_AUTH_STATUS); - Msg.AddInt(1); //authed - Msg.AddInt(1); //cmdlist + Msg.AddInt(1); // authed + Msg.AddInt(1); // cmdlist SendMsgEx(&Msg, MSGFLAG_VITAL, ClientID, true); m_aClients[ClientID].m_Authed = AUTHED_ADMIN; int SendRconCmds = Unpacker.GetInt(); - if(Unpacker.Error() == 0 && SendRconCmds) + if (Unpacker.Error() == 0 && SendRconCmds) m_aClients[ClientID].m_pRconCmdToSend = Console()->FirstCommandInfo(IConsole::ACCESS_LEVEL_ADMIN, CFGFLAG_SERVER); SendRconLine(ClientID, "Admin authentication successful. Full remote console access granted."); char aBuf[256]; str_format(aBuf, sizeof(aBuf), "ClientID=%d authed (admin)", ClientID); Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf); } - else if(g_Config.m_SvRconModPassword[0] && str_comp(pPw, g_Config.m_SvRconModPassword) == 0) + else if (g_Config.m_SvRconModPassword[0] && str_comp(pPw, g_Config.m_SvRconModPassword) == 0) { CMsgPacker Msg(NETMSG_RCON_AUTH_STATUS); - Msg.AddInt(1); //authed - Msg.AddInt(1); //cmdlist + Msg.AddInt(1); // authed + Msg.AddInt(1); // cmdlist SendMsgEx(&Msg, MSGFLAG_VITAL, ClientID, true); m_aClients[ClientID].m_Authed = AUTHED_MOD; int SendRconCmds = Unpacker.GetInt(); - if(Unpacker.Error() == 0 && SendRconCmds) + if (Unpacker.Error() == 0 && SendRconCmds) m_aClients[ClientID].m_pRconCmdToSend = Console()->FirstCommandInfo(IConsole::ACCESS_LEVEL_MOD, CFGFLAG_SERVER); SendRconLine(ClientID, "Moderator authentication successful. Limited remote console access granted."); char aBuf[256]; str_format(aBuf, sizeof(aBuf), "ClientID=%d authed (moderator)", ClientID); Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf); } - else if(g_Config.m_SvRconMaxTries) + else if (g_Config.m_SvRconMaxTries) { m_aClients[ClientID].m_AuthTries++; char aBuf[128]; str_format(aBuf, sizeof(aBuf), "Wrong password %d/%d.", m_aClients[ClientID].m_AuthTries, g_Config.m_SvRconMaxTries); SendRconLine(ClientID, aBuf); - if(m_aClients[ClientID].m_AuthTries >= g_Config.m_SvRconMaxTries) + if (m_aClients[ClientID].m_AuthTries >= g_Config.m_SvRconMaxTries) { - if(!g_Config.m_SvRconBantime) + if (!g_Config.m_SvRconBantime) m_NetServer.Drop(ClientID, "Too many remote console authentication tries"); else - m_ServerBan.BanAddr(m_NetServer.ClientAddr(ClientID), g_Config.m_SvRconBantime*60, "Too many remote console authentication tries"); + m_ServerBan.BanAddr(m_NetServer.ClientAddr(ClientID), g_Config.m_SvRconBantime * 60, "Too many remote console authentication tries"); } } else @@ -1065,24 +1046,24 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket) } } } - else if(Msg == NETMSG_PING) + else if (Msg == NETMSG_PING) { CMsgPacker Msg(NETMSG_PING_REPLY); SendMsgEx(&Msg, 0, ClientID, true); } else { - if(g_Config.m_Debug) + if (g_Config.m_Debug) { char aHex[] = "0123456789ABCDEF"; char aBuf[512]; - for(int b = 0; b < pPacket->m_DataSize && b < 32; b++) + for (int b = 0; b < pPacket->m_DataSize && b < 32; b++) { - aBuf[b*3] = aHex[((const unsigned char *)pPacket->m_pData)[b]>>4]; - aBuf[b*3+1] = aHex[((const unsigned char *)pPacket->m_pData)[b]&0xf]; - aBuf[b*3+2] = ' '; - aBuf[b*3+3] = 0; + aBuf[b * 3] = aHex[((const unsigned char *)pPacket->m_pData)[b] >> 4]; + aBuf[b * 3 + 1] = aHex[((const unsigned char *)pPacket->m_pData)[b] & 0xf]; + aBuf[b * 3 + 2] = ' '; + aBuf[b * 3 + 3] = 0; } char aBufMsg[256]; @@ -1095,7 +1076,7 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket) else { // game message - if(m_aClients[ClientID].m_State >= CClient::STATE_READY) + if (m_aClients[ClientID].m_State >= CClient::STATE_READY) GameServer()->OnMessage(Msg, &Unpacker, ClientID); } } @@ -1108,11 +1089,11 @@ void CServer::SendServerInfo(const NETADDR *pAddr, int Token) // count the players int PlayerCount = 0, ClientCount = 0; - for(int i = 0; i < MAX_CLIENTS; i++) + for (int i = 0; i < MAX_CLIENTS; i++) { - if(m_aClients[i].m_State != CClient::STATE_EMPTY && !m_aClients[i].m_Bot) + if (m_aClients[i].m_State != CClient::STATE_EMPTY && !m_aClients[i].m_Bot) { - if(GameServer()->IsClientPlayer(i)) + if (GameServer()->IsClientPlayer(i)) PlayerCount++; ClientCount++; @@ -1134,26 +1115,33 @@ void CServer::SendServerInfo(const NETADDR *pAddr, int Token) // flags int i = 0; - if(g_Config.m_Password[0]) // password set + if (g_Config.m_Password[0]) // password set i |= SERVER_FLAG_PASSWORD; str_format(aBuf, sizeof(aBuf), "%d", i); p.AddString(aBuf, 2); - str_format(aBuf, sizeof(aBuf), "%d", PlayerCount); p.AddString(aBuf, 3); // num players - str_format(aBuf, sizeof(aBuf), "%d", MAX_CLIENTS-g_Config.m_SvSpectatorSlots); p.AddString(aBuf, 3); // max players - //str_format(aBuf, sizeof(aBuf), "%d", m_NetServer.MaxClients()-g_Config.m_SvSpectatorSlots); p.AddString(aBuf, 3); // max players - str_format(aBuf, sizeof(aBuf), "%d", ClientCount); p.AddString(aBuf, 3); // num clients - str_format(aBuf, sizeof(aBuf), "%d", MAX_CLIENTS); p.AddString(aBuf, 3); // max clients - - for(i = 0; i < MAX_CLIENTS; i++) + str_format(aBuf, sizeof(aBuf), "%d", PlayerCount); + p.AddString(aBuf, 3); // num players + str_format(aBuf, sizeof(aBuf), "%d", MAX_CLIENTS - g_Config.m_SvSpectatorSlots); + p.AddString(aBuf, 3); // max players + // str_format(aBuf, sizeof(aBuf), "%d", m_NetServer.MaxClients()-g_Config.m_SvSpectatorSlots); p.AddString(aBuf, 3); // max players + str_format(aBuf, sizeof(aBuf), "%d", ClientCount); + p.AddString(aBuf, 3); // num clients + str_format(aBuf, sizeof(aBuf), "%d", MAX_CLIENTS); + p.AddString(aBuf, 3); // max clients + + for (i = 0; i < MAX_CLIENTS; i++) { - if(m_aClients[i].m_State != CClient::STATE_EMPTY && !m_aClients[i].m_Bot) + if (m_aClients[i].m_State != CClient::STATE_EMPTY && !m_aClients[i].m_Bot) { p.AddString(ClientName(i), MAX_NAME_LENGTH); // client name p.AddString(ClientClan(i), MAX_CLAN_LENGTH); // client clan - str_format(aBuf, sizeof(aBuf), "%d", m_aClients[i].m_Country); p.AddString(aBuf, 6); // client country - str_format(aBuf, sizeof(aBuf), "%d", m_aClients[i].m_Score); p.AddString(aBuf, 6); // client score - str_format(aBuf, sizeof(aBuf), "%d", GameServer()->IsClientPlayer(i)?1:0); p.AddString(aBuf, 2); // is player? + str_format(aBuf, sizeof(aBuf), "%d", m_aClients[i].m_Country); + p.AddString(aBuf, 6); // client country + str_format(aBuf, sizeof(aBuf), "%d", m_aClients[i].m_Score); + p.AddString(aBuf, 6); // client score + str_format(aBuf, sizeof(aBuf), "%d", GameServer()->IsClientPlayer(i) ? 1 : 0); + p.AddString(aBuf, 2); // is player? } } @@ -1167,15 +1155,13 @@ void CServer::SendServerInfo(const NETADDR *pAddr, int Token) void CServer::UpdateServerInfo() { - for(int i = 0; i < MAX_CLIENTS; ++i) + for (int i = 0; i < MAX_CLIENTS; ++i) { - if(m_aClients[i].m_State != CClient::STATE_EMPTY && !m_aClients[i].m_Bot) + if (m_aClients[i].m_State != CClient::STATE_EMPTY && !m_aClients[i].m_Bot) SendServerInfo(m_NetServer.ClientAddr(i), -1); } } - - /* enum InputList { @@ -1183,18 +1169,17 @@ enum InputList INPUT_SHOOT = 4, INPUT_JUMP = 3, INPUT_HOOK = 5 - + //1 & 2 vectors for weapon direction }; */ - // called right after PumpNetwork() void CServer::UpdateAIInput() { for (int i = 0; i < MAX_CLIENTS; i++) { - if(m_aClients[i].m_State == CClient::STATE_INGAME && m_aClients[i].m_Bot) + if (m_aClients[i].m_State == CClient::STATE_INGAME && m_aClients[i].m_Bot) { if (GameServer()->AIInputUpdateNeeded(i)) { @@ -1203,28 +1188,24 @@ void CServer::UpdateAIInput() m_aClients[i].m_LastInputTick = Tick(); pInput = &m_aClients[i].m_aInputs[m_aClients[i].m_CurrentInput]; - pInput->m_GameTick = Tick()+1; - + pInput->m_GameTick = Tick() + 1; // update input data GameServer()->AIUpdateInput(i, pInput->m_aData); - - mem_copy(m_aClients[i].m_LatestInput.m_aData, pInput->m_aData, MAX_INPUT_SIZE*sizeof(int)); + mem_copy(m_aClients[i].m_LatestInput.m_aData, pInput->m_aData, MAX_INPUT_SIZE * sizeof(int)); m_aClients[i].m_CurrentInput++; m_aClients[i].m_CurrentInput %= 200; // call the mod with the fresh input data - if(m_aClients[i].m_State == CClient::STATE_INGAME) + if (m_aClients[i].m_State == CClient::STATE_INGAME) GameServer()->OnClientDirectInput(i, m_aClients[i].m_LatestInput.m_aData); } } } } - - void CServer::PumpNetwork() { CNetChunk Packet; @@ -1232,14 +1213,14 @@ void CServer::PumpNetwork() m_NetServer.Update(); // process packets - while(m_NetServer.Recv(&Packet)) + while (m_NetServer.Recv(&Packet)) { - if(Packet.m_ClientID == -1) + if (Packet.m_ClientID == -1) { // stateless - if(!m_Register.RegisterProcessPacket(&Packet)) + if (!m_Register.RegisterProcessPacket(&Packet)) { - if(Packet.m_DataSize == sizeof(SERVERBROWSE_GETINFO)+1 && + if (Packet.m_DataSize == sizeof(SERVERBROWSE_GETINFO) + 1 && mem_comp(Packet.m_pData, SERVERBROWSE_GETINFO, sizeof(SERVERBROWSE_GETINFO)) == 0) { SendServerInfo(&Packet.m_Address, ((unsigned char *)Packet.m_pData)[sizeof(SERVERBROWSE_GETINFO)]); @@ -1258,10 +1239,10 @@ char *CServer::GetMapName() { // get the name of the map without his path char *pMapShortName = &g_Config.m_SvMap[0]; - for(int i = 0; i < str_length(g_Config.m_SvMap)-1; i++) + for (int i = 0; i < str_length(g_Config.m_SvMap) - 1; i++) { - if(g_Config.m_SvMap[i] == '/' || g_Config.m_SvMap[i] == '\\') - pMapShortName = &g_Config.m_SvMap[i+1]; + if (g_Config.m_SvMap[i] == '/' || g_Config.m_SvMap[i] == '\\') + pMapShortName = &g_Config.m_SvMap[i + 1]; } return pMapShortName; } @@ -1269,8 +1250,8 @@ char *CServer::GetMapName() int CServer::LoadMap(const char *pMapName) { KickBots(); - - //DATAFILE *df; + + // DATAFILE *df; char aBuf[512]; str_format(aBuf, sizeof(aBuf), "maps/%s.map", pMapName); @@ -1279,13 +1260,13 @@ int CServer::LoadMap(const char *pMapName) return 0;*/ // check for valid standard map - if(!m_MapChecker.ReadAndValidateMap(Storage(), aBuf, IStorage::TYPE_ALL)) + if (!m_MapChecker.ReadAndValidateMap(Storage(), aBuf, IStorage::TYPE_ALL)) { Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "mapchecker", "invalid standard map"); return 0; } - if(!m_pMap->Load(aBuf)) + if (!m_pMap->Load(aBuf)) return 0; // stop recording when we change map @@ -1301,13 +1282,13 @@ int CServer::LoadMap(const char *pMapName) Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "server", aBufMsg); str_copy(m_aCurrentMap, pMapName, sizeof(m_aCurrentMap)); - //map_set(df); + // map_set(df); // load complete map into memory for download { IOHANDLE File = Storage()->OpenFile(aBuf, IOFLAG_READ, IStorage::TYPE_ALL); m_CurrentMapSize = (int)io_length(File); - if(m_pCurrentMapData) + if (m_pCurrentMapData) mem_free(m_pCurrentMapData); m_pCurrentMapData = (unsigned char *)mem_alloc(m_CurrentMapSize, 1); io_read(File, m_pCurrentMapData, m_CurrentMapSize); @@ -1327,7 +1308,7 @@ int CServer::Run() m_PrintCBIndex = Console()->RegisterPrintCallback(g_Config.m_ConsoleOutputLevel, SendRconLineAuthed, this); // load map - if(!LoadMap(g_Config.m_SvMap)) + if (!LoadMap(g_Config.m_SvMap)) { dbg_msg("server", "failed to load map. mapname='%s'", g_Config.m_SvMap); return -1; @@ -1335,7 +1316,7 @@ int CServer::Run() // start server NETADDR BindAddr; - if(g_Config.m_Bindaddr[0] && net_host_lookup(g_Config.m_Bindaddr, &BindAddr, NETTYPE_ALL) == 0) + if (g_Config.m_Bindaddr[0] && net_host_lookup(g_Config.m_Bindaddr, &BindAddr, NETTYPE_ALL) == 0) { // sweet! BindAddr.type = NETTYPE_ALL; @@ -1348,7 +1329,7 @@ int CServer::Run() BindAddr.port = g_Config.m_SvPort; } - if(!m_NetServer.Open(BindAddr, &m_ServerBan, g_Config.m_SvMaxClients, g_Config.m_SvMaxClientsPerIP, 0)) + if (!m_NetServer.Open(BindAddr, &m_ServerBan, g_Config.m_SvMaxClients, g_Config.m_SvMaxClientsPerIP, 0)) { dbg_msg("server", "couldn't open socket. port %d might already be in use", g_Config.m_SvPort); return -1; @@ -1377,39 +1358,37 @@ int CServer::Run() m_Lastheartbeat = 0; m_GameStartTime = time_get(); - if(g_Config.m_Debug) + if (g_Config.m_Debug) { - str_format(aBuf, sizeof(aBuf), "baseline memory usage %dk", mem_stats()->allocated/1024); + str_format(aBuf, sizeof(aBuf), "baseline memory usage %dk", mem_stats()->allocated / 1024); Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "server", aBuf); } - while(m_RunServer) + while (m_RunServer) { int64 t = time_get(); int NewTicks = 0; // load new map TODO: don't poll this - if(str_comp(g_Config.m_SvMap, m_aCurrentMap) != 0 || m_MapReload) + if (str_comp(g_Config.m_SvMap, m_aCurrentMap) != 0 || m_MapReload) { m_MapReload = 0; // load map - if(LoadMap(g_Config.m_SvMap)) + if (LoadMap(g_Config.m_SvMap)) { // new map loaded GameServer()->OnShutdown(); - - - for(int c = 0; c < MAX_CLIENTS; c++) + for (int c = 0; c < MAX_CLIENTS; c++) { m_NetServer.m_SlotTakenByBot[c] = false; - - if(m_aClients[c].m_State <= CClient::STATE_AUTH) + + if (m_aClients[c].m_State <= CClient::STATE_AUTH) continue; SendMap(c); - + m_aClients[c].Reset(); m_aClients[c].m_State = CClient::STATE_CONNECTING; } @@ -1428,21 +1407,21 @@ int CServer::Run() } } - while(t > TickStartTime(m_CurrentGameTick+1)) + while (t > TickStartTime(m_CurrentGameTick + 1)) { m_CurrentGameTick++; NewTicks++; // apply new input - for(int c = 0; c < MAX_CLIENTS; c++) + for (int c = 0; c < MAX_CLIENTS; c++) { - if(m_aClients[c].m_State == CClient::STATE_EMPTY) + if (m_aClients[c].m_State == CClient::STATE_EMPTY) continue; - for(int i = 0; i < 200; i++) + for (int i = 0; i < 200; i++) { - if(m_aClients[c].m_aInputs[i].m_GameTick == Tick()) + if (m_aClients[c].m_aInputs[i].m_GameTick == Tick()) { - if(m_aClients[c].m_State == CClient::STATE_INGAME) + if (m_aClients[c].m_State == CClient::STATE_INGAME) GameServer()->OnClientPredictedInput(c, m_aClients[c].m_aInputs[i].m_aData); break; } @@ -1453,9 +1432,9 @@ int CServer::Run() } // snap game - if(NewTicks) + if (NewTicks) { - if(g_Config.m_SvHighBandwidth || (m_CurrentGameTick%2) == 0) + if (g_Config.m_SvHighBandwidth || (m_CurrentGameTick % 2) == 0) DoSnapshot(); UpdateClientRconCommands(); @@ -1464,13 +1443,12 @@ int CServer::Run() // master server stuff m_Register.RegisterUpdate(m_NetServer.NetType()); - UpdateAIInput(); PumpNetwork(); - if(ReportTime < time_get()) + if (ReportTime < time_get()) { - if(g_Config.m_Debug) + if (g_Config.m_Debug) { /* static NETSTATS prev_stats; @@ -1490,7 +1468,7 @@ int CServer::Run() */ } - ReportTime += time_freq()*ReportInterval; + ReportTime += time_freq() * ReportInterval; } // wait for incomming data @@ -1498,9 +1476,9 @@ int CServer::Run() } } // disconnect all clients on shutdown - for(int i = 0; i < MAX_CLIENTS; ++i) + for (int i = 0; i < MAX_CLIENTS; ++i) { - if(m_aClients[i].m_State != CClient::STATE_EMPTY) + if (m_aClients[i].m_State != CClient::STATE_EMPTY) m_NetServer.Drop(i, "Server shutdown"); m_Econ.Shutdown(); @@ -1509,14 +1487,14 @@ int CServer::Run() GameServer()->OnShutdown(); m_pMap->Unload(); - if(m_pCurrentMapData) + if (m_pCurrentMapData) mem_free(m_pCurrentMapData); return 0; } void CServer::ConKick(IConsole::IResult *pResult, void *pUser) { - if(pResult->NumArguments() > 1) + if (pResult->NumArguments() > 1) { char aBuf[128]; str_format(aBuf, sizeof(aBuf), "Kicked (%s)", pResult->GetString(1)); @@ -1530,19 +1508,19 @@ void CServer::ConStatus(IConsole::IResult *pResult, void *pUser) { char aBuf[1024]; char aAddrStr[NETADDR_MAXSTRSIZE]; - CServer* pThis = static_cast(pUser); + CServer *pThis = static_cast(pUser); - for(int i = 0; i < MAX_CLIENTS; i++) + for (int i = 0; i < MAX_CLIENTS; i++) { - if(pThis->m_aClients[i].m_State != CClient::STATE_EMPTY) + if (pThis->m_aClients[i].m_State != CClient::STATE_EMPTY) { net_addr_str(pThis->m_NetServer.ClientAddr(i), aAddrStr, sizeof(aAddrStr), true); - if(pThis->m_aClients[i].m_State == CClient::STATE_INGAME) + if (pThis->m_aClients[i].m_State == CClient::STATE_INGAME) { - const char *pAuthStr = pThis->m_aClients[i].m_Authed == CServer::AUTHED_ADMIN ? "(Admin)" : - pThis->m_aClients[i].m_Authed == CServer::AUTHED_MOD ? "(Mod)" : ""; + const char *pAuthStr = pThis->m_aClients[i].m_Authed == CServer::AUTHED_ADMIN ? "(Admin)" : pThis->m_aClients[i].m_Authed == CServer::AUTHED_MOD ? "(Mod)" + : ""; str_format(aBuf, sizeof(aBuf), "id=%d addr=%s name='%s' score=%d %s", i, aAddrStr, - pThis->m_aClients[i].m_aName, pThis->m_aClients[i].m_Score, pAuthStr); + pThis->m_aClients[i].m_aName, pThis->m_aClients[i].m_Score, pAuthStr); } else str_format(aBuf, sizeof(aBuf), "id=%d addr=%s connecting", i, aAddrStr); @@ -1558,7 +1536,7 @@ void CServer::ConShutdown(IConsole::IResult *pResult, void *pUser) void CServer::DemoRecorder_HandleAutoStart() { - if(g_Config.m_SvAutoDemoRecord) + if (g_Config.m_SvAutoDemoRecord) { m_DemoRecorder.Stop(); char aFilename[128]; @@ -1566,7 +1544,7 @@ void CServer::DemoRecorder_HandleAutoStart() str_timestamp(aDate, sizeof(aDate)); str_format(aFilename, sizeof(aFilename), "demos/%s_%s.demo", "auto/autorecord", aDate); m_DemoRecorder.Start(Storage(), m_pConsole, aFilename, GameServer()->NetVersion(), m_aCurrentMap, m_CurrentMapCrc, "server"); - if(g_Config.m_SvAutoDemoMax) + if (g_Config.m_SvAutoDemoMax) { // clean up auto recorded demos CFileCollection AutoDemos; @@ -1582,10 +1560,10 @@ bool CServer::DemoRecorder_IsRecording() void CServer::ConRecord(IConsole::IResult *pResult, void *pUser) { - CServer* pServer = (CServer *)pUser; + CServer *pServer = (CServer *)pUser; char aFilename[128]; - if(pResult->NumArguments()) + if (pResult->NumArguments()) str_format(aFilename, sizeof(aFilename), "demos/%s.demo", pResult->GetString(0)); else { @@ -1610,12 +1588,12 @@ void CServer::ConLogout(IConsole::IResult *pResult, void *pUser) { CServer *pServer = (CServer *)pUser; - if(pServer->m_RconClientID >= 0 && pServer->m_RconClientID < MAX_CLIENTS && + if (pServer->m_RconClientID >= 0 && pServer->m_RconClientID < MAX_CLIENTS && pServer->m_aClients[pServer->m_RconClientID].m_State != CServer::CClient::STATE_EMPTY) { CMsgPacker Msg(NETMSG_RCON_AUTH_STATUS); - Msg.AddInt(0); //authed - Msg.AddInt(0); //cmdlist + Msg.AddInt(0); // authed + Msg.AddInt(0); // cmdlist pServer->SendMsgEx(&Msg, MSGFLAG_VITAL, pServer->m_RconClientID, true); pServer->m_aClients[pServer->m_RconClientID].m_Authed = AUTHED_NO; @@ -1631,36 +1609,36 @@ void CServer::ConLogout(IConsole::IResult *pResult, void *pUser) void CServer::ConchainSpecialInfoupdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData) { pfnCallback(pResult, pCallbackUserData); - if(pResult->NumArguments()) + if (pResult->NumArguments()) ((CServer *)pUserData)->UpdateServerInfo(); } void CServer::ConchainMaxclientsperipUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData) { pfnCallback(pResult, pCallbackUserData); - if(pResult->NumArguments()) + if (pResult->NumArguments()) ((CServer *)pUserData)->m_NetServer.SetMaxClientsPerIP(pResult->GetInteger(0)); } void CServer::ConchainModCommandUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData) { - if(pResult->NumArguments() == 2) + if (pResult->NumArguments() == 2) { CServer *pThis = static_cast(pUserData); const IConsole::CCommandInfo *pInfo = pThis->Console()->GetCommandInfo(pResult->GetString(0), CFGFLAG_SERVER, false); int OldAccessLevel = 0; - if(pInfo) + if (pInfo) OldAccessLevel = pInfo->GetAccessLevel(); pfnCallback(pResult, pCallbackUserData); - if(pInfo && OldAccessLevel != pInfo->GetAccessLevel()) + if (pInfo && OldAccessLevel != pInfo->GetAccessLevel()) { - for(int i = 0; i < MAX_CLIENTS; ++i) + for (int i = 0; i < MAX_CLIENTS; ++i) { - if(pThis->m_aClients[i].m_State == CServer::CClient::STATE_EMPTY || pThis->m_aClients[i].m_Authed != CServer::AUTHED_MOD || + if (pThis->m_aClients[i].m_State == CServer::CClient::STATE_EMPTY || pThis->m_aClients[i].m_Authed != CServer::AUTHED_MOD || (pThis->m_aClients[i].m_pRconCmdToSend && str_comp(pResult->GetString(0), pThis->m_aClients[i].m_pRconCmdToSend->m_pName) >= 0)) continue; - if(OldAccessLevel == IConsole::ACCESS_LEVEL_ADMIN) + if (OldAccessLevel == IConsole::ACCESS_LEVEL_ADMIN) pThis->SendRconCmdAdd(pInfo, i); else pThis->SendRconCmdRem(pInfo, i); @@ -1674,7 +1652,7 @@ void CServer::ConchainModCommandUpdate(IConsole::IResult *pResult, void *pUserDa void CServer::ConchainConsoleOutputLevelUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData) { pfnCallback(pResult, pCallbackUserData); - if(pResult->NumArguments() == 1) + if (pResult->NumArguments() == 1) { CServer *pThis = static_cast(pUserData); pThis->Console()->SetPrintOutputLevel(pThis->m_PrintCBIndex, pResult->GetInteger(0)); @@ -1694,7 +1672,7 @@ void CServer::RegisterCommands() Console()->Register("shutdown", "", CFGFLAG_SERVER, ConShutdown, this, "Shut down"); Console()->Register("logout", "", CFGFLAG_SERVER, ConLogout, this, "Logout of rcon"); - Console()->Register("record", "?s", CFGFLAG_SERVER|CFGFLAG_STORE, ConRecord, this, "Record to a file"); + Console()->Register("record", "?s", CFGFLAG_SERVER | CFGFLAG_STORE, ConRecord, this, "Record to a file"); Console()->Register("stoprecord", "", CFGFLAG_SERVER, ConStopRecord, this, "Stop recording"); Console()->Register("reload", "", CFGFLAG_SERVER, ConMapReload, this, "Reload the map"); @@ -1711,7 +1689,6 @@ void CServer::RegisterCommands() m_pGameServer->OnConsoleInit(); } - int CServer::SnapNewID() { return m_IDPool.NewID(); @@ -1722,11 +1699,10 @@ void CServer::SnapFreeID(int ID) m_IDPool.FreeID(ID); } - void *CServer::SnapNewItem(int Type, int ID, int Size) { - dbg_assert(Type >= 0 && Type <=0xffff, "incorrect type"); - dbg_assert(ID >= 0 && ID <=0xffff, "incorrect id"); + dbg_assert(Type >= 0 && Type <= 0xffff, "incorrect type"); + dbg_assert(ID >= 0 && ID <= 0xffff, "incorrect id"); return ID < 0 ? 0 : m_SnapshotBuilder.NewItem(Type, ID, Size); } @@ -1740,9 +1716,9 @@ static CServer *CreateServer() { return new CServer(); } int main(int argc, const char **argv) // ignore_convention { #if defined(CONF_FAMILY_WINDOWS) - for(int i = 1; i < argc; i++) // ignore_convention + for (int i = 1; i < argc; i++) // ignore_convention { - if(str_comp("-s", argv[i]) == 0 || str_comp("--silent", argv[i]) == 0) // ignore_convention + if (str_comp("-s", argv[i]) == 0 || str_comp("--silent", argv[i]) == 0) // ignore_convention { ShowWindow(GetConsoleWindow(), SW_HIDE); break; @@ -1757,7 +1733,7 @@ int main(int argc, const char **argv) // ignore_convention IEngine *pEngine = CreateEngine("Teeworlds"); IEngineMap *pEngineMap = CreateEngineMap(); IGameServer *pGameServer = CreateGameServer(); - IConsole *pConsole = CreateConsole(CFGFLAG_SERVER|CFGFLAG_ECON); + IConsole *pConsole = CreateConsole(CFGFLAG_SERVER | CFGFLAG_ECON); IEngineMasterServer *pEngineMasterServer = CreateEngineMasterServer(); IStorage *pStorage = CreateStorage("Teeworlds", IStorage::STORAGETYPE_SERVER, argc, argv); // ignore_convention IConfig *pConfig = CreateConfig(); @@ -1769,16 +1745,16 @@ int main(int argc, const char **argv) // ignore_convention RegisterFail = RegisterFail || !pKernel->RegisterInterface(pServer); // register as both RegisterFail = RegisterFail || !pKernel->RegisterInterface(pEngine); - RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast(pEngineMap)); // register as both - RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast(pEngineMap)); + RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast(pEngineMap)); // register as both + RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast(pEngineMap)); RegisterFail = RegisterFail || !pKernel->RegisterInterface(pGameServer); RegisterFail = RegisterFail || !pKernel->RegisterInterface(pConsole); RegisterFail = RegisterFail || !pKernel->RegisterInterface(pStorage); RegisterFail = RegisterFail || !pKernel->RegisterInterface(pConfig); - RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast(pEngineMasterServer)); // register as both - RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast(pEngineMasterServer)); + RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast(pEngineMasterServer)); // register as both + RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast(pEngineMasterServer)); - if(RegisterFail) + if (RegisterFail) return -1; } @@ -1794,8 +1770,8 @@ int main(int argc, const char **argv) // ignore_convention pConsole->ExecuteFile("autoexec.cfg"); // parse the command line arguments - if(argc > 1) // ignore_convention - pConsole->ParseArguments(argc-1, &argv[1]); // ignore_convention + if (argc > 1) // ignore_convention + pConsole->ParseArguments(argc - 1, &argv[1]); // ignore_convention // restore empty config strings to their defaults pConfig->RestoreStrings(); @@ -1818,7 +1794,6 @@ int main(int argc, const char **argv) // ignore_convention return 0; } - void CServer::AddZombie() { int ClientID = -1; @@ -1830,125 +1805,277 @@ void CServer::AddZombie() break; } } - + if (ClientID == -1) return; - + // fake reserve a slot m_NetServer.m_SlotTakenByBot[ClientID] = true; - + m_aClients[ClientID].m_State = CClient::STATE_CONNECTING; GameServer()->OnClientConnected(ClientID, true); - //GameServer()->OnClientEnter(i); + // GameServer()->OnClientEnter(i); m_aClients[ClientID].m_State = CClient::STATE_INGAME; m_aClients[ClientID].m_Bot = true; - // generate a cool name for the bot char aName1[128]; char aName2[128]; - + switch (rand() % 40) { - case 0: str_format(aName1, sizeof(aName1), "Ran"); break; - case 1: str_format(aName1, sizeof(aName1), "Dom"); break; - case 2: str_format(aName1, sizeof(aName1), "Lan"); break; - case 3: str_format(aName1, sizeof(aName1), "Mag"); break; - case 4: str_format(aName1, sizeof(aName1), "Kit"); break; - case 5: str_format(aName1, sizeof(aName1), "Dog"); break; - case 6: str_format(aName1, sizeof(aName1), "Bana"); break; - case 7: str_format(aName1, sizeof(aName1), "Sync"); break; - case 8: str_format(aName1, sizeof(aName1), "Ju"); break; - case 9: str_format(aName1, sizeof(aName1), "Rub"); break; - case 10: str_format(aName1, sizeof(aName1), "Hor"); break; - case 11: str_format(aName1, sizeof(aName1), "Afro"); break; - case 12: str_format(aName1, sizeof(aName1), "Red"); break; - case 13: str_format(aName1, sizeof(aName1), "And"); break; - case 14: str_format(aName1, sizeof(aName1), "Boss"); break; - case 15: str_format(aName1, sizeof(aName1), "Nek"); break; - case 16: str_format(aName1, sizeof(aName1), "Pat"); break; - case 17: str_format(aName1, sizeof(aName1), "Hos"); break; - case 18: str_format(aName1, sizeof(aName1), "Tee"); break; - case 19: str_format(aName1, sizeof(aName1), "Dis"); break; - case 20: str_format(aName1, sizeof(aName1), "Fat"); break; - case 21: str_format(aName1, sizeof(aName1), "Win"); break; - case 22: str_format(aName1, sizeof(aName1), "Los"); break; - case 23: str_format(aName1, sizeof(aName1), "Def"); break; - case 24: str_format(aName1, sizeof(aName1), "Whi"); break; - case 25: str_format(aName1, sizeof(aName1), "Dil"); break; - case 26: str_format(aName1, sizeof(aName1), "Ban"); break; - case 27: str_format(aName1, sizeof(aName1), "Pet"); break; - case 28: str_format(aName1, sizeof(aName1), "Cat"); break; - case 29: str_format(aName1, sizeof(aName1), "Moo"); break; - case 30: str_format(aName1, sizeof(aName1), "Piu"); break; - case 31: str_format(aName1, sizeof(aName1), "Pau"); break; - case 32: str_format(aName1, sizeof(aName1), "Two"); break; - case 33: str_format(aName1, sizeof(aName1), "One"); break; - case 34: str_format(aName1, sizeof(aName1), "Jon"); break; - case 35: str_format(aName1, sizeof(aName1), "Hax"); break; - case 36: str_format(aName1, sizeof(aName1), "Nig"); break; - case 37: str_format(aName1, sizeof(aName1), "Ho"); break; - case 38: str_format(aName1, sizeof(aName1), "Ta"); break; - default: str_format(aName1, sizeof(aName1), "Lol"); break; + case 0: + str_format(aName1, sizeof(aName1), "Ran"); + break; + case 1: + str_format(aName1, sizeof(aName1), "Dom"); + break; + case 2: + str_format(aName1, sizeof(aName1), "Lan"); + break; + case 3: + str_format(aName1, sizeof(aName1), "Mag"); + break; + case 4: + str_format(aName1, sizeof(aName1), "Kit"); + break; + case 5: + str_format(aName1, sizeof(aName1), "Dog"); + break; + case 6: + str_format(aName1, sizeof(aName1), "Bana"); + break; + case 7: + str_format(aName1, sizeof(aName1), "Sync"); + break; + case 8: + str_format(aName1, sizeof(aName1), "Ju"); + break; + case 9: + str_format(aName1, sizeof(aName1), "Rub"); + break; + case 10: + str_format(aName1, sizeof(aName1), "Hor"); + break; + case 11: + str_format(aName1, sizeof(aName1), "Afro"); + break; + case 12: + str_format(aName1, sizeof(aName1), "Red"); + break; + case 13: + str_format(aName1, sizeof(aName1), "And"); + break; + case 14: + str_format(aName1, sizeof(aName1), "Boss"); + break; + case 15: + str_format(aName1, sizeof(aName1), "Nek"); + break; + case 16: + str_format(aName1, sizeof(aName1), "Pat"); + break; + case 17: + str_format(aName1, sizeof(aName1), "Hos"); + break; + case 18: + str_format(aName1, sizeof(aName1), "Tee"); + break; + case 19: + str_format(aName1, sizeof(aName1), "Dis"); + break; + case 20: + str_format(aName1, sizeof(aName1), "Fat"); + break; + case 21: + str_format(aName1, sizeof(aName1), "Win"); + break; + case 22: + str_format(aName1, sizeof(aName1), "Los"); + break; + case 23: + str_format(aName1, sizeof(aName1), "Def"); + break; + case 24: + str_format(aName1, sizeof(aName1), "Whi"); + break; + case 25: + str_format(aName1, sizeof(aName1), "Dil"); + break; + case 26: + str_format(aName1, sizeof(aName1), "Ban"); + break; + case 27: + str_format(aName1, sizeof(aName1), "Pet"); + break; + case 28: + str_format(aName1, sizeof(aName1), "Cat"); + break; + case 29: + str_format(aName1, sizeof(aName1), "Moo"); + break; + case 30: + str_format(aName1, sizeof(aName1), "Piu"); + break; + case 31: + str_format(aName1, sizeof(aName1), "Pau"); + break; + case 32: + str_format(aName1, sizeof(aName1), "Two"); + break; + case 33: + str_format(aName1, sizeof(aName1), "One"); + break; + case 34: + str_format(aName1, sizeof(aName1), "Jon"); + break; + case 35: + str_format(aName1, sizeof(aName1), "Hax"); + break; + case 36: + str_format(aName1, sizeof(aName1), "Nig"); + break; + case 37: + str_format(aName1, sizeof(aName1), "Ho"); + break; + case 38: + str_format(aName1, sizeof(aName1), "Ta"); + break; + default: + str_format(aName1, sizeof(aName1), "Lol"); + break; } - + switch (rand() % 40) { - case 0: str_format(aName2, sizeof(aName2), "dom"); break; - case 1: str_format(aName2, sizeof(aName2), "ina"); break; - case 2: str_format(aName2, sizeof(aName2), "dil"); break; - case 3: str_format(aName2, sizeof(aName2), "net"); break; - case 4: str_format(aName2, sizeof(aName2), "ty"); break; - case 5: str_format(aName2, sizeof(aName2), "ie"); break; - case 6: str_format(aName2, sizeof(aName2), "na"); break; - case 7: str_format(aName2, sizeof(aName2), "hack"); break; - case 8: str_format(aName2, sizeof(aName2), "fox"); break; - case 9: str_format(aName2, sizeof(aName2), "duck"); break; - case 10: str_format(aName2, sizeof(aName2), "ero"); break; - case 11: str_format(aName2, sizeof(aName2), "gon"); break; - case 12: str_format(aName2, sizeof(aName2), "ho"); break; - case 13: str_format(aName2, sizeof(aName2), "pie"); break; - case 14: str_format(aName2, sizeof(aName2), "la"); break; - case 15: str_format(aName2, sizeof(aName2), "sie"); break; - case 16: str_format(aName2, sizeof(aName2), "doge"); break; - case 17: str_format(aName2, sizeof(aName2), "nana"); break; - case 18: str_format(aName2, sizeof(aName2), "per"); break; - case 19: str_format(aName2, sizeof(aName2), "style"); break; - case 20: str_format(aName2, sizeof(aName2), "tee"); break; - case 21: str_format(aName2, sizeof(aName2), "lan"); break; - case 22: str_format(aName2, sizeof(aName2), "ly"); break; - case 23: str_format(aName2, sizeof(aName2), "ling"); break; - case 24: str_format(aName2, sizeof(aName2), "do"); break; - case 25: str_format(aName2, sizeof(aName2), "die"); break; - case 26: str_format(aName2, sizeof(aName2), "be"); break; - case 27: str_format(aName2, sizeof(aName2), "lol"); break; - case 28: str_format(aName2, sizeof(aName2), "er"); break; - case 29: str_format(aName2, sizeof(aName2), "cat"); break; - case 30: str_format(aName2, sizeof(aName2), "gie"); break; - case 31: str_format(aName2, sizeof(aName2), "gy"); break; - case 32: str_format(aName2, sizeof(aName2), "piu"); break; - case 33: str_format(aName2, sizeof(aName2), "pau"); break; - case 34: str_format(aName2, sizeof(aName2), "pack"); break; - case 35: str_format(aName2, sizeof(aName2), "cent"); break; - case 36: str_format(aName2, sizeof(aName2), "hax"); break; - case 37: str_format(aName2, sizeof(aName2), "ger"); break; - case 38: str_format(aName2, sizeof(aName2), "mo"); break; - case 39: str_format(aName2, sizeof(aName2), "rd"); break; - default: str_format(aName2, sizeof(aName2), "uit"); break; + case 0: + str_format(aName2, sizeof(aName2), "dom"); + break; + case 1: + str_format(aName2, sizeof(aName2), "ina"); + break; + case 2: + str_format(aName2, sizeof(aName2), "dil"); + break; + case 3: + str_format(aName2, sizeof(aName2), "net"); + break; + case 4: + str_format(aName2, sizeof(aName2), "ty"); + break; + case 5: + str_format(aName2, sizeof(aName2), "ie"); + break; + case 6: + str_format(aName2, sizeof(aName2), "na"); + break; + case 7: + str_format(aName2, sizeof(aName2), "hack"); + break; + case 8: + str_format(aName2, sizeof(aName2), "fox"); + break; + case 9: + str_format(aName2, sizeof(aName2), "duck"); + break; + case 10: + str_format(aName2, sizeof(aName2), "ero"); + break; + case 11: + str_format(aName2, sizeof(aName2), "gon"); + break; + case 12: + str_format(aName2, sizeof(aName2), "ho"); + break; + case 13: + str_format(aName2, sizeof(aName2), "pie"); + break; + case 14: + str_format(aName2, sizeof(aName2), "la"); + break; + case 15: + str_format(aName2, sizeof(aName2), "sie"); + break; + case 16: + str_format(aName2, sizeof(aName2), "doge"); + break; + case 17: + str_format(aName2, sizeof(aName2), "nana"); + break; + case 18: + str_format(aName2, sizeof(aName2), "per"); + break; + case 19: + str_format(aName2, sizeof(aName2), "style"); + break; + case 20: + str_format(aName2, sizeof(aName2), "tee"); + break; + case 21: + str_format(aName2, sizeof(aName2), "lan"); + break; + case 22: + str_format(aName2, sizeof(aName2), "ly"); + break; + case 23: + str_format(aName2, sizeof(aName2), "ling"); + break; + case 24: + str_format(aName2, sizeof(aName2), "do"); + break; + case 25: + str_format(aName2, sizeof(aName2), "die"); + break; + case 26: + str_format(aName2, sizeof(aName2), "be"); + break; + case 27: + str_format(aName2, sizeof(aName2), "lol"); + break; + case 28: + str_format(aName2, sizeof(aName2), "er"); + break; + case 29: + str_format(aName2, sizeof(aName2), "cat"); + break; + case 30: + str_format(aName2, sizeof(aName2), "gie"); + break; + case 31: + str_format(aName2, sizeof(aName2), "gy"); + break; + case 32: + str_format(aName2, sizeof(aName2), "piu"); + break; + case 33: + str_format(aName2, sizeof(aName2), "pau"); + break; + case 34: + str_format(aName2, sizeof(aName2), "pack"); + break; + case 35: + str_format(aName2, sizeof(aName2), "cent"); + break; + case 36: + str_format(aName2, sizeof(aName2), "hax"); + break; + case 37: + str_format(aName2, sizeof(aName2), "ger"); + break; + case 38: + str_format(aName2, sizeof(aName2), "mo"); + break; + case 39: + str_format(aName2, sizeof(aName2), "rd"); + break; + default: + str_format(aName2, sizeof(aName2), "uit"); + break; } - - + char aName[128]; str_format(aName, sizeof(aName), "%s%s", aName1, aName2); - + SetClientName(ClientID, aName); SetClientClan(ClientID, "ai"); } - - - - - - - - diff --git a/src/game/collision.cpp b/src/game/collision.cpp index af0c03d..a06ce02 100644 --- a/src/game/collision.cpp +++ b/src/game/collision.cpp @@ -13,21 +13,18 @@ #include #include - - using namespace std; - CCollision::CCollision() { m_pTiles = 0; m_Width = 0; m_Height = 0; m_pLayers = 0; - + m_pPath = 0; m_pCenterWaypoint = 0; - + for (int i = 0; i < MAX_WAYPOINTS; i++) m_apWaypoint[i] = 0; } @@ -38,15 +35,15 @@ void CCollision::Init(class CLayers *pLayers) m_Width = m_pLayers->GameLayer()->m_Width; m_Height = m_pLayers->GameLayer()->m_Height; m_pTiles = static_cast(m_pLayers->Map()->GetData(m_pLayers->GameLayer()->m_Data)); - - for(int i = 0; i < m_Width*m_Height; i++) + + for (int i = 0; i < m_Width * m_Height; i++) { int Index = m_pTiles[i].m_Index; - if(Index > 128) + if (Index > 128) continue; - switch(Index) + switch (Index) { case TILE_DEATH: m_pTiles[i].m_Index = COLFLAG_DEATH; @@ -55,91 +52,76 @@ void CCollision::Init(class CLayers *pLayers) m_pTiles[i].m_Index = COLFLAG_SOLID; break; case TILE_NOHOOK: - m_pTiles[i].m_Index = COLFLAG_SOLID|COLFLAG_NOHOOK; + m_pTiles[i].m_Index = COLFLAG_SOLID | COLFLAG_NOHOOK; break; default: m_pTiles[i].m_Index = 0; } } - + m_pCenterWaypoint = 0; - + for (int i = 0; i < MAX_WAYPOINTS; i++) m_apWaypoint[i] = 0; } - - - - - - - - - void CCollision::ClearWaypoints() { m_WaypointCount = 0; - + for (int i = 0; i < MAX_WAYPOINTS; i++) { if (m_apWaypoint[i]) delete m_apWaypoint[i]; - + m_apWaypoint[i] = NULL; } - + m_pCenterWaypoint = NULL; } - void CCollision::AddWaypoint(vec2 Position, bool InnerCorner) { if (m_WaypointCount >= MAX_WAYPOINTS) return; - + m_apWaypoint[m_WaypointCount] = new CWaypoint(Position, InnerCorner); m_WaypointCount++; } - - - - void CCollision::GenerateWaypoints() { ClearWaypoints(); - for(int x = 2; x < m_Width-2; x++) + for (int x = 2; x < m_Width - 2; x++) { - for(int y = 2; y < m_Height-2; y++) + for (int y = 2; y < m_Height - 2; y++) { - if (m_pTiles[y*m_Width+x].m_Index && m_pTiles[y*m_Width+x].m_Index < 128) + if (m_pTiles[y * m_Width + x].m_Index && m_pTiles[y * m_Width + x].m_Index < 128) continue; - // find all outer corners - if ((IsTileSolid((x-1)*32, (y-1)*32) && !IsTileSolid((x-1)*32, (y-0)*32) && !IsTileSolid((x-0)*32, (y-1)*32)) || - (IsTileSolid((x-1)*32, (y+1)*32) && !IsTileSolid((x-1)*32, (y-0)*32) && !IsTileSolid((x-0)*32, (y+1)*32)) || - (IsTileSolid((x+1)*32, (y+1)*32) && !IsTileSolid((x+1)*32, (y-0)*32) && !IsTileSolid((x-0)*32, (y+1)*32)) || - (IsTileSolid((x+1)*32, (y-1)*32) && !IsTileSolid((x+1)*32, (y-0)*32) && !IsTileSolid((x-0)*32, (y-1)*32))) + if ((IsTileSolid((x - 1) * 32, (y - 1) * 32) && !IsTileSolid((x - 1) * 32, (y - 0) * 32) && !IsTileSolid((x - 0) * 32, (y - 1) * 32)) || + (IsTileSolid((x - 1) * 32, (y + 1) * 32) && !IsTileSolid((x - 1) * 32, (y - 0) * 32) && !IsTileSolid((x - 0) * 32, (y + 1) * 32)) || + (IsTileSolid((x + 1) * 32, (y + 1) * 32) && !IsTileSolid((x + 1) * 32, (y - 0) * 32) && !IsTileSolid((x - 0) * 32, (y + 1) * 32)) || + (IsTileSolid((x + 1) * 32, (y - 1) * 32) && !IsTileSolid((x + 1) * 32, (y - 0) * 32) && !IsTileSolid((x - 0) * 32, (y - 1) * 32))) { // outer corner found -> create a waypoint AddWaypoint(vec2(x, y)); } else - // find all inner corners - if ((IsTileSolid((x+1)*32, y*32) || IsTileSolid((x-1)*32, y*32)) && (IsTileSolid(x*32, (y+1)*32) || IsTileSolid(x*32, (y+1)*32))) - { - // inner corner found -> create a waypoint - //AddWaypoint(vec2(x, y), true); - AddWaypoint(vec2(x, y)); - } + // find all inner corners + if ((IsTileSolid((x + 1) * 32, y * 32) || IsTileSolid((x - 1) * 32, y * 32)) && (IsTileSolid(x * 32, (y + 1) * 32) || IsTileSolid(x * 32, (y + 1) * 32))) + { + // inner corner found -> create a waypoint + // AddWaypoint(vec2(x, y), true); + AddWaypoint(vec2(x, y)); + } } } - + bool KeepGoing = true; int i = 0; - + while (KeepGoing && i++ < 10) { ConnectWaypoints(); @@ -148,12 +130,11 @@ void CCollision::GenerateWaypoints() ConnectWaypoints(); } - // create a new waypoints between connected, far apart ones bool CCollision::GenerateSomeMoreWaypoints() { bool Result = false; - + for (int i = 0; i < m_WaypointCount; i++) { for (int j = 0; j < m_WaypointCount; j++) @@ -163,36 +144,33 @@ bool CCollision::GenerateSomeMoreWaypoints() if (abs(m_apWaypoint[i]->m_X - m_apWaypoint[j]->m_X) > 20 && m_apWaypoint[i]->m_Y == m_apWaypoint[j]->m_Y) { int x = (m_apWaypoint[i]->m_X + m_apWaypoint[j]->m_X) / 2; - - if (IsTileSolid(x*32, (m_apWaypoint[i]->m_Y+1)*32) || IsTileSolid(x*32, (m_apWaypoint[i]->m_Y-1)*32)) + + if (IsTileSolid(x * 32, (m_apWaypoint[i]->m_Y + 1) * 32) || IsTileSolid(x * 32, (m_apWaypoint[i]->m_Y - 1) * 32)) { AddWaypoint(vec2(x, m_apWaypoint[i]->m_Y)); Result = true; } } - + if (abs(m_apWaypoint[i]->m_Y - m_apWaypoint[j]->m_Y) > 30 && m_apWaypoint[i]->m_X == m_apWaypoint[j]->m_X) { int y = (m_apWaypoint[i]->m_Y + m_apWaypoint[j]->m_Y) / 2; - - if (IsTileSolid((m_apWaypoint[i]->m_X+1)*32, y*32) || IsTileSolid((m_apWaypoint[i]->m_X-1)*32, y*32)) + + if (IsTileSolid((m_apWaypoint[i]->m_X + 1) * 32, y * 32) || IsTileSolid((m_apWaypoint[i]->m_X - 1) * 32, y * 32)) { AddWaypoint(vec2(m_apWaypoint[i]->m_X, y)); Result = true; } } - - + m_apWaypoint[i]->Unconnect(m_apWaypoint[j]); } } } - + return Result; } - - CWaypoint *CCollision::GetWaypointAt(int x, int y) { for (int i = 0; i < m_WaypointCount; i++) @@ -206,93 +184,89 @@ CWaypoint *CCollision::GetWaypointAt(int x, int y) return NULL; } - void CCollision::ConnectWaypoints() { m_ConnectionCount = 0; - + // clear existing connections for (int i = 0; i < m_WaypointCount; i++) { if (!m_apWaypoint[i]) continue; - + m_apWaypoint[i]->ClearConnections(); } - - - + for (int i = 0; i < m_WaypointCount; i++) { if (!m_apWaypoint[i]) continue; - + int x, y; - + x = m_apWaypoint[i]->m_X - 1; y = m_apWaypoint[i]->m_Y; - + // find waypoints at left - while (!m_pTiles[y*m_Width+x].m_Index || m_pTiles[y*m_Width+x].m_Index >= 128) + while (!m_pTiles[y * m_Width + x].m_Index || m_pTiles[y * m_Width + x].m_Index >= 128) { CWaypoint *W = GetWaypointAt(x, y); - + if (W) { if (m_apWaypoint[i]->Connect(W)) m_ConnectionCount++; break; } - - //if (!IsTileSolid(x*32, (y-1)*32) && !IsTileSolid(x*32, (y+1)*32)) - if (!IsTileSolid(x*32, (y+1)*32)) + + // if (!IsTileSolid(x*32, (y-1)*32) && !IsTileSolid(x*32, (y+1)*32)) + if (!IsTileSolid(x * 32, (y + 1) * 32)) break; - + x--; } - + x = m_apWaypoint[i]->m_X; y = m_apWaypoint[i]->m_Y - 1; - + int n = 0; - + // find waypoints at up - //bool SolidFound = false; - while ((!m_pTiles[y*m_Width+x].m_Index || m_pTiles[y*m_Width+x].m_Index >= 128) && n++ < 10) + // bool SolidFound = false; + while ((!m_pTiles[y * m_Width + x].m_Index || m_pTiles[y * m_Width + x].m_Index >= 128) && n++ < 10) { CWaypoint *W = GetWaypointAt(x, y); - - //if (IsTileSolid((x+1)*32, y*32) || IsTileSolid((x+1)*32, y*32)) + + // if (IsTileSolid((x+1)*32, y*32) || IsTileSolid((x+1)*32, y*32)) // SolidFound = true; - - //if (W && SolidFound) + + // if (W && SolidFound) if (W) { if (m_apWaypoint[i]->Connect(W)) m_ConnectionCount++; break; } - + y--; } } - - + // connect to near, visible waypoints for (int i = 0; i < m_WaypointCount; i++) { if (m_apWaypoint[i] && m_apWaypoint[i]->m_InnerCorner) continue; - + for (int j = 0; j < m_WaypointCount; j++) { if (m_apWaypoint[j] && m_apWaypoint[j]->m_InnerCorner) continue; - + if (m_apWaypoint[i] && m_apWaypoint[j] && !m_apWaypoint[i]->Connected(m_apWaypoint[j])) { float Dist = distance(m_apWaypoint[i]->m_Pos, m_apWaypoint[j]->m_Pos); - + if (Dist < 600 && !IntersectLine(m_apWaypoint[i]->m_Pos, m_apWaypoint[j]->m_Pos, NULL, NULL, true)) { if (m_apWaypoint[i]->Connect(m_apWaypoint[j])) @@ -303,19 +277,18 @@ void CCollision::ConnectWaypoints() } } - CWaypoint *CCollision::GetClosestWaypoint(vec2 Pos) { CWaypoint *W = NULL; float Dist = 9000; - + for (int i = 0; i < m_WaypointCount; i++) { if (m_apWaypoint[i]) { - //int d = abs(m_apWaypoint[i]->m_Pos.x-Pos.x)+abs(m_apWaypoint[i]->m_Pos.y-Pos.y); + // int d = abs(m_apWaypoint[i]->m_Pos.x-Pos.x)+abs(m_apWaypoint[i]->m_Pos.y-Pos.y); int d = distance(m_apWaypoint[i]->m_Pos, Pos); - + if (d < Dist && d < 800) { if (!FastIntersectLine(m_apWaypoint[i]->m_Pos, Pos)) @@ -326,144 +299,127 @@ CWaypoint *CCollision::GetClosestWaypoint(vec2 Pos) } } } - + return W; } - void CCollision::SetWaypointCenter(vec2 Position) { m_pCenterWaypoint = GetClosestWaypoint(Position); - + // clear path weights for (int i = 0; i < m_WaypointCount; i++) { if (m_apWaypoint[i]) m_apWaypoint[i]->m_PathDistance = 0; } - + if (m_pCenterWaypoint) m_pCenterWaypoint->SetCenter(); - } - void CCollision::AddWeight(vec2 Pos, int Weight) { CWaypoint *Wp = GetClosestWaypoint(Pos); - + if (Wp) Wp->AddWeight(Weight); } - - bool CCollision::FindWaypointPath(vec2 TargetPos) { - CWaypoint *Target = GetClosestWaypoint(TargetPos); - - + if (Target && m_pCenterWaypoint) { if (m_pPath) delete m_pPath; - + m_pPath = Target->FindPathToCenter(); - + // for displaying the chosen waypoints for (int w = 0; w < 99; w++) m_aPath[w] = vec2(0, 0); - + if (m_pPath) { CWaypointPath *Wp = m_pPath; - + int p = 0; for (int w = 0; w < 10; w++) { m_aPath[p++] = vec2(Wp->m_Pos.x, Wp->m_Pos.y); - + if (Wp->m_pNext) Wp = Wp->m_pNext; else break; } - + return true; } } - + return false; } - - - - int CCollision::GetTile(int x, int y) { - int Nx = clamp(x/32, 0, m_Width-1); - int Ny = clamp(y/32, 0, m_Height-1); + int Nx = clamp(x / 32, 0, m_Width - 1); + int Ny = clamp(y / 32, 0, m_Height - 1); - return m_pTiles[Ny*m_Width+Nx].m_Index > 128 ? 0 : m_pTiles[Ny*m_Width+Nx].m_Index; + return m_pTiles[Ny * m_Width + Nx].m_Index > 128 ? 0 : m_pTiles[Ny * m_Width + Nx].m_Index; } bool CCollision::IsTileSolid(int x, int y, bool IncludeDeath) { - int t = GetTile(x, y); - if (IncludeDeath && GetTile(x, y)&COLFLAG_DEATH) + if (IncludeDeath && GetTile(x, y) & COLFLAG_DEATH) return true; - - return GetTile(x, y)&COLFLAG_SOLID; - - //return GetTile(x, y)&COLFLAG_SOLID; -} - + return GetTile(x, y) & COLFLAG_SOLID; +} int CCollision::FastIntersectLine(vec2 Pos0, vec2 Pos1) { - //float Distance = distance(Pos0, Pos1) / 4.0f; + // float Distance = distance(Pos0, Pos1) / 4.0f; float Distance = distance(Pos0, Pos1); - int End(Distance+1); + int End(Distance + 1); - for(int i = 0; i < End; i++) + for (int i = 0; i < End; i++) { - float a = i/Distance; + float a = i / Distance; vec2 Pos = mix(Pos0, Pos1, a); - if(CheckPoint(Pos.x, Pos.y)) + if (CheckPoint(Pos.x, Pos.y)) return GetCollisionAt(Pos.x, Pos.y); } return 0; } - // TODO: rewrite this smarter! int CCollision::IntersectLine(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision, bool IncludeDeath) { float Distance = distance(Pos0, Pos1); - int End(Distance+1); + int End(Distance + 1); vec2 Last = Pos0; - for(int i = 0; i < End; i++) + for (int i = 0; i < End; i++) { - float a = i/Distance; + float a = i / Distance; vec2 Pos = mix(Pos0, Pos1, a); - if(CheckPoint(Pos.x, Pos.y, IncludeDeath)) + if (CheckPoint(Pos.x, Pos.y, IncludeDeath)) { - if(pOutCollision) + if (pOutCollision) *pOutCollision = Pos; - if(pOutBeforeCollision) + if (pOutBeforeCollision) *pOutBeforeCollision = Last; return GetCollisionAt(Pos.x, Pos.y); } Last = Pos; } - if(pOutCollision) + if (pOutCollision) *pOutCollision = Pos1; - if(pOutBeforeCollision) + if (pOutBeforeCollision) *pOutBeforeCollision = Pos1; return 0; } @@ -471,31 +427,31 @@ int CCollision::IntersectLine(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *p // TODO: OPT: rewrite this smarter! void CCollision::MovePoint(vec2 *pInoutPos, vec2 *pInoutVel, float Elasticity, int *pBounces) { - if(pBounces) + if (pBounces) *pBounces = 0; vec2 Pos = *pInoutPos; vec2 Vel = *pInoutVel; - if(CheckPoint(Pos + Vel)) + if (CheckPoint(Pos + Vel)) { int Affected = 0; - if(CheckPoint(Pos.x + Vel.x, Pos.y)) + if (CheckPoint(Pos.x + Vel.x, Pos.y)) { pInoutVel->x *= -Elasticity; - if(pBounces) + if (pBounces) (*pBounces)++; Affected++; } - if(CheckPoint(Pos.x, Pos.y + Vel.y)) + if (CheckPoint(Pos.x, Pos.y + Vel.y)) { pInoutVel->y *= -Elasticity; - if(pBounces) + if (pBounces) (*pBounces)++; Affected++; } - if(Affected == 0) + if (Affected == 0) { pInoutVel->x *= -Elasticity; pInoutVel->y *= -Elasticity; @@ -510,13 +466,13 @@ void CCollision::MovePoint(vec2 *pInoutPos, vec2 *pInoutVel, float Elasticity, i bool CCollision::TestBox(vec2 Pos, vec2 Size) { Size *= 0.5f; - if(CheckPoint(Pos.x-Size.x, Pos.y-Size.y)) + if (CheckPoint(Pos.x - Size.x, Pos.y - Size.y)) return true; - if(CheckPoint(Pos.x+Size.x, Pos.y-Size.y)) + if (CheckPoint(Pos.x + Size.x, Pos.y - Size.y)) return true; - if(CheckPoint(Pos.x-Size.x, Pos.y+Size.y)) + if (CheckPoint(Pos.x - Size.x, Pos.y + Size.y)) return true; - if(CheckPoint(Pos.x+Size.x, Pos.y+Size.y)) + if (CheckPoint(Pos.x + Size.x, Pos.y + Size.y)) return true; return false; } @@ -530,30 +486,30 @@ void CCollision::MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, float Elas float Distance = length(Vel); int Max = (int)Distance; - if(Distance > 0.00001f) + if (Distance > 0.00001f) { - //vec2 old_pos = pos; - float Fraction = 1.0f/(float)(Max+1); - for(int i = 0; i <= Max; i++) + // vec2 old_pos = pos; + float Fraction = 1.0f / (float)(Max + 1); + for (int i = 0; i <= Max; i++) { - //float amount = i/(float)max; - //if(max == 0) - //amount = 0; + // float amount = i/(float)max; + // if(max == 0) + // amount = 0; - vec2 NewPos = Pos + Vel*Fraction; // TODO: this row is not nice + vec2 NewPos = Pos + Vel * Fraction; // TODO: this row is not nice - if(TestBox(vec2(NewPos.x, NewPos.y), Size)) + if (TestBox(vec2(NewPos.x, NewPos.y), Size)) { int Hits = 0; - if(TestBox(vec2(Pos.x, NewPos.y), Size)) + if (TestBox(vec2(Pos.x, NewPos.y), Size)) { NewPos.y = Pos.y; Vel.y *= -Elasticity; Hits++; } - if(TestBox(vec2(NewPos.x, Pos.y), Size)) + if (TestBox(vec2(NewPos.x, Pos.y), Size)) { NewPos.x = Pos.x; Vel.x *= -Elasticity; @@ -562,7 +518,7 @@ void CCollision::MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, float Elas // neither of the tests got a collision. // this is a real _corner case_! - if(Hits == 0) + if (Hits == 0) { NewPos.y = Pos.y; Vel.y *= -Elasticity; diff --git a/src/game/editor/layer_tiles.cpp b/src/game/editor/layer_tiles.cpp index 032f391..3d02a19 100644 --- a/src/game/editor/layer_tiles.cpp +++ b/src/game/editor/layer_tiles.cpp @@ -28,94 +28,94 @@ CLayerTiles::CLayerTiles(int w, int h) m_ColorEnv = -1; m_ColorEnvOffset = 0; - m_pTiles = new CTile[m_Width*m_Height]; - mem_zero(m_pTiles, m_Width*m_Height*sizeof(CTile)); + m_pTiles = new CTile[m_Width * m_Height]; + mem_zero(m_pTiles, m_Width * m_Height * sizeof(CTile)); } CLayerTiles::~CLayerTiles() { - delete [] m_pTiles; + delete[] m_pTiles; } void CLayerTiles::PrepareForSave() { - for(int y = 0; y < m_Height; y++) - for(int x = 0; x < m_Width; x++) - m_pTiles[y*m_Width+x].m_Flags &= TILEFLAG_VFLIP|TILEFLAG_HFLIP|TILEFLAG_ROTATE; + for (int y = 0; y < m_Height; y++) + for (int x = 0; x < m_Width; x++) + m_pTiles[y * m_Width + x].m_Flags &= TILEFLAG_VFLIP | TILEFLAG_HFLIP | TILEFLAG_ROTATE; - if(m_Image != -1 && m_Color.a == 255) + if (m_Image != -1 && m_Color.a == 255) { - for(int y = 0; y < m_Height; y++) - for(int x = 0; x < m_Width; x++) - m_pTiles[y*m_Width+x].m_Flags |= m_pEditor->m_Map.m_lImages[m_Image]->m_aTileFlags[m_pTiles[y*m_Width+x].m_Index]; + for (int y = 0; y < m_Height; y++) + for (int x = 0; x < m_Width; x++) + m_pTiles[y * m_Width + x].m_Flags |= m_pEditor->m_Map.m_lImages[m_Image]->m_aTileFlags[m_pTiles[y * m_Width + x].m_Index]; } } void CLayerTiles::MakePalette() { - for(int y = 0; y < m_Height; y++) - for(int x = 0; x < m_Width; x++) - m_pTiles[y*m_Width+x].m_Index = y*16+x; + for (int y = 0; y < m_Height; y++) + for (int x = 0; x < m_Width; x++) + m_pTiles[y * m_Width + x].m_Index = y * 16 + x; } void CLayerTiles::Render() { - if(m_Image >= 0 && m_Image < m_pEditor->m_Map.m_lImages.size()) + if (m_Image >= 0 && m_Image < m_pEditor->m_Map.m_lImages.size()) m_TexID = m_pEditor->m_Map.m_lImages[m_Image]->m_TexID; Graphics()->TextureSet(m_TexID); - vec4 Color = vec4(m_Color.r/255.0f, m_Color.g/255.0f, m_Color.b/255.0f, m_Color.a/255.0f); + vec4 Color = vec4(m_Color.r / 255.0f, m_Color.g / 255.0f, m_Color.b / 255.0f, m_Color.a / 255.0f); Graphics()->BlendNone(); m_pEditor->RenderTools()->RenderTilemap(m_pTiles, m_Width, m_Height, 32.0f, Color, LAYERRENDERFLAG_OPAQUE, - m_pEditor->EnvelopeEval, m_pEditor, m_ColorEnv, m_ColorEnvOffset); + m_pEditor->EnvelopeEval, m_pEditor, m_ColorEnv, m_ColorEnvOffset); Graphics()->BlendNormal(); m_pEditor->RenderTools()->RenderTilemap(m_pTiles, m_Width, m_Height, 32.0f, Color, LAYERRENDERFLAG_TRANSPARENT, - m_pEditor->EnvelopeEval, m_pEditor, m_ColorEnv, m_ColorEnvOffset); + m_pEditor->EnvelopeEval, m_pEditor, m_ColorEnv, m_ColorEnvOffset); } -int CLayerTiles::ConvertX(float x) const { return (int)(x/32.0f); } -int CLayerTiles::ConvertY(float y) const { return (int)(y/32.0f); } +int CLayerTiles::ConvertX(float x) const { return (int)(x / 32.0f); } +int CLayerTiles::ConvertY(float y) const { return (int)(y / 32.0f); } void CLayerTiles::Convert(CUIRect Rect, RECTi *pOut) { pOut->x = ConvertX(Rect.x); pOut->y = ConvertY(Rect.y); - pOut->w = ConvertX(Rect.x+Rect.w+31) - pOut->x; - pOut->h = ConvertY(Rect.y+Rect.h+31) - pOut->y; + pOut->w = ConvertX(Rect.x + Rect.w + 31) - pOut->x; + pOut->h = ConvertY(Rect.y + Rect.h + 31) - pOut->y; } void CLayerTiles::Snap(CUIRect *pRect) { RECTi Out; Convert(*pRect, &Out); - pRect->x = Out.x*32.0f; - pRect->y = Out.y*32.0f; - pRect->w = Out.w*32.0f; - pRect->h = Out.h*32.0f; + pRect->x = Out.x * 32.0f; + pRect->y = Out.y * 32.0f; + pRect->w = Out.w * 32.0f; + pRect->h = Out.h * 32.0f; } void CLayerTiles::Clamp(RECTi *pRect) { - if(pRect->x < 0) + if (pRect->x < 0) { pRect->w += pRect->x; pRect->x = 0; } - if(pRect->y < 0) + if (pRect->y < 0) { pRect->h += pRect->y; pRect->y = 0; } - if(pRect->x+pRect->w > m_Width) + if (pRect->x + pRect->w > m_Width) pRect->w = m_Width - pRect->x; - if(pRect->y+pRect->h > m_Height) + if (pRect->y + pRect->h > m_Height) pRect->h = m_Height - pRect->y; - if(pRect->h < 0) + if (pRect->h < 0) pRect->h = 0; - if(pRect->w < 0) + if (pRect->w < 0) pRect->w = 0; } @@ -130,7 +130,7 @@ void CLayerTiles::BrushSelecting(CUIRect Rect) m_pEditor->Graphics()->QuadsEnd(); char aBuf[16]; str_format(aBuf, sizeof(aBuf), "%d,%d", ConvertX(Rect.w), ConvertY(Rect.h)); - TextRender()->Text(0, Rect.x+3.0f, Rect.y+3.0f, m_pEditor->m_ShowPicker?15.0f:15.0f*m_pEditor->m_WorldZoom, aBuf, -1); + TextRender()->Text(0, Rect.x + 3.0f, Rect.y + 3.0f, m_pEditor->m_ShowPicker ? 15.0f : 15.0f * m_pEditor->m_WorldZoom, aBuf, -1); } int CLayerTiles::BrushGrab(CLayerGroup *pBrush, CUIRect Rect) @@ -139,7 +139,7 @@ int CLayerTiles::BrushGrab(CLayerGroup *pBrush, CUIRect Rect) Convert(Rect, &r); Clamp(&r); - if(!r.w || !r.h) + if (!r.w || !r.h) return 0; // create new layers @@ -151,16 +151,16 @@ int CLayerTiles::BrushGrab(CLayerGroup *pBrush, CUIRect Rect) pBrush->AddLayer(pGrabbed); // copy the tiles - for(int y = 0; y < r.h; y++) - for(int x = 0; x < r.w; x++) - pGrabbed->m_pTiles[y*pGrabbed->m_Width+x] = m_pTiles[(r.y+y)*m_Width+(r.x+x)]; + for (int y = 0; y < r.h; y++) + for (int x = 0; x < r.w; x++) + pGrabbed->m_pTiles[y * pGrabbed->m_Width + x] = m_pTiles[(r.y + y) * m_Width + (r.x + x)]; return 1; } void CLayerTiles::FillSelection(bool Empty, CLayer *pBrush, CUIRect Rect) { - if(m_Readonly) + if (m_Readonly) return; Snap(&Rect); @@ -170,22 +170,22 @@ void CLayerTiles::FillSelection(bool Empty, CLayer *pBrush, CUIRect Rect) int w = ConvertX(Rect.w); int h = ConvertY(Rect.h); - CLayerTiles *pLt = static_cast(pBrush); + CLayerTiles *pLt = static_cast(pBrush); - for(int y = 0; y < h; y++) + for (int y = 0; y < h; y++) { - for(int x = 0; x < w; x++) + for (int x = 0; x < w; x++) { - int fx = x+sx; - int fy = y+sy; + int fx = x + sx; + int fy = y + sy; - if(fx < 0 || fx >= m_Width || fy < 0 || fy >= m_Height) + if (fx < 0 || fx >= m_Width || fy < 0 || fy >= m_Height) continue; - if(Empty) - m_pTiles[fy*m_Width+fx].m_Index = 1; + if (Empty) + m_pTiles[fy * m_Width + fx].m_Index = 1; else - m_pTiles[fy*m_Width+fx] = pLt->m_pTiles[(y*pLt->m_Width + x%pLt->m_Width) % (pLt->m_Width*pLt->m_Height)]; + m_pTiles[fy * m_Width + fx] = pLt->m_pTiles[(y * pLt->m_Width + x % pLt->m_Width) % (pLt->m_Width * pLt->m_Height)]; } } m_pEditor->m_Map.m_Modified = true; @@ -193,7 +193,7 @@ void CLayerTiles::FillSelection(bool Empty, CLayer *pBrush, CUIRect Rect) void CLayerTiles::BrushDraw(CLayer *pBrush, float wx, float wy) { - if(m_Readonly) + if (m_Readonly) return; // @@ -201,71 +201,71 @@ void CLayerTiles::BrushDraw(CLayer *pBrush, float wx, float wy) int sx = ConvertX(wx); int sy = ConvertY(wy); - for(int y = 0; y < l->m_Height; y++) - for(int x = 0; x < l->m_Width; x++) + for (int y = 0; y < l->m_Height; y++) + for (int x = 0; x < l->m_Width; x++) { - int fx = x+sx; - int fy = y+sy; - if(fx<0 || fx >= m_Width || fy < 0 || fy >= m_Height) + int fx = x + sx; + int fy = y + sy; + if (fx < 0 || fx >= m_Width || fy < 0 || fy >= m_Height) continue; - m_pTiles[fy*m_Width+fx] = l->m_pTiles[y*l->m_Width+x]; + m_pTiles[fy * m_Width + fx] = l->m_pTiles[y * l->m_Width + x]; } m_pEditor->m_Map.m_Modified = true; } void CLayerTiles::BrushFlipX() { - for(int y = 0; y < m_Height; y++) - for(int x = 0; x < m_Width/2; x++) + for (int y = 0; y < m_Height; y++) + for (int x = 0; x < m_Width / 2; x++) { - CTile Tmp = m_pTiles[y*m_Width+x]; - m_pTiles[y*m_Width+x] = m_pTiles[y*m_Width+m_Width-1-x]; - m_pTiles[y*m_Width+m_Width-1-x] = Tmp; + CTile Tmp = m_pTiles[y * m_Width + x]; + m_pTiles[y * m_Width + x] = m_pTiles[y * m_Width + m_Width - 1 - x]; + m_pTiles[y * m_Width + m_Width - 1 - x] = Tmp; } - if(!m_Game) - for(int y = 0; y < m_Height; y++) - for(int x = 0; x < m_Width; x++) - m_pTiles[y*m_Width+x].m_Flags ^= m_pTiles[y*m_Width+x].m_Flags&TILEFLAG_ROTATE ? TILEFLAG_HFLIP : TILEFLAG_VFLIP; + if (!m_Game) + for (int y = 0; y < m_Height; y++) + for (int x = 0; x < m_Width; x++) + m_pTiles[y * m_Width + x].m_Flags ^= m_pTiles[y * m_Width + x].m_Flags & TILEFLAG_ROTATE ? TILEFLAG_HFLIP : TILEFLAG_VFLIP; } void CLayerTiles::BrushFlipY() { - for(int y = 0; y < m_Height/2; y++) - for(int x = 0; x < m_Width; x++) + for (int y = 0; y < m_Height / 2; y++) + for (int x = 0; x < m_Width; x++) { - CTile Tmp = m_pTiles[y*m_Width+x]; - m_pTiles[y*m_Width+x] = m_pTiles[(m_Height-1-y)*m_Width+x]; - m_pTiles[(m_Height-1-y)*m_Width+x] = Tmp; + CTile Tmp = m_pTiles[y * m_Width + x]; + m_pTiles[y * m_Width + x] = m_pTiles[(m_Height - 1 - y) * m_Width + x]; + m_pTiles[(m_Height - 1 - y) * m_Width + x] = Tmp; } - if(!m_Game) - for(int y = 0; y < m_Height; y++) - for(int x = 0; x < m_Width; x++) - m_pTiles[y*m_Width+x].m_Flags ^= m_pTiles[y*m_Width+x].m_Flags&TILEFLAG_ROTATE ? TILEFLAG_VFLIP : TILEFLAG_HFLIP; + if (!m_Game) + for (int y = 0; y < m_Height; y++) + for (int x = 0; x < m_Width; x++) + m_pTiles[y * m_Width + x].m_Flags ^= m_pTiles[y * m_Width + x].m_Flags & TILEFLAG_ROTATE ? TILEFLAG_VFLIP : TILEFLAG_HFLIP; } void CLayerTiles::BrushRotate(float Amount) { - int Rotation = (round(360.0f*Amount/(pi*2))/90)%4; // 0=0°, 1=90°, 2=180°, 3=270° - if(Rotation < 0) - Rotation +=4; + int Rotation = (round_to_int(360.0f * Amount / (pi * 2)) / 90) % 4; // 0=0�, 1=90�, 2=180�, 3=270� + if (Rotation < 0) + Rotation += 4; - if(Rotation == 1 || Rotation == 3) + if (Rotation == 1 || Rotation == 3) { - // 90° rotation - CTile *pTempData = new CTile[m_Width*m_Height]; - mem_copy(pTempData, m_pTiles, m_Width*m_Height*sizeof(CTile)); + // 90� rotation + CTile *pTempData = new CTile[m_Width * m_Height]; + mem_copy(pTempData, m_pTiles, m_Width * m_Height * sizeof(CTile)); CTile *pDst = m_pTiles; - for(int x = 0; x < m_Width; ++x) - for(int y = m_Height-1; y >= 0; --y, ++pDst) + for (int x = 0; x < m_Width; ++x) + for (int y = m_Height - 1; y >= 0; --y, ++pDst) { - *pDst = pTempData[y*m_Width+x]; - if(!m_Game) + *pDst = pTempData[y * m_Width + x]; + if (!m_Game) { - if(pDst->m_Flags&TILEFLAG_ROTATE) - pDst->m_Flags ^= (TILEFLAG_HFLIP|TILEFLAG_VFLIP); + if (pDst->m_Flags & TILEFLAG_ROTATE) + pDst->m_Flags ^= (TILEFLAG_HFLIP | TILEFLAG_VFLIP); pDst->m_Flags ^= TILEFLAG_ROTATE; } } @@ -276,7 +276,7 @@ void CLayerTiles::BrushRotate(float Amount) delete[] pTempData; } - if(Rotation == 2 || Rotation == 3) + if (Rotation == 2 || Rotation == 3) { BrushFlipX(); BrushFlipY(); @@ -285,15 +285,15 @@ void CLayerTiles::BrushRotate(float Amount) void CLayerTiles::Resize(int NewW, int NewH) { - CTile *pNewData = new CTile[NewW*NewH]; - mem_zero(pNewData, NewW*NewH*sizeof(CTile)); + CTile *pNewData = new CTile[NewW * NewH]; + mem_zero(pNewData, NewW * NewH * sizeof(CTile)); // copy old data - for(int y = 0; y < min(NewH, m_Height); y++) - mem_copy(&pNewData[y*NewW], &m_pTiles[y*m_Width], min(m_Width, NewW)*sizeof(CTile)); + for (int y = 0; y < min(NewH, m_Height); y++) + mem_copy(&pNewData[y * NewW], &m_pTiles[y * m_Width], min(m_Width, NewW) * sizeof(CTile)); // replace old - delete [] m_pTiles; + delete[] m_pTiles; m_pTiles = pNewData; m_Width = NewW; m_Height = NewH; @@ -301,35 +301,35 @@ void CLayerTiles::Resize(int NewW, int NewH) void CLayerTiles::Shift(int Direction) { - switch(Direction) + switch (Direction) { case 1: - { - // left - for(int y = 0; y < m_Height; ++y) - mem_move(&m_pTiles[y*m_Width], &m_pTiles[y*m_Width+1], (m_Width-1)*sizeof(CTile)); - } - break; + { + // left + for (int y = 0; y < m_Height; ++y) + mem_move(&m_pTiles[y * m_Width], &m_pTiles[y * m_Width + 1], (m_Width - 1) * sizeof(CTile)); + } + break; case 2: - { - // right - for(int y = 0; y < m_Height; ++y) - mem_move(&m_pTiles[y*m_Width+1], &m_pTiles[y*m_Width], (m_Width-1)*sizeof(CTile)); - } - break; + { + // right + for (int y = 0; y < m_Height; ++y) + mem_move(&m_pTiles[y * m_Width + 1], &m_pTiles[y * m_Width], (m_Width - 1) * sizeof(CTile)); + } + break; case 4: - { - // up - for(int y = 0; y < m_Height-1; ++y) - mem_copy(&m_pTiles[y*m_Width], &m_pTiles[(y+1)*m_Width], m_Width*sizeof(CTile)); - } - break; + { + // up + for (int y = 0; y < m_Height - 1; ++y) + mem_copy(&m_pTiles[y * m_Width], &m_pTiles[(y + 1) * m_Width], m_Width * sizeof(CTile)); + } + break; case 8: - { - // down - for(int y = m_Height-1; y > 0; --y) - mem_copy(&m_pTiles[y*m_Width], &m_pTiles[(y-1)*m_Width], m_Width*sizeof(CTile)); - } + { + // down + for (int y = m_Height - 1; y > 0; --y) + mem_copy(&m_pTiles[y * m_Width], &m_pTiles[(y - 1) * m_Width], m_Width * sizeof(CTile)); + } } } @@ -340,26 +340,26 @@ void CLayerTiles::ShowInfo() Graphics()->TextureSet(m_pEditor->Client()->GetDebugFont()); Graphics()->QuadsBegin(); - int StartY = max(0, (int)(ScreenY0/32.0f)-1); - int StartX = max(0, (int)(ScreenX0/32.0f)-1); - int EndY = min((int)(ScreenY1/32.0f)+1, m_Height); - int EndX = min((int)(ScreenX1/32.0f)+1, m_Width); + int StartY = max(0, (int)(ScreenY0 / 32.0f) - 1); + int StartX = max(0, (int)(ScreenX0 / 32.0f) - 1); + int EndY = min((int)(ScreenY1 / 32.0f) + 1, m_Height); + int EndX = min((int)(ScreenX1 / 32.0f) + 1, m_Width); - for(int y = StartY; y < EndY; y++) - for(int x = StartX; x < EndX; x++) + for (int y = StartY; y < EndY; y++) + for (int x = StartX; x < EndX; x++) { - int c = x + y*m_Width; - if(m_pTiles[c].m_Index) + int c = x + y * m_Width; + if (m_pTiles[c].m_Index) { char aBuf[64]; str_format(aBuf, sizeof(aBuf), "%i", m_pTiles[c].m_Index); - m_pEditor->Graphics()->QuadsText(x*32, y*32, 16.0f, aBuf); + m_pEditor->Graphics()->QuadsText(x * 32, y * 32, 16.0f, aBuf); - char aFlags[4] = { m_pTiles[c].m_Flags&TILEFLAG_VFLIP ? 'V' : ' ', - m_pTiles[c].m_Flags&TILEFLAG_HFLIP ? 'H' : ' ', - m_pTiles[c].m_Flags&TILEFLAG_ROTATE? 'R' : ' ', - 0}; - m_pEditor->Graphics()->QuadsText(x*32, y*32+16, 16.0f, aFlags); + char aFlags[4] = {m_pTiles[c].m_Flags & TILEFLAG_VFLIP ? 'V' : ' ', + m_pTiles[c].m_Flags & TILEFLAG_HFLIP ? 'H' : ' ', + m_pTiles[c].m_Flags & TILEFLAG_ROTATE ? 'R' : ' ', + 0}; + m_pEditor->Graphics()->QuadsText(x * 32, y * 32 + 16, 16.0f, aFlags); } x += m_pTiles[c].m_Skip; } @@ -373,17 +373,17 @@ int CLayerTiles::RenderProperties(CUIRect *pToolBox) CUIRect Button; bool InGameGroup = !find_linear(m_pEditor->m_Map.m_pGameGroup->m_lLayers.all(), this).empty(); - if(m_pEditor->m_Map.m_pGameLayer != this) + if (m_pEditor->m_Map.m_pGameLayer != this) { - if(m_Image >= 0 && m_Image < m_pEditor->m_Map.m_lImages.size() && m_pEditor->m_Map.m_lImages[m_Image]->m_AutoMapper.IsLoaded()) + if (m_Image >= 0 && m_Image < m_pEditor->m_Map.m_lImages.size() && m_pEditor->m_Map.m_lImages[m_Image]->m_AutoMapper.IsLoaded()) { static int s_AutoMapperButton = 0; pToolBox->HSplitBottom(12.0f, pToolBox, &Button); - if(m_pEditor->DoButton_Editor(&s_AutoMapperButton, "Auto map", 0, &Button, 0, "")) + if (m_pEditor->DoButton_Editor(&s_AutoMapperButton, "Auto map", 0, &Button, 0, "")) m_pEditor->PopupSelectConfigAutoMapInvoke(m_pEditor->UI()->MouseX(), m_pEditor->UI()->MouseY()); int Result = m_pEditor->PopupSelectConfigAutoMapResult(); - if(Result > -1) + if (Result > -1) { m_pEditor->m_Map.m_lImages[m_Image]->m_AutoMapper.Proceed(this, Result); return 1; @@ -393,24 +393,24 @@ int CLayerTiles::RenderProperties(CUIRect *pToolBox) else InGameGroup = false; - if(InGameGroup) + if (InGameGroup) { pToolBox->HSplitBottom(2.0f, pToolBox, 0); pToolBox->HSplitBottom(12.0f, pToolBox, &Button); static int s_ColclButton = 0; - if(m_pEditor->DoButton_Editor(&s_ColclButton, "Game tiles", 0, &Button, 0, "Constructs game tiles from this layer")) + if (m_pEditor->DoButton_Editor(&s_ColclButton, "Game tiles", 0, &Button, 0, "Constructs game tiles from this layer")) m_pEditor->PopupSelectGametileOpInvoke(m_pEditor->UI()->MouseX(), m_pEditor->UI()->MouseY()); int Result = m_pEditor->PopupSelectGameTileOpResult(); - if(Result > -1) + if (Result > -1) { CLayerTiles *gl = m_pEditor->m_Map.m_pGameLayer; int w = min(gl->m_Width, m_Width); int h = min(gl->m_Height, m_Height); - for(int y = 0; y < h; y++) - for(int x = 0; x < w; x++) - if(m_pTiles[y*m_Width+x].m_Index) - gl->m_pTiles[y*gl->m_Width+x].m_Index = TILE_AIR+Result; + for (int y = 0; y < h; y++) + for (int x = 0; x < w; x++) + if (m_pTiles[y * m_Width + x].m_Index) + gl->m_pTiles[y * gl->m_Width + x].m_Index = TILE_AIR + Result; return 1; } @@ -418,7 +418,7 @@ int CLayerTiles::RenderProperties(CUIRect *pToolBox) enum { - PROP_WIDTH=0, + PROP_WIDTH = 0, PROP_HEIGHT, PROP_SHIFT, PROP_IMAGE, @@ -429,9 +429,9 @@ int CLayerTiles::RenderProperties(CUIRect *pToolBox) }; int Color = 0; - Color |= m_Color.r<<24; - Color |= m_Color.g<<16; - Color |= m_Color.b<<8; + Color |= m_Color.r << 24; + Color |= m_Color.g << 16; + Color |= m_Color.b << 8; Color |= m_Color.a; CProperty aProps[] = { @@ -440,12 +440,12 @@ int CLayerTiles::RenderProperties(CUIRect *pToolBox) {"Shift", 0, PROPTYPE_SHIFT, 0, 0}, {"Image", m_Image, PROPTYPE_IMAGE, 0, 0}, {"Color", Color, PROPTYPE_COLOR, 0, 0}, - {"Color Env", m_ColorEnv+1, PROPTYPE_INT_STEP, 0, m_pEditor->m_Map.m_lEnvelopes.size()+1}, + {"Color Env", m_ColorEnv + 1, PROPTYPE_INT_STEP, 0, m_pEditor->m_Map.m_lEnvelopes.size() + 1}, {"Color TO", m_ColorEnvOffset, PROPTYPE_INT_SCROLL, -1000000, 1000000}, {0}, }; - if(m_pEditor->m_Map.m_pGameLayer == this) // remove the image and color properties if this is the game layer + if (m_pEditor->m_Map.m_pGameLayer == this) // remove the image and color properties if this is the game layer { aProps[3].m_pName = 0; aProps[4].m_pName = 0; @@ -454,16 +454,16 @@ int CLayerTiles::RenderProperties(CUIRect *pToolBox) static int s_aIds[NUM_PROPS] = {0}; int NewVal = 0; int Prop = m_pEditor->DoProperties(pToolBox, aProps, s_aIds, &NewVal); - if(Prop != -1) + if (Prop != -1) m_pEditor->m_Map.m_Modified = true; - if(Prop == PROP_WIDTH && NewVal > 1) + if (Prop == PROP_WIDTH && NewVal > 1) Resize(NewVal, m_Height); - else if(Prop == PROP_HEIGHT && NewVal > 1) + else if (Prop == PROP_HEIGHT && NewVal > 1) Resize(m_Width, NewVal); - else if(Prop == PROP_SHIFT) + else if (Prop == PROP_SHIFT) Shift(NewVal); - else if(Prop == PROP_IMAGE) + else if (Prop == PROP_IMAGE) { if (NewVal == -1) { @@ -471,36 +471,35 @@ int CLayerTiles::RenderProperties(CUIRect *pToolBox) m_Image = -1; } else - m_Image = NewVal%m_pEditor->m_Map.m_lImages.size(); + m_Image = NewVal % m_pEditor->m_Map.m_lImages.size(); } - else if(Prop == PROP_COLOR) + else if (Prop == PROP_COLOR) { - m_Color.r = (NewVal>>24)&0xff; - m_Color.g = (NewVal>>16)&0xff; - m_Color.b = (NewVal>>8)&0xff; - m_Color.a = NewVal&0xff; + m_Color.r = (NewVal >> 24) & 0xff; + m_Color.g = (NewVal >> 16) & 0xff; + m_Color.b = (NewVal >> 8) & 0xff; + m_Color.a = NewVal & 0xff; } - if(Prop == PROP_COLOR_ENV) + if (Prop == PROP_COLOR_ENV) { - int Index = clamp(NewVal-1, -1, m_pEditor->m_Map.m_lEnvelopes.size()-1); - int Step = (Index-m_ColorEnv)%2; - if(Step != 0) + int Index = clamp(NewVal - 1, -1, m_pEditor->m_Map.m_lEnvelopes.size() - 1); + int Step = (Index - m_ColorEnv) % 2; + if (Step != 0) { - for(; Index >= -1 && Index < m_pEditor->m_Map.m_lEnvelopes.size(); Index += Step) - if(Index == -1 || m_pEditor->m_Map.m_lEnvelopes[Index]->m_Channels == 4) + for (; Index >= -1 && Index < m_pEditor->m_Map.m_lEnvelopes.size(); Index += Step) + if (Index == -1 || m_pEditor->m_Map.m_lEnvelopes[Index]->m_Channels == 4) { m_ColorEnv = Index; break; } } } - if(Prop == PROP_COLOR_ENV_OFFSET) + if (Prop == PROP_COLOR_ENV_OFFSET) m_ColorEnvOffset = NewVal; return 0; } - void CLayerTiles::ModifyImageIndex(INDEX_MODIFY_FUNC Func) { Func(&m_Image); diff --git a/src/game/server/ai.cpp b/src/game/server/ai.cpp index 63f1b9f..3c31928 100644 --- a/src/game/server/ai.cpp +++ b/src/game/server/ai.cpp @@ -8,29 +8,27 @@ #include #include "gamecontext.h" - CAI::CAI(CGameContext *pGameServer, CPlayer *pPlayer) { m_pGameServer = pGameServer; m_pPlayer = pPlayer; m_pTargetPlayer = 0; - + m_pPath = 0; m_pVisible = 0; - + Reset(); } - void CAI::Reset() { if (m_pPath) delete m_pPath; - + m_WaypointUpdateNeeded = true; m_WayPointUpdateTick = 0; m_WayVisibleUpdateTick = 0; - + m_Sleep = 0; m_Stun = 0; m_ReactionTime = 20; @@ -46,35 +44,34 @@ void CAI::Reset() m_LastPos = vec2(-9000, -9000); m_Direction = vec2(-1, 0); m_DisplayDirection = vec2(-1, 0); - + m_HookTick = 0; m_HookReleaseTick = 0; - + m_UnstuckCount = 0; - + m_WayPointUpdateWait = 0; m_WayFound = false; - + m_TargetTimer = 99; m_AttackTimer = 0; m_HookTimer = 0; m_HookReleaseTimer = 0; - + m_pTargetPlayer = 0; m_PlayerPos = vec2(0, 0); m_TargetPos = vec2(0, 0); m_PlayerSpotTimer = 0; m_PlayerSpotCount = 0; - + m_TurnSpeed = 0.2f; - + m_DontMoveTick = 0; - + m_OldTargetPos = vec2(0, 0); ClearEmotions(); } - void CAI::OnCharacterSpawn(class CCharacter *pChr) { pChr->SetCustomWeapon(HAMMER_BASIC); @@ -82,114 +79,98 @@ void CAI::OnCharacterSpawn(class CCharacter *pChr) m_WaypointPos = pChr->m_Pos; } - void CAI::Zzz(int Time) { if (!Player()->GetCharacter()) return; - + if (m_Sleep < Time) { m_Sleep = Time; - Player()->GetCharacter()->SetEmoteFor(EMOTE_HAPPY, Time*17, Time*17, true); + Player()->GetCharacter()->SetEmoteFor(EMOTE_HAPPY, Time * 17, Time * 17, true); GameServer()->SendEmoticon(Player()->GetCID(), EMOTICON_ZZZ); } } - void CAI::Stun(int Time) { if (!Player()->GetCharacter()) return; - if (m_Stun < Time) { m_Stun = Time; - Player()->GetCharacter()->SetEmoteFor(EMOTE_SURPRISE, Time*17, Time*17, true); + Player()->GetCharacter()->SetEmoteFor(EMOTE_SURPRISE, Time * 17, Time * 17, true); } } - void CAI::Panic() { Player()->GetCharacter()->SetEmoteFor(EMOTE_PAIN, 2, 2, false); - m_DontMoveTick = GameServer()->Server()->Tick() + GameServer()->Server()->TickSpeed()*1; - - if (frandom()*10 < 7) + m_DontMoveTick = GameServer()->Server()->Tick() + GameServer()->Server()->TickSpeed() * 1; + + if (frandom() * 10 < 7) m_Attack = 1; else m_Attack = 0; } - - void CAI::StandStill(int Time) { m_Sleep = Time; } - void CAI::UpdateInput(int *Data) { m_InputChanged = false; Data[0] = m_Move; - Data[1] = m_DisplayDirection.x; Data[2] = m_DisplayDirection.y; - //Data[1] = m_Direction.x; Data[2] = m_Direction.y; - + Data[1] = m_DisplayDirection.x; + Data[2] = m_DisplayDirection.y; + // Data[1] = m_Direction.x; Data[2] = m_Direction.y; + Data[3] = m_Jump; Data[4] = m_Attack; Data[5] = m_Hook; } - - - void CAI::AirJump() { - if (Player()->GetCharacter()->GetVel().y > 0 && m_WaypointPos.y + 80 < m_Pos.y ) + if (Player()->GetCharacter()->GetVel().y > 0 && m_WaypointPos.y + 80 < m_Pos.y) { - if (!GameServer()->Collision()->FastIntersectLine(m_Pos, m_Pos+vec2(0, 120)) && frandom()*10 < 5) + if (!GameServer()->Collision()->FastIntersectLine(m_Pos, m_Pos + vec2(0, 120)) && frandom() * 10 < 5) m_Jump = 1; } } - - - void CAI::DoJumping() { if (abs(m_Pos.x - m_WaypointPos.x) < 20 && m_Pos.y > m_WaypointPos.y + 30) m_Jump = 1; - if (Player()->GetCharacter()->IsGrounded() && + if (Player()->GetCharacter()->IsGrounded() && (GameServer()->Collision()->IsTileSolid(m_Pos.x + m_Move * 32, m_Pos.y) || GameServer()->Collision()->IsTileSolid(m_Pos.x + m_Move * 96, m_Pos.y))) m_Jump = 1; } - - bool CAI::UpdateWaypoint() { - if (m_WayPointUpdateTick + GameServer()->Server()->TickSpeed()*5 < GameServer()->Server()->Tick()) + if (m_WayPointUpdateTick + GameServer()->Server()->TickSpeed() * 5 < GameServer()->Server()->Tick()) m_WaypointUpdateNeeded = true; - - - //if (distance(m_WaypointPos, m_LastPos) < 100) // || m_TargetTimer++ > 30)// && m_WayPointUpdateWait > 10) + + // if (distance(m_WaypointPos, m_LastPos) < 100) // || m_TargetTimer++ > 30)// && m_WayPointUpdateWait > 10) if (m_TargetTimer++ > 40 && (!m_pVisible || m_WaypointUpdateNeeded)) { m_TargetTimer = 0; - + m_WayFound = false; - + // old inefficient path finding // prepare waypoints for path finding - //GameServer()->Collision()->SetWaypointCenter(m_Pos); - //if (GameServer()->Collision()->FindWaypointPath(m_TargetPos)) - - + // GameServer()->Collision()->SetWaypointCenter(m_Pos); + // if (GameServer()->Collision()->FindWaypointPath(m_TargetPos)) + if (GameServer()->Collision()->AStar(m_Pos, m_TargetPos)) - { + { if (m_pPath) { delete m_pPath; @@ -197,63 +178,52 @@ bool CAI::UpdateWaypoint() } m_pPath = GameServer()->Collision()->GetPath(); GameServer()->Collision()->ForgetAboutThePath(); - + if (!m_pPath) return false; - - m_pVisible = m_pPath->GetVisible(GameServer(), m_Pos-vec2(0, 16)); - + + m_pVisible = m_pPath->GetVisible(GameServer(), m_Pos - vec2(0, 16)); + m_WayPointUpdateWait = 0; m_WayFound = true; - + m_WaypointPos = m_pPath->m_Pos; m_WaypointDir = m_WaypointPos - m_Pos; - - //char aBuf[128]; str_format(aBuf, sizeof(aBuf), "Path found, lenght: %d", m_pPath->Length()); - //GameServer()->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "path", aBuf); - - vec2 LastPos = m_Pos; - CWaypointPath *Next = m_pPath; - m_WaypointUpdateNeeded = false; m_WayPointUpdateTick = GameServer()->Server()->Tick(); } - - + if (GameServer()->m_ShowWaypoints) { for (int i = 0; i < 10; i++) - new CStaticlaser(&GameServer()->m_World, GameServer()->Collision()->m_aPath[i], GameServer()->Collision()->m_aPath[i+1], 10); - + new CStaticlaser(&GameServer()->m_World, GameServer()->Collision()->m_aPath[i], GameServer()->Collision()->m_aPath[i + 1], 10); + new CStaticlaser(&GameServer()->m_World, m_Pos, m_WaypointPos, 20); } } - - + m_WayPointUpdateWait++; - - + if (m_pVisible) { m_WaypointPos = m_pVisible->m_Pos; m_WaypointDir = m_WaypointPos - m_Pos; - - m_pVisible = m_pVisible->GetVisible(GameServer(), m_Pos-vec2(0, 16)); - + + m_pVisible = m_pVisible->GetVisible(GameServer(), m_Pos - vec2(0, 16)); + m_WayVisibleUpdateTick = GameServer()->Server()->Tick(); - - //if (GameServer()->m_ShowWaypoints) + + // if (GameServer()->m_ShowWaypoints) // new CStaticlaser(&GameServer()->m_World, m_Pos, m_WaypointPos, 10); } - - + // left or right - if (abs(m_WaypointDir.y)*2 < abs(m_WaypointDir.x)) + if (abs(m_WaypointDir.y) * 2 < abs(m_WaypointDir.x)) { if (Player()->GetCharacter()->IsGrounded() && abs(m_WaypointDir.x) > 128) { int Dir = m_WaypointDir.x < 0 ? -1 : 1; - + // simple pits if (!GameServer()->Collision()->IsTileSolid(m_Pos.x + Dir * 32, m_Pos.y + 16) || !GameServer()->Collision()->IsTileSolid(m_Pos.x + Dir * 64, m_Pos.y + 16)) @@ -262,11 +232,9 @@ bool CAI::UpdateWaypoint() GameServer()->Collision()->IsTileSolid(m_Pos.x + Dir * 196, m_Pos.y + 16)) m_Jump = 1; } - } } - // check target if (distance(m_Pos, m_TargetPos) < 800) { @@ -276,57 +244,51 @@ bool CAI::UpdateWaypoint() m_WaypointDir = m_WaypointPos - m_Pos; } } - + if (!m_WayFound && !m_pVisible) { return false; - //m_WaypointPos = m_TargetPos; - //m_WaypointDir = m_WaypointPos - m_Pos; + // m_WaypointPos = m_TargetPos; + // m_WaypointDir = m_WaypointPos - m_Pos; } - + return true; } - - void CAI::HookMove() { // release hook if hooking an ally if (Player()->GetCharacter()->HookedPlayer() >= 0 && m_Hook == 1) { CPlayer *pPlayer = GameServer()->m_apPlayers[Player()->GetCharacter()->HookedPlayer()]; - if(pPlayer && pPlayer->GetTeam() == Player()->GetTeam()) + if (pPlayer && pPlayer->GetTeam() == Player()->GetTeam()) m_Hook = 0; } - + // ...or not hooking at all if (!Player()->GetCharacter()->Hooking()) { if (m_Hook == 1) m_Hook = 0; } - - - // hook if (m_LastHook == 0) { - vec2 HookDir = m_WaypointPos - m_Pos; vec2 HookPos; - + bool TryHooking = false; - + float Distance = distance(m_Pos, m_WaypointPos); - + for (int i = 0; i < 6; i++) { HookPos = m_Pos; - - vec2 Random = vec2(frandom()-frandom(), frandom()-frandom()) * (Distance / 2.0f); - - int C = GameServer()->Collision()->IntersectLine(m_Pos, m_WaypointPos+Random, &HookPos, NULL); - if (C&CCollision::COLFLAG_SOLID && !(C&CCollision::COLFLAG_NOHOOK) && m_LastHook == 0) + + vec2 Random = vec2(frandom() - frandom(), frandom() - frandom()) * (Distance / 2.0f); + + int C = GameServer()->Collision()->IntersectLine(m_Pos, m_WaypointPos + Random, &HookPos, NULL); + if (C & CCollision::COLFLAG_SOLID && !(C & CCollision::COLFLAG_NOHOOK) && m_LastHook == 0) { float Dist = distance(m_Pos, HookPos); if (abs(Dist - 230) < 170) @@ -339,90 +301,86 @@ void CAI::HookMove() if (TryHooking) { - + if (m_Pos.y < m_WaypointPos.y) TryHooking = false; - + if (m_Pos.y < m_WaypointPos.y - 100) { if (m_pVisible && m_pVisible->m_pNext && m_pVisible->m_pNext->m_Pos.y > m_Pos.y) TryHooking = false; } - + if (TryHooking) { - //if (abs(atan2(m_Direction.x, m_Direction.y) - atan2(m_DisplayDirection.x, m_DisplayDirection.y)) < PI / 6.0f && + // if (abs(atan2(m_Direction.x, m_Direction.y) - atan2(m_DisplayDirection.x, m_DisplayDirection.y)) < PI / 6.0f && if (m_DisplayDirection.y < 0) { m_Hook = 1; m_HookTick = GameServer()->Server()->Tick(); - + m_Direction = HookPos - m_Pos; } - else - if (m_PlayerSpotCount == 0) + else if (m_PlayerSpotCount == 0) m_Direction = HookPos - m_Pos; } } else m_Hook = 0; } - - + vec2 Vel = Player()->GetCharacter()->GetVel(); vec2 HookPos = Player()->GetCharacter()->GetCore().m_HookPos; // lock move direction - //if (m_Hook != 0 && Player()->GetCharacter()->Hooking()) // && m_HookMoveLock) + // if (m_Hook != 0 && Player()->GetCharacter()->Hooking()) // && m_HookMoveLock) if (m_Hook != 0) // && m_HookMoveLock) { // check hook point's surroundings for (int y = -2; y < 2; y++) { - if (GameServer()->Collision()->IsTileSolid(HookPos.x + 32, HookPos.y + y*32) && - GameServer()->Collision()->IsTileSolid(HookPos.x + 64, HookPos.y + y*32) && - GameServer()->Collision()->IsTileSolid(HookPos.x + 96, HookPos.y + y*32)) + if (GameServer()->Collision()->IsTileSolid(HookPos.x + 32, HookPos.y + y * 32) && + GameServer()->Collision()->IsTileSolid(HookPos.x + 64, HookPos.y + y * 32) && + GameServer()->Collision()->IsTileSolid(HookPos.x + 96, HookPos.y + y * 32)) m_Move = HookPos.y < m_Pos.y ? -1 : 1; - if (GameServer()->Collision()->IsTileSolid(HookPos.x - 32, HookPos.y + y*32) && - GameServer()->Collision()->IsTileSolid(HookPos.x - 64, HookPos.y + y*32) && - GameServer()->Collision()->IsTileSolid(HookPos.x - 96, HookPos.y + y*32)) + if (GameServer()->Collision()->IsTileSolid(HookPos.x - 32, HookPos.y + y * 32) && + GameServer()->Collision()->IsTileSolid(HookPos.x - 64, HookPos.y + y * 32) && + GameServer()->Collision()->IsTileSolid(HookPos.x - 96, HookPos.y + y * 32)) m_Move = HookPos.y < m_Pos.y ? 1 : -1; } } - - + // check if we're next to a wall mid air - if (!GameServer()->Collision()->IsTileSolid(m_Pos.x, m_Pos.y + 32) && + if (!GameServer()->Collision()->IsTileSolid(m_Pos.x, m_Pos.y + 32) && Vel.y > -5.0f) { /* - if (GameServer()->Collision()->IsTileSolid(m_Pos.x + 48, m_Pos.y - 32) && + if (GameServer()->Collision()->IsTileSolid(m_Pos.x + 48, m_Pos.y - 32) && GameServer()->Collision()->IsTileSolid(m_Pos.x + 48, m_Pos.y + 0 ) && GameServer()->Collision()->IsTileSolid(m_Pos.x + 48, m_Pos.y + 32)) m_Move = -1; - - if (GameServer()->Collision()->IsTileSolid(m_Pos.x - 48, m_Pos.y - 32) && + + if (GameServer()->Collision()->IsTileSolid(m_Pos.x - 48, m_Pos.y - 32) && GameServer()->Collision()->IsTileSolid(m_Pos.x - 48, m_Pos.y + 0 ) && GameServer()->Collision()->IsTileSolid(m_Pos.x - 48, m_Pos.y + 32)) m_Move = 1; */ - - if (GameServer()->Collision()->IsTileSolid(m_Pos.x + 48, m_Pos.y - 16) && - GameServer()->Collision()->IsTileSolid(m_Pos.x + 48, m_Pos.y + 16 )) + + if (GameServer()->Collision()->IsTileSolid(m_Pos.x + 48, m_Pos.y - 16) && + GameServer()->Collision()->IsTileSolid(m_Pos.x + 48, m_Pos.y + 16)) m_Move = -1; - - if (GameServer()->Collision()->IsTileSolid(m_Pos.x - 48, m_Pos.y - 16) && + + if (GameServer()->Collision()->IsTileSolid(m_Pos.x - 48, m_Pos.y - 16) && GameServer()->Collision()->IsTileSolid(m_Pos.x - 48, m_Pos.y + 16)) m_Move = 1; } - - + // release hook if (m_Hook == 1 && Player()->GetCharacter()->Hooking()) { if ((distance(m_Pos, HookPos) < 50 && Vel.y > 0) || m_Pos.y < HookPos.y || - m_HookTick + GameServer()->Server()->TickSpeed()*1 < GameServer()->Server()->Tick()) + m_HookTick + GameServer()->Server()->TickSpeed() * 1 < GameServer()->Server()->Tick()) { m_Hook = 0; m_HookReleaseTick = GameServer()->Server()->Tick(); @@ -430,21 +388,18 @@ void CAI::HookMove() } } - - - void CAI::JumpIfPlayerIsAbove() { - if (abs(m_PlayerPos.x - m_Pos.x) < 100 && m_Pos.y > m_PlayerPos.y + 100) - { - if (frandom() * 10 < 4) - m_Jump = 1; - } + if (abs(m_PlayerPos.x - m_Pos.x) < 100 && m_Pos.y > m_PlayerPos.y + 100) + { + if (frandom() * 10 < 4) + m_Jump = 1; + } } void CAI::HeadToMovingDirection() { - //if (m_Move != 0) + // if (m_Move != 0) // m_Direction = vec2(m_Move, 0); } @@ -458,13 +413,13 @@ void CAI::Unstuck() m_Move = -1; else m_Move = 1; - + /* if (frandom() * 10 < 4) m_Jump = 1; */ } - + if (m_UnstuckCount > 4) { if (frandom() * 10 < 4) @@ -476,44 +431,40 @@ void CAI::Unstuck() m_UnstuckCount = 0; m_StuckPos = m_Pos; } - + // death tile check - if (Player()->GetCharacter()->GetVel().y > 0 && GameServer()->Collision()->GetCollisionAt(m_Pos.x, m_Pos.y+32)&CCollision::COLFLAG_DEATH) + if (Player()->GetCharacter()->GetVel().y > 0 && GameServer()->Collision()->GetCollisionAt(m_Pos.x, m_Pos.y + 32) & CCollision::COLFLAG_DEATH) { m_Jump = 1; } } - - bool CAI::SeekBombArea() { CFlag *BombArea = GameServer()->m_pController->GetClosestBase(m_LastPos); - + if (BombArea) { m_TargetPos = BombArea->m_Pos; return true; } - + return false; } - bool CAI::SeekBomb() { CBomb *Bomb = GameServer()->m_pController->GetBomb(); - + if (Bomb) { m_TargetPos = Bomb->m_Pos; return true; } - + return false; } - bool CAI::MoveTowardsPlayer(int Dist) { if (abs(m_LastPos.x - m_PlayerPos.x) < Dist) @@ -521,17 +472,16 @@ bool CAI::MoveTowardsPlayer(int Dist) m_Move = 0; return true; } - + if (m_LastPos.x < m_PlayerPos.x) m_Move = 1; - + if (m_LastPos.x > m_PlayerPos.x) m_Move = -1; - + return false; } - bool CAI::MoveTowardsTarget(int Dist) { @@ -540,17 +490,16 @@ bool CAI::MoveTowardsTarget(int Dist) m_Move = 0; return true; } - + if (m_LastPos.x < m_TargetPos.x) m_Move = 1; - + if (m_LastPos.x > m_TargetPos.x) m_Move = -1; - + return false; } - bool CAI::MoveTowardsWaypoint(int Dist) { @@ -559,69 +508,62 @@ bool CAI::MoveTowardsWaypoint(int Dist) m_Move = 0; return true; } - + if (m_LastPos.x < m_WaypointPos.x) m_Move = 1; - + if (m_LastPos.x > m_WaypointPos.x) m_Move = -1; - + return false; } - void CAI::ReceiveDamage(int CID, int Dmg) { if (CID >= 0 && CID < 16) { m_aAnger[CID] += Dmg; m_aAnger[CID] *= 1.1f; - + m_aAttachment[CID] *= 0.9f; - - if (frandom()*25 < 2 && m_EnemiesInSight > 1) - m_PanicTick = GameServer()->Server()->Tick() + GameServer()->Server()->TickSpeed()*(2+frandom()*2); + + if (frandom() * 25 < 2 && m_EnemiesInSight > 1) + m_PanicTick = GameServer()->Server()->Tick() + GameServer()->Server()->TickSpeed() * (2 + frandom() * 2); } else { // world damage for (int i = 0; i < 16; i++) { - m_aAnger[i] += Dmg/2; + m_aAnger[i] += Dmg / 2; m_aAnger[i] *= 1.1f; } } } - - - void CAI::HandleEmotions() { m_TotalAnger = 0.0f; - + for (int i = 0; i < 16; i++) { m_aAnger[i] *= 0.97f; m_aAttachment[i] *= 0.97f; - + m_TotalAnger += m_aAnger[i]; } - + if (m_TotalAnger > 35.0f) Player()->GetCharacter()->SetEmoteFor(EMOTE_ANGRY, 40, 40, false); - + if (m_PanicTick > GameServer()->Server()->Tick()) Panic(); } - - - void CAI::ClearEmotions() { m_PanicTick = 0; - + for (int i = 0; i < 16; i++) { m_aAnger[i] = 0.0f; @@ -629,77 +571,79 @@ void CAI::ClearEmotions() } } - - int CAI::WeaponShootRange() { int Weapon = Player()->GetCharacter()->m_ActiveCustomWeapon; int Range = 40; - + if (Weapon >= 0 && Weapon < NUM_CUSTOMWEAPONS) Range = BotAttackRange[Weapon]; - + return Range; } - - void CAI::ReactToPlayer() { // angry face - //if (m_PlayerSpotCount > 20) + // if (m_PlayerSpotCount > 20) // Player()->GetCharacter()->SetEmoteFor(EMOTE_ANGRY, 0, 1200); - + if (m_PlayerSpotCount == 20 && m_TotalAnger > 35.0f) { switch (rand() % 3) { - case 0: GameServer()->SendEmoticon(Player()->GetCID(), EMOTICON_SPLATTEE); break; - case 1: GameServer()->SendEmoticon(Player()->GetCID(), EMOTICON_EXCLAMATION); break; + case 0: + GameServer()->SendEmoticon(Player()->GetCID(), EMOTICON_SPLATTEE); + break; + case 1: + GameServer()->SendEmoticon(Player()->GetCID(), EMOTICON_EXCLAMATION); + break; default: /*__*/; } } - + if (m_PlayerSpotCount == 80) { switch (rand() % 3) { - case 0: GameServer()->SendEmoticon(Player()->GetCID(), EMOTICON_ZOMG); break; - case 1: GameServer()->SendEmoticon(Player()->GetCID(), EMOTICON_WTF); break; + case 0: + GameServer()->SendEmoticon(Player()->GetCID(), EMOTICON_ZOMG); + break; + case 1: + GameServer()->SendEmoticon(Player()->GetCID(), EMOTICON_WTF); + break; default: /*__*/; } } } - - void CAI::ShootAtClosestEnemy() { CCharacter *pClosestCharacter = NULL; int ClosestDistance = 0; - + // FIRST_BOT_ID, fix for (int i = 0; i < MAX_CLIENTS; i++) { CPlayer *pPlayer = GameServer()->m_apPlayers[i]; - if(!pPlayer) + if (!pPlayer) continue; - + if (pPlayer == Player()) continue; - + if (pPlayer->GetTeam() == Player()->GetTeam() && GameServer()->m_pController->IsTeamplay()) continue; CCharacter *pCharacter = pPlayer->GetCharacter(); if (!pCharacter) continue; - + if (!pCharacter->IsAlive()) continue; - + int Distance = distance(pCharacter->m_Pos, m_LastPos); - if (Distance < 800 && + if (Distance < 800 && !GameServer()->Collision()->FastIntersectLine(pCharacter->m_Pos, m_LastPos)) { if (!pClosestCharacter || Distance < ClosestDistance) @@ -711,41 +655,39 @@ void CAI::ShootAtClosestEnemy() } } } - - if (pClosestCharacter && ClosestDistance < WeaponShootRange()*1.2f) + + if (pClosestCharacter && ClosestDistance < WeaponShootRange() * 1.2f) { - vec2 AttackDirection = vec2(m_PlayerDirection.x+ClosestDistance*(frandom()*0.3f-frandom()*0.3f), m_PlayerDirection.y+ClosestDistance*(frandom()*0.3f-frandom()*0.3f)); - + vec2 AttackDirection = vec2(m_PlayerDirection.x + ClosestDistance * (frandom() * 0.3f - frandom() * 0.3f), m_PlayerDirection.y + ClosestDistance * (frandom() * 0.3f - frandom() * 0.3f)); + if (m_HookTick < GameServer()->Server()->Tick() - 20) m_Direction = AttackDirection; - + // shooting part if (m_AttackTimer++ > g_Config.m_SvBotReactTime) { if (ClosestDistance < WeaponShootRange() && abs(atan2(m_Direction.x, m_Direction.y) - atan2(AttackDirection.x, AttackDirection.y)) < PI / 4.0f) { m_Attack = 1; - if (frandom()*30 < 2 && !Player()->GetCharacter()->UsingMeleeWeapon()) - m_DontMoveTick = GameServer()->Server()->Tick() + GameServer()->Server()->TickSpeed()*(1+frandom()); + if (frandom() * 30 < 2 && !Player()->GetCharacter()->UsingMeleeWeapon()) + m_DontMoveTick = GameServer()->Server()->Tick() + GameServer()->Server()->TickSpeed() * (1 + frandom()); } } } - + Player()->GetCharacter()->AutoWeaponChange(); } - void CAI::RandomlyStopShooting() { - if (frandom()*20 < 4 && m_Attack == 1) + if (frandom() * 20 < 4 && m_Attack == 1) { m_Attack = 0; - - m_AttackTimer = g_Config.m_SvBotReactTime*0.5f; + + m_AttackTimer = g_Config.m_SvBotReactTime * 0.5f; } } - bool CAI::SeekRandomEnemy() { if (m_pTargetPlayer && m_pTargetPlayer->GetCharacter() && m_pTargetPlayer->GetCharacter()->IsAlive()) @@ -755,66 +697,64 @@ bool CAI::SeekRandomEnemy() m_PlayerDistance = distance(m_pTargetPlayer->GetCharacter()->m_Pos, m_Pos); return true; } - + int i = 0; while (i++ < 9) { - int p = rand()%MAX_CLIENTS; - + int p = rand() % MAX_CLIENTS; + CPlayer *pPlayer = GameServer()->m_apPlayers[p]; - if(!pPlayer) + if (!pPlayer) continue; - + if (pPlayer == Player()) continue; - + if (pPlayer->GetTeam() == Player()->GetTeam() && GameServer()->m_pController->IsTeamplay()) continue; CCharacter *pCharacter = pPlayer->GetCharacter(); if (!pCharacter) continue; - + if (!pCharacter->IsAlive()) continue; - + m_pTargetPlayer = pPlayer; m_PlayerDirection = m_pTargetPlayer->GetCharacter()->m_Pos - m_Pos; m_PlayerPos = m_pTargetPlayer->GetCharacter()->m_Pos; m_PlayerDistance = distance(m_pTargetPlayer->GetCharacter()->m_Pos, m_Pos); return true; } - + return false; } - - bool CAI::SeekClosestFriend() { CCharacter *pClosestCharacter = NULL; int ClosestDistance = 0; - + // FIRST_BOT_ID, fix for (int i = 0; i < MAX_CLIENTS; i++) { CPlayer *pPlayer = GameServer()->m_apPlayers[i]; - if(!pPlayer) + if (!pPlayer) continue; - + if (pPlayer == Player()) continue; - + if (pPlayer->GetTeam() != Player()->GetTeam() || !GameServer()->m_pController->IsTeamplay()) continue; CCharacter *pCharacter = pPlayer->GetCharacter(); if (!pCharacter) continue; - + if (!pCharacter->IsAlive()) continue; - + int Distance = distance(pCharacter->m_Pos, m_LastPos); if ((!pClosestCharacter || Distance < ClosestDistance)) { @@ -824,7 +764,7 @@ bool CAI::SeekClosestFriend() m_PlayerPos = pCharacter->m_Pos; } } - + if (pClosestCharacter) { m_PlayerDistance = ClosestDistance; @@ -834,32 +774,31 @@ bool CAI::SeekClosestFriend() return false; } - bool CAI::SeekClosestEnemy() { CCharacter *pClosestCharacter = NULL; int ClosestDistance = 0; - + // FIRST_BOT_ID, fix for (int i = 0; i < MAX_CLIENTS; i++) { CPlayer *pPlayer = GameServer()->m_apPlayers[i]; - if(!pPlayer) + if (!pPlayer) continue; - + if (pPlayer == Player()) continue; - + if (pPlayer->GetTeam() == Player()->GetTeam() && GameServer()->m_pController->IsTeamplay()) continue; CCharacter *pCharacter = pPlayer->GetCharacter(); if (!pCharacter) continue; - + if (!pCharacter->IsAlive()) continue; - + int Distance = distance(pCharacter->m_Pos, m_LastPos); if ((!pClosestCharacter || Distance < ClosestDistance)) { @@ -869,7 +808,7 @@ bool CAI::SeekClosestEnemy() m_PlayerPos = pCharacter->m_Pos; } } - + if (pClosestCharacter) { m_PlayerDistance = ClosestDistance; @@ -879,41 +818,40 @@ bool CAI::SeekClosestEnemy() return false; } - bool CAI::SeekClosestEnemyInSight() { CCharacter *pClosestCharacter = NULL; int ClosestDistance = 0; - + m_EnemiesInSight = 0; - + // FIRST_BOT_ID, fix for (int i = 0; i < MAX_CLIENTS; i++) { CPlayer *pPlayer = GameServer()->m_apPlayers[i]; - if(!pPlayer) + if (!pPlayer) continue; - + if (pPlayer == Player()) continue; - + if (pPlayer->GetTeam() == Player()->GetTeam() && GameServer()->m_pController->IsTeamplay()) continue; CCharacter *pCharacter = pPlayer->GetCharacter(); if (!pCharacter) continue; - + if (!pCharacter->IsAlive()) continue; - + int Distance = distance(pCharacter->m_Pos, m_LastPos); - if (Distance < 900 && + if (Distance < 900 && !GameServer()->Collision()->FastIntersectLine(pCharacter->m_Pos, m_LastPos)) - //!GameServer()->Collision()->IntersectLine(pCharacter->m_Pos, m_LastPos, NULL, NULL)) + //! GameServer()->Collision()->IntersectLine(pCharacter->m_Pos, m_LastPos, NULL, NULL)) { m_EnemiesInSight++; - + if (!pClosestCharacter || Distance < ClosestDistance) { pClosestCharacter = pCharacter; @@ -923,10 +861,10 @@ bool CAI::SeekClosestEnemyInSight() } } } - + if (m_EnemiesInSight == 0) m_DontMoveTick = 0; - + if (pClosestCharacter) { m_PlayerSpotCount++; @@ -938,68 +876,63 @@ bool CAI::SeekClosestEnemyInSight() return false; } - - - - void CAI::Tick() { m_NextReaction--; - + // character check & position update if (m_pPlayer->GetCharacter()) m_Pos = m_pPlayer->GetCharacter()->m_Pos; else return; - - + // skip if sleeping or stunned if (m_Sleep > 0 || m_Stun > 0) { if (m_Sleep > 0) m_Sleep--; - + if (m_Stun > 0) m_Stun--; - + m_Move = 0; m_Jump = 0; m_Hook = 0; m_Attack = 0; m_InputChanged = true; - + return; } - + HandleEmotions(); - + // stupid AI can't even react to events every frame if (m_NextReaction <= 0) { m_NextReaction = m_ReactionTime; - + DoBehavior(); - + if (m_DontMoveTick > GameServer()->Server()->Tick()) { m_Move = 0; m_Hook = m_LastHook; m_Jump = 0; } - + if (m_pPlayer->GetCharacter()) m_LastPos = m_pPlayer->GetCharacter()->m_Pos; m_LastMove = m_Move; m_LastJump = m_Jump; m_LastAttack = m_Attack; m_LastHook = m_Hook; - + if (m_OldTargetPos != m_TargetPos) { m_OldTargetPos = m_TargetPos; m_WaypointUpdateNeeded = true; } - + m_InputChanged = true; } else @@ -1007,12 +940,7 @@ void CAI::Tick() m_Attack = 0; } m_InputChanged = true; - - m_DisplayDirection.x += (m_Direction.x - m_DisplayDirection.x) / 4.0f; m_DisplayDirection.y += (m_Direction.y - m_DisplayDirection.y) / 4.0f; } - - - diff --git a/src/game/server/ai/csbb_ai.cpp b/src/game/server/ai/csbb_ai.cpp index de48b4d..0715c6a 100644 --- a/src/game/server/ai/csbb_ai.cpp +++ b/src/game/server/ai/csbb_ai.cpp @@ -9,23 +9,20 @@ #include "csbb_ai.h" - CAIcsbb::CAIcsbb(CGameContext *pGameServer, CPlayer *pPlayer) -: CAI(pGameServer, pPlayer) + : CAI(pGameServer, pPlayer) { m_SkipMoveUpdate = 0; } - void CAIcsbb::OnCharacterSpawn(CCharacter *pChr) { CAI::OnCharacterSpawn(pChr); - + m_WaypointDir = vec2(0, 0); - + int Round = GameServer()->m_pController->GetRound(); - if (g_Config.m_SvRandomWeapons) { pChr->GiveRandomWeapon(); @@ -33,34 +30,34 @@ void CAIcsbb::OnCharacterSpawn(CCharacter *pChr) else { int Weapon = GUN_PISTOL; - - if (frandom()*10 < (Round+1)*3) + + if (frandom() * 10 < (Round + 1) * 3) { - if (frandom()*12 < 2) + if (frandom() * 12 < 2) Weapon = SWORD_KATANA; - else if (frandom()*12 < 2) + else if (frandom() * 12 < 2) Weapon = GUN_MAGNUM; - else if (frandom()*12 < 3) + else if (frandom() * 12 < 3) Weapon = RIFLE_ASSAULTRIFLE; - else if (frandom()*12 < 3) + else if (frandom() * 12 < 3) Weapon = GRENADE_GRENADELAUNCHER; - else if (frandom()*12 < 3) + else if (frandom() * 12 < 3) Weapon = SHOTGUN_DOUBLEBARREL; - else if (frandom()*12 < 3) + else if (frandom() * 12 < 3) Weapon = RIFLE_LIGHTNINGRIFLE; - else if (frandom()*12 < 3) + else if (frandom() * 12 < 3) Weapon = RIFLE_LASERRIFLE; else if (Round > 3) { - if (frandom()*12 < 3) + if (frandom() * 12 < 3) Weapon = SHOTGUN_COMBAT; - else if (frandom()*12 < 3) + else if (frandom() * 12 < 3) Weapon = RIFLE_STORMRIFLE; - else if (frandom()*12 < 3) + else if (frandom() * 12 < 3) Weapon = RIFLE_DOOMRAY; - else if (frandom()*12 < 3) + else if (frandom() * 12 < 3) Weapon = GRENADE_DOOMLAUNCHER; - else if (frandom()*12 < 3) + else if (frandom() * 12 < 3) Weapon = RIFLE_HEAVYRIFLE; } } @@ -68,48 +65,42 @@ void CAIcsbb::OnCharacterSpawn(CCharacter *pChr) pChr->GiveCustomWeapon(Weapon); pChr->SetCustomWeapon(Weapon); } - - //pChr->SetHealth(100); - - str_copy(pChr->GetPlayer()->m_TeeInfos.m_SkinName, "bluestripe", 64); -} + // pChr->SetHealth(100); + str_copy(pChr->GetPlayer()->m_TeeInfos.m_SkinName, "bluestripe", 64); +} void CAIcsbb::DoBehavior() { // reset jump and attack m_Jump = 0; m_Attack = 0; - + // using this later CBomb *Bomb = GameServer()->m_pController->GetBomb(); - HeadToMovingDirection(); SeekClosestEnemyInSight(); - + // if we see a player if (m_EnemiesInSight > 0) { ShootAtClosestEnemy(); ReactToPlayer(); - + if (m_EnemiesInSight > 1) { // distance to the player if (m_PlayerPos.x < m_Pos.x) - m_TargetPos.x = m_PlayerPos.x + WeaponShootRange()/2*(0.5f+frandom()*1.0f); + m_TargetPos.x = m_PlayerPos.x + WeaponShootRange() / 2 * (0.5f + frandom() * 1.0f); else - m_TargetPos.x = m_PlayerPos.x - WeaponShootRange()/2*(0.5f+frandom()*1.0f); + m_TargetPos.x = m_PlayerPos.x - WeaponShootRange() / 2 * (0.5f + frandom() * 1.0f); } } else m_AttackTimer = 0; - - int f = 400+m_EnemiesInSight*100; - - + // main logic if (Player()->GetTeam() == GameServer()->m_pController->GetDefendingTeam()) { @@ -134,7 +125,7 @@ void CAIcsbb::DoBehavior() if (SeekBombArea()) { // ...unless we're near it or didn't find it - if (!m_WayFound || (distance(m_Pos, m_TargetPos) < g_Config.m_SvBaseCaptureDistance/2)) + if (!m_WayFound || (distance(m_Pos, m_TargetPos) < g_Config.m_SvBaseCaptureDistance / 2)) { if (SeekClosestEnemy()) m_TargetPos = m_PlayerPos; @@ -146,56 +137,52 @@ void CAIcsbb::DoBehavior() m_TargetPos = m_PlayerPos; } } - /* UpdateWaypoint(); MoveTowardsWaypoint(10); */ - + if (UpdateWaypoint()) { MoveTowardsWaypoint(20); HookMove(); AirJump(); - + // jump if waypoint is above us - if (abs(m_WaypointPos.x - m_Pos.x) < 60 && m_WaypointPos.y < m_Pos.y - 100 && frandom()*20 < 4) + if (abs(m_WaypointPos.x - m_Pos.x) < 60 && m_WaypointPos.y < m_Pos.y - 100 && frandom() * 20 < 4) m_Jump = 1; } else { m_Hook = 0; } - - + DoJumping(); Unstuck(); - - - + // go plant the bomb if (Player()->GetTeam() != GameServer()->m_pController->GetDefendingTeam()) { if (Bomb && (Bomb->m_Status == BOMB_CARRYING || Bomb->m_Status == BOMB_PLANTING)) { CFlag *BombArea = GameServer()->m_pController->GetClosestBase(m_Pos); - - if (BombArea && abs(BombArea->m_Pos.x-m_Pos.x) < 100 && abs(BombArea->m_Pos.y-m_Pos.y-50) < 100) + + if (BombArea && abs(BombArea->m_Pos.x - m_Pos.x) < 100 && abs(BombArea->m_Pos.y - m_Pos.y - 50) < 100) { m_Move = 0; m_Jump = 0; m_Hook = 0; } } - } - + } + // don't move if defusing the bomb if (Player()->GetTeam() == GameServer()->m_pController->GetDefendingTeam()) { if (Bomb && Bomb->m_Status == BOMB_PLANTED) { - if (abs(Bomb->m_Pos.x-m_Pos.x) < 100 && abs(Bomb->m_Pos.y-m_Pos.y-50) < 100) + if (abs(Bomb->m_Pos.x - m_Pos.x) < 100 && abs(Bomb->m_Pos.y - m_Pos.y - 50) < 100) { m_Move = 0; m_Jump = 0; @@ -203,21 +190,20 @@ void CAIcsbb::DoBehavior() } } } - + // don't move if defusing the bomb if (GameServer()->m_pController->GetRoundStatus() == 0) // CSBB_NEWBASE { - if (abs(m_TargetPos.x-m_Pos.x) < 200 && abs(m_TargetPos.y-m_Pos.y-50) < 100 && frandom()*10 < 8) + if (abs(m_TargetPos.x - m_Pos.x) < 200 && abs(m_TargetPos.y - m_Pos.y - 50) < 100 && frandom() * 10 < 8) { m_Move = 0; m_Jump = 0; m_Hook = 0; } } - + RandomlyStopShooting(); - + // next reaction in - m_ReactionTime = 2 + frandom()*4; - + m_ReactionTime = 2 + frandom() * 4; } \ No newline at end of file diff --git a/src/game/server/ai/dom_ai.cpp b/src/game/server/ai/dom_ai.cpp index c90f938..883b208 100644 --- a/src/game/server/ai/dom_ai.cpp +++ b/src/game/server/ai/dom_ai.cpp @@ -8,41 +8,34 @@ #include "dom_ai.h" - CAIdom::CAIdom(CGameContext *pGameServer, CPlayer *pPlayer) -: CAI(pGameServer, pPlayer) + : CAI(pGameServer, pPlayer) { m_SkipMoveUpdate = 0; pPlayer->SetRandomSkin(); m_TargetBase = NULL; } - void CAIdom::OnCharacterSpawn(CCharacter *pChr) { CAI::OnCharacterSpawn(pChr); - + m_WaypointDir = vec2(0, 0); - + if (g_Config.m_SvRandomWeapons) pChr->GiveRandomWeapon(); } - void CAIdom::DoBehavior() { // reset jump and attack m_Jump = 0; m_Attack = 0; - - - HeadToMovingDirection(); SeekClosestEnemyInSight(); - // if we see a player if (m_EnemiesInSight > 0) { @@ -52,14 +45,12 @@ void CAIdom::DoBehavior() else m_AttackTimer = 0; - int f = 400+m_EnemiesInSight*100; - // if we got a target base if (m_TargetBase) { m_TargetPos = m_TargetBase->m_Pos; - - if (m_TargetBase->m_CaptureTeam == Player()->GetTeam() && GameServer()->m_pController->Defenders(m_TargetBase) > 1 && frandom()*50 < 2) + + if (m_TargetBase->m_CaptureTeam == Player()->GetTeam() && GameServer()->m_pController->Defenders(m_TargetBase) > 1 && frandom() * 50 < 2) { // seek undefended base CFlag *Base = GameServer()->m_pController->GetUndefendedBase(Player()->GetTeam()); @@ -73,18 +64,18 @@ void CAIdom::DoBehavior() else { m_TargetBase = GameServer()->m_pController->GetRandomBase(); - + /* if (SeekClosestFriend()) { m_TargetPos = m_PlayerPos; - + if (m_PlayerDistance < f) { if (SeekRandomEnemy()) { m_TargetPos = m_PlayerPos; - + if (m_EnemiesInSight > 1) { // distance to the player @@ -98,29 +89,27 @@ void CAIdom::DoBehavior() } */ } - + if (UpdateWaypoint()) { MoveTowardsWaypoint(10); HookMove(); AirJump(); - + // jump if waypoint is above us - if (abs(m_WaypointPos.x - m_Pos.x) < 60 && m_WaypointPos.y < m_Pos.y - 100 && frandom()*20 < 4) + if (abs(m_WaypointPos.x - m_Pos.x) < 60 && m_WaypointPos.y < m_Pos.y - 100 && frandom() * 20 < 4) m_Jump = 1; } else { m_Hook = 0; } - - + DoJumping(); Unstuck(); RandomlyStopShooting(); - + // next reaction in - m_ReactionTime = 2 + frandom()*4; - + m_ReactionTime = 2 + frandom() * 4; } \ No newline at end of file diff --git a/src/game/server/gamecontext.cpp b/src/game/server/gamecontext.cpp index 0922564..9a7a4f0 100644 --- a/src/game/server/gamecontext.cpp +++ b/src/game/server/gamecontext.cpp @@ -10,7 +10,7 @@ #include "gamecontext.h" #include #include -#include +#include #include "gamemodes/dm.h" #include "gamemodes/tdm.h" #include "gamemodes/ctf.h" @@ -24,16 +24,11 @@ #include #include - - - -const char *aClassName[NUM_CLASSES] = -{ - "Soldier", - "Medic", - "Technician" -}; - +const char *aClassName[NUM_CLASSES] = + { + "Soldier", + "Medic", + "Technician"}; enum { @@ -46,25 +41,25 @@ void CGameContext::Construct(int Resetting) m_Resetting = 0; m_pServer = 0; - for(int i = 0; i < MAX_CLIENTS; i++) + for (int i = 0; i < MAX_CLIENTS; i++) m_apPlayers[i] = 0; m_BroadcastLockTick = 0; - + m_pController = 0; m_VoteCloseTime = 0; m_pVoteOptionFirst = 0; m_pVoteOptionLast = 0; m_NumVoteOptions = 0; m_LockTeams = 0; - + m_aMostInterestingPlayer[0] = -1; m_aMostInterestingPlayer[1] = -1; - + m_ShowWaypoints = false; m_FreezeCharacters = false; - if(Resetting==NO_RESET) + if (Resetting == NO_RESET) m_pVoteOptionHeap = new CHeap(); } @@ -80,9 +75,9 @@ CGameContext::CGameContext() CGameContext::~CGameContext() { - for(int i = 0; i < MAX_CLIENTS; i++) + for (int i = 0; i < MAX_CLIENTS; i++) delete m_apPlayers[i]; - if(!m_Resetting) + if (!m_Resetting) delete m_pVoteOptionHeap; } @@ -106,10 +101,9 @@ void CGameContext::Clear() m_Tuning = Tuning; } - class CCharacter *CGameContext::GetPlayerChar(int ClientID) { - if(ClientID < 0 || ClientID >= MAX_CLIENTS || !m_apPlayers[ClientID]) + if (ClientID < 0 || ClientID >= MAX_CLIENTS || !m_apPlayers[ClientID]) return 0; return m_apPlayers[ClientID]->GetCharacter(); } @@ -119,15 +113,15 @@ void CGameContext::CreateDamageInd(vec2 Pos, float Angle, int Amount) int a = Amount / 4; if (a == 0) a = 1; - - for(int i = 0; i < a; i++) + + for (int i = 0; i < a; i++) { CNetEvent_DamageInd *pEvent = (CNetEvent_DamageInd *)m_Events.Create(NETEVENTTYPE_DAMAGEIND, sizeof(CNetEvent_DamageInd)); - if(pEvent) + if (pEvent) { pEvent->m_X = (int)Pos.x; pEvent->m_Y = (int)Pos.y; - pEvent->m_Angle = (int)(Angle*256.0f + frandom()*200 - frandom()*200); + pEvent->m_Angle = (int)(Angle * 256.0f + frandom() * 200 - frandom() * 200); } } } @@ -136,39 +130,34 @@ void CGameContext::CreateHammerHit(vec2 Pos) { // create the event CNetEvent_HammerHit *pEvent = (CNetEvent_HammerHit *)m_Events.Create(NETEVENTTYPE_HAMMERHIT, sizeof(CNetEvent_HammerHit)); - if(pEvent) + if (pEvent) { pEvent->m_X = (int)Pos.x; pEvent->m_Y = (int)Pos.y; } } - void CGameContext::GenerateArrows() { m_pArrow = new CArrow(&m_World); } - - bool CGameContext::GotAbility(int ClientID, int Ability) { if (ClientID < 0 || ClientID >= MAX_CLIENTS) return false; - + if (m_apPlayers[ClientID] && m_apPlayers[ClientID]->GotAbility(Ability)) return true; - + return false; } - - void CGameContext::CreateExplosion(vec2 Pos, int Owner, int Weapon, bool NoDamage, bool Superdamage) { // create the event CNetEvent_Explosion *pEvent = (CNetEvent_Explosion *)m_Events.Create(NETEVENTTYPE_EXPLOSION, sizeof(CNetEvent_Explosion)); - if(pEvent) + if (pEvent) { pEvent->m_X = (int)Pos.x; pEvent->m_Y = (int)Pos.y; @@ -180,31 +169,31 @@ void CGameContext::CreateExplosion(vec2 Pos, int Owner, int Weapon, bool NoDamag CCharacter *apEnts[MAX_CLIENTS]; float Radius = 135.0f; float InnerRadius = 48.0f; - int Num = m_World.FindEntities(Pos, Radius, (CEntity**)apEnts, MAX_CLIENTS, CGameWorld::ENTTYPE_CHARACTER); - for(int i = 0; i < Num; i++) + int Num = m_World.FindEntities(Pos, Radius, (CEntity **)apEnts, MAX_CLIENTS, CGameWorld::ENTTYPE_CHARACTER); + for (int i = 0; i < Num; i++) { vec2 Diff = apEnts[i]->m_Pos - Pos; - vec2 ForceDir(0,1); + vec2 ForceDir(0, 1); float l = length(Diff); - if(l) + if (l) ForceDir = normalize(Diff); - l = 1-clamp((l-InnerRadius)/(Radius-InnerRadius), 0.0f, 1.0f); + l = 1 - clamp((l - InnerRadius) / (Radius - InnerRadius), 0.0f, 1.0f); float Dmg = 14 * l; - + if (Superdamage) Dmg *= 5; - + if (apEnts[i]->GetPlayer()->GotAbility(ANTIEXPLOSIONARMOR)) Dmg -= 1.0f; - + if (Owner > 0 && Owner < MAX_CLIENTS) { if (m_apPlayers[Owner] && m_apPlayers[Owner]->GotAbility(EXPLOSION_DAMAGE1)) Dmg += 1.0f; } - - if((int)Dmg && Dmg > 0.0f) - apEnts[i]->TakeDamage(ForceDir*Dmg*0.9f, (int)Dmg, Owner, Weapon); + + if ((int)Dmg && Dmg > 0.0f) + apEnts[i]->TakeDamage(ForceDir * Dmg * 0.9f, (int)Dmg, Owner, Weapon); } } } @@ -225,7 +214,7 @@ void CGameContext::CreatePlayerSpawn(vec2 Pos) { // create the event CNetEvent_Spawn *ev = (CNetEvent_Spawn *)m_Events.Create(NETEVENTTYPE_SPAWN, sizeof(CNetEvent_Spawn)); - if(ev) + if (ev) { ev->m_X = (int)Pos.x; ev->m_Y = (int)Pos.y; @@ -236,7 +225,7 @@ void CGameContext::CreateDeath(vec2 Pos, int ClientID) { // create the event CNetEvent_Death *pEvent = (CNetEvent_Death *)m_Events.Create(NETEVENTTYPE_DEATH, sizeof(CNetEvent_Death)); - if(pEvent) + if (pEvent) { pEvent->m_X = (int)Pos.x; pEvent->m_Y = (int)Pos.y; @@ -251,7 +240,7 @@ void CGameContext::CreateSound(vec2 Pos, int Sound, int Mask) // create a sound CNetEvent_SoundWorld *pEvent = (CNetEvent_SoundWorld *)m_Events.Create(NETEVENTTYPE_SOUNDWORLD, sizeof(CNetEvent_SoundWorld), Mask); - if(pEvent) + if (pEvent) { pEvent->m_X = (int)Pos.x; pEvent->m_Y = (int)Pos.y; @@ -266,31 +255,25 @@ void CGameContext::CreateSoundGlobal(int Sound, int Target) CNetMsg_Sv_SoundGlobal Msg; Msg.m_SoundID = Sound; - if(Target == -2) + if (Target == -2) Server()->SendPackMsg(&Msg, MSGFLAG_NOSEND, -1); else { int Flag = MSGFLAG_VITAL; - if(Target != -1) + if (Target != -1) Flag |= MSGFLAG_NORECORD; Server()->SendPackMsg(&Msg, Flag, Target); } } - - - - bool CGameContext::IsBot(int ClientID) { - if(m_apPlayers[ClientID] && m_apPlayers[ClientID]->m_pAI) + if (m_apPlayers[ClientID] && m_apPlayers[ClientID]->m_pAI) return true; - + return false; } - - void CGameContext::SendChatTarget(int To, const char *pText) { // skip sending to bots @@ -303,17 +286,16 @@ void CGameContext::SendChatTarget(int To, const char *pText) Server()->SendPackMsg(&Msg, MSGFLAG_VITAL, To); } - void CGameContext::SendChat(int ChatterClientID, int Team, const char *pText) { char aBuf[256]; - if(ChatterClientID >= 0 && ChatterClientID < MAX_CLIENTS) + if (ChatterClientID >= 0 && ChatterClientID < MAX_CLIENTS) str_format(aBuf, sizeof(aBuf), "%d:%d:%s: %s", ChatterClientID, Team, Server()->ClientName(ChatterClientID), pText); else str_format(aBuf, sizeof(aBuf), "*** %s", pText); - Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, Team!=CHAT_ALL?"teamchat":"chat", aBuf); + Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, Team != CHAT_ALL ? "teamchat" : "chat", aBuf); - if(Team == CHAT_ALL) + if (Team == CHAT_ALL) { CNetMsg_Sv_Chat Msg; Msg.m_Team = 0; @@ -329,13 +311,13 @@ void CGameContext::SendChat(int ChatterClientID, int Team, const char *pText) Msg.m_pMessage = pText; // pack one for the recording only - Server()->SendPackMsg(&Msg, MSGFLAG_VITAL|MSGFLAG_NOSEND, -1); + Server()->SendPackMsg(&Msg, MSGFLAG_VITAL | MSGFLAG_NOSEND, -1); // send to the clients - for(int i = 0; i < MAX_CLIENTS; i++) + for (int i = 0; i < MAX_CLIENTS; i++) { - if(m_apPlayers[i] && !IsBot(i) && m_apPlayers[i]->GetTeam() == Team) - Server()->SendPackMsg(&Msg, MSGFLAG_VITAL|MSGFLAG_NORECORD, i); + if (m_apPlayers[i] && !IsBot(i) && m_apPlayers[i]->GetTeam() == Team) + Server()->SendPackMsg(&Msg, MSGFLAG_VITAL | MSGFLAG_NORECORD, i); } } } @@ -355,28 +337,27 @@ void CGameContext::SendWeaponPickup(int ClientID, int Weapon) Server()->SendPackMsg(&Msg, MSGFLAG_VITAL, ClientID); } - void CGameContext::SendBroadcast(const char *pText, int ClientID, bool Lock) { CNetMsg_Sv_Broadcast Msg; Msg.m_pMessage = pText; Server()->SendPackMsg(&Msg, MSGFLAG_VITAL, ClientID); if (Lock) - m_BroadcastLockTick = Server()->Tick() + g_Config.m_SvBroadcastLock*Server()->TickSpeed(); + m_BroadcastLockTick = Server()->Tick() + g_Config.m_SvBroadcastLock * Server()->TickSpeed(); } // void CGameContext::StartVote(const char *pDesc, const char *pCommand, const char *pReason) { // check if a vote is already running - if(m_VoteCloseTime) + if (m_VoteCloseTime) return; - + // reset votes m_VoteEnforce = VOTE_ENFORCE_UNKNOWN; - for(int i = 0; i < MAX_CLIENTS; i++) + for (int i = 0; i < MAX_CLIENTS; i++) { - if(m_apPlayers[i]) + if (m_apPlayers[i]) { m_apPlayers[i]->m_Vote = 0; m_apPlayers[i]->m_VotePos = 0; @@ -384,7 +365,7 @@ void CGameContext::StartVote(const char *pDesc, const char *pCommand, const char } // start vote - m_VoteCloseTime = time_get() + time_freq()*25; + m_VoteCloseTime = time_get() + time_freq() * 25; str_copy(m_aVoteDescription, pDesc, sizeof(m_aVoteDescription)); str_copy(m_aVoteCommand, pCommand, sizeof(m_aVoteCommand)); str_copy(m_aVoteReason, pReason, sizeof(m_aVoteReason)); @@ -392,7 +373,6 @@ void CGameContext::StartVote(const char *pDesc, const char *pCommand, const char m_VoteUpdate = true; } - void CGameContext::EndVote() { m_VoteCloseTime = 0; @@ -402,9 +382,9 @@ void CGameContext::EndVote() void CGameContext::SendVoteSet(int ClientID) { CNetMsg_Sv_VoteSet Msg; - if(m_VoteCloseTime) + if (m_VoteCloseTime) { - Msg.m_Timeout = (m_VoteCloseTime-time_get())/time_freq(); + Msg.m_Timeout = (m_VoteCloseTime - time_get()) / time_freq(); Msg.m_pDescription = m_aVoteDescription; Msg.m_pReason = m_aVoteReason; } @@ -423,32 +403,30 @@ void CGameContext::SendVoteStatus(int ClientID, int Total, int Yes, int No) Msg.m_Total = Total; Msg.m_Yes = Yes; Msg.m_No = No; - Msg.m_Pass = Total - (Yes+No); + Msg.m_Pass = Total - (Yes + No); Server()->SendPackMsg(&Msg, MSGFLAG_VITAL, ClientID); - } void CGameContext::AbortVoteKickOnDisconnect(int ClientID) { - if(m_VoteCloseTime && ((!str_comp_num(m_aVoteCommand, "kick ", 5) && str_toint(&m_aVoteCommand[5]) == ClientID) || - (!str_comp_num(m_aVoteCommand, "set_team ", 9) && str_toint(&m_aVoteCommand[9]) == ClientID))) + if (m_VoteCloseTime && ((!str_comp_num(m_aVoteCommand, "kick ", 5) && str_toint(&m_aVoteCommand[5]) == ClientID) || + (!str_comp_num(m_aVoteCommand, "set_team ", 9) && str_toint(&m_aVoteCommand[9]) == ClientID))) m_VoteCloseTime = -1; } - void CGameContext::CheckPureTuning() { // might not be created yet during start up - if(!m_pController) + if (!m_pController) return; - if( str_comp(m_pController->m_pGameType, "DM")==0 || - str_comp(m_pController->m_pGameType, "TDM")==0 || - str_comp(m_pController->m_pGameType, "CTF")==0) + if (str_comp(m_pController->m_pGameType, "DM") == 0 || + str_comp(m_pController->m_pGameType, "TDM") == 0 || + str_comp(m_pController->m_pGameType, "CTF") == 0) { CTuningParams p; - if(mem_comp(&p, &m_Tuning, sizeof(p)) != 0) + if (mem_comp(&p, &m_Tuning, sizeof(p)) != 0) { Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", "resetting tuning due to pure server"); m_Tuning = p; @@ -462,31 +440,30 @@ void CGameContext::SendTuningParams(int ClientID) CMsgPacker Msg(NETMSGTYPE_SV_TUNEPARAMS); int *pParams = (int *)&m_Tuning; - for(unsigned i = 0; i < sizeof(m_Tuning)/sizeof(int); i++) + for (unsigned i = 0; i < sizeof(m_Tuning) / sizeof(int); i++) Msg.AddInt(pParams[i]); Server()->SendMsg(&Msg, MSGFLAG_VITAL, ClientID); } void CGameContext::SwapTeams() { - if(!m_pController->IsTeamplay()) + if (!m_pController->IsTeamplay()) return; - + SendChat(-1, CGameContext::CHAT_ALL, "Teams were swapped"); - for(int i = 0; i < MAX_CLIENTS; ++i) + for (int i = 0; i < MAX_CLIENTS; ++i) { /* if(m_apPlayers[i] && m_apPlayers[i]->m_WantedTeam != TEAM_SPECTATORS) m_apPlayers[i]->SetWantedTeam(m_apPlayers[i]->m_WantedTeam^1, false); //m_apPlayers[i]->SetTeam(m_apPlayers[i]->GetTeam()^1, false); */ - if(m_apPlayers[i] && m_apPlayers[i]->GetTeam() != TEAM_SPECTATORS) + if (m_apPlayers[i] && m_apPlayers[i]->GetTeam() != TEAM_SPECTATORS) { - int t = m_apPlayers[i]->GetTeam()^1; + int t = m_apPlayers[i]->GetTeam() ^ 1; m_apPlayers[i]->SetTeam(t, false); m_apPlayers[i]->SetWantedTeam(t, false); - } } @@ -495,9 +472,9 @@ void CGameContext::SwapTeams() void CGameContext::UpdateSpectators() { - + bool Found[2] = {false, false}; - + // check validity for (int i = 0; i < 2; i++) { @@ -517,39 +494,36 @@ void CGameContext::UpdateSpectators() } } - // find the most interesting player of both teams - for(int i = 0; i < MAX_CLIENTS; i++) + for (int i = 0; i < MAX_CLIENTS; i++) { // player and character exists if (m_apPlayers[i] && m_apPlayers[i]->m_EnableAutoSpectating && m_apPlayers[i]->GetCharacter() && m_apPlayers[i]->GetCharacter()->IsAlive()) { int Team = m_apPlayers[i]->GetTeam(); - + // team is correct - if(Team == TEAM_RED || Team == TEAM_BLUE) + if (Team == TEAM_RED || Team == TEAM_BLUE) { // most interesting player exists - + int Points = -1; int Player = m_aMostInterestingPlayer[Team]; - + if (Player >= 0) if (m_apPlayers[Player] && m_apPlayers[Player]->GetCharacter()) Points = m_apPlayers[Player]->m_InterestPoints; - - + if (m_apPlayers[i]->m_InterestPoints > Points) { // works - //char aBuf[128]; str_format(aBuf, sizeof(aBuf), "i = %d, team = %d", i, Team); - //Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "cstt", aBuf); - + // char aBuf[128]; str_format(aBuf, sizeof(aBuf), "i = %d, team = %d", i, Team); + // Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "cstt", aBuf); + m_aMostInterestingPlayer[Team] = i; - Found[Team] = true; + Found[Team] = true; } - - + /* if (m_aMostInterestingPlayer[Team] >= 0) { @@ -571,40 +545,39 @@ void CGameContext::UpdateSpectators() } } - // update the spectator views - for(int i = 0; i < MAX_CLIENTS; i++) + for (int i = 0; i < MAX_CLIENTS; i++) { - //if(m_apPlayers[i] && m_apPlayers[i]->GetTeam() == TEAM_SPECTATORS && !m_apPlayers[i]->m_IsBot) - if(m_apPlayers[i] && (m_apPlayers[i]->GetTeam() == TEAM_SPECTATORS || !m_apPlayers[i]->GetCharacter()) && !m_apPlayers[i]->m_IsBot) + // if(m_apPlayers[i] && m_apPlayers[i]->GetTeam() == TEAM_SPECTATORS && !m_apPlayers[i]->m_IsBot) + if (m_apPlayers[i] && (m_apPlayers[i]->GetTeam() == TEAM_SPECTATORS || !m_apPlayers[i]->GetCharacter()) && !m_apPlayers[i]->m_IsBot) { if (!m_apPlayers[i]->m_LastSetSpectatorMode) - m_apPlayers[i]->m_LastSetSpectatorMode = Server()->Tick() - Server()->TickSpeed()*g_Config.m_SvSpectatorUpdateTime; + m_apPlayers[i]->m_LastSetSpectatorMode = Server()->Tick() - Server()->TickSpeed() * g_Config.m_SvSpectatorUpdateTime; else { - if (m_apPlayers[i]->m_LastSetSpectatorMode+Server()->TickSpeed()*g_Config.m_SvSpectatorUpdateTime < Server()->Tick()) + if (m_apPlayers[i]->m_LastSetSpectatorMode + Server()->TickSpeed() * g_Config.m_SvSpectatorUpdateTime < Server()->Tick()) { int WantedPlayer = -1; - + /* if (!m_pController->IsTeamplay()) { if (m_apPlayers[i]->GetTeam() != TEAM_SPECTATORS) { - - - + + + } } else*/ { int Team = m_apPlayers[i]->GetTeam(); - + // get the correct player if (Team == TEAM_RED || Team == TEAM_BLUE) { WantedPlayer = m_aMostInterestingPlayer[Team]; - + // update the view if (WantedPlayer >= 0 && m_apPlayers[i]->m_SpectatorID != WantedPlayer && Found[Team]) { @@ -618,9 +591,7 @@ void CGameContext::UpdateSpectators() } } } - - - + /* CNetMsg_Cl_SetSpectatorMode *pMsg = (CNetMsg_Cl_SetSpectatorMode *)pRawMsg; @@ -636,10 +607,6 @@ void CGameContext::UpdateSpectators() */ } - - - - void CGameContext::OnTick() { // check tuning @@ -649,12 +616,12 @@ void CGameContext::OnTick() m_World.m_Core.m_Tuning = m_Tuning; m_World.Tick(); - //if(world.paused) // make sure that the game object always updates + // if(world.paused) // make sure that the game object always updates m_pController->Tick(); - for(int i = 0; i < MAX_CLIENTS; i++) + for (int i = 0; i < MAX_CLIENTS; i++) { - if(m_apPlayers[i]) + if (m_apPlayers[i]) { m_apPlayers[i]->Tick(); m_apPlayers[i]->PostTick(); @@ -662,10 +629,10 @@ void CGameContext::OnTick() } // update voting - if(m_VoteCloseTime) + if (m_VoteCloseTime) { // abort the kick-vote on player-leave - if(m_VoteCloseTime == -1) + if (m_VoteCloseTime == -1) { SendChat(-1, CGameContext::CHAT_ALL, "Vote aborted"); EndVote(); @@ -673,30 +640,30 @@ void CGameContext::OnTick() else { int Total = 0, Yes = 0, No = 0; - if(m_VoteUpdate) + if (m_VoteUpdate) { // count votes char aaBuf[MAX_CLIENTS][NETADDR_MAXSTRSIZE] = {{0}}; - for(int i = 0; i < MAX_CLIENTS; i++) - if(m_apPlayers[i] && !IsBot(i)) + for (int i = 0; i < MAX_CLIENTS; i++) + if (m_apPlayers[i] && !IsBot(i)) Server()->GetClientAddr(i, aaBuf[i], NETADDR_MAXSTRSIZE); bool aVoteChecked[MAX_CLIENTS] = {0}; - for(int i = 0; i < MAX_CLIENTS; i++) + for (int i = 0; i < MAX_CLIENTS; i++) { - if(!m_apPlayers[i] || IsBot(i) || m_apPlayers[i]->GetTeam() == TEAM_SPECTATORS || aVoteChecked[i]) // don't count in votes by spectators + if (!m_apPlayers[i] || IsBot(i) || m_apPlayers[i]->GetTeam() == TEAM_SPECTATORS || aVoteChecked[i]) // don't count in votes by spectators continue; int ActVote = m_apPlayers[i]->m_Vote; int ActVotePos = m_apPlayers[i]->m_VotePos; // check for more players with the same ip (only use the vote of the one who voted first) - for(int j = i+1; j < MAX_CLIENTS; ++j) + for (int j = i + 1; j < MAX_CLIENTS; ++j) { - if(!m_apPlayers[j] || IsBot(i) || aVoteChecked[j] || str_comp(aaBuf[j], aaBuf[i])) + if (!m_apPlayers[j] || IsBot(i) || aVoteChecked[j] || str_comp(aaBuf[j], aaBuf[i])) continue; aVoteChecked[j] = true; - if(m_apPlayers[j]->m_Vote && (!ActVote || ActVotePos > m_apPlayers[j]->m_VotePos)) + if (m_apPlayers[j]->m_Vote && (!ActVote || ActVotePos > m_apPlayers[j]->m_VotePos)) { ActVote = m_apPlayers[j]->m_Vote; ActVotePos = m_apPlayers[j]->m_VotePos; @@ -704,19 +671,19 @@ void CGameContext::OnTick() } Total++; - if(ActVote > 0) + if (ActVote > 0) Yes++; - else if(ActVote < 0) + else if (ActVote < 0) No++; } - if(Yes >= Total/2+1) + if (Yes >= Total / 2 + 1) m_VoteEnforce = VOTE_ENFORCE_YES; - else if(No >= (Total+1)/2) + else if (No >= (Total + 1) / 2) m_VoteEnforce = VOTE_ENFORCE_NO; } - if(m_VoteEnforce == VOTE_ENFORCE_YES) + if (m_VoteEnforce == VOTE_ENFORCE_YES) { Server()->SetRconCID(IServer::RCON_CID_VOTE); Console()->ExecuteLine(m_aVoteCommand); @@ -724,15 +691,15 @@ void CGameContext::OnTick() EndVote(); SendChat(-1, CGameContext::CHAT_ALL, "Vote passed"); - if(m_apPlayers[m_VoteCreator]) + if (m_apPlayers[m_VoteCreator]) m_apPlayers[m_VoteCreator]->m_LastVoteCall = 0; } - else if(m_VoteEnforce == VOTE_ENFORCE_NO || time_get() > m_VoteCloseTime) + else if (m_VoteEnforce == VOTE_ENFORCE_NO || time_get() > m_VoteCloseTime) { EndVote(); SendChat(-1, CGameContext::CHAT_ALL, "Vote failed"); } - else if(m_VoteUpdate) + else if (m_VoteUpdate) { m_VoteUpdate = false; SendVoteStatus(-1, Total, Yes, No); @@ -740,41 +707,36 @@ void CGameContext::OnTick() } } - #ifdef CONF_DEBUG - if(g_Config.m_DbgDummies) + if (g_Config.m_DbgDummies) { - for(int i = 0; i < g_Config.m_DbgDummies ; i++) + for (int i = 0; i < g_Config.m_DbgDummies; i++) { CNetObj_PlayerInput Input = {0}; - Input.m_Direction = (i&1)?-1:1; - m_apPlayers[MAX_CLIENTS-i-1]->OnPredictedInput(&Input); + Input.m_Direction = (i & 1) ? -1 : 1; + m_apPlayers[MAX_CLIENTS - i - 1]->OnPredictedInput(&Input); } } #endif } - - bool CGameContext::AIInputUpdateNeeded(int ClientID) { - if(m_apPlayers[ClientID]) + if (m_apPlayers[ClientID]) return m_apPlayers[ClientID]->AIInputChanged(); - + return false; } - void CGameContext::UpdateAI() { - for(int i = 0; i < MAX_CLIENTS; i++) + for (int i = 0; i < MAX_CLIENTS; i++) { - if(m_apPlayers[i] && IsBot(i)) + if (m_apPlayers[i] && IsBot(i)) m_apPlayers[i]->AITick(); } } - /* enum InputList { @@ -782,42 +744,38 @@ enum InputList INPUT_SHOOT = 4, INPUT_JUMP = 3, INPUT_HOOK = 5 - + //1 & 2 vectors for weapon direction }; */ - void CGameContext::AIUpdateInput(int ClientID, int *Data) { - if(m_apPlayers[ClientID] && m_apPlayers[ClientID]->m_pAI) + if (m_apPlayers[ClientID] && m_apPlayers[ClientID]->m_pAI) m_apPlayers[ClientID]->m_pAI->UpdateInput(Data); } - - // Server hooks void CGameContext::AddZombie() { Server()->AddZombie(); } - void CGameContext::OnClientDirectInput(int ClientID, void *pInput) { - if(!m_World.m_Paused) + if (!m_World.m_Paused) m_apPlayers[ClientID]->OnDirectInput((CNetObj_PlayerInput *)pInput); } void CGameContext::OnClientPredictedInput(int ClientID, void *pInput) { - if(!m_World.m_Paused) + if (!m_World.m_Paused) m_apPlayers[ClientID]->OnPredictedInput((CNetObj_PlayerInput *)pInput); } void CGameContext::OnClientEnter(int ClientID) { - //world.insert_entity(&players[client_id]); + // world.insert_entity(&players[client_id]); m_apPlayers[ClientID]->Respawn(); char aBuf[512]; str_format(aBuf, sizeof(aBuf), "'%s' joined the fun", Server()->ClientName(ClientID)); @@ -834,24 +792,24 @@ void CGameContext::OnClientConnected(int ClientID, bool AI) // Check which team the player should be on const int StartTeam = g_Config.m_SvTournamentMode ? TEAM_SPECTATORS : m_pController->GetAutoTeam(ClientID); - m_apPlayers[ClientID] = new(ClientID) CPlayer(this, ClientID, StartTeam); - //players[client_id].init(client_id); - //players[client_id].client_id = client_id; + m_apPlayers[ClientID] = new (ClientID) CPlayer(this, ClientID, StartTeam); + // players[client_id].init(client_id); + // players[client_id].client_id = client_id; m_apPlayers[ClientID]->m_IsBot = AI; - + (void)m_pController->CheckTeamBalance(); #ifdef CONF_DEBUG - if(g_Config.m_DbgDummies) + if (g_Config.m_DbgDummies) { - if(ClientID >= MAX_CLIENTS-g_Config.m_DbgDummies) + if (ClientID >= MAX_CLIENTS - g_Config.m_DbgDummies) return; } #endif // send active vote - if(m_VoteCloseTime) + if (m_VoteCloseTime) SendVoteSet(ClientID); // send motd @@ -873,9 +831,9 @@ void CGameContext::OnClientDrop(int ClientID, const char *pReason) m_VoteUpdate = true; // update spectator modes - for(int i = 0; i < MAX_CLIENTS; ++i) + for (int i = 0; i < MAX_CLIENTS; ++i) { - if(m_apPlayers[i] && m_apPlayers[i]->m_SpectatorID == ClientID) + if (m_apPlayers[i] && m_apPlayers[i]->m_SpectatorID == ClientID) m_apPlayers[i]->m_SpectatorID = SPEC_FREEVIEW; } } @@ -885,9 +843,9 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) void *pRawMsg = m_NetObjHandler.SecureUnpackMsg(MsgID, pUnpacker); CPlayer *pPlayer = m_apPlayers[ClientID]; - if(!pRawMsg) + if (!pRawMsg) { - if(g_Config.m_Debug) + if (g_Config.m_Debug) { char aBuf[256]; str_format(aBuf, sizeof(aBuf), "dropped weird message '%s' (%d), failed on '%s'", m_NetObjHandler.GetMsgName(MsgID), MsgID, m_NetObjHandler.FailedMsgOn()); @@ -896,55 +854,55 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) return; } - if(Server()->ClientIngame(ClientID)) + if (Server()->ClientIngame(ClientID)) { - if(MsgID == NETMSGTYPE_CL_SAY) + if (MsgID == NETMSGTYPE_CL_SAY) { - if(g_Config.m_SvSpamprotection && pPlayer->m_LastChat && pPlayer->m_LastChat+Server()->TickSpeed() > Server()->Tick()) + if (g_Config.m_SvSpamprotection && pPlayer->m_LastChat && pPlayer->m_LastChat + Server()->TickSpeed() > Server()->Tick()) return; CNetMsg_Cl_Say *pMsg = (CNetMsg_Cl_Say *)pRawMsg; int Team = pMsg->m_Team ? pPlayer->GetTeam() : CGameContext::CHAT_ALL; - + // trim right and set maximum length to 128 utf8-characters int Length = 0; const char *p = pMsg->m_pMessage; const char *pEnd = 0; - while(*p) - { + while (*p) + { const char *pStrOld = p; int Code = str_utf8_decode(&p); // check if unicode is not empty - if(Code > 0x20 && Code != 0xA0 && Code != 0x034F && (Code < 0x2000 || Code > 0x200F) && (Code < 0x2028 || Code > 0x202F) && + if (Code > 0x20 && Code != 0xA0 && Code != 0x034F && (Code < 0x2000 || Code > 0x200F) && (Code < 0x2028 || Code > 0x202F) && (Code < 0x205F || Code > 0x2064) && (Code < 0x206A || Code > 0x206F) && (Code < 0xFE00 || Code > 0xFE0F) && Code != 0xFEFF && (Code < 0xFFF9 || Code > 0xFFFC)) { pEnd = 0; } - else if(pEnd == 0) + else if (pEnd == 0) pEnd = pStrOld; - if(++Length >= 127) + if (++Length >= 127) { *(const_cast(p)) = 0; break; } - } - if(pEnd != 0) + } + if (pEnd != 0) *(const_cast(pEnd)) = 0; // drop empty and autocreated spam messages (more than 16 characters per second) - if(Length == 0 || (g_Config.m_SvSpamprotection && pPlayer->m_LastChat && pPlayer->m_LastChat+Server()->TickSpeed()*((15+Length)/16) > Server()->Tick())) + if (Length == 0 || (g_Config.m_SvSpamprotection && pPlayer->m_LastChat && pPlayer->m_LastChat + Server()->TickSpeed() * ((15 + Length) / 16) > Server()->Tick())) return; bool SendToTeam = true; bool SkipSending = false; - + pPlayer->m_LastChat = Server()->Tick(); // /help /weapon /smth - if ( strcmp(pMsg->m_pMessage, "/help") == 0 || strcmp(pMsg->m_pMessage, "/info") == 0) + if (strcmp(pMsg->m_pMessage, "/help") == 0 || strcmp(pMsg->m_pMessage, "/info") == 0) { SendChatTarget(ClientID, "Engine version 1.54"); SendChatTarget(ClientID, ""); @@ -952,7 +910,7 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) SendChatTarget(ClientID, "For updates and more info check teeworlds.com/forum"); SkipSending = true; } - + // /help /weapon /smth if (strcmp(pMsg->m_pMessage, "/cmdlist") == 0 || strcmp(pMsg->m_pMessage, "/cmd") == 0 || strcmp(pMsg->m_pMessage, "/commands") == 0) { @@ -960,52 +918,51 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) SendChatTarget(ClientID, "/dwc - Disable / enable weapon chat (Using: ...)"); SendChatTarget(ClientID, "/dwb - Disable / enable weapon broadcast"); SendChatTarget(ClientID, "/das - Disable / enable auto spectating"); - //SendChatTarget(ClientID, "/deg - Disable / enable throwing grenades with emoticons"); - //SendChatTarget(ClientID, "/grenade - Throw a grenade"); + // SendChatTarget(ClientID, "/deg - Disable / enable throwing grenades with emoticons"); + // SendChatTarget(ClientID, "/grenade - Throw a grenade"); SkipSending = true; } - + /* if (strcmp(pMsg->m_pMessage, "/grenade") == 0) { if (pPlayer->GetCharacter()) pPlayer->GetCharacter()->ThrowGrenade(); - + SkipSending = true; } */ - + if (strcmp(pMsg->m_pMessage, "/dwc") == 0) { if (pPlayer->m_EnableWeaponInfo == 1) pPlayer->m_EnableWeaponInfo = 0; else pPlayer->m_EnableWeaponInfo = 1; - + if (pPlayer->m_EnableWeaponInfo) SendChatTarget(ClientID, "Weapon chat info messages enabled"); else SendChatTarget(ClientID, "Weapon chat info messages disabled"); - + SkipSending = true; } - + if (strcmp(pMsg->m_pMessage, "/dwb") == 0) { if (pPlayer->m_EnableWeaponInfo == 2) pPlayer->m_EnableWeaponInfo = 0; else pPlayer->m_EnableWeaponInfo = 2; - + if (pPlayer->m_EnableWeaponInfo) SendChatTarget(ClientID, "Weapon broadcast info messages enabled"); else SendChatTarget(ClientID, "Weapon broadcast info messages disabled"); - + SkipSending = true; } - - + if (strcmp(pMsg->m_pMessage, "/deg") == 0) { pPlayer->m_EnableEmoticonGrenades = !pPlayer->m_EnableEmoticonGrenades; @@ -1013,10 +970,10 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) SendChatTarget(ClientID, "Throwing grenades with emoticons enabled"); else SendChatTarget(ClientID, "Throwing grenades with emoticons disabled"); - + SkipSending = true; } - + if (strcmp(pMsg->m_pMessage, "/das") == 0) { pPlayer->m_EnableAutoSpectating = !pPlayer->m_EnableAutoSpectating; @@ -1024,44 +981,43 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) SendChatTarget(ClientID, "Auto spectating enabled"); else SendChatTarget(ClientID, "Auto spectating disabled"); - + SkipSending = true; } - - if ( strcmp(pMsg->m_pMessage, "/showwaypoints") == 0 ) + + if (strcmp(pMsg->m_pMessage, "/showwaypoints") == 0) { m_ShowWaypoints = !m_ShowWaypoints; SkipSending = true; } - - if ( strcmp(pMsg->m_pMessage, "/addbot") == 0 ) + + if (strcmp(pMsg->m_pMessage, "/addbot") == 0) { SendChatTarget(ClientID, "Adding bot..."); AddBot(); - + SkipSending = true; - } - + } + /* if ( strcmp(pMsg->m_pMessage, "/addbots") == 0 ) { SendChatTarget(ClientID, "Adding bots..."); for (int i = 0; i < 16; i++) AddBot(); - + SkipSending = true; - } - */ - - if ( strcmp(pMsg->m_pMessage, "/kickbots") == 0 ) + } + */ + + if (strcmp(pMsg->m_pMessage, "/kickbots") == 0) { SendChatTarget(ClientID, "Kicking bots..."); KickBots(); - + SkipSending = true; } - - + /* disable shopping with chat commands else if ( strcmp(pMsg->m_pMessage, "/buy") == 0 || strcmp(pMsg->m_pMessage, "/shop") == 0 || strcmp(pMsg->m_pMessage, "/upg") == 0) @@ -1069,7 +1025,7 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) pPlayer->ListBuyableWeapons(); //SendToTeam = false; } - + for (int i = 0; i < NUM_CUSTOMWEAPONS; i++) { if ( strcmp(pMsg->m_pMessage, aCustomWeapon[i].m_BuyCmd) == 0) @@ -1080,7 +1036,7 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) } } */ - + if (!SkipSending) { if (SendToTeam) @@ -1088,38 +1044,35 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) else SendChatTarget(ClientID, pMsg->m_pMessage); } - + /* */ - - - } - else if(MsgID == NETMSGTYPE_CL_CALLVOTE) + else if (MsgID == NETMSGTYPE_CL_CALLVOTE) { - if(g_Config.m_SvSpamprotection && pPlayer->m_LastVoteTry && pPlayer->m_LastVoteTry+Server()->TickSpeed()*1 > Server()->Tick()) + if (g_Config.m_SvSpamprotection && pPlayer->m_LastVoteTry && pPlayer->m_LastVoteTry + Server()->TickSpeed() * 1 > Server()->Tick()) return; int64 Now = Server()->Tick(); pPlayer->m_LastVoteTry = Now; - if(pPlayer->GetTeam() == TEAM_SPECTATORS) + if (pPlayer->GetTeam() == TEAM_SPECTATORS) { SendChatTarget(ClientID, "Spectators aren't allowed to start a vote."); return; } - if(m_VoteCloseTime) + if (m_VoteCloseTime) { SendChatTarget(ClientID, "Wait for current vote to end before calling a new one."); return; } - int Timeleft = pPlayer->m_LastVoteCall + Server()->TickSpeed()*60 - Now; - if(pPlayer->m_LastVoteCall && Timeleft > 0) + int Timeleft = pPlayer->m_LastVoteCall + Server()->TickSpeed() * 60 - Now; + if (pPlayer->m_LastVoteCall && Timeleft > 0) { char aChatmsg[512] = {0}; - str_format(aChatmsg, sizeof(aChatmsg), "You must wait %d seconds before making another vote", (Timeleft/Server()->TickSpeed())+1); + str_format(aChatmsg, sizeof(aChatmsg), "You must wait %d seconds before making another vote", (Timeleft / Server()->TickSpeed()) + 1); SendChatTarget(ClientID, aChatmsg); return; } @@ -1130,16 +1083,16 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) CNetMsg_Cl_CallVote *pMsg = (CNetMsg_Cl_CallVote *)pRawMsg; const char *pReason = pMsg->m_Reason[0] ? pMsg->m_Reason : "No reason given"; - if(str_comp_nocase(pMsg->m_Type, "option") == 0) + if (str_comp_nocase(pMsg->m_Type, "option") == 0) { CVoteOptionServer *pOption = m_pVoteOptionFirst; - - while(pOption) + + while (pOption) { - if(str_comp_nocase(pMsg->m_Value, pOption->m_aDescription) == 0) + if (str_comp_nocase(pMsg->m_Value, pOption->m_aDescription) == 0) { str_format(aChatmsg, sizeof(aChatmsg), "'%s' called vote to change server option '%s' (%s)", Server()->ClientName(ClientID), - pOption->m_aDescription, pReason); + pOption->m_aDescription, pReason); str_format(aDesc, sizeof(aDesc), "%s", pOption->m_aDescription); str_format(aCmd, sizeof(aCmd), "%s", pOption->m_aCommand); break; @@ -1148,33 +1101,33 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) pOption = pOption->m_pNext; } - if(!pOption) + if (!pOption) { - //str_format(aDesc, sizeof(aDesc), "%s", pOption->m_aDescription); - //str_format(aCmd, sizeof(aCmd), "%s", pOption->m_aCommand); - + // str_format(aDesc, sizeof(aDesc), "%s", pOption->m_aDescription); + // str_format(aCmd, sizeof(aCmd), "%s", pOption->m_aCommand); + str_format(aChatmsg, sizeof(aChatmsg), "'%s' isn't an option on this server", pMsg->m_Value); SendChatTarget(ClientID, aChatmsg); - + return; } } - else if(str_comp_nocase(pMsg->m_Type, "kick") == 0) + else if (str_comp_nocase(pMsg->m_Type, "kick") == 0) { - if(!g_Config.m_SvVoteKick) + if (!g_Config.m_SvVoteKick) { SendChatTarget(ClientID, "Server does not allow voting to kick players"); return; } - if(g_Config.m_SvVoteKickMin) + if (g_Config.m_SvVoteKickMin) { int PlayerNum = 0; - for(int i = 0; i < MAX_CLIENTS; ++i) - if(m_apPlayers[i] && m_apPlayers[i]->GetTeam() != TEAM_SPECTATORS) + for (int i = 0; i < MAX_CLIENTS; ++i) + if (m_apPlayers[i] && m_apPlayers[i]->GetTeam() != TEAM_SPECTATORS) ++PlayerNum; - if(PlayerNum < g_Config.m_SvVoteKickMin) + if (PlayerNum < g_Config.m_SvVoteKickMin) { str_format(aChatmsg, sizeof(aChatmsg), "Kick voting requires %d players on the server", g_Config.m_SvVoteKickMin); SendChatTarget(ClientID, aChatmsg); @@ -1183,17 +1136,17 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) } int KickID = str_toint(pMsg->m_Value); - if(KickID < 0 || !m_apPlayers[KickID]) + if (KickID < 0 || !m_apPlayers[KickID]) { SendChatTarget(ClientID, "Invalid client id to kick"); return; } - if(KickID == ClientID) + if (KickID == ClientID) { SendChatTarget(ClientID, "You can't kick yourself"); return; } - if(Server()->IsAuthed(KickID)) + if (Server()->IsAuthed(KickID)) { SendChatTarget(ClientID, "You can't kick admins"); char aBufKick[128]; @@ -1213,21 +1166,21 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) str_format(aCmd, sizeof(aCmd), "ban %s %d Banned by vote", aAddrStr, g_Config.m_SvVoteKickBantime); } } - else if(str_comp_nocase(pMsg->m_Type, "spectate") == 0) + else if (str_comp_nocase(pMsg->m_Type, "spectate") == 0) { - if(!g_Config.m_SvVoteSpectate) + if (!g_Config.m_SvVoteSpectate) { SendChatTarget(ClientID, "Server does not allow voting to move players to spectators"); return; } int SpectateID = str_toint(pMsg->m_Value); - if(SpectateID < 0 || SpectateID >= MAX_CLIENTS || !m_apPlayers[SpectateID] || m_apPlayers[SpectateID]->GetTeam() == TEAM_SPECTATORS) + if (SpectateID < 0 || SpectateID >= MAX_CLIENTS || !m_apPlayers[SpectateID] || m_apPlayers[SpectateID]->GetTeam() == TEAM_SPECTATORS) { SendChatTarget(ClientID, "Invalid client id to move"); return; } - if(SpectateID == ClientID) + if (SpectateID == ClientID) { SendChatTarget(ClientID, "You can't move yourself"); return; @@ -1237,13 +1190,12 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) str_format(aDesc, sizeof(aDesc), "move '%s' to spectators", Server()->ClientName(SpectateID)); str_format(aCmd, sizeof(aCmd), "set_team %d -1 %d", SpectateID, g_Config.m_SvVoteSpectateRejoindelay); } - + // do nothing - if(str_comp(aCmd, "null") == 0) + if (str_comp(aCmd, "null") == 0) { return; } - // class selecting for (int i = 0; i < NUM_CLASSES; i++) @@ -1263,18 +1215,17 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) if (str_comp(aCmd, aAbilities[i].m_aName) == 0) { if (m_apPlayers[ClientID]->SelectAbility(i)) - //char aBuf[256]; - //str_format(aBuf, sizeof(aBuf), "Not enough money for %s", aCustomWeapon[CustomWeapon].m_Name); + // char aBuf[256]; + // str_format(aBuf, sizeof(aBuf), "Not enough money for %s", aCustomWeapon[CustomWeapon].m_Name); SendChatTarget(ClientID, aAbilities[i].m_aChatMsg); else SendChatTarget(ClientID, "Not enough ability points."); - + ResetVotes(); return; } } - // buying for (int i = 0; i < NUM_CUSTOMWEAPONS; i++) { @@ -1285,9 +1236,8 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) return; } } - - - if(aCmd[0]) + + if (aCmd[0]) { SendChat(-1, CGameContext::CHAT_ALL, aChatmsg); StartVote(aDesc, aCmd, pReason); @@ -1297,15 +1247,15 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) pPlayer->m_LastVoteCall = Now; } } - else if(MsgID == NETMSGTYPE_CL_VOTE) + else if (MsgID == NETMSGTYPE_CL_VOTE) { - if(!m_VoteCloseTime) + if (!m_VoteCloseTime) return; - if(pPlayer->m_Vote == 0) + if (pPlayer->m_Vote == 0) { CNetMsg_Cl_Vote *pMsg = (CNetMsg_Cl_Vote *)pRawMsg; - if(!pMsg->m_Vote) + if (!pMsg->m_Vote) return; pPlayer->m_Vote = pMsg->m_Vote; @@ -1317,19 +1267,17 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) { CNetMsg_Cl_SetTeam *pMsg = (CNetMsg_Cl_SetTeam *)pRawMsg; - if(pPlayer->GetTeam() == pMsg->m_Team || (g_Config.m_SvSpamprotection && pPlayer->m_LastSetTeam && pPlayer->m_LastSetTeam+Server()->TickSpeed()*1 > Server()->Tick())) + if (pPlayer->GetTeam() == pMsg->m_Team || (g_Config.m_SvSpamprotection && pPlayer->m_LastSetTeam && pPlayer->m_LastSetTeam + Server()->TickSpeed() * 1 > Server()->Tick())) return; - - + pPlayer->m_LastSetTeam = Server()->Tick(); - if(pPlayer->GetTeam() == TEAM_SPECTATORS || pMsg->m_Team == TEAM_SPECTATORS) + if (pPlayer->GetTeam() == TEAM_SPECTATORS || pMsg->m_Team == TEAM_SPECTATORS) m_VoteUpdate = true; pPlayer->SetTeam(pMsg->m_Team); pPlayer->m_TeamChangeTick = Server()->Tick(); - - - //pPlayer->m_WantedTeam = pMsg->m_Team; - + + // pPlayer->m_WantedTeam = pMsg->m_Team; + /* if(pMsg->m_Team != TEAM_SPECTATORS) { @@ -1342,8 +1290,7 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) pPlayer->m_WantedTeam = pMsg->m_Team; } */ - - + /* 1.4 way pPlayer->m_LastSetTeam = Server()->Tick(); if(str_comp(g_Config.m_SvGametype, "cstt") == 0) @@ -1357,7 +1304,7 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) pPlayer->SetWantedTeam(pMsg->m_Team); } */ - + /* if(pMsg->m_Team != TEAM_SPECTATORS && m_LockTeams) { @@ -1377,7 +1324,7 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) SendBroadcast(aBuf, ClientID); return; } - + // Switch team on given client and kill/respawn him if(m_pController->CanJoinTeam(pMsg->m_Team, ClientID)) @@ -1406,19 +1353,19 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) { CNetMsg_Cl_SetSpectatorMode *pMsg = (CNetMsg_Cl_SetSpectatorMode *)pRawMsg; - if(pPlayer->GetTeam() != TEAM_SPECTATORS || pPlayer->m_SpectatorID == pMsg->m_SpectatorID || ClientID == pMsg->m_SpectatorID || - (g_Config.m_SvSpamprotection && pPlayer->m_LastSetSpectatorMode && pPlayer->m_LastSetSpectatorMode+Server()->TickSpeed()*1 > Server()->Tick())) + if (pPlayer->GetTeam() != TEAM_SPECTATORS || pPlayer->m_SpectatorID == pMsg->m_SpectatorID || ClientID == pMsg->m_SpectatorID || + (g_Config.m_SvSpamprotection && pPlayer->m_LastSetSpectatorMode && pPlayer->m_LastSetSpectatorMode + Server()->TickSpeed() * 1 > Server()->Tick())) return; pPlayer->m_LastSetSpectatorMode = Server()->Tick(); - if(pMsg->m_SpectatorID != SPEC_FREEVIEW && (!m_apPlayers[pMsg->m_SpectatorID] || m_apPlayers[pMsg->m_SpectatorID]->GetTeam() == TEAM_SPECTATORS)) + if (pMsg->m_SpectatorID != SPEC_FREEVIEW && (!m_apPlayers[pMsg->m_SpectatorID] || m_apPlayers[pMsg->m_SpectatorID]->GetTeam() == TEAM_SPECTATORS)) SendChatTarget(ClientID, "Invalid spectator id used"); else pPlayer->m_SpectatorID = pMsg->m_SpectatorID; } else if (MsgID == NETMSGTYPE_CL_CHANGEINFO) { - if(g_Config.m_SvSpamprotection && pPlayer->m_LastChangeInfo && pPlayer->m_LastChangeInfo+Server()->TickSpeed()*5 > Server()->Tick()) + if (g_Config.m_SvSpamprotection && pPlayer->m_LastChangeInfo && pPlayer->m_LastChangeInfo + Server()->TickSpeed() * 5 > Server()->Tick()) return; CNetMsg_Cl_ChangeInfo *pMsg = (CNetMsg_Cl_ChangeInfo *)pRawMsg; @@ -1428,7 +1375,7 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) char aOldName[MAX_NAME_LENGTH]; str_copy(aOldName, Server()->ClientName(ClientID), sizeof(aOldName)); Server()->SetClientName(ClientID, pMsg->m_pName); - if(str_comp(aOldName, Server()->ClientName(ClientID)) != 0) + if (str_comp(aOldName, Server()->ClientName(ClientID)) != 0) { char aChatText[256]; str_format(aChatText, sizeof(aChatText), "'%s' changed name to '%s'", aOldName, Server()->ClientName(ClientID)); @@ -1446,12 +1393,11 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) { CNetMsg_Cl_Emoticon *pMsg = (CNetMsg_Cl_Emoticon *)pRawMsg; - if(g_Config.m_SvSpamprotection && pPlayer->m_LastEmote && pPlayer->m_LastEmote+Server()->TickSpeed()*1 > Server()->Tick()) + if (g_Config.m_SvSpamprotection && pPlayer->m_LastEmote && pPlayer->m_LastEmote + Server()->TickSpeed() * 1 > Server()->Tick()) return; pPlayer->m_LastEmote = Server()->Tick(); - - + // throwing grenades /* //if (pMsg->m_Emoticon == EMOTICON_DEVILTEE) @@ -1462,7 +1408,7 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) } } */ - + if (pMsg->m_Emoticon == EMOTICON_HEARTS) { if (pPlayer->GetCharacter() && pPlayer->GotAbility(STORE_HEALTH)) @@ -1472,7 +1418,7 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) int Explode = 0; if (pPlayer->GotAbility(EXPLOSIVE_HEARTS)) Explode = 1; - + switch (pPlayer->GetCharacter()->m_HealthStored) { case 1: @@ -1488,7 +1434,7 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) m_pController->DropPickup(pPlayer->GetCharacter()->m_Pos+vec2(0, -32), POWERUP_HEALTH, vec2(5, -10.0f), Explode, pPlayer->GetCID()); break; }; - + pPlayer->GetCharacter()->m_HealthStored = 0; */ } @@ -1498,12 +1444,12 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) } else if (MsgID == NETMSGTYPE_CL_KILL && !m_World.m_Paused) { - if(pPlayer->m_LastKill && pPlayer->m_LastKill+Server()->TickSpeed()*1 > Server()->Tick()) + if (pPlayer->m_LastKill && pPlayer->m_LastKill + Server()->TickSpeed() * 1 > Server()->Tick()) return; - //pPlayer->m_LastKill = Server()->Tick(); - //pPlayer->KillCharacter(WEAPON_SELF); - + // pPlayer->m_LastKill = Server()->Tick(); + // pPlayer->KillCharacter(WEAPON_SELF); + // reload instead of self kill if (pPlayer->GetCharacter()) pPlayer->GetCharacter()->DoReloading(true); @@ -1511,9 +1457,9 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) } else { - if(MsgID == NETMSGTYPE_CL_STARTINFO) + if (MsgID == NETMSGTYPE_CL_STARTINFO) { - if(pPlayer->m_IsReady) + if (pPlayer->m_IsReady) return; CNetMsg_Cl_StartInfo *pMsg = (CNetMsg_Cl_StartInfo *)pRawMsg; @@ -1551,50 +1497,78 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) OptionMsg.m_pDescription13 = ""; OptionMsg.m_pDescription14 = ""; CVoteOptionServer *pCurrent = m_pVoteOptionFirst; - while(pCurrent) + while (pCurrent) { - switch(NumOptions++) + switch (NumOptions++) { - case 0: OptionMsg.m_pDescription0 = pCurrent->m_aDescription; break; - case 1: OptionMsg.m_pDescription1 = pCurrent->m_aDescription; break; - case 2: OptionMsg.m_pDescription2 = pCurrent->m_aDescription; break; - case 3: OptionMsg.m_pDescription3 = pCurrent->m_aDescription; break; - case 4: OptionMsg.m_pDescription4 = pCurrent->m_aDescription; break; - case 5: OptionMsg.m_pDescription5 = pCurrent->m_aDescription; break; - case 6: OptionMsg.m_pDescription6 = pCurrent->m_aDescription; break; - case 7: OptionMsg.m_pDescription7 = pCurrent->m_aDescription; break; - case 8: OptionMsg.m_pDescription8 = pCurrent->m_aDescription; break; - case 9: OptionMsg.m_pDescription9 = pCurrent->m_aDescription; break; - case 10: OptionMsg.m_pDescription10 = pCurrent->m_aDescription; break; - case 11: OptionMsg.m_pDescription11 = pCurrent->m_aDescription; break; - case 12: OptionMsg.m_pDescription12 = pCurrent->m_aDescription; break; - case 13: OptionMsg.m_pDescription13 = pCurrent->m_aDescription; break; + case 0: + OptionMsg.m_pDescription0 = pCurrent->m_aDescription; + break; + case 1: + OptionMsg.m_pDescription1 = pCurrent->m_aDescription; + break; + case 2: + OptionMsg.m_pDescription2 = pCurrent->m_aDescription; + break; + case 3: + OptionMsg.m_pDescription3 = pCurrent->m_aDescription; + break; + case 4: + OptionMsg.m_pDescription4 = pCurrent->m_aDescription; + break; + case 5: + OptionMsg.m_pDescription5 = pCurrent->m_aDescription; + break; + case 6: + OptionMsg.m_pDescription6 = pCurrent->m_aDescription; + break; + case 7: + OptionMsg.m_pDescription7 = pCurrent->m_aDescription; + break; + case 8: + OptionMsg.m_pDescription8 = pCurrent->m_aDescription; + break; + case 9: + OptionMsg.m_pDescription9 = pCurrent->m_aDescription; + break; + case 10: + OptionMsg.m_pDescription10 = pCurrent->m_aDescription; + break; + case 11: + OptionMsg.m_pDescription11 = pCurrent->m_aDescription; + break; + case 12: + OptionMsg.m_pDescription12 = pCurrent->m_aDescription; + break; + case 13: + OptionMsg.m_pDescription13 = pCurrent->m_aDescription; + break; case 14: - { - OptionMsg.m_pDescription14 = pCurrent->m_aDescription; - OptionMsg.m_NumOptions = NumOptions; - Server()->SendPackMsg(&OptionMsg, MSGFLAG_VITAL, ClientID); - OptionMsg = CNetMsg_Sv_VoteOptionListAdd(); - NumOptions = 0; - OptionMsg.m_pDescription1 = ""; - OptionMsg.m_pDescription2 = ""; - OptionMsg.m_pDescription3 = ""; - OptionMsg.m_pDescription4 = ""; - OptionMsg.m_pDescription5 = ""; - OptionMsg.m_pDescription6 = ""; - OptionMsg.m_pDescription7 = ""; - OptionMsg.m_pDescription8 = ""; - OptionMsg.m_pDescription9 = ""; - OptionMsg.m_pDescription10 = ""; - OptionMsg.m_pDescription11 = ""; - OptionMsg.m_pDescription12 = ""; - OptionMsg.m_pDescription13 = ""; - OptionMsg.m_pDescription14 = ""; - } + { + OptionMsg.m_pDescription14 = pCurrent->m_aDescription; + OptionMsg.m_NumOptions = NumOptions; + Server()->SendPackMsg(&OptionMsg, MSGFLAG_VITAL, ClientID); + OptionMsg = CNetMsg_Sv_VoteOptionListAdd(); + NumOptions = 0; + OptionMsg.m_pDescription1 = ""; + OptionMsg.m_pDescription2 = ""; + OptionMsg.m_pDescription3 = ""; + OptionMsg.m_pDescription4 = ""; + OptionMsg.m_pDescription5 = ""; + OptionMsg.m_pDescription6 = ""; + OptionMsg.m_pDescription7 = ""; + OptionMsg.m_pDescription8 = ""; + OptionMsg.m_pDescription9 = ""; + OptionMsg.m_pDescription10 = ""; + OptionMsg.m_pDescription11 = ""; + OptionMsg.m_pDescription12 = ""; + OptionMsg.m_pDescription13 = ""; + OptionMsg.m_pDescription14 = ""; + } } pCurrent = pCurrent->m_pNext; } - if(NumOptions > 0) + if (NumOptions > 0) { OptionMsg.m_NumOptions = NumOptions; Server()->SendPackMsg(&OptionMsg, MSGFLAG_VITAL, ClientID); @@ -1606,7 +1580,7 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) // client is ready to enter pPlayer->m_IsReady = true; CNetMsg_Sv_ReadyToEnter m; - Server()->SendPackMsg(&m, MSGFLAG_VITAL|MSGFLAG_FLUSH, ClientID); + Server()->SendPackMsg(&m, MSGFLAG_VITAL | MSGFLAG_FLUSH, ClientID); } } } @@ -1617,7 +1591,7 @@ void CGameContext::ConTuneParam(IConsole::IResult *pResult, void *pUserData) const char *pParamName = pResult->GetString(0); float NewValue = pResult->GetFloat(1); - if(pSelf->Tuning()->Set(pParamName, NewValue)) + if (pSelf->Tuning()->Set(pParamName, NewValue)) { char aBuf[256]; str_format(aBuf, sizeof(aBuf), "%s changed to %.2f", pParamName, NewValue); @@ -1641,7 +1615,7 @@ void CGameContext::ConTuneDump(IConsole::IResult *pResult, void *pUserData) { CGameContext *pSelf = (CGameContext *)pUserData; char aBuf[256]; - for(int i = 0; i < pSelf->Tuning()->Num(); i++) + for (int i = 0; i < pSelf->Tuning()->Num(); i++) { float v; pSelf->Tuning()->Get(i, &v); @@ -1654,7 +1628,7 @@ void CGameContext::ConPause(IConsole::IResult *pResult, void *pUserData) { CGameContext *pSelf = (CGameContext *)pUserData; - if(pSelf->m_pController->IsGameOver()) + if (pSelf->m_pController->IsGameOver()) return; pSelf->m_World.m_Paused ^= 1; @@ -1669,7 +1643,7 @@ void CGameContext::ConChangeMap(IConsole::IResult *pResult, void *pUserData) void CGameContext::ConRestart(IConsole::IResult *pResult, void *pUserData) { CGameContext *pSelf = (CGameContext *)pUserData; - if(pResult->NumArguments()) + if (pResult->NumArguments()) pSelf->m_pController->DoWarmup(pResult->GetInteger(0)); else pSelf->m_pController->StartRound(); @@ -1690,17 +1664,17 @@ void CGameContext::ConSay(IConsole::IResult *pResult, void *pUserData) void CGameContext::ConSetTeam(IConsole::IResult *pResult, void *pUserData) { CGameContext *pSelf = (CGameContext *)pUserData; - int ClientID = clamp(pResult->GetInteger(0), 0, (int)MAX_CLIENTS-1); + int ClientID = clamp(pResult->GetInteger(0), 0, (int)MAX_CLIENTS - 1); int Team = clamp(pResult->GetInteger(1), -1, 1); - int Delay = pResult->NumArguments()>2 ? pResult->GetInteger(2) : 0; - if(!pSelf->m_apPlayers[ClientID]) + int Delay = pResult->NumArguments() > 2 ? pResult->GetInteger(2) : 0; + if (!pSelf->m_apPlayers[ClientID]) return; char aBuf[256]; str_format(aBuf, sizeof(aBuf), "moved client %d to team %d", ClientID, Team); pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf); - pSelf->m_apPlayers[ClientID]->m_TeamChangeTick = pSelf->Server()->Tick()+pSelf->Server()->TickSpeed()*Delay*60; + pSelf->m_apPlayers[ClientID]->m_TeamChangeTick = pSelf->Server()->Tick() + pSelf->Server()->TickSpeed() * Delay * 60; pSelf->m_apPlayers[ClientID]->SetTeam(Team); (void)pSelf->m_pController->CheckTeamBalance(); } @@ -1714,8 +1688,8 @@ void CGameContext::ConSetTeamAll(IConsole::IResult *pResult, void *pUserData) str_format(aBuf, sizeof(aBuf), "All players were moved to the %s", pSelf->m_pController->GetTeamName(Team)); pSelf->SendChat(-1, CGameContext::CHAT_ALL, aBuf); - for(int i = 0; i < MAX_CLIENTS; ++i) - if(pSelf->m_apPlayers[i]) + for (int i = 0; i < MAX_CLIENTS; ++i) + if (pSelf->m_apPlayers[i]) pSelf->m_apPlayers[i]->SetTeam(Team, false); (void)pSelf->m_pController->CheckTeamBalance(); @@ -1730,45 +1704,16 @@ void CGameContext::ConSwapTeams(IConsole::IResult *pResult, void *pUserData) void CGameContext::ConShuffleTeams(IConsole::IResult *pResult, void *pUserData) { CGameContext *pSelf = (CGameContext *)pUserData; - if(!pSelf->m_pController->IsTeamplay()) + if (!pSelf->m_pController->IsTeamplay()) return; - int CounterRed = 0; - int CounterBlue = 0; int PlayerTeam = 0; - for(int i = 0; i < MAX_CLIENTS; ++i) - if(pSelf->m_apPlayers[i] && pSelf->m_apPlayers[i]->GetTeam() != TEAM_SPECTATORS) + for (int i = 0; i < MAX_CLIENTS; ++i) + if (pSelf->m_apPlayers[i] && pSelf->m_apPlayers[i]->GetTeam() != TEAM_SPECTATORS) ++PlayerTeam; - PlayerTeam = (PlayerTeam+1)/2; - - pSelf->SendChat(-1, CGameContext::CHAT_ALL, "Teams were shuffled"); - - /* - for(int i = 0; i < MAX_CLIENTS; ++i) - { - if(pSelf->m_apPlayers[i] && pSelf->m_apPlayers[i]->GetTeam() != TEAM_SPECTATORS) - { - if(CounterRed == PlayerTeam) - pSelf->m_apPlayers[i]->SetTeam(TEAM_BLUE, false); - else if(CounterBlue == PlayerTeam) - pSelf->m_apPlayers[i]->SetTeam(TEAM_RED, false); - else - { - if(rand() % 2) - { - pSelf->m_apPlayers[i]->SetTeam(TEAM_BLUE, false); - ++CounterBlue; - } - else - { - pSelf->m_apPlayers[i]->SetTeam(TEAM_RED, false); - ++CounterRed; - } - } - } - } - */ + PlayerTeam = (PlayerTeam + 1) / 2; + pSelf->SendChat(-1, CGameContext::CHAT_ALL, "Teams were shuffled"); (void)pSelf->m_pController->CheckTeamBalance(); } @@ -1776,7 +1721,7 @@ void CGameContext::ConLockTeams(IConsole::IResult *pResult, void *pUserData) { CGameContext *pSelf = (CGameContext *)pUserData; pSelf->m_LockTeams ^= 1; - if(pSelf->m_LockTeams) + if (pSelf->m_LockTeams) pSelf->SendChat(-1, CGameContext::CHAT_ALL, "Teams were locked"); else pSelf->SendChat(-1, CGameContext::CHAT_ALL, "Teams were unlocked"); @@ -1788,23 +1733,23 @@ void CGameContext::ConAddVote(IConsole::IResult *pResult, void *pUserData) const char *pDescription = pResult->GetString(0); const char *pCommand = pResult->GetString(1); - if(pSelf->m_NumVoteOptions == MAX_VOTE_OPTIONS) + if (pSelf->m_NumVoteOptions == MAX_VOTE_OPTIONS) { pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", "maximum number of vote options reached"); return; } // check for valid option - if(!pSelf->Console()->LineIsValid(pCommand) || str_length(pCommand) >= VOTE_CMD_LENGTH) + if (!pSelf->Console()->LineIsValid(pCommand) || str_length(pCommand) >= VOTE_CMD_LENGTH) { char aBuf[256]; str_format(aBuf, sizeof(aBuf), "skipped invalid command '%s'", pCommand); pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf); return; } - while(*pDescription && *pDescription == ' ') + while (*pDescription && *pDescription == ' ') pDescription++; - if(str_length(pDescription) >= VOTE_DESC_LENGTH || *pDescription == 0) + if (str_length(pDescription) >= VOTE_DESC_LENGTH || *pDescription == 0) { char aBuf[256]; str_format(aBuf, sizeof(aBuf), "skipped invalid option '%s'", pDescription); @@ -1814,9 +1759,9 @@ void CGameContext::ConAddVote(IConsole::IResult *pResult, void *pUserData) // check for duplicate entry CVoteOptionServer *pOption = pSelf->m_pVoteOptionFirst; - while(pOption) + while (pOption) { - if(str_comp_nocase(pDescription, pOption->m_aDescription) == 0) + if (str_comp_nocase(pDescription, pOption->m_aDescription) == 0) { char aBuf[256]; str_format(aBuf, sizeof(aBuf), "option '%s' already exists", pDescription); @@ -1833,14 +1778,14 @@ void CGameContext::ConAddVote(IConsole::IResult *pResult, void *pUserData) pOption = (CVoteOptionServer *)pSelf->m_pVoteOptionHeap->Allocate(sizeof(CVoteOptionServer) + Len); pOption->m_pNext = 0; pOption->m_pPrev = pSelf->m_pVoteOptionLast; - if(pOption->m_pPrev) + if (pOption->m_pPrev) pOption->m_pPrev->m_pNext = pOption; pSelf->m_pVoteOptionLast = pOption; - if(!pSelf->m_pVoteOptionFirst) + if (!pSelf->m_pVoteOptionFirst) pSelf->m_pVoteOptionFirst = pOption; str_copy(pOption->m_aDescription, pDescription, sizeof(pOption->m_aDescription)); - mem_copy(pOption->m_aCommand, pCommand, Len+1); + mem_copy(pOption->m_aCommand, pCommand, Len + 1); char aBuf[256]; str_format(aBuf, sizeof(aBuf), "added option '%s' '%s'", pOption->m_aDescription, pOption->m_aCommand); pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf); @@ -1858,13 +1803,13 @@ void CGameContext::ConRemoveVote(IConsole::IResult *pResult, void *pUserData) // check for valid option CVoteOptionServer *pOption = pSelf->m_pVoteOptionFirst; - while(pOption) + while (pOption) { - if(str_comp_nocase(pDescription, pOption->m_aDescription) == 0) + if (str_comp_nocase(pDescription, pOption->m_aDescription) == 0) break; pOption = pOption->m_pNext; } - if(!pOption) + if (!pOption) { char aBuf[256]; str_format(aBuf, sizeof(aBuf), "option '%s' does not exist", pDescription); @@ -1888,9 +1833,9 @@ void CGameContext::ConRemoveVote(IConsole::IResult *pResult, void *pUserData) CVoteOptionServer *pVoteOptionFirst = 0; CVoteOptionServer *pVoteOptionLast = 0; int NumVoteOptions = pSelf->m_NumVoteOptions; - for(CVoteOptionServer *pSrc = pSelf->m_pVoteOptionFirst; pSrc; pSrc = pSrc->m_pNext) + for (CVoteOptionServer *pSrc = pSelf->m_pVoteOptionFirst; pSrc; pSrc = pSrc->m_pNext) { - if(pSrc == pOption) + if (pSrc == pOption) continue; // copy option @@ -1898,14 +1843,14 @@ void CGameContext::ConRemoveVote(IConsole::IResult *pResult, void *pUserData) CVoteOptionServer *pDst = (CVoteOptionServer *)pVoteOptionHeap->Allocate(sizeof(CVoteOptionServer) + Len); pDst->m_pNext = 0; pDst->m_pPrev = pVoteOptionLast; - if(pDst->m_pPrev) + if (pDst->m_pPrev) pDst->m_pPrev->m_pNext = pDst; pVoteOptionLast = pDst; - if(!pVoteOptionFirst) + if (!pVoteOptionFirst) pVoteOptionFirst = pDst; str_copy(pDst->m_aDescription, pSrc->m_aDescription, sizeof(pDst->m_aDescription)); - mem_copy(pDst->m_aCommand, pSrc->m_aCommand, Len+1); + mem_copy(pDst->m_aCommand, pSrc->m_aCommand, Len + 1); } // clean up @@ -1924,12 +1869,12 @@ void CGameContext::ConForceVote(IConsole::IResult *pResult, void *pUserData) const char *pReason = pResult->NumArguments() > 2 && pResult->GetString(2)[0] ? pResult->GetString(2) : "No reason given"; char aBuf[128] = {0}; - if(str_comp_nocase(pType, "option") == 0) + if (str_comp_nocase(pType, "option") == 0) { CVoteOptionServer *pOption = pSelf->m_pVoteOptionFirst; - while(pOption) + while (pOption) { - if(str_comp_nocase(pValue, pOption->m_aDescription) == 0) + if (str_comp_nocase(pValue, pOption->m_aDescription) == 0) { str_format(aBuf, sizeof(aBuf), "admin forced server option '%s' (%s)", pValue, pReason); pSelf->SendChatTarget(-1, aBuf); @@ -1940,17 +1885,17 @@ void CGameContext::ConForceVote(IConsole::IResult *pResult, void *pUserData) pOption = pOption->m_pNext; } - if(!pOption) + if (!pOption) { str_format(aBuf, sizeof(aBuf), "'%s' isn't an option on this server", pValue); pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf); return; } } - else if(str_comp_nocase(pType, "kick") == 0) + else if (str_comp_nocase(pType, "kick") == 0) { int KickID = str_toint(pValue); - if(KickID < 0 || KickID >= MAX_CLIENTS || !pSelf->m_apPlayers[KickID]) + if (KickID < 0 || KickID >= MAX_CLIENTS || !pSelf->m_apPlayers[KickID]) { pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", "Invalid client id to kick"); return; @@ -1969,10 +1914,10 @@ void CGameContext::ConForceVote(IConsole::IResult *pResult, void *pUserData) pSelf->Console()->ExecuteLine(aBuf); } } - else if(str_comp_nocase(pType, "spectate") == 0) + else if (str_comp_nocase(pType, "spectate") == 0) { int SpectateID = str_toint(pValue); - if(SpectateID < 0 || SpectateID >= MAX_CLIENTS || !pSelf->m_apPlayers[SpectateID] || pSelf->m_apPlayers[SpectateID]->GetTeam() == TEAM_SPECTATORS) + if (SpectateID < 0 || SpectateID >= MAX_CLIENTS || !pSelf->m_apPlayers[SpectateID] || pSelf->m_apPlayers[SpectateID]->GetTeam() == TEAM_SPECTATORS) { pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", "Invalid client id to move"); return; @@ -2003,12 +1948,12 @@ void CGameContext::ConVote(IConsole::IResult *pResult, void *pUserData) CGameContext *pSelf = (CGameContext *)pUserData; // check if there is a vote running - if(!pSelf->m_VoteCloseTime) + if (!pSelf->m_VoteCloseTime) return; - if(str_comp_nocase(pResult->GetString(0), "yes") == 0) + if (str_comp_nocase(pResult->GetString(0), "yes") == 0) pSelf->m_VoteEnforce = CGameContext::VOTE_ENFORCE_YES; - else if(str_comp_nocase(pResult->GetString(0), "no") == 0) + else if (str_comp_nocase(pResult->GetString(0), "no") == 0) pSelf->m_VoteEnforce = CGameContext::VOTE_ENFORCE_NO; char aBuf[256]; str_format(aBuf, sizeof(aBuf), "admin forced vote %s", pResult->GetString(0)); @@ -2020,13 +1965,13 @@ void CGameContext::ConVote(IConsole::IResult *pResult, void *pUserData) void CGameContext::ConchainSpecialMotdupdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData) { pfnCallback(pResult, pCallbackUserData); - if(pResult->NumArguments()) + if (pResult->NumArguments()) { CNetMsg_Sv_Motd Msg; Msg.m_pMessage = g_Config.m_SvMotd; CGameContext *pSelf = (CGameContext *)pUserData; - for(int i = 0; i < MAX_CLIENTS; ++i) - if(pSelf->m_apPlayers[i] && !pSelf->IsBot(i)) + for (int i = 0; i < MAX_CLIENTS; ++i) + if (pSelf->m_apPlayers[i] && !pSelf->IsBot(i)) pSelf->Server()->SendPackMsg(&Msg, MSGFLAG_VITAL, i); } } @@ -2041,8 +1986,8 @@ void CGameContext::OnConsoleInit() Console()->Register("tune_dump", "", CFGFLAG_SERVER, ConTuneDump, this, "Dump tuning"); Console()->Register("pause", "", CFGFLAG_SERVER, ConPause, this, "Pause/unpause game"); - Console()->Register("change_map", "?r", CFGFLAG_SERVER|CFGFLAG_STORE, ConChangeMap, this, "Change map"); - Console()->Register("restart", "?i", CFGFLAG_SERVER|CFGFLAG_STORE, ConRestart, this, "Restart in x seconds (0 = abort)"); + Console()->Register("change_map", "?r", CFGFLAG_SERVER | CFGFLAG_STORE, ConChangeMap, this, "Change map"); + Console()->Register("restart", "?i", CFGFLAG_SERVER | CFGFLAG_STORE, ConRestart, this, "Restart in x seconds (0 = abort)"); Console()->Register("broadcast", "r", CFGFLAG_SERVER, ConBroadcast, this, "Broadcast message"); Console()->Register("say", "r", CFGFLAG_SERVER, ConSay, this, "Say in chat"); Console()->Register("set_team", "ii?i", CFGFLAG_SERVER, ConSetTeam, this, "Set team of player to team"); @@ -2067,75 +2012,71 @@ void CGameContext::OnInit(/*class IKernel *pKernel*/) m_World.SetGameServer(this); m_Events.SetGameServer(this); - //if(!data) // only load once - //data = load_data_from_memory(internal_data); + // if(!data) // only load once + // data = load_data_from_memory(internal_data); - for(int i = 0; i < NUM_NETOBJTYPES; i++) + for (int i = 0; i < NUM_NETOBJTYPES; i++) Server()->SnapSetStaticsize(i, m_NetObjHandler.GetObjSize(i)); m_Layers.Init(Kernel()); m_Collision.Init(&m_Layers); // reset everything here - //world = new GAMEWORLD; - //players = new CPlayer[MAX_CLIENTS]; + // world = new GAMEWORLD; + // players = new CPlayer[MAX_CLIENTS]; // select gametype - if(str_comp(g_Config.m_SvGametype, "mod") == 0) + if (str_comp(g_Config.m_SvGametype, "mod") == 0) m_pController = new CGameControllerMOD(this); - else if(str_comp(g_Config.m_SvGametype, "ctf") == 0) + else if (str_comp(g_Config.m_SvGametype, "ctf") == 0) m_pController = new CGameControllerCTF(this); - else if(str_comp(g_Config.m_SvGametype, "dom") == 0) + else if (str_comp(g_Config.m_SvGametype, "dom") == 0) m_pController = new CGameControllerDOM(this); - else if(str_comp(g_Config.m_SvGametype, "tdm") == 0) + else if (str_comp(g_Config.m_SvGametype, "tdm") == 0) m_pController = new CGameControllerTDM(this); - else if(str_comp(g_Config.m_SvGametype, "cstt") == 0) + else if (str_comp(g_Config.m_SvGametype, "cstt") == 0) m_pController = new CGameControllerCSTT(this); - else if(str_comp(g_Config.m_SvGametype, "csbb") == 0) + else if (str_comp(g_Config.m_SvGametype, "csbb") == 0) m_pController = new CGameControllerCSBB(this); else m_pController = new CGameControllerDM(this); // setup core world - //for(int i = 0; i < MAX_CLIENTS; i++) + // for(int i = 0; i < MAX_CLIENTS; i++) // game.players[i].core.world = &game.world.core; // create all entities from the game layer CMapItemLayerTilemap *pTileMap = m_Layers.GameLayer(); CTile *pTiles = (CTile *)Kernel()->RequestInterface()->GetData(pTileMap->m_Data); - - - /* num_spawn_points[0] = 0; num_spawn_points[1] = 0; num_spawn_points[2] = 0; */ - for(int y = 0; y < pTileMap->m_Height; y++) + for (int y = 0; y < pTileMap->m_Height; y++) { - for(int x = 0; x < pTileMap->m_Width; x++) + for (int x = 0; x < pTileMap->m_Width; x++) { - int Index = pTiles[y*pTileMap->m_Width+x].m_Index; + int Index = pTiles[y * pTileMap->m_Width + x].m_Index; - if(Index >= ENTITY_OFFSET) + if (Index >= ENTITY_OFFSET) { - vec2 Pos(x*32.0f+16.0f, y*32.0f+16.0f); - m_pController->OnEntity(Index-ENTITY_OFFSET, Pos); + vec2 Pos(x * 32.0f + 16.0f, y * 32.0f + 16.0f); + m_pController->OnEntity(Index - ENTITY_OFFSET, Pos); } } } - - //game.world.insert_entity(game.Controller); + // game.world.insert_entity(game.Controller); + + // SetupVotes(-1); - //SetupVotes(-1); - /* CNetMsg_Sv_VoteClearOptions VoteClearOptionsMsg; Server()->SendPackMsg(&VoteClearOptionsMsg, MSGFLAG_VITAL, -1); - + m_pVoteOptionHeap->Reset(); m_pVoteOptionFirst = 0; m_pVoteOptionLast = 0; @@ -2163,39 +2104,36 @@ enum VoteTypes VOTE_ABILITYDESC, }; - void CGameContext::ResetVotes() { CNetMsg_Sv_VoteClearOptions VoteClearOptionsMsg; Server()->SendPackMsg(&VoteClearOptionsMsg, MSGFLAG_VITAL, -1); - + m_pVoteOptionHeap->Reset(); m_pVoteOptionFirst = 0; m_pVoteOptionLast = 0; m_NumVoteOptions = 0; - - if (g_Config.m_SvAbilities) { AddCustomVote("PICK A CLASS", "null", VOTE_CLASSDESC); for (int i = 0; i < NUM_CLASSES; i++) AddCustomVote(aClassName[i], aClassName[i], VOTE_CLASS, i); AddCustomVote("", "null", VOTE_CLASSDESC); - + AddCustomVote("", "null", VOTE_ABILITYPOINTS); // Pick abilities - x point(s) left for (int i = 0; i < NUM_ABILITIES; i++) AddCustomVote(aAbilities[i].m_aName, aAbilities[i].m_aName, VOTE_ABILITY, i); AddCustomVote("", "null", VOTE_ABILITYDESC); } - + if (!g_Config.m_SvRandomWeapons) { AddCustomVote("", "null", VOTE_MONEY); // Money: x - //AddCustomVote("", "null", VOTE_WEAPONDESC); - - //AddCustomVote("Buy & Upgrade:", "null", VOTE_WEAPONDESC); - + // AddCustomVote("", "null", VOTE_WEAPONDESC); + + // AddCustomVote("Buy & Upgrade:", "null", VOTE_WEAPONDESC); + for (int i = 0; i < NUM_CUSTOMWEAPONS; i++) { if (aCustomWeapon[i].m_Cost > 0) @@ -2209,45 +2147,41 @@ void CGameContext::ResetVotes() } } - - void CGameContext::ClearShopVotes(int ClientID) { if (str_comp(g_Config.m_SvGametype, "csbb") == 0) return; - + CNetMsg_Sv_VoteClearOptions VoteClearOptionsMsg; Server()->SendPackMsg(&VoteClearOptionsMsg, MSGFLAG_VITAL, ClientID); - + /* char aBuf[256]; str_format(aBuf, sizeof(aBuf), "Can't shop right now"); - + CNetMsg_Sv_VoteOptionAdd OptionMsg; OptionMsg.m_pDescription = aBuf; Server()->SendPackMsg(&OptionMsg, MSGFLAG_VITAL, ClientID); */ } - - -void CGameContext::AddCustomVote(const char * Desc, const char * Cmd, int Type, int WeaponIndex) -{ - if(m_NumVoteOptions == MAX_VOTE_OPTIONS) +void CGameContext::AddCustomVote(const char *Desc, const char *Cmd, int Type, int WeaponIndex) +{ + if (m_NumVoteOptions == MAX_VOTE_OPTIONS) { Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "pseudovote", "ERROR - MAX_VOTE_OPTIONS REACHED! (did you really reach 128?)"); return; } // check for valid option" - if(str_length(Cmd) >= VOTE_CMD_LENGTH) + if (str_length(Cmd) >= VOTE_CMD_LENGTH) { char aBuf[256]; str_format(aBuf, sizeof(aBuf), "skipped invalid command '%s'", Cmd); Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "pseudovote", aBuf); return; } - while(*Desc && *Desc == ' ') + while (*Desc && *Desc == ' ') Desc++; CVoteOptionServer *pOption = m_pVoteOptionFirst; @@ -2259,56 +2193,53 @@ void CGameContext::AddCustomVote(const char * Desc, const char * Cmd, int Type, pOption = (CVoteOptionServer *)this->m_pVoteOptionHeap->Allocate(sizeof(CVoteOptionServer) + Len); pOption->m_pNext = 0; pOption->m_pPrev = m_pVoteOptionLast; - if(pOption->m_pPrev) + if (pOption->m_pPrev) pOption->m_pPrev->m_pNext = pOption; m_pVoteOptionLast = pOption; - if(!m_pVoteOptionFirst) + if (!m_pVoteOptionFirst) m_pVoteOptionFirst = pOption; str_copy(pOption->m_aDescription, Desc, sizeof(pOption->m_aDescription)); - mem_copy(pOption->m_aCommand, Cmd, Len+1); + mem_copy(pOption->m_aCommand, Cmd, Len + 1); char aBuf[256]; str_format(aBuf, sizeof(aBuf), "added option '%s' '%s'", pOption->m_aDescription, pOption->m_aCommand); - //Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "pseudovote", aBuf); + // Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "pseudovote", aBuf); - - if (Type == VOTE_MONEY) { CNetMsg_Sv_VoteOptionAdd OptionMsg; - + for (int i = 0; i < MAX_CLIENTS; i++) { if (m_apPlayers[i] && !IsBot(i)) { char aBuf[256]; - + if (m_apPlayers[i]->m_CanShop || str_comp(g_Config.m_SvGametype, "csbb") == 0) str_format(aBuf, sizeof(aBuf), "WEAPON SHOP - money: ♪%d ", m_apPlayers[i]->m_Money); else str_format(aBuf, sizeof(aBuf), "Can't shop right now"); - + str_copy(pOption->m_aDescription, aBuf, sizeof(pOption->m_aDescription)); - + OptionMsg.m_pDescription = pOption->m_aDescription; Server()->SendPackMsg(&OptionMsg, MSGFLAG_VITAL, i); } } } - - + if (Type == VOTE_WEAPON) { CNetMsg_Sv_VoteOptionAdd OptionMsg; OptionMsg.m_pDescription = pOption->m_aDescription; - + for (int i = 0; i < MAX_CLIENTS; i++) { if (m_apPlayers[i] && !IsBot(i)) { if (!m_apPlayers[i]->m_CanShop && str_comp(g_Config.m_SvGametype, "cstt") == 0) continue; - + if (m_apPlayers[i]->BuyableWeapon(WeaponIndex)) { Server()->SendPackMsg(&OptionMsg, MSGFLAG_VITAL, i); @@ -2316,89 +2247,86 @@ void CGameContext::AddCustomVote(const char * Desc, const char * Cmd, int Type, } } } - + // buy & upgrade text if (Type == VOTE_WEAPONDESC) { CNetMsg_Sv_VoteOptionAdd OptionMsg; OptionMsg.m_pDescription = pOption->m_aDescription; - + for (int i = 0; i < MAX_CLIENTS; i++) { if (m_apPlayers[i] && !IsBot(i)) { if (!m_apPlayers[i]->m_CanShop && str_comp(g_Config.m_SvGametype, "cstt") == 0) continue; - + Server()->SendPackMsg(&OptionMsg, MSGFLAG_VITAL, i); } } } - if (Type == VOTE_CLASSDESC || Type == VOTE_CLASS) { CNetMsg_Sv_VoteOptionAdd OptionMsg; OptionMsg.m_pDescription = pOption->m_aDescription; - + for (int i = 0; i < MAX_CLIENTS; i++) { if (m_apPlayers[i] && !IsBot(i)) { if (m_apPlayers[i]->GetClass() != -1) continue; - + Server()->SendPackMsg(&OptionMsg, MSGFLAG_VITAL, i); } } } - if (Type == VOTE_ABILITYDESC) { CNetMsg_Sv_VoteOptionAdd OptionMsg; OptionMsg.m_pDescription = pOption->m_aDescription; - + for (int i = 0; i < MAX_CLIENTS; i++) { if (m_apPlayers[i] && !IsBot(i)) { if (m_apPlayers[i]->GetClass() == -1 || !m_apPlayers[i]->GetAbilityPoints()) continue; - + Server()->SendPackMsg(&OptionMsg, MSGFLAG_VITAL, i); } } } - - + if (Type == VOTE_ABILITYPOINTS) { CNetMsg_Sv_VoteOptionAdd OptionMsg; - + for (int i = 0; i < MAX_CLIENTS; i++) { if (m_apPlayers[i] && !IsBot(i)) { int c = m_apPlayers[i]->GetClass(); - + if (c == -1) continue; - + int Points = m_apPlayers[i]->GetAbilityPoints(); - + if (!Points) continue; - + char aBuf[256]; - + if (Points > 1) str_format(aBuf, sizeof(aBuf), "PICK ABILITIES - %d skill points left", Points); else str_format(aBuf, sizeof(aBuf), "PICK ABILITIES - 1 skill point left"); - + str_copy(pOption->m_aDescription, aBuf, sizeof(pOption->m_aDescription)); - + OptionMsg.m_pDescription = pOption->m_aDescription; Server()->SendPackMsg(&OptionMsg, MSGFLAG_VITAL, i); } @@ -2409,7 +2337,7 @@ void CGameContext::AddCustomVote(const char * Desc, const char * Cmd, int Type, { CNetMsg_Sv_VoteOptionAdd OptionMsg; OptionMsg.m_pDescription = pOption->m_aDescription; - + for (int i = 0; i < MAX_CLIENTS; i++) { if (m_apPlayers[i] && !IsBot(i)) @@ -2417,16 +2345,15 @@ void CGameContext::AddCustomVote(const char * Desc, const char * Cmd, int Type, int c = m_apPlayers[i]->GetClass(); if (c == -1) continue; - + if (!m_apPlayers[i]->GetAbilityPoints() || !m_apPlayers[i]->AbilityAvailable(WeaponIndex)) continue; - + Server()->SendPackMsg(&OptionMsg, MSGFLAG_VITAL, i); } } } - - + if (Type == VOTE_ALL) { CNetMsg_Sv_VoteOptionAdd OptionMsg; @@ -2435,26 +2362,23 @@ void CGameContext::AddCustomVote(const char * Desc, const char * Cmd, int Type, } } - - - -void CGameContext::AddVote(const char * Desc, const char * Cmd, int ClientID) +void CGameContext::AddVote(const char *Desc, const char *Cmd, int ClientID) { - if(m_NumVoteOptions == MAX_VOTE_OPTIONS) + if (m_NumVoteOptions == MAX_VOTE_OPTIONS) { Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "pseudovote", "ERROR - MAX_VOTE_OPTIONS REACHED! (did you really reach 128?)"); return; } // check for valid option" - if(/*!pSelf->Console()->LineIsValid(pCommand) || */str_length(Cmd) >= VOTE_CMD_LENGTH) + if (/*!pSelf->Console()->LineIsValid(pCommand) || */ str_length(Cmd) >= VOTE_CMD_LENGTH) { char aBuf[256]; str_format(aBuf, sizeof(aBuf), "skipped invalid command '%s'", Cmd); Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "pseudovote", aBuf); return; } - while(*Desc && *Desc == ' ') + while (*Desc && *Desc == ' ') Desc++; /*if(str_length(Desc) >= VOTE_DESC_LENGTH || *Desc == 0) { @@ -2464,8 +2388,8 @@ void CGameContext::AddVote(const char * Desc, const char * Cmd, int ClientID) return; }*/ - //Shall we keep this? -kiChris // no need - Kompl.exe - // check for duplicate entry + // Shall we keep this? -kiChris // no need - Kompl.exe + // check for duplicate entry CVoteOptionServer *pOption = m_pVoteOptionFirst; /*while(pOption) { @@ -2486,19 +2410,19 @@ void CGameContext::AddVote(const char * Desc, const char * Cmd, int ClientID) pOption = (CVoteOptionServer *)this->m_pVoteOptionHeap->Allocate(sizeof(CVoteOptionServer) + Len); pOption->m_pNext = 0; pOption->m_pPrev = m_pVoteOptionLast; - if(pOption->m_pPrev) + if (pOption->m_pPrev) pOption->m_pPrev->m_pNext = pOption; m_pVoteOptionLast = pOption; - if(!m_pVoteOptionFirst) + if (!m_pVoteOptionFirst) m_pVoteOptionFirst = pOption; str_copy(pOption->m_aDescription, Desc, sizeof(pOption->m_aDescription)); - mem_copy(pOption->m_aCommand, Cmd, Len+1); + mem_copy(pOption->m_aCommand, Cmd, Len + 1); char aBuf[256]; str_format(aBuf, sizeof(aBuf), "added option '%s' '%s'", pOption->m_aDescription, pOption->m_aCommand); - //Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "pseudovote", aBuf); + // Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "pseudovote", aBuf); - if(ClientID == -2) + if (ClientID == -2) return; // inform clients about added option CNetMsg_Sv_VoteOptionAdd OptionMsg; @@ -2506,11 +2430,6 @@ void CGameContext::AddVote(const char * Desc, const char * Cmd, int ClientID) Server()->SendPackMsg(&OptionMsg, MSGFLAG_VITAL, ClientID); } - - - - - void CGameContext::OnShutdown() { KickBots(); @@ -2523,22 +2442,22 @@ void CGameContext::OnSnap(int ClientID) { // add tuning to demo CTuningParams StandardTuning; - if(ClientID == -1 && Server()->DemoRecorder_IsRecording() && mem_comp(&StandardTuning, &m_Tuning, sizeof(CTuningParams)) != 0) + if (ClientID == -1 && Server()->DemoRecorder_IsRecording() && mem_comp(&StandardTuning, &m_Tuning, sizeof(CTuningParams)) != 0) { CMsgPacker Msg(NETMSGTYPE_SV_TUNEPARAMS); int *pParams = (int *)&m_Tuning; - for(unsigned i = 0; i < sizeof(m_Tuning)/sizeof(int); i++) + for (unsigned i = 0; i < sizeof(m_Tuning) / sizeof(int); i++) Msg.AddInt(pParams[i]); - Server()->SendMsg(&Msg, MSGFLAG_RECORD|MSGFLAG_NOSEND, ClientID); + Server()->SendMsg(&Msg, MSGFLAG_RECORD | MSGFLAG_NOSEND, ClientID); } m_World.Snap(ClientID); m_pController->Snap(ClientID); m_Events.Snap(ClientID); - for(int i = 0; i < MAX_CLIENTS; i++) + for (int i = 0; i < MAX_CLIENTS; i++) { - if(m_apPlayers[i]) + if (m_apPlayers[i]) m_apPlayers[i]->Snap(ClientID); } } @@ -2564,28 +2483,23 @@ const char *CGameContext::NetVersion() { return GAME_NETVERSION; } IGameServer *CreateGameServer() { return new CGameContext; } - - void CGameContext::KickBots() { Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "engine", "Kicking bots..."); - for(int i = 0; i < MAX_CLIENTS; i++) + for (int i = 0; i < MAX_CLIENTS; i++) { - if(IsBot(i)) + if (IsBot(i)) Server()->Kick(i, ""); } } - void CGameContext::KickBot(int ClientID) { - if(IsBot(ClientID)) + if (IsBot(ClientID)) Server()->Kick(ClientID, ""); } - - void CGameContext::AddBot() { Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "engine", "Adding a bot...");